* c-common.c (get_priority): Add check for
[official-gcc.git] / gcc / cp / parser.c
blobb8af4d288bd939b8949a4442d883947eef06d496
1 /* C++ Parser.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005 Free Software Foundation, Inc.
4 Written by Mark Mitchell <mark@codesourcery.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "dyn-string.h"
28 #include "varray.h"
29 #include "cpplib.h"
30 #include "tree.h"
31 #include "cp-tree.h"
32 #include "c-pragma.h"
33 #include "decl.h"
34 #include "flags.h"
35 #include "diagnostic.h"
36 #include "toplev.h"
37 #include "output.h"
38 #include "target.h"
39 #include "cgraph.h"
40 #include "c-common.h"
43 /* The lexer. */
45 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
46 and c-lex.c) and the C++ parser. */
48 /* A token's value and its associated deferred access checks and
49 qualifying scope. */
51 struct tree_check GTY(())
53 /* The value associated with the token. */
54 tree value;
55 /* The checks that have been associated with value. */
56 VEC (deferred_access_check, gc)* checks;
57 /* The token's qualifying scope (used when it is a
58 CPP_NESTED_NAME_SPECIFIER). */
59 tree qualifying_scope;
62 /* A C++ token. */
64 typedef struct cp_token GTY (())
66 /* The kind of token. */
67 ENUM_BITFIELD (cpp_ttype) type : 8;
68 /* If this token is a keyword, this value indicates which keyword.
69 Otherwise, this value is RID_MAX. */
70 ENUM_BITFIELD (rid) keyword : 8;
71 /* Token flags. */
72 unsigned char flags;
73 /* Identifier for the pragma. */
74 ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
75 /* True if this token is from a system header. */
76 BOOL_BITFIELD in_system_header : 1;
77 /* True if this token is from a context where it is implicitly extern "C" */
78 BOOL_BITFIELD implicit_extern_c : 1;
79 /* True for a CPP_NAME token that is not a keyword (i.e., for which
80 KEYWORD is RID_MAX) iff this name was looked up and found to be
81 ambiguous. An error has already been reported. */
82 BOOL_BITFIELD ambiguous_p : 1;
83 /* The input file stack index at which this token was found. */
84 unsigned input_file_stack_index : INPUT_FILE_STACK_BITS;
85 /* The value associated with this token, if any. */
86 union cp_token_value {
87 /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID. */
88 struct tree_check* GTY((tag ("1"))) tree_check_value;
89 /* Use for all other tokens. */
90 tree GTY((tag ("0"))) value;
91 } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
92 /* The location at which this token was found. */
93 location_t location;
94 } cp_token;
96 /* We use a stack of token pointer for saving token sets. */
97 typedef struct cp_token *cp_token_position;
98 DEF_VEC_P (cp_token_position);
99 DEF_VEC_ALLOC_P (cp_token_position,heap);
101 static const cp_token eof_token =
103 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, 0, 0, false, 0, { NULL },
104 #if USE_MAPPED_LOCATION
106 #else
107 {0, 0}
108 #endif
111 /* The cp_lexer structure represents the C++ lexer. It is responsible
112 for managing the token stream from the preprocessor and supplying
113 it to the parser. Tokens are never added to the cp_lexer after
114 it is created. */
116 typedef struct cp_lexer GTY (())
118 /* The memory allocated for the buffer. NULL if this lexer does not
119 own the token buffer. */
120 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
121 /* If the lexer owns the buffer, this is the number of tokens in the
122 buffer. */
123 size_t buffer_length;
125 /* A pointer just past the last available token. The tokens
126 in this lexer are [buffer, last_token). */
127 cp_token_position GTY ((skip)) last_token;
129 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
130 no more available tokens. */
131 cp_token_position GTY ((skip)) next_token;
133 /* A stack indicating positions at which cp_lexer_save_tokens was
134 called. The top entry is the most recent position at which we
135 began saving tokens. If the stack is non-empty, we are saving
136 tokens. */
137 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
139 /* The next lexer in a linked list of lexers. */
140 struct cp_lexer *next;
142 /* True if we should output debugging information. */
143 bool debugging_p;
145 /* True if we're in the context of parsing a pragma, and should not
146 increment past the end-of-line marker. */
147 bool in_pragma;
148 } cp_lexer;
150 /* cp_token_cache is a range of tokens. There is no need to represent
151 allocate heap memory for it, since tokens are never removed from the
152 lexer's array. There is also no need for the GC to walk through
153 a cp_token_cache, since everything in here is referenced through
154 a lexer. */
156 typedef struct cp_token_cache GTY(())
158 /* The beginning of the token range. */
159 cp_token * GTY((skip)) first;
161 /* Points immediately after the last token in the range. */
162 cp_token * GTY ((skip)) last;
163 } cp_token_cache;
165 /* Prototypes. */
167 static cp_lexer *cp_lexer_new_main
168 (void);
169 static cp_lexer *cp_lexer_new_from_tokens
170 (cp_token_cache *tokens);
171 static void cp_lexer_destroy
172 (cp_lexer *);
173 static int cp_lexer_saving_tokens
174 (const cp_lexer *);
175 static cp_token_position cp_lexer_token_position
176 (cp_lexer *, bool);
177 static cp_token *cp_lexer_token_at
178 (cp_lexer *, cp_token_position);
179 static void cp_lexer_get_preprocessor_token
180 (cp_lexer *, cp_token *);
181 static inline cp_token *cp_lexer_peek_token
182 (cp_lexer *);
183 static cp_token *cp_lexer_peek_nth_token
184 (cp_lexer *, size_t);
185 static inline bool cp_lexer_next_token_is
186 (cp_lexer *, enum cpp_ttype);
187 static bool cp_lexer_next_token_is_not
188 (cp_lexer *, enum cpp_ttype);
189 static bool cp_lexer_next_token_is_keyword
190 (cp_lexer *, enum rid);
191 static cp_token *cp_lexer_consume_token
192 (cp_lexer *);
193 static void cp_lexer_purge_token
194 (cp_lexer *);
195 static void cp_lexer_purge_tokens_after
196 (cp_lexer *, cp_token_position);
197 static void cp_lexer_save_tokens
198 (cp_lexer *);
199 static void cp_lexer_commit_tokens
200 (cp_lexer *);
201 static void cp_lexer_rollback_tokens
202 (cp_lexer *);
203 #ifdef ENABLE_CHECKING
204 static void cp_lexer_print_token
205 (FILE *, cp_token *);
206 static inline bool cp_lexer_debugging_p
207 (cp_lexer *);
208 static void cp_lexer_start_debugging
209 (cp_lexer *) ATTRIBUTE_UNUSED;
210 static void cp_lexer_stop_debugging
211 (cp_lexer *) ATTRIBUTE_UNUSED;
212 #else
213 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
214 about passing NULL to functions that require non-NULL arguments
215 (fputs, fprintf). It will never be used, so all we need is a value
216 of the right type that's guaranteed not to be NULL. */
217 #define cp_lexer_debug_stream stdout
218 #define cp_lexer_print_token(str, tok) (void) 0
219 #define cp_lexer_debugging_p(lexer) 0
220 #endif /* ENABLE_CHECKING */
222 static cp_token_cache *cp_token_cache_new
223 (cp_token *, cp_token *);
225 static void cp_parser_initial_pragma
226 (cp_token *);
228 /* Manifest constants. */
229 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
230 #define CP_SAVED_TOKEN_STACK 5
232 /* A token type for keywords, as opposed to ordinary identifiers. */
233 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
235 /* A token type for template-ids. If a template-id is processed while
236 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
237 the value of the CPP_TEMPLATE_ID is whatever was returned by
238 cp_parser_template_id. */
239 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
241 /* A token type for nested-name-specifiers. If a
242 nested-name-specifier is processed while parsing tentatively, it is
243 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
244 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
245 cp_parser_nested_name_specifier_opt. */
246 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
248 /* A token type for tokens that are not tokens at all; these are used
249 to represent slots in the array where there used to be a token
250 that has now been deleted. */
251 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
253 /* The number of token types, including C++-specific ones. */
254 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
256 /* Variables. */
258 #ifdef ENABLE_CHECKING
259 /* The stream to which debugging output should be written. */
260 static FILE *cp_lexer_debug_stream;
261 #endif /* ENABLE_CHECKING */
263 /* Create a new main C++ lexer, the lexer that gets tokens from the
264 preprocessor. */
266 static cp_lexer *
267 cp_lexer_new_main (void)
269 cp_token first_token;
270 cp_lexer *lexer;
271 cp_token *pos;
272 size_t alloc;
273 size_t space;
274 cp_token *buffer;
276 /* It's possible that parsing the first pragma will load a PCH file,
277 which is a GC collection point. So we have to do that before
278 allocating any memory. */
279 cp_parser_initial_pragma (&first_token);
281 /* Tell c_lex_with_flags not to merge string constants. */
282 c_lex_return_raw_strings = true;
284 c_common_no_more_pch ();
286 /* Allocate the memory. */
287 lexer = GGC_CNEW (cp_lexer);
289 #ifdef ENABLE_CHECKING
290 /* Initially we are not debugging. */
291 lexer->debugging_p = false;
292 #endif /* ENABLE_CHECKING */
293 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
294 CP_SAVED_TOKEN_STACK);
296 /* Create the buffer. */
297 alloc = CP_LEXER_BUFFER_SIZE;
298 buffer = GGC_NEWVEC (cp_token, alloc);
300 /* Put the first token in the buffer. */
301 space = alloc;
302 pos = buffer;
303 *pos = first_token;
305 /* Get the remaining tokens from the preprocessor. */
306 while (pos->type != CPP_EOF)
308 pos++;
309 if (!--space)
311 space = alloc;
312 alloc *= 2;
313 buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
314 pos = buffer + space;
316 cp_lexer_get_preprocessor_token (lexer, pos);
318 lexer->buffer = buffer;
319 lexer->buffer_length = alloc - space;
320 lexer->last_token = pos;
321 lexer->next_token = lexer->buffer_length ? buffer : (cp_token *)&eof_token;
323 /* Subsequent preprocessor diagnostics should use compiler
324 diagnostic functions to get the compiler source location. */
325 cpp_get_options (parse_in)->client_diagnostic = true;
326 cpp_get_callbacks (parse_in)->error = cp_cpp_error;
328 gcc_assert (lexer->next_token->type != CPP_PURGED);
329 return lexer;
332 /* Create a new lexer whose token stream is primed with the tokens in
333 CACHE. When these tokens are exhausted, no new tokens will be read. */
335 static cp_lexer *
336 cp_lexer_new_from_tokens (cp_token_cache *cache)
338 cp_token *first = cache->first;
339 cp_token *last = cache->last;
340 cp_lexer *lexer = GGC_CNEW (cp_lexer);
342 /* We do not own the buffer. */
343 lexer->buffer = NULL;
344 lexer->buffer_length = 0;
345 lexer->next_token = first == last ? (cp_token *)&eof_token : first;
346 lexer->last_token = last;
348 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
349 CP_SAVED_TOKEN_STACK);
351 #ifdef ENABLE_CHECKING
352 /* Initially we are not debugging. */
353 lexer->debugging_p = false;
354 #endif
356 gcc_assert (lexer->next_token->type != CPP_PURGED);
357 return lexer;
360 /* Frees all resources associated with LEXER. */
362 static void
363 cp_lexer_destroy (cp_lexer *lexer)
365 if (lexer->buffer)
366 ggc_free (lexer->buffer);
367 VEC_free (cp_token_position, heap, lexer->saved_tokens);
368 ggc_free (lexer);
371 /* Returns nonzero if debugging information should be output. */
373 #ifdef ENABLE_CHECKING
375 static inline bool
376 cp_lexer_debugging_p (cp_lexer *lexer)
378 return lexer->debugging_p;
381 #endif /* ENABLE_CHECKING */
383 static inline cp_token_position
384 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
386 gcc_assert (!previous_p || lexer->next_token != &eof_token);
388 return lexer->next_token - previous_p;
391 static inline cp_token *
392 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
394 return pos;
397 /* nonzero if we are presently saving tokens. */
399 static inline int
400 cp_lexer_saving_tokens (const cp_lexer* lexer)
402 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
405 /* Store the next token from the preprocessor in *TOKEN. Return true
406 if we reach EOF. */
408 static void
409 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
410 cp_token *token)
412 static int is_extern_c = 0;
414 /* Get a new token from the preprocessor. */
415 token->type
416 = c_lex_with_flags (&token->u.value, &token->location, &token->flags);
417 token->input_file_stack_index = input_file_stack_tick;
418 token->keyword = RID_MAX;
419 token->pragma_kind = PRAGMA_NONE;
420 token->in_system_header = in_system_header;
422 /* On some systems, some header files are surrounded by an
423 implicit extern "C" block. Set a flag in the token if it
424 comes from such a header. */
425 is_extern_c += pending_lang_change;
426 pending_lang_change = 0;
427 token->implicit_extern_c = is_extern_c > 0;
429 /* Check to see if this token is a keyword. */
430 if (token->type == CPP_NAME)
432 if (C_IS_RESERVED_WORD (token->u.value))
434 /* Mark this token as a keyword. */
435 token->type = CPP_KEYWORD;
436 /* Record which keyword. */
437 token->keyword = C_RID_CODE (token->u.value);
438 /* Update the value. Some keywords are mapped to particular
439 entities, rather than simply having the value of the
440 corresponding IDENTIFIER_NODE. For example, `__const' is
441 mapped to `const'. */
442 token->u.value = ridpointers[token->keyword];
444 else
446 if (warn_cxx0x_compat
447 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
448 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
450 /* Warn about the C++0x keyword (but still treat it as
451 an identifier). */
452 warning (OPT_Wc__0x_compat,
453 "identifier %<%s%> will become a keyword in C++0x",
454 IDENTIFIER_POINTER (token->u.value));
456 /* Clear out the C_RID_CODE so we don't warn about this
457 particular identifier-turned-keyword again. */
458 C_RID_CODE (token->u.value) = RID_MAX;
461 token->ambiguous_p = false;
462 token->keyword = RID_MAX;
465 /* Handle Objective-C++ keywords. */
466 else if (token->type == CPP_AT_NAME)
468 token->type = CPP_KEYWORD;
469 switch (C_RID_CODE (token->u.value))
471 /* Map 'class' to '@class', 'private' to '@private', etc. */
472 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
473 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
474 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
475 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
476 case RID_THROW: token->keyword = RID_AT_THROW; break;
477 case RID_TRY: token->keyword = RID_AT_TRY; break;
478 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
479 default: token->keyword = C_RID_CODE (token->u.value);
482 else if (token->type == CPP_PRAGMA)
484 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
485 token->pragma_kind = TREE_INT_CST_LOW (token->u.value);
486 token->u.value = NULL_TREE;
490 /* Update the globals input_location and in_system_header and the
491 input file stack from TOKEN. */
492 static inline void
493 cp_lexer_set_source_position_from_token (cp_token *token)
495 if (token->type != CPP_EOF)
497 input_location = token->location;
498 in_system_header = token->in_system_header;
499 restore_input_file_stack (token->input_file_stack_index);
503 /* Return a pointer to the next token in the token stream, but do not
504 consume it. */
506 static inline cp_token *
507 cp_lexer_peek_token (cp_lexer *lexer)
509 if (cp_lexer_debugging_p (lexer))
511 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
512 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
513 putc ('\n', cp_lexer_debug_stream);
515 return lexer->next_token;
518 /* Return true if the next token has the indicated TYPE. */
520 static inline bool
521 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
523 return cp_lexer_peek_token (lexer)->type == type;
526 /* Return true if the next token does not have the indicated TYPE. */
528 static inline bool
529 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
531 return !cp_lexer_next_token_is (lexer, type);
534 /* Return true if the next token is the indicated KEYWORD. */
536 static inline bool
537 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
539 return cp_lexer_peek_token (lexer)->keyword == keyword;
542 /* Return true if the next token is a keyword for a decl-specifier. */
544 static bool
545 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
547 cp_token *token;
549 token = cp_lexer_peek_token (lexer);
550 switch (token->keyword)
552 /* Storage classes. */
553 case RID_AUTO:
554 case RID_REGISTER:
555 case RID_STATIC:
556 case RID_EXTERN:
557 case RID_MUTABLE:
558 case RID_THREAD:
559 /* Elaborated type specifiers. */
560 case RID_ENUM:
561 case RID_CLASS:
562 case RID_STRUCT:
563 case RID_UNION:
564 case RID_TYPENAME:
565 /* Simple type specifiers. */
566 case RID_CHAR:
567 case RID_WCHAR:
568 case RID_BOOL:
569 case RID_SHORT:
570 case RID_INT:
571 case RID_LONG:
572 case RID_SIGNED:
573 case RID_UNSIGNED:
574 case RID_FLOAT:
575 case RID_DOUBLE:
576 case RID_VOID:
577 /* GNU extensions. */
578 case RID_ATTRIBUTE:
579 case RID_TYPEOF:
580 return true;
582 default:
583 return false;
587 /* Return a pointer to the Nth token in the token stream. If N is 1,
588 then this is precisely equivalent to cp_lexer_peek_token (except
589 that it is not inline). One would like to disallow that case, but
590 there is one case (cp_parser_nth_token_starts_template_id) where
591 the caller passes a variable for N and it might be 1. */
593 static cp_token *
594 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
596 cp_token *token;
598 /* N is 1-based, not zero-based. */
599 gcc_assert (n > 0);
601 if (cp_lexer_debugging_p (lexer))
602 fprintf (cp_lexer_debug_stream,
603 "cp_lexer: peeking ahead %ld at token: ", (long)n);
605 --n;
606 token = lexer->next_token;
607 gcc_assert (!n || token != &eof_token);
608 while (n != 0)
610 ++token;
611 if (token == lexer->last_token)
613 token = (cp_token *)&eof_token;
614 break;
617 if (token->type != CPP_PURGED)
618 --n;
621 if (cp_lexer_debugging_p (lexer))
623 cp_lexer_print_token (cp_lexer_debug_stream, token);
624 putc ('\n', cp_lexer_debug_stream);
627 return token;
630 /* Return the next token, and advance the lexer's next_token pointer
631 to point to the next non-purged token. */
633 static cp_token *
634 cp_lexer_consume_token (cp_lexer* lexer)
636 cp_token *token = lexer->next_token;
638 gcc_assert (token != &eof_token);
639 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
643 lexer->next_token++;
644 if (lexer->next_token == lexer->last_token)
646 lexer->next_token = (cp_token *)&eof_token;
647 break;
651 while (lexer->next_token->type == CPP_PURGED);
653 cp_lexer_set_source_position_from_token (token);
655 /* Provide debugging output. */
656 if (cp_lexer_debugging_p (lexer))
658 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
659 cp_lexer_print_token (cp_lexer_debug_stream, token);
660 putc ('\n', cp_lexer_debug_stream);
663 return token;
666 /* Permanently remove the next token from the token stream, and
667 advance the next_token pointer to refer to the next non-purged
668 token. */
670 static void
671 cp_lexer_purge_token (cp_lexer *lexer)
673 cp_token *tok = lexer->next_token;
675 gcc_assert (tok != &eof_token);
676 tok->type = CPP_PURGED;
677 tok->location = UNKNOWN_LOCATION;
678 tok->u.value = NULL_TREE;
679 tok->keyword = RID_MAX;
683 tok++;
684 if (tok == lexer->last_token)
686 tok = (cp_token *)&eof_token;
687 break;
690 while (tok->type == CPP_PURGED);
691 lexer->next_token = tok;
694 /* Permanently remove all tokens after TOK, up to, but not
695 including, the token that will be returned next by
696 cp_lexer_peek_token. */
698 static void
699 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
701 cp_token *peek = lexer->next_token;
703 if (peek == &eof_token)
704 peek = lexer->last_token;
706 gcc_assert (tok < peek);
708 for ( tok += 1; tok != peek; tok += 1)
710 tok->type = CPP_PURGED;
711 tok->location = UNKNOWN_LOCATION;
712 tok->u.value = NULL_TREE;
713 tok->keyword = RID_MAX;
717 /* Begin saving tokens. All tokens consumed after this point will be
718 preserved. */
720 static void
721 cp_lexer_save_tokens (cp_lexer* lexer)
723 /* Provide debugging output. */
724 if (cp_lexer_debugging_p (lexer))
725 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
727 VEC_safe_push (cp_token_position, heap,
728 lexer->saved_tokens, lexer->next_token);
731 /* Commit to the portion of the token stream most recently saved. */
733 static void
734 cp_lexer_commit_tokens (cp_lexer* lexer)
736 /* Provide debugging output. */
737 if (cp_lexer_debugging_p (lexer))
738 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
740 VEC_pop (cp_token_position, lexer->saved_tokens);
743 /* Return all tokens saved since the last call to cp_lexer_save_tokens
744 to the token stream. Stop saving tokens. */
746 static void
747 cp_lexer_rollback_tokens (cp_lexer* lexer)
749 /* Provide debugging output. */
750 if (cp_lexer_debugging_p (lexer))
751 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
753 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
756 /* Print a representation of the TOKEN on the STREAM. */
758 #ifdef ENABLE_CHECKING
760 static void
761 cp_lexer_print_token (FILE * stream, cp_token *token)
763 /* We don't use cpp_type2name here because the parser defines
764 a few tokens of its own. */
765 static const char *const token_names[] = {
766 /* cpplib-defined token types */
767 #define OP(e, s) #e,
768 #define TK(e, s) #e,
769 TTYPE_TABLE
770 #undef OP
771 #undef TK
772 /* C++ parser token types - see "Manifest constants", above. */
773 "KEYWORD",
774 "TEMPLATE_ID",
775 "NESTED_NAME_SPECIFIER",
776 "PURGED"
779 /* If we have a name for the token, print it out. Otherwise, we
780 simply give the numeric code. */
781 gcc_assert (token->type < ARRAY_SIZE(token_names));
782 fputs (token_names[token->type], stream);
784 /* For some tokens, print the associated data. */
785 switch (token->type)
787 case CPP_KEYWORD:
788 /* Some keywords have a value that is not an IDENTIFIER_NODE.
789 For example, `struct' is mapped to an INTEGER_CST. */
790 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
791 break;
792 /* else fall through */
793 case CPP_NAME:
794 fputs (IDENTIFIER_POINTER (token->u.value), stream);
795 break;
797 case CPP_STRING:
798 case CPP_WSTRING:
799 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
800 break;
802 default:
803 break;
807 /* Start emitting debugging information. */
809 static void
810 cp_lexer_start_debugging (cp_lexer* lexer)
812 lexer->debugging_p = true;
815 /* Stop emitting debugging information. */
817 static void
818 cp_lexer_stop_debugging (cp_lexer* lexer)
820 lexer->debugging_p = false;
823 #endif /* ENABLE_CHECKING */
825 /* Create a new cp_token_cache, representing a range of tokens. */
827 static cp_token_cache *
828 cp_token_cache_new (cp_token *first, cp_token *last)
830 cp_token_cache *cache = GGC_NEW (cp_token_cache);
831 cache->first = first;
832 cache->last = last;
833 return cache;
837 /* Decl-specifiers. */
839 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
841 static void
842 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
844 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
847 /* Declarators. */
849 /* Nothing other than the parser should be creating declarators;
850 declarators are a semi-syntactic representation of C++ entities.
851 Other parts of the front end that need to create entities (like
852 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
854 static cp_declarator *make_call_declarator
855 (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
856 static cp_declarator *make_array_declarator
857 (cp_declarator *, tree);
858 static cp_declarator *make_pointer_declarator
859 (cp_cv_quals, cp_declarator *);
860 static cp_declarator *make_reference_declarator
861 (cp_cv_quals, cp_declarator *);
862 static cp_parameter_declarator *make_parameter_declarator
863 (cp_decl_specifier_seq *, cp_declarator *, tree);
864 static cp_declarator *make_ptrmem_declarator
865 (cp_cv_quals, tree, cp_declarator *);
867 /* An erroneous declarator. */
868 static cp_declarator *cp_error_declarator;
870 /* The obstack on which declarators and related data structures are
871 allocated. */
872 static struct obstack declarator_obstack;
874 /* Alloc BYTES from the declarator memory pool. */
876 static inline void *
877 alloc_declarator (size_t bytes)
879 return obstack_alloc (&declarator_obstack, bytes);
882 /* Allocate a declarator of the indicated KIND. Clear fields that are
883 common to all declarators. */
885 static cp_declarator *
886 make_declarator (cp_declarator_kind kind)
888 cp_declarator *declarator;
890 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
891 declarator->kind = kind;
892 declarator->attributes = NULL_TREE;
893 declarator->declarator = NULL;
895 return declarator;
898 /* Make a declarator for a generalized identifier. If
899 QUALIFYING_SCOPE is non-NULL, the identifier is
900 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
901 UNQUALIFIED_NAME. SFK indicates the kind of special function this
902 is, if any. */
904 static cp_declarator *
905 make_id_declarator (tree qualifying_scope, tree unqualified_name,
906 special_function_kind sfk)
908 cp_declarator *declarator;
910 /* It is valid to write:
912 class C { void f(); };
913 typedef C D;
914 void D::f();
916 The standard is not clear about whether `typedef const C D' is
917 legal; as of 2002-09-15 the committee is considering that
918 question. EDG 3.0 allows that syntax. Therefore, we do as
919 well. */
920 if (qualifying_scope && TYPE_P (qualifying_scope))
921 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
923 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
924 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
925 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
927 declarator = make_declarator (cdk_id);
928 declarator->u.id.qualifying_scope = qualifying_scope;
929 declarator->u.id.unqualified_name = unqualified_name;
930 declarator->u.id.sfk = sfk;
932 return declarator;
935 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
936 of modifiers such as const or volatile to apply to the pointer
937 type, represented as identifiers. */
939 cp_declarator *
940 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
942 cp_declarator *declarator;
944 declarator = make_declarator (cdk_pointer);
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 references. */
954 cp_declarator *
955 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
957 cp_declarator *declarator;
959 declarator = make_declarator (cdk_reference);
960 declarator->declarator = target;
961 declarator->u.pointer.qualifiers = cv_qualifiers;
962 declarator->u.pointer.class_type = NULL_TREE;
964 return declarator;
967 /* Like make_pointer_declarator -- but for a pointer to a non-static
968 member of CLASS_TYPE. */
970 cp_declarator *
971 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
972 cp_declarator *pointee)
974 cp_declarator *declarator;
976 declarator = make_declarator (cdk_ptrmem);
977 declarator->declarator = pointee;
978 declarator->u.pointer.qualifiers = cv_qualifiers;
979 declarator->u.pointer.class_type = class_type;
981 return declarator;
984 /* Make a declarator for the function given by TARGET, with the
985 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
986 "const"-qualified member function. The EXCEPTION_SPECIFICATION
987 indicates what exceptions can be thrown. */
989 cp_declarator *
990 make_call_declarator (cp_declarator *target,
991 cp_parameter_declarator *parms,
992 cp_cv_quals cv_qualifiers,
993 tree exception_specification)
995 cp_declarator *declarator;
997 declarator = make_declarator (cdk_function);
998 declarator->declarator = target;
999 declarator->u.function.parameters = parms;
1000 declarator->u.function.qualifiers = cv_qualifiers;
1001 declarator->u.function.exception_specification = exception_specification;
1003 return declarator;
1006 /* Make a declarator for an array of BOUNDS elements, each of which is
1007 defined by ELEMENT. */
1009 cp_declarator *
1010 make_array_declarator (cp_declarator *element, tree bounds)
1012 cp_declarator *declarator;
1014 declarator = make_declarator (cdk_array);
1015 declarator->declarator = element;
1016 declarator->u.array.bounds = bounds;
1018 return declarator;
1021 cp_parameter_declarator *no_parameters;
1023 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1024 DECLARATOR and DEFAULT_ARGUMENT. */
1026 cp_parameter_declarator *
1027 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1028 cp_declarator *declarator,
1029 tree default_argument)
1031 cp_parameter_declarator *parameter;
1033 parameter = ((cp_parameter_declarator *)
1034 alloc_declarator (sizeof (cp_parameter_declarator)));
1035 parameter->next = NULL;
1036 if (decl_specifiers)
1037 parameter->decl_specifiers = *decl_specifiers;
1038 else
1039 clear_decl_specs (&parameter->decl_specifiers);
1040 parameter->declarator = declarator;
1041 parameter->default_argument = default_argument;
1042 parameter->ellipsis_p = false;
1044 return parameter;
1047 /* Returns true iff DECLARATOR is a declaration for a function. */
1049 static bool
1050 function_declarator_p (const cp_declarator *declarator)
1052 while (declarator)
1054 if (declarator->kind == cdk_function
1055 && declarator->declarator->kind == cdk_id)
1056 return true;
1057 if (declarator->kind == cdk_id
1058 || declarator->kind == cdk_error)
1059 return false;
1060 declarator = declarator->declarator;
1062 return false;
1065 /* The parser. */
1067 /* Overview
1068 --------
1070 A cp_parser parses the token stream as specified by the C++
1071 grammar. Its job is purely parsing, not semantic analysis. For
1072 example, the parser breaks the token stream into declarators,
1073 expressions, statements, and other similar syntactic constructs.
1074 It does not check that the types of the expressions on either side
1075 of an assignment-statement are compatible, or that a function is
1076 not declared with a parameter of type `void'.
1078 The parser invokes routines elsewhere in the compiler to perform
1079 semantic analysis and to build up the abstract syntax tree for the
1080 code processed.
1082 The parser (and the template instantiation code, which is, in a
1083 way, a close relative of parsing) are the only parts of the
1084 compiler that should be calling push_scope and pop_scope, or
1085 related functions. The parser (and template instantiation code)
1086 keeps track of what scope is presently active; everything else
1087 should simply honor that. (The code that generates static
1088 initializers may also need to set the scope, in order to check
1089 access control correctly when emitting the initializers.)
1091 Methodology
1092 -----------
1094 The parser is of the standard recursive-descent variety. Upcoming
1095 tokens in the token stream are examined in order to determine which
1096 production to use when parsing a non-terminal. Some C++ constructs
1097 require arbitrary look ahead to disambiguate. For example, it is
1098 impossible, in the general case, to tell whether a statement is an
1099 expression or declaration without scanning the entire statement.
1100 Therefore, the parser is capable of "parsing tentatively." When the
1101 parser is not sure what construct comes next, it enters this mode.
1102 Then, while we attempt to parse the construct, the parser queues up
1103 error messages, rather than issuing them immediately, and saves the
1104 tokens it consumes. If the construct is parsed successfully, the
1105 parser "commits", i.e., it issues any queued error messages and
1106 the tokens that were being preserved are permanently discarded.
1107 If, however, the construct is not parsed successfully, the parser
1108 rolls back its state completely so that it can resume parsing using
1109 a different alternative.
1111 Future Improvements
1112 -------------------
1114 The performance of the parser could probably be improved substantially.
1115 We could often eliminate the need to parse tentatively by looking ahead
1116 a little bit. In some places, this approach might not entirely eliminate
1117 the need to parse tentatively, but it might still speed up the average
1118 case. */
1120 /* Flags that are passed to some parsing functions. These values can
1121 be bitwise-ored together. */
1123 typedef enum cp_parser_flags
1125 /* No flags. */
1126 CP_PARSER_FLAGS_NONE = 0x0,
1127 /* The construct is optional. If it is not present, then no error
1128 should be issued. */
1129 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1130 /* When parsing a type-specifier, do not allow user-defined types. */
1131 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1132 } cp_parser_flags;
1134 /* The different kinds of declarators we want to parse. */
1136 typedef enum cp_parser_declarator_kind
1138 /* We want an abstract declarator. */
1139 CP_PARSER_DECLARATOR_ABSTRACT,
1140 /* We want a named declarator. */
1141 CP_PARSER_DECLARATOR_NAMED,
1142 /* We don't mind, but the name must be an unqualified-id. */
1143 CP_PARSER_DECLARATOR_EITHER
1144 } cp_parser_declarator_kind;
1146 /* The precedence values used to parse binary expressions. The minimum value
1147 of PREC must be 1, because zero is reserved to quickly discriminate
1148 binary operators from other tokens. */
1150 enum cp_parser_prec
1152 PREC_NOT_OPERATOR,
1153 PREC_LOGICAL_OR_EXPRESSION,
1154 PREC_LOGICAL_AND_EXPRESSION,
1155 PREC_INCLUSIVE_OR_EXPRESSION,
1156 PREC_EXCLUSIVE_OR_EXPRESSION,
1157 PREC_AND_EXPRESSION,
1158 PREC_EQUALITY_EXPRESSION,
1159 PREC_RELATIONAL_EXPRESSION,
1160 PREC_SHIFT_EXPRESSION,
1161 PREC_ADDITIVE_EXPRESSION,
1162 PREC_MULTIPLICATIVE_EXPRESSION,
1163 PREC_PM_EXPRESSION,
1164 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1167 /* A mapping from a token type to a corresponding tree node type, with a
1168 precedence value. */
1170 typedef struct cp_parser_binary_operations_map_node
1172 /* The token type. */
1173 enum cpp_ttype token_type;
1174 /* The corresponding tree code. */
1175 enum tree_code tree_type;
1176 /* The precedence of this operator. */
1177 enum cp_parser_prec prec;
1178 } cp_parser_binary_operations_map_node;
1180 /* The status of a tentative parse. */
1182 typedef enum cp_parser_status_kind
1184 /* No errors have occurred. */
1185 CP_PARSER_STATUS_KIND_NO_ERROR,
1186 /* An error has occurred. */
1187 CP_PARSER_STATUS_KIND_ERROR,
1188 /* We are committed to this tentative parse, whether or not an error
1189 has occurred. */
1190 CP_PARSER_STATUS_KIND_COMMITTED
1191 } cp_parser_status_kind;
1193 typedef struct cp_parser_expression_stack_entry
1195 /* Left hand side of the binary operation we are currently
1196 parsing. */
1197 tree lhs;
1198 /* Original tree code for left hand side, if it was a binary
1199 expression itself (used for -Wparentheses). */
1200 enum tree_code lhs_type;
1201 /* Tree code for the binary operation we are parsing. */
1202 enum tree_code tree_type;
1203 /* Precedence of the binary operation we are parsing. */
1204 int prec;
1205 } cp_parser_expression_stack_entry;
1207 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1208 entries because precedence levels on the stack are monotonically
1209 increasing. */
1210 typedef struct cp_parser_expression_stack_entry
1211 cp_parser_expression_stack[NUM_PREC_VALUES];
1213 /* Context that is saved and restored when parsing tentatively. */
1214 typedef struct cp_parser_context GTY (())
1216 /* If this is a tentative parsing context, the status of the
1217 tentative parse. */
1218 enum cp_parser_status_kind status;
1219 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1220 that are looked up in this context must be looked up both in the
1221 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1222 the context of the containing expression. */
1223 tree object_type;
1225 /* The next parsing context in the stack. */
1226 struct cp_parser_context *next;
1227 } cp_parser_context;
1229 /* Prototypes. */
1231 /* Constructors and destructors. */
1233 static cp_parser_context *cp_parser_context_new
1234 (cp_parser_context *);
1236 /* Class variables. */
1238 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1240 /* The operator-precedence table used by cp_parser_binary_expression.
1241 Transformed into an associative array (binops_by_token) by
1242 cp_parser_new. */
1244 static const cp_parser_binary_operations_map_node binops[] = {
1245 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1246 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1248 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1249 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1250 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1252 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1253 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1255 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1256 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1258 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1259 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1260 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1261 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1263 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1264 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1266 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1268 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1270 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1272 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1274 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1277 /* The same as binops, but initialized by cp_parser_new so that
1278 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1279 for speed. */
1280 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1282 /* Constructors and destructors. */
1284 /* Construct a new context. The context below this one on the stack
1285 is given by NEXT. */
1287 static cp_parser_context *
1288 cp_parser_context_new (cp_parser_context* next)
1290 cp_parser_context *context;
1292 /* Allocate the storage. */
1293 if (cp_parser_context_free_list != NULL)
1295 /* Pull the first entry from the free list. */
1296 context = cp_parser_context_free_list;
1297 cp_parser_context_free_list = context->next;
1298 memset (context, 0, sizeof (*context));
1300 else
1301 context = GGC_CNEW (cp_parser_context);
1303 /* No errors have occurred yet in this context. */
1304 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1305 /* If this is not the bottomost context, copy information that we
1306 need from the previous context. */
1307 if (next)
1309 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1310 expression, then we are parsing one in this context, too. */
1311 context->object_type = next->object_type;
1312 /* Thread the stack. */
1313 context->next = next;
1316 return context;
1319 /* The cp_parser structure represents the C++ parser. */
1321 typedef struct cp_parser GTY(())
1323 /* The lexer from which we are obtaining tokens. */
1324 cp_lexer *lexer;
1326 /* The scope in which names should be looked up. If NULL_TREE, then
1327 we look up names in the scope that is currently open in the
1328 source program. If non-NULL, this is either a TYPE or
1329 NAMESPACE_DECL for the scope in which we should look. It can
1330 also be ERROR_MARK, when we've parsed a bogus scope.
1332 This value is not cleared automatically after a name is looked
1333 up, so we must be careful to clear it before starting a new look
1334 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1335 will look up `Z' in the scope of `X', rather than the current
1336 scope.) Unfortunately, it is difficult to tell when name lookup
1337 is complete, because we sometimes peek at a token, look it up,
1338 and then decide not to consume it. */
1339 tree scope;
1341 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1342 last lookup took place. OBJECT_SCOPE is used if an expression
1343 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1344 respectively. QUALIFYING_SCOPE is used for an expression of the
1345 form "X::Y"; it refers to X. */
1346 tree object_scope;
1347 tree qualifying_scope;
1349 /* A stack of parsing contexts. All but the bottom entry on the
1350 stack will be tentative contexts.
1352 We parse tentatively in order to determine which construct is in
1353 use in some situations. For example, in order to determine
1354 whether a statement is an expression-statement or a
1355 declaration-statement we parse it tentatively as a
1356 declaration-statement. If that fails, we then reparse the same
1357 token stream as an expression-statement. */
1358 cp_parser_context *context;
1360 /* True if we are parsing GNU C++. If this flag is not set, then
1361 GNU extensions are not recognized. */
1362 bool allow_gnu_extensions_p;
1364 /* TRUE if the `>' token should be interpreted as the greater-than
1365 operator. FALSE if it is the end of a template-id or
1366 template-parameter-list. */
1367 bool greater_than_is_operator_p;
1369 /* TRUE if default arguments are allowed within a parameter list
1370 that starts at this point. FALSE if only a gnu extension makes
1371 them permissible. */
1372 bool default_arg_ok_p;
1374 /* TRUE if we are parsing an integral constant-expression. See
1375 [expr.const] for a precise definition. */
1376 bool integral_constant_expression_p;
1378 /* TRUE if we are parsing an integral constant-expression -- but a
1379 non-constant expression should be permitted as well. This flag
1380 is used when parsing an array bound so that GNU variable-length
1381 arrays are tolerated. */
1382 bool allow_non_integral_constant_expression_p;
1384 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1385 been seen that makes the expression non-constant. */
1386 bool non_integral_constant_expression_p;
1388 /* TRUE if local variable names and `this' are forbidden in the
1389 current context. */
1390 bool local_variables_forbidden_p;
1392 /* TRUE if the declaration we are parsing is part of a
1393 linkage-specification of the form `extern string-literal
1394 declaration'. */
1395 bool in_unbraced_linkage_specification_p;
1397 /* TRUE if we are presently parsing a declarator, after the
1398 direct-declarator. */
1399 bool in_declarator_p;
1401 /* TRUE if we are presently parsing a template-argument-list. */
1402 bool in_template_argument_list_p;
1404 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1405 to IN_OMP_BLOCK if parsing OpenMP structured block and
1406 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1407 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1408 iteration-statement, OpenMP block or loop within that switch. */
1409 #define IN_SWITCH_STMT 1
1410 #define IN_ITERATION_STMT 2
1411 #define IN_OMP_BLOCK 4
1412 #define IN_OMP_FOR 8
1413 unsigned char in_statement;
1415 /* TRUE if we are presently parsing the body of a switch statement.
1416 Note that this doesn't quite overlap with in_statement above.
1417 The difference relates to giving the right sets of error messages:
1418 "case not in switch" vs "break statement used with OpenMP...". */
1419 bool in_switch_statement_p;
1421 /* TRUE if we are parsing a type-id in an expression context. In
1422 such a situation, both "type (expr)" and "type (type)" are valid
1423 alternatives. */
1424 bool in_type_id_in_expr_p;
1426 /* TRUE if we are currently in a header file where declarations are
1427 implicitly extern "C". */
1428 bool implicit_extern_c;
1430 /* TRUE if strings in expressions should be translated to the execution
1431 character set. */
1432 bool translate_strings_p;
1434 /* TRUE if we are presently parsing the body of a function, but not
1435 a local class. */
1436 bool in_function_body;
1438 /* If non-NULL, then we are parsing a construct where new type
1439 definitions are not permitted. The string stored here will be
1440 issued as an error message if a type is defined. */
1441 const char *type_definition_forbidden_message;
1443 /* A list of lists. The outer list is a stack, used for member
1444 functions of local classes. At each level there are two sub-list,
1445 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1446 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1447 TREE_VALUE's. The functions are chained in reverse declaration
1448 order.
1450 The TREE_PURPOSE sublist contains those functions with default
1451 arguments that need post processing, and the TREE_VALUE sublist
1452 contains those functions with definitions that need post
1453 processing.
1455 These lists can only be processed once the outermost class being
1456 defined is complete. */
1457 tree unparsed_functions_queues;
1459 /* The number of classes whose definitions are currently in
1460 progress. */
1461 unsigned num_classes_being_defined;
1463 /* The number of template parameter lists that apply directly to the
1464 current declaration. */
1465 unsigned num_template_parameter_lists;
1466 } cp_parser;
1468 /* Prototypes. */
1470 /* Constructors and destructors. */
1472 static cp_parser *cp_parser_new
1473 (void);
1475 /* Routines to parse various constructs.
1477 Those that return `tree' will return the error_mark_node (rather
1478 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1479 Sometimes, they will return an ordinary node if error-recovery was
1480 attempted, even though a parse error occurred. So, to check
1481 whether or not a parse error occurred, you should always use
1482 cp_parser_error_occurred. If the construct is optional (indicated
1483 either by an `_opt' in the name of the function that does the
1484 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1485 the construct is not present. */
1487 /* Lexical conventions [gram.lex] */
1489 static tree cp_parser_identifier
1490 (cp_parser *);
1491 static tree cp_parser_string_literal
1492 (cp_parser *, bool, bool);
1494 /* Basic concepts [gram.basic] */
1496 static bool cp_parser_translation_unit
1497 (cp_parser *);
1499 /* Expressions [gram.expr] */
1501 static tree cp_parser_primary_expression
1502 (cp_parser *, bool, bool, bool, cp_id_kind *);
1503 static tree cp_parser_id_expression
1504 (cp_parser *, bool, bool, bool *, bool, bool);
1505 static tree cp_parser_unqualified_id
1506 (cp_parser *, bool, bool, bool, bool);
1507 static tree cp_parser_nested_name_specifier_opt
1508 (cp_parser *, bool, bool, bool, bool);
1509 static tree cp_parser_nested_name_specifier
1510 (cp_parser *, bool, bool, bool, bool);
1511 static tree cp_parser_class_or_namespace_name
1512 (cp_parser *, bool, bool, bool, bool, bool);
1513 static tree cp_parser_postfix_expression
1514 (cp_parser *, bool, bool);
1515 static tree cp_parser_postfix_open_square_expression
1516 (cp_parser *, tree, bool);
1517 static tree cp_parser_postfix_dot_deref_expression
1518 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1519 static tree cp_parser_parenthesized_expression_list
1520 (cp_parser *, bool, bool, bool *);
1521 static void cp_parser_pseudo_destructor_name
1522 (cp_parser *, tree *, tree *);
1523 static tree cp_parser_unary_expression
1524 (cp_parser *, bool, bool);
1525 static enum tree_code cp_parser_unary_operator
1526 (cp_token *);
1527 static tree cp_parser_new_expression
1528 (cp_parser *);
1529 static tree cp_parser_new_placement
1530 (cp_parser *);
1531 static tree cp_parser_new_type_id
1532 (cp_parser *, tree *);
1533 static cp_declarator *cp_parser_new_declarator_opt
1534 (cp_parser *);
1535 static cp_declarator *cp_parser_direct_new_declarator
1536 (cp_parser *);
1537 static tree cp_parser_new_initializer
1538 (cp_parser *);
1539 static tree cp_parser_delete_expression
1540 (cp_parser *);
1541 static tree cp_parser_cast_expression
1542 (cp_parser *, bool, bool);
1543 static tree cp_parser_binary_expression
1544 (cp_parser *, bool);
1545 static tree cp_parser_question_colon_clause
1546 (cp_parser *, tree);
1547 static tree cp_parser_assignment_expression
1548 (cp_parser *, bool);
1549 static enum tree_code cp_parser_assignment_operator_opt
1550 (cp_parser *);
1551 static tree cp_parser_expression
1552 (cp_parser *, bool);
1553 static tree cp_parser_constant_expression
1554 (cp_parser *, bool, bool *);
1555 static tree cp_parser_builtin_offsetof
1556 (cp_parser *);
1558 /* Statements [gram.stmt.stmt] */
1560 static void cp_parser_statement
1561 (cp_parser *, tree, bool, bool *);
1562 static void cp_parser_label_for_labeled_statement
1563 (cp_parser *);
1564 static tree cp_parser_expression_statement
1565 (cp_parser *, tree);
1566 static tree cp_parser_compound_statement
1567 (cp_parser *, tree, bool);
1568 static void cp_parser_statement_seq_opt
1569 (cp_parser *, tree);
1570 static tree cp_parser_selection_statement
1571 (cp_parser *, bool *);
1572 static tree cp_parser_condition
1573 (cp_parser *);
1574 static tree cp_parser_iteration_statement
1575 (cp_parser *);
1576 static void cp_parser_for_init_statement
1577 (cp_parser *);
1578 static tree cp_parser_jump_statement
1579 (cp_parser *);
1580 static void cp_parser_declaration_statement
1581 (cp_parser *);
1583 static tree cp_parser_implicitly_scoped_statement
1584 (cp_parser *, bool *);
1585 static void cp_parser_already_scoped_statement
1586 (cp_parser *);
1588 /* Declarations [gram.dcl.dcl] */
1590 static void cp_parser_declaration_seq_opt
1591 (cp_parser *);
1592 static void cp_parser_declaration
1593 (cp_parser *);
1594 static void cp_parser_block_declaration
1595 (cp_parser *, bool);
1596 static void cp_parser_simple_declaration
1597 (cp_parser *, bool);
1598 static void cp_parser_decl_specifier_seq
1599 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1600 static tree cp_parser_storage_class_specifier_opt
1601 (cp_parser *);
1602 static tree cp_parser_function_specifier_opt
1603 (cp_parser *, cp_decl_specifier_seq *);
1604 static tree cp_parser_type_specifier
1605 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1606 int *, bool *);
1607 static tree cp_parser_simple_type_specifier
1608 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1609 static tree cp_parser_type_name
1610 (cp_parser *);
1611 static tree cp_parser_elaborated_type_specifier
1612 (cp_parser *, bool, bool);
1613 static tree cp_parser_enum_specifier
1614 (cp_parser *);
1615 static void cp_parser_enumerator_list
1616 (cp_parser *, tree);
1617 static void cp_parser_enumerator_definition
1618 (cp_parser *, tree);
1619 static tree cp_parser_namespace_name
1620 (cp_parser *);
1621 static void cp_parser_namespace_definition
1622 (cp_parser *);
1623 static void cp_parser_namespace_body
1624 (cp_parser *);
1625 static tree cp_parser_qualified_namespace_specifier
1626 (cp_parser *);
1627 static void cp_parser_namespace_alias_definition
1628 (cp_parser *);
1629 static bool cp_parser_using_declaration
1630 (cp_parser *, bool);
1631 static void cp_parser_using_directive
1632 (cp_parser *);
1633 static void cp_parser_asm_definition
1634 (cp_parser *);
1635 static void cp_parser_linkage_specification
1636 (cp_parser *);
1637 static void cp_parser_static_assert
1638 (cp_parser *, bool);
1640 /* Declarators [gram.dcl.decl] */
1642 static tree cp_parser_init_declarator
1643 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1644 static cp_declarator *cp_parser_declarator
1645 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1646 static cp_declarator *cp_parser_direct_declarator
1647 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1648 static enum tree_code cp_parser_ptr_operator
1649 (cp_parser *, tree *, cp_cv_quals *);
1650 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1651 (cp_parser *);
1652 static tree cp_parser_declarator_id
1653 (cp_parser *, bool);
1654 static tree cp_parser_type_id
1655 (cp_parser *);
1656 static void cp_parser_type_specifier_seq
1657 (cp_parser *, bool, cp_decl_specifier_seq *);
1658 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1659 (cp_parser *);
1660 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1661 (cp_parser *, bool *);
1662 static cp_parameter_declarator *cp_parser_parameter_declaration
1663 (cp_parser *, bool, bool *);
1664 static void cp_parser_function_body
1665 (cp_parser *);
1666 static tree cp_parser_initializer
1667 (cp_parser *, bool *, bool *);
1668 static tree cp_parser_initializer_clause
1669 (cp_parser *, bool *);
1670 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1671 (cp_parser *, bool *);
1673 static bool cp_parser_ctor_initializer_opt_and_function_body
1674 (cp_parser *);
1676 /* Classes [gram.class] */
1678 static tree cp_parser_class_name
1679 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1680 static tree cp_parser_class_specifier
1681 (cp_parser *);
1682 static tree cp_parser_class_head
1683 (cp_parser *, bool *, tree *, tree *);
1684 static enum tag_types cp_parser_class_key
1685 (cp_parser *);
1686 static void cp_parser_member_specification_opt
1687 (cp_parser *);
1688 static void cp_parser_member_declaration
1689 (cp_parser *);
1690 static tree cp_parser_pure_specifier
1691 (cp_parser *);
1692 static tree cp_parser_constant_initializer
1693 (cp_parser *);
1695 /* Derived classes [gram.class.derived] */
1697 static tree cp_parser_base_clause
1698 (cp_parser *);
1699 static tree cp_parser_base_specifier
1700 (cp_parser *);
1702 /* Special member functions [gram.special] */
1704 static tree cp_parser_conversion_function_id
1705 (cp_parser *);
1706 static tree cp_parser_conversion_type_id
1707 (cp_parser *);
1708 static cp_declarator *cp_parser_conversion_declarator_opt
1709 (cp_parser *);
1710 static bool cp_parser_ctor_initializer_opt
1711 (cp_parser *);
1712 static void cp_parser_mem_initializer_list
1713 (cp_parser *);
1714 static tree cp_parser_mem_initializer
1715 (cp_parser *);
1716 static tree cp_parser_mem_initializer_id
1717 (cp_parser *);
1719 /* Overloading [gram.over] */
1721 static tree cp_parser_operator_function_id
1722 (cp_parser *);
1723 static tree cp_parser_operator
1724 (cp_parser *);
1726 /* Templates [gram.temp] */
1728 static void cp_parser_template_declaration
1729 (cp_parser *, bool);
1730 static tree cp_parser_template_parameter_list
1731 (cp_parser *);
1732 static tree cp_parser_template_parameter
1733 (cp_parser *, bool *);
1734 static tree cp_parser_type_parameter
1735 (cp_parser *);
1736 static tree cp_parser_template_id
1737 (cp_parser *, bool, bool, bool);
1738 static tree cp_parser_template_name
1739 (cp_parser *, bool, bool, bool, bool *);
1740 static tree cp_parser_template_argument_list
1741 (cp_parser *);
1742 static tree cp_parser_template_argument
1743 (cp_parser *);
1744 static void cp_parser_explicit_instantiation
1745 (cp_parser *);
1746 static void cp_parser_explicit_specialization
1747 (cp_parser *);
1749 /* Exception handling [gram.exception] */
1751 static tree cp_parser_try_block
1752 (cp_parser *);
1753 static bool cp_parser_function_try_block
1754 (cp_parser *);
1755 static void cp_parser_handler_seq
1756 (cp_parser *);
1757 static void cp_parser_handler
1758 (cp_parser *);
1759 static tree cp_parser_exception_declaration
1760 (cp_parser *);
1761 static tree cp_parser_throw_expression
1762 (cp_parser *);
1763 static tree cp_parser_exception_specification_opt
1764 (cp_parser *);
1765 static tree cp_parser_type_id_list
1766 (cp_parser *);
1768 /* GNU Extensions */
1770 static tree cp_parser_asm_specification_opt
1771 (cp_parser *);
1772 static tree cp_parser_asm_operand_list
1773 (cp_parser *);
1774 static tree cp_parser_asm_clobber_list
1775 (cp_parser *);
1776 static tree cp_parser_attributes_opt
1777 (cp_parser *);
1778 static tree cp_parser_attribute_list
1779 (cp_parser *);
1780 static bool cp_parser_extension_opt
1781 (cp_parser *, int *);
1782 static void cp_parser_label_declaration
1783 (cp_parser *);
1785 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1786 static bool cp_parser_pragma
1787 (cp_parser *, enum pragma_context);
1789 /* Objective-C++ Productions */
1791 static tree cp_parser_objc_message_receiver
1792 (cp_parser *);
1793 static tree cp_parser_objc_message_args
1794 (cp_parser *);
1795 static tree cp_parser_objc_message_expression
1796 (cp_parser *);
1797 static tree cp_parser_objc_encode_expression
1798 (cp_parser *);
1799 static tree cp_parser_objc_defs_expression
1800 (cp_parser *);
1801 static tree cp_parser_objc_protocol_expression
1802 (cp_parser *);
1803 static tree cp_parser_objc_selector_expression
1804 (cp_parser *);
1805 static tree cp_parser_objc_expression
1806 (cp_parser *);
1807 static bool cp_parser_objc_selector_p
1808 (enum cpp_ttype);
1809 static tree cp_parser_objc_selector
1810 (cp_parser *);
1811 static tree cp_parser_objc_protocol_refs_opt
1812 (cp_parser *);
1813 static void cp_parser_objc_declaration
1814 (cp_parser *);
1815 static tree cp_parser_objc_statement
1816 (cp_parser *);
1818 /* Utility Routines */
1820 static tree cp_parser_lookup_name
1821 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *);
1822 static tree cp_parser_lookup_name_simple
1823 (cp_parser *, tree);
1824 static tree cp_parser_maybe_treat_template_as_class
1825 (tree, bool);
1826 static bool cp_parser_check_declarator_template_parameters
1827 (cp_parser *, cp_declarator *);
1828 static bool cp_parser_check_template_parameters
1829 (cp_parser *, unsigned);
1830 static tree cp_parser_simple_cast_expression
1831 (cp_parser *);
1832 static tree cp_parser_global_scope_opt
1833 (cp_parser *, bool);
1834 static bool cp_parser_constructor_declarator_p
1835 (cp_parser *, bool);
1836 static tree cp_parser_function_definition_from_specifiers_and_declarator
1837 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1838 static tree cp_parser_function_definition_after_declarator
1839 (cp_parser *, bool);
1840 static void cp_parser_template_declaration_after_export
1841 (cp_parser *, bool);
1842 static void cp_parser_perform_template_parameter_access_checks
1843 (VEC (deferred_access_check,gc)*);
1844 static tree cp_parser_single_declaration
1845 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool *);
1846 static tree cp_parser_functional_cast
1847 (cp_parser *, tree);
1848 static tree cp_parser_save_member_function_body
1849 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1850 static tree cp_parser_enclosed_template_argument_list
1851 (cp_parser *);
1852 static void cp_parser_save_default_args
1853 (cp_parser *, tree);
1854 static void cp_parser_late_parsing_for_member
1855 (cp_parser *, tree);
1856 static void cp_parser_late_parsing_default_args
1857 (cp_parser *, tree);
1858 static tree cp_parser_sizeof_operand
1859 (cp_parser *, enum rid);
1860 static bool cp_parser_declares_only_class_p
1861 (cp_parser *);
1862 static void cp_parser_set_storage_class
1863 (cp_parser *, cp_decl_specifier_seq *, enum rid);
1864 static void cp_parser_set_decl_spec_type
1865 (cp_decl_specifier_seq *, tree, bool);
1866 static bool cp_parser_friend_p
1867 (const cp_decl_specifier_seq *);
1868 static cp_token *cp_parser_require
1869 (cp_parser *, enum cpp_ttype, const char *);
1870 static cp_token *cp_parser_require_keyword
1871 (cp_parser *, enum rid, const char *);
1872 static bool cp_parser_token_starts_function_definition_p
1873 (cp_token *);
1874 static bool cp_parser_next_token_starts_class_definition_p
1875 (cp_parser *);
1876 static bool cp_parser_next_token_ends_template_argument_p
1877 (cp_parser *);
1878 static bool cp_parser_nth_token_starts_template_argument_list_p
1879 (cp_parser *, size_t);
1880 static enum tag_types cp_parser_token_is_class_key
1881 (cp_token *);
1882 static void cp_parser_check_class_key
1883 (enum tag_types, tree type);
1884 static void cp_parser_check_access_in_redeclaration
1885 (tree type);
1886 static bool cp_parser_optional_template_keyword
1887 (cp_parser *);
1888 static void cp_parser_pre_parsed_nested_name_specifier
1889 (cp_parser *);
1890 static void cp_parser_cache_group
1891 (cp_parser *, enum cpp_ttype, unsigned);
1892 static void cp_parser_parse_tentatively
1893 (cp_parser *);
1894 static void cp_parser_commit_to_tentative_parse
1895 (cp_parser *);
1896 static void cp_parser_abort_tentative_parse
1897 (cp_parser *);
1898 static bool cp_parser_parse_definitely
1899 (cp_parser *);
1900 static inline bool cp_parser_parsing_tentatively
1901 (cp_parser *);
1902 static bool cp_parser_uncommitted_to_tentative_parse_p
1903 (cp_parser *);
1904 static void cp_parser_error
1905 (cp_parser *, const char *);
1906 static void cp_parser_name_lookup_error
1907 (cp_parser *, tree, tree, const char *);
1908 static bool cp_parser_simulate_error
1909 (cp_parser *);
1910 static bool cp_parser_check_type_definition
1911 (cp_parser *);
1912 static void cp_parser_check_for_definition_in_return_type
1913 (cp_declarator *, tree);
1914 static void cp_parser_check_for_invalid_template_id
1915 (cp_parser *, tree);
1916 static bool cp_parser_non_integral_constant_expression
1917 (cp_parser *, const char *);
1918 static void cp_parser_diagnose_invalid_type_name
1919 (cp_parser *, tree, tree);
1920 static bool cp_parser_parse_and_diagnose_invalid_type_name
1921 (cp_parser *);
1922 static int cp_parser_skip_to_closing_parenthesis
1923 (cp_parser *, bool, bool, bool);
1924 static void cp_parser_skip_to_end_of_statement
1925 (cp_parser *);
1926 static void cp_parser_consume_semicolon_at_end_of_statement
1927 (cp_parser *);
1928 static void cp_parser_skip_to_end_of_block_or_statement
1929 (cp_parser *);
1930 static void cp_parser_skip_to_closing_brace
1931 (cp_parser *);
1932 static void cp_parser_skip_to_end_of_template_parameter_list
1933 (cp_parser *);
1934 static void cp_parser_skip_to_pragma_eol
1935 (cp_parser*, cp_token *);
1936 static bool cp_parser_error_occurred
1937 (cp_parser *);
1938 static bool cp_parser_allow_gnu_extensions_p
1939 (cp_parser *);
1940 static bool cp_parser_is_string_literal
1941 (cp_token *);
1942 static bool cp_parser_is_keyword
1943 (cp_token *, enum rid);
1944 static tree cp_parser_make_typename_type
1945 (cp_parser *, tree, tree);
1947 /* Returns nonzero if we are parsing tentatively. */
1949 static inline bool
1950 cp_parser_parsing_tentatively (cp_parser* parser)
1952 return parser->context->next != NULL;
1955 /* Returns nonzero if TOKEN is a string literal. */
1957 static bool
1958 cp_parser_is_string_literal (cp_token* token)
1960 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1963 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1965 static bool
1966 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1968 return token->keyword == keyword;
1971 /* If not parsing tentatively, issue a diagnostic of the form
1972 FILE:LINE: MESSAGE before TOKEN
1973 where TOKEN is the next token in the input stream. MESSAGE
1974 (specified by the caller) is usually of the form "expected
1975 OTHER-TOKEN". */
1977 static void
1978 cp_parser_error (cp_parser* parser, const char* message)
1980 if (!cp_parser_simulate_error (parser))
1982 cp_token *token = cp_lexer_peek_token (parser->lexer);
1983 /* This diagnostic makes more sense if it is tagged to the line
1984 of the token we just peeked at. */
1985 cp_lexer_set_source_position_from_token (token);
1987 if (token->type == CPP_PRAGMA)
1989 error ("%<#pragma%> is not allowed here");
1990 cp_parser_skip_to_pragma_eol (parser, token);
1991 return;
1994 c_parse_error (message,
1995 /* Because c_parser_error does not understand
1996 CPP_KEYWORD, keywords are treated like
1997 identifiers. */
1998 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1999 token->u.value);
2003 /* Issue an error about name-lookup failing. NAME is the
2004 IDENTIFIER_NODE DECL is the result of
2005 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2006 the thing that we hoped to find. */
2008 static void
2009 cp_parser_name_lookup_error (cp_parser* parser,
2010 tree name,
2011 tree decl,
2012 const char* desired)
2014 /* If name lookup completely failed, tell the user that NAME was not
2015 declared. */
2016 if (decl == error_mark_node)
2018 if (parser->scope && parser->scope != global_namespace)
2019 error ("%<%D::%D%> has not been declared",
2020 parser->scope, name);
2021 else if (parser->scope == global_namespace)
2022 error ("%<::%D%> has not been declared", name);
2023 else if (parser->object_scope
2024 && !CLASS_TYPE_P (parser->object_scope))
2025 error ("request for member %qD in non-class type %qT",
2026 name, parser->object_scope);
2027 else if (parser->object_scope)
2028 error ("%<%T::%D%> has not been declared",
2029 parser->object_scope, name);
2030 else
2031 error ("%qD has not been declared", name);
2033 else if (parser->scope && parser->scope != global_namespace)
2034 error ("%<%D::%D%> %s", parser->scope, name, desired);
2035 else if (parser->scope == global_namespace)
2036 error ("%<::%D%> %s", name, desired);
2037 else
2038 error ("%qD %s", name, desired);
2041 /* If we are parsing tentatively, remember that an error has occurred
2042 during this tentative parse. Returns true if the error was
2043 simulated; false if a message should be issued by the caller. */
2045 static bool
2046 cp_parser_simulate_error (cp_parser* parser)
2048 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2050 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2051 return true;
2053 return false;
2056 /* Check for repeated decl-specifiers. */
2058 static void
2059 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs)
2061 cp_decl_spec ds;
2063 for (ds = ds_first; ds != ds_last; ++ds)
2065 unsigned count = decl_specs->specs[(int)ds];
2066 if (count < 2)
2067 continue;
2068 /* The "long" specifier is a special case because of "long long". */
2069 if (ds == ds_long)
2071 if (count > 2)
2072 error ("%<long long long%> is too long for GCC");
2073 else if (pedantic && !in_system_header && warn_long_long)
2074 pedwarn ("ISO C++ does not support %<long long%>");
2076 else if (count > 1)
2078 static const char *const decl_spec_names[] = {
2079 "signed",
2080 "unsigned",
2081 "short",
2082 "long",
2083 "const",
2084 "volatile",
2085 "restrict",
2086 "inline",
2087 "virtual",
2088 "explicit",
2089 "friend",
2090 "typedef",
2091 "__complex",
2092 "__thread"
2094 error ("duplicate %qs", decl_spec_names[(int)ds]);
2099 /* This function is called when a type is defined. If type
2100 definitions are forbidden at this point, an error message is
2101 issued. */
2103 static bool
2104 cp_parser_check_type_definition (cp_parser* parser)
2106 /* If types are forbidden here, issue a message. */
2107 if (parser->type_definition_forbidden_message)
2109 /* Use `%s' to print the string in case there are any escape
2110 characters in the message. */
2111 error ("%s", parser->type_definition_forbidden_message);
2112 return false;
2114 return true;
2117 /* This function is called when the DECLARATOR is processed. The TYPE
2118 was a type defined in the decl-specifiers. If it is invalid to
2119 define a type in the decl-specifiers for DECLARATOR, an error is
2120 issued. */
2122 static void
2123 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2124 tree type)
2126 /* [dcl.fct] forbids type definitions in return types.
2127 Unfortunately, it's not easy to know whether or not we are
2128 processing a return type until after the fact. */
2129 while (declarator
2130 && (declarator->kind == cdk_pointer
2131 || declarator->kind == cdk_reference
2132 || declarator->kind == cdk_ptrmem))
2133 declarator = declarator->declarator;
2134 if (declarator
2135 && declarator->kind == cdk_function)
2137 error ("new types may not be defined in a return type");
2138 inform ("(perhaps a semicolon is missing after the definition of %qT)",
2139 type);
2143 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2144 "<" in any valid C++ program. If the next token is indeed "<",
2145 issue a message warning the user about what appears to be an
2146 invalid attempt to form a template-id. */
2148 static void
2149 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2150 tree type)
2152 cp_token_position start = 0;
2154 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2156 if (TYPE_P (type))
2157 error ("%qT is not a template", type);
2158 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2159 error ("%qE is not a template", type);
2160 else
2161 error ("invalid template-id");
2162 /* Remember the location of the invalid "<". */
2163 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2164 start = cp_lexer_token_position (parser->lexer, true);
2165 /* Consume the "<". */
2166 cp_lexer_consume_token (parser->lexer);
2167 /* Parse the template arguments. */
2168 cp_parser_enclosed_template_argument_list (parser);
2169 /* Permanently remove the invalid template arguments so that
2170 this error message is not issued again. */
2171 if (start)
2172 cp_lexer_purge_tokens_after (parser->lexer, start);
2176 /* If parsing an integral constant-expression, issue an error message
2177 about the fact that THING appeared and return true. Otherwise,
2178 return false. In either case, set
2179 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2181 static bool
2182 cp_parser_non_integral_constant_expression (cp_parser *parser,
2183 const char *thing)
2185 parser->non_integral_constant_expression_p = true;
2186 if (parser->integral_constant_expression_p)
2188 if (!parser->allow_non_integral_constant_expression_p)
2190 error ("%s cannot appear in a constant-expression", thing);
2191 return true;
2194 return false;
2197 /* Emit a diagnostic for an invalid type name. SCOPE is the
2198 qualifying scope (or NULL, if none) for ID. This function commits
2199 to the current active tentative parse, if any. (Otherwise, the
2200 problematic construct might be encountered again later, resulting
2201 in duplicate error messages.) */
2203 static void
2204 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2206 tree decl, old_scope;
2207 /* Try to lookup the identifier. */
2208 old_scope = parser->scope;
2209 parser->scope = scope;
2210 decl = cp_parser_lookup_name_simple (parser, id);
2211 parser->scope = old_scope;
2212 /* If the lookup found a template-name, it means that the user forgot
2213 to specify an argument list. Emit a useful error message. */
2214 if (TREE_CODE (decl) == TEMPLATE_DECL)
2215 error ("invalid use of template-name %qE without an argument list", decl);
2216 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2217 error ("invalid use of destructor %qD as a type", id);
2218 else if (TREE_CODE (decl) == TYPE_DECL)
2219 /* Something like 'unsigned A a;' */
2220 error ("invalid combination of multiple type-specifiers");
2221 else if (!parser->scope)
2223 /* Issue an error message. */
2224 error ("%qE does not name a type", id);
2225 /* If we're in a template class, it's possible that the user was
2226 referring to a type from a base class. For example:
2228 template <typename T> struct A { typedef T X; };
2229 template <typename T> struct B : public A<T> { X x; };
2231 The user should have said "typename A<T>::X". */
2232 if (processing_template_decl && current_class_type
2233 && TYPE_BINFO (current_class_type))
2235 tree b;
2237 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2239 b = TREE_CHAIN (b))
2241 tree base_type = BINFO_TYPE (b);
2242 if (CLASS_TYPE_P (base_type)
2243 && dependent_type_p (base_type))
2245 tree field;
2246 /* Go from a particular instantiation of the
2247 template (which will have an empty TYPE_FIELDs),
2248 to the main version. */
2249 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2250 for (field = TYPE_FIELDS (base_type);
2251 field;
2252 field = TREE_CHAIN (field))
2253 if (TREE_CODE (field) == TYPE_DECL
2254 && DECL_NAME (field) == id)
2256 inform ("(perhaps %<typename %T::%E%> was intended)",
2257 BINFO_TYPE (b), id);
2258 break;
2260 if (field)
2261 break;
2266 /* Here we diagnose qualified-ids where the scope is actually correct,
2267 but the identifier does not resolve to a valid type name. */
2268 else if (parser->scope != error_mark_node)
2270 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2271 error ("%qE in namespace %qE does not name a type",
2272 id, parser->scope);
2273 else if (TYPE_P (parser->scope))
2274 error ("%qE in class %qT does not name a type", id, parser->scope);
2275 else
2276 gcc_unreachable ();
2278 cp_parser_commit_to_tentative_parse (parser);
2281 /* Check for a common situation where a type-name should be present,
2282 but is not, and issue a sensible error message. Returns true if an
2283 invalid type-name was detected.
2285 The situation handled by this function are variable declarations of the
2286 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2287 Usually, `ID' should name a type, but if we got here it means that it
2288 does not. We try to emit the best possible error message depending on
2289 how exactly the id-expression looks like. */
2291 static bool
2292 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2294 tree id;
2296 cp_parser_parse_tentatively (parser);
2297 id = cp_parser_id_expression (parser,
2298 /*template_keyword_p=*/false,
2299 /*check_dependency_p=*/true,
2300 /*template_p=*/NULL,
2301 /*declarator_p=*/true,
2302 /*optional_p=*/false);
2303 /* After the id-expression, there should be a plain identifier,
2304 otherwise this is not a simple variable declaration. Also, if
2305 the scope is dependent, we cannot do much. */
2306 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2307 || (parser->scope && TYPE_P (parser->scope)
2308 && dependent_type_p (parser->scope)))
2310 cp_parser_abort_tentative_parse (parser);
2311 return false;
2313 if (!cp_parser_parse_definitely (parser) || TREE_CODE (id) == TYPE_DECL)
2314 return false;
2316 /* Emit a diagnostic for the invalid type. */
2317 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2318 /* Skip to the end of the declaration; there's no point in
2319 trying to process it. */
2320 cp_parser_skip_to_end_of_block_or_statement (parser);
2321 return true;
2324 /* Consume tokens up to, and including, the next non-nested closing `)'.
2325 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2326 are doing error recovery. Returns -1 if OR_COMMA is true and we
2327 found an unnested comma. */
2329 static int
2330 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2331 bool recovering,
2332 bool or_comma,
2333 bool consume_paren)
2335 unsigned paren_depth = 0;
2336 unsigned brace_depth = 0;
2338 if (recovering && !or_comma
2339 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2340 return 0;
2342 while (true)
2344 cp_token * token = cp_lexer_peek_token (parser->lexer);
2346 switch (token->type)
2348 case CPP_EOF:
2349 case CPP_PRAGMA_EOL:
2350 /* If we've run out of tokens, then there is no closing `)'. */
2351 return 0;
2353 case CPP_SEMICOLON:
2354 /* This matches the processing in skip_to_end_of_statement. */
2355 if (!brace_depth)
2356 return 0;
2357 break;
2359 case CPP_OPEN_BRACE:
2360 ++brace_depth;
2361 break;
2362 case CPP_CLOSE_BRACE:
2363 if (!brace_depth--)
2364 return 0;
2365 break;
2367 case CPP_COMMA:
2368 if (recovering && or_comma && !brace_depth && !paren_depth)
2369 return -1;
2370 break;
2372 case CPP_OPEN_PAREN:
2373 if (!brace_depth)
2374 ++paren_depth;
2375 break;
2377 case CPP_CLOSE_PAREN:
2378 if (!brace_depth && !paren_depth--)
2380 if (consume_paren)
2381 cp_lexer_consume_token (parser->lexer);
2382 return 1;
2384 break;
2386 default:
2387 break;
2390 /* Consume the token. */
2391 cp_lexer_consume_token (parser->lexer);
2395 /* Consume tokens until we reach the end of the current statement.
2396 Normally, that will be just before consuming a `;'. However, if a
2397 non-nested `}' comes first, then we stop before consuming that. */
2399 static void
2400 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2402 unsigned nesting_depth = 0;
2404 while (true)
2406 cp_token *token = cp_lexer_peek_token (parser->lexer);
2408 switch (token->type)
2410 case CPP_EOF:
2411 case CPP_PRAGMA_EOL:
2412 /* If we've run out of tokens, stop. */
2413 return;
2415 case CPP_SEMICOLON:
2416 /* If the next token is a `;', we have reached the end of the
2417 statement. */
2418 if (!nesting_depth)
2419 return;
2420 break;
2422 case CPP_CLOSE_BRACE:
2423 /* If this is a non-nested '}', stop before consuming it.
2424 That way, when confronted with something like:
2426 { 3 + }
2428 we stop before consuming the closing '}', even though we
2429 have not yet reached a `;'. */
2430 if (nesting_depth == 0)
2431 return;
2433 /* If it is the closing '}' for a block that we have
2434 scanned, stop -- but only after consuming the token.
2435 That way given:
2437 void f g () { ... }
2438 typedef int I;
2440 we will stop after the body of the erroneously declared
2441 function, but before consuming the following `typedef'
2442 declaration. */
2443 if (--nesting_depth == 0)
2445 cp_lexer_consume_token (parser->lexer);
2446 return;
2449 case CPP_OPEN_BRACE:
2450 ++nesting_depth;
2451 break;
2453 default:
2454 break;
2457 /* Consume the token. */
2458 cp_lexer_consume_token (parser->lexer);
2462 /* This function is called at the end of a statement or declaration.
2463 If the next token is a semicolon, it is consumed; otherwise, error
2464 recovery is attempted. */
2466 static void
2467 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2469 /* Look for the trailing `;'. */
2470 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2472 /* If there is additional (erroneous) input, skip to the end of
2473 the statement. */
2474 cp_parser_skip_to_end_of_statement (parser);
2475 /* If the next token is now a `;', consume it. */
2476 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2477 cp_lexer_consume_token (parser->lexer);
2481 /* Skip tokens until we have consumed an entire block, or until we
2482 have consumed a non-nested `;'. */
2484 static void
2485 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2487 int nesting_depth = 0;
2489 while (nesting_depth >= 0)
2491 cp_token *token = cp_lexer_peek_token (parser->lexer);
2493 switch (token->type)
2495 case CPP_EOF:
2496 case CPP_PRAGMA_EOL:
2497 /* If we've run out of tokens, stop. */
2498 return;
2500 case CPP_SEMICOLON:
2501 /* Stop if this is an unnested ';'. */
2502 if (!nesting_depth)
2503 nesting_depth = -1;
2504 break;
2506 case CPP_CLOSE_BRACE:
2507 /* Stop if this is an unnested '}', or closes the outermost
2508 nesting level. */
2509 nesting_depth--;
2510 if (!nesting_depth)
2511 nesting_depth = -1;
2512 break;
2514 case CPP_OPEN_BRACE:
2515 /* Nest. */
2516 nesting_depth++;
2517 break;
2519 default:
2520 break;
2523 /* Consume the token. */
2524 cp_lexer_consume_token (parser->lexer);
2528 /* Skip tokens until a non-nested closing curly brace is the next
2529 token. */
2531 static void
2532 cp_parser_skip_to_closing_brace (cp_parser *parser)
2534 unsigned nesting_depth = 0;
2536 while (true)
2538 cp_token *token = cp_lexer_peek_token (parser->lexer);
2540 switch (token->type)
2542 case CPP_EOF:
2543 case CPP_PRAGMA_EOL:
2544 /* If we've run out of tokens, stop. */
2545 return;
2547 case CPP_CLOSE_BRACE:
2548 /* If the next token is a non-nested `}', then we have reached
2549 the end of the current block. */
2550 if (nesting_depth-- == 0)
2551 return;
2552 break;
2554 case CPP_OPEN_BRACE:
2555 /* If it the next token is a `{', then we are entering a new
2556 block. Consume the entire block. */
2557 ++nesting_depth;
2558 break;
2560 default:
2561 break;
2564 /* Consume the token. */
2565 cp_lexer_consume_token (parser->lexer);
2569 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2570 parameter is the PRAGMA token, allowing us to purge the entire pragma
2571 sequence. */
2573 static void
2574 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2576 cp_token *token;
2578 parser->lexer->in_pragma = false;
2581 token = cp_lexer_consume_token (parser->lexer);
2582 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2584 /* Ensure that the pragma is not parsed again. */
2585 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2588 /* Require pragma end of line, resyncing with it as necessary. The
2589 arguments are as for cp_parser_skip_to_pragma_eol. */
2591 static void
2592 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2594 parser->lexer->in_pragma = false;
2595 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2596 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2599 /* This is a simple wrapper around make_typename_type. When the id is
2600 an unresolved identifier node, we can provide a superior diagnostic
2601 using cp_parser_diagnose_invalid_type_name. */
2603 static tree
2604 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2606 tree result;
2607 if (TREE_CODE (id) == IDENTIFIER_NODE)
2609 result = make_typename_type (scope, id, typename_type,
2610 /*complain=*/tf_none);
2611 if (result == error_mark_node)
2612 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2613 return result;
2615 return make_typename_type (scope, id, typename_type, tf_error);
2619 /* Create a new C++ parser. */
2621 static cp_parser *
2622 cp_parser_new (void)
2624 cp_parser *parser;
2625 cp_lexer *lexer;
2626 unsigned i;
2628 /* cp_lexer_new_main is called before calling ggc_alloc because
2629 cp_lexer_new_main might load a PCH file. */
2630 lexer = cp_lexer_new_main ();
2632 /* Initialize the binops_by_token so that we can get the tree
2633 directly from the token. */
2634 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2635 binops_by_token[binops[i].token_type] = binops[i];
2637 parser = GGC_CNEW (cp_parser);
2638 parser->lexer = lexer;
2639 parser->context = cp_parser_context_new (NULL);
2641 /* For now, we always accept GNU extensions. */
2642 parser->allow_gnu_extensions_p = 1;
2644 /* The `>' token is a greater-than operator, not the end of a
2645 template-id. */
2646 parser->greater_than_is_operator_p = true;
2648 parser->default_arg_ok_p = true;
2650 /* We are not parsing a constant-expression. */
2651 parser->integral_constant_expression_p = false;
2652 parser->allow_non_integral_constant_expression_p = false;
2653 parser->non_integral_constant_expression_p = false;
2655 /* Local variable names are not forbidden. */
2656 parser->local_variables_forbidden_p = false;
2658 /* We are not processing an `extern "C"' declaration. */
2659 parser->in_unbraced_linkage_specification_p = false;
2661 /* We are not processing a declarator. */
2662 parser->in_declarator_p = false;
2664 /* We are not processing a template-argument-list. */
2665 parser->in_template_argument_list_p = false;
2667 /* We are not in an iteration statement. */
2668 parser->in_statement = 0;
2670 /* We are not in a switch statement. */
2671 parser->in_switch_statement_p = false;
2673 /* We are not parsing a type-id inside an expression. */
2674 parser->in_type_id_in_expr_p = false;
2676 /* Declarations aren't implicitly extern "C". */
2677 parser->implicit_extern_c = false;
2679 /* String literals should be translated to the execution character set. */
2680 parser->translate_strings_p = true;
2682 /* We are not parsing a function body. */
2683 parser->in_function_body = false;
2685 /* The unparsed function queue is empty. */
2686 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2688 /* There are no classes being defined. */
2689 parser->num_classes_being_defined = 0;
2691 /* No template parameters apply. */
2692 parser->num_template_parameter_lists = 0;
2694 return parser;
2697 /* Create a cp_lexer structure which will emit the tokens in CACHE
2698 and push it onto the parser's lexer stack. This is used for delayed
2699 parsing of in-class method bodies and default arguments, and should
2700 not be confused with tentative parsing. */
2701 static void
2702 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2704 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2705 lexer->next = parser->lexer;
2706 parser->lexer = lexer;
2708 /* Move the current source position to that of the first token in the
2709 new lexer. */
2710 cp_lexer_set_source_position_from_token (lexer->next_token);
2713 /* Pop the top lexer off the parser stack. This is never used for the
2714 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2715 static void
2716 cp_parser_pop_lexer (cp_parser *parser)
2718 cp_lexer *lexer = parser->lexer;
2719 parser->lexer = lexer->next;
2720 cp_lexer_destroy (lexer);
2722 /* Put the current source position back where it was before this
2723 lexer was pushed. */
2724 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2727 /* Lexical conventions [gram.lex] */
2729 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2730 identifier. */
2732 static tree
2733 cp_parser_identifier (cp_parser* parser)
2735 cp_token *token;
2737 /* Look for the identifier. */
2738 token = cp_parser_require (parser, CPP_NAME, "identifier");
2739 /* Return the value. */
2740 return token ? token->u.value : error_mark_node;
2743 /* Parse a sequence of adjacent string constants. Returns a
2744 TREE_STRING representing the combined, nul-terminated string
2745 constant. If TRANSLATE is true, translate the string to the
2746 execution character set. If WIDE_OK is true, a wide string is
2747 invalid here.
2749 C++98 [lex.string] says that if a narrow string literal token is
2750 adjacent to a wide string literal token, the behavior is undefined.
2751 However, C99 6.4.5p4 says that this results in a wide string literal.
2752 We follow C99 here, for consistency with the C front end.
2754 This code is largely lifted from lex_string() in c-lex.c.
2756 FUTURE: ObjC++ will need to handle @-strings here. */
2757 static tree
2758 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2760 tree value;
2761 bool wide = false;
2762 size_t count;
2763 struct obstack str_ob;
2764 cpp_string str, istr, *strs;
2765 cp_token *tok;
2767 tok = cp_lexer_peek_token (parser->lexer);
2768 if (!cp_parser_is_string_literal (tok))
2770 cp_parser_error (parser, "expected string-literal");
2771 return error_mark_node;
2774 /* Try to avoid the overhead of creating and destroying an obstack
2775 for the common case of just one string. */
2776 if (!cp_parser_is_string_literal
2777 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2779 cp_lexer_consume_token (parser->lexer);
2781 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2782 str.len = TREE_STRING_LENGTH (tok->u.value);
2783 count = 1;
2784 if (tok->type == CPP_WSTRING)
2785 wide = true;
2787 strs = &str;
2789 else
2791 gcc_obstack_init (&str_ob);
2792 count = 0;
2796 cp_lexer_consume_token (parser->lexer);
2797 count++;
2798 str.text = (unsigned char *)TREE_STRING_POINTER (tok->u.value);
2799 str.len = TREE_STRING_LENGTH (tok->u.value);
2800 if (tok->type == CPP_WSTRING)
2801 wide = true;
2803 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2805 tok = cp_lexer_peek_token (parser->lexer);
2807 while (cp_parser_is_string_literal (tok));
2809 strs = (cpp_string *) obstack_finish (&str_ob);
2812 if (wide && !wide_ok)
2814 cp_parser_error (parser, "a wide string is invalid in this context");
2815 wide = false;
2818 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2819 (parse_in, strs, count, &istr, wide))
2821 value = build_string (istr.len, (char *)istr.text);
2822 free ((void *)istr.text);
2824 TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2825 value = fix_string_type (value);
2827 else
2828 /* cpp_interpret_string has issued an error. */
2829 value = error_mark_node;
2831 if (count > 1)
2832 obstack_free (&str_ob, 0);
2834 return value;
2838 /* Basic concepts [gram.basic] */
2840 /* Parse a translation-unit.
2842 translation-unit:
2843 declaration-seq [opt]
2845 Returns TRUE if all went well. */
2847 static bool
2848 cp_parser_translation_unit (cp_parser* parser)
2850 /* The address of the first non-permanent object on the declarator
2851 obstack. */
2852 static void *declarator_obstack_base;
2854 bool success;
2856 /* Create the declarator obstack, if necessary. */
2857 if (!cp_error_declarator)
2859 gcc_obstack_init (&declarator_obstack);
2860 /* Create the error declarator. */
2861 cp_error_declarator = make_declarator (cdk_error);
2862 /* Create the empty parameter list. */
2863 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2864 /* Remember where the base of the declarator obstack lies. */
2865 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2868 cp_parser_declaration_seq_opt (parser);
2870 /* If there are no tokens left then all went well. */
2871 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2873 /* Get rid of the token array; we don't need it any more. */
2874 cp_lexer_destroy (parser->lexer);
2875 parser->lexer = NULL;
2877 /* This file might have been a context that's implicitly extern
2878 "C". If so, pop the lang context. (Only relevant for PCH.) */
2879 if (parser->implicit_extern_c)
2881 pop_lang_context ();
2882 parser->implicit_extern_c = false;
2885 /* Finish up. */
2886 finish_translation_unit ();
2888 success = true;
2890 else
2892 cp_parser_error (parser, "expected declaration");
2893 success = false;
2896 /* Make sure the declarator obstack was fully cleaned up. */
2897 gcc_assert (obstack_next_free (&declarator_obstack)
2898 == declarator_obstack_base);
2900 /* All went well. */
2901 return success;
2904 /* Expressions [gram.expr] */
2906 /* Parse a primary-expression.
2908 primary-expression:
2909 literal
2910 this
2911 ( expression )
2912 id-expression
2914 GNU Extensions:
2916 primary-expression:
2917 ( compound-statement )
2918 __builtin_va_arg ( assignment-expression , type-id )
2919 __builtin_offsetof ( type-id , offsetof-expression )
2921 Objective-C++ Extension:
2923 primary-expression:
2924 objc-expression
2926 literal:
2927 __null
2929 ADDRESS_P is true iff this expression was immediately preceded by
2930 "&" and therefore might denote a pointer-to-member. CAST_P is true
2931 iff this expression is the target of a cast. TEMPLATE_ARG_P is
2932 true iff this expression is a template argument.
2934 Returns a representation of the expression. Upon return, *IDK
2935 indicates what kind of id-expression (if any) was present. */
2937 static tree
2938 cp_parser_primary_expression (cp_parser *parser,
2939 bool address_p,
2940 bool cast_p,
2941 bool template_arg_p,
2942 cp_id_kind *idk)
2944 cp_token *token;
2946 /* Assume the primary expression is not an id-expression. */
2947 *idk = CP_ID_KIND_NONE;
2949 /* Peek at the next token. */
2950 token = cp_lexer_peek_token (parser->lexer);
2951 switch (token->type)
2953 /* literal:
2954 integer-literal
2955 character-literal
2956 floating-literal
2957 string-literal
2958 boolean-literal */
2959 case CPP_CHAR:
2960 case CPP_WCHAR:
2961 case CPP_NUMBER:
2962 token = cp_lexer_consume_token (parser->lexer);
2963 /* Floating-point literals are only allowed in an integral
2964 constant expression if they are cast to an integral or
2965 enumeration type. */
2966 if (TREE_CODE (token->u.value) == REAL_CST
2967 && parser->integral_constant_expression_p
2968 && pedantic)
2970 /* CAST_P will be set even in invalid code like "int(2.7 +
2971 ...)". Therefore, we have to check that the next token
2972 is sure to end the cast. */
2973 if (cast_p)
2975 cp_token *next_token;
2977 next_token = cp_lexer_peek_token (parser->lexer);
2978 if (/* The comma at the end of an
2979 enumerator-definition. */
2980 next_token->type != CPP_COMMA
2981 /* The curly brace at the end of an enum-specifier. */
2982 && next_token->type != CPP_CLOSE_BRACE
2983 /* The end of a statement. */
2984 && next_token->type != CPP_SEMICOLON
2985 /* The end of the cast-expression. */
2986 && next_token->type != CPP_CLOSE_PAREN
2987 /* The end of an array bound. */
2988 && next_token->type != CPP_CLOSE_SQUARE
2989 /* The closing ">" in a template-argument-list. */
2990 && (next_token->type != CPP_GREATER
2991 || parser->greater_than_is_operator_p))
2992 cast_p = false;
2995 /* If we are within a cast, then the constraint that the
2996 cast is to an integral or enumeration type will be
2997 checked at that point. If we are not within a cast, then
2998 this code is invalid. */
2999 if (!cast_p)
3000 cp_parser_non_integral_constant_expression
3001 (parser, "floating-point literal");
3003 return token->u.value;
3005 case CPP_STRING:
3006 case CPP_WSTRING:
3007 /* ??? Should wide strings be allowed when parser->translate_strings_p
3008 is false (i.e. in attributes)? If not, we can kill the third
3009 argument to cp_parser_string_literal. */
3010 return cp_parser_string_literal (parser,
3011 parser->translate_strings_p,
3012 true);
3014 case CPP_OPEN_PAREN:
3016 tree expr;
3017 bool saved_greater_than_is_operator_p;
3019 /* Consume the `('. */
3020 cp_lexer_consume_token (parser->lexer);
3021 /* Within a parenthesized expression, a `>' token is always
3022 the greater-than operator. */
3023 saved_greater_than_is_operator_p
3024 = parser->greater_than_is_operator_p;
3025 parser->greater_than_is_operator_p = true;
3026 /* If we see `( { ' then we are looking at the beginning of
3027 a GNU statement-expression. */
3028 if (cp_parser_allow_gnu_extensions_p (parser)
3029 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3031 /* Statement-expressions are not allowed by the standard. */
3032 if (pedantic)
3033 pedwarn ("ISO C++ forbids braced-groups within expressions");
3035 /* And they're not allowed outside of a function-body; you
3036 cannot, for example, write:
3038 int i = ({ int j = 3; j + 1; });
3040 at class or namespace scope. */
3041 if (!parser->in_function_body)
3043 error ("statement-expressions are allowed only inside functions");
3044 cp_parser_skip_to_end_of_block_or_statement (parser);
3045 expr = error_mark_node;
3047 else
3049 /* Start the statement-expression. */
3050 expr = begin_stmt_expr ();
3051 /* Parse the compound-statement. */
3052 cp_parser_compound_statement (parser, expr, false);
3053 /* Finish up. */
3054 expr = finish_stmt_expr (expr, false);
3057 else
3059 /* Parse the parenthesized expression. */
3060 expr = cp_parser_expression (parser, cast_p);
3061 /* Let the front end know that this expression was
3062 enclosed in parentheses. This matters in case, for
3063 example, the expression is of the form `A::B', since
3064 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3065 not. */
3066 finish_parenthesized_expr (expr);
3068 /* The `>' token might be the end of a template-id or
3069 template-parameter-list now. */
3070 parser->greater_than_is_operator_p
3071 = saved_greater_than_is_operator_p;
3072 /* Consume the `)'. */
3073 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
3074 cp_parser_skip_to_end_of_statement (parser);
3076 return expr;
3079 case CPP_KEYWORD:
3080 switch (token->keyword)
3082 /* These two are the boolean literals. */
3083 case RID_TRUE:
3084 cp_lexer_consume_token (parser->lexer);
3085 return boolean_true_node;
3086 case RID_FALSE:
3087 cp_lexer_consume_token (parser->lexer);
3088 return boolean_false_node;
3090 /* The `__null' literal. */
3091 case RID_NULL:
3092 cp_lexer_consume_token (parser->lexer);
3093 return null_node;
3095 /* Recognize the `this' keyword. */
3096 case RID_THIS:
3097 cp_lexer_consume_token (parser->lexer);
3098 if (parser->local_variables_forbidden_p)
3100 error ("%<this%> may not be used in this context");
3101 return error_mark_node;
3103 /* Pointers cannot appear in constant-expressions. */
3104 if (cp_parser_non_integral_constant_expression (parser,
3105 "`this'"))
3106 return error_mark_node;
3107 return finish_this_expr ();
3109 /* The `operator' keyword can be the beginning of an
3110 id-expression. */
3111 case RID_OPERATOR:
3112 goto id_expression;
3114 case RID_FUNCTION_NAME:
3115 case RID_PRETTY_FUNCTION_NAME:
3116 case RID_C99_FUNCTION_NAME:
3117 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3118 __func__ are the names of variables -- but they are
3119 treated specially. Therefore, they are handled here,
3120 rather than relying on the generic id-expression logic
3121 below. Grammatically, these names are id-expressions.
3123 Consume the token. */
3124 token = cp_lexer_consume_token (parser->lexer);
3125 /* Look up the name. */
3126 return finish_fname (token->u.value);
3128 case RID_VA_ARG:
3130 tree expression;
3131 tree type;
3133 /* The `__builtin_va_arg' construct is used to handle
3134 `va_arg'. Consume the `__builtin_va_arg' token. */
3135 cp_lexer_consume_token (parser->lexer);
3136 /* Look for the opening `('. */
3137 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3138 /* Now, parse the assignment-expression. */
3139 expression = cp_parser_assignment_expression (parser,
3140 /*cast_p=*/false);
3141 /* Look for the `,'. */
3142 cp_parser_require (parser, CPP_COMMA, "`,'");
3143 /* Parse the type-id. */
3144 type = cp_parser_type_id (parser);
3145 /* Look for the closing `)'. */
3146 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3147 /* Using `va_arg' in a constant-expression is not
3148 allowed. */
3149 if (cp_parser_non_integral_constant_expression (parser,
3150 "`va_arg'"))
3151 return error_mark_node;
3152 return build_x_va_arg (expression, type);
3155 case RID_OFFSETOF:
3156 return cp_parser_builtin_offsetof (parser);
3158 /* Objective-C++ expressions. */
3159 case RID_AT_ENCODE:
3160 case RID_AT_PROTOCOL:
3161 case RID_AT_SELECTOR:
3162 return cp_parser_objc_expression (parser);
3164 default:
3165 cp_parser_error (parser, "expected primary-expression");
3166 return error_mark_node;
3169 /* An id-expression can start with either an identifier, a
3170 `::' as the beginning of a qualified-id, or the "operator"
3171 keyword. */
3172 case CPP_NAME:
3173 case CPP_SCOPE:
3174 case CPP_TEMPLATE_ID:
3175 case CPP_NESTED_NAME_SPECIFIER:
3177 tree id_expression;
3178 tree decl;
3179 const char *error_msg;
3180 bool template_p;
3181 bool done;
3183 id_expression:
3184 /* Parse the id-expression. */
3185 id_expression
3186 = cp_parser_id_expression (parser,
3187 /*template_keyword_p=*/false,
3188 /*check_dependency_p=*/true,
3189 &template_p,
3190 /*declarator_p=*/false,
3191 /*optional_p=*/false);
3192 if (id_expression == error_mark_node)
3193 return error_mark_node;
3194 token = cp_lexer_peek_token (parser->lexer);
3195 done = (token->type != CPP_OPEN_SQUARE
3196 && token->type != CPP_OPEN_PAREN
3197 && token->type != CPP_DOT
3198 && token->type != CPP_DEREF
3199 && token->type != CPP_PLUS_PLUS
3200 && token->type != CPP_MINUS_MINUS);
3201 /* If we have a template-id, then no further lookup is
3202 required. If the template-id was for a template-class, we
3203 will sometimes have a TYPE_DECL at this point. */
3204 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3205 || TREE_CODE (id_expression) == TYPE_DECL)
3206 decl = id_expression;
3207 /* Look up the name. */
3208 else
3210 tree ambiguous_decls;
3212 decl = cp_parser_lookup_name (parser, id_expression,
3213 none_type,
3214 template_p,
3215 /*is_namespace=*/false,
3216 /*check_dependency=*/true,
3217 &ambiguous_decls);
3218 /* If the lookup was ambiguous, an error will already have
3219 been issued. */
3220 if (ambiguous_decls)
3221 return error_mark_node;
3223 /* In Objective-C++, an instance variable (ivar) may be preferred
3224 to whatever cp_parser_lookup_name() found. */
3225 decl = objc_lookup_ivar (decl, id_expression);
3227 /* If name lookup gives us a SCOPE_REF, then the
3228 qualifying scope was dependent. */
3229 if (TREE_CODE (decl) == SCOPE_REF)
3230 return decl;
3231 /* Check to see if DECL is a local variable in a context
3232 where that is forbidden. */
3233 if (parser->local_variables_forbidden_p
3234 && local_variable_p (decl))
3236 /* It might be that we only found DECL because we are
3237 trying to be generous with pre-ISO scoping rules.
3238 For example, consider:
3240 int i;
3241 void g() {
3242 for (int i = 0; i < 10; ++i) {}
3243 extern void f(int j = i);
3246 Here, name look up will originally find the out
3247 of scope `i'. We need to issue a warning message,
3248 but then use the global `i'. */
3249 decl = check_for_out_of_scope_variable (decl);
3250 if (local_variable_p (decl))
3252 error ("local variable %qD may not appear in this context",
3253 decl);
3254 return error_mark_node;
3259 decl = (finish_id_expression
3260 (id_expression, decl, parser->scope,
3261 idk,
3262 parser->integral_constant_expression_p,
3263 parser->allow_non_integral_constant_expression_p,
3264 &parser->non_integral_constant_expression_p,
3265 template_p, done, address_p,
3266 template_arg_p,
3267 &error_msg));
3268 if (error_msg)
3269 cp_parser_error (parser, error_msg);
3270 return decl;
3273 /* Anything else is an error. */
3274 default:
3275 /* ...unless we have an Objective-C++ message or string literal,
3276 that is. */
3277 if (c_dialect_objc ()
3278 && (token->type == CPP_OPEN_SQUARE
3279 || token->type == CPP_OBJC_STRING))
3280 return cp_parser_objc_expression (parser);
3282 cp_parser_error (parser, "expected primary-expression");
3283 return error_mark_node;
3287 /* Parse an id-expression.
3289 id-expression:
3290 unqualified-id
3291 qualified-id
3293 qualified-id:
3294 :: [opt] nested-name-specifier template [opt] unqualified-id
3295 :: identifier
3296 :: operator-function-id
3297 :: template-id
3299 Return a representation of the unqualified portion of the
3300 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3301 a `::' or nested-name-specifier.
3303 Often, if the id-expression was a qualified-id, the caller will
3304 want to make a SCOPE_REF to represent the qualified-id. This
3305 function does not do this in order to avoid wastefully creating
3306 SCOPE_REFs when they are not required.
3308 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3309 `template' keyword.
3311 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3312 uninstantiated templates.
3314 If *TEMPLATE_P is non-NULL, it is set to true iff the
3315 `template' keyword is used to explicitly indicate that the entity
3316 named is a template.
3318 If DECLARATOR_P is true, the id-expression is appearing as part of
3319 a declarator, rather than as part of an expression. */
3321 static tree
3322 cp_parser_id_expression (cp_parser *parser,
3323 bool template_keyword_p,
3324 bool check_dependency_p,
3325 bool *template_p,
3326 bool declarator_p,
3327 bool optional_p)
3329 bool global_scope_p;
3330 bool nested_name_specifier_p;
3332 /* Assume the `template' keyword was not used. */
3333 if (template_p)
3334 *template_p = template_keyword_p;
3336 /* Look for the optional `::' operator. */
3337 global_scope_p
3338 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3339 != NULL_TREE);
3340 /* Look for the optional nested-name-specifier. */
3341 nested_name_specifier_p
3342 = (cp_parser_nested_name_specifier_opt (parser,
3343 /*typename_keyword_p=*/false,
3344 check_dependency_p,
3345 /*type_p=*/false,
3346 declarator_p)
3347 != NULL_TREE);
3348 /* If there is a nested-name-specifier, then we are looking at
3349 the first qualified-id production. */
3350 if (nested_name_specifier_p)
3352 tree saved_scope;
3353 tree saved_object_scope;
3354 tree saved_qualifying_scope;
3355 tree unqualified_id;
3356 bool is_template;
3358 /* See if the next token is the `template' keyword. */
3359 if (!template_p)
3360 template_p = &is_template;
3361 *template_p = cp_parser_optional_template_keyword (parser);
3362 /* Name lookup we do during the processing of the
3363 unqualified-id might obliterate SCOPE. */
3364 saved_scope = parser->scope;
3365 saved_object_scope = parser->object_scope;
3366 saved_qualifying_scope = parser->qualifying_scope;
3367 /* Process the final unqualified-id. */
3368 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3369 check_dependency_p,
3370 declarator_p,
3371 /*optional_p=*/false);
3372 /* Restore the SAVED_SCOPE for our caller. */
3373 parser->scope = saved_scope;
3374 parser->object_scope = saved_object_scope;
3375 parser->qualifying_scope = saved_qualifying_scope;
3377 return unqualified_id;
3379 /* Otherwise, if we are in global scope, then we are looking at one
3380 of the other qualified-id productions. */
3381 else if (global_scope_p)
3383 cp_token *token;
3384 tree id;
3386 /* Peek at the next token. */
3387 token = cp_lexer_peek_token (parser->lexer);
3389 /* If it's an identifier, and the next token is not a "<", then
3390 we can avoid the template-id case. This is an optimization
3391 for this common case. */
3392 if (token->type == CPP_NAME
3393 && !cp_parser_nth_token_starts_template_argument_list_p
3394 (parser, 2))
3395 return cp_parser_identifier (parser);
3397 cp_parser_parse_tentatively (parser);
3398 /* Try a template-id. */
3399 id = cp_parser_template_id (parser,
3400 /*template_keyword_p=*/false,
3401 /*check_dependency_p=*/true,
3402 declarator_p);
3403 /* If that worked, we're done. */
3404 if (cp_parser_parse_definitely (parser))
3405 return id;
3407 /* Peek at the next token. (Changes in the token buffer may
3408 have invalidated the pointer obtained above.) */
3409 token = cp_lexer_peek_token (parser->lexer);
3411 switch (token->type)
3413 case CPP_NAME:
3414 return cp_parser_identifier (parser);
3416 case CPP_KEYWORD:
3417 if (token->keyword == RID_OPERATOR)
3418 return cp_parser_operator_function_id (parser);
3419 /* Fall through. */
3421 default:
3422 cp_parser_error (parser, "expected id-expression");
3423 return error_mark_node;
3426 else
3427 return cp_parser_unqualified_id (parser, template_keyword_p,
3428 /*check_dependency_p=*/true,
3429 declarator_p,
3430 optional_p);
3433 /* Parse an unqualified-id.
3435 unqualified-id:
3436 identifier
3437 operator-function-id
3438 conversion-function-id
3439 ~ class-name
3440 template-id
3442 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3443 keyword, in a construct like `A::template ...'.
3445 Returns a representation of unqualified-id. For the `identifier'
3446 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3447 production a BIT_NOT_EXPR is returned; the operand of the
3448 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3449 other productions, see the documentation accompanying the
3450 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3451 names are looked up in uninstantiated templates. If DECLARATOR_P
3452 is true, the unqualified-id is appearing as part of a declarator,
3453 rather than as part of an expression. */
3455 static tree
3456 cp_parser_unqualified_id (cp_parser* parser,
3457 bool template_keyword_p,
3458 bool check_dependency_p,
3459 bool declarator_p,
3460 bool optional_p)
3462 cp_token *token;
3464 /* Peek at the next token. */
3465 token = cp_lexer_peek_token (parser->lexer);
3467 switch (token->type)
3469 case CPP_NAME:
3471 tree id;
3473 /* We don't know yet whether or not this will be a
3474 template-id. */
3475 cp_parser_parse_tentatively (parser);
3476 /* Try a template-id. */
3477 id = cp_parser_template_id (parser, template_keyword_p,
3478 check_dependency_p,
3479 declarator_p);
3480 /* If it worked, we're done. */
3481 if (cp_parser_parse_definitely (parser))
3482 return id;
3483 /* Otherwise, it's an ordinary identifier. */
3484 return cp_parser_identifier (parser);
3487 case CPP_TEMPLATE_ID:
3488 return cp_parser_template_id (parser, template_keyword_p,
3489 check_dependency_p,
3490 declarator_p);
3492 case CPP_COMPL:
3494 tree type_decl;
3495 tree qualifying_scope;
3496 tree object_scope;
3497 tree scope;
3498 bool done;
3500 /* Consume the `~' token. */
3501 cp_lexer_consume_token (parser->lexer);
3502 /* Parse the class-name. The standard, as written, seems to
3503 say that:
3505 template <typename T> struct S { ~S (); };
3506 template <typename T> S<T>::~S() {}
3508 is invalid, since `~' must be followed by a class-name, but
3509 `S<T>' is dependent, and so not known to be a class.
3510 That's not right; we need to look in uninstantiated
3511 templates. A further complication arises from:
3513 template <typename T> void f(T t) {
3514 t.T::~T();
3517 Here, it is not possible to look up `T' in the scope of `T'
3518 itself. We must look in both the current scope, and the
3519 scope of the containing complete expression.
3521 Yet another issue is:
3523 struct S {
3524 int S;
3525 ~S();
3528 S::~S() {}
3530 The standard does not seem to say that the `S' in `~S'
3531 should refer to the type `S' and not the data member
3532 `S::S'. */
3534 /* DR 244 says that we look up the name after the "~" in the
3535 same scope as we looked up the qualifying name. That idea
3536 isn't fully worked out; it's more complicated than that. */
3537 scope = parser->scope;
3538 object_scope = parser->object_scope;
3539 qualifying_scope = parser->qualifying_scope;
3541 /* Check for invalid scopes. */
3542 if (scope == error_mark_node)
3544 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3545 cp_lexer_consume_token (parser->lexer);
3546 return error_mark_node;
3548 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3550 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3551 error ("scope %qT before %<~%> is not a class-name", scope);
3552 cp_parser_simulate_error (parser);
3553 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3554 cp_lexer_consume_token (parser->lexer);
3555 return error_mark_node;
3557 gcc_assert (!scope || TYPE_P (scope));
3559 /* If the name is of the form "X::~X" it's OK. */
3560 token = cp_lexer_peek_token (parser->lexer);
3561 if (scope
3562 && token->type == CPP_NAME
3563 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3564 == CPP_OPEN_PAREN)
3565 && constructor_name_p (token->u.value, scope))
3567 cp_lexer_consume_token (parser->lexer);
3568 return build_nt (BIT_NOT_EXPR, scope);
3571 /* If there was an explicit qualification (S::~T), first look
3572 in the scope given by the qualification (i.e., S). */
3573 done = false;
3574 type_decl = NULL_TREE;
3575 if (scope)
3577 cp_parser_parse_tentatively (parser);
3578 type_decl = cp_parser_class_name (parser,
3579 /*typename_keyword_p=*/false,
3580 /*template_keyword_p=*/false,
3581 none_type,
3582 /*check_dependency=*/false,
3583 /*class_head_p=*/false,
3584 declarator_p);
3585 if (cp_parser_parse_definitely (parser))
3586 done = true;
3588 /* In "N::S::~S", look in "N" as well. */
3589 if (!done && scope && qualifying_scope)
3591 cp_parser_parse_tentatively (parser);
3592 parser->scope = qualifying_scope;
3593 parser->object_scope = NULL_TREE;
3594 parser->qualifying_scope = NULL_TREE;
3595 type_decl
3596 = cp_parser_class_name (parser,
3597 /*typename_keyword_p=*/false,
3598 /*template_keyword_p=*/false,
3599 none_type,
3600 /*check_dependency=*/false,
3601 /*class_head_p=*/false,
3602 declarator_p);
3603 if (cp_parser_parse_definitely (parser))
3604 done = true;
3606 /* In "p->S::~T", look in the scope given by "*p" as well. */
3607 else if (!done && object_scope)
3609 cp_parser_parse_tentatively (parser);
3610 parser->scope = object_scope;
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);
3621 if (cp_parser_parse_definitely (parser))
3622 done = true;
3624 /* Look in the surrounding context. */
3625 if (!done)
3627 parser->scope = NULL_TREE;
3628 parser->object_scope = NULL_TREE;
3629 parser->qualifying_scope = NULL_TREE;
3630 type_decl
3631 = cp_parser_class_name (parser,
3632 /*typename_keyword_p=*/false,
3633 /*template_keyword_p=*/false,
3634 none_type,
3635 /*check_dependency=*/false,
3636 /*class_head_p=*/false,
3637 declarator_p);
3639 /* If an error occurred, assume that the name of the
3640 destructor is the same as the name of the qualifying
3641 class. That allows us to keep parsing after running
3642 into ill-formed destructor names. */
3643 if (type_decl == error_mark_node && scope)
3644 return build_nt (BIT_NOT_EXPR, scope);
3645 else if (type_decl == error_mark_node)
3646 return error_mark_node;
3648 /* Check that destructor name and scope match. */
3649 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3651 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3652 error ("declaration of %<~%T%> as member of %qT",
3653 type_decl, scope);
3654 cp_parser_simulate_error (parser);
3655 return error_mark_node;
3658 /* [class.dtor]
3660 A typedef-name that names a class shall not be used as the
3661 identifier in the declarator for a destructor declaration. */
3662 if (declarator_p
3663 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3664 && !DECL_SELF_REFERENCE_P (type_decl)
3665 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3666 error ("typedef-name %qD used as destructor declarator",
3667 type_decl);
3669 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3672 case CPP_KEYWORD:
3673 if (token->keyword == RID_OPERATOR)
3675 tree id;
3677 /* This could be a template-id, so we try that first. */
3678 cp_parser_parse_tentatively (parser);
3679 /* Try a template-id. */
3680 id = cp_parser_template_id (parser, template_keyword_p,
3681 /*check_dependency_p=*/true,
3682 declarator_p);
3683 /* If that worked, we're done. */
3684 if (cp_parser_parse_definitely (parser))
3685 return id;
3686 /* We still don't know whether we're looking at an
3687 operator-function-id or a conversion-function-id. */
3688 cp_parser_parse_tentatively (parser);
3689 /* Try an operator-function-id. */
3690 id = cp_parser_operator_function_id (parser);
3691 /* If that didn't work, try a conversion-function-id. */
3692 if (!cp_parser_parse_definitely (parser))
3693 id = cp_parser_conversion_function_id (parser);
3695 return id;
3697 /* Fall through. */
3699 default:
3700 if (optional_p)
3701 return NULL_TREE;
3702 cp_parser_error (parser, "expected unqualified-id");
3703 return error_mark_node;
3707 /* Parse an (optional) nested-name-specifier.
3709 nested-name-specifier:
3710 class-or-namespace-name :: nested-name-specifier [opt]
3711 class-or-namespace-name :: template nested-name-specifier [opt]
3713 PARSER->SCOPE should be set appropriately before this function is
3714 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3715 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3716 in name lookups.
3718 Sets PARSER->SCOPE to the class (TYPE) or namespace
3719 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3720 it unchanged if there is no nested-name-specifier. Returns the new
3721 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3723 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3724 part of a declaration and/or decl-specifier. */
3726 static tree
3727 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3728 bool typename_keyword_p,
3729 bool check_dependency_p,
3730 bool type_p,
3731 bool is_declaration)
3733 bool success = false;
3734 cp_token_position start = 0;
3735 cp_token *token;
3737 /* Remember where the nested-name-specifier starts. */
3738 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3740 start = cp_lexer_token_position (parser->lexer, false);
3741 push_deferring_access_checks (dk_deferred);
3744 while (true)
3746 tree new_scope;
3747 tree old_scope;
3748 tree saved_qualifying_scope;
3749 bool template_keyword_p;
3751 /* Spot cases that cannot be the beginning of a
3752 nested-name-specifier. */
3753 token = cp_lexer_peek_token (parser->lexer);
3755 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3756 the already parsed nested-name-specifier. */
3757 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3759 /* Grab the nested-name-specifier and continue the loop. */
3760 cp_parser_pre_parsed_nested_name_specifier (parser);
3761 /* If we originally encountered this nested-name-specifier
3762 with IS_DECLARATION set to false, we will not have
3763 resolved TYPENAME_TYPEs, so we must do so here. */
3764 if (is_declaration
3765 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3767 new_scope = resolve_typename_type (parser->scope,
3768 /*only_current_p=*/false);
3769 if (new_scope != error_mark_node)
3770 parser->scope = new_scope;
3772 success = true;
3773 continue;
3776 /* Spot cases that cannot be the beginning of a
3777 nested-name-specifier. On the second and subsequent times
3778 through the loop, we look for the `template' keyword. */
3779 if (success && token->keyword == RID_TEMPLATE)
3781 /* A template-id can start a nested-name-specifier. */
3782 else if (token->type == CPP_TEMPLATE_ID)
3784 else
3786 /* If the next token is not an identifier, then it is
3787 definitely not a class-or-namespace-name. */
3788 if (token->type != CPP_NAME)
3789 break;
3790 /* If the following token is neither a `<' (to begin a
3791 template-id), nor a `::', then we are not looking at a
3792 nested-name-specifier. */
3793 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3794 if (token->type != CPP_SCOPE
3795 && !cp_parser_nth_token_starts_template_argument_list_p
3796 (parser, 2))
3797 break;
3800 /* The nested-name-specifier is optional, so we parse
3801 tentatively. */
3802 cp_parser_parse_tentatively (parser);
3804 /* Look for the optional `template' keyword, if this isn't the
3805 first time through the loop. */
3806 if (success)
3807 template_keyword_p = cp_parser_optional_template_keyword (parser);
3808 else
3809 template_keyword_p = false;
3811 /* Save the old scope since the name lookup we are about to do
3812 might destroy it. */
3813 old_scope = parser->scope;
3814 saved_qualifying_scope = parser->qualifying_scope;
3815 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3816 look up names in "X<T>::I" in order to determine that "Y" is
3817 a template. So, if we have a typename at this point, we make
3818 an effort to look through it. */
3819 if (is_declaration
3820 && !typename_keyword_p
3821 && parser->scope
3822 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3823 parser->scope = resolve_typename_type (parser->scope,
3824 /*only_current_p=*/false);
3825 /* Parse the qualifying entity. */
3826 new_scope
3827 = cp_parser_class_or_namespace_name (parser,
3828 typename_keyword_p,
3829 template_keyword_p,
3830 check_dependency_p,
3831 type_p,
3832 is_declaration);
3833 /* Look for the `::' token. */
3834 cp_parser_require (parser, CPP_SCOPE, "`::'");
3836 /* If we found what we wanted, we keep going; otherwise, we're
3837 done. */
3838 if (!cp_parser_parse_definitely (parser))
3840 bool error_p = false;
3842 /* Restore the OLD_SCOPE since it was valid before the
3843 failed attempt at finding the last
3844 class-or-namespace-name. */
3845 parser->scope = old_scope;
3846 parser->qualifying_scope = saved_qualifying_scope;
3847 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3848 break;
3849 /* If the next token is an identifier, and the one after
3850 that is a `::', then any valid interpretation would have
3851 found a class-or-namespace-name. */
3852 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3853 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3854 == CPP_SCOPE)
3855 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3856 != CPP_COMPL))
3858 token = cp_lexer_consume_token (parser->lexer);
3859 if (!error_p)
3861 if (!token->ambiguous_p)
3863 tree decl;
3864 tree ambiguous_decls;
3866 decl = cp_parser_lookup_name (parser, token->u.value,
3867 none_type,
3868 /*is_template=*/false,
3869 /*is_namespace=*/false,
3870 /*check_dependency=*/true,
3871 &ambiguous_decls);
3872 if (TREE_CODE (decl) == TEMPLATE_DECL)
3873 error ("%qD used without template parameters", decl);
3874 else if (ambiguous_decls)
3876 error ("reference to %qD is ambiguous",
3877 token->u.value);
3878 print_candidates (ambiguous_decls);
3879 decl = error_mark_node;
3881 else
3882 cp_parser_name_lookup_error
3883 (parser, token->u.value, decl,
3884 "is not a class or namespace");
3886 parser->scope = error_mark_node;
3887 error_p = true;
3888 /* Treat this as a successful nested-name-specifier
3889 due to:
3891 [basic.lookup.qual]
3893 If the name found is not a class-name (clause
3894 _class_) or namespace-name (_namespace.def_), the
3895 program is ill-formed. */
3896 success = true;
3898 cp_lexer_consume_token (parser->lexer);
3900 break;
3902 /* We've found one valid nested-name-specifier. */
3903 success = true;
3904 /* Name lookup always gives us a DECL. */
3905 if (TREE_CODE (new_scope) == TYPE_DECL)
3906 new_scope = TREE_TYPE (new_scope);
3907 /* Uses of "template" must be followed by actual templates. */
3908 if (template_keyword_p
3909 && !(CLASS_TYPE_P (new_scope)
3910 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
3911 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
3912 || CLASSTYPE_IS_TEMPLATE (new_scope)))
3913 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
3914 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
3915 == TEMPLATE_ID_EXPR)))
3916 pedwarn (TYPE_P (new_scope)
3917 ? "%qT is not a template"
3918 : "%qD is not a template",
3919 new_scope);
3920 /* If it is a class scope, try to complete it; we are about to
3921 be looking up names inside the class. */
3922 if (TYPE_P (new_scope)
3923 /* Since checking types for dependency can be expensive,
3924 avoid doing it if the type is already complete. */
3925 && !COMPLETE_TYPE_P (new_scope)
3926 /* Do not try to complete dependent types. */
3927 && !dependent_type_p (new_scope))
3928 new_scope = complete_type (new_scope);
3929 /* Make sure we look in the right scope the next time through
3930 the loop. */
3931 parser->scope = new_scope;
3934 /* If parsing tentatively, replace the sequence of tokens that makes
3935 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3936 token. That way, should we re-parse the token stream, we will
3937 not have to repeat the effort required to do the parse, nor will
3938 we issue duplicate error messages. */
3939 if (success && start)
3941 cp_token *token;
3943 token = cp_lexer_token_at (parser->lexer, start);
3944 /* Reset the contents of the START token. */
3945 token->type = CPP_NESTED_NAME_SPECIFIER;
3946 /* Retrieve any deferred checks. Do not pop this access checks yet
3947 so the memory will not be reclaimed during token replacing below. */
3948 token->u.tree_check_value = GGC_CNEW (struct tree_check);
3949 token->u.tree_check_value->value = parser->scope;
3950 token->u.tree_check_value->checks = get_deferred_access_checks ();
3951 token->u.tree_check_value->qualifying_scope =
3952 parser->qualifying_scope;
3953 token->keyword = RID_MAX;
3955 /* Purge all subsequent tokens. */
3956 cp_lexer_purge_tokens_after (parser->lexer, start);
3959 if (start)
3960 pop_to_parent_deferring_access_checks ();
3962 return success ? parser->scope : NULL_TREE;
3965 /* Parse a nested-name-specifier. See
3966 cp_parser_nested_name_specifier_opt for details. This function
3967 behaves identically, except that it will an issue an error if no
3968 nested-name-specifier is present. */
3970 static tree
3971 cp_parser_nested_name_specifier (cp_parser *parser,
3972 bool typename_keyword_p,
3973 bool check_dependency_p,
3974 bool type_p,
3975 bool is_declaration)
3977 tree scope;
3979 /* Look for the nested-name-specifier. */
3980 scope = cp_parser_nested_name_specifier_opt (parser,
3981 typename_keyword_p,
3982 check_dependency_p,
3983 type_p,
3984 is_declaration);
3985 /* If it was not present, issue an error message. */
3986 if (!scope)
3988 cp_parser_error (parser, "expected nested-name-specifier");
3989 parser->scope = NULL_TREE;
3992 return scope;
3995 /* Parse a class-or-namespace-name.
3997 class-or-namespace-name:
3998 class-name
3999 namespace-name
4001 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4002 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4003 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4004 TYPE_P is TRUE iff the next name should be taken as a class-name,
4005 even the same name is declared to be another entity in the same
4006 scope.
4008 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4009 specified by the class-or-namespace-name. If neither is found the
4010 ERROR_MARK_NODE is returned. */
4012 static tree
4013 cp_parser_class_or_namespace_name (cp_parser *parser,
4014 bool typename_keyword_p,
4015 bool template_keyword_p,
4016 bool check_dependency_p,
4017 bool type_p,
4018 bool is_declaration)
4020 tree saved_scope;
4021 tree saved_qualifying_scope;
4022 tree saved_object_scope;
4023 tree scope;
4024 bool only_class_p;
4026 /* Before we try to parse the class-name, we must save away the
4027 current PARSER->SCOPE since cp_parser_class_name will destroy
4028 it. */
4029 saved_scope = parser->scope;
4030 saved_qualifying_scope = parser->qualifying_scope;
4031 saved_object_scope = parser->object_scope;
4032 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4033 there is no need to look for a namespace-name. */
4034 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
4035 if (!only_class_p)
4036 cp_parser_parse_tentatively (parser);
4037 scope = cp_parser_class_name (parser,
4038 typename_keyword_p,
4039 template_keyword_p,
4040 type_p ? class_type : none_type,
4041 check_dependency_p,
4042 /*class_head_p=*/false,
4043 is_declaration);
4044 /* If that didn't work, try for a namespace-name. */
4045 if (!only_class_p && !cp_parser_parse_definitely (parser))
4047 /* Restore the saved scope. */
4048 parser->scope = saved_scope;
4049 parser->qualifying_scope = saved_qualifying_scope;
4050 parser->object_scope = saved_object_scope;
4051 /* If we are not looking at an identifier followed by the scope
4052 resolution operator, then this is not part of a
4053 nested-name-specifier. (Note that this function is only used
4054 to parse the components of a nested-name-specifier.) */
4055 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4056 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4057 return error_mark_node;
4058 scope = cp_parser_namespace_name (parser);
4061 return scope;
4064 /* Parse a postfix-expression.
4066 postfix-expression:
4067 primary-expression
4068 postfix-expression [ expression ]
4069 postfix-expression ( expression-list [opt] )
4070 simple-type-specifier ( expression-list [opt] )
4071 typename :: [opt] nested-name-specifier identifier
4072 ( expression-list [opt] )
4073 typename :: [opt] nested-name-specifier template [opt] template-id
4074 ( expression-list [opt] )
4075 postfix-expression . template [opt] id-expression
4076 postfix-expression -> template [opt] id-expression
4077 postfix-expression . pseudo-destructor-name
4078 postfix-expression -> pseudo-destructor-name
4079 postfix-expression ++
4080 postfix-expression --
4081 dynamic_cast < type-id > ( expression )
4082 static_cast < type-id > ( expression )
4083 reinterpret_cast < type-id > ( expression )
4084 const_cast < type-id > ( expression )
4085 typeid ( expression )
4086 typeid ( type-id )
4088 GNU Extension:
4090 postfix-expression:
4091 ( type-id ) { initializer-list , [opt] }
4093 This extension is a GNU version of the C99 compound-literal
4094 construct. (The C99 grammar uses `type-name' instead of `type-id',
4095 but they are essentially the same concept.)
4097 If ADDRESS_P is true, the postfix expression is the operand of the
4098 `&' operator. CAST_P is true if this expression is the target of a
4099 cast.
4101 Returns a representation of the expression. */
4103 static tree
4104 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
4106 cp_token *token;
4107 enum rid keyword;
4108 cp_id_kind idk = CP_ID_KIND_NONE;
4109 tree postfix_expression = NULL_TREE;
4111 /* Peek at the next token. */
4112 token = cp_lexer_peek_token (parser->lexer);
4113 /* Some of the productions are determined by keywords. */
4114 keyword = token->keyword;
4115 switch (keyword)
4117 case RID_DYNCAST:
4118 case RID_STATCAST:
4119 case RID_REINTCAST:
4120 case RID_CONSTCAST:
4122 tree type;
4123 tree expression;
4124 const char *saved_message;
4126 /* All of these can be handled in the same way from the point
4127 of view of parsing. Begin by consuming the token
4128 identifying the cast. */
4129 cp_lexer_consume_token (parser->lexer);
4131 /* New types cannot be defined in the cast. */
4132 saved_message = parser->type_definition_forbidden_message;
4133 parser->type_definition_forbidden_message
4134 = "types may not be defined in casts";
4136 /* Look for the opening `<'. */
4137 cp_parser_require (parser, CPP_LESS, "`<'");
4138 /* Parse the type to which we are casting. */
4139 type = cp_parser_type_id (parser);
4140 /* Look for the closing `>'. */
4141 cp_parser_require (parser, CPP_GREATER, "`>'");
4142 /* Restore the old message. */
4143 parser->type_definition_forbidden_message = saved_message;
4145 /* And the expression which is being cast. */
4146 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4147 expression = cp_parser_expression (parser, /*cast_p=*/true);
4148 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4150 /* Only type conversions to integral or enumeration types
4151 can be used in constant-expressions. */
4152 if (!cast_valid_in_integral_constant_expression_p (type)
4153 && (cp_parser_non_integral_constant_expression
4154 (parser,
4155 "a cast to a type other than an integral or "
4156 "enumeration type")))
4157 return error_mark_node;
4159 switch (keyword)
4161 case RID_DYNCAST:
4162 postfix_expression
4163 = build_dynamic_cast (type, expression);
4164 break;
4165 case RID_STATCAST:
4166 postfix_expression
4167 = build_static_cast (type, expression);
4168 break;
4169 case RID_REINTCAST:
4170 postfix_expression
4171 = build_reinterpret_cast (type, expression);
4172 break;
4173 case RID_CONSTCAST:
4174 postfix_expression
4175 = build_const_cast (type, expression);
4176 break;
4177 default:
4178 gcc_unreachable ();
4181 break;
4183 case RID_TYPEID:
4185 tree type;
4186 const char *saved_message;
4187 bool saved_in_type_id_in_expr_p;
4189 /* Consume the `typeid' token. */
4190 cp_lexer_consume_token (parser->lexer);
4191 /* Look for the `(' token. */
4192 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4193 /* Types cannot be defined in a `typeid' expression. */
4194 saved_message = parser->type_definition_forbidden_message;
4195 parser->type_definition_forbidden_message
4196 = "types may not be defined in a `typeid\' expression";
4197 /* We can't be sure yet whether we're looking at a type-id or an
4198 expression. */
4199 cp_parser_parse_tentatively (parser);
4200 /* Try a type-id first. */
4201 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4202 parser->in_type_id_in_expr_p = true;
4203 type = cp_parser_type_id (parser);
4204 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4205 /* Look for the `)' token. Otherwise, we can't be sure that
4206 we're not looking at an expression: consider `typeid (int
4207 (3))', for example. */
4208 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4209 /* If all went well, simply lookup the type-id. */
4210 if (cp_parser_parse_definitely (parser))
4211 postfix_expression = get_typeid (type);
4212 /* Otherwise, fall back to the expression variant. */
4213 else
4215 tree expression;
4217 /* Look for an expression. */
4218 expression = cp_parser_expression (parser, /*cast_p=*/false);
4219 /* Compute its typeid. */
4220 postfix_expression = build_typeid (expression);
4221 /* Look for the `)' token. */
4222 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4224 /* Restore the saved message. */
4225 parser->type_definition_forbidden_message = saved_message;
4226 /* `typeid' may not appear in an integral constant expression. */
4227 if (cp_parser_non_integral_constant_expression(parser,
4228 "`typeid' operator"))
4229 return error_mark_node;
4231 break;
4233 case RID_TYPENAME:
4235 tree type;
4236 /* The syntax permitted here is the same permitted for an
4237 elaborated-type-specifier. */
4238 type = cp_parser_elaborated_type_specifier (parser,
4239 /*is_friend=*/false,
4240 /*is_declaration=*/false);
4241 postfix_expression = cp_parser_functional_cast (parser, type);
4243 break;
4245 default:
4247 tree type;
4249 /* If the next thing is a simple-type-specifier, we may be
4250 looking at a functional cast. We could also be looking at
4251 an id-expression. So, we try the functional cast, and if
4252 that doesn't work we fall back to the primary-expression. */
4253 cp_parser_parse_tentatively (parser);
4254 /* Look for the simple-type-specifier. */
4255 type = cp_parser_simple_type_specifier (parser,
4256 /*decl_specs=*/NULL,
4257 CP_PARSER_FLAGS_NONE);
4258 /* Parse the cast itself. */
4259 if (!cp_parser_error_occurred (parser))
4260 postfix_expression
4261 = cp_parser_functional_cast (parser, type);
4262 /* If that worked, we're done. */
4263 if (cp_parser_parse_definitely (parser))
4264 break;
4266 /* If the functional-cast didn't work out, try a
4267 compound-literal. */
4268 if (cp_parser_allow_gnu_extensions_p (parser)
4269 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4271 VEC(constructor_elt,gc) *initializer_list = NULL;
4272 bool saved_in_type_id_in_expr_p;
4274 cp_parser_parse_tentatively (parser);
4275 /* Consume the `('. */
4276 cp_lexer_consume_token (parser->lexer);
4277 /* Parse the type. */
4278 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4279 parser->in_type_id_in_expr_p = true;
4280 type = cp_parser_type_id (parser);
4281 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4282 /* Look for the `)'. */
4283 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4284 /* Look for the `{'. */
4285 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4286 /* If things aren't going well, there's no need to
4287 keep going. */
4288 if (!cp_parser_error_occurred (parser))
4290 bool non_constant_p;
4291 /* Parse the initializer-list. */
4292 initializer_list
4293 = cp_parser_initializer_list (parser, &non_constant_p);
4294 /* Allow a trailing `,'. */
4295 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4296 cp_lexer_consume_token (parser->lexer);
4297 /* Look for the final `}'. */
4298 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4300 /* If that worked, we're definitely looking at a
4301 compound-literal expression. */
4302 if (cp_parser_parse_definitely (parser))
4304 /* Warn the user that a compound literal is not
4305 allowed in standard C++. */
4306 if (pedantic)
4307 pedwarn ("ISO C++ forbids compound-literals");
4308 /* Form the representation of the compound-literal. */
4309 postfix_expression
4310 = finish_compound_literal (type, initializer_list);
4311 break;
4315 /* It must be a primary-expression. */
4316 postfix_expression
4317 = cp_parser_primary_expression (parser, address_p, cast_p,
4318 /*template_arg_p=*/false,
4319 &idk);
4321 break;
4324 /* Keep looping until the postfix-expression is complete. */
4325 while (true)
4327 if (idk == CP_ID_KIND_UNQUALIFIED
4328 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4329 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4330 /* It is not a Koenig lookup function call. */
4331 postfix_expression
4332 = unqualified_name_lookup_error (postfix_expression);
4334 /* Peek at the next token. */
4335 token = cp_lexer_peek_token (parser->lexer);
4337 switch (token->type)
4339 case CPP_OPEN_SQUARE:
4340 postfix_expression
4341 = cp_parser_postfix_open_square_expression (parser,
4342 postfix_expression,
4343 false);
4344 idk = CP_ID_KIND_NONE;
4345 break;
4347 case CPP_OPEN_PAREN:
4348 /* postfix-expression ( expression-list [opt] ) */
4350 bool koenig_p;
4351 bool is_builtin_constant_p;
4352 bool saved_integral_constant_expression_p = false;
4353 bool saved_non_integral_constant_expression_p = false;
4354 tree args;
4356 is_builtin_constant_p
4357 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4358 if (is_builtin_constant_p)
4360 /* The whole point of __builtin_constant_p is to allow
4361 non-constant expressions to appear as arguments. */
4362 saved_integral_constant_expression_p
4363 = parser->integral_constant_expression_p;
4364 saved_non_integral_constant_expression_p
4365 = parser->non_integral_constant_expression_p;
4366 parser->integral_constant_expression_p = false;
4368 args = (cp_parser_parenthesized_expression_list
4369 (parser, /*is_attribute_list=*/false,
4370 /*cast_p=*/false,
4371 /*non_constant_p=*/NULL));
4372 if (is_builtin_constant_p)
4374 parser->integral_constant_expression_p
4375 = saved_integral_constant_expression_p;
4376 parser->non_integral_constant_expression_p
4377 = saved_non_integral_constant_expression_p;
4380 if (args == error_mark_node)
4382 postfix_expression = error_mark_node;
4383 break;
4386 /* Function calls are not permitted in
4387 constant-expressions. */
4388 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4389 && cp_parser_non_integral_constant_expression (parser,
4390 "a function call"))
4392 postfix_expression = error_mark_node;
4393 break;
4396 koenig_p = false;
4397 if (idk == CP_ID_KIND_UNQUALIFIED)
4399 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4401 if (args)
4403 koenig_p = true;
4404 postfix_expression
4405 = perform_koenig_lookup (postfix_expression, args);
4407 else
4408 postfix_expression
4409 = unqualified_fn_lookup_error (postfix_expression);
4411 /* We do not perform argument-dependent lookup if
4412 normal lookup finds a non-function, in accordance
4413 with the expected resolution of DR 218. */
4414 else if (args && is_overloaded_fn (postfix_expression))
4416 tree fn = get_first_fn (postfix_expression);
4418 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4419 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4421 /* Only do argument dependent lookup if regular
4422 lookup does not find a set of member functions.
4423 [basic.lookup.koenig]/2a */
4424 if (!DECL_FUNCTION_MEMBER_P (fn))
4426 koenig_p = true;
4427 postfix_expression
4428 = perform_koenig_lookup (postfix_expression, args);
4433 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4435 tree instance = TREE_OPERAND (postfix_expression, 0);
4436 tree fn = TREE_OPERAND (postfix_expression, 1);
4438 if (processing_template_decl
4439 && (type_dependent_expression_p (instance)
4440 || (!BASELINK_P (fn)
4441 && TREE_CODE (fn) != FIELD_DECL)
4442 || type_dependent_expression_p (fn)
4443 || any_type_dependent_arguments_p (args)))
4445 postfix_expression
4446 = build_nt_call_list (postfix_expression, args);
4447 break;
4450 if (BASELINK_P (fn))
4451 postfix_expression
4452 = (build_new_method_call
4453 (instance, fn, args, NULL_TREE,
4454 (idk == CP_ID_KIND_QUALIFIED
4455 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4456 /*fn_p=*/NULL));
4457 else
4458 postfix_expression
4459 = finish_call_expr (postfix_expression, args,
4460 /*disallow_virtual=*/false,
4461 /*koenig_p=*/false);
4463 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4464 || TREE_CODE (postfix_expression) == MEMBER_REF
4465 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4466 postfix_expression = (build_offset_ref_call_from_tree
4467 (postfix_expression, args));
4468 else if (idk == CP_ID_KIND_QUALIFIED)
4469 /* A call to a static class member, or a namespace-scope
4470 function. */
4471 postfix_expression
4472 = finish_call_expr (postfix_expression, args,
4473 /*disallow_virtual=*/true,
4474 koenig_p);
4475 else
4476 /* All other function calls. */
4477 postfix_expression
4478 = finish_call_expr (postfix_expression, args,
4479 /*disallow_virtual=*/false,
4480 koenig_p);
4482 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4483 idk = CP_ID_KIND_NONE;
4485 break;
4487 case CPP_DOT:
4488 case CPP_DEREF:
4489 /* postfix-expression . template [opt] id-expression
4490 postfix-expression . pseudo-destructor-name
4491 postfix-expression -> template [opt] id-expression
4492 postfix-expression -> pseudo-destructor-name */
4494 /* Consume the `.' or `->' operator. */
4495 cp_lexer_consume_token (parser->lexer);
4497 postfix_expression
4498 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4499 postfix_expression,
4500 false, &idk);
4501 break;
4503 case CPP_PLUS_PLUS:
4504 /* postfix-expression ++ */
4505 /* Consume the `++' token. */
4506 cp_lexer_consume_token (parser->lexer);
4507 /* Generate a representation for the complete expression. */
4508 postfix_expression
4509 = finish_increment_expr (postfix_expression,
4510 POSTINCREMENT_EXPR);
4511 /* Increments may not appear in constant-expressions. */
4512 if (cp_parser_non_integral_constant_expression (parser,
4513 "an increment"))
4514 postfix_expression = error_mark_node;
4515 idk = CP_ID_KIND_NONE;
4516 break;
4518 case CPP_MINUS_MINUS:
4519 /* postfix-expression -- */
4520 /* Consume the `--' token. */
4521 cp_lexer_consume_token (parser->lexer);
4522 /* Generate a representation for the complete expression. */
4523 postfix_expression
4524 = finish_increment_expr (postfix_expression,
4525 POSTDECREMENT_EXPR);
4526 /* Decrements may not appear in constant-expressions. */
4527 if (cp_parser_non_integral_constant_expression (parser,
4528 "a decrement"))
4529 postfix_expression = error_mark_node;
4530 idk = CP_ID_KIND_NONE;
4531 break;
4533 default:
4534 return postfix_expression;
4538 /* We should never get here. */
4539 gcc_unreachable ();
4540 return error_mark_node;
4543 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4544 by cp_parser_builtin_offsetof. We're looking for
4546 postfix-expression [ expression ]
4548 FOR_OFFSETOF is set if we're being called in that context, which
4549 changes how we deal with integer constant expressions. */
4551 static tree
4552 cp_parser_postfix_open_square_expression (cp_parser *parser,
4553 tree postfix_expression,
4554 bool for_offsetof)
4556 tree index;
4558 /* Consume the `[' token. */
4559 cp_lexer_consume_token (parser->lexer);
4561 /* Parse the index expression. */
4562 /* ??? For offsetof, there is a question of what to allow here. If
4563 offsetof is not being used in an integral constant expression context,
4564 then we *could* get the right answer by computing the value at runtime.
4565 If we are in an integral constant expression context, then we might
4566 could accept any constant expression; hard to say without analysis.
4567 Rather than open the barn door too wide right away, allow only integer
4568 constant expressions here. */
4569 if (for_offsetof)
4570 index = cp_parser_constant_expression (parser, false, NULL);
4571 else
4572 index = cp_parser_expression (parser, /*cast_p=*/false);
4574 /* Look for the closing `]'. */
4575 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4577 /* Build the ARRAY_REF. */
4578 postfix_expression = grok_array_decl (postfix_expression, index);
4580 /* When not doing offsetof, array references are not permitted in
4581 constant-expressions. */
4582 if (!for_offsetof
4583 && (cp_parser_non_integral_constant_expression
4584 (parser, "an array reference")))
4585 postfix_expression = error_mark_node;
4587 return postfix_expression;
4590 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4591 by cp_parser_builtin_offsetof. We're looking for
4593 postfix-expression . template [opt] id-expression
4594 postfix-expression . pseudo-destructor-name
4595 postfix-expression -> template [opt] id-expression
4596 postfix-expression -> pseudo-destructor-name
4598 FOR_OFFSETOF is set if we're being called in that context. That sorta
4599 limits what of the above we'll actually accept, but nevermind.
4600 TOKEN_TYPE is the "." or "->" token, which will already have been
4601 removed from the stream. */
4603 static tree
4604 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4605 enum cpp_ttype token_type,
4606 tree postfix_expression,
4607 bool for_offsetof, cp_id_kind *idk)
4609 tree name;
4610 bool dependent_p;
4611 bool pseudo_destructor_p;
4612 tree scope = NULL_TREE;
4614 /* If this is a `->' operator, dereference the pointer. */
4615 if (token_type == CPP_DEREF)
4616 postfix_expression = build_x_arrow (postfix_expression);
4617 /* Check to see whether or not the expression is type-dependent. */
4618 dependent_p = type_dependent_expression_p (postfix_expression);
4619 /* The identifier following the `->' or `.' is not qualified. */
4620 parser->scope = NULL_TREE;
4621 parser->qualifying_scope = NULL_TREE;
4622 parser->object_scope = NULL_TREE;
4623 *idk = CP_ID_KIND_NONE;
4624 /* Enter the scope corresponding to the type of the object
4625 given by the POSTFIX_EXPRESSION. */
4626 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4628 scope = TREE_TYPE (postfix_expression);
4629 /* According to the standard, no expression should ever have
4630 reference type. Unfortunately, we do not currently match
4631 the standard in this respect in that our internal representation
4632 of an expression may have reference type even when the standard
4633 says it does not. Therefore, we have to manually obtain the
4634 underlying type here. */
4635 scope = non_reference (scope);
4636 /* The type of the POSTFIX_EXPRESSION must be complete. */
4637 if (scope == unknown_type_node)
4639 error ("%qE does not have class type", postfix_expression);
4640 scope = NULL_TREE;
4642 else
4643 scope = complete_type_or_else (scope, NULL_TREE);
4644 /* Let the name lookup machinery know that we are processing a
4645 class member access expression. */
4646 parser->context->object_type = scope;
4647 /* If something went wrong, we want to be able to discern that case,
4648 as opposed to the case where there was no SCOPE due to the type
4649 of expression being dependent. */
4650 if (!scope)
4651 scope = error_mark_node;
4652 /* If the SCOPE was erroneous, make the various semantic analysis
4653 functions exit quickly -- and without issuing additional error
4654 messages. */
4655 if (scope == error_mark_node)
4656 postfix_expression = error_mark_node;
4659 /* Assume this expression is not a pseudo-destructor access. */
4660 pseudo_destructor_p = false;
4662 /* If the SCOPE is a scalar type, then, if this is a valid program,
4663 we must be looking at a pseudo-destructor-name. */
4664 if (scope && SCALAR_TYPE_P (scope))
4666 tree s;
4667 tree type;
4669 cp_parser_parse_tentatively (parser);
4670 /* Parse the pseudo-destructor-name. */
4671 s = NULL_TREE;
4672 cp_parser_pseudo_destructor_name (parser, &s, &type);
4673 if (cp_parser_parse_definitely (parser))
4675 pseudo_destructor_p = true;
4676 postfix_expression
4677 = finish_pseudo_destructor_expr (postfix_expression,
4678 s, TREE_TYPE (type));
4682 if (!pseudo_destructor_p)
4684 /* If the SCOPE is not a scalar type, we are looking at an
4685 ordinary class member access expression, rather than a
4686 pseudo-destructor-name. */
4687 bool template_p;
4688 /* Parse the id-expression. */
4689 name = (cp_parser_id_expression
4690 (parser,
4691 cp_parser_optional_template_keyword (parser),
4692 /*check_dependency_p=*/true,
4693 &template_p,
4694 /*declarator_p=*/false,
4695 /*optional_p=*/false));
4696 /* In general, build a SCOPE_REF if the member name is qualified.
4697 However, if the name was not dependent and has already been
4698 resolved; there is no need to build the SCOPE_REF. For example;
4700 struct X { void f(); };
4701 template <typename T> void f(T* t) { t->X::f(); }
4703 Even though "t" is dependent, "X::f" is not and has been resolved
4704 to a BASELINK; there is no need to include scope information. */
4706 /* But we do need to remember that there was an explicit scope for
4707 virtual function calls. */
4708 if (parser->scope)
4709 *idk = CP_ID_KIND_QUALIFIED;
4711 /* If the name is a template-id that names a type, we will get a
4712 TYPE_DECL here. That is invalid code. */
4713 if (TREE_CODE (name) == TYPE_DECL)
4715 error ("invalid use of %qD", name);
4716 postfix_expression = error_mark_node;
4718 else
4720 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4722 name = build_qualified_name (/*type=*/NULL_TREE,
4723 parser->scope,
4724 name,
4725 template_p);
4726 parser->scope = NULL_TREE;
4727 parser->qualifying_scope = NULL_TREE;
4728 parser->object_scope = NULL_TREE;
4730 if (scope && name && BASELINK_P (name))
4731 adjust_result_of_qualified_name_lookup
4732 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
4733 postfix_expression
4734 = finish_class_member_access_expr (postfix_expression, name,
4735 template_p);
4739 /* We no longer need to look up names in the scope of the object on
4740 the left-hand side of the `.' or `->' operator. */
4741 parser->context->object_type = NULL_TREE;
4743 /* Outside of offsetof, these operators may not appear in
4744 constant-expressions. */
4745 if (!for_offsetof
4746 && (cp_parser_non_integral_constant_expression
4747 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4748 postfix_expression = error_mark_node;
4750 return postfix_expression;
4753 /* Parse a parenthesized expression-list.
4755 expression-list:
4756 assignment-expression
4757 expression-list, assignment-expression
4759 attribute-list:
4760 expression-list
4761 identifier
4762 identifier, expression-list
4764 CAST_P is true if this expression is the target of a cast.
4766 Returns a TREE_LIST. The TREE_VALUE of each node is a
4767 representation of an assignment-expression. Note that a TREE_LIST
4768 is returned even if there is only a single expression in the list.
4769 error_mark_node is returned if the ( and or ) are
4770 missing. NULL_TREE is returned on no expressions. The parentheses
4771 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4772 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4773 indicates whether or not all of the expressions in the list were
4774 constant. */
4776 static tree
4777 cp_parser_parenthesized_expression_list (cp_parser* parser,
4778 bool is_attribute_list,
4779 bool cast_p,
4780 bool *non_constant_p)
4782 tree expression_list = NULL_TREE;
4783 bool fold_expr_p = is_attribute_list;
4784 tree identifier = NULL_TREE;
4786 /* Assume all the expressions will be constant. */
4787 if (non_constant_p)
4788 *non_constant_p = false;
4790 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4791 return error_mark_node;
4793 /* Consume expressions until there are no more. */
4794 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4795 while (true)
4797 tree expr;
4799 /* At the beginning of attribute lists, check to see if the
4800 next token is an identifier. */
4801 if (is_attribute_list
4802 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4804 cp_token *token;
4806 /* Consume the identifier. */
4807 token = cp_lexer_consume_token (parser->lexer);
4808 /* Save the identifier. */
4809 identifier = token->u.value;
4811 else
4813 /* Parse the next assignment-expression. */
4814 if (non_constant_p)
4816 bool expr_non_constant_p;
4817 expr = (cp_parser_constant_expression
4818 (parser, /*allow_non_constant_p=*/true,
4819 &expr_non_constant_p));
4820 if (expr_non_constant_p)
4821 *non_constant_p = true;
4823 else
4824 expr = cp_parser_assignment_expression (parser, cast_p);
4826 if (fold_expr_p)
4827 expr = fold_non_dependent_expr (expr);
4829 /* Add it to the list. We add error_mark_node
4830 expressions to the list, so that we can still tell if
4831 the correct form for a parenthesized expression-list
4832 is found. That gives better errors. */
4833 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4835 if (expr == error_mark_node)
4836 goto skip_comma;
4839 /* After the first item, attribute lists look the same as
4840 expression lists. */
4841 is_attribute_list = false;
4843 get_comma:;
4844 /* If the next token isn't a `,', then we are done. */
4845 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4846 break;
4848 /* Otherwise, consume the `,' and keep going. */
4849 cp_lexer_consume_token (parser->lexer);
4852 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4854 int ending;
4856 skip_comma:;
4857 /* We try and resync to an unnested comma, as that will give the
4858 user better diagnostics. */
4859 ending = cp_parser_skip_to_closing_parenthesis (parser,
4860 /*recovering=*/true,
4861 /*or_comma=*/true,
4862 /*consume_paren=*/true);
4863 if (ending < 0)
4864 goto get_comma;
4865 if (!ending)
4866 return error_mark_node;
4869 /* We built up the list in reverse order so we must reverse it now. */
4870 expression_list = nreverse (expression_list);
4871 if (identifier)
4872 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4874 return expression_list;
4877 /* Parse a pseudo-destructor-name.
4879 pseudo-destructor-name:
4880 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4881 :: [opt] nested-name-specifier template template-id :: ~ type-name
4882 :: [opt] nested-name-specifier [opt] ~ type-name
4884 If either of the first two productions is used, sets *SCOPE to the
4885 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4886 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4887 or ERROR_MARK_NODE if the parse fails. */
4889 static void
4890 cp_parser_pseudo_destructor_name (cp_parser* parser,
4891 tree* scope,
4892 tree* type)
4894 bool nested_name_specifier_p;
4896 /* Assume that things will not work out. */
4897 *type = error_mark_node;
4899 /* Look for the optional `::' operator. */
4900 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4901 /* Look for the optional nested-name-specifier. */
4902 nested_name_specifier_p
4903 = (cp_parser_nested_name_specifier_opt (parser,
4904 /*typename_keyword_p=*/false,
4905 /*check_dependency_p=*/true,
4906 /*type_p=*/false,
4907 /*is_declaration=*/true)
4908 != NULL_TREE);
4909 /* Now, if we saw a nested-name-specifier, we might be doing the
4910 second production. */
4911 if (nested_name_specifier_p
4912 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4914 /* Consume the `template' keyword. */
4915 cp_lexer_consume_token (parser->lexer);
4916 /* Parse the template-id. */
4917 cp_parser_template_id (parser,
4918 /*template_keyword_p=*/true,
4919 /*check_dependency_p=*/false,
4920 /*is_declaration=*/true);
4921 /* Look for the `::' token. */
4922 cp_parser_require (parser, CPP_SCOPE, "`::'");
4924 /* If the next token is not a `~', then there might be some
4925 additional qualification. */
4926 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4928 /* Look for the type-name. */
4929 *scope = TREE_TYPE (cp_parser_type_name (parser));
4931 if (*scope == error_mark_node)
4932 return;
4934 /* If we don't have ::~, then something has gone wrong. Since
4935 the only caller of this function is looking for something
4936 after `.' or `->' after a scalar type, most likely the
4937 program is trying to get a member of a non-aggregate
4938 type. */
4939 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4940 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4942 cp_parser_error (parser, "request for member of non-aggregate type");
4943 return;
4946 /* Look for the `::' token. */
4947 cp_parser_require (parser, CPP_SCOPE, "`::'");
4949 else
4950 *scope = NULL_TREE;
4952 /* Look for the `~'. */
4953 cp_parser_require (parser, CPP_COMPL, "`~'");
4954 /* Look for the type-name again. We are not responsible for
4955 checking that it matches the first type-name. */
4956 *type = cp_parser_type_name (parser);
4959 /* Parse a unary-expression.
4961 unary-expression:
4962 postfix-expression
4963 ++ cast-expression
4964 -- cast-expression
4965 unary-operator cast-expression
4966 sizeof unary-expression
4967 sizeof ( type-id )
4968 new-expression
4969 delete-expression
4971 GNU Extensions:
4973 unary-expression:
4974 __extension__ cast-expression
4975 __alignof__ unary-expression
4976 __alignof__ ( type-id )
4977 __real__ cast-expression
4978 __imag__ cast-expression
4979 && identifier
4981 ADDRESS_P is true iff the unary-expression is appearing as the
4982 operand of the `&' operator. CAST_P is true if this expression is
4983 the target of a cast.
4985 Returns a representation of the expression. */
4987 static tree
4988 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
4990 cp_token *token;
4991 enum tree_code unary_operator;
4993 /* Peek at the next token. */
4994 token = cp_lexer_peek_token (parser->lexer);
4995 /* Some keywords give away the kind of expression. */
4996 if (token->type == CPP_KEYWORD)
4998 enum rid keyword = token->keyword;
5000 switch (keyword)
5002 case RID_ALIGNOF:
5003 case RID_SIZEOF:
5005 tree operand;
5006 enum tree_code op;
5008 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5009 /* Consume the token. */
5010 cp_lexer_consume_token (parser->lexer);
5011 /* Parse the operand. */
5012 operand = cp_parser_sizeof_operand (parser, keyword);
5014 if (TYPE_P (operand))
5015 return cxx_sizeof_or_alignof_type (operand, op, true);
5016 else
5017 return cxx_sizeof_or_alignof_expr (operand, op);
5020 case RID_NEW:
5021 return cp_parser_new_expression (parser);
5023 case RID_DELETE:
5024 return cp_parser_delete_expression (parser);
5026 case RID_EXTENSION:
5028 /* The saved value of the PEDANTIC flag. */
5029 int saved_pedantic;
5030 tree expr;
5032 /* Save away the PEDANTIC flag. */
5033 cp_parser_extension_opt (parser, &saved_pedantic);
5034 /* Parse the cast-expression. */
5035 expr = cp_parser_simple_cast_expression (parser);
5036 /* Restore the PEDANTIC flag. */
5037 pedantic = saved_pedantic;
5039 return expr;
5042 case RID_REALPART:
5043 case RID_IMAGPART:
5045 tree expression;
5047 /* Consume the `__real__' or `__imag__' token. */
5048 cp_lexer_consume_token (parser->lexer);
5049 /* Parse the cast-expression. */
5050 expression = cp_parser_simple_cast_expression (parser);
5051 /* Create the complete representation. */
5052 return build_x_unary_op ((keyword == RID_REALPART
5053 ? REALPART_EXPR : IMAGPART_EXPR),
5054 expression);
5056 break;
5058 default:
5059 break;
5063 /* Look for the `:: new' and `:: delete', which also signal the
5064 beginning of a new-expression, or delete-expression,
5065 respectively. If the next token is `::', then it might be one of
5066 these. */
5067 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5069 enum rid keyword;
5071 /* See if the token after the `::' is one of the keywords in
5072 which we're interested. */
5073 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5074 /* If it's `new', we have a new-expression. */
5075 if (keyword == RID_NEW)
5076 return cp_parser_new_expression (parser);
5077 /* Similarly, for `delete'. */
5078 else if (keyword == RID_DELETE)
5079 return cp_parser_delete_expression (parser);
5082 /* Look for a unary operator. */
5083 unary_operator = cp_parser_unary_operator (token);
5084 /* The `++' and `--' operators can be handled similarly, even though
5085 they are not technically unary-operators in the grammar. */
5086 if (unary_operator == ERROR_MARK)
5088 if (token->type == CPP_PLUS_PLUS)
5089 unary_operator = PREINCREMENT_EXPR;
5090 else if (token->type == CPP_MINUS_MINUS)
5091 unary_operator = PREDECREMENT_EXPR;
5092 /* Handle the GNU address-of-label extension. */
5093 else if (cp_parser_allow_gnu_extensions_p (parser)
5094 && token->type == CPP_AND_AND)
5096 tree identifier;
5098 /* Consume the '&&' token. */
5099 cp_lexer_consume_token (parser->lexer);
5100 /* Look for the identifier. */
5101 identifier = cp_parser_identifier (parser);
5102 /* Create an expression representing the address. */
5103 return finish_label_address_expr (identifier);
5106 if (unary_operator != ERROR_MARK)
5108 tree cast_expression;
5109 tree expression = error_mark_node;
5110 const char *non_constant_p = NULL;
5112 /* Consume the operator token. */
5113 token = cp_lexer_consume_token (parser->lexer);
5114 /* Parse the cast-expression. */
5115 cast_expression
5116 = cp_parser_cast_expression (parser,
5117 unary_operator == ADDR_EXPR,
5118 /*cast_p=*/false);
5119 /* Now, build an appropriate representation. */
5120 switch (unary_operator)
5122 case INDIRECT_REF:
5123 non_constant_p = "`*'";
5124 expression = build_x_indirect_ref (cast_expression, "unary *");
5125 break;
5127 case ADDR_EXPR:
5128 non_constant_p = "`&'";
5129 /* Fall through. */
5130 case BIT_NOT_EXPR:
5131 expression = build_x_unary_op (unary_operator, cast_expression);
5132 break;
5134 case PREINCREMENT_EXPR:
5135 case PREDECREMENT_EXPR:
5136 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5137 ? "`++'" : "`--'");
5138 /* Fall through. */
5139 case UNARY_PLUS_EXPR:
5140 case NEGATE_EXPR:
5141 case TRUTH_NOT_EXPR:
5142 expression = finish_unary_op_expr (unary_operator, cast_expression);
5143 break;
5145 default:
5146 gcc_unreachable ();
5149 if (non_constant_p
5150 && cp_parser_non_integral_constant_expression (parser,
5151 non_constant_p))
5152 expression = error_mark_node;
5154 return expression;
5157 return cp_parser_postfix_expression (parser, address_p, cast_p);
5160 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5161 unary-operator, the corresponding tree code is returned. */
5163 static enum tree_code
5164 cp_parser_unary_operator (cp_token* token)
5166 switch (token->type)
5168 case CPP_MULT:
5169 return INDIRECT_REF;
5171 case CPP_AND:
5172 return ADDR_EXPR;
5174 case CPP_PLUS:
5175 return UNARY_PLUS_EXPR;
5177 case CPP_MINUS:
5178 return NEGATE_EXPR;
5180 case CPP_NOT:
5181 return TRUTH_NOT_EXPR;
5183 case CPP_COMPL:
5184 return BIT_NOT_EXPR;
5186 default:
5187 return ERROR_MARK;
5191 /* Parse a new-expression.
5193 new-expression:
5194 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5195 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5197 Returns a representation of the expression. */
5199 static tree
5200 cp_parser_new_expression (cp_parser* parser)
5202 bool global_scope_p;
5203 tree placement;
5204 tree type;
5205 tree initializer;
5206 tree nelts;
5208 /* Look for the optional `::' operator. */
5209 global_scope_p
5210 = (cp_parser_global_scope_opt (parser,
5211 /*current_scope_valid_p=*/false)
5212 != NULL_TREE);
5213 /* Look for the `new' operator. */
5214 cp_parser_require_keyword (parser, RID_NEW, "`new'");
5215 /* There's no easy way to tell a new-placement from the
5216 `( type-id )' construct. */
5217 cp_parser_parse_tentatively (parser);
5218 /* Look for a new-placement. */
5219 placement = cp_parser_new_placement (parser);
5220 /* If that didn't work out, there's no new-placement. */
5221 if (!cp_parser_parse_definitely (parser))
5222 placement = NULL_TREE;
5224 /* If the next token is a `(', then we have a parenthesized
5225 type-id. */
5226 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5228 /* Consume the `('. */
5229 cp_lexer_consume_token (parser->lexer);
5230 /* Parse the type-id. */
5231 type = cp_parser_type_id (parser);
5232 /* Look for the closing `)'. */
5233 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5234 /* There should not be a direct-new-declarator in this production,
5235 but GCC used to allowed this, so we check and emit a sensible error
5236 message for this case. */
5237 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5239 error ("array bound forbidden after parenthesized type-id");
5240 inform ("try removing the parentheses around the type-id");
5241 cp_parser_direct_new_declarator (parser);
5243 nelts = NULL_TREE;
5245 /* Otherwise, there must be a new-type-id. */
5246 else
5247 type = cp_parser_new_type_id (parser, &nelts);
5249 /* If the next token is a `(', then we have a new-initializer. */
5250 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5251 initializer = cp_parser_new_initializer (parser);
5252 else
5253 initializer = NULL_TREE;
5255 /* A new-expression may not appear in an integral constant
5256 expression. */
5257 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5258 return error_mark_node;
5260 /* Create a representation of the new-expression. */
5261 return build_new (placement, type, nelts, initializer, global_scope_p);
5264 /* Parse a new-placement.
5266 new-placement:
5267 ( expression-list )
5269 Returns the same representation as for an expression-list. */
5271 static tree
5272 cp_parser_new_placement (cp_parser* parser)
5274 tree expression_list;
5276 /* Parse the expression-list. */
5277 expression_list = (cp_parser_parenthesized_expression_list
5278 (parser, false, /*cast_p=*/false,
5279 /*non_constant_p=*/NULL));
5281 return expression_list;
5284 /* Parse a new-type-id.
5286 new-type-id:
5287 type-specifier-seq new-declarator [opt]
5289 Returns the TYPE allocated. If the new-type-id indicates an array
5290 type, *NELTS is set to the number of elements in the last array
5291 bound; the TYPE will not include the last array bound. */
5293 static tree
5294 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5296 cp_decl_specifier_seq type_specifier_seq;
5297 cp_declarator *new_declarator;
5298 cp_declarator *declarator;
5299 cp_declarator *outer_declarator;
5300 const char *saved_message;
5301 tree type;
5303 /* The type-specifier sequence must not contain type definitions.
5304 (It cannot contain declarations of new types either, but if they
5305 are not definitions we will catch that because they are not
5306 complete.) */
5307 saved_message = parser->type_definition_forbidden_message;
5308 parser->type_definition_forbidden_message
5309 = "types may not be defined in a new-type-id";
5310 /* Parse the type-specifier-seq. */
5311 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5312 &type_specifier_seq);
5313 /* Restore the old message. */
5314 parser->type_definition_forbidden_message = saved_message;
5315 /* Parse the new-declarator. */
5316 new_declarator = cp_parser_new_declarator_opt (parser);
5318 /* Determine the number of elements in the last array dimension, if
5319 any. */
5320 *nelts = NULL_TREE;
5321 /* Skip down to the last array dimension. */
5322 declarator = new_declarator;
5323 outer_declarator = NULL;
5324 while (declarator && (declarator->kind == cdk_pointer
5325 || declarator->kind == cdk_ptrmem))
5327 outer_declarator = declarator;
5328 declarator = declarator->declarator;
5330 while (declarator
5331 && declarator->kind == cdk_array
5332 && declarator->declarator
5333 && declarator->declarator->kind == cdk_array)
5335 outer_declarator = declarator;
5336 declarator = declarator->declarator;
5339 if (declarator && declarator->kind == cdk_array)
5341 *nelts = declarator->u.array.bounds;
5342 if (*nelts == error_mark_node)
5343 *nelts = integer_one_node;
5345 if (outer_declarator)
5346 outer_declarator->declarator = declarator->declarator;
5347 else
5348 new_declarator = NULL;
5351 type = groktypename (&type_specifier_seq, new_declarator);
5352 if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
5354 *nelts = array_type_nelts_top (type);
5355 type = TREE_TYPE (type);
5357 return type;
5360 /* Parse an (optional) new-declarator.
5362 new-declarator:
5363 ptr-operator new-declarator [opt]
5364 direct-new-declarator
5366 Returns the declarator. */
5368 static cp_declarator *
5369 cp_parser_new_declarator_opt (cp_parser* parser)
5371 enum tree_code code;
5372 tree type;
5373 cp_cv_quals cv_quals;
5375 /* We don't know if there's a ptr-operator next, or not. */
5376 cp_parser_parse_tentatively (parser);
5377 /* Look for a ptr-operator. */
5378 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5379 /* If that worked, look for more new-declarators. */
5380 if (cp_parser_parse_definitely (parser))
5382 cp_declarator *declarator;
5384 /* Parse another optional declarator. */
5385 declarator = cp_parser_new_declarator_opt (parser);
5387 /* Create the representation of the declarator. */
5388 if (type)
5389 declarator = make_ptrmem_declarator (cv_quals, type, declarator);
5390 else if (code == INDIRECT_REF)
5391 declarator = make_pointer_declarator (cv_quals, declarator);
5392 else
5393 declarator = make_reference_declarator (cv_quals, declarator);
5395 return declarator;
5398 /* If the next token is a `[', there is a direct-new-declarator. */
5399 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5400 return cp_parser_direct_new_declarator (parser);
5402 return NULL;
5405 /* Parse a direct-new-declarator.
5407 direct-new-declarator:
5408 [ expression ]
5409 direct-new-declarator [constant-expression]
5413 static cp_declarator *
5414 cp_parser_direct_new_declarator (cp_parser* parser)
5416 cp_declarator *declarator = NULL;
5418 while (true)
5420 tree expression;
5422 /* Look for the opening `['. */
5423 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5424 /* The first expression is not required to be constant. */
5425 if (!declarator)
5427 expression = cp_parser_expression (parser, /*cast_p=*/false);
5428 /* The standard requires that the expression have integral
5429 type. DR 74 adds enumeration types. We believe that the
5430 real intent is that these expressions be handled like the
5431 expression in a `switch' condition, which also allows
5432 classes with a single conversion to integral or
5433 enumeration type. */
5434 if (!processing_template_decl)
5436 expression
5437 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5438 expression,
5439 /*complain=*/true);
5440 if (!expression)
5442 error ("expression in new-declarator must have integral "
5443 "or enumeration type");
5444 expression = error_mark_node;
5448 /* But all the other expressions must be. */
5449 else
5450 expression
5451 = cp_parser_constant_expression (parser,
5452 /*allow_non_constant=*/false,
5453 NULL);
5454 /* Look for the closing `]'. */
5455 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5457 /* Add this bound to the declarator. */
5458 declarator = make_array_declarator (declarator, expression);
5460 /* If the next token is not a `[', then there are no more
5461 bounds. */
5462 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5463 break;
5466 return declarator;
5469 /* Parse a new-initializer.
5471 new-initializer:
5472 ( expression-list [opt] )
5474 Returns a representation of the expression-list. If there is no
5475 expression-list, VOID_ZERO_NODE is returned. */
5477 static tree
5478 cp_parser_new_initializer (cp_parser* parser)
5480 tree expression_list;
5482 expression_list = (cp_parser_parenthesized_expression_list
5483 (parser, false, /*cast_p=*/false,
5484 /*non_constant_p=*/NULL));
5485 if (!expression_list)
5486 expression_list = void_zero_node;
5488 return expression_list;
5491 /* Parse a delete-expression.
5493 delete-expression:
5494 :: [opt] delete cast-expression
5495 :: [opt] delete [ ] cast-expression
5497 Returns a representation of the expression. */
5499 static tree
5500 cp_parser_delete_expression (cp_parser* parser)
5502 bool global_scope_p;
5503 bool array_p;
5504 tree expression;
5506 /* Look for the optional `::' operator. */
5507 global_scope_p
5508 = (cp_parser_global_scope_opt (parser,
5509 /*current_scope_valid_p=*/false)
5510 != NULL_TREE);
5511 /* Look for the `delete' keyword. */
5512 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5513 /* See if the array syntax is in use. */
5514 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5516 /* Consume the `[' token. */
5517 cp_lexer_consume_token (parser->lexer);
5518 /* Look for the `]' token. */
5519 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5520 /* Remember that this is the `[]' construct. */
5521 array_p = true;
5523 else
5524 array_p = false;
5526 /* Parse the cast-expression. */
5527 expression = cp_parser_simple_cast_expression (parser);
5529 /* A delete-expression may not appear in an integral constant
5530 expression. */
5531 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5532 return error_mark_node;
5534 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5537 /* Parse a cast-expression.
5539 cast-expression:
5540 unary-expression
5541 ( type-id ) cast-expression
5543 ADDRESS_P is true iff the unary-expression is appearing as the
5544 operand of the `&' operator. CAST_P is true if this expression is
5545 the target of a cast.
5547 Returns a representation of the expression. */
5549 static tree
5550 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5552 /* If it's a `(', then we might be looking at a cast. */
5553 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5555 tree type = NULL_TREE;
5556 tree expr = NULL_TREE;
5557 bool compound_literal_p;
5558 const char *saved_message;
5560 /* There's no way to know yet whether or not this is a cast.
5561 For example, `(int (3))' is a unary-expression, while `(int)
5562 3' is a cast. So, we resort to parsing tentatively. */
5563 cp_parser_parse_tentatively (parser);
5564 /* Types may not be defined in a cast. */
5565 saved_message = parser->type_definition_forbidden_message;
5566 parser->type_definition_forbidden_message
5567 = "types may not be defined in casts";
5568 /* Consume the `('. */
5569 cp_lexer_consume_token (parser->lexer);
5570 /* A very tricky bit is that `(struct S) { 3 }' is a
5571 compound-literal (which we permit in C++ as an extension).
5572 But, that construct is not a cast-expression -- it is a
5573 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5574 is legal; if the compound-literal were a cast-expression,
5575 you'd need an extra set of parentheses.) But, if we parse
5576 the type-id, and it happens to be a class-specifier, then we
5577 will commit to the parse at that point, because we cannot
5578 undo the action that is done when creating a new class. So,
5579 then we cannot back up and do a postfix-expression.
5581 Therefore, we scan ahead to the closing `)', and check to see
5582 if the token after the `)' is a `{'. If so, we are not
5583 looking at a cast-expression.
5585 Save tokens so that we can put them back. */
5586 cp_lexer_save_tokens (parser->lexer);
5587 /* Skip tokens until the next token is a closing parenthesis.
5588 If we find the closing `)', and the next token is a `{', then
5589 we are looking at a compound-literal. */
5590 compound_literal_p
5591 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5592 /*consume_paren=*/true)
5593 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5594 /* Roll back the tokens we skipped. */
5595 cp_lexer_rollback_tokens (parser->lexer);
5596 /* If we were looking at a compound-literal, simulate an error
5597 so that the call to cp_parser_parse_definitely below will
5598 fail. */
5599 if (compound_literal_p)
5600 cp_parser_simulate_error (parser);
5601 else
5603 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5604 parser->in_type_id_in_expr_p = true;
5605 /* Look for the type-id. */
5606 type = cp_parser_type_id (parser);
5607 /* Look for the closing `)'. */
5608 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5609 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5612 /* Restore the saved message. */
5613 parser->type_definition_forbidden_message = saved_message;
5615 /* If ok so far, parse the dependent expression. We cannot be
5616 sure it is a cast. Consider `(T ())'. It is a parenthesized
5617 ctor of T, but looks like a cast to function returning T
5618 without a dependent expression. */
5619 if (!cp_parser_error_occurred (parser))
5620 expr = cp_parser_cast_expression (parser,
5621 /*address_p=*/false,
5622 /*cast_p=*/true);
5624 if (cp_parser_parse_definitely (parser))
5626 /* Warn about old-style casts, if so requested. */
5627 if (warn_old_style_cast
5628 && !in_system_header
5629 && !VOID_TYPE_P (type)
5630 && current_lang_name != lang_name_c)
5631 warning (OPT_Wold_style_cast, "use of old-style cast");
5633 /* Only type conversions to integral or enumeration types
5634 can be used in constant-expressions. */
5635 if (!cast_valid_in_integral_constant_expression_p (type)
5636 && (cp_parser_non_integral_constant_expression
5637 (parser,
5638 "a cast to a type other than an integral or "
5639 "enumeration type")))
5640 return error_mark_node;
5642 /* Perform the cast. */
5643 expr = build_c_cast (type, expr);
5644 return expr;
5648 /* If we get here, then it's not a cast, so it must be a
5649 unary-expression. */
5650 return cp_parser_unary_expression (parser, address_p, cast_p);
5653 /* Parse a binary expression of the general form:
5655 pm-expression:
5656 cast-expression
5657 pm-expression .* cast-expression
5658 pm-expression ->* cast-expression
5660 multiplicative-expression:
5661 pm-expression
5662 multiplicative-expression * pm-expression
5663 multiplicative-expression / pm-expression
5664 multiplicative-expression % pm-expression
5666 additive-expression:
5667 multiplicative-expression
5668 additive-expression + multiplicative-expression
5669 additive-expression - multiplicative-expression
5671 shift-expression:
5672 additive-expression
5673 shift-expression << additive-expression
5674 shift-expression >> additive-expression
5676 relational-expression:
5677 shift-expression
5678 relational-expression < shift-expression
5679 relational-expression > shift-expression
5680 relational-expression <= shift-expression
5681 relational-expression >= shift-expression
5683 GNU Extension:
5685 relational-expression:
5686 relational-expression <? shift-expression
5687 relational-expression >? shift-expression
5689 equality-expression:
5690 relational-expression
5691 equality-expression == relational-expression
5692 equality-expression != relational-expression
5694 and-expression:
5695 equality-expression
5696 and-expression & equality-expression
5698 exclusive-or-expression:
5699 and-expression
5700 exclusive-or-expression ^ and-expression
5702 inclusive-or-expression:
5703 exclusive-or-expression
5704 inclusive-or-expression | exclusive-or-expression
5706 logical-and-expression:
5707 inclusive-or-expression
5708 logical-and-expression && inclusive-or-expression
5710 logical-or-expression:
5711 logical-and-expression
5712 logical-or-expression || logical-and-expression
5714 All these are implemented with a single function like:
5716 binary-expression:
5717 simple-cast-expression
5718 binary-expression <token> binary-expression
5720 CAST_P is true if this expression is the target of a cast.
5722 The binops_by_token map is used to get the tree codes for each <token> type.
5723 binary-expressions are associated according to a precedence table. */
5725 #define TOKEN_PRECEDENCE(token) \
5726 ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5727 ? PREC_NOT_OPERATOR \
5728 : binops_by_token[token->type].prec)
5730 static tree
5731 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5733 cp_parser_expression_stack stack;
5734 cp_parser_expression_stack_entry *sp = &stack[0];
5735 tree lhs, rhs;
5736 cp_token *token;
5737 enum tree_code tree_type, lhs_type, rhs_type;
5738 enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5739 bool overloaded_p;
5741 /* Parse the first expression. */
5742 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5743 lhs_type = ERROR_MARK;
5745 for (;;)
5747 /* Get an operator token. */
5748 token = cp_lexer_peek_token (parser->lexer);
5750 new_prec = TOKEN_PRECEDENCE (token);
5752 /* Popping an entry off the stack means we completed a subexpression:
5753 - either we found a token which is not an operator (`>' where it is not
5754 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5755 will happen repeatedly;
5756 - or, we found an operator which has lower priority. This is the case
5757 where the recursive descent *ascends*, as in `3 * 4 + 5' after
5758 parsing `3 * 4'. */
5759 if (new_prec <= prec)
5761 if (sp == stack)
5762 break;
5763 else
5764 goto pop;
5767 get_rhs:
5768 tree_type = binops_by_token[token->type].tree_type;
5770 /* We used the operator token. */
5771 cp_lexer_consume_token (parser->lexer);
5773 /* Extract another operand. It may be the RHS of this expression
5774 or the LHS of a new, higher priority expression. */
5775 rhs = cp_parser_simple_cast_expression (parser);
5776 rhs_type = ERROR_MARK;
5778 /* Get another operator token. Look up its precedence to avoid
5779 building a useless (immediately popped) stack entry for common
5780 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
5781 token = cp_lexer_peek_token (parser->lexer);
5782 lookahead_prec = TOKEN_PRECEDENCE (token);
5783 if (lookahead_prec > new_prec)
5785 /* ... and prepare to parse the RHS of the new, higher priority
5786 expression. Since precedence levels on the stack are
5787 monotonically increasing, we do not have to care about
5788 stack overflows. */
5789 sp->prec = prec;
5790 sp->tree_type = tree_type;
5791 sp->lhs = lhs;
5792 sp->lhs_type = lhs_type;
5793 sp++;
5794 lhs = rhs;
5795 lhs_type = rhs_type;
5796 prec = new_prec;
5797 new_prec = lookahead_prec;
5798 goto get_rhs;
5800 pop:
5801 /* If the stack is not empty, we have parsed into LHS the right side
5802 (`4' in the example above) of an expression we had suspended.
5803 We can use the information on the stack to recover the LHS (`3')
5804 from the stack together with the tree code (`MULT_EXPR'), and
5805 the precedence of the higher level subexpression
5806 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
5807 which will be used to actually build the additive expression. */
5808 --sp;
5809 prec = sp->prec;
5810 tree_type = sp->tree_type;
5811 rhs = lhs;
5812 rhs_type = lhs_type;
5813 lhs = sp->lhs;
5814 lhs_type = sp->lhs_type;
5817 overloaded_p = false;
5818 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
5819 &overloaded_p);
5820 lhs_type = tree_type;
5822 /* If the binary operator required the use of an overloaded operator,
5823 then this expression cannot be an integral constant-expression.
5824 An overloaded operator can be used even if both operands are
5825 otherwise permissible in an integral constant-expression if at
5826 least one of the operands is of enumeration type. */
5828 if (overloaded_p
5829 && (cp_parser_non_integral_constant_expression
5830 (parser, "calls to overloaded operators")))
5831 return error_mark_node;
5834 return lhs;
5838 /* Parse the `? expression : assignment-expression' part of a
5839 conditional-expression. The LOGICAL_OR_EXPR is the
5840 logical-or-expression that started the conditional-expression.
5841 Returns a representation of the entire conditional-expression.
5843 This routine is used by cp_parser_assignment_expression.
5845 ? expression : assignment-expression
5847 GNU Extensions:
5849 ? : assignment-expression */
5851 static tree
5852 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5854 tree expr;
5855 tree assignment_expr;
5857 /* Consume the `?' token. */
5858 cp_lexer_consume_token (parser->lexer);
5859 if (cp_parser_allow_gnu_extensions_p (parser)
5860 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5861 /* Implicit true clause. */
5862 expr = NULL_TREE;
5863 else
5864 /* Parse the expression. */
5865 expr = cp_parser_expression (parser, /*cast_p=*/false);
5867 /* The next token should be a `:'. */
5868 cp_parser_require (parser, CPP_COLON, "`:'");
5869 /* Parse the assignment-expression. */
5870 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5872 /* Build the conditional-expression. */
5873 return build_x_conditional_expr (logical_or_expr,
5874 expr,
5875 assignment_expr);
5878 /* Parse an assignment-expression.
5880 assignment-expression:
5881 conditional-expression
5882 logical-or-expression assignment-operator assignment_expression
5883 throw-expression
5885 CAST_P is true if this expression is the target of a cast.
5887 Returns a representation for the expression. */
5889 static tree
5890 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
5892 tree expr;
5894 /* If the next token is the `throw' keyword, then we're looking at
5895 a throw-expression. */
5896 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5897 expr = cp_parser_throw_expression (parser);
5898 /* Otherwise, it must be that we are looking at a
5899 logical-or-expression. */
5900 else
5902 /* Parse the binary expressions (logical-or-expression). */
5903 expr = cp_parser_binary_expression (parser, cast_p);
5904 /* If the next token is a `?' then we're actually looking at a
5905 conditional-expression. */
5906 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5907 return cp_parser_question_colon_clause (parser, expr);
5908 else
5910 enum tree_code assignment_operator;
5912 /* If it's an assignment-operator, we're using the second
5913 production. */
5914 assignment_operator
5915 = cp_parser_assignment_operator_opt (parser);
5916 if (assignment_operator != ERROR_MARK)
5918 tree rhs;
5920 /* Parse the right-hand side of the assignment. */
5921 rhs = cp_parser_assignment_expression (parser, cast_p);
5922 /* An assignment may not appear in a
5923 constant-expression. */
5924 if (cp_parser_non_integral_constant_expression (parser,
5925 "an assignment"))
5926 return error_mark_node;
5927 /* Build the assignment expression. */
5928 expr = build_x_modify_expr (expr,
5929 assignment_operator,
5930 rhs);
5935 return expr;
5938 /* Parse an (optional) assignment-operator.
5940 assignment-operator: one of
5941 = *= /= %= += -= >>= <<= &= ^= |=
5943 GNU Extension:
5945 assignment-operator: one of
5946 <?= >?=
5948 If the next token is an assignment operator, the corresponding tree
5949 code is returned, and the token is consumed. For example, for
5950 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5951 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5952 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5953 operator, ERROR_MARK is returned. */
5955 static enum tree_code
5956 cp_parser_assignment_operator_opt (cp_parser* parser)
5958 enum tree_code op;
5959 cp_token *token;
5961 /* Peek at the next toen. */
5962 token = cp_lexer_peek_token (parser->lexer);
5964 switch (token->type)
5966 case CPP_EQ:
5967 op = NOP_EXPR;
5968 break;
5970 case CPP_MULT_EQ:
5971 op = MULT_EXPR;
5972 break;
5974 case CPP_DIV_EQ:
5975 op = TRUNC_DIV_EXPR;
5976 break;
5978 case CPP_MOD_EQ:
5979 op = TRUNC_MOD_EXPR;
5980 break;
5982 case CPP_PLUS_EQ:
5983 op = PLUS_EXPR;
5984 break;
5986 case CPP_MINUS_EQ:
5987 op = MINUS_EXPR;
5988 break;
5990 case CPP_RSHIFT_EQ:
5991 op = RSHIFT_EXPR;
5992 break;
5994 case CPP_LSHIFT_EQ:
5995 op = LSHIFT_EXPR;
5996 break;
5998 case CPP_AND_EQ:
5999 op = BIT_AND_EXPR;
6000 break;
6002 case CPP_XOR_EQ:
6003 op = BIT_XOR_EXPR;
6004 break;
6006 case CPP_OR_EQ:
6007 op = BIT_IOR_EXPR;
6008 break;
6010 default:
6011 /* Nothing else is an assignment operator. */
6012 op = ERROR_MARK;
6015 /* If it was an assignment operator, consume it. */
6016 if (op != ERROR_MARK)
6017 cp_lexer_consume_token (parser->lexer);
6019 return op;
6022 /* Parse an expression.
6024 expression:
6025 assignment-expression
6026 expression , assignment-expression
6028 CAST_P is true if this expression is the target of a cast.
6030 Returns a representation of the expression. */
6032 static tree
6033 cp_parser_expression (cp_parser* parser, bool cast_p)
6035 tree expression = NULL_TREE;
6037 while (true)
6039 tree assignment_expression;
6041 /* Parse the next assignment-expression. */
6042 assignment_expression
6043 = cp_parser_assignment_expression (parser, cast_p);
6044 /* If this is the first assignment-expression, we can just
6045 save it away. */
6046 if (!expression)
6047 expression = assignment_expression;
6048 else
6049 expression = build_x_compound_expr (expression,
6050 assignment_expression);
6051 /* If the next token is not a comma, then we are done with the
6052 expression. */
6053 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6054 break;
6055 /* Consume the `,'. */
6056 cp_lexer_consume_token (parser->lexer);
6057 /* A comma operator cannot appear in a constant-expression. */
6058 if (cp_parser_non_integral_constant_expression (parser,
6059 "a comma operator"))
6060 expression = error_mark_node;
6063 return expression;
6066 /* Parse a constant-expression.
6068 constant-expression:
6069 conditional-expression
6071 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6072 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6073 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6074 is false, NON_CONSTANT_P should be NULL. */
6076 static tree
6077 cp_parser_constant_expression (cp_parser* parser,
6078 bool allow_non_constant_p,
6079 bool *non_constant_p)
6081 bool saved_integral_constant_expression_p;
6082 bool saved_allow_non_integral_constant_expression_p;
6083 bool saved_non_integral_constant_expression_p;
6084 tree expression;
6086 /* It might seem that we could simply parse the
6087 conditional-expression, and then check to see if it were
6088 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6089 one that the compiler can figure out is constant, possibly after
6090 doing some simplifications or optimizations. The standard has a
6091 precise definition of constant-expression, and we must honor
6092 that, even though it is somewhat more restrictive.
6094 For example:
6096 int i[(2, 3)];
6098 is not a legal declaration, because `(2, 3)' is not a
6099 constant-expression. The `,' operator is forbidden in a
6100 constant-expression. However, GCC's constant-folding machinery
6101 will fold this operation to an INTEGER_CST for `3'. */
6103 /* Save the old settings. */
6104 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6105 saved_allow_non_integral_constant_expression_p
6106 = parser->allow_non_integral_constant_expression_p;
6107 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6108 /* We are now parsing a constant-expression. */
6109 parser->integral_constant_expression_p = true;
6110 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6111 parser->non_integral_constant_expression_p = false;
6112 /* Although the grammar says "conditional-expression", we parse an
6113 "assignment-expression", which also permits "throw-expression"
6114 and the use of assignment operators. In the case that
6115 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6116 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6117 actually essential that we look for an assignment-expression.
6118 For example, cp_parser_initializer_clauses uses this function to
6119 determine whether a particular assignment-expression is in fact
6120 constant. */
6121 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6122 /* Restore the old settings. */
6123 parser->integral_constant_expression_p
6124 = saved_integral_constant_expression_p;
6125 parser->allow_non_integral_constant_expression_p
6126 = saved_allow_non_integral_constant_expression_p;
6127 if (allow_non_constant_p)
6128 *non_constant_p = parser->non_integral_constant_expression_p;
6129 else if (parser->non_integral_constant_expression_p)
6130 expression = error_mark_node;
6131 parser->non_integral_constant_expression_p
6132 = saved_non_integral_constant_expression_p;
6134 return expression;
6137 /* Parse __builtin_offsetof.
6139 offsetof-expression:
6140 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6142 offsetof-member-designator:
6143 id-expression
6144 | offsetof-member-designator "." id-expression
6145 | offsetof-member-designator "[" expression "]" */
6147 static tree
6148 cp_parser_builtin_offsetof (cp_parser *parser)
6150 int save_ice_p, save_non_ice_p;
6151 tree type, expr;
6152 cp_id_kind dummy;
6154 /* We're about to accept non-integral-constant things, but will
6155 definitely yield an integral constant expression. Save and
6156 restore these values around our local parsing. */
6157 save_ice_p = parser->integral_constant_expression_p;
6158 save_non_ice_p = parser->non_integral_constant_expression_p;
6160 /* Consume the "__builtin_offsetof" token. */
6161 cp_lexer_consume_token (parser->lexer);
6162 /* Consume the opening `('. */
6163 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6164 /* Parse the type-id. */
6165 type = cp_parser_type_id (parser);
6166 /* Look for the `,'. */
6167 cp_parser_require (parser, CPP_COMMA, "`,'");
6169 /* Build the (type *)null that begins the traditional offsetof macro. */
6170 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
6172 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6173 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6174 true, &dummy);
6175 while (true)
6177 cp_token *token = cp_lexer_peek_token (parser->lexer);
6178 switch (token->type)
6180 case CPP_OPEN_SQUARE:
6181 /* offsetof-member-designator "[" expression "]" */
6182 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6183 break;
6185 case CPP_DOT:
6186 /* offsetof-member-designator "." identifier */
6187 cp_lexer_consume_token (parser->lexer);
6188 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6189 true, &dummy);
6190 break;
6192 case CPP_CLOSE_PAREN:
6193 /* Consume the ")" token. */
6194 cp_lexer_consume_token (parser->lexer);
6195 goto success;
6197 default:
6198 /* Error. We know the following require will fail, but
6199 that gives the proper error message. */
6200 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6201 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6202 expr = error_mark_node;
6203 goto failure;
6207 success:
6208 /* If we're processing a template, we can't finish the semantics yet.
6209 Otherwise we can fold the entire expression now. */
6210 if (processing_template_decl)
6211 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6212 else
6213 expr = finish_offsetof (expr);
6215 failure:
6216 parser->integral_constant_expression_p = save_ice_p;
6217 parser->non_integral_constant_expression_p = save_non_ice_p;
6219 return expr;
6222 /* Statements [gram.stmt.stmt] */
6224 /* Parse a statement.
6226 statement:
6227 labeled-statement
6228 expression-statement
6229 compound-statement
6230 selection-statement
6231 iteration-statement
6232 jump-statement
6233 declaration-statement
6234 try-block
6236 IN_COMPOUND is true when the statement is nested inside a
6237 cp_parser_compound_statement; this matters for certain pragmas.
6239 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6240 is a (possibly labeled) if statement which is not enclosed in braces
6241 and has an else clause. This is used to implement -Wparentheses. */
6243 static void
6244 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6245 bool in_compound, bool *if_p)
6247 tree statement;
6248 cp_token *token;
6249 location_t statement_location;
6251 restart:
6252 if (if_p != NULL)
6253 *if_p = false;
6254 /* There is no statement yet. */
6255 statement = NULL_TREE;
6256 /* Peek at the next token. */
6257 token = cp_lexer_peek_token (parser->lexer);
6258 /* Remember the location of the first token in the statement. */
6259 statement_location = token->location;
6260 /* If this is a keyword, then that will often determine what kind of
6261 statement we have. */
6262 if (token->type == CPP_KEYWORD)
6264 enum rid keyword = token->keyword;
6266 switch (keyword)
6268 case RID_CASE:
6269 case RID_DEFAULT:
6270 /* Looks like a labeled-statement with a case label.
6271 Parse the label, and then use tail recursion to parse
6272 the statement. */
6273 cp_parser_label_for_labeled_statement (parser);
6274 goto restart;
6276 case RID_IF:
6277 case RID_SWITCH:
6278 statement = cp_parser_selection_statement (parser, if_p);
6279 break;
6281 case RID_WHILE:
6282 case RID_DO:
6283 case RID_FOR:
6284 statement = cp_parser_iteration_statement (parser);
6285 break;
6287 case RID_BREAK:
6288 case RID_CONTINUE:
6289 case RID_RETURN:
6290 case RID_GOTO:
6291 statement = cp_parser_jump_statement (parser);
6292 break;
6294 /* Objective-C++ exception-handling constructs. */
6295 case RID_AT_TRY:
6296 case RID_AT_CATCH:
6297 case RID_AT_FINALLY:
6298 case RID_AT_SYNCHRONIZED:
6299 case RID_AT_THROW:
6300 statement = cp_parser_objc_statement (parser);
6301 break;
6303 case RID_TRY:
6304 statement = cp_parser_try_block (parser);
6305 break;
6307 default:
6308 /* It might be a keyword like `int' that can start a
6309 declaration-statement. */
6310 break;
6313 else if (token->type == CPP_NAME)
6315 /* If the next token is a `:', then we are looking at a
6316 labeled-statement. */
6317 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6318 if (token->type == CPP_COLON)
6320 /* Looks like a labeled-statement with an ordinary label.
6321 Parse the label, and then use tail recursion to parse
6322 the statement. */
6323 cp_parser_label_for_labeled_statement (parser);
6324 goto restart;
6327 /* Anything that starts with a `{' must be a compound-statement. */
6328 else if (token->type == CPP_OPEN_BRACE)
6329 statement = cp_parser_compound_statement (parser, NULL, false);
6330 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6331 a statement all its own. */
6332 else if (token->type == CPP_PRAGMA)
6334 /* Only certain OpenMP pragmas are attached to statements, and thus
6335 are considered statements themselves. All others are not. In
6336 the context of a compound, accept the pragma as a "statement" and
6337 return so that we can check for a close brace. Otherwise we
6338 require a real statement and must go back and read one. */
6339 if (in_compound)
6340 cp_parser_pragma (parser, pragma_compound);
6341 else if (!cp_parser_pragma (parser, pragma_stmt))
6342 goto restart;
6343 return;
6345 else if (token->type == CPP_EOF)
6347 cp_parser_error (parser, "expected statement");
6348 return;
6351 /* Everything else must be a declaration-statement or an
6352 expression-statement. Try for the declaration-statement
6353 first, unless we are looking at a `;', in which case we know that
6354 we have an expression-statement. */
6355 if (!statement)
6357 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6359 cp_parser_parse_tentatively (parser);
6360 /* Try to parse the declaration-statement. */
6361 cp_parser_declaration_statement (parser);
6362 /* If that worked, we're done. */
6363 if (cp_parser_parse_definitely (parser))
6364 return;
6366 /* Look for an expression-statement instead. */
6367 statement = cp_parser_expression_statement (parser, in_statement_expr);
6370 /* Set the line number for the statement. */
6371 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6372 SET_EXPR_LOCATION (statement, statement_location);
6375 /* Parse the label for a labeled-statement, i.e.
6377 identifier :
6378 case constant-expression :
6379 default :
6381 GNU Extension:
6382 case constant-expression ... constant-expression : statement
6384 When a label is parsed without errors, the label is added to the
6385 parse tree by the finish_* functions, so this function doesn't
6386 have to return the label. */
6388 static void
6389 cp_parser_label_for_labeled_statement (cp_parser* parser)
6391 cp_token *token;
6393 /* The next token should be an identifier. */
6394 token = cp_lexer_peek_token (parser->lexer);
6395 if (token->type != CPP_NAME
6396 && token->type != CPP_KEYWORD)
6398 cp_parser_error (parser, "expected labeled-statement");
6399 return;
6402 switch (token->keyword)
6404 case RID_CASE:
6406 tree expr, expr_hi;
6407 cp_token *ellipsis;
6409 /* Consume the `case' token. */
6410 cp_lexer_consume_token (parser->lexer);
6411 /* Parse the constant-expression. */
6412 expr = cp_parser_constant_expression (parser,
6413 /*allow_non_constant_p=*/false,
6414 NULL);
6416 ellipsis = cp_lexer_peek_token (parser->lexer);
6417 if (ellipsis->type == CPP_ELLIPSIS)
6419 /* Consume the `...' token. */
6420 cp_lexer_consume_token (parser->lexer);
6421 expr_hi =
6422 cp_parser_constant_expression (parser,
6423 /*allow_non_constant_p=*/false,
6424 NULL);
6425 /* We don't need to emit warnings here, as the common code
6426 will do this for us. */
6428 else
6429 expr_hi = NULL_TREE;
6431 if (parser->in_switch_statement_p)
6432 finish_case_label (expr, expr_hi);
6433 else
6434 error ("case label %qE not within a switch statement", expr);
6436 break;
6438 case RID_DEFAULT:
6439 /* Consume the `default' token. */
6440 cp_lexer_consume_token (parser->lexer);
6442 if (parser->in_switch_statement_p)
6443 finish_case_label (NULL_TREE, NULL_TREE);
6444 else
6445 error ("case label not within a switch statement");
6446 break;
6448 default:
6449 /* Anything else must be an ordinary label. */
6450 finish_label_stmt (cp_parser_identifier (parser));
6451 break;
6454 /* Require the `:' token. */
6455 cp_parser_require (parser, CPP_COLON, "`:'");
6458 /* Parse an expression-statement.
6460 expression-statement:
6461 expression [opt] ;
6463 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6464 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6465 indicates whether this expression-statement is part of an
6466 expression statement. */
6468 static tree
6469 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6471 tree statement = NULL_TREE;
6473 /* If the next token is a ';', then there is no expression
6474 statement. */
6475 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6476 statement = cp_parser_expression (parser, /*cast_p=*/false);
6478 /* Consume the final `;'. */
6479 cp_parser_consume_semicolon_at_end_of_statement (parser);
6481 if (in_statement_expr
6482 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6483 /* This is the final expression statement of a statement
6484 expression. */
6485 statement = finish_stmt_expr_expr (statement, in_statement_expr);
6486 else if (statement)
6487 statement = finish_expr_stmt (statement);
6488 else
6489 finish_stmt ();
6491 return statement;
6494 /* Parse a compound-statement.
6496 compound-statement:
6497 { statement-seq [opt] }
6499 Returns a tree representing the statement. */
6501 static tree
6502 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6503 bool in_try)
6505 tree compound_stmt;
6507 /* Consume the `{'. */
6508 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6509 return error_mark_node;
6510 /* Begin the compound-statement. */
6511 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6512 /* Parse an (optional) statement-seq. */
6513 cp_parser_statement_seq_opt (parser, in_statement_expr);
6514 /* Finish the compound-statement. */
6515 finish_compound_stmt (compound_stmt);
6516 /* Consume the `}'. */
6517 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6519 return compound_stmt;
6522 /* Parse an (optional) statement-seq.
6524 statement-seq:
6525 statement
6526 statement-seq [opt] statement */
6528 static void
6529 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6531 /* Scan statements until there aren't any more. */
6532 while (true)
6534 cp_token *token = cp_lexer_peek_token (parser->lexer);
6536 /* If we're looking at a `}', then we've run out of statements. */
6537 if (token->type == CPP_CLOSE_BRACE
6538 || token->type == CPP_EOF
6539 || token->type == CPP_PRAGMA_EOL)
6540 break;
6542 /* Parse the statement. */
6543 cp_parser_statement (parser, in_statement_expr, true, NULL);
6547 /* Parse a selection-statement.
6549 selection-statement:
6550 if ( condition ) statement
6551 if ( condition ) statement else statement
6552 switch ( condition ) statement
6554 Returns the new IF_STMT or SWITCH_STMT.
6556 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6557 is a (possibly labeled) if statement which is not enclosed in
6558 braces and has an else clause. This is used to implement
6559 -Wparentheses. */
6561 static tree
6562 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
6564 cp_token *token;
6565 enum rid keyword;
6567 if (if_p != NULL)
6568 *if_p = false;
6570 /* Peek at the next token. */
6571 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6573 /* See what kind of keyword it is. */
6574 keyword = token->keyword;
6575 switch (keyword)
6577 case RID_IF:
6578 case RID_SWITCH:
6580 tree statement;
6581 tree condition;
6583 /* Look for the `('. */
6584 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6586 cp_parser_skip_to_end_of_statement (parser);
6587 return error_mark_node;
6590 /* Begin the selection-statement. */
6591 if (keyword == RID_IF)
6592 statement = begin_if_stmt ();
6593 else
6594 statement = begin_switch_stmt ();
6596 /* Parse the condition. */
6597 condition = cp_parser_condition (parser);
6598 /* Look for the `)'. */
6599 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6600 cp_parser_skip_to_closing_parenthesis (parser, true, false,
6601 /*consume_paren=*/true);
6603 if (keyword == RID_IF)
6605 bool nested_if;
6607 /* Add the condition. */
6608 finish_if_stmt_cond (condition, statement);
6610 /* Parse the then-clause. */
6611 cp_parser_implicitly_scoped_statement (parser, &nested_if);
6612 finish_then_clause (statement);
6614 /* If the next token is `else', parse the else-clause. */
6615 if (cp_lexer_next_token_is_keyword (parser->lexer,
6616 RID_ELSE))
6618 /* Consume the `else' keyword. */
6619 cp_lexer_consume_token (parser->lexer);
6620 begin_else_clause (statement);
6621 /* Parse the else-clause. */
6622 cp_parser_implicitly_scoped_statement (parser, NULL);
6623 finish_else_clause (statement);
6625 /* If we are currently parsing a then-clause, then
6626 IF_P will not be NULL. We set it to true to
6627 indicate that this if statement has an else clause.
6628 This may trigger the Wparentheses warning below
6629 when we get back up to the parent if statement. */
6630 if (if_p != NULL)
6631 *if_p = true;
6633 else
6635 /* This if statement does not have an else clause. If
6636 NESTED_IF is true, then the then-clause is an if
6637 statement which does have an else clause. We warn
6638 about the potential ambiguity. */
6639 if (nested_if)
6640 warning (OPT_Wparentheses,
6641 ("%Hsuggest explicit braces "
6642 "to avoid ambiguous %<else%>"),
6643 EXPR_LOCUS (statement));
6646 /* Now we're all done with the if-statement. */
6647 finish_if_stmt (statement);
6649 else
6651 bool in_switch_statement_p;
6652 unsigned char in_statement;
6654 /* Add the condition. */
6655 finish_switch_cond (condition, statement);
6657 /* Parse the body of the switch-statement. */
6658 in_switch_statement_p = parser->in_switch_statement_p;
6659 in_statement = parser->in_statement;
6660 parser->in_switch_statement_p = true;
6661 parser->in_statement |= IN_SWITCH_STMT;
6662 cp_parser_implicitly_scoped_statement (parser, NULL);
6663 parser->in_switch_statement_p = in_switch_statement_p;
6664 parser->in_statement = in_statement;
6666 /* Now we're all done with the switch-statement. */
6667 finish_switch_stmt (statement);
6670 return statement;
6672 break;
6674 default:
6675 cp_parser_error (parser, "expected selection-statement");
6676 return error_mark_node;
6680 /* Parse a condition.
6682 condition:
6683 expression
6684 type-specifier-seq declarator = assignment-expression
6686 GNU Extension:
6688 condition:
6689 type-specifier-seq declarator asm-specification [opt]
6690 attributes [opt] = assignment-expression
6692 Returns the expression that should be tested. */
6694 static tree
6695 cp_parser_condition (cp_parser* parser)
6697 cp_decl_specifier_seq type_specifiers;
6698 const char *saved_message;
6700 /* Try the declaration first. */
6701 cp_parser_parse_tentatively (parser);
6702 /* New types are not allowed in the type-specifier-seq for a
6703 condition. */
6704 saved_message = parser->type_definition_forbidden_message;
6705 parser->type_definition_forbidden_message
6706 = "types may not be defined in conditions";
6707 /* Parse the type-specifier-seq. */
6708 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
6709 &type_specifiers);
6710 /* Restore the saved message. */
6711 parser->type_definition_forbidden_message = saved_message;
6712 /* If all is well, we might be looking at a declaration. */
6713 if (!cp_parser_error_occurred (parser))
6715 tree decl;
6716 tree asm_specification;
6717 tree attributes;
6718 cp_declarator *declarator;
6719 tree initializer = NULL_TREE;
6721 /* Parse the declarator. */
6722 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6723 /*ctor_dtor_or_conv_p=*/NULL,
6724 /*parenthesized_p=*/NULL,
6725 /*member_p=*/false);
6726 /* Parse the attributes. */
6727 attributes = cp_parser_attributes_opt (parser);
6728 /* Parse the asm-specification. */
6729 asm_specification = cp_parser_asm_specification_opt (parser);
6730 /* If the next token is not an `=', then we might still be
6731 looking at an expression. For example:
6733 if (A(a).x)
6735 looks like a decl-specifier-seq and a declarator -- but then
6736 there is no `=', so this is an expression. */
6737 cp_parser_require (parser, CPP_EQ, "`='");
6738 /* If we did see an `=', then we are looking at a declaration
6739 for sure. */
6740 if (cp_parser_parse_definitely (parser))
6742 tree pushed_scope;
6743 bool non_constant_p;
6745 /* Create the declaration. */
6746 decl = start_decl (declarator, &type_specifiers,
6747 /*initialized_p=*/true,
6748 attributes, /*prefix_attributes=*/NULL_TREE,
6749 &pushed_scope);
6750 /* Parse the assignment-expression. */
6751 initializer
6752 = cp_parser_constant_expression (parser,
6753 /*allow_non_constant_p=*/true,
6754 &non_constant_p);
6755 if (!non_constant_p)
6756 initializer = fold_non_dependent_expr (initializer);
6758 /* Process the initializer. */
6759 cp_finish_decl (decl,
6760 initializer, !non_constant_p,
6761 asm_specification,
6762 LOOKUP_ONLYCONVERTING);
6764 if (pushed_scope)
6765 pop_scope (pushed_scope);
6767 return convert_from_reference (decl);
6770 /* If we didn't even get past the declarator successfully, we are
6771 definitely not looking at a declaration. */
6772 else
6773 cp_parser_abort_tentative_parse (parser);
6775 /* Otherwise, we are looking at an expression. */
6776 return cp_parser_expression (parser, /*cast_p=*/false);
6779 /* Parse an iteration-statement.
6781 iteration-statement:
6782 while ( condition ) statement
6783 do statement while ( expression ) ;
6784 for ( for-init-statement condition [opt] ; expression [opt] )
6785 statement
6787 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6789 static tree
6790 cp_parser_iteration_statement (cp_parser* parser)
6792 cp_token *token;
6793 enum rid keyword;
6794 tree statement;
6795 unsigned char in_statement;
6797 /* Peek at the next token. */
6798 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6799 if (!token)
6800 return error_mark_node;
6802 /* Remember whether or not we are already within an iteration
6803 statement. */
6804 in_statement = parser->in_statement;
6806 /* See what kind of keyword it is. */
6807 keyword = token->keyword;
6808 switch (keyword)
6810 case RID_WHILE:
6812 tree condition;
6814 /* Begin the while-statement. */
6815 statement = begin_while_stmt ();
6816 /* Look for the `('. */
6817 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6818 /* Parse the condition. */
6819 condition = cp_parser_condition (parser);
6820 finish_while_stmt_cond (condition, statement);
6821 /* Look for the `)'. */
6822 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6823 /* Parse the dependent statement. */
6824 parser->in_statement = IN_ITERATION_STMT;
6825 cp_parser_already_scoped_statement (parser);
6826 parser->in_statement = in_statement;
6827 /* We're done with the while-statement. */
6828 finish_while_stmt (statement);
6830 break;
6832 case RID_DO:
6834 tree expression;
6836 /* Begin the do-statement. */
6837 statement = begin_do_stmt ();
6838 /* Parse the body of the do-statement. */
6839 parser->in_statement = IN_ITERATION_STMT;
6840 cp_parser_implicitly_scoped_statement (parser, NULL);
6841 parser->in_statement = in_statement;
6842 finish_do_body (statement);
6843 /* Look for the `while' keyword. */
6844 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6845 /* Look for the `('. */
6846 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6847 /* Parse the expression. */
6848 expression = cp_parser_expression (parser, /*cast_p=*/false);
6849 /* We're done with the do-statement. */
6850 finish_do_stmt (expression, statement);
6851 /* Look for the `)'. */
6852 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6853 /* Look for the `;'. */
6854 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6856 break;
6858 case RID_FOR:
6860 tree condition = NULL_TREE;
6861 tree expression = NULL_TREE;
6863 /* Begin the for-statement. */
6864 statement = begin_for_stmt ();
6865 /* Look for the `('. */
6866 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6867 /* Parse the initialization. */
6868 cp_parser_for_init_statement (parser);
6869 finish_for_init_stmt (statement);
6871 /* If there's a condition, process it. */
6872 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6873 condition = cp_parser_condition (parser);
6874 finish_for_cond (condition, statement);
6875 /* Look for the `;'. */
6876 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6878 /* If there's an expression, process it. */
6879 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6880 expression = cp_parser_expression (parser, /*cast_p=*/false);
6881 finish_for_expr (expression, statement);
6882 /* Look for the `)'. */
6883 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6885 /* Parse the body of the for-statement. */
6886 parser->in_statement = IN_ITERATION_STMT;
6887 cp_parser_already_scoped_statement (parser);
6888 parser->in_statement = in_statement;
6890 /* We're done with the for-statement. */
6891 finish_for_stmt (statement);
6893 break;
6895 default:
6896 cp_parser_error (parser, "expected iteration-statement");
6897 statement = error_mark_node;
6898 break;
6901 return statement;
6904 /* Parse a for-init-statement.
6906 for-init-statement:
6907 expression-statement
6908 simple-declaration */
6910 static void
6911 cp_parser_for_init_statement (cp_parser* parser)
6913 /* If the next token is a `;', then we have an empty
6914 expression-statement. Grammatically, this is also a
6915 simple-declaration, but an invalid one, because it does not
6916 declare anything. Therefore, if we did not handle this case
6917 specially, we would issue an error message about an invalid
6918 declaration. */
6919 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6921 /* We're going to speculatively look for a declaration, falling back
6922 to an expression, if necessary. */
6923 cp_parser_parse_tentatively (parser);
6924 /* Parse the declaration. */
6925 cp_parser_simple_declaration (parser,
6926 /*function_definition_allowed_p=*/false);
6927 /* If the tentative parse failed, then we shall need to look for an
6928 expression-statement. */
6929 if (cp_parser_parse_definitely (parser))
6930 return;
6933 cp_parser_expression_statement (parser, false);
6936 /* Parse a jump-statement.
6938 jump-statement:
6939 break ;
6940 continue ;
6941 return expression [opt] ;
6942 goto identifier ;
6944 GNU extension:
6946 jump-statement:
6947 goto * expression ;
6949 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
6951 static tree
6952 cp_parser_jump_statement (cp_parser* parser)
6954 tree statement = error_mark_node;
6955 cp_token *token;
6956 enum rid keyword;
6958 /* Peek at the next token. */
6959 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6960 if (!token)
6961 return error_mark_node;
6963 /* See what kind of keyword it is. */
6964 keyword = token->keyword;
6965 switch (keyword)
6967 case RID_BREAK:
6968 switch (parser->in_statement)
6970 case 0:
6971 error ("break statement not within loop or switch");
6972 break;
6973 default:
6974 gcc_assert ((parser->in_statement & IN_SWITCH_STMT)
6975 || parser->in_statement == IN_ITERATION_STMT);
6976 statement = finish_break_stmt ();
6977 break;
6978 case IN_OMP_BLOCK:
6979 error ("invalid exit from OpenMP structured block");
6980 break;
6981 case IN_OMP_FOR:
6982 error ("break statement used with OpenMP for loop");
6983 break;
6985 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6986 break;
6988 case RID_CONTINUE:
6989 switch (parser->in_statement & ~IN_SWITCH_STMT)
6991 case 0:
6992 error ("continue statement not within a loop");
6993 break;
6994 case IN_ITERATION_STMT:
6995 case IN_OMP_FOR:
6996 statement = finish_continue_stmt ();
6997 break;
6998 case IN_OMP_BLOCK:
6999 error ("invalid exit from OpenMP structured block");
7000 break;
7001 default:
7002 gcc_unreachable ();
7004 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7005 break;
7007 case RID_RETURN:
7009 tree expr;
7011 /* If the next token is a `;', then there is no
7012 expression. */
7013 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7014 expr = cp_parser_expression (parser, /*cast_p=*/false);
7015 else
7016 expr = NULL_TREE;
7017 /* Build the return-statement. */
7018 statement = finish_return_stmt (expr);
7019 /* Look for the final `;'. */
7020 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7022 break;
7024 case RID_GOTO:
7025 /* Create the goto-statement. */
7026 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7028 /* Issue a warning about this use of a GNU extension. */
7029 if (pedantic)
7030 pedwarn ("ISO C++ forbids computed gotos");
7031 /* Consume the '*' token. */
7032 cp_lexer_consume_token (parser->lexer);
7033 /* Parse the dependent expression. */
7034 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
7036 else
7037 finish_goto_stmt (cp_parser_identifier (parser));
7038 /* Look for the final `;'. */
7039 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7040 break;
7042 default:
7043 cp_parser_error (parser, "expected jump-statement");
7044 break;
7047 return statement;
7050 /* Parse a declaration-statement.
7052 declaration-statement:
7053 block-declaration */
7055 static void
7056 cp_parser_declaration_statement (cp_parser* parser)
7058 void *p;
7060 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7061 p = obstack_alloc (&declarator_obstack, 0);
7063 /* Parse the block-declaration. */
7064 cp_parser_block_declaration (parser, /*statement_p=*/true);
7066 /* Free any declarators allocated. */
7067 obstack_free (&declarator_obstack, p);
7069 /* Finish off the statement. */
7070 finish_stmt ();
7073 /* Some dependent statements (like `if (cond) statement'), are
7074 implicitly in their own scope. In other words, if the statement is
7075 a single statement (as opposed to a compound-statement), it is
7076 none-the-less treated as if it were enclosed in braces. Any
7077 declarations appearing in the dependent statement are out of scope
7078 after control passes that point. This function parses a statement,
7079 but ensures that is in its own scope, even if it is not a
7080 compound-statement.
7082 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7083 is a (possibly labeled) if statement which is not enclosed in
7084 braces and has an else clause. This is used to implement
7085 -Wparentheses.
7087 Returns the new statement. */
7089 static tree
7090 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7092 tree statement;
7094 if (if_p != NULL)
7095 *if_p = false;
7097 /* Mark if () ; with a special NOP_EXPR. */
7098 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7100 cp_lexer_consume_token (parser->lexer);
7101 statement = add_stmt (build_empty_stmt ());
7103 /* if a compound is opened, we simply parse the statement directly. */
7104 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7105 statement = cp_parser_compound_statement (parser, NULL, false);
7106 /* If the token is not a `{', then we must take special action. */
7107 else
7109 /* Create a compound-statement. */
7110 statement = begin_compound_stmt (0);
7111 /* Parse the dependent-statement. */
7112 cp_parser_statement (parser, NULL_TREE, false, if_p);
7113 /* Finish the dummy compound-statement. */
7114 finish_compound_stmt (statement);
7117 /* Return the statement. */
7118 return statement;
7121 /* For some dependent statements (like `while (cond) statement'), we
7122 have already created a scope. Therefore, even if the dependent
7123 statement is a compound-statement, we do not want to create another
7124 scope. */
7126 static void
7127 cp_parser_already_scoped_statement (cp_parser* parser)
7129 /* If the token is a `{', then we must take special action. */
7130 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7131 cp_parser_statement (parser, NULL_TREE, false, NULL);
7132 else
7134 /* Avoid calling cp_parser_compound_statement, so that we
7135 don't create a new scope. Do everything else by hand. */
7136 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
7137 cp_parser_statement_seq_opt (parser, NULL_TREE);
7138 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7142 /* Declarations [gram.dcl.dcl] */
7144 /* Parse an optional declaration-sequence.
7146 declaration-seq:
7147 declaration
7148 declaration-seq declaration */
7150 static void
7151 cp_parser_declaration_seq_opt (cp_parser* parser)
7153 while (true)
7155 cp_token *token;
7157 token = cp_lexer_peek_token (parser->lexer);
7159 if (token->type == CPP_CLOSE_BRACE
7160 || token->type == CPP_EOF
7161 || token->type == CPP_PRAGMA_EOL)
7162 break;
7164 if (token->type == CPP_SEMICOLON)
7166 /* A declaration consisting of a single semicolon is
7167 invalid. Allow it unless we're being pedantic. */
7168 cp_lexer_consume_token (parser->lexer);
7169 if (pedantic && !in_system_header)
7170 pedwarn ("extra %<;%>");
7171 continue;
7174 /* If we're entering or exiting a region that's implicitly
7175 extern "C", modify the lang context appropriately. */
7176 if (!parser->implicit_extern_c && token->implicit_extern_c)
7178 push_lang_context (lang_name_c);
7179 parser->implicit_extern_c = true;
7181 else if (parser->implicit_extern_c && !token->implicit_extern_c)
7183 pop_lang_context ();
7184 parser->implicit_extern_c = false;
7187 if (token->type == CPP_PRAGMA)
7189 /* A top-level declaration can consist solely of a #pragma.
7190 A nested declaration cannot, so this is done here and not
7191 in cp_parser_declaration. (A #pragma at block scope is
7192 handled in cp_parser_statement.) */
7193 cp_parser_pragma (parser, pragma_external);
7194 continue;
7197 /* Parse the declaration itself. */
7198 cp_parser_declaration (parser);
7202 /* Parse a declaration.
7204 declaration:
7205 block-declaration
7206 function-definition
7207 template-declaration
7208 explicit-instantiation
7209 explicit-specialization
7210 linkage-specification
7211 namespace-definition
7213 GNU extension:
7215 declaration:
7216 __extension__ declaration */
7218 static void
7219 cp_parser_declaration (cp_parser* parser)
7221 cp_token token1;
7222 cp_token token2;
7223 int saved_pedantic;
7224 void *p;
7226 /* Check for the `__extension__' keyword. */
7227 if (cp_parser_extension_opt (parser, &saved_pedantic))
7229 /* Parse the qualified declaration. */
7230 cp_parser_declaration (parser);
7231 /* Restore the PEDANTIC flag. */
7232 pedantic = saved_pedantic;
7234 return;
7237 /* Try to figure out what kind of declaration is present. */
7238 token1 = *cp_lexer_peek_token (parser->lexer);
7240 if (token1.type != CPP_EOF)
7241 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7242 else
7244 token2.type = CPP_EOF;
7245 token2.keyword = RID_MAX;
7248 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7249 p = obstack_alloc (&declarator_obstack, 0);
7251 /* If the next token is `extern' and the following token is a string
7252 literal, then we have a linkage specification. */
7253 if (token1.keyword == RID_EXTERN
7254 && cp_parser_is_string_literal (&token2))
7255 cp_parser_linkage_specification (parser);
7256 /* If the next token is `template', then we have either a template
7257 declaration, an explicit instantiation, or an explicit
7258 specialization. */
7259 else if (token1.keyword == RID_TEMPLATE)
7261 /* `template <>' indicates a template specialization. */
7262 if (token2.type == CPP_LESS
7263 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7264 cp_parser_explicit_specialization (parser);
7265 /* `template <' indicates a template declaration. */
7266 else if (token2.type == CPP_LESS)
7267 cp_parser_template_declaration (parser, /*member_p=*/false);
7268 /* Anything else must be an explicit instantiation. */
7269 else
7270 cp_parser_explicit_instantiation (parser);
7272 /* If the next token is `export', then we have a template
7273 declaration. */
7274 else if (token1.keyword == RID_EXPORT)
7275 cp_parser_template_declaration (parser, /*member_p=*/false);
7276 /* If the next token is `extern', 'static' or 'inline' and the one
7277 after that is `template', we have a GNU extended explicit
7278 instantiation directive. */
7279 else if (cp_parser_allow_gnu_extensions_p (parser)
7280 && (token1.keyword == RID_EXTERN
7281 || token1.keyword == RID_STATIC
7282 || token1.keyword == RID_INLINE)
7283 && token2.keyword == RID_TEMPLATE)
7284 cp_parser_explicit_instantiation (parser);
7285 /* If the next token is `namespace', check for a named or unnamed
7286 namespace definition. */
7287 else if (token1.keyword == RID_NAMESPACE
7288 && (/* A named namespace definition. */
7289 (token2.type == CPP_NAME
7290 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7291 != CPP_EQ))
7292 /* An unnamed namespace definition. */
7293 || token2.type == CPP_OPEN_BRACE
7294 || token2.keyword == RID_ATTRIBUTE))
7295 cp_parser_namespace_definition (parser);
7296 /* Objective-C++ declaration/definition. */
7297 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7298 cp_parser_objc_declaration (parser);
7299 /* We must have either a block declaration or a function
7300 definition. */
7301 else
7302 /* Try to parse a block-declaration, or a function-definition. */
7303 cp_parser_block_declaration (parser, /*statement_p=*/false);
7305 /* Free any declarators allocated. */
7306 obstack_free (&declarator_obstack, p);
7309 /* Parse a block-declaration.
7311 block-declaration:
7312 simple-declaration
7313 asm-definition
7314 namespace-alias-definition
7315 using-declaration
7316 using-directive
7318 GNU Extension:
7320 block-declaration:
7321 __extension__ block-declaration
7322 label-declaration
7324 C++0x Extension:
7326 block-declaration:
7327 static_assert-declaration
7329 If STATEMENT_P is TRUE, then this block-declaration is occurring as
7330 part of a declaration-statement. */
7332 static void
7333 cp_parser_block_declaration (cp_parser *parser,
7334 bool statement_p)
7336 cp_token *token1;
7337 int saved_pedantic;
7339 /* Check for the `__extension__' keyword. */
7340 if (cp_parser_extension_opt (parser, &saved_pedantic))
7342 /* Parse the qualified declaration. */
7343 cp_parser_block_declaration (parser, statement_p);
7344 /* Restore the PEDANTIC flag. */
7345 pedantic = saved_pedantic;
7347 return;
7350 /* Peek at the next token to figure out which kind of declaration is
7351 present. */
7352 token1 = cp_lexer_peek_token (parser->lexer);
7354 /* If the next keyword is `asm', we have an asm-definition. */
7355 if (token1->keyword == RID_ASM)
7357 if (statement_p)
7358 cp_parser_commit_to_tentative_parse (parser);
7359 cp_parser_asm_definition (parser);
7361 /* If the next keyword is `namespace', we have a
7362 namespace-alias-definition. */
7363 else if (token1->keyword == RID_NAMESPACE)
7364 cp_parser_namespace_alias_definition (parser);
7365 /* If the next keyword is `using', we have either a
7366 using-declaration or a using-directive. */
7367 else if (token1->keyword == RID_USING)
7369 cp_token *token2;
7371 if (statement_p)
7372 cp_parser_commit_to_tentative_parse (parser);
7373 /* If the token after `using' is `namespace', then we have a
7374 using-directive. */
7375 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7376 if (token2->keyword == RID_NAMESPACE)
7377 cp_parser_using_directive (parser);
7378 /* Otherwise, it's a using-declaration. */
7379 else
7380 cp_parser_using_declaration (parser,
7381 /*access_declaration_p=*/false);
7383 /* If the next keyword is `__label__' we have a label declaration. */
7384 else if (token1->keyword == RID_LABEL)
7386 if (statement_p)
7387 cp_parser_commit_to_tentative_parse (parser);
7388 cp_parser_label_declaration (parser);
7390 /* If the next token is `static_assert' we have a static assertion. */
7391 else if (token1->keyword == RID_STATIC_ASSERT)
7392 cp_parser_static_assert (parser, /*member_p=*/false);
7393 /* Anything else must be a simple-declaration. */
7394 else
7395 cp_parser_simple_declaration (parser, !statement_p);
7398 /* Parse a simple-declaration.
7400 simple-declaration:
7401 decl-specifier-seq [opt] init-declarator-list [opt] ;
7403 init-declarator-list:
7404 init-declarator
7405 init-declarator-list , init-declarator
7407 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7408 function-definition as a simple-declaration. */
7410 static void
7411 cp_parser_simple_declaration (cp_parser* parser,
7412 bool function_definition_allowed_p)
7414 cp_decl_specifier_seq decl_specifiers;
7415 int declares_class_or_enum;
7416 bool saw_declarator;
7418 /* Defer access checks until we know what is being declared; the
7419 checks for names appearing in the decl-specifier-seq should be
7420 done as if we were in the scope of the thing being declared. */
7421 push_deferring_access_checks (dk_deferred);
7423 /* Parse the decl-specifier-seq. We have to keep track of whether
7424 or not the decl-specifier-seq declares a named class or
7425 enumeration type, since that is the only case in which the
7426 init-declarator-list is allowed to be empty.
7428 [dcl.dcl]
7430 In a simple-declaration, the optional init-declarator-list can be
7431 omitted only when declaring a class or enumeration, that is when
7432 the decl-specifier-seq contains either a class-specifier, an
7433 elaborated-type-specifier, or an enum-specifier. */
7434 cp_parser_decl_specifier_seq (parser,
7435 CP_PARSER_FLAGS_OPTIONAL,
7436 &decl_specifiers,
7437 &declares_class_or_enum);
7438 /* We no longer need to defer access checks. */
7439 stop_deferring_access_checks ();
7441 /* In a block scope, a valid declaration must always have a
7442 decl-specifier-seq. By not trying to parse declarators, we can
7443 resolve the declaration/expression ambiguity more quickly. */
7444 if (!function_definition_allowed_p
7445 && !decl_specifiers.any_specifiers_p)
7447 cp_parser_error (parser, "expected declaration");
7448 goto done;
7451 /* If the next two tokens are both identifiers, the code is
7452 erroneous. The usual cause of this situation is code like:
7454 T t;
7456 where "T" should name a type -- but does not. */
7457 if (!decl_specifiers.type
7458 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7460 /* If parsing tentatively, we should commit; we really are
7461 looking at a declaration. */
7462 cp_parser_commit_to_tentative_parse (parser);
7463 /* Give up. */
7464 goto done;
7467 /* If we have seen at least one decl-specifier, and the next token
7468 is not a parenthesis, then we must be looking at a declaration.
7469 (After "int (" we might be looking at a functional cast.) */
7470 if (decl_specifiers.any_specifiers_p
7471 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7472 cp_parser_commit_to_tentative_parse (parser);
7474 /* Keep going until we hit the `;' at the end of the simple
7475 declaration. */
7476 saw_declarator = false;
7477 while (cp_lexer_next_token_is_not (parser->lexer,
7478 CPP_SEMICOLON))
7480 cp_token *token;
7481 bool function_definition_p;
7482 tree decl;
7484 if (saw_declarator)
7486 /* If we are processing next declarator, coma is expected */
7487 token = cp_lexer_peek_token (parser->lexer);
7488 gcc_assert (token->type == CPP_COMMA);
7489 cp_lexer_consume_token (parser->lexer);
7491 else
7492 saw_declarator = true;
7494 /* Parse the init-declarator. */
7495 decl = cp_parser_init_declarator (parser, &decl_specifiers,
7496 /*checks=*/NULL,
7497 function_definition_allowed_p,
7498 /*member_p=*/false,
7499 declares_class_or_enum,
7500 &function_definition_p);
7501 /* If an error occurred while parsing tentatively, exit quickly.
7502 (That usually happens when in the body of a function; each
7503 statement is treated as a declaration-statement until proven
7504 otherwise.) */
7505 if (cp_parser_error_occurred (parser))
7506 goto done;
7507 /* Handle function definitions specially. */
7508 if (function_definition_p)
7510 /* If the next token is a `,', then we are probably
7511 processing something like:
7513 void f() {}, *p;
7515 which is erroneous. */
7516 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7517 error ("mixing declarations and function-definitions is forbidden");
7518 /* Otherwise, we're done with the list of declarators. */
7519 else
7521 pop_deferring_access_checks ();
7522 return;
7525 /* The next token should be either a `,' or a `;'. */
7526 token = cp_lexer_peek_token (parser->lexer);
7527 /* If it's a `,', there are more declarators to come. */
7528 if (token->type == CPP_COMMA)
7529 /* will be consumed next time around */;
7530 /* If it's a `;', we are done. */
7531 else if (token->type == CPP_SEMICOLON)
7532 break;
7533 /* Anything else is an error. */
7534 else
7536 /* If we have already issued an error message we don't need
7537 to issue another one. */
7538 if (decl != error_mark_node
7539 || cp_parser_uncommitted_to_tentative_parse_p (parser))
7540 cp_parser_error (parser, "expected %<,%> or %<;%>");
7541 /* Skip tokens until we reach the end of the statement. */
7542 cp_parser_skip_to_end_of_statement (parser);
7543 /* If the next token is now a `;', consume it. */
7544 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7545 cp_lexer_consume_token (parser->lexer);
7546 goto done;
7548 /* After the first time around, a function-definition is not
7549 allowed -- even if it was OK at first. For example:
7551 int i, f() {}
7553 is not valid. */
7554 function_definition_allowed_p = false;
7557 /* Issue an error message if no declarators are present, and the
7558 decl-specifier-seq does not itself declare a class or
7559 enumeration. */
7560 if (!saw_declarator)
7562 if (cp_parser_declares_only_class_p (parser))
7563 shadow_tag (&decl_specifiers);
7564 /* Perform any deferred access checks. */
7565 perform_deferred_access_checks ();
7568 /* Consume the `;'. */
7569 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7571 done:
7572 pop_deferring_access_checks ();
7575 /* Parse a decl-specifier-seq.
7577 decl-specifier-seq:
7578 decl-specifier-seq [opt] decl-specifier
7580 decl-specifier:
7581 storage-class-specifier
7582 type-specifier
7583 function-specifier
7584 friend
7585 typedef
7587 GNU Extension:
7589 decl-specifier:
7590 attributes
7592 Set *DECL_SPECS to a representation of the decl-specifier-seq.
7594 The parser flags FLAGS is used to control type-specifier parsing.
7596 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7597 flags:
7599 1: one of the decl-specifiers is an elaborated-type-specifier
7600 (i.e., a type declaration)
7601 2: one of the decl-specifiers is an enum-specifier or a
7602 class-specifier (i.e., a type definition)
7606 static void
7607 cp_parser_decl_specifier_seq (cp_parser* parser,
7608 cp_parser_flags flags,
7609 cp_decl_specifier_seq *decl_specs,
7610 int* declares_class_or_enum)
7612 bool constructor_possible_p = !parser->in_declarator_p;
7614 /* Clear DECL_SPECS. */
7615 clear_decl_specs (decl_specs);
7617 /* Assume no class or enumeration type is declared. */
7618 *declares_class_or_enum = 0;
7620 /* Keep reading specifiers until there are no more to read. */
7621 while (true)
7623 bool constructor_p;
7624 bool found_decl_spec;
7625 cp_token *token;
7627 /* Peek at the next token. */
7628 token = cp_lexer_peek_token (parser->lexer);
7629 /* Handle attributes. */
7630 if (token->keyword == RID_ATTRIBUTE)
7632 /* Parse the attributes. */
7633 decl_specs->attributes
7634 = chainon (decl_specs->attributes,
7635 cp_parser_attributes_opt (parser));
7636 continue;
7638 /* Assume we will find a decl-specifier keyword. */
7639 found_decl_spec = true;
7640 /* If the next token is an appropriate keyword, we can simply
7641 add it to the list. */
7642 switch (token->keyword)
7644 /* decl-specifier:
7645 friend */
7646 case RID_FRIEND:
7647 if (!at_class_scope_p ())
7649 error ("%<friend%> used outside of class");
7650 cp_lexer_purge_token (parser->lexer);
7652 else
7654 ++decl_specs->specs[(int) ds_friend];
7655 /* Consume the token. */
7656 cp_lexer_consume_token (parser->lexer);
7658 break;
7660 /* function-specifier:
7661 inline
7662 virtual
7663 explicit */
7664 case RID_INLINE:
7665 case RID_VIRTUAL:
7666 case RID_EXPLICIT:
7667 cp_parser_function_specifier_opt (parser, decl_specs);
7668 break;
7670 /* decl-specifier:
7671 typedef */
7672 case RID_TYPEDEF:
7673 ++decl_specs->specs[(int) ds_typedef];
7674 /* Consume the token. */
7675 cp_lexer_consume_token (parser->lexer);
7676 /* A constructor declarator cannot appear in a typedef. */
7677 constructor_possible_p = false;
7678 /* The "typedef" keyword can only occur in a declaration; we
7679 may as well commit at this point. */
7680 cp_parser_commit_to_tentative_parse (parser);
7682 if (decl_specs->storage_class != sc_none)
7683 decl_specs->conflicting_specifiers_p = true;
7684 break;
7686 /* storage-class-specifier:
7687 auto
7688 register
7689 static
7690 extern
7691 mutable
7693 GNU Extension:
7694 thread */
7695 case RID_AUTO:
7696 case RID_REGISTER:
7697 case RID_STATIC:
7698 case RID_EXTERN:
7699 case RID_MUTABLE:
7700 /* Consume the token. */
7701 cp_lexer_consume_token (parser->lexer);
7702 cp_parser_set_storage_class (parser, decl_specs, token->keyword);
7703 break;
7704 case RID_THREAD:
7705 /* Consume the token. */
7706 cp_lexer_consume_token (parser->lexer);
7707 ++decl_specs->specs[(int) ds_thread];
7708 break;
7710 default:
7711 /* We did not yet find a decl-specifier yet. */
7712 found_decl_spec = false;
7713 break;
7716 /* Constructors are a special case. The `S' in `S()' is not a
7717 decl-specifier; it is the beginning of the declarator. */
7718 constructor_p
7719 = (!found_decl_spec
7720 && constructor_possible_p
7721 && (cp_parser_constructor_declarator_p
7722 (parser, decl_specs->specs[(int) ds_friend] != 0)));
7724 /* If we don't have a DECL_SPEC yet, then we must be looking at
7725 a type-specifier. */
7726 if (!found_decl_spec && !constructor_p)
7728 int decl_spec_declares_class_or_enum;
7729 bool is_cv_qualifier;
7730 tree type_spec;
7732 type_spec
7733 = cp_parser_type_specifier (parser, flags,
7734 decl_specs,
7735 /*is_declaration=*/true,
7736 &decl_spec_declares_class_or_enum,
7737 &is_cv_qualifier);
7739 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7741 /* If this type-specifier referenced a user-defined type
7742 (a typedef, class-name, etc.), then we can't allow any
7743 more such type-specifiers henceforth.
7745 [dcl.spec]
7747 The longest sequence of decl-specifiers that could
7748 possibly be a type name is taken as the
7749 decl-specifier-seq of a declaration. The sequence shall
7750 be self-consistent as described below.
7752 [dcl.type]
7754 As a general rule, at most one type-specifier is allowed
7755 in the complete decl-specifier-seq of a declaration. The
7756 only exceptions are the following:
7758 -- const or volatile can be combined with any other
7759 type-specifier.
7761 -- signed or unsigned can be combined with char, long,
7762 short, or int.
7764 -- ..
7766 Example:
7768 typedef char* Pc;
7769 void g (const int Pc);
7771 Here, Pc is *not* part of the decl-specifier seq; it's
7772 the declarator. Therefore, once we see a type-specifier
7773 (other than a cv-qualifier), we forbid any additional
7774 user-defined types. We *do* still allow things like `int
7775 int' to be considered a decl-specifier-seq, and issue the
7776 error message later. */
7777 if (type_spec && !is_cv_qualifier)
7778 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7779 /* A constructor declarator cannot follow a type-specifier. */
7780 if (type_spec)
7782 constructor_possible_p = false;
7783 found_decl_spec = true;
7787 /* If we still do not have a DECL_SPEC, then there are no more
7788 decl-specifiers. */
7789 if (!found_decl_spec)
7790 break;
7792 decl_specs->any_specifiers_p = true;
7793 /* After we see one decl-specifier, further decl-specifiers are
7794 always optional. */
7795 flags |= CP_PARSER_FLAGS_OPTIONAL;
7798 cp_parser_check_decl_spec (decl_specs);
7800 /* Don't allow a friend specifier with a class definition. */
7801 if (decl_specs->specs[(int) ds_friend] != 0
7802 && (*declares_class_or_enum & 2))
7803 error ("class definition may not be declared a friend");
7806 /* Parse an (optional) storage-class-specifier.
7808 storage-class-specifier:
7809 auto
7810 register
7811 static
7812 extern
7813 mutable
7815 GNU Extension:
7817 storage-class-specifier:
7818 thread
7820 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7822 static tree
7823 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7825 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7827 case RID_AUTO:
7828 case RID_REGISTER:
7829 case RID_STATIC:
7830 case RID_EXTERN:
7831 case RID_MUTABLE:
7832 case RID_THREAD:
7833 /* Consume the token. */
7834 return cp_lexer_consume_token (parser->lexer)->u.value;
7836 default:
7837 return NULL_TREE;
7841 /* Parse an (optional) function-specifier.
7843 function-specifier:
7844 inline
7845 virtual
7846 explicit
7848 Returns an IDENTIFIER_NODE corresponding to the keyword used.
7849 Updates DECL_SPECS, if it is non-NULL. */
7851 static tree
7852 cp_parser_function_specifier_opt (cp_parser* parser,
7853 cp_decl_specifier_seq *decl_specs)
7855 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7857 case RID_INLINE:
7858 if (decl_specs)
7859 ++decl_specs->specs[(int) ds_inline];
7860 break;
7862 case RID_VIRTUAL:
7863 /* 14.5.2.3 [temp.mem]
7865 A member function template shall not be virtual. */
7866 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
7867 error ("templates may not be %<virtual%>");
7868 else if (decl_specs)
7869 ++decl_specs->specs[(int) ds_virtual];
7870 break;
7872 case RID_EXPLICIT:
7873 if (decl_specs)
7874 ++decl_specs->specs[(int) ds_explicit];
7875 break;
7877 default:
7878 return NULL_TREE;
7881 /* Consume the token. */
7882 return cp_lexer_consume_token (parser->lexer)->u.value;
7885 /* Parse a linkage-specification.
7887 linkage-specification:
7888 extern string-literal { declaration-seq [opt] }
7889 extern string-literal declaration */
7891 static void
7892 cp_parser_linkage_specification (cp_parser* parser)
7894 tree linkage;
7896 /* Look for the `extern' keyword. */
7897 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7899 /* Look for the string-literal. */
7900 linkage = cp_parser_string_literal (parser, false, false);
7902 /* Transform the literal into an identifier. If the literal is a
7903 wide-character string, or contains embedded NULs, then we can't
7904 handle it as the user wants. */
7905 if (strlen (TREE_STRING_POINTER (linkage))
7906 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7908 cp_parser_error (parser, "invalid linkage-specification");
7909 /* Assume C++ linkage. */
7910 linkage = lang_name_cplusplus;
7912 else
7913 linkage = get_identifier (TREE_STRING_POINTER (linkage));
7915 /* We're now using the new linkage. */
7916 push_lang_context (linkage);
7918 /* If the next token is a `{', then we're using the first
7919 production. */
7920 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7922 /* Consume the `{' token. */
7923 cp_lexer_consume_token (parser->lexer);
7924 /* Parse the declarations. */
7925 cp_parser_declaration_seq_opt (parser);
7926 /* Look for the closing `}'. */
7927 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7929 /* Otherwise, there's just one declaration. */
7930 else
7932 bool saved_in_unbraced_linkage_specification_p;
7934 saved_in_unbraced_linkage_specification_p
7935 = parser->in_unbraced_linkage_specification_p;
7936 parser->in_unbraced_linkage_specification_p = true;
7937 cp_parser_declaration (parser);
7938 parser->in_unbraced_linkage_specification_p
7939 = saved_in_unbraced_linkage_specification_p;
7942 /* We're done with the linkage-specification. */
7943 pop_lang_context ();
7946 /* Parse a static_assert-declaration.
7948 static_assert-declaration:
7949 static_assert ( constant-expression , string-literal ) ;
7951 If MEMBER_P, this static_assert is a class member. */
7953 static void
7954 cp_parser_static_assert(cp_parser *parser, bool member_p)
7956 tree condition;
7957 tree message;
7958 cp_token *token;
7959 location_t saved_loc;
7961 /* Peek at the `static_assert' token so we can keep track of exactly
7962 where the static assertion started. */
7963 token = cp_lexer_peek_token (parser->lexer);
7964 saved_loc = token->location;
7966 /* Look for the `static_assert' keyword. */
7967 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
7968 "`static_assert'"))
7969 return;
7971 /* We know we are in a static assertion; commit to any tentative
7972 parse. */
7973 if (cp_parser_parsing_tentatively (parser))
7974 cp_parser_commit_to_tentative_parse (parser);
7976 /* Parse the `(' starting the static assertion condition. */
7977 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7979 /* Parse the constant-expression. */
7980 condition =
7981 cp_parser_constant_expression (parser,
7982 /*allow_non_constant_p=*/false,
7983 /*non_constant_p=*/NULL);
7985 /* Parse the separating `,'. */
7986 cp_parser_require (parser, CPP_COMMA, "`,'");
7988 /* Parse the string-literal message. */
7989 message = cp_parser_string_literal (parser,
7990 /*translate=*/false,
7991 /*wide_ok=*/true);
7993 /* A `)' completes the static assertion. */
7994 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
7995 cp_parser_skip_to_closing_parenthesis (parser,
7996 /*recovering=*/true,
7997 /*or_comma=*/false,
7998 /*consume_paren=*/true);
8000 /* A semicolon terminates the declaration. */
8001 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
8003 /* Complete the static assertion, which may mean either processing
8004 the static assert now or saving it for template instantiation. */
8005 finish_static_assert (condition, message, saved_loc, member_p);
8008 /* Special member functions [gram.special] */
8010 /* Parse a conversion-function-id.
8012 conversion-function-id:
8013 operator conversion-type-id
8015 Returns an IDENTIFIER_NODE representing the operator. */
8017 static tree
8018 cp_parser_conversion_function_id (cp_parser* parser)
8020 tree type;
8021 tree saved_scope;
8022 tree saved_qualifying_scope;
8023 tree saved_object_scope;
8024 tree pushed_scope = NULL_TREE;
8026 /* Look for the `operator' token. */
8027 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8028 return error_mark_node;
8029 /* When we parse the conversion-type-id, the current scope will be
8030 reset. However, we need that information in able to look up the
8031 conversion function later, so we save it here. */
8032 saved_scope = parser->scope;
8033 saved_qualifying_scope = parser->qualifying_scope;
8034 saved_object_scope = parser->object_scope;
8035 /* We must enter the scope of the class so that the names of
8036 entities declared within the class are available in the
8037 conversion-type-id. For example, consider:
8039 struct S {
8040 typedef int I;
8041 operator I();
8044 S::operator I() { ... }
8046 In order to see that `I' is a type-name in the definition, we
8047 must be in the scope of `S'. */
8048 if (saved_scope)
8049 pushed_scope = push_scope (saved_scope);
8050 /* Parse the conversion-type-id. */
8051 type = cp_parser_conversion_type_id (parser);
8052 /* Leave the scope of the class, if any. */
8053 if (pushed_scope)
8054 pop_scope (pushed_scope);
8055 /* Restore the saved scope. */
8056 parser->scope = saved_scope;
8057 parser->qualifying_scope = saved_qualifying_scope;
8058 parser->object_scope = saved_object_scope;
8059 /* If the TYPE is invalid, indicate failure. */
8060 if (type == error_mark_node)
8061 return error_mark_node;
8062 return mangle_conv_op_name_for_type (type);
8065 /* Parse a conversion-type-id:
8067 conversion-type-id:
8068 type-specifier-seq conversion-declarator [opt]
8070 Returns the TYPE specified. */
8072 static tree
8073 cp_parser_conversion_type_id (cp_parser* parser)
8075 tree attributes;
8076 cp_decl_specifier_seq type_specifiers;
8077 cp_declarator *declarator;
8078 tree type_specified;
8080 /* Parse the attributes. */
8081 attributes = cp_parser_attributes_opt (parser);
8082 /* Parse the type-specifiers. */
8083 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
8084 &type_specifiers);
8085 /* If that didn't work, stop. */
8086 if (type_specifiers.type == error_mark_node)
8087 return error_mark_node;
8088 /* Parse the conversion-declarator. */
8089 declarator = cp_parser_conversion_declarator_opt (parser);
8091 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
8092 /*initialized=*/0, &attributes);
8093 if (attributes)
8094 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
8095 return type_specified;
8098 /* Parse an (optional) conversion-declarator.
8100 conversion-declarator:
8101 ptr-operator conversion-declarator [opt]
8105 static cp_declarator *
8106 cp_parser_conversion_declarator_opt (cp_parser* parser)
8108 enum tree_code code;
8109 tree class_type;
8110 cp_cv_quals cv_quals;
8112 /* We don't know if there's a ptr-operator next, or not. */
8113 cp_parser_parse_tentatively (parser);
8114 /* Try the ptr-operator. */
8115 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
8116 /* If it worked, look for more conversion-declarators. */
8117 if (cp_parser_parse_definitely (parser))
8119 cp_declarator *declarator;
8121 /* Parse another optional declarator. */
8122 declarator = cp_parser_conversion_declarator_opt (parser);
8124 /* Create the representation of the declarator. */
8125 if (class_type)
8126 declarator = make_ptrmem_declarator (cv_quals, class_type,
8127 declarator);
8128 else if (code == INDIRECT_REF)
8129 declarator = make_pointer_declarator (cv_quals, declarator);
8130 else
8131 declarator = make_reference_declarator (cv_quals, declarator);
8133 return declarator;
8136 return NULL;
8139 /* Parse an (optional) ctor-initializer.
8141 ctor-initializer:
8142 : mem-initializer-list
8144 Returns TRUE iff the ctor-initializer was actually present. */
8146 static bool
8147 cp_parser_ctor_initializer_opt (cp_parser* parser)
8149 /* If the next token is not a `:', then there is no
8150 ctor-initializer. */
8151 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8153 /* Do default initialization of any bases and members. */
8154 if (DECL_CONSTRUCTOR_P (current_function_decl))
8155 finish_mem_initializers (NULL_TREE);
8157 return false;
8160 /* Consume the `:' token. */
8161 cp_lexer_consume_token (parser->lexer);
8162 /* And the mem-initializer-list. */
8163 cp_parser_mem_initializer_list (parser);
8165 return true;
8168 /* Parse a mem-initializer-list.
8170 mem-initializer-list:
8171 mem-initializer
8172 mem-initializer , mem-initializer-list */
8174 static void
8175 cp_parser_mem_initializer_list (cp_parser* parser)
8177 tree mem_initializer_list = NULL_TREE;
8179 /* Let the semantic analysis code know that we are starting the
8180 mem-initializer-list. */
8181 if (!DECL_CONSTRUCTOR_P (current_function_decl))
8182 error ("only constructors take base initializers");
8184 /* Loop through the list. */
8185 while (true)
8187 tree mem_initializer;
8189 /* Parse the mem-initializer. */
8190 mem_initializer = cp_parser_mem_initializer (parser);
8191 /* Add it to the list, unless it was erroneous. */
8192 if (mem_initializer != error_mark_node)
8194 TREE_CHAIN (mem_initializer) = mem_initializer_list;
8195 mem_initializer_list = mem_initializer;
8197 /* If the next token is not a `,', we're done. */
8198 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8199 break;
8200 /* Consume the `,' token. */
8201 cp_lexer_consume_token (parser->lexer);
8204 /* Perform semantic analysis. */
8205 if (DECL_CONSTRUCTOR_P (current_function_decl))
8206 finish_mem_initializers (mem_initializer_list);
8209 /* Parse a mem-initializer.
8211 mem-initializer:
8212 mem-initializer-id ( expression-list [opt] )
8214 GNU extension:
8216 mem-initializer:
8217 ( expression-list [opt] )
8219 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
8220 class) or FIELD_DECL (for a non-static data member) to initialize;
8221 the TREE_VALUE is the expression-list. An empty initialization
8222 list is represented by void_list_node. */
8224 static tree
8225 cp_parser_mem_initializer (cp_parser* parser)
8227 tree mem_initializer_id;
8228 tree expression_list;
8229 tree member;
8231 /* Find out what is being initialized. */
8232 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8234 pedwarn ("anachronistic old-style base class initializer");
8235 mem_initializer_id = NULL_TREE;
8237 else
8238 mem_initializer_id = cp_parser_mem_initializer_id (parser);
8239 member = expand_member_init (mem_initializer_id);
8240 if (member && !DECL_P (member))
8241 in_base_initializer = 1;
8243 expression_list
8244 = cp_parser_parenthesized_expression_list (parser, false,
8245 /*cast_p=*/false,
8246 /*non_constant_p=*/NULL);
8247 if (expression_list == error_mark_node)
8248 return error_mark_node;
8249 if (!expression_list)
8250 expression_list = void_type_node;
8252 in_base_initializer = 0;
8254 return member ? build_tree_list (member, expression_list) : error_mark_node;
8257 /* Parse a mem-initializer-id.
8259 mem-initializer-id:
8260 :: [opt] nested-name-specifier [opt] class-name
8261 identifier
8263 Returns a TYPE indicating the class to be initializer for the first
8264 production. Returns an IDENTIFIER_NODE indicating the data member
8265 to be initialized for the second production. */
8267 static tree
8268 cp_parser_mem_initializer_id (cp_parser* parser)
8270 bool global_scope_p;
8271 bool nested_name_specifier_p;
8272 bool template_p = false;
8273 tree id;
8275 /* `typename' is not allowed in this context ([temp.res]). */
8276 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
8278 error ("keyword %<typename%> not allowed in this context (a qualified "
8279 "member initializer is implicitly a type)");
8280 cp_lexer_consume_token (parser->lexer);
8282 /* Look for the optional `::' operator. */
8283 global_scope_p
8284 = (cp_parser_global_scope_opt (parser,
8285 /*current_scope_valid_p=*/false)
8286 != NULL_TREE);
8287 /* Look for the optional nested-name-specifier. The simplest way to
8288 implement:
8290 [temp.res]
8292 The keyword `typename' is not permitted in a base-specifier or
8293 mem-initializer; in these contexts a qualified name that
8294 depends on a template-parameter is implicitly assumed to be a
8295 type name.
8297 is to assume that we have seen the `typename' keyword at this
8298 point. */
8299 nested_name_specifier_p
8300 = (cp_parser_nested_name_specifier_opt (parser,
8301 /*typename_keyword_p=*/true,
8302 /*check_dependency_p=*/true,
8303 /*type_p=*/true,
8304 /*is_declaration=*/true)
8305 != NULL_TREE);
8306 if (nested_name_specifier_p)
8307 template_p = cp_parser_optional_template_keyword (parser);
8308 /* If there is a `::' operator or a nested-name-specifier, then we
8309 are definitely looking for a class-name. */
8310 if (global_scope_p || nested_name_specifier_p)
8311 return cp_parser_class_name (parser,
8312 /*typename_keyword_p=*/true,
8313 /*template_keyword_p=*/template_p,
8314 none_type,
8315 /*check_dependency_p=*/true,
8316 /*class_head_p=*/false,
8317 /*is_declaration=*/true);
8318 /* Otherwise, we could also be looking for an ordinary identifier. */
8319 cp_parser_parse_tentatively (parser);
8320 /* Try a class-name. */
8321 id = cp_parser_class_name (parser,
8322 /*typename_keyword_p=*/true,
8323 /*template_keyword_p=*/false,
8324 none_type,
8325 /*check_dependency_p=*/true,
8326 /*class_head_p=*/false,
8327 /*is_declaration=*/true);
8328 /* If we found one, we're done. */
8329 if (cp_parser_parse_definitely (parser))
8330 return id;
8331 /* Otherwise, look for an ordinary identifier. */
8332 return cp_parser_identifier (parser);
8335 /* Overloading [gram.over] */
8337 /* Parse an operator-function-id.
8339 operator-function-id:
8340 operator operator
8342 Returns an IDENTIFIER_NODE for the operator which is a
8343 human-readable spelling of the identifier, e.g., `operator +'. */
8345 static tree
8346 cp_parser_operator_function_id (cp_parser* parser)
8348 /* Look for the `operator' keyword. */
8349 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8350 return error_mark_node;
8351 /* And then the name of the operator itself. */
8352 return cp_parser_operator (parser);
8355 /* Parse an operator.
8357 operator:
8358 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8359 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8360 || ++ -- , ->* -> () []
8362 GNU Extensions:
8364 operator:
8365 <? >? <?= >?=
8367 Returns an IDENTIFIER_NODE for the operator which is a
8368 human-readable spelling of the identifier, e.g., `operator +'. */
8370 static tree
8371 cp_parser_operator (cp_parser* parser)
8373 tree id = NULL_TREE;
8374 cp_token *token;
8376 /* Peek at the next token. */
8377 token = cp_lexer_peek_token (parser->lexer);
8378 /* Figure out which operator we have. */
8379 switch (token->type)
8381 case CPP_KEYWORD:
8383 enum tree_code op;
8385 /* The keyword should be either `new' or `delete'. */
8386 if (token->keyword == RID_NEW)
8387 op = NEW_EXPR;
8388 else if (token->keyword == RID_DELETE)
8389 op = DELETE_EXPR;
8390 else
8391 break;
8393 /* Consume the `new' or `delete' token. */
8394 cp_lexer_consume_token (parser->lexer);
8396 /* Peek at the next token. */
8397 token = cp_lexer_peek_token (parser->lexer);
8398 /* If it's a `[' token then this is the array variant of the
8399 operator. */
8400 if (token->type == CPP_OPEN_SQUARE)
8402 /* Consume the `[' token. */
8403 cp_lexer_consume_token (parser->lexer);
8404 /* Look for the `]' token. */
8405 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8406 id = ansi_opname (op == NEW_EXPR
8407 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
8409 /* Otherwise, we have the non-array variant. */
8410 else
8411 id = ansi_opname (op);
8413 return id;
8416 case CPP_PLUS:
8417 id = ansi_opname (PLUS_EXPR);
8418 break;
8420 case CPP_MINUS:
8421 id = ansi_opname (MINUS_EXPR);
8422 break;
8424 case CPP_MULT:
8425 id = ansi_opname (MULT_EXPR);
8426 break;
8428 case CPP_DIV:
8429 id = ansi_opname (TRUNC_DIV_EXPR);
8430 break;
8432 case CPP_MOD:
8433 id = ansi_opname (TRUNC_MOD_EXPR);
8434 break;
8436 case CPP_XOR:
8437 id = ansi_opname (BIT_XOR_EXPR);
8438 break;
8440 case CPP_AND:
8441 id = ansi_opname (BIT_AND_EXPR);
8442 break;
8444 case CPP_OR:
8445 id = ansi_opname (BIT_IOR_EXPR);
8446 break;
8448 case CPP_COMPL:
8449 id = ansi_opname (BIT_NOT_EXPR);
8450 break;
8452 case CPP_NOT:
8453 id = ansi_opname (TRUTH_NOT_EXPR);
8454 break;
8456 case CPP_EQ:
8457 id = ansi_assopname (NOP_EXPR);
8458 break;
8460 case CPP_LESS:
8461 id = ansi_opname (LT_EXPR);
8462 break;
8464 case CPP_GREATER:
8465 id = ansi_opname (GT_EXPR);
8466 break;
8468 case CPP_PLUS_EQ:
8469 id = ansi_assopname (PLUS_EXPR);
8470 break;
8472 case CPP_MINUS_EQ:
8473 id = ansi_assopname (MINUS_EXPR);
8474 break;
8476 case CPP_MULT_EQ:
8477 id = ansi_assopname (MULT_EXPR);
8478 break;
8480 case CPP_DIV_EQ:
8481 id = ansi_assopname (TRUNC_DIV_EXPR);
8482 break;
8484 case CPP_MOD_EQ:
8485 id = ansi_assopname (TRUNC_MOD_EXPR);
8486 break;
8488 case CPP_XOR_EQ:
8489 id = ansi_assopname (BIT_XOR_EXPR);
8490 break;
8492 case CPP_AND_EQ:
8493 id = ansi_assopname (BIT_AND_EXPR);
8494 break;
8496 case CPP_OR_EQ:
8497 id = ansi_assopname (BIT_IOR_EXPR);
8498 break;
8500 case CPP_LSHIFT:
8501 id = ansi_opname (LSHIFT_EXPR);
8502 break;
8504 case CPP_RSHIFT:
8505 id = ansi_opname (RSHIFT_EXPR);
8506 break;
8508 case CPP_LSHIFT_EQ:
8509 id = ansi_assopname (LSHIFT_EXPR);
8510 break;
8512 case CPP_RSHIFT_EQ:
8513 id = ansi_assopname (RSHIFT_EXPR);
8514 break;
8516 case CPP_EQ_EQ:
8517 id = ansi_opname (EQ_EXPR);
8518 break;
8520 case CPP_NOT_EQ:
8521 id = ansi_opname (NE_EXPR);
8522 break;
8524 case CPP_LESS_EQ:
8525 id = ansi_opname (LE_EXPR);
8526 break;
8528 case CPP_GREATER_EQ:
8529 id = ansi_opname (GE_EXPR);
8530 break;
8532 case CPP_AND_AND:
8533 id = ansi_opname (TRUTH_ANDIF_EXPR);
8534 break;
8536 case CPP_OR_OR:
8537 id = ansi_opname (TRUTH_ORIF_EXPR);
8538 break;
8540 case CPP_PLUS_PLUS:
8541 id = ansi_opname (POSTINCREMENT_EXPR);
8542 break;
8544 case CPP_MINUS_MINUS:
8545 id = ansi_opname (PREDECREMENT_EXPR);
8546 break;
8548 case CPP_COMMA:
8549 id = ansi_opname (COMPOUND_EXPR);
8550 break;
8552 case CPP_DEREF_STAR:
8553 id = ansi_opname (MEMBER_REF);
8554 break;
8556 case CPP_DEREF:
8557 id = ansi_opname (COMPONENT_REF);
8558 break;
8560 case CPP_OPEN_PAREN:
8561 /* Consume the `('. */
8562 cp_lexer_consume_token (parser->lexer);
8563 /* Look for the matching `)'. */
8564 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8565 return ansi_opname (CALL_EXPR);
8567 case CPP_OPEN_SQUARE:
8568 /* Consume the `['. */
8569 cp_lexer_consume_token (parser->lexer);
8570 /* Look for the matching `]'. */
8571 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8572 return ansi_opname (ARRAY_REF);
8574 default:
8575 /* Anything else is an error. */
8576 break;
8579 /* If we have selected an identifier, we need to consume the
8580 operator token. */
8581 if (id)
8582 cp_lexer_consume_token (parser->lexer);
8583 /* Otherwise, no valid operator name was present. */
8584 else
8586 cp_parser_error (parser, "expected operator");
8587 id = error_mark_node;
8590 return id;
8593 /* Parse a template-declaration.
8595 template-declaration:
8596 export [opt] template < template-parameter-list > declaration
8598 If MEMBER_P is TRUE, this template-declaration occurs within a
8599 class-specifier.
8601 The grammar rule given by the standard isn't correct. What
8602 is really meant is:
8604 template-declaration:
8605 export [opt] template-parameter-list-seq
8606 decl-specifier-seq [opt] init-declarator [opt] ;
8607 export [opt] template-parameter-list-seq
8608 function-definition
8610 template-parameter-list-seq:
8611 template-parameter-list-seq [opt]
8612 template < template-parameter-list > */
8614 static void
8615 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8617 /* Check for `export'. */
8618 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8620 /* Consume the `export' token. */
8621 cp_lexer_consume_token (parser->lexer);
8622 /* Warn that we do not support `export'. */
8623 warning (0, "keyword %<export%> not implemented, and will be ignored");
8626 cp_parser_template_declaration_after_export (parser, member_p);
8629 /* Parse a template-parameter-list.
8631 template-parameter-list:
8632 template-parameter
8633 template-parameter-list , template-parameter
8635 Returns a TREE_LIST. Each node represents a template parameter.
8636 The nodes are connected via their TREE_CHAINs. */
8638 static tree
8639 cp_parser_template_parameter_list (cp_parser* parser)
8641 tree parameter_list = NULL_TREE;
8643 begin_template_parm_list ();
8644 while (true)
8646 tree parameter;
8647 cp_token *token;
8648 bool is_non_type;
8650 /* Parse the template-parameter. */
8651 parameter = cp_parser_template_parameter (parser, &is_non_type);
8652 /* Add it to the list. */
8653 if (parameter != error_mark_node)
8654 parameter_list = process_template_parm (parameter_list,
8655 parameter,
8656 is_non_type);
8657 else
8659 tree err_parm = build_tree_list (parameter, parameter);
8660 TREE_VALUE (err_parm) = error_mark_node;
8661 parameter_list = chainon (parameter_list, err_parm);
8664 /* Peek at the next token. */
8665 token = cp_lexer_peek_token (parser->lexer);
8666 /* If it's not a `,', we're done. */
8667 if (token->type != CPP_COMMA)
8668 break;
8669 /* Otherwise, consume the `,' token. */
8670 cp_lexer_consume_token (parser->lexer);
8673 return end_template_parm_list (parameter_list);
8676 /* Parse a template-parameter.
8678 template-parameter:
8679 type-parameter
8680 parameter-declaration
8682 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
8683 the parameter. The TREE_PURPOSE is the default value, if any.
8684 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
8685 iff this parameter is a non-type parameter. */
8687 static tree
8688 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8690 cp_token *token;
8691 cp_parameter_declarator *parameter_declarator;
8692 tree parm;
8694 /* Assume it is a type parameter or a template parameter. */
8695 *is_non_type = false;
8696 /* Peek at the next token. */
8697 token = cp_lexer_peek_token (parser->lexer);
8698 /* If it is `class' or `template', we have a type-parameter. */
8699 if (token->keyword == RID_TEMPLATE)
8700 return cp_parser_type_parameter (parser);
8701 /* If it is `class' or `typename' we do not know yet whether it is a
8702 type parameter or a non-type parameter. Consider:
8704 template <typename T, typename T::X X> ...
8708 template <class C, class D*> ...
8710 Here, the first parameter is a type parameter, and the second is
8711 a non-type parameter. We can tell by looking at the token after
8712 the identifier -- if it is a `,', `=', or `>' then we have a type
8713 parameter. */
8714 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8716 /* Peek at the token after `class' or `typename'. */
8717 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8718 /* If it's an identifier, skip it. */
8719 if (token->type == CPP_NAME)
8720 token = cp_lexer_peek_nth_token (parser->lexer, 3);
8721 /* Now, see if the token looks like the end of a template
8722 parameter. */
8723 if (token->type == CPP_COMMA
8724 || token->type == CPP_EQ
8725 || token->type == CPP_GREATER)
8726 return cp_parser_type_parameter (parser);
8729 /* Otherwise, it is a non-type parameter.
8731 [temp.param]
8733 When parsing a default template-argument for a non-type
8734 template-parameter, the first non-nested `>' is taken as the end
8735 of the template parameter-list rather than a greater-than
8736 operator. */
8737 *is_non_type = true;
8738 parameter_declarator
8739 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8740 /*parenthesized_p=*/NULL);
8741 parm = grokdeclarator (parameter_declarator->declarator,
8742 &parameter_declarator->decl_specifiers,
8743 PARM, /*initialized=*/0,
8744 /*attrlist=*/NULL);
8745 if (parm == error_mark_node)
8746 return error_mark_node;
8747 return build_tree_list (parameter_declarator->default_argument, parm);
8750 /* Parse a type-parameter.
8752 type-parameter:
8753 class identifier [opt]
8754 class identifier [opt] = type-id
8755 typename identifier [opt]
8756 typename identifier [opt] = type-id
8757 template < template-parameter-list > class identifier [opt]
8758 template < template-parameter-list > class identifier [opt]
8759 = id-expression
8761 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
8762 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
8763 the declaration of the parameter. */
8765 static tree
8766 cp_parser_type_parameter (cp_parser* parser)
8768 cp_token *token;
8769 tree parameter;
8771 /* Look for a keyword to tell us what kind of parameter this is. */
8772 token = cp_parser_require (parser, CPP_KEYWORD,
8773 "`class', `typename', or `template'");
8774 if (!token)
8775 return error_mark_node;
8777 switch (token->keyword)
8779 case RID_CLASS:
8780 case RID_TYPENAME:
8782 tree identifier;
8783 tree default_argument;
8785 /* If the next token is an identifier, then it names the
8786 parameter. */
8787 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8788 identifier = cp_parser_identifier (parser);
8789 else
8790 identifier = NULL_TREE;
8792 /* Create the parameter. */
8793 parameter = finish_template_type_parm (class_type_node, identifier);
8795 /* If the next token is an `=', we have a default argument. */
8796 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8798 /* Consume the `=' token. */
8799 cp_lexer_consume_token (parser->lexer);
8800 /* Parse the default-argument. */
8801 push_deferring_access_checks (dk_no_deferred);
8802 default_argument = cp_parser_type_id (parser);
8803 pop_deferring_access_checks ();
8805 else
8806 default_argument = NULL_TREE;
8808 /* Create the combined representation of the parameter and the
8809 default argument. */
8810 parameter = build_tree_list (default_argument, parameter);
8812 break;
8814 case RID_TEMPLATE:
8816 tree parameter_list;
8817 tree identifier;
8818 tree default_argument;
8820 /* Look for the `<'. */
8821 cp_parser_require (parser, CPP_LESS, "`<'");
8822 /* Parse the template-parameter-list. */
8823 parameter_list = cp_parser_template_parameter_list (parser);
8824 /* Look for the `>'. */
8825 cp_parser_require (parser, CPP_GREATER, "`>'");
8826 /* Look for the `class' keyword. */
8827 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8828 /* If the next token is an `=', then there is a
8829 default-argument. If the next token is a `>', we are at
8830 the end of the parameter-list. If the next token is a `,',
8831 then we are at the end of this parameter. */
8832 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8833 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8834 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8836 identifier = cp_parser_identifier (parser);
8837 /* Treat invalid names as if the parameter were nameless. */
8838 if (identifier == error_mark_node)
8839 identifier = NULL_TREE;
8841 else
8842 identifier = NULL_TREE;
8844 /* Create the template parameter. */
8845 parameter = finish_template_template_parm (class_type_node,
8846 identifier);
8848 /* If the next token is an `=', then there is a
8849 default-argument. */
8850 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8852 bool is_template;
8854 /* Consume the `='. */
8855 cp_lexer_consume_token (parser->lexer);
8856 /* Parse the id-expression. */
8857 push_deferring_access_checks (dk_no_deferred);
8858 default_argument
8859 = cp_parser_id_expression (parser,
8860 /*template_keyword_p=*/false,
8861 /*check_dependency_p=*/true,
8862 /*template_p=*/&is_template,
8863 /*declarator_p=*/false,
8864 /*optional_p=*/false);
8865 if (TREE_CODE (default_argument) == TYPE_DECL)
8866 /* If the id-expression was a template-id that refers to
8867 a template-class, we already have the declaration here,
8868 so no further lookup is needed. */
8870 else
8871 /* Look up the name. */
8872 default_argument
8873 = cp_parser_lookup_name (parser, default_argument,
8874 none_type,
8875 /*is_template=*/is_template,
8876 /*is_namespace=*/false,
8877 /*check_dependency=*/true,
8878 /*ambiguous_decls=*/NULL);
8879 /* See if the default argument is valid. */
8880 default_argument
8881 = check_template_template_default_arg (default_argument);
8882 pop_deferring_access_checks ();
8884 else
8885 default_argument = NULL_TREE;
8887 /* Create the combined representation of the parameter and the
8888 default argument. */
8889 parameter = build_tree_list (default_argument, parameter);
8891 break;
8893 default:
8894 gcc_unreachable ();
8895 break;
8898 return parameter;
8901 /* Parse a template-id.
8903 template-id:
8904 template-name < template-argument-list [opt] >
8906 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8907 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8908 returned. Otherwise, if the template-name names a function, or set
8909 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
8910 names a class, returns a TYPE_DECL for the specialization.
8912 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8913 uninstantiated templates. */
8915 static tree
8916 cp_parser_template_id (cp_parser *parser,
8917 bool template_keyword_p,
8918 bool check_dependency_p,
8919 bool is_declaration)
8921 int i;
8922 tree template;
8923 tree arguments;
8924 tree template_id;
8925 cp_token_position start_of_id = 0;
8926 deferred_access_check *chk;
8927 VEC (deferred_access_check,gc) *access_check;
8928 cp_token *next_token, *next_token_2;
8929 bool is_identifier;
8931 /* If the next token corresponds to a template-id, there is no need
8932 to reparse it. */
8933 next_token = cp_lexer_peek_token (parser->lexer);
8934 if (next_token->type == CPP_TEMPLATE_ID)
8936 struct tree_check *check_value;
8938 /* Get the stored value. */
8939 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
8940 /* Perform any access checks that were deferred. */
8941 access_check = check_value->checks;
8942 if (access_check)
8944 for (i = 0 ;
8945 VEC_iterate (deferred_access_check, access_check, i, chk) ;
8946 ++i)
8948 perform_or_defer_access_check (chk->binfo,
8949 chk->decl,
8950 chk->diag_decl);
8953 /* Return the stored value. */
8954 return check_value->value;
8957 /* Avoid performing name lookup if there is no possibility of
8958 finding a template-id. */
8959 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8960 || (next_token->type == CPP_NAME
8961 && !cp_parser_nth_token_starts_template_argument_list_p
8962 (parser, 2)))
8964 cp_parser_error (parser, "expected template-id");
8965 return error_mark_node;
8968 /* Remember where the template-id starts. */
8969 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
8970 start_of_id = cp_lexer_token_position (parser->lexer, false);
8972 push_deferring_access_checks (dk_deferred);
8974 /* Parse the template-name. */
8975 is_identifier = false;
8976 template = cp_parser_template_name (parser, template_keyword_p,
8977 check_dependency_p,
8978 is_declaration,
8979 &is_identifier);
8980 if (template == error_mark_node || is_identifier)
8982 pop_deferring_access_checks ();
8983 return template;
8986 /* If we find the sequence `[:' after a template-name, it's probably
8987 a digraph-typo for `< ::'. Substitute the tokens and check if we can
8988 parse correctly the argument list. */
8989 next_token = cp_lexer_peek_token (parser->lexer);
8990 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8991 if (next_token->type == CPP_OPEN_SQUARE
8992 && next_token->flags & DIGRAPH
8993 && next_token_2->type == CPP_COLON
8994 && !(next_token_2->flags & PREV_WHITE))
8996 cp_parser_parse_tentatively (parser);
8997 /* Change `:' into `::'. */
8998 next_token_2->type = CPP_SCOPE;
8999 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
9000 CPP_LESS. */
9001 cp_lexer_consume_token (parser->lexer);
9002 /* Parse the arguments. */
9003 arguments = cp_parser_enclosed_template_argument_list (parser);
9004 if (!cp_parser_parse_definitely (parser))
9006 /* If we couldn't parse an argument list, then we revert our changes
9007 and return simply an error. Maybe this is not a template-id
9008 after all. */
9009 next_token_2->type = CPP_COLON;
9010 cp_parser_error (parser, "expected %<<%>");
9011 pop_deferring_access_checks ();
9012 return error_mark_node;
9014 /* Otherwise, emit an error about the invalid digraph, but continue
9015 parsing because we got our argument list. */
9016 pedwarn ("%<<::%> cannot begin a template-argument list");
9017 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
9018 "between %<<%> and %<::%>");
9019 if (!flag_permissive)
9021 static bool hint;
9022 if (!hint)
9024 inform ("(if you use -fpermissive G++ will accept your code)");
9025 hint = true;
9029 else
9031 /* Look for the `<' that starts the template-argument-list. */
9032 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
9034 pop_deferring_access_checks ();
9035 return error_mark_node;
9037 /* Parse the arguments. */
9038 arguments = cp_parser_enclosed_template_argument_list (parser);
9041 /* Build a representation of the specialization. */
9042 if (TREE_CODE (template) == IDENTIFIER_NODE)
9043 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
9044 else if (DECL_CLASS_TEMPLATE_P (template)
9045 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
9047 bool entering_scope;
9048 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
9049 template (rather than some instantiation thereof) only if
9050 is not nested within some other construct. For example, in
9051 "template <typename T> void f(T) { A<T>::", A<T> is just an
9052 instantiation of A. */
9053 entering_scope = (template_parm_scope_p ()
9054 && cp_lexer_next_token_is (parser->lexer,
9055 CPP_SCOPE));
9056 template_id
9057 = finish_template_type (template, arguments, entering_scope);
9059 else
9061 /* If it's not a class-template or a template-template, it should be
9062 a function-template. */
9063 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
9064 || TREE_CODE (template) == OVERLOAD
9065 || BASELINK_P (template)));
9067 template_id = lookup_template_function (template, arguments);
9070 /* If parsing tentatively, replace the sequence of tokens that makes
9071 up the template-id with a CPP_TEMPLATE_ID token. That way,
9072 should we re-parse the token stream, we will not have to repeat
9073 the effort required to do the parse, nor will we issue duplicate
9074 error messages about problems during instantiation of the
9075 template. */
9076 if (start_of_id)
9078 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
9080 /* Reset the contents of the START_OF_ID token. */
9081 token->type = CPP_TEMPLATE_ID;
9082 /* Retrieve any deferred checks. Do not pop this access checks yet
9083 so the memory will not be reclaimed during token replacing below. */
9084 token->u.tree_check_value = GGC_CNEW (struct tree_check);
9085 token->u.tree_check_value->value = template_id;
9086 token->u.tree_check_value->checks = get_deferred_access_checks ();
9087 token->keyword = RID_MAX;
9089 /* Purge all subsequent tokens. */
9090 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
9092 /* ??? Can we actually assume that, if template_id ==
9093 error_mark_node, we will have issued a diagnostic to the
9094 user, as opposed to simply marking the tentative parse as
9095 failed? */
9096 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
9097 error ("parse error in template argument list");
9100 pop_deferring_access_checks ();
9101 return template_id;
9104 /* Parse a template-name.
9106 template-name:
9107 identifier
9109 The standard should actually say:
9111 template-name:
9112 identifier
9113 operator-function-id
9115 A defect report has been filed about this issue.
9117 A conversion-function-id cannot be a template name because they cannot
9118 be part of a template-id. In fact, looking at this code:
9120 a.operator K<int>()
9122 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
9123 It is impossible to call a templated conversion-function-id with an
9124 explicit argument list, since the only allowed template parameter is
9125 the type to which it is converting.
9127 If TEMPLATE_KEYWORD_P is true, then we have just seen the
9128 `template' keyword, in a construction like:
9130 T::template f<3>()
9132 In that case `f' is taken to be a template-name, even though there
9133 is no way of knowing for sure.
9135 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
9136 name refers to a set of overloaded functions, at least one of which
9137 is a template, or an IDENTIFIER_NODE with the name of the template,
9138 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
9139 names are looked up inside uninstantiated templates. */
9141 static tree
9142 cp_parser_template_name (cp_parser* parser,
9143 bool template_keyword_p,
9144 bool check_dependency_p,
9145 bool is_declaration,
9146 bool *is_identifier)
9148 tree identifier;
9149 tree decl;
9150 tree fns;
9152 /* If the next token is `operator', then we have either an
9153 operator-function-id or a conversion-function-id. */
9154 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
9156 /* We don't know whether we're looking at an
9157 operator-function-id or a conversion-function-id. */
9158 cp_parser_parse_tentatively (parser);
9159 /* Try an operator-function-id. */
9160 identifier = cp_parser_operator_function_id (parser);
9161 /* If that didn't work, try a conversion-function-id. */
9162 if (!cp_parser_parse_definitely (parser))
9164 cp_parser_error (parser, "expected template-name");
9165 return error_mark_node;
9168 /* Look for the identifier. */
9169 else
9170 identifier = cp_parser_identifier (parser);
9172 /* If we didn't find an identifier, we don't have a template-id. */
9173 if (identifier == error_mark_node)
9174 return error_mark_node;
9176 /* If the name immediately followed the `template' keyword, then it
9177 is a template-name. However, if the next token is not `<', then
9178 we do not treat it as a template-name, since it is not being used
9179 as part of a template-id. This enables us to handle constructs
9180 like:
9182 template <typename T> struct S { S(); };
9183 template <typename T> S<T>::S();
9185 correctly. We would treat `S' as a template -- if it were `S<T>'
9186 -- but we do not if there is no `<'. */
9188 if (processing_template_decl
9189 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
9191 /* In a declaration, in a dependent context, we pretend that the
9192 "template" keyword was present in order to improve error
9193 recovery. For example, given:
9195 template <typename T> void f(T::X<int>);
9197 we want to treat "X<int>" as a template-id. */
9198 if (is_declaration
9199 && !template_keyword_p
9200 && parser->scope && TYPE_P (parser->scope)
9201 && check_dependency_p
9202 && dependent_type_p (parser->scope)
9203 /* Do not do this for dtors (or ctors), since they never
9204 need the template keyword before their name. */
9205 && !constructor_name_p (identifier, parser->scope))
9207 cp_token_position start = 0;
9209 /* Explain what went wrong. */
9210 error ("non-template %qD used as template", identifier);
9211 inform ("use %<%T::template %D%> to indicate that it is a template",
9212 parser->scope, identifier);
9213 /* If parsing tentatively, find the location of the "<" token. */
9214 if (cp_parser_simulate_error (parser))
9215 start = cp_lexer_token_position (parser->lexer, true);
9216 /* Parse the template arguments so that we can issue error
9217 messages about them. */
9218 cp_lexer_consume_token (parser->lexer);
9219 cp_parser_enclosed_template_argument_list (parser);
9220 /* Skip tokens until we find a good place from which to
9221 continue parsing. */
9222 cp_parser_skip_to_closing_parenthesis (parser,
9223 /*recovering=*/true,
9224 /*or_comma=*/true,
9225 /*consume_paren=*/false);
9226 /* If parsing tentatively, permanently remove the
9227 template argument list. That will prevent duplicate
9228 error messages from being issued about the missing
9229 "template" keyword. */
9230 if (start)
9231 cp_lexer_purge_tokens_after (parser->lexer, start);
9232 if (is_identifier)
9233 *is_identifier = true;
9234 return identifier;
9237 /* If the "template" keyword is present, then there is generally
9238 no point in doing name-lookup, so we just return IDENTIFIER.
9239 But, if the qualifying scope is non-dependent then we can
9240 (and must) do name-lookup normally. */
9241 if (template_keyword_p
9242 && (!parser->scope
9243 || (TYPE_P (parser->scope)
9244 && dependent_type_p (parser->scope))))
9245 return identifier;
9248 /* Look up the name. */
9249 decl = cp_parser_lookup_name (parser, identifier,
9250 none_type,
9251 /*is_template=*/false,
9252 /*is_namespace=*/false,
9253 check_dependency_p,
9254 /*ambiguous_decls=*/NULL);
9255 decl = maybe_get_template_decl_from_type_decl (decl);
9257 /* If DECL is a template, then the name was a template-name. */
9258 if (TREE_CODE (decl) == TEMPLATE_DECL)
9260 else
9262 tree fn = NULL_TREE;
9264 /* The standard does not explicitly indicate whether a name that
9265 names a set of overloaded declarations, some of which are
9266 templates, is a template-name. However, such a name should
9267 be a template-name; otherwise, there is no way to form a
9268 template-id for the overloaded templates. */
9269 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
9270 if (TREE_CODE (fns) == OVERLOAD)
9271 for (fn = fns; fn; fn = OVL_NEXT (fn))
9272 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
9273 break;
9275 if (!fn)
9277 /* The name does not name a template. */
9278 cp_parser_error (parser, "expected template-name");
9279 return error_mark_node;
9283 /* If DECL is dependent, and refers to a function, then just return
9284 its name; we will look it up again during template instantiation. */
9285 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
9287 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
9288 if (TYPE_P (scope) && dependent_type_p (scope))
9289 return identifier;
9292 return decl;
9295 /* Parse a template-argument-list.
9297 template-argument-list:
9298 template-argument
9299 template-argument-list , template-argument
9301 Returns a TREE_VEC containing the arguments. */
9303 static tree
9304 cp_parser_template_argument_list (cp_parser* parser)
9306 tree fixed_args[10];
9307 unsigned n_args = 0;
9308 unsigned alloced = 10;
9309 tree *arg_ary = fixed_args;
9310 tree vec;
9311 bool saved_in_template_argument_list_p;
9312 bool saved_ice_p;
9313 bool saved_non_ice_p;
9315 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
9316 parser->in_template_argument_list_p = true;
9317 /* Even if the template-id appears in an integral
9318 constant-expression, the contents of the argument list do
9319 not. */
9320 saved_ice_p = parser->integral_constant_expression_p;
9321 parser->integral_constant_expression_p = false;
9322 saved_non_ice_p = parser->non_integral_constant_expression_p;
9323 parser->non_integral_constant_expression_p = false;
9324 /* Parse the arguments. */
9327 tree argument;
9329 if (n_args)
9330 /* Consume the comma. */
9331 cp_lexer_consume_token (parser->lexer);
9333 /* Parse the template-argument. */
9334 argument = cp_parser_template_argument (parser);
9335 if (n_args == alloced)
9337 alloced *= 2;
9339 if (arg_ary == fixed_args)
9341 arg_ary = XNEWVEC (tree, alloced);
9342 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
9344 else
9345 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
9347 arg_ary[n_args++] = argument;
9349 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
9351 vec = make_tree_vec (n_args);
9353 while (n_args--)
9354 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
9356 if (arg_ary != fixed_args)
9357 free (arg_ary);
9358 parser->non_integral_constant_expression_p = saved_non_ice_p;
9359 parser->integral_constant_expression_p = saved_ice_p;
9360 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
9361 return vec;
9364 /* Parse a template-argument.
9366 template-argument:
9367 assignment-expression
9368 type-id
9369 id-expression
9371 The representation is that of an assignment-expression, type-id, or
9372 id-expression -- except that the qualified id-expression is
9373 evaluated, so that the value returned is either a DECL or an
9374 OVERLOAD.
9376 Although the standard says "assignment-expression", it forbids
9377 throw-expressions or assignments in the template argument.
9378 Therefore, we use "conditional-expression" instead. */
9380 static tree
9381 cp_parser_template_argument (cp_parser* parser)
9383 tree argument;
9384 bool template_p;
9385 bool address_p;
9386 bool maybe_type_id = false;
9387 cp_token *token;
9388 cp_id_kind idk;
9390 /* There's really no way to know what we're looking at, so we just
9391 try each alternative in order.
9393 [temp.arg]
9395 In a template-argument, an ambiguity between a type-id and an
9396 expression is resolved to a type-id, regardless of the form of
9397 the corresponding template-parameter.
9399 Therefore, we try a type-id first. */
9400 cp_parser_parse_tentatively (parser);
9401 argument = cp_parser_type_id (parser);
9402 /* If there was no error parsing the type-id but the next token is a '>>',
9403 we probably found a typo for '> >'. But there are type-id which are
9404 also valid expressions. For instance:
9406 struct X { int operator >> (int); };
9407 template <int V> struct Foo {};
9408 Foo<X () >> 5> r;
9410 Here 'X()' is a valid type-id of a function type, but the user just
9411 wanted to write the expression "X() >> 5". Thus, we remember that we
9412 found a valid type-id, but we still try to parse the argument as an
9413 expression to see what happens. */
9414 if (!cp_parser_error_occurred (parser)
9415 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
9417 maybe_type_id = true;
9418 cp_parser_abort_tentative_parse (parser);
9420 else
9422 /* If the next token isn't a `,' or a `>', then this argument wasn't
9423 really finished. This means that the argument is not a valid
9424 type-id. */
9425 if (!cp_parser_next_token_ends_template_argument_p (parser))
9426 cp_parser_error (parser, "expected template-argument");
9427 /* If that worked, we're done. */
9428 if (cp_parser_parse_definitely (parser))
9429 return argument;
9431 /* We're still not sure what the argument will be. */
9432 cp_parser_parse_tentatively (parser);
9433 /* Try a template. */
9434 argument = cp_parser_id_expression (parser,
9435 /*template_keyword_p=*/false,
9436 /*check_dependency_p=*/true,
9437 &template_p,
9438 /*declarator_p=*/false,
9439 /*optional_p=*/false);
9440 /* If the next token isn't a `,' or a `>', then this argument wasn't
9441 really finished. */
9442 if (!cp_parser_next_token_ends_template_argument_p (parser))
9443 cp_parser_error (parser, "expected template-argument");
9444 if (!cp_parser_error_occurred (parser))
9446 /* Figure out what is being referred to. If the id-expression
9447 was for a class template specialization, then we will have a
9448 TYPE_DECL at this point. There is no need to do name lookup
9449 at this point in that case. */
9450 if (TREE_CODE (argument) != TYPE_DECL)
9451 argument = cp_parser_lookup_name (parser, argument,
9452 none_type,
9453 /*is_template=*/template_p,
9454 /*is_namespace=*/false,
9455 /*check_dependency=*/true,
9456 /*ambiguous_decls=*/NULL);
9457 if (TREE_CODE (argument) != TEMPLATE_DECL
9458 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
9459 cp_parser_error (parser, "expected template-name");
9461 if (cp_parser_parse_definitely (parser))
9462 return argument;
9463 /* It must be a non-type argument. There permitted cases are given
9464 in [temp.arg.nontype]:
9466 -- an integral constant-expression of integral or enumeration
9467 type; or
9469 -- the name of a non-type template-parameter; or
9471 -- the name of an object or function with external linkage...
9473 -- the address of an object or function with external linkage...
9475 -- a pointer to member... */
9476 /* Look for a non-type template parameter. */
9477 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9479 cp_parser_parse_tentatively (parser);
9480 argument = cp_parser_primary_expression (parser,
9481 /*adress_p=*/false,
9482 /*cast_p=*/false,
9483 /*template_arg_p=*/true,
9484 &idk);
9485 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
9486 || !cp_parser_next_token_ends_template_argument_p (parser))
9487 cp_parser_simulate_error (parser);
9488 if (cp_parser_parse_definitely (parser))
9489 return argument;
9492 /* If the next token is "&", the argument must be the address of an
9493 object or function with external linkage. */
9494 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
9495 if (address_p)
9496 cp_lexer_consume_token (parser->lexer);
9497 /* See if we might have an id-expression. */
9498 token = cp_lexer_peek_token (parser->lexer);
9499 if (token->type == CPP_NAME
9500 || token->keyword == RID_OPERATOR
9501 || token->type == CPP_SCOPE
9502 || token->type == CPP_TEMPLATE_ID
9503 || token->type == CPP_NESTED_NAME_SPECIFIER)
9505 cp_parser_parse_tentatively (parser);
9506 argument = cp_parser_primary_expression (parser,
9507 address_p,
9508 /*cast_p=*/false,
9509 /*template_arg_p=*/true,
9510 &idk);
9511 if (cp_parser_error_occurred (parser)
9512 || !cp_parser_next_token_ends_template_argument_p (parser))
9513 cp_parser_abort_tentative_parse (parser);
9514 else
9516 if (TREE_CODE (argument) == INDIRECT_REF)
9518 gcc_assert (REFERENCE_REF_P (argument));
9519 argument = TREE_OPERAND (argument, 0);
9522 if (TREE_CODE (argument) == VAR_DECL)
9524 /* A variable without external linkage might still be a
9525 valid constant-expression, so no error is issued here
9526 if the external-linkage check fails. */
9527 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
9528 cp_parser_simulate_error (parser);
9530 else if (is_overloaded_fn (argument))
9531 /* All overloaded functions are allowed; if the external
9532 linkage test does not pass, an error will be issued
9533 later. */
9535 else if (address_p
9536 && (TREE_CODE (argument) == OFFSET_REF
9537 || TREE_CODE (argument) == SCOPE_REF))
9538 /* A pointer-to-member. */
9540 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
9542 else
9543 cp_parser_simulate_error (parser);
9545 if (cp_parser_parse_definitely (parser))
9547 if (address_p)
9548 argument = build_x_unary_op (ADDR_EXPR, argument);
9549 return argument;
9553 /* If the argument started with "&", there are no other valid
9554 alternatives at this point. */
9555 if (address_p)
9557 cp_parser_error (parser, "invalid non-type template argument");
9558 return error_mark_node;
9561 /* If the argument wasn't successfully parsed as a type-id followed
9562 by '>>', the argument can only be a constant expression now.
9563 Otherwise, we try parsing the constant-expression tentatively,
9564 because the argument could really be a type-id. */
9565 if (maybe_type_id)
9566 cp_parser_parse_tentatively (parser);
9567 argument = cp_parser_constant_expression (parser,
9568 /*allow_non_constant_p=*/false,
9569 /*non_constant_p=*/NULL);
9570 argument = fold_non_dependent_expr (argument);
9571 if (!maybe_type_id)
9572 return argument;
9573 if (!cp_parser_next_token_ends_template_argument_p (parser))
9574 cp_parser_error (parser, "expected template-argument");
9575 if (cp_parser_parse_definitely (parser))
9576 return argument;
9577 /* We did our best to parse the argument as a non type-id, but that
9578 was the only alternative that matched (albeit with a '>' after
9579 it). We can assume it's just a typo from the user, and a
9580 diagnostic will then be issued. */
9581 return cp_parser_type_id (parser);
9584 /* Parse an explicit-instantiation.
9586 explicit-instantiation:
9587 template declaration
9589 Although the standard says `declaration', what it really means is:
9591 explicit-instantiation:
9592 template decl-specifier-seq [opt] declarator [opt] ;
9594 Things like `template int S<int>::i = 5, int S<double>::j;' are not
9595 supposed to be allowed. A defect report has been filed about this
9596 issue.
9598 GNU Extension:
9600 explicit-instantiation:
9601 storage-class-specifier template
9602 decl-specifier-seq [opt] declarator [opt] ;
9603 function-specifier template
9604 decl-specifier-seq [opt] declarator [opt] ; */
9606 static void
9607 cp_parser_explicit_instantiation (cp_parser* parser)
9609 int declares_class_or_enum;
9610 cp_decl_specifier_seq decl_specifiers;
9611 tree extension_specifier = NULL_TREE;
9613 /* Look for an (optional) storage-class-specifier or
9614 function-specifier. */
9615 if (cp_parser_allow_gnu_extensions_p (parser))
9617 extension_specifier
9618 = cp_parser_storage_class_specifier_opt (parser);
9619 if (!extension_specifier)
9620 extension_specifier
9621 = cp_parser_function_specifier_opt (parser,
9622 /*decl_specs=*/NULL);
9625 /* Look for the `template' keyword. */
9626 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9627 /* Let the front end know that we are processing an explicit
9628 instantiation. */
9629 begin_explicit_instantiation ();
9630 /* [temp.explicit] says that we are supposed to ignore access
9631 control while processing explicit instantiation directives. */
9632 push_deferring_access_checks (dk_no_check);
9633 /* Parse a decl-specifier-seq. */
9634 cp_parser_decl_specifier_seq (parser,
9635 CP_PARSER_FLAGS_OPTIONAL,
9636 &decl_specifiers,
9637 &declares_class_or_enum);
9638 /* If there was exactly one decl-specifier, and it declared a class,
9639 and there's no declarator, then we have an explicit type
9640 instantiation. */
9641 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9643 tree type;
9645 type = check_tag_decl (&decl_specifiers);
9646 /* Turn access control back on for names used during
9647 template instantiation. */
9648 pop_deferring_access_checks ();
9649 if (type)
9650 do_type_instantiation (type, extension_specifier,
9651 /*complain=*/tf_error);
9653 else
9655 cp_declarator *declarator;
9656 tree decl;
9658 /* Parse the declarator. */
9659 declarator
9660 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9661 /*ctor_dtor_or_conv_p=*/NULL,
9662 /*parenthesized_p=*/NULL,
9663 /*member_p=*/false);
9664 if (declares_class_or_enum & 2)
9665 cp_parser_check_for_definition_in_return_type (declarator,
9666 decl_specifiers.type);
9667 if (declarator != cp_error_declarator)
9669 decl = grokdeclarator (declarator, &decl_specifiers,
9670 NORMAL, 0, &decl_specifiers.attributes);
9671 /* Turn access control back on for names used during
9672 template instantiation. */
9673 pop_deferring_access_checks ();
9674 /* Do the explicit instantiation. */
9675 do_decl_instantiation (decl, extension_specifier);
9677 else
9679 pop_deferring_access_checks ();
9680 /* Skip the body of the explicit instantiation. */
9681 cp_parser_skip_to_end_of_statement (parser);
9684 /* We're done with the instantiation. */
9685 end_explicit_instantiation ();
9687 cp_parser_consume_semicolon_at_end_of_statement (parser);
9690 /* Parse an explicit-specialization.
9692 explicit-specialization:
9693 template < > declaration
9695 Although the standard says `declaration', what it really means is:
9697 explicit-specialization:
9698 template <> decl-specifier [opt] init-declarator [opt] ;
9699 template <> function-definition
9700 template <> explicit-specialization
9701 template <> template-declaration */
9703 static void
9704 cp_parser_explicit_specialization (cp_parser* parser)
9706 bool need_lang_pop;
9707 /* Look for the `template' keyword. */
9708 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9709 /* Look for the `<'. */
9710 cp_parser_require (parser, CPP_LESS, "`<'");
9711 /* Look for the `>'. */
9712 cp_parser_require (parser, CPP_GREATER, "`>'");
9713 /* We have processed another parameter list. */
9714 ++parser->num_template_parameter_lists;
9715 /* [temp]
9717 A template ... explicit specialization ... shall not have C
9718 linkage. */
9719 if (current_lang_name == lang_name_c)
9721 error ("template specialization with C linkage");
9722 /* Give it C++ linkage to avoid confusing other parts of the
9723 front end. */
9724 push_lang_context (lang_name_cplusplus);
9725 need_lang_pop = true;
9727 else
9728 need_lang_pop = false;
9729 /* Let the front end know that we are beginning a specialization. */
9730 if (!begin_specialization ())
9732 end_specialization ();
9733 cp_parser_skip_to_end_of_block_or_statement (parser);
9734 return;
9737 /* If the next keyword is `template', we need to figure out whether
9738 or not we're looking a template-declaration. */
9739 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9741 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9742 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9743 cp_parser_template_declaration_after_export (parser,
9744 /*member_p=*/false);
9745 else
9746 cp_parser_explicit_specialization (parser);
9748 else
9749 /* Parse the dependent declaration. */
9750 cp_parser_single_declaration (parser,
9751 /*checks=*/NULL,
9752 /*member_p=*/false,
9753 /*friend_p=*/NULL);
9754 /* We're done with the specialization. */
9755 end_specialization ();
9756 /* For the erroneous case of a template with C linkage, we pushed an
9757 implicit C++ linkage scope; exit that scope now. */
9758 if (need_lang_pop)
9759 pop_lang_context ();
9760 /* We're done with this parameter list. */
9761 --parser->num_template_parameter_lists;
9764 /* Parse a type-specifier.
9766 type-specifier:
9767 simple-type-specifier
9768 class-specifier
9769 enum-specifier
9770 elaborated-type-specifier
9771 cv-qualifier
9773 GNU Extension:
9775 type-specifier:
9776 __complex__
9778 Returns a representation of the type-specifier. For a
9779 class-specifier, enum-specifier, or elaborated-type-specifier, a
9780 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9782 The parser flags FLAGS is used to control type-specifier parsing.
9784 If IS_DECLARATION is TRUE, then this type-specifier is appearing
9785 in a decl-specifier-seq.
9787 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9788 class-specifier, enum-specifier, or elaborated-type-specifier, then
9789 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
9790 if a type is declared; 2 if it is defined. Otherwise, it is set to
9791 zero.
9793 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9794 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
9795 is set to FALSE. */
9797 static tree
9798 cp_parser_type_specifier (cp_parser* parser,
9799 cp_parser_flags flags,
9800 cp_decl_specifier_seq *decl_specs,
9801 bool is_declaration,
9802 int* declares_class_or_enum,
9803 bool* is_cv_qualifier)
9805 tree type_spec = NULL_TREE;
9806 cp_token *token;
9807 enum rid keyword;
9808 cp_decl_spec ds = ds_last;
9810 /* Assume this type-specifier does not declare a new type. */
9811 if (declares_class_or_enum)
9812 *declares_class_or_enum = 0;
9813 /* And that it does not specify a cv-qualifier. */
9814 if (is_cv_qualifier)
9815 *is_cv_qualifier = false;
9816 /* Peek at the next token. */
9817 token = cp_lexer_peek_token (parser->lexer);
9819 /* If we're looking at a keyword, we can use that to guide the
9820 production we choose. */
9821 keyword = token->keyword;
9822 switch (keyword)
9824 case RID_ENUM:
9825 /* Look for the enum-specifier. */
9826 type_spec = cp_parser_enum_specifier (parser);
9827 /* If that worked, we're done. */
9828 if (type_spec)
9830 if (declares_class_or_enum)
9831 *declares_class_or_enum = 2;
9832 if (decl_specs)
9833 cp_parser_set_decl_spec_type (decl_specs,
9834 type_spec,
9835 /*user_defined_p=*/true);
9836 return type_spec;
9838 else
9839 goto elaborated_type_specifier;
9841 /* Any of these indicate either a class-specifier, or an
9842 elaborated-type-specifier. */
9843 case RID_CLASS:
9844 case RID_STRUCT:
9845 case RID_UNION:
9846 /* Parse tentatively so that we can back up if we don't find a
9847 class-specifier. */
9848 cp_parser_parse_tentatively (parser);
9849 /* Look for the class-specifier. */
9850 type_spec = cp_parser_class_specifier (parser);
9851 /* If that worked, we're done. */
9852 if (cp_parser_parse_definitely (parser))
9854 if (declares_class_or_enum)
9855 *declares_class_or_enum = 2;
9856 if (decl_specs)
9857 cp_parser_set_decl_spec_type (decl_specs,
9858 type_spec,
9859 /*user_defined_p=*/true);
9860 return type_spec;
9863 /* Fall through. */
9864 elaborated_type_specifier:
9865 /* We're declaring (not defining) a class or enum. */
9866 if (declares_class_or_enum)
9867 *declares_class_or_enum = 1;
9869 /* Fall through. */
9870 case RID_TYPENAME:
9871 /* Look for an elaborated-type-specifier. */
9872 type_spec
9873 = (cp_parser_elaborated_type_specifier
9874 (parser,
9875 decl_specs && decl_specs->specs[(int) ds_friend],
9876 is_declaration));
9877 if (decl_specs)
9878 cp_parser_set_decl_spec_type (decl_specs,
9879 type_spec,
9880 /*user_defined_p=*/true);
9881 return type_spec;
9883 case RID_CONST:
9884 ds = ds_const;
9885 if (is_cv_qualifier)
9886 *is_cv_qualifier = true;
9887 break;
9889 case RID_VOLATILE:
9890 ds = ds_volatile;
9891 if (is_cv_qualifier)
9892 *is_cv_qualifier = true;
9893 break;
9895 case RID_RESTRICT:
9896 ds = ds_restrict;
9897 if (is_cv_qualifier)
9898 *is_cv_qualifier = true;
9899 break;
9901 case RID_COMPLEX:
9902 /* The `__complex__' keyword is a GNU extension. */
9903 ds = ds_complex;
9904 break;
9906 default:
9907 break;
9910 /* Handle simple keywords. */
9911 if (ds != ds_last)
9913 if (decl_specs)
9915 ++decl_specs->specs[(int)ds];
9916 decl_specs->any_specifiers_p = true;
9918 return cp_lexer_consume_token (parser->lexer)->u.value;
9921 /* If we do not already have a type-specifier, assume we are looking
9922 at a simple-type-specifier. */
9923 type_spec = cp_parser_simple_type_specifier (parser,
9924 decl_specs,
9925 flags);
9927 /* If we didn't find a type-specifier, and a type-specifier was not
9928 optional in this context, issue an error message. */
9929 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9931 cp_parser_error (parser, "expected type specifier");
9932 return error_mark_node;
9935 return type_spec;
9938 /* Parse a simple-type-specifier.
9940 simple-type-specifier:
9941 :: [opt] nested-name-specifier [opt] type-name
9942 :: [opt] nested-name-specifier template template-id
9943 char
9944 wchar_t
9945 bool
9946 short
9948 long
9949 signed
9950 unsigned
9951 float
9952 double
9953 void
9955 GNU Extension:
9957 simple-type-specifier:
9958 __typeof__ unary-expression
9959 __typeof__ ( type-id )
9961 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
9962 appropriately updated. */
9964 static tree
9965 cp_parser_simple_type_specifier (cp_parser* parser,
9966 cp_decl_specifier_seq *decl_specs,
9967 cp_parser_flags flags)
9969 tree type = NULL_TREE;
9970 cp_token *token;
9972 /* Peek at the next token. */
9973 token = cp_lexer_peek_token (parser->lexer);
9975 /* If we're looking at a keyword, things are easy. */
9976 switch (token->keyword)
9978 case RID_CHAR:
9979 if (decl_specs)
9980 decl_specs->explicit_char_p = true;
9981 type = char_type_node;
9982 break;
9983 case RID_WCHAR:
9984 type = wchar_type_node;
9985 break;
9986 case RID_BOOL:
9987 type = boolean_type_node;
9988 break;
9989 case RID_SHORT:
9990 if (decl_specs)
9991 ++decl_specs->specs[(int) ds_short];
9992 type = short_integer_type_node;
9993 break;
9994 case RID_INT:
9995 if (decl_specs)
9996 decl_specs->explicit_int_p = true;
9997 type = integer_type_node;
9998 break;
9999 case RID_LONG:
10000 if (decl_specs)
10001 ++decl_specs->specs[(int) ds_long];
10002 type = long_integer_type_node;
10003 break;
10004 case RID_SIGNED:
10005 if (decl_specs)
10006 ++decl_specs->specs[(int) ds_signed];
10007 type = integer_type_node;
10008 break;
10009 case RID_UNSIGNED:
10010 if (decl_specs)
10011 ++decl_specs->specs[(int) ds_unsigned];
10012 type = unsigned_type_node;
10013 break;
10014 case RID_FLOAT:
10015 type = float_type_node;
10016 break;
10017 case RID_DOUBLE:
10018 type = double_type_node;
10019 break;
10020 case RID_VOID:
10021 type = void_type_node;
10022 break;
10024 case RID_TYPEOF:
10025 /* Consume the `typeof' token. */
10026 cp_lexer_consume_token (parser->lexer);
10027 /* Parse the operand to `typeof'. */
10028 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
10029 /* If it is not already a TYPE, take its type. */
10030 if (!TYPE_P (type))
10031 type = finish_typeof (type);
10033 if (decl_specs)
10034 cp_parser_set_decl_spec_type (decl_specs, type,
10035 /*user_defined_p=*/true);
10037 return type;
10039 default:
10040 break;
10043 /* If the type-specifier was for a built-in type, we're done. */
10044 if (type)
10046 tree id;
10048 /* Record the type. */
10049 if (decl_specs
10050 && (token->keyword != RID_SIGNED
10051 && token->keyword != RID_UNSIGNED
10052 && token->keyword != RID_SHORT
10053 && token->keyword != RID_LONG))
10054 cp_parser_set_decl_spec_type (decl_specs,
10055 type,
10056 /*user_defined=*/false);
10057 if (decl_specs)
10058 decl_specs->any_specifiers_p = true;
10060 /* Consume the token. */
10061 id = cp_lexer_consume_token (parser->lexer)->u.value;
10063 /* There is no valid C++ program where a non-template type is
10064 followed by a "<". That usually indicates that the user thought
10065 that the type was a template. */
10066 cp_parser_check_for_invalid_template_id (parser, type);
10068 return TYPE_NAME (type);
10071 /* The type-specifier must be a user-defined type. */
10072 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
10074 bool qualified_p;
10075 bool global_p;
10077 /* Don't gobble tokens or issue error messages if this is an
10078 optional type-specifier. */
10079 if (flags & CP_PARSER_FLAGS_OPTIONAL)
10080 cp_parser_parse_tentatively (parser);
10082 /* Look for the optional `::' operator. */
10083 global_p
10084 = (cp_parser_global_scope_opt (parser,
10085 /*current_scope_valid_p=*/false)
10086 != NULL_TREE);
10087 /* Look for the nested-name specifier. */
10088 qualified_p
10089 = (cp_parser_nested_name_specifier_opt (parser,
10090 /*typename_keyword_p=*/false,
10091 /*check_dependency_p=*/true,
10092 /*type_p=*/false,
10093 /*is_declaration=*/false)
10094 != NULL_TREE);
10095 /* If we have seen a nested-name-specifier, and the next token
10096 is `template', then we are using the template-id production. */
10097 if (parser->scope
10098 && cp_parser_optional_template_keyword (parser))
10100 /* Look for the template-id. */
10101 type = cp_parser_template_id (parser,
10102 /*template_keyword_p=*/true,
10103 /*check_dependency_p=*/true,
10104 /*is_declaration=*/false);
10105 /* If the template-id did not name a type, we are out of
10106 luck. */
10107 if (TREE_CODE (type) != TYPE_DECL)
10109 cp_parser_error (parser, "expected template-id for type");
10110 type = NULL_TREE;
10113 /* Otherwise, look for a type-name. */
10114 else
10115 type = cp_parser_type_name (parser);
10116 /* Keep track of all name-lookups performed in class scopes. */
10117 if (type
10118 && !global_p
10119 && !qualified_p
10120 && TREE_CODE (type) == TYPE_DECL
10121 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
10122 maybe_note_name_used_in_class (DECL_NAME (type), type);
10123 /* If it didn't work out, we don't have a TYPE. */
10124 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
10125 && !cp_parser_parse_definitely (parser))
10126 type = NULL_TREE;
10127 if (type && decl_specs)
10128 cp_parser_set_decl_spec_type (decl_specs, type,
10129 /*user_defined=*/true);
10132 /* If we didn't get a type-name, issue an error message. */
10133 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10135 cp_parser_error (parser, "expected type-name");
10136 return error_mark_node;
10139 /* There is no valid C++ program where a non-template type is
10140 followed by a "<". That usually indicates that the user thought
10141 that the type was a template. */
10142 if (type && type != error_mark_node)
10144 /* As a last-ditch effort, see if TYPE is an Objective-C type.
10145 If it is, then the '<'...'>' enclose protocol names rather than
10146 template arguments, and so everything is fine. */
10147 if (c_dialect_objc ()
10148 && (objc_is_id (type) || objc_is_class_name (type)))
10150 tree protos = cp_parser_objc_protocol_refs_opt (parser);
10151 tree qual_type = objc_get_protocol_qualified_type (type, protos);
10153 /* Clobber the "unqualified" type previously entered into
10154 DECL_SPECS with the new, improved protocol-qualified version. */
10155 if (decl_specs)
10156 decl_specs->type = qual_type;
10158 return qual_type;
10161 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
10164 return type;
10167 /* Parse a type-name.
10169 type-name:
10170 class-name
10171 enum-name
10172 typedef-name
10174 enum-name:
10175 identifier
10177 typedef-name:
10178 identifier
10180 Returns a TYPE_DECL for the type. */
10182 static tree
10183 cp_parser_type_name (cp_parser* parser)
10185 tree type_decl;
10186 tree identifier;
10188 /* We can't know yet whether it is a class-name or not. */
10189 cp_parser_parse_tentatively (parser);
10190 /* Try a class-name. */
10191 type_decl = cp_parser_class_name (parser,
10192 /*typename_keyword_p=*/false,
10193 /*template_keyword_p=*/false,
10194 none_type,
10195 /*check_dependency_p=*/true,
10196 /*class_head_p=*/false,
10197 /*is_declaration=*/false);
10198 /* If it's not a class-name, keep looking. */
10199 if (!cp_parser_parse_definitely (parser))
10201 /* It must be a typedef-name or an enum-name. */
10202 identifier = cp_parser_identifier (parser);
10203 if (identifier == error_mark_node)
10204 return error_mark_node;
10206 /* Look up the type-name. */
10207 type_decl = cp_parser_lookup_name_simple (parser, identifier);
10209 if (TREE_CODE (type_decl) != TYPE_DECL
10210 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
10212 /* See if this is an Objective-C type. */
10213 tree protos = cp_parser_objc_protocol_refs_opt (parser);
10214 tree type = objc_get_protocol_qualified_type (identifier, protos);
10215 if (type)
10216 type_decl = TYPE_NAME (type);
10219 /* Issue an error if we did not find a type-name. */
10220 if (TREE_CODE (type_decl) != TYPE_DECL)
10222 if (!cp_parser_simulate_error (parser))
10223 cp_parser_name_lookup_error (parser, identifier, type_decl,
10224 "is not a type");
10225 type_decl = error_mark_node;
10227 /* Remember that the name was used in the definition of the
10228 current class so that we can check later to see if the
10229 meaning would have been different after the class was
10230 entirely defined. */
10231 else if (type_decl != error_mark_node
10232 && !parser->scope)
10233 maybe_note_name_used_in_class (identifier, type_decl);
10236 return type_decl;
10240 /* Parse an elaborated-type-specifier. Note that the grammar given
10241 here incorporates the resolution to DR68.
10243 elaborated-type-specifier:
10244 class-key :: [opt] nested-name-specifier [opt] identifier
10245 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
10246 enum :: [opt] nested-name-specifier [opt] identifier
10247 typename :: [opt] nested-name-specifier identifier
10248 typename :: [opt] nested-name-specifier template [opt]
10249 template-id
10251 GNU extension:
10253 elaborated-type-specifier:
10254 class-key attributes :: [opt] nested-name-specifier [opt] identifier
10255 class-key attributes :: [opt] nested-name-specifier [opt]
10256 template [opt] template-id
10257 enum attributes :: [opt] nested-name-specifier [opt] identifier
10259 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
10260 declared `friend'. If IS_DECLARATION is TRUE, then this
10261 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
10262 something is being declared.
10264 Returns the TYPE specified. */
10266 static tree
10267 cp_parser_elaborated_type_specifier (cp_parser* parser,
10268 bool is_friend,
10269 bool is_declaration)
10271 enum tag_types tag_type;
10272 tree identifier;
10273 tree type = NULL_TREE;
10274 tree attributes = NULL_TREE;
10276 /* See if we're looking at the `enum' keyword. */
10277 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
10279 /* Consume the `enum' token. */
10280 cp_lexer_consume_token (parser->lexer);
10281 /* Remember that it's an enumeration type. */
10282 tag_type = enum_type;
10283 /* Parse the attributes. */
10284 attributes = cp_parser_attributes_opt (parser);
10286 /* Or, it might be `typename'. */
10287 else if (cp_lexer_next_token_is_keyword (parser->lexer,
10288 RID_TYPENAME))
10290 /* Consume the `typename' token. */
10291 cp_lexer_consume_token (parser->lexer);
10292 /* Remember that it's a `typename' type. */
10293 tag_type = typename_type;
10294 /* The `typename' keyword is only allowed in templates. */
10295 if (!processing_template_decl)
10296 pedwarn ("using %<typename%> outside of template");
10298 /* Otherwise it must be a class-key. */
10299 else
10301 tag_type = cp_parser_class_key (parser);
10302 if (tag_type == none_type)
10303 return error_mark_node;
10304 /* Parse the attributes. */
10305 attributes = cp_parser_attributes_opt (parser);
10308 /* Look for the `::' operator. */
10309 cp_parser_global_scope_opt (parser,
10310 /*current_scope_valid_p=*/false);
10311 /* Look for the nested-name-specifier. */
10312 if (tag_type == typename_type)
10314 if (!cp_parser_nested_name_specifier (parser,
10315 /*typename_keyword_p=*/true,
10316 /*check_dependency_p=*/true,
10317 /*type_p=*/true,
10318 is_declaration))
10319 return error_mark_node;
10321 else
10322 /* Even though `typename' is not present, the proposed resolution
10323 to Core Issue 180 says that in `class A<T>::B', `B' should be
10324 considered a type-name, even if `A<T>' is dependent. */
10325 cp_parser_nested_name_specifier_opt (parser,
10326 /*typename_keyword_p=*/true,
10327 /*check_dependency_p=*/true,
10328 /*type_p=*/true,
10329 is_declaration);
10330 /* For everything but enumeration types, consider a template-id.
10331 For an enumeration type, consider only a plain identifier. */
10332 if (tag_type != enum_type)
10334 bool template_p = false;
10335 tree decl;
10337 /* Allow the `template' keyword. */
10338 template_p = cp_parser_optional_template_keyword (parser);
10339 /* If we didn't see `template', we don't know if there's a
10340 template-id or not. */
10341 if (!template_p)
10342 cp_parser_parse_tentatively (parser);
10343 /* Parse the template-id. */
10344 decl = cp_parser_template_id (parser, template_p,
10345 /*check_dependency_p=*/true,
10346 is_declaration);
10347 /* If we didn't find a template-id, look for an ordinary
10348 identifier. */
10349 if (!template_p && !cp_parser_parse_definitely (parser))
10351 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
10352 in effect, then we must assume that, upon instantiation, the
10353 template will correspond to a class. */
10354 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
10355 && tag_type == typename_type)
10356 type = make_typename_type (parser->scope, decl,
10357 typename_type,
10358 /*complain=*/tf_error);
10359 else
10360 type = TREE_TYPE (decl);
10363 if (!type)
10365 identifier = cp_parser_identifier (parser);
10367 if (identifier == error_mark_node)
10369 parser->scope = NULL_TREE;
10370 return error_mark_node;
10373 /* For a `typename', we needn't call xref_tag. */
10374 if (tag_type == typename_type
10375 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
10376 return cp_parser_make_typename_type (parser, parser->scope,
10377 identifier);
10378 /* Look up a qualified name in the usual way. */
10379 if (parser->scope)
10381 tree decl;
10383 decl = cp_parser_lookup_name (parser, identifier,
10384 tag_type,
10385 /*is_template=*/false,
10386 /*is_namespace=*/false,
10387 /*check_dependency=*/true,
10388 /*ambiguous_decls=*/NULL);
10390 /* If we are parsing friend declaration, DECL may be a
10391 TEMPLATE_DECL tree node here. However, we need to check
10392 whether this TEMPLATE_DECL results in valid code. Consider
10393 the following example:
10395 namespace N {
10396 template <class T> class C {};
10398 class X {
10399 template <class T> friend class N::C; // #1, valid code
10401 template <class T> class Y {
10402 friend class N::C; // #2, invalid code
10405 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
10406 name lookup of `N::C'. We see that friend declaration must
10407 be template for the code to be valid. Note that
10408 processing_template_decl does not work here since it is
10409 always 1 for the above two cases. */
10411 decl = (cp_parser_maybe_treat_template_as_class
10412 (decl, /*tag_name_p=*/is_friend
10413 && parser->num_template_parameter_lists));
10415 if (TREE_CODE (decl) != TYPE_DECL)
10417 cp_parser_diagnose_invalid_type_name (parser,
10418 parser->scope,
10419 identifier);
10420 return error_mark_node;
10423 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
10425 bool allow_template = (parser->num_template_parameter_lists
10426 || DECL_SELF_REFERENCE_P (decl));
10427 type = check_elaborated_type_specifier (tag_type, decl,
10428 allow_template);
10430 if (type == error_mark_node)
10431 return error_mark_node;
10434 type = TREE_TYPE (decl);
10436 else
10438 /* An elaborated-type-specifier sometimes introduces a new type and
10439 sometimes names an existing type. Normally, the rule is that it
10440 introduces a new type only if there is not an existing type of
10441 the same name already in scope. For example, given:
10443 struct S {};
10444 void f() { struct S s; }
10446 the `struct S' in the body of `f' is the same `struct S' as in
10447 the global scope; the existing definition is used. However, if
10448 there were no global declaration, this would introduce a new
10449 local class named `S'.
10451 An exception to this rule applies to the following code:
10453 namespace N { struct S; }
10455 Here, the elaborated-type-specifier names a new type
10456 unconditionally; even if there is already an `S' in the
10457 containing scope this declaration names a new type.
10458 This exception only applies if the elaborated-type-specifier
10459 forms the complete declaration:
10461 [class.name]
10463 A declaration consisting solely of `class-key identifier ;' is
10464 either a redeclaration of the name in the current scope or a
10465 forward declaration of the identifier as a class name. It
10466 introduces the name into the current scope.
10468 We are in this situation precisely when the next token is a `;'.
10470 An exception to the exception is that a `friend' declaration does
10471 *not* name a new type; i.e., given:
10473 struct S { friend struct T; };
10475 `T' is not a new type in the scope of `S'.
10477 Also, `new struct S' or `sizeof (struct S)' never results in the
10478 definition of a new type; a new type can only be declared in a
10479 declaration context. */
10481 tag_scope ts;
10482 bool template_p;
10484 if (is_friend)
10485 /* Friends have special name lookup rules. */
10486 ts = ts_within_enclosing_non_class;
10487 else if (is_declaration
10488 && cp_lexer_next_token_is (parser->lexer,
10489 CPP_SEMICOLON))
10490 /* This is a `class-key identifier ;' */
10491 ts = ts_current;
10492 else
10493 ts = ts_global;
10495 template_p =
10496 (parser->num_template_parameter_lists
10497 && (cp_parser_next_token_starts_class_definition_p (parser)
10498 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
10499 /* An unqualified name was used to reference this type, so
10500 there were no qualifying templates. */
10501 if (!cp_parser_check_template_parameters (parser,
10502 /*num_templates=*/0))
10503 return error_mark_node;
10504 type = xref_tag (tag_type, identifier, ts, template_p);
10508 if (type == error_mark_node)
10509 return error_mark_node;
10511 /* Allow attributes on forward declarations of classes. */
10512 if (attributes)
10514 if (TREE_CODE (type) == TYPENAME_TYPE)
10515 warning (OPT_Wattributes,
10516 "attributes ignored on uninstantiated type");
10517 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
10518 && ! processing_explicit_instantiation)
10519 warning (OPT_Wattributes,
10520 "attributes ignored on template instantiation");
10521 else if (is_declaration && cp_parser_declares_only_class_p (parser))
10522 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
10523 else
10524 warning (OPT_Wattributes,
10525 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
10528 if (tag_type != enum_type)
10529 cp_parser_check_class_key (tag_type, type);
10531 /* A "<" cannot follow an elaborated type specifier. If that
10532 happens, the user was probably trying to form a template-id. */
10533 cp_parser_check_for_invalid_template_id (parser, type);
10535 return type;
10538 /* Parse an enum-specifier.
10540 enum-specifier:
10541 enum identifier [opt] { enumerator-list [opt] }
10543 GNU Extensions:
10544 enum attributes[opt] identifier [opt] { enumerator-list [opt] }
10545 attributes[opt]
10547 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
10548 if the token stream isn't an enum-specifier after all. */
10550 static tree
10551 cp_parser_enum_specifier (cp_parser* parser)
10553 tree identifier;
10554 tree type;
10555 tree attributes;
10557 /* Parse tentatively so that we can back up if we don't find a
10558 enum-specifier. */
10559 cp_parser_parse_tentatively (parser);
10561 /* Caller guarantees that the current token is 'enum', an identifier
10562 possibly follows, and the token after that is an opening brace.
10563 If we don't have an identifier, fabricate an anonymous name for
10564 the enumeration being defined. */
10565 cp_lexer_consume_token (parser->lexer);
10567 attributes = cp_parser_attributes_opt (parser);
10569 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10570 identifier = cp_parser_identifier (parser);
10571 else
10572 identifier = make_anon_name ();
10574 /* Look for the `{' but don't consume it yet. */
10575 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10576 cp_parser_simulate_error (parser);
10578 if (!cp_parser_parse_definitely (parser))
10579 return NULL_TREE;
10581 /* Issue an error message if type-definitions are forbidden here. */
10582 if (!cp_parser_check_type_definition (parser))
10583 type = error_mark_node;
10584 else
10585 /* Create the new type. We do this before consuming the opening
10586 brace so the enum will be recorded as being on the line of its
10587 tag (or the 'enum' keyword, if there is no tag). */
10588 type = start_enum (identifier);
10590 /* Consume the opening brace. */
10591 cp_lexer_consume_token (parser->lexer);
10593 if (type == error_mark_node)
10595 cp_parser_skip_to_end_of_block_or_statement (parser);
10596 return error_mark_node;
10599 /* If the next token is not '}', then there are some enumerators. */
10600 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
10601 cp_parser_enumerator_list (parser, type);
10603 /* Consume the final '}'. */
10604 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10606 /* Look for trailing attributes to apply to this enumeration, and
10607 apply them if appropriate. */
10608 if (cp_parser_allow_gnu_extensions_p (parser))
10610 tree trailing_attr = cp_parser_attributes_opt (parser);
10611 cplus_decl_attributes (&type,
10612 trailing_attr,
10613 (int) ATTR_FLAG_TYPE_IN_PLACE);
10616 /* Finish up the enumeration. */
10617 finish_enum (type);
10619 return type;
10622 /* Parse an enumerator-list. The enumerators all have the indicated
10623 TYPE.
10625 enumerator-list:
10626 enumerator-definition
10627 enumerator-list , enumerator-definition */
10629 static void
10630 cp_parser_enumerator_list (cp_parser* parser, tree type)
10632 while (true)
10634 /* Parse an enumerator-definition. */
10635 cp_parser_enumerator_definition (parser, type);
10637 /* If the next token is not a ',', we've reached the end of
10638 the list. */
10639 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10640 break;
10641 /* Otherwise, consume the `,' and keep going. */
10642 cp_lexer_consume_token (parser->lexer);
10643 /* If the next token is a `}', there is a trailing comma. */
10644 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10646 if (pedantic && !in_system_header)
10647 pedwarn ("comma at end of enumerator list");
10648 break;
10653 /* Parse an enumerator-definition. The enumerator has the indicated
10654 TYPE.
10656 enumerator-definition:
10657 enumerator
10658 enumerator = constant-expression
10660 enumerator:
10661 identifier */
10663 static void
10664 cp_parser_enumerator_definition (cp_parser* parser, tree type)
10666 tree identifier;
10667 tree value;
10669 /* Look for the identifier. */
10670 identifier = cp_parser_identifier (parser);
10671 if (identifier == error_mark_node)
10672 return;
10674 /* If the next token is an '=', then there is an explicit value. */
10675 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10677 /* Consume the `=' token. */
10678 cp_lexer_consume_token (parser->lexer);
10679 /* Parse the value. */
10680 value = cp_parser_constant_expression (parser,
10681 /*allow_non_constant_p=*/false,
10682 NULL);
10684 else
10685 value = NULL_TREE;
10687 /* Create the enumerator. */
10688 build_enumerator (identifier, value, type);
10691 /* Parse a namespace-name.
10693 namespace-name:
10694 original-namespace-name
10695 namespace-alias
10697 Returns the NAMESPACE_DECL for the namespace. */
10699 static tree
10700 cp_parser_namespace_name (cp_parser* parser)
10702 tree identifier;
10703 tree namespace_decl;
10705 /* Get the name of the namespace. */
10706 identifier = cp_parser_identifier (parser);
10707 if (identifier == error_mark_node)
10708 return error_mark_node;
10710 /* Look up the identifier in the currently active scope. Look only
10711 for namespaces, due to:
10713 [basic.lookup.udir]
10715 When looking up a namespace-name in a using-directive or alias
10716 definition, only namespace names are considered.
10718 And:
10720 [basic.lookup.qual]
10722 During the lookup of a name preceding the :: scope resolution
10723 operator, object, function, and enumerator names are ignored.
10725 (Note that cp_parser_class_or_namespace_name only calls this
10726 function if the token after the name is the scope resolution
10727 operator.) */
10728 namespace_decl = cp_parser_lookup_name (parser, identifier,
10729 none_type,
10730 /*is_template=*/false,
10731 /*is_namespace=*/true,
10732 /*check_dependency=*/true,
10733 /*ambiguous_decls=*/NULL);
10734 /* If it's not a namespace, issue an error. */
10735 if (namespace_decl == error_mark_node
10736 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10738 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
10739 error ("%qD is not a namespace-name", identifier);
10740 cp_parser_error (parser, "expected namespace-name");
10741 namespace_decl = error_mark_node;
10744 return namespace_decl;
10747 /* Parse a namespace-definition.
10749 namespace-definition:
10750 named-namespace-definition
10751 unnamed-namespace-definition
10753 named-namespace-definition:
10754 original-namespace-definition
10755 extension-namespace-definition
10757 original-namespace-definition:
10758 namespace identifier { namespace-body }
10760 extension-namespace-definition:
10761 namespace original-namespace-name { namespace-body }
10763 unnamed-namespace-definition:
10764 namespace { namespace-body } */
10766 static void
10767 cp_parser_namespace_definition (cp_parser* parser)
10769 tree identifier, attribs;
10771 /* Look for the `namespace' keyword. */
10772 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10774 /* Get the name of the namespace. We do not attempt to distinguish
10775 between an original-namespace-definition and an
10776 extension-namespace-definition at this point. The semantic
10777 analysis routines are responsible for that. */
10778 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10779 identifier = cp_parser_identifier (parser);
10780 else
10781 identifier = NULL_TREE;
10783 /* Parse any specified attributes. */
10784 attribs = cp_parser_attributes_opt (parser);
10786 /* Look for the `{' to start the namespace. */
10787 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10788 /* Start the namespace. */
10789 push_namespace_with_attribs (identifier, attribs);
10790 /* Parse the body of the namespace. */
10791 cp_parser_namespace_body (parser);
10792 /* Finish the namespace. */
10793 pop_namespace ();
10794 /* Look for the final `}'. */
10795 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10798 /* Parse a namespace-body.
10800 namespace-body:
10801 declaration-seq [opt] */
10803 static void
10804 cp_parser_namespace_body (cp_parser* parser)
10806 cp_parser_declaration_seq_opt (parser);
10809 /* Parse a namespace-alias-definition.
10811 namespace-alias-definition:
10812 namespace identifier = qualified-namespace-specifier ; */
10814 static void
10815 cp_parser_namespace_alias_definition (cp_parser* parser)
10817 tree identifier;
10818 tree namespace_specifier;
10820 /* Look for the `namespace' keyword. */
10821 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10822 /* Look for the identifier. */
10823 identifier = cp_parser_identifier (parser);
10824 if (identifier == error_mark_node)
10825 return;
10826 /* Look for the `=' token. */
10827 cp_parser_require (parser, CPP_EQ, "`='");
10828 /* Look for the qualified-namespace-specifier. */
10829 namespace_specifier
10830 = cp_parser_qualified_namespace_specifier (parser);
10831 /* Look for the `;' token. */
10832 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10834 /* Register the alias in the symbol table. */
10835 do_namespace_alias (identifier, namespace_specifier);
10838 /* Parse a qualified-namespace-specifier.
10840 qualified-namespace-specifier:
10841 :: [opt] nested-name-specifier [opt] namespace-name
10843 Returns a NAMESPACE_DECL corresponding to the specified
10844 namespace. */
10846 static tree
10847 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10849 /* Look for the optional `::'. */
10850 cp_parser_global_scope_opt (parser,
10851 /*current_scope_valid_p=*/false);
10853 /* Look for the optional nested-name-specifier. */
10854 cp_parser_nested_name_specifier_opt (parser,
10855 /*typename_keyword_p=*/false,
10856 /*check_dependency_p=*/true,
10857 /*type_p=*/false,
10858 /*is_declaration=*/true);
10860 return cp_parser_namespace_name (parser);
10863 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
10864 access declaration.
10866 using-declaration:
10867 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10868 using :: unqualified-id ;
10870 access-declaration:
10871 qualified-id ;
10875 static bool
10876 cp_parser_using_declaration (cp_parser* parser,
10877 bool access_declaration_p)
10879 cp_token *token;
10880 bool typename_p = false;
10881 bool global_scope_p;
10882 tree decl;
10883 tree identifier;
10884 tree qscope;
10886 if (access_declaration_p)
10887 cp_parser_parse_tentatively (parser);
10888 else
10890 /* Look for the `using' keyword. */
10891 cp_parser_require_keyword (parser, RID_USING, "`using'");
10893 /* Peek at the next token. */
10894 token = cp_lexer_peek_token (parser->lexer);
10895 /* See if it's `typename'. */
10896 if (token->keyword == RID_TYPENAME)
10898 /* Remember that we've seen it. */
10899 typename_p = true;
10900 /* Consume the `typename' token. */
10901 cp_lexer_consume_token (parser->lexer);
10905 /* Look for the optional global scope qualification. */
10906 global_scope_p
10907 = (cp_parser_global_scope_opt (parser,
10908 /*current_scope_valid_p=*/false)
10909 != NULL_TREE);
10911 /* If we saw `typename', or didn't see `::', then there must be a
10912 nested-name-specifier present. */
10913 if (typename_p || !global_scope_p)
10914 qscope = cp_parser_nested_name_specifier (parser, typename_p,
10915 /*check_dependency_p=*/true,
10916 /*type_p=*/false,
10917 /*is_declaration=*/true);
10918 /* Otherwise, we could be in either of the two productions. In that
10919 case, treat the nested-name-specifier as optional. */
10920 else
10921 qscope = cp_parser_nested_name_specifier_opt (parser,
10922 /*typename_keyword_p=*/false,
10923 /*check_dependency_p=*/true,
10924 /*type_p=*/false,
10925 /*is_declaration=*/true);
10926 if (!qscope)
10927 qscope = global_namespace;
10929 if (access_declaration_p && cp_parser_error_occurred (parser))
10930 /* Something has already gone wrong; there's no need to parse
10931 further. Since an error has occurred, the return value of
10932 cp_parser_parse_definitely will be false, as required. */
10933 return cp_parser_parse_definitely (parser);
10935 /* Parse the unqualified-id. */
10936 identifier = cp_parser_unqualified_id (parser,
10937 /*template_keyword_p=*/false,
10938 /*check_dependency_p=*/true,
10939 /*declarator_p=*/true,
10940 /*optional_p=*/false);
10942 if (access_declaration_p)
10944 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10945 cp_parser_simulate_error (parser);
10946 if (!cp_parser_parse_definitely (parser))
10947 return false;
10950 /* The function we call to handle a using-declaration is different
10951 depending on what scope we are in. */
10952 if (qscope == error_mark_node || identifier == error_mark_node)
10954 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10955 && TREE_CODE (identifier) != BIT_NOT_EXPR)
10956 /* [namespace.udecl]
10958 A using declaration shall not name a template-id. */
10959 error ("a template-id may not appear in a using-declaration");
10960 else
10962 if (at_class_scope_p ())
10964 /* Create the USING_DECL. */
10965 decl = do_class_using_decl (parser->scope, identifier);
10966 /* Add it to the list of members in this class. */
10967 finish_member_declaration (decl);
10969 else
10971 decl = cp_parser_lookup_name_simple (parser, identifier);
10972 if (decl == error_mark_node)
10973 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10974 else if (!at_namespace_scope_p ())
10975 do_local_using_decl (decl, qscope, identifier);
10976 else
10977 do_toplevel_using_decl (decl, qscope, identifier);
10981 /* Look for the final `;'. */
10982 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10984 return true;
10987 /* Parse a using-directive.
10989 using-directive:
10990 using namespace :: [opt] nested-name-specifier [opt]
10991 namespace-name ; */
10993 static void
10994 cp_parser_using_directive (cp_parser* parser)
10996 tree namespace_decl;
10997 tree attribs;
10999 /* Look for the `using' keyword. */
11000 cp_parser_require_keyword (parser, RID_USING, "`using'");
11001 /* And the `namespace' keyword. */
11002 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11003 /* Look for the optional `::' operator. */
11004 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
11005 /* And the optional nested-name-specifier. */
11006 cp_parser_nested_name_specifier_opt (parser,
11007 /*typename_keyword_p=*/false,
11008 /*check_dependency_p=*/true,
11009 /*type_p=*/false,
11010 /*is_declaration=*/true);
11011 /* Get the namespace being used. */
11012 namespace_decl = cp_parser_namespace_name (parser);
11013 /* And any specified attributes. */
11014 attribs = cp_parser_attributes_opt (parser);
11015 /* Update the symbol table. */
11016 parse_using_directive (namespace_decl, attribs);
11017 /* Look for the final `;'. */
11018 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11021 /* Parse an asm-definition.
11023 asm-definition:
11024 asm ( string-literal ) ;
11026 GNU Extension:
11028 asm-definition:
11029 asm volatile [opt] ( string-literal ) ;
11030 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
11031 asm volatile [opt] ( string-literal : asm-operand-list [opt]
11032 : asm-operand-list [opt] ) ;
11033 asm volatile [opt] ( string-literal : asm-operand-list [opt]
11034 : asm-operand-list [opt]
11035 : asm-operand-list [opt] ) ; */
11037 static void
11038 cp_parser_asm_definition (cp_parser* parser)
11040 tree string;
11041 tree outputs = NULL_TREE;
11042 tree inputs = NULL_TREE;
11043 tree clobbers = NULL_TREE;
11044 tree asm_stmt;
11045 bool volatile_p = false;
11046 bool extended_p = false;
11048 /* Look for the `asm' keyword. */
11049 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
11050 /* See if the next token is `volatile'. */
11051 if (cp_parser_allow_gnu_extensions_p (parser)
11052 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
11054 /* Remember that we saw the `volatile' keyword. */
11055 volatile_p = true;
11056 /* Consume the token. */
11057 cp_lexer_consume_token (parser->lexer);
11059 /* Look for the opening `('. */
11060 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
11061 return;
11062 /* Look for the string. */
11063 string = cp_parser_string_literal (parser, false, false);
11064 if (string == error_mark_node)
11066 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11067 /*consume_paren=*/true);
11068 return;
11071 /* If we're allowing GNU extensions, check for the extended assembly
11072 syntax. Unfortunately, the `:' tokens need not be separated by
11073 a space in C, and so, for compatibility, we tolerate that here
11074 too. Doing that means that we have to treat the `::' operator as
11075 two `:' tokens. */
11076 if (cp_parser_allow_gnu_extensions_p (parser)
11077 && parser->in_function_body
11078 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
11079 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
11081 bool inputs_p = false;
11082 bool clobbers_p = false;
11084 /* The extended syntax was used. */
11085 extended_p = true;
11087 /* Look for outputs. */
11088 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11090 /* Consume the `:'. */
11091 cp_lexer_consume_token (parser->lexer);
11092 /* Parse the output-operands. */
11093 if (cp_lexer_next_token_is_not (parser->lexer,
11094 CPP_COLON)
11095 && cp_lexer_next_token_is_not (parser->lexer,
11096 CPP_SCOPE)
11097 && cp_lexer_next_token_is_not (parser->lexer,
11098 CPP_CLOSE_PAREN))
11099 outputs = cp_parser_asm_operand_list (parser);
11101 /* If the next token is `::', there are no outputs, and the
11102 next token is the beginning of the inputs. */
11103 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11104 /* The inputs are coming next. */
11105 inputs_p = true;
11107 /* Look for inputs. */
11108 if (inputs_p
11109 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11111 /* Consume the `:' or `::'. */
11112 cp_lexer_consume_token (parser->lexer);
11113 /* Parse the output-operands. */
11114 if (cp_lexer_next_token_is_not (parser->lexer,
11115 CPP_COLON)
11116 && cp_lexer_next_token_is_not (parser->lexer,
11117 CPP_CLOSE_PAREN))
11118 inputs = cp_parser_asm_operand_list (parser);
11120 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11121 /* The clobbers are coming next. */
11122 clobbers_p = true;
11124 /* Look for clobbers. */
11125 if (clobbers_p
11126 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11128 /* Consume the `:' or `::'. */
11129 cp_lexer_consume_token (parser->lexer);
11130 /* Parse the clobbers. */
11131 if (cp_lexer_next_token_is_not (parser->lexer,
11132 CPP_CLOSE_PAREN))
11133 clobbers = cp_parser_asm_clobber_list (parser);
11136 /* Look for the closing `)'. */
11137 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11138 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11139 /*consume_paren=*/true);
11140 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11142 /* Create the ASM_EXPR. */
11143 if (parser->in_function_body)
11145 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
11146 inputs, clobbers);
11147 /* If the extended syntax was not used, mark the ASM_EXPR. */
11148 if (!extended_p)
11150 tree temp = asm_stmt;
11151 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
11152 temp = TREE_OPERAND (temp, 0);
11154 ASM_INPUT_P (temp) = 1;
11157 else
11158 cgraph_add_asm_node (string);
11161 /* Declarators [gram.dcl.decl] */
11163 /* Parse an init-declarator.
11165 init-declarator:
11166 declarator initializer [opt]
11168 GNU Extension:
11170 init-declarator:
11171 declarator asm-specification [opt] attributes [opt] initializer [opt]
11173 function-definition:
11174 decl-specifier-seq [opt] declarator ctor-initializer [opt]
11175 function-body
11176 decl-specifier-seq [opt] declarator function-try-block
11178 GNU Extension:
11180 function-definition:
11181 __extension__ function-definition
11183 The DECL_SPECIFIERS apply to this declarator. Returns a
11184 representation of the entity declared. If MEMBER_P is TRUE, then
11185 this declarator appears in a class scope. The new DECL created by
11186 this declarator is returned.
11188 The CHECKS are access checks that should be performed once we know
11189 what entity is being declared (and, therefore, what classes have
11190 befriended it).
11192 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
11193 for a function-definition here as well. If the declarator is a
11194 declarator for a function-definition, *FUNCTION_DEFINITION_P will
11195 be TRUE upon return. By that point, the function-definition will
11196 have been completely parsed.
11198 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
11199 is FALSE. */
11201 static tree
11202 cp_parser_init_declarator (cp_parser* parser,
11203 cp_decl_specifier_seq *decl_specifiers,
11204 VEC (deferred_access_check,gc)* checks,
11205 bool function_definition_allowed_p,
11206 bool member_p,
11207 int declares_class_or_enum,
11208 bool* function_definition_p)
11210 cp_token *token;
11211 cp_declarator *declarator;
11212 tree prefix_attributes;
11213 tree attributes;
11214 tree asm_specification;
11215 tree initializer;
11216 tree decl = NULL_TREE;
11217 tree scope;
11218 bool is_initialized;
11219 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
11220 initialized with "= ..", CPP_OPEN_PAREN if initialized with
11221 "(...)". */
11222 enum cpp_ttype initialization_kind;
11223 bool is_parenthesized_init = false;
11224 bool is_non_constant_init;
11225 int ctor_dtor_or_conv_p;
11226 bool friend_p;
11227 tree pushed_scope = NULL;
11229 /* Gather the attributes that were provided with the
11230 decl-specifiers. */
11231 prefix_attributes = decl_specifiers->attributes;
11233 /* Assume that this is not the declarator for a function
11234 definition. */
11235 if (function_definition_p)
11236 *function_definition_p = false;
11238 /* Defer access checks while parsing the declarator; we cannot know
11239 what names are accessible until we know what is being
11240 declared. */
11241 resume_deferring_access_checks ();
11243 /* Parse the declarator. */
11244 declarator
11245 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11246 &ctor_dtor_or_conv_p,
11247 /*parenthesized_p=*/NULL,
11248 /*member_p=*/false);
11249 /* Gather up the deferred checks. */
11250 stop_deferring_access_checks ();
11252 /* If the DECLARATOR was erroneous, there's no need to go
11253 further. */
11254 if (declarator == cp_error_declarator)
11255 return error_mark_node;
11257 /* Check that the number of template-parameter-lists is OK. */
11258 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
11259 return error_mark_node;
11261 if (declares_class_or_enum & 2)
11262 cp_parser_check_for_definition_in_return_type (declarator,
11263 decl_specifiers->type);
11265 /* Figure out what scope the entity declared by the DECLARATOR is
11266 located in. `grokdeclarator' sometimes changes the scope, so
11267 we compute it now. */
11268 scope = get_scope_of_declarator (declarator);
11270 /* If we're allowing GNU extensions, look for an asm-specification
11271 and attributes. */
11272 if (cp_parser_allow_gnu_extensions_p (parser))
11274 /* Look for an asm-specification. */
11275 asm_specification = cp_parser_asm_specification_opt (parser);
11276 /* And attributes. */
11277 attributes = cp_parser_attributes_opt (parser);
11279 else
11281 asm_specification = NULL_TREE;
11282 attributes = NULL_TREE;
11285 /* Peek at the next token. */
11286 token = cp_lexer_peek_token (parser->lexer);
11287 /* Check to see if the token indicates the start of a
11288 function-definition. */
11289 if (cp_parser_token_starts_function_definition_p (token))
11291 if (!function_definition_allowed_p)
11293 /* If a function-definition should not appear here, issue an
11294 error message. */
11295 cp_parser_error (parser,
11296 "a function-definition is not allowed here");
11297 return error_mark_node;
11299 else
11301 /* Neither attributes nor an asm-specification are allowed
11302 on a function-definition. */
11303 if (asm_specification)
11304 error ("an asm-specification is not allowed on a function-definition");
11305 if (attributes)
11306 error ("attributes are not allowed on a function-definition");
11307 /* This is a function-definition. */
11308 *function_definition_p = true;
11310 /* Parse the function definition. */
11311 if (member_p)
11312 decl = cp_parser_save_member_function_body (parser,
11313 decl_specifiers,
11314 declarator,
11315 prefix_attributes);
11316 else
11317 decl
11318 = (cp_parser_function_definition_from_specifiers_and_declarator
11319 (parser, decl_specifiers, prefix_attributes, declarator));
11321 return decl;
11325 /* [dcl.dcl]
11327 Only in function declarations for constructors, destructors, and
11328 type conversions can the decl-specifier-seq be omitted.
11330 We explicitly postpone this check past the point where we handle
11331 function-definitions because we tolerate function-definitions
11332 that are missing their return types in some modes. */
11333 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
11335 cp_parser_error (parser,
11336 "expected constructor, destructor, or type conversion");
11337 return error_mark_node;
11340 /* An `=' or an `(' indicates an initializer. */
11341 if (token->type == CPP_EQ
11342 || token->type == CPP_OPEN_PAREN)
11344 is_initialized = true;
11345 initialization_kind = token->type;
11347 else
11349 /* If the init-declarator isn't initialized and isn't followed by a
11350 `,' or `;', it's not a valid init-declarator. */
11351 if (token->type != CPP_COMMA
11352 && token->type != CPP_SEMICOLON)
11354 cp_parser_error (parser, "expected initializer");
11355 return error_mark_node;
11357 is_initialized = false;
11358 initialization_kind = CPP_EOF;
11361 /* Because start_decl has side-effects, we should only call it if we
11362 know we're going ahead. By this point, we know that we cannot
11363 possibly be looking at any other construct. */
11364 cp_parser_commit_to_tentative_parse (parser);
11366 /* If the decl specifiers were bad, issue an error now that we're
11367 sure this was intended to be a declarator. Then continue
11368 declaring the variable(s), as int, to try to cut down on further
11369 errors. */
11370 if (decl_specifiers->any_specifiers_p
11371 && decl_specifiers->type == error_mark_node)
11373 cp_parser_error (parser, "invalid type in declaration");
11374 decl_specifiers->type = integer_type_node;
11377 /* Check to see whether or not this declaration is a friend. */
11378 friend_p = cp_parser_friend_p (decl_specifiers);
11380 /* Enter the newly declared entry in the symbol table. If we're
11381 processing a declaration in a class-specifier, we wait until
11382 after processing the initializer. */
11383 if (!member_p)
11385 if (parser->in_unbraced_linkage_specification_p)
11386 decl_specifiers->storage_class = sc_extern;
11387 decl = start_decl (declarator, decl_specifiers,
11388 is_initialized, attributes, prefix_attributes,
11389 &pushed_scope);
11391 else if (scope)
11392 /* Enter the SCOPE. That way unqualified names appearing in the
11393 initializer will be looked up in SCOPE. */
11394 pushed_scope = push_scope (scope);
11396 /* Perform deferred access control checks, now that we know in which
11397 SCOPE the declared entity resides. */
11398 if (!member_p && decl)
11400 tree saved_current_function_decl = NULL_TREE;
11402 /* If the entity being declared is a function, pretend that we
11403 are in its scope. If it is a `friend', it may have access to
11404 things that would not otherwise be accessible. */
11405 if (TREE_CODE (decl) == FUNCTION_DECL)
11407 saved_current_function_decl = current_function_decl;
11408 current_function_decl = decl;
11411 /* Perform access checks for template parameters. */
11412 cp_parser_perform_template_parameter_access_checks (checks);
11414 /* Perform the access control checks for the declarator and the
11415 the decl-specifiers. */
11416 perform_deferred_access_checks ();
11418 /* Restore the saved value. */
11419 if (TREE_CODE (decl) == FUNCTION_DECL)
11420 current_function_decl = saved_current_function_decl;
11423 /* Parse the initializer. */
11424 initializer = NULL_TREE;
11425 is_parenthesized_init = false;
11426 is_non_constant_init = true;
11427 if (is_initialized)
11429 if (function_declarator_p (declarator))
11431 if (initialization_kind == CPP_EQ)
11432 initializer = cp_parser_pure_specifier (parser);
11433 else
11435 /* If the declaration was erroneous, we don't really
11436 know what the user intended, so just silently
11437 consume the initializer. */
11438 if (decl != error_mark_node)
11439 error ("initializer provided for function");
11440 cp_parser_skip_to_closing_parenthesis (parser,
11441 /*recovering=*/true,
11442 /*or_comma=*/false,
11443 /*consume_paren=*/true);
11446 else
11447 initializer = cp_parser_initializer (parser,
11448 &is_parenthesized_init,
11449 &is_non_constant_init);
11452 /* The old parser allows attributes to appear after a parenthesized
11453 initializer. Mark Mitchell proposed removing this functionality
11454 on the GCC mailing lists on 2002-08-13. This parser accepts the
11455 attributes -- but ignores them. */
11456 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
11457 if (cp_parser_attributes_opt (parser))
11458 warning (OPT_Wattributes,
11459 "attributes after parenthesized initializer ignored");
11461 /* For an in-class declaration, use `grokfield' to create the
11462 declaration. */
11463 if (member_p)
11465 if (pushed_scope)
11467 pop_scope (pushed_scope);
11468 pushed_scope = false;
11470 decl = grokfield (declarator, decl_specifiers,
11471 initializer, !is_non_constant_init,
11472 /*asmspec=*/NULL_TREE,
11473 prefix_attributes);
11474 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
11475 cp_parser_save_default_args (parser, decl);
11478 /* Finish processing the declaration. But, skip friend
11479 declarations. */
11480 if (!friend_p && decl && decl != error_mark_node)
11482 cp_finish_decl (decl,
11483 initializer, !is_non_constant_init,
11484 asm_specification,
11485 /* If the initializer is in parentheses, then this is
11486 a direct-initialization, which means that an
11487 `explicit' constructor is OK. Otherwise, an
11488 `explicit' constructor cannot be used. */
11489 ((is_parenthesized_init || !is_initialized)
11490 ? 0 : LOOKUP_ONLYCONVERTING));
11492 if (!friend_p && pushed_scope)
11493 pop_scope (pushed_scope);
11495 return decl;
11498 /* Parse a declarator.
11500 declarator:
11501 direct-declarator
11502 ptr-operator declarator
11504 abstract-declarator:
11505 ptr-operator abstract-declarator [opt]
11506 direct-abstract-declarator
11508 GNU Extensions:
11510 declarator:
11511 attributes [opt] direct-declarator
11512 attributes [opt] ptr-operator declarator
11514 abstract-declarator:
11515 attributes [opt] ptr-operator abstract-declarator [opt]
11516 attributes [opt] direct-abstract-declarator
11518 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
11519 detect constructor, destructor or conversion operators. It is set
11520 to -1 if the declarator is a name, and +1 if it is a
11521 function. Otherwise it is set to zero. Usually you just want to
11522 test for >0, but internally the negative value is used.
11524 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
11525 a decl-specifier-seq unless it declares a constructor, destructor,
11526 or conversion. It might seem that we could check this condition in
11527 semantic analysis, rather than parsing, but that makes it difficult
11528 to handle something like `f()'. We want to notice that there are
11529 no decl-specifiers, and therefore realize that this is an
11530 expression, not a declaration.)
11532 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11533 the declarator is a direct-declarator of the form "(...)".
11535 MEMBER_P is true iff this declarator is a member-declarator. */
11537 static cp_declarator *
11538 cp_parser_declarator (cp_parser* parser,
11539 cp_parser_declarator_kind dcl_kind,
11540 int* ctor_dtor_or_conv_p,
11541 bool* parenthesized_p,
11542 bool member_p)
11544 cp_token *token;
11545 cp_declarator *declarator;
11546 enum tree_code code;
11547 cp_cv_quals cv_quals;
11548 tree class_type;
11549 tree attributes = NULL_TREE;
11551 /* Assume this is not a constructor, destructor, or type-conversion
11552 operator. */
11553 if (ctor_dtor_or_conv_p)
11554 *ctor_dtor_or_conv_p = 0;
11556 if (cp_parser_allow_gnu_extensions_p (parser))
11557 attributes = cp_parser_attributes_opt (parser);
11559 /* Peek at the next token. */
11560 token = cp_lexer_peek_token (parser->lexer);
11562 /* Check for the ptr-operator production. */
11563 cp_parser_parse_tentatively (parser);
11564 /* Parse the ptr-operator. */
11565 code = cp_parser_ptr_operator (parser,
11566 &class_type,
11567 &cv_quals);
11568 /* If that worked, then we have a ptr-operator. */
11569 if (cp_parser_parse_definitely (parser))
11571 /* If a ptr-operator was found, then this declarator was not
11572 parenthesized. */
11573 if (parenthesized_p)
11574 *parenthesized_p = true;
11575 /* The dependent declarator is optional if we are parsing an
11576 abstract-declarator. */
11577 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11578 cp_parser_parse_tentatively (parser);
11580 /* Parse the dependent declarator. */
11581 declarator = cp_parser_declarator (parser, dcl_kind,
11582 /*ctor_dtor_or_conv_p=*/NULL,
11583 /*parenthesized_p=*/NULL,
11584 /*member_p=*/false);
11586 /* If we are parsing an abstract-declarator, we must handle the
11587 case where the dependent declarator is absent. */
11588 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
11589 && !cp_parser_parse_definitely (parser))
11590 declarator = NULL;
11592 /* Build the representation of the ptr-operator. */
11593 if (class_type)
11594 declarator = make_ptrmem_declarator (cv_quals,
11595 class_type,
11596 declarator);
11597 else if (code == INDIRECT_REF)
11598 declarator = make_pointer_declarator (cv_quals, declarator);
11599 else
11600 declarator = make_reference_declarator (cv_quals, declarator);
11602 /* Everything else is a direct-declarator. */
11603 else
11605 if (parenthesized_p)
11606 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
11607 CPP_OPEN_PAREN);
11608 declarator = cp_parser_direct_declarator (parser, dcl_kind,
11609 ctor_dtor_or_conv_p,
11610 member_p);
11613 if (attributes && declarator && declarator != cp_error_declarator)
11614 declarator->attributes = attributes;
11616 return declarator;
11619 /* Parse a direct-declarator or direct-abstract-declarator.
11621 direct-declarator:
11622 declarator-id
11623 direct-declarator ( parameter-declaration-clause )
11624 cv-qualifier-seq [opt]
11625 exception-specification [opt]
11626 direct-declarator [ constant-expression [opt] ]
11627 ( declarator )
11629 direct-abstract-declarator:
11630 direct-abstract-declarator [opt]
11631 ( parameter-declaration-clause )
11632 cv-qualifier-seq [opt]
11633 exception-specification [opt]
11634 direct-abstract-declarator [opt] [ constant-expression [opt] ]
11635 ( abstract-declarator )
11637 Returns a representation of the declarator. DCL_KIND is
11638 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
11639 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
11640 we are parsing a direct-declarator. It is
11641 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
11642 of ambiguity we prefer an abstract declarator, as per
11643 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
11644 cp_parser_declarator. */
11646 static cp_declarator *
11647 cp_parser_direct_declarator (cp_parser* parser,
11648 cp_parser_declarator_kind dcl_kind,
11649 int* ctor_dtor_or_conv_p,
11650 bool member_p)
11652 cp_token *token;
11653 cp_declarator *declarator = NULL;
11654 tree scope = NULL_TREE;
11655 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11656 bool saved_in_declarator_p = parser->in_declarator_p;
11657 bool first = true;
11658 tree pushed_scope = NULL_TREE;
11660 while (true)
11662 /* Peek at the next token. */
11663 token = cp_lexer_peek_token (parser->lexer);
11664 if (token->type == CPP_OPEN_PAREN)
11666 /* This is either a parameter-declaration-clause, or a
11667 parenthesized declarator. When we know we are parsing a
11668 named declarator, it must be a parenthesized declarator
11669 if FIRST is true. For instance, `(int)' is a
11670 parameter-declaration-clause, with an omitted
11671 direct-abstract-declarator. But `((*))', is a
11672 parenthesized abstract declarator. Finally, when T is a
11673 template parameter `(T)' is a
11674 parameter-declaration-clause, and not a parenthesized
11675 named declarator.
11677 We first try and parse a parameter-declaration-clause,
11678 and then try a nested declarator (if FIRST is true).
11680 It is not an error for it not to be a
11681 parameter-declaration-clause, even when FIRST is
11682 false. Consider,
11684 int i (int);
11685 int i (3);
11687 The first is the declaration of a function while the
11688 second is a the definition of a variable, including its
11689 initializer.
11691 Having seen only the parenthesis, we cannot know which of
11692 these two alternatives should be selected. Even more
11693 complex are examples like:
11695 int i (int (a));
11696 int i (int (3));
11698 The former is a function-declaration; the latter is a
11699 variable initialization.
11701 Thus again, we try a parameter-declaration-clause, and if
11702 that fails, we back out and return. */
11704 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11706 cp_parameter_declarator *params;
11707 unsigned saved_num_template_parameter_lists;
11709 /* In a member-declarator, the only valid interpretation
11710 of a parenthesis is the start of a
11711 parameter-declaration-clause. (It is invalid to
11712 initialize a static data member with a parenthesized
11713 initializer; only the "=" form of initialization is
11714 permitted.) */
11715 if (!member_p)
11716 cp_parser_parse_tentatively (parser);
11718 /* Consume the `('. */
11719 cp_lexer_consume_token (parser->lexer);
11720 if (first)
11722 /* If this is going to be an abstract declarator, we're
11723 in a declarator and we can't have default args. */
11724 parser->default_arg_ok_p = false;
11725 parser->in_declarator_p = true;
11728 /* Inside the function parameter list, surrounding
11729 template-parameter-lists do not apply. */
11730 saved_num_template_parameter_lists
11731 = parser->num_template_parameter_lists;
11732 parser->num_template_parameter_lists = 0;
11734 /* Parse the parameter-declaration-clause. */
11735 params = cp_parser_parameter_declaration_clause (parser);
11737 parser->num_template_parameter_lists
11738 = saved_num_template_parameter_lists;
11740 /* If all went well, parse the cv-qualifier-seq and the
11741 exception-specification. */
11742 if (member_p || cp_parser_parse_definitely (parser))
11744 cp_cv_quals cv_quals;
11745 tree exception_specification;
11747 if (ctor_dtor_or_conv_p)
11748 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
11749 first = false;
11750 /* Consume the `)'. */
11751 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
11753 /* Parse the cv-qualifier-seq. */
11754 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11755 /* And the exception-specification. */
11756 exception_specification
11757 = cp_parser_exception_specification_opt (parser);
11759 /* Create the function-declarator. */
11760 declarator = make_call_declarator (declarator,
11761 params,
11762 cv_quals,
11763 exception_specification);
11764 /* Any subsequent parameter lists are to do with
11765 return type, so are not those of the declared
11766 function. */
11767 parser->default_arg_ok_p = false;
11769 /* Repeat the main loop. */
11770 continue;
11774 /* If this is the first, we can try a parenthesized
11775 declarator. */
11776 if (first)
11778 bool saved_in_type_id_in_expr_p;
11780 parser->default_arg_ok_p = saved_default_arg_ok_p;
11781 parser->in_declarator_p = saved_in_declarator_p;
11783 /* Consume the `('. */
11784 cp_lexer_consume_token (parser->lexer);
11785 /* Parse the nested declarator. */
11786 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11787 parser->in_type_id_in_expr_p = true;
11788 declarator
11789 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
11790 /*parenthesized_p=*/NULL,
11791 member_p);
11792 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
11793 first = false;
11794 /* Expect a `)'. */
11795 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11796 declarator = cp_error_declarator;
11797 if (declarator == cp_error_declarator)
11798 break;
11800 goto handle_declarator;
11802 /* Otherwise, we must be done. */
11803 else
11804 break;
11806 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11807 && token->type == CPP_OPEN_SQUARE)
11809 /* Parse an array-declarator. */
11810 tree bounds;
11812 if (ctor_dtor_or_conv_p)
11813 *ctor_dtor_or_conv_p = 0;
11815 first = false;
11816 parser->default_arg_ok_p = false;
11817 parser->in_declarator_p = true;
11818 /* Consume the `['. */
11819 cp_lexer_consume_token (parser->lexer);
11820 /* Peek at the next token. */
11821 token = cp_lexer_peek_token (parser->lexer);
11822 /* If the next token is `]', then there is no
11823 constant-expression. */
11824 if (token->type != CPP_CLOSE_SQUARE)
11826 bool non_constant_p;
11828 bounds
11829 = cp_parser_constant_expression (parser,
11830 /*allow_non_constant=*/true,
11831 &non_constant_p);
11832 if (!non_constant_p)
11833 bounds = fold_non_dependent_expr (bounds);
11834 /* Normally, the array bound must be an integral constant
11835 expression. However, as an extension, we allow VLAs
11836 in function scopes. */
11837 else if (!parser->in_function_body)
11839 error ("array bound is not an integer constant");
11840 bounds = error_mark_node;
11843 else
11844 bounds = NULL_TREE;
11845 /* Look for the closing `]'. */
11846 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11848 declarator = cp_error_declarator;
11849 break;
11852 declarator = make_array_declarator (declarator, bounds);
11854 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11856 tree qualifying_scope;
11857 tree unqualified_name;
11858 special_function_kind sfk;
11859 bool abstract_ok;
11861 /* Parse a declarator-id */
11862 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
11863 if (abstract_ok)
11864 cp_parser_parse_tentatively (parser);
11865 unqualified_name
11866 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
11867 qualifying_scope = parser->scope;
11868 if (abstract_ok)
11870 if (!cp_parser_parse_definitely (parser))
11871 unqualified_name = error_mark_node;
11872 else if (unqualified_name
11873 && (qualifying_scope
11874 || (TREE_CODE (unqualified_name)
11875 != IDENTIFIER_NODE)))
11877 cp_parser_error (parser, "expected unqualified-id");
11878 unqualified_name = error_mark_node;
11882 if (!unqualified_name)
11883 return NULL;
11884 if (unqualified_name == error_mark_node)
11886 declarator = cp_error_declarator;
11887 break;
11890 if (qualifying_scope && at_namespace_scope_p ()
11891 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
11893 /* In the declaration of a member of a template class
11894 outside of the class itself, the SCOPE will sometimes
11895 be a TYPENAME_TYPE. For example, given:
11897 template <typename T>
11898 int S<T>::R::i = 3;
11900 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
11901 this context, we must resolve S<T>::R to an ordinary
11902 type, rather than a typename type.
11904 The reason we normally avoid resolving TYPENAME_TYPEs
11905 is that a specialization of `S' might render
11906 `S<T>::R' not a type. However, if `S' is
11907 specialized, then this `i' will not be used, so there
11908 is no harm in resolving the types here. */
11909 tree type;
11911 /* Resolve the TYPENAME_TYPE. */
11912 type = resolve_typename_type (qualifying_scope,
11913 /*only_current_p=*/false);
11914 /* If that failed, the declarator is invalid. */
11915 if (type == error_mark_node)
11916 error ("%<%T::%D%> is not a type",
11917 TYPE_CONTEXT (qualifying_scope),
11918 TYPE_IDENTIFIER (qualifying_scope));
11919 qualifying_scope = type;
11922 sfk = sfk_none;
11923 if (unqualified_name)
11925 tree class_type;
11927 if (qualifying_scope
11928 && CLASS_TYPE_P (qualifying_scope))
11929 class_type = qualifying_scope;
11930 else
11931 class_type = current_class_type;
11933 if (TREE_CODE (unqualified_name) == TYPE_DECL)
11935 tree name_type = TREE_TYPE (unqualified_name);
11936 if (class_type && same_type_p (name_type, class_type))
11938 if (qualifying_scope
11939 && CLASSTYPE_USE_TEMPLATE (name_type))
11941 error ("invalid use of constructor as a template");
11942 inform ("use %<%T::%D%> instead of %<%T::%D%> to "
11943 "name the constructor in a qualified name",
11944 class_type,
11945 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11946 class_type, name_type);
11947 declarator = cp_error_declarator;
11948 break;
11950 else
11951 unqualified_name = constructor_name (class_type);
11953 else
11955 /* We do not attempt to print the declarator
11956 here because we do not have enough
11957 information about its original syntactic
11958 form. */
11959 cp_parser_error (parser, "invalid declarator");
11960 declarator = cp_error_declarator;
11961 break;
11965 if (class_type)
11967 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11968 sfk = sfk_destructor;
11969 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11970 sfk = sfk_conversion;
11971 else if (/* There's no way to declare a constructor
11972 for an anonymous type, even if the type
11973 got a name for linkage purposes. */
11974 !TYPE_WAS_ANONYMOUS (class_type)
11975 && constructor_name_p (unqualified_name,
11976 class_type))
11978 unqualified_name = constructor_name (class_type);
11979 sfk = sfk_constructor;
11982 if (ctor_dtor_or_conv_p && sfk != sfk_none)
11983 *ctor_dtor_or_conv_p = -1;
11986 declarator = make_id_declarator (qualifying_scope,
11987 unqualified_name,
11988 sfk);
11989 declarator->id_loc = token->location;
11991 handle_declarator:;
11992 scope = get_scope_of_declarator (declarator);
11993 if (scope)
11994 /* Any names that appear after the declarator-id for a
11995 member are looked up in the containing scope. */
11996 pushed_scope = push_scope (scope);
11997 parser->in_declarator_p = true;
11998 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11999 || (declarator && declarator->kind == cdk_id))
12000 /* Default args are only allowed on function
12001 declarations. */
12002 parser->default_arg_ok_p = saved_default_arg_ok_p;
12003 else
12004 parser->default_arg_ok_p = false;
12006 first = false;
12008 /* We're done. */
12009 else
12010 break;
12013 /* For an abstract declarator, we might wind up with nothing at this
12014 point. That's an error; the declarator is not optional. */
12015 if (!declarator)
12016 cp_parser_error (parser, "expected declarator");
12018 /* If we entered a scope, we must exit it now. */
12019 if (pushed_scope)
12020 pop_scope (pushed_scope);
12022 parser->default_arg_ok_p = saved_default_arg_ok_p;
12023 parser->in_declarator_p = saved_in_declarator_p;
12025 return declarator;
12028 /* Parse a ptr-operator.
12030 ptr-operator:
12031 * cv-qualifier-seq [opt]
12033 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
12035 GNU Extension:
12037 ptr-operator:
12038 & cv-qualifier-seq [opt]
12040 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
12041 Returns ADDR_EXPR if a reference was used. In the case of a
12042 pointer-to-member, *TYPE is filled in with the TYPE containing the
12043 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
12044 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
12045 ERROR_MARK if an error occurred. */
12047 static enum tree_code
12048 cp_parser_ptr_operator (cp_parser* parser,
12049 tree* type,
12050 cp_cv_quals *cv_quals)
12052 enum tree_code code = ERROR_MARK;
12053 cp_token *token;
12055 /* Assume that it's not a pointer-to-member. */
12056 *type = NULL_TREE;
12057 /* And that there are no cv-qualifiers. */
12058 *cv_quals = TYPE_UNQUALIFIED;
12060 /* Peek at the next token. */
12061 token = cp_lexer_peek_token (parser->lexer);
12062 /* If it's a `*' or `&' we have a pointer or reference. */
12063 if (token->type == CPP_MULT || token->type == CPP_AND)
12065 /* Remember which ptr-operator we were processing. */
12066 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
12068 /* Consume the `*' or `&'. */
12069 cp_lexer_consume_token (parser->lexer);
12071 /* A `*' can be followed by a cv-qualifier-seq, and so can a
12072 `&', if we are allowing GNU extensions. (The only qualifier
12073 that can legally appear after `&' is `restrict', but that is
12074 enforced during semantic analysis. */
12075 if (code == INDIRECT_REF
12076 || cp_parser_allow_gnu_extensions_p (parser))
12077 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12079 else
12081 /* Try the pointer-to-member case. */
12082 cp_parser_parse_tentatively (parser);
12083 /* Look for the optional `::' operator. */
12084 cp_parser_global_scope_opt (parser,
12085 /*current_scope_valid_p=*/false);
12086 /* Look for the nested-name specifier. */
12087 cp_parser_nested_name_specifier (parser,
12088 /*typename_keyword_p=*/false,
12089 /*check_dependency_p=*/true,
12090 /*type_p=*/false,
12091 /*is_declaration=*/false);
12092 /* If we found it, and the next token is a `*', then we are
12093 indeed looking at a pointer-to-member operator. */
12094 if (!cp_parser_error_occurred (parser)
12095 && cp_parser_require (parser, CPP_MULT, "`*'"))
12097 /* Indicate that the `*' operator was used. */
12098 code = INDIRECT_REF;
12100 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
12101 error ("%qD is a namespace", parser->scope);
12102 else
12104 /* The type of which the member is a member is given by the
12105 current SCOPE. */
12106 *type = parser->scope;
12107 /* The next name will not be qualified. */
12108 parser->scope = NULL_TREE;
12109 parser->qualifying_scope = NULL_TREE;
12110 parser->object_scope = NULL_TREE;
12111 /* Look for the optional cv-qualifier-seq. */
12112 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12115 /* If that didn't work we don't have a ptr-operator. */
12116 if (!cp_parser_parse_definitely (parser))
12117 cp_parser_error (parser, "expected ptr-operator");
12120 return code;
12123 /* Parse an (optional) cv-qualifier-seq.
12125 cv-qualifier-seq:
12126 cv-qualifier cv-qualifier-seq [opt]
12128 cv-qualifier:
12129 const
12130 volatile
12132 GNU Extension:
12134 cv-qualifier:
12135 __restrict__
12137 Returns a bitmask representing the cv-qualifiers. */
12139 static cp_cv_quals
12140 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
12142 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
12144 while (true)
12146 cp_token *token;
12147 cp_cv_quals cv_qualifier;
12149 /* Peek at the next token. */
12150 token = cp_lexer_peek_token (parser->lexer);
12151 /* See if it's a cv-qualifier. */
12152 switch (token->keyword)
12154 case RID_CONST:
12155 cv_qualifier = TYPE_QUAL_CONST;
12156 break;
12158 case RID_VOLATILE:
12159 cv_qualifier = TYPE_QUAL_VOLATILE;
12160 break;
12162 case RID_RESTRICT:
12163 cv_qualifier = TYPE_QUAL_RESTRICT;
12164 break;
12166 default:
12167 cv_qualifier = TYPE_UNQUALIFIED;
12168 break;
12171 if (!cv_qualifier)
12172 break;
12174 if (cv_quals & cv_qualifier)
12176 error ("duplicate cv-qualifier");
12177 cp_lexer_purge_token (parser->lexer);
12179 else
12181 cp_lexer_consume_token (parser->lexer);
12182 cv_quals |= cv_qualifier;
12186 return cv_quals;
12189 /* Parse a declarator-id.
12191 declarator-id:
12192 id-expression
12193 :: [opt] nested-name-specifier [opt] type-name
12195 In the `id-expression' case, the value returned is as for
12196 cp_parser_id_expression if the id-expression was an unqualified-id.
12197 If the id-expression was a qualified-id, then a SCOPE_REF is
12198 returned. The first operand is the scope (either a NAMESPACE_DECL
12199 or TREE_TYPE), but the second is still just a representation of an
12200 unqualified-id. */
12202 static tree
12203 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
12205 tree id;
12206 /* The expression must be an id-expression. Assume that qualified
12207 names are the names of types so that:
12209 template <class T>
12210 int S<T>::R::i = 3;
12212 will work; we must treat `S<T>::R' as the name of a type.
12213 Similarly, assume that qualified names are templates, where
12214 required, so that:
12216 template <class T>
12217 int S<T>::R<T>::i = 3;
12219 will work, too. */
12220 id = cp_parser_id_expression (parser,
12221 /*template_keyword_p=*/false,
12222 /*check_dependency_p=*/false,
12223 /*template_p=*/NULL,
12224 /*declarator_p=*/true,
12225 optional_p);
12226 if (id && BASELINK_P (id))
12227 id = BASELINK_FUNCTIONS (id);
12228 return id;
12231 /* Parse a type-id.
12233 type-id:
12234 type-specifier-seq abstract-declarator [opt]
12236 Returns the TYPE specified. */
12238 static tree
12239 cp_parser_type_id (cp_parser* parser)
12241 cp_decl_specifier_seq type_specifier_seq;
12242 cp_declarator *abstract_declarator;
12244 /* Parse the type-specifier-seq. */
12245 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
12246 &type_specifier_seq);
12247 if (type_specifier_seq.type == error_mark_node)
12248 return error_mark_node;
12250 /* There might or might not be an abstract declarator. */
12251 cp_parser_parse_tentatively (parser);
12252 /* Look for the declarator. */
12253 abstract_declarator
12254 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
12255 /*parenthesized_p=*/NULL,
12256 /*member_p=*/false);
12257 /* Check to see if there really was a declarator. */
12258 if (!cp_parser_parse_definitely (parser))
12259 abstract_declarator = NULL;
12261 return groktypename (&type_specifier_seq, abstract_declarator);
12264 /* Parse a type-specifier-seq.
12266 type-specifier-seq:
12267 type-specifier type-specifier-seq [opt]
12269 GNU extension:
12271 type-specifier-seq:
12272 attributes type-specifier-seq [opt]
12274 If IS_CONDITION is true, we are at the start of a "condition",
12275 e.g., we've just seen "if (".
12277 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
12279 static void
12280 cp_parser_type_specifier_seq (cp_parser* parser,
12281 bool is_condition,
12282 cp_decl_specifier_seq *type_specifier_seq)
12284 bool seen_type_specifier = false;
12285 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
12287 /* Clear the TYPE_SPECIFIER_SEQ. */
12288 clear_decl_specs (type_specifier_seq);
12290 /* Parse the type-specifiers and attributes. */
12291 while (true)
12293 tree type_specifier;
12294 bool is_cv_qualifier;
12296 /* Check for attributes first. */
12297 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
12299 type_specifier_seq->attributes =
12300 chainon (type_specifier_seq->attributes,
12301 cp_parser_attributes_opt (parser));
12302 continue;
12305 /* Look for the type-specifier. */
12306 type_specifier = cp_parser_type_specifier (parser,
12307 flags,
12308 type_specifier_seq,
12309 /*is_declaration=*/false,
12310 NULL,
12311 &is_cv_qualifier);
12312 if (!type_specifier)
12314 /* If the first type-specifier could not be found, this is not a
12315 type-specifier-seq at all. */
12316 if (!seen_type_specifier)
12318 cp_parser_error (parser, "expected type-specifier");
12319 type_specifier_seq->type = error_mark_node;
12320 return;
12322 /* If subsequent type-specifiers could not be found, the
12323 type-specifier-seq is complete. */
12324 break;
12327 seen_type_specifier = true;
12328 /* The standard says that a condition can be:
12330 type-specifier-seq declarator = assignment-expression
12332 However, given:
12334 struct S {};
12335 if (int S = ...)
12337 we should treat the "S" as a declarator, not as a
12338 type-specifier. The standard doesn't say that explicitly for
12339 type-specifier-seq, but it does say that for
12340 decl-specifier-seq in an ordinary declaration. Perhaps it
12341 would be clearer just to allow a decl-specifier-seq here, and
12342 then add a semantic restriction that if any decl-specifiers
12343 that are not type-specifiers appear, the program is invalid. */
12344 if (is_condition && !is_cv_qualifier)
12345 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
12348 cp_parser_check_decl_spec (type_specifier_seq);
12351 /* Parse a parameter-declaration-clause.
12353 parameter-declaration-clause:
12354 parameter-declaration-list [opt] ... [opt]
12355 parameter-declaration-list , ...
12357 Returns a representation for the parameter declarations. A return
12358 value of NULL indicates a parameter-declaration-clause consisting
12359 only of an ellipsis. */
12361 static cp_parameter_declarator *
12362 cp_parser_parameter_declaration_clause (cp_parser* parser)
12364 cp_parameter_declarator *parameters;
12365 cp_token *token;
12366 bool ellipsis_p;
12367 bool is_error;
12369 /* Peek at the next token. */
12370 token = cp_lexer_peek_token (parser->lexer);
12371 /* Check for trivial parameter-declaration-clauses. */
12372 if (token->type == CPP_ELLIPSIS)
12374 /* Consume the `...' token. */
12375 cp_lexer_consume_token (parser->lexer);
12376 return NULL;
12378 else if (token->type == CPP_CLOSE_PAREN)
12379 /* There are no parameters. */
12381 #ifndef NO_IMPLICIT_EXTERN_C
12382 if (in_system_header && current_class_type == NULL
12383 && current_lang_name == lang_name_c)
12384 return NULL;
12385 else
12386 #endif
12387 return no_parameters;
12389 /* Check for `(void)', too, which is a special case. */
12390 else if (token->keyword == RID_VOID
12391 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
12392 == CPP_CLOSE_PAREN))
12394 /* Consume the `void' token. */
12395 cp_lexer_consume_token (parser->lexer);
12396 /* There are no parameters. */
12397 return no_parameters;
12400 /* Parse the parameter-declaration-list. */
12401 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
12402 /* If a parse error occurred while parsing the
12403 parameter-declaration-list, then the entire
12404 parameter-declaration-clause is erroneous. */
12405 if (is_error)
12406 return NULL;
12408 /* Peek at the next token. */
12409 token = cp_lexer_peek_token (parser->lexer);
12410 /* If it's a `,', the clause should terminate with an ellipsis. */
12411 if (token->type == CPP_COMMA)
12413 /* Consume the `,'. */
12414 cp_lexer_consume_token (parser->lexer);
12415 /* Expect an ellipsis. */
12416 ellipsis_p
12417 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
12419 /* It might also be `...' if the optional trailing `,' was
12420 omitted. */
12421 else if (token->type == CPP_ELLIPSIS)
12423 /* Consume the `...' token. */
12424 cp_lexer_consume_token (parser->lexer);
12425 /* And remember that we saw it. */
12426 ellipsis_p = true;
12428 else
12429 ellipsis_p = false;
12431 /* Finish the parameter list. */
12432 if (parameters && ellipsis_p)
12433 parameters->ellipsis_p = true;
12435 return parameters;
12438 /* Parse a parameter-declaration-list.
12440 parameter-declaration-list:
12441 parameter-declaration
12442 parameter-declaration-list , parameter-declaration
12444 Returns a representation of the parameter-declaration-list, as for
12445 cp_parser_parameter_declaration_clause. However, the
12446 `void_list_node' is never appended to the list. Upon return,
12447 *IS_ERROR will be true iff an error occurred. */
12449 static cp_parameter_declarator *
12450 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
12452 cp_parameter_declarator *parameters = NULL;
12453 cp_parameter_declarator **tail = &parameters;
12454 bool saved_in_unbraced_linkage_specification_p;
12456 /* Assume all will go well. */
12457 *is_error = false;
12458 /* The special considerations that apply to a function within an
12459 unbraced linkage specifications do not apply to the parameters
12460 to the function. */
12461 saved_in_unbraced_linkage_specification_p
12462 = parser->in_unbraced_linkage_specification_p;
12463 parser->in_unbraced_linkage_specification_p = false;
12465 /* Look for more parameters. */
12466 while (true)
12468 cp_parameter_declarator *parameter;
12469 bool parenthesized_p;
12470 /* Parse the parameter. */
12471 parameter
12472 = cp_parser_parameter_declaration (parser,
12473 /*template_parm_p=*/false,
12474 &parenthesized_p);
12476 /* If a parse error occurred parsing the parameter declaration,
12477 then the entire parameter-declaration-list is erroneous. */
12478 if (!parameter)
12480 *is_error = true;
12481 parameters = NULL;
12482 break;
12484 /* Add the new parameter to the list. */
12485 *tail = parameter;
12486 tail = &parameter->next;
12488 /* Peek at the next token. */
12489 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
12490 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12491 /* These are for Objective-C++ */
12492 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12493 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12494 /* The parameter-declaration-list is complete. */
12495 break;
12496 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12498 cp_token *token;
12500 /* Peek at the next token. */
12501 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12502 /* If it's an ellipsis, then the list is complete. */
12503 if (token->type == CPP_ELLIPSIS)
12504 break;
12505 /* Otherwise, there must be more parameters. Consume the
12506 `,'. */
12507 cp_lexer_consume_token (parser->lexer);
12508 /* When parsing something like:
12510 int i(float f, double d)
12512 we can tell after seeing the declaration for "f" that we
12513 are not looking at an initialization of a variable "i",
12514 but rather at the declaration of a function "i".
12516 Due to the fact that the parsing of template arguments
12517 (as specified to a template-id) requires backtracking we
12518 cannot use this technique when inside a template argument
12519 list. */
12520 if (!parser->in_template_argument_list_p
12521 && !parser->in_type_id_in_expr_p
12522 && cp_parser_uncommitted_to_tentative_parse_p (parser)
12523 /* However, a parameter-declaration of the form
12524 "foat(f)" (which is a valid declaration of a
12525 parameter "f") can also be interpreted as an
12526 expression (the conversion of "f" to "float"). */
12527 && !parenthesized_p)
12528 cp_parser_commit_to_tentative_parse (parser);
12530 else
12532 cp_parser_error (parser, "expected %<,%> or %<...%>");
12533 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12534 cp_parser_skip_to_closing_parenthesis (parser,
12535 /*recovering=*/true,
12536 /*or_comma=*/false,
12537 /*consume_paren=*/false);
12538 break;
12542 parser->in_unbraced_linkage_specification_p
12543 = saved_in_unbraced_linkage_specification_p;
12545 return parameters;
12548 /* Parse a parameter declaration.
12550 parameter-declaration:
12551 decl-specifier-seq declarator
12552 decl-specifier-seq declarator = assignment-expression
12553 decl-specifier-seq abstract-declarator [opt]
12554 decl-specifier-seq abstract-declarator [opt] = assignment-expression
12556 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
12557 declares a template parameter. (In that case, a non-nested `>'
12558 token encountered during the parsing of the assignment-expression
12559 is not interpreted as a greater-than operator.)
12561 Returns a representation of the parameter, or NULL if an error
12562 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
12563 true iff the declarator is of the form "(p)". */
12565 static cp_parameter_declarator *
12566 cp_parser_parameter_declaration (cp_parser *parser,
12567 bool template_parm_p,
12568 bool *parenthesized_p)
12570 int declares_class_or_enum;
12571 bool greater_than_is_operator_p;
12572 cp_decl_specifier_seq decl_specifiers;
12573 cp_declarator *declarator;
12574 tree default_argument;
12575 cp_token *token;
12576 const char *saved_message;
12578 /* In a template parameter, `>' is not an operator.
12580 [temp.param]
12582 When parsing a default template-argument for a non-type
12583 template-parameter, the first non-nested `>' is taken as the end
12584 of the template parameter-list rather than a greater-than
12585 operator. */
12586 greater_than_is_operator_p = !template_parm_p;
12588 /* Type definitions may not appear in parameter types. */
12589 saved_message = parser->type_definition_forbidden_message;
12590 parser->type_definition_forbidden_message
12591 = "types may not be defined in parameter types";
12593 /* Parse the declaration-specifiers. */
12594 cp_parser_decl_specifier_seq (parser,
12595 CP_PARSER_FLAGS_NONE,
12596 &decl_specifiers,
12597 &declares_class_or_enum);
12598 /* If an error occurred, there's no reason to attempt to parse the
12599 rest of the declaration. */
12600 if (cp_parser_error_occurred (parser))
12602 parser->type_definition_forbidden_message = saved_message;
12603 return NULL;
12606 /* Peek at the next token. */
12607 token = cp_lexer_peek_token (parser->lexer);
12608 /* If the next token is a `)', `,', `=', `>', or `...', then there
12609 is no declarator. */
12610 if (token->type == CPP_CLOSE_PAREN
12611 || token->type == CPP_COMMA
12612 || token->type == CPP_EQ
12613 || token->type == CPP_ELLIPSIS
12614 || token->type == CPP_GREATER)
12616 declarator = NULL;
12617 if (parenthesized_p)
12618 *parenthesized_p = false;
12620 /* Otherwise, there should be a declarator. */
12621 else
12623 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12624 parser->default_arg_ok_p = false;
12626 /* After seeing a decl-specifier-seq, if the next token is not a
12627 "(", there is no possibility that the code is a valid
12628 expression. Therefore, if parsing tentatively, we commit at
12629 this point. */
12630 if (!parser->in_template_argument_list_p
12631 /* In an expression context, having seen:
12633 (int((char ...
12635 we cannot be sure whether we are looking at a
12636 function-type (taking a "char" as a parameter) or a cast
12637 of some object of type "char" to "int". */
12638 && !parser->in_type_id_in_expr_p
12639 && cp_parser_uncommitted_to_tentative_parse_p (parser)
12640 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
12641 cp_parser_commit_to_tentative_parse (parser);
12642 /* Parse the declarator. */
12643 declarator = cp_parser_declarator (parser,
12644 CP_PARSER_DECLARATOR_EITHER,
12645 /*ctor_dtor_or_conv_p=*/NULL,
12646 parenthesized_p,
12647 /*member_p=*/false);
12648 parser->default_arg_ok_p = saved_default_arg_ok_p;
12649 /* After the declarator, allow more attributes. */
12650 decl_specifiers.attributes
12651 = chainon (decl_specifiers.attributes,
12652 cp_parser_attributes_opt (parser));
12655 /* The restriction on defining new types applies only to the type
12656 of the parameter, not to the default argument. */
12657 parser->type_definition_forbidden_message = saved_message;
12659 /* If the next token is `=', then process a default argument. */
12660 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12662 bool saved_greater_than_is_operator_p;
12663 /* Consume the `='. */
12664 cp_lexer_consume_token (parser->lexer);
12666 /* If we are defining a class, then the tokens that make up the
12667 default argument must be saved and processed later. */
12668 if (!template_parm_p && at_class_scope_p ()
12669 && TYPE_BEING_DEFINED (current_class_type))
12671 unsigned depth = 0;
12672 cp_token *first_token;
12673 cp_token *token;
12675 /* Add tokens until we have processed the entire default
12676 argument. We add the range [first_token, token). */
12677 first_token = cp_lexer_peek_token (parser->lexer);
12678 while (true)
12680 bool done = false;
12682 /* Peek at the next token. */
12683 token = cp_lexer_peek_token (parser->lexer);
12684 /* What we do depends on what token we have. */
12685 switch (token->type)
12687 /* In valid code, a default argument must be
12688 immediately followed by a `,' `)', or `...'. */
12689 case CPP_COMMA:
12690 case CPP_CLOSE_PAREN:
12691 case CPP_ELLIPSIS:
12692 /* If we run into a non-nested `;', `}', or `]',
12693 then the code is invalid -- but the default
12694 argument is certainly over. */
12695 case CPP_SEMICOLON:
12696 case CPP_CLOSE_BRACE:
12697 case CPP_CLOSE_SQUARE:
12698 if (depth == 0)
12699 done = true;
12700 /* Update DEPTH, if necessary. */
12701 else if (token->type == CPP_CLOSE_PAREN
12702 || token->type == CPP_CLOSE_BRACE
12703 || token->type == CPP_CLOSE_SQUARE)
12704 --depth;
12705 break;
12707 case CPP_OPEN_PAREN:
12708 case CPP_OPEN_SQUARE:
12709 case CPP_OPEN_BRACE:
12710 ++depth;
12711 break;
12713 case CPP_GREATER:
12714 /* If we see a non-nested `>', and `>' is not an
12715 operator, then it marks the end of the default
12716 argument. */
12717 if (!depth && !greater_than_is_operator_p)
12718 done = true;
12719 break;
12721 /* If we run out of tokens, issue an error message. */
12722 case CPP_EOF:
12723 case CPP_PRAGMA_EOL:
12724 error ("file ends in default argument");
12725 done = true;
12726 break;
12728 case CPP_NAME:
12729 case CPP_SCOPE:
12730 /* In these cases, we should look for template-ids.
12731 For example, if the default argument is
12732 `X<int, double>()', we need to do name lookup to
12733 figure out whether or not `X' is a template; if
12734 so, the `,' does not end the default argument.
12736 That is not yet done. */
12737 break;
12739 default:
12740 break;
12743 /* If we've reached the end, stop. */
12744 if (done)
12745 break;
12747 /* Add the token to the token block. */
12748 token = cp_lexer_consume_token (parser->lexer);
12751 /* Create a DEFAULT_ARG to represented the unparsed default
12752 argument. */
12753 default_argument = make_node (DEFAULT_ARG);
12754 DEFARG_TOKENS (default_argument)
12755 = cp_token_cache_new (first_token, token);
12756 DEFARG_INSTANTIATIONS (default_argument) = NULL;
12758 /* Outside of a class definition, we can just parse the
12759 assignment-expression. */
12760 else
12762 bool saved_local_variables_forbidden_p;
12764 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
12765 set correctly. */
12766 saved_greater_than_is_operator_p
12767 = parser->greater_than_is_operator_p;
12768 parser->greater_than_is_operator_p = greater_than_is_operator_p;
12769 /* Local variable names (and the `this' keyword) may not
12770 appear in a default argument. */
12771 saved_local_variables_forbidden_p
12772 = parser->local_variables_forbidden_p;
12773 parser->local_variables_forbidden_p = true;
12774 /* The default argument expression may cause implicitly
12775 defined member functions to be synthesized, which will
12776 result in garbage collection. We must treat this
12777 situation as if we were within the body of function so as
12778 to avoid collecting live data on the stack. */
12779 ++function_depth;
12780 /* Parse the assignment-expression. */
12781 if (template_parm_p)
12782 push_deferring_access_checks (dk_no_deferred);
12783 default_argument
12784 = cp_parser_assignment_expression (parser, /*cast_p=*/false);
12785 if (template_parm_p)
12786 pop_deferring_access_checks ();
12787 /* Restore saved state. */
12788 --function_depth;
12789 parser->greater_than_is_operator_p
12790 = saved_greater_than_is_operator_p;
12791 parser->local_variables_forbidden_p
12792 = saved_local_variables_forbidden_p;
12794 if (!parser->default_arg_ok_p)
12796 if (!flag_pedantic_errors)
12797 warning (0, "deprecated use of default argument for parameter of non-function");
12798 else
12800 error ("default arguments are only permitted for function parameters");
12801 default_argument = NULL_TREE;
12805 else
12806 default_argument = NULL_TREE;
12808 return make_parameter_declarator (&decl_specifiers,
12809 declarator,
12810 default_argument);
12813 /* Parse a function-body.
12815 function-body:
12816 compound_statement */
12818 static void
12819 cp_parser_function_body (cp_parser *parser)
12821 cp_parser_compound_statement (parser, NULL, false);
12824 /* Parse a ctor-initializer-opt followed by a function-body. Return
12825 true if a ctor-initializer was present. */
12827 static bool
12828 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
12830 tree body;
12831 bool ctor_initializer_p;
12833 /* Begin the function body. */
12834 body = begin_function_body ();
12835 /* Parse the optional ctor-initializer. */
12836 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
12837 /* Parse the function-body. */
12838 cp_parser_function_body (parser);
12839 /* Finish the function body. */
12840 finish_function_body (body);
12842 return ctor_initializer_p;
12845 /* Parse an initializer.
12847 initializer:
12848 = initializer-clause
12849 ( expression-list )
12851 Returns an expression representing the initializer. If no
12852 initializer is present, NULL_TREE is returned.
12854 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12855 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
12856 set to FALSE if there is no initializer present. If there is an
12857 initializer, and it is not a constant-expression, *NON_CONSTANT_P
12858 is set to true; otherwise it is set to false. */
12860 static tree
12861 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
12862 bool* non_constant_p)
12864 cp_token *token;
12865 tree init;
12867 /* Peek at the next token. */
12868 token = cp_lexer_peek_token (parser->lexer);
12870 /* Let our caller know whether or not this initializer was
12871 parenthesized. */
12872 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
12873 /* Assume that the initializer is constant. */
12874 *non_constant_p = false;
12876 if (token->type == CPP_EQ)
12878 /* Consume the `='. */
12879 cp_lexer_consume_token (parser->lexer);
12880 /* Parse the initializer-clause. */
12881 init = cp_parser_initializer_clause (parser, non_constant_p);
12883 else if (token->type == CPP_OPEN_PAREN)
12884 init = cp_parser_parenthesized_expression_list (parser, false,
12885 /*cast_p=*/false,
12886 non_constant_p);
12887 else
12889 /* Anything else is an error. */
12890 cp_parser_error (parser, "expected initializer");
12891 init = error_mark_node;
12894 return init;
12897 /* Parse an initializer-clause.
12899 initializer-clause:
12900 assignment-expression
12901 { initializer-list , [opt] }
12904 Returns an expression representing the initializer.
12906 If the `assignment-expression' production is used the value
12907 returned is simply a representation for the expression.
12909 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
12910 the elements of the initializer-list (or NULL, if the last
12911 production is used). The TREE_TYPE for the CONSTRUCTOR will be
12912 NULL_TREE. There is no way to detect whether or not the optional
12913 trailing `,' was provided. NON_CONSTANT_P is as for
12914 cp_parser_initializer. */
12916 static tree
12917 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
12919 tree initializer;
12921 /* Assume the expression is constant. */
12922 *non_constant_p = false;
12924 /* If it is not a `{', then we are looking at an
12925 assignment-expression. */
12926 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12928 initializer
12929 = cp_parser_constant_expression (parser,
12930 /*allow_non_constant_p=*/true,
12931 non_constant_p);
12932 if (!*non_constant_p)
12933 initializer = fold_non_dependent_expr (initializer);
12935 else
12937 /* Consume the `{' token. */
12938 cp_lexer_consume_token (parser->lexer);
12939 /* Create a CONSTRUCTOR to represent the braced-initializer. */
12940 initializer = make_node (CONSTRUCTOR);
12941 /* If it's not a `}', then there is a non-trivial initializer. */
12942 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12944 /* Parse the initializer list. */
12945 CONSTRUCTOR_ELTS (initializer)
12946 = cp_parser_initializer_list (parser, non_constant_p);
12947 /* A trailing `,' token is allowed. */
12948 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12949 cp_lexer_consume_token (parser->lexer);
12951 /* Now, there should be a trailing `}'. */
12952 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12955 return initializer;
12958 /* Parse an initializer-list.
12960 initializer-list:
12961 initializer-clause
12962 initializer-list , initializer-clause
12964 GNU Extension:
12966 initializer-list:
12967 identifier : initializer-clause
12968 initializer-list, identifier : initializer-clause
12970 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
12971 for the initializer. If the INDEX of the elt is non-NULL, it is the
12972 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
12973 as for cp_parser_initializer. */
12975 static VEC(constructor_elt,gc) *
12976 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12978 VEC(constructor_elt,gc) *v = NULL;
12980 /* Assume all of the expressions are constant. */
12981 *non_constant_p = false;
12983 /* Parse the rest of the list. */
12984 while (true)
12986 cp_token *token;
12987 tree identifier;
12988 tree initializer;
12989 bool clause_non_constant_p;
12991 /* If the next token is an identifier and the following one is a
12992 colon, we are looking at the GNU designated-initializer
12993 syntax. */
12994 if (cp_parser_allow_gnu_extensions_p (parser)
12995 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12996 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12998 /* Warn the user that they are using an extension. */
12999 if (pedantic)
13000 pedwarn ("ISO C++ does not allow designated initializers");
13001 /* Consume the identifier. */
13002 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
13003 /* Consume the `:'. */
13004 cp_lexer_consume_token (parser->lexer);
13006 else
13007 identifier = NULL_TREE;
13009 /* Parse the initializer. */
13010 initializer = cp_parser_initializer_clause (parser,
13011 &clause_non_constant_p);
13012 /* If any clause is non-constant, so is the entire initializer. */
13013 if (clause_non_constant_p)
13014 *non_constant_p = true;
13016 /* Add it to the vector. */
13017 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
13019 /* If the next token is not a comma, we have reached the end of
13020 the list. */
13021 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13022 break;
13024 /* Peek at the next token. */
13025 token = cp_lexer_peek_nth_token (parser->lexer, 2);
13026 /* If the next token is a `}', then we're still done. An
13027 initializer-clause can have a trailing `,' after the
13028 initializer-list and before the closing `}'. */
13029 if (token->type == CPP_CLOSE_BRACE)
13030 break;
13032 /* Consume the `,' token. */
13033 cp_lexer_consume_token (parser->lexer);
13036 return v;
13039 /* Classes [gram.class] */
13041 /* Parse a class-name.
13043 class-name:
13044 identifier
13045 template-id
13047 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
13048 to indicate that names looked up in dependent types should be
13049 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
13050 keyword has been used to indicate that the name that appears next
13051 is a template. TAG_TYPE indicates the explicit tag given before
13052 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
13053 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
13054 is the class being defined in a class-head.
13056 Returns the TYPE_DECL representing the class. */
13058 static tree
13059 cp_parser_class_name (cp_parser *parser,
13060 bool typename_keyword_p,
13061 bool template_keyword_p,
13062 enum tag_types tag_type,
13063 bool check_dependency_p,
13064 bool class_head_p,
13065 bool is_declaration)
13067 tree decl;
13068 tree scope;
13069 bool typename_p;
13070 cp_token *token;
13072 /* All class-names start with an identifier. */
13073 token = cp_lexer_peek_token (parser->lexer);
13074 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
13076 cp_parser_error (parser, "expected class-name");
13077 return error_mark_node;
13080 /* PARSER->SCOPE can be cleared when parsing the template-arguments
13081 to a template-id, so we save it here. */
13082 scope = parser->scope;
13083 if (scope == error_mark_node)
13084 return error_mark_node;
13086 /* Any name names a type if we're following the `typename' keyword
13087 in a qualified name where the enclosing scope is type-dependent. */
13088 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
13089 && dependent_type_p (scope));
13090 /* Handle the common case (an identifier, but not a template-id)
13091 efficiently. */
13092 if (token->type == CPP_NAME
13093 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
13095 cp_token *identifier_token;
13096 tree identifier;
13097 bool ambiguous_p;
13099 /* Look for the identifier. */
13100 identifier_token = cp_lexer_peek_token (parser->lexer);
13101 ambiguous_p = identifier_token->ambiguous_p;
13102 identifier = cp_parser_identifier (parser);
13103 /* If the next token isn't an identifier, we are certainly not
13104 looking at a class-name. */
13105 if (identifier == error_mark_node)
13106 decl = error_mark_node;
13107 /* If we know this is a type-name, there's no need to look it
13108 up. */
13109 else if (typename_p)
13110 decl = identifier;
13111 else
13113 tree ambiguous_decls;
13114 /* If we already know that this lookup is ambiguous, then
13115 we've already issued an error message; there's no reason
13116 to check again. */
13117 if (ambiguous_p)
13119 cp_parser_simulate_error (parser);
13120 return error_mark_node;
13122 /* If the next token is a `::', then the name must be a type
13123 name.
13125 [basic.lookup.qual]
13127 During the lookup for a name preceding the :: scope
13128 resolution operator, object, function, and enumerator
13129 names are ignored. */
13130 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13131 tag_type = typename_type;
13132 /* Look up the name. */
13133 decl = cp_parser_lookup_name (parser, identifier,
13134 tag_type,
13135 /*is_template=*/false,
13136 /*is_namespace=*/false,
13137 check_dependency_p,
13138 &ambiguous_decls);
13139 if (ambiguous_decls)
13141 error ("reference to %qD is ambiguous", identifier);
13142 print_candidates (ambiguous_decls);
13143 if (cp_parser_parsing_tentatively (parser))
13145 identifier_token->ambiguous_p = true;
13146 cp_parser_simulate_error (parser);
13148 return error_mark_node;
13152 else
13154 /* Try a template-id. */
13155 decl = cp_parser_template_id (parser, template_keyword_p,
13156 check_dependency_p,
13157 is_declaration);
13158 if (decl == error_mark_node)
13159 return error_mark_node;
13162 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
13164 /* If this is a typename, create a TYPENAME_TYPE. */
13165 if (typename_p && decl != error_mark_node)
13167 decl = make_typename_type (scope, decl, typename_type,
13168 /*complain=*/tf_error);
13169 if (decl != error_mark_node)
13170 decl = TYPE_NAME (decl);
13173 /* Check to see that it is really the name of a class. */
13174 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
13175 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
13176 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13177 /* Situations like this:
13179 template <typename T> struct A {
13180 typename T::template X<int>::I i;
13183 are problematic. Is `T::template X<int>' a class-name? The
13184 standard does not seem to be definitive, but there is no other
13185 valid interpretation of the following `::'. Therefore, those
13186 names are considered class-names. */
13188 decl = make_typename_type (scope, decl, tag_type, tf_error);
13189 if (decl != error_mark_node)
13190 decl = TYPE_NAME (decl);
13192 else if (TREE_CODE (decl) != TYPE_DECL
13193 || TREE_TYPE (decl) == error_mark_node
13194 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
13195 decl = error_mark_node;
13197 if (decl == error_mark_node)
13198 cp_parser_error (parser, "expected class-name");
13200 return decl;
13203 /* Parse a class-specifier.
13205 class-specifier:
13206 class-head { member-specification [opt] }
13208 Returns the TREE_TYPE representing the class. */
13210 static tree
13211 cp_parser_class_specifier (cp_parser* parser)
13213 cp_token *token;
13214 tree type;
13215 tree attributes = NULL_TREE;
13216 int has_trailing_semicolon;
13217 bool nested_name_specifier_p;
13218 unsigned saved_num_template_parameter_lists;
13219 bool saved_in_function_body;
13220 tree old_scope = NULL_TREE;
13221 tree scope = NULL_TREE;
13222 tree bases;
13224 push_deferring_access_checks (dk_no_deferred);
13226 /* Parse the class-head. */
13227 type = cp_parser_class_head (parser,
13228 &nested_name_specifier_p,
13229 &attributes,
13230 &bases);
13231 /* If the class-head was a semantic disaster, skip the entire body
13232 of the class. */
13233 if (!type)
13235 cp_parser_skip_to_end_of_block_or_statement (parser);
13236 pop_deferring_access_checks ();
13237 return error_mark_node;
13240 /* Look for the `{'. */
13241 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
13243 pop_deferring_access_checks ();
13244 return error_mark_node;
13247 /* Process the base classes. If they're invalid, skip the
13248 entire class body. */
13249 if (!xref_basetypes (type, bases))
13251 cp_parser_skip_to_closing_brace (parser);
13253 /* Consuming the closing brace yields better error messages
13254 later on. */
13255 cp_lexer_consume_token (parser->lexer);
13256 pop_deferring_access_checks ();
13257 return error_mark_node;
13260 /* Issue an error message if type-definitions are forbidden here. */
13261 cp_parser_check_type_definition (parser);
13262 /* Remember that we are defining one more class. */
13263 ++parser->num_classes_being_defined;
13264 /* Inside the class, surrounding template-parameter-lists do not
13265 apply. */
13266 saved_num_template_parameter_lists
13267 = parser->num_template_parameter_lists;
13268 parser->num_template_parameter_lists = 0;
13269 /* We are not in a function body. */
13270 saved_in_function_body = parser->in_function_body;
13271 parser->in_function_body = false;
13273 /* Start the class. */
13274 if (nested_name_specifier_p)
13276 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
13277 old_scope = push_inner_scope (scope);
13279 type = begin_class_definition (type, attributes);
13281 if (type == error_mark_node)
13282 /* If the type is erroneous, skip the entire body of the class. */
13283 cp_parser_skip_to_closing_brace (parser);
13284 else
13285 /* Parse the member-specification. */
13286 cp_parser_member_specification_opt (parser);
13288 /* Look for the trailing `}'. */
13289 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13290 /* We get better error messages by noticing a common problem: a
13291 missing trailing `;'. */
13292 token = cp_lexer_peek_token (parser->lexer);
13293 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
13294 /* Look for trailing attributes to apply to this class. */
13295 if (cp_parser_allow_gnu_extensions_p (parser))
13296 attributes = cp_parser_attributes_opt (parser);
13297 if (type != error_mark_node)
13298 type = finish_struct (type, attributes);
13299 if (nested_name_specifier_p)
13300 pop_inner_scope (old_scope, scope);
13301 /* If this class is not itself within the scope of another class,
13302 then we need to parse the bodies of all of the queued function
13303 definitions. Note that the queued functions defined in a class
13304 are not always processed immediately following the
13305 class-specifier for that class. Consider:
13307 struct A {
13308 struct B { void f() { sizeof (A); } };
13311 If `f' were processed before the processing of `A' were
13312 completed, there would be no way to compute the size of `A'.
13313 Note that the nesting we are interested in here is lexical --
13314 not the semantic nesting given by TYPE_CONTEXT. In particular,
13315 for:
13317 struct A { struct B; };
13318 struct A::B { void f() { } };
13320 there is no need to delay the parsing of `A::B::f'. */
13321 if (--parser->num_classes_being_defined == 0)
13323 tree queue_entry;
13324 tree fn;
13325 tree class_type = NULL_TREE;
13326 tree pushed_scope = NULL_TREE;
13328 /* In a first pass, parse default arguments to the functions.
13329 Then, in a second pass, parse the bodies of the functions.
13330 This two-phased approach handles cases like:
13332 struct S {
13333 void f() { g(); }
13334 void g(int i = 3);
13338 for (TREE_PURPOSE (parser->unparsed_functions_queues)
13339 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
13340 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
13341 TREE_PURPOSE (parser->unparsed_functions_queues)
13342 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
13344 fn = TREE_VALUE (queue_entry);
13345 /* If there are default arguments that have not yet been processed,
13346 take care of them now. */
13347 if (class_type != TREE_PURPOSE (queue_entry))
13349 if (pushed_scope)
13350 pop_scope (pushed_scope);
13351 class_type = TREE_PURPOSE (queue_entry);
13352 pushed_scope = push_scope (class_type);
13354 /* Make sure that any template parameters are in scope. */
13355 maybe_begin_member_template_processing (fn);
13356 /* Parse the default argument expressions. */
13357 cp_parser_late_parsing_default_args (parser, fn);
13358 /* Remove any template parameters from the symbol table. */
13359 maybe_end_member_template_processing ();
13361 if (pushed_scope)
13362 pop_scope (pushed_scope);
13363 /* Now parse the body of the functions. */
13364 for (TREE_VALUE (parser->unparsed_functions_queues)
13365 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
13366 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
13367 TREE_VALUE (parser->unparsed_functions_queues)
13368 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
13370 /* Figure out which function we need to process. */
13371 fn = TREE_VALUE (queue_entry);
13372 /* Parse the function. */
13373 cp_parser_late_parsing_for_member (parser, fn);
13377 /* Put back any saved access checks. */
13378 pop_deferring_access_checks ();
13380 /* Restore saved state. */
13381 parser->in_function_body = saved_in_function_body;
13382 parser->num_template_parameter_lists
13383 = saved_num_template_parameter_lists;
13385 return type;
13388 /* Parse a class-head.
13390 class-head:
13391 class-key identifier [opt] base-clause [opt]
13392 class-key nested-name-specifier identifier base-clause [opt]
13393 class-key nested-name-specifier [opt] template-id
13394 base-clause [opt]
13396 GNU Extensions:
13397 class-key attributes identifier [opt] base-clause [opt]
13398 class-key attributes nested-name-specifier identifier base-clause [opt]
13399 class-key attributes nested-name-specifier [opt] template-id
13400 base-clause [opt]
13402 Upon return BASES is initialized to the list of base classes (or
13403 NULL, if there are none) in the same form returned by
13404 cp_parser_base_clause.
13406 Returns the TYPE of the indicated class. Sets
13407 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
13408 involving a nested-name-specifier was used, and FALSE otherwise.
13410 Returns error_mark_node if this is not a class-head.
13412 Returns NULL_TREE if the class-head is syntactically valid, but
13413 semantically invalid in a way that means we should skip the entire
13414 body of the class. */
13416 static tree
13417 cp_parser_class_head (cp_parser* parser,
13418 bool* nested_name_specifier_p,
13419 tree *attributes_p,
13420 tree *bases)
13422 tree nested_name_specifier;
13423 enum tag_types class_key;
13424 tree id = NULL_TREE;
13425 tree type = NULL_TREE;
13426 tree attributes;
13427 bool template_id_p = false;
13428 bool qualified_p = false;
13429 bool invalid_nested_name_p = false;
13430 bool invalid_explicit_specialization_p = false;
13431 tree pushed_scope = NULL_TREE;
13432 unsigned num_templates;
13434 /* Assume no nested-name-specifier will be present. */
13435 *nested_name_specifier_p = false;
13436 /* Assume no template parameter lists will be used in defining the
13437 type. */
13438 num_templates = 0;
13440 *bases = NULL_TREE;
13442 /* Look for the class-key. */
13443 class_key = cp_parser_class_key (parser);
13444 if (class_key == none_type)
13445 return error_mark_node;
13447 /* Parse the attributes. */
13448 attributes = cp_parser_attributes_opt (parser);
13450 /* If the next token is `::', that is invalid -- but sometimes
13451 people do try to write:
13453 struct ::S {};
13455 Handle this gracefully by accepting the extra qualifier, and then
13456 issuing an error about it later if this really is a
13457 class-head. If it turns out just to be an elaborated type
13458 specifier, remain silent. */
13459 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
13460 qualified_p = true;
13462 push_deferring_access_checks (dk_no_check);
13464 /* Determine the name of the class. Begin by looking for an
13465 optional nested-name-specifier. */
13466 nested_name_specifier
13467 = cp_parser_nested_name_specifier_opt (parser,
13468 /*typename_keyword_p=*/false,
13469 /*check_dependency_p=*/false,
13470 /*type_p=*/false,
13471 /*is_declaration=*/false);
13472 /* If there was a nested-name-specifier, then there *must* be an
13473 identifier. */
13474 if (nested_name_specifier)
13476 /* Although the grammar says `identifier', it really means
13477 `class-name' or `template-name'. You are only allowed to
13478 define a class that has already been declared with this
13479 syntax.
13481 The proposed resolution for Core Issue 180 says that wherever
13482 you see `class T::X' you should treat `X' as a type-name.
13484 It is OK to define an inaccessible class; for example:
13486 class A { class B; };
13487 class A::B {};
13489 We do not know if we will see a class-name, or a
13490 template-name. We look for a class-name first, in case the
13491 class-name is a template-id; if we looked for the
13492 template-name first we would stop after the template-name. */
13493 cp_parser_parse_tentatively (parser);
13494 type = cp_parser_class_name (parser,
13495 /*typename_keyword_p=*/false,
13496 /*template_keyword_p=*/false,
13497 class_type,
13498 /*check_dependency_p=*/false,
13499 /*class_head_p=*/true,
13500 /*is_declaration=*/false);
13501 /* If that didn't work, ignore the nested-name-specifier. */
13502 if (!cp_parser_parse_definitely (parser))
13504 invalid_nested_name_p = true;
13505 id = cp_parser_identifier (parser);
13506 if (id == error_mark_node)
13507 id = NULL_TREE;
13509 /* If we could not find a corresponding TYPE, treat this
13510 declaration like an unqualified declaration. */
13511 if (type == error_mark_node)
13512 nested_name_specifier = NULL_TREE;
13513 /* Otherwise, count the number of templates used in TYPE and its
13514 containing scopes. */
13515 else
13517 tree scope;
13519 for (scope = TREE_TYPE (type);
13520 scope && TREE_CODE (scope) != NAMESPACE_DECL;
13521 scope = (TYPE_P (scope)
13522 ? TYPE_CONTEXT (scope)
13523 : DECL_CONTEXT (scope)))
13524 if (TYPE_P (scope)
13525 && CLASS_TYPE_P (scope)
13526 && CLASSTYPE_TEMPLATE_INFO (scope)
13527 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
13528 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
13529 ++num_templates;
13532 /* Otherwise, the identifier is optional. */
13533 else
13535 /* We don't know whether what comes next is a template-id,
13536 an identifier, or nothing at all. */
13537 cp_parser_parse_tentatively (parser);
13538 /* Check for a template-id. */
13539 id = cp_parser_template_id (parser,
13540 /*template_keyword_p=*/false,
13541 /*check_dependency_p=*/true,
13542 /*is_declaration=*/true);
13543 /* If that didn't work, it could still be an identifier. */
13544 if (!cp_parser_parse_definitely (parser))
13546 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13547 id = cp_parser_identifier (parser);
13548 else
13549 id = NULL_TREE;
13551 else
13553 template_id_p = true;
13554 ++num_templates;
13558 pop_deferring_access_checks ();
13560 if (id)
13561 cp_parser_check_for_invalid_template_id (parser, id);
13563 /* If it's not a `:' or a `{' then we can't really be looking at a
13564 class-head, since a class-head only appears as part of a
13565 class-specifier. We have to detect this situation before calling
13566 xref_tag, since that has irreversible side-effects. */
13567 if (!cp_parser_next_token_starts_class_definition_p (parser))
13569 cp_parser_error (parser, "expected %<{%> or %<:%>");
13570 return error_mark_node;
13573 /* At this point, we're going ahead with the class-specifier, even
13574 if some other problem occurs. */
13575 cp_parser_commit_to_tentative_parse (parser);
13576 /* Issue the error about the overly-qualified name now. */
13577 if (qualified_p)
13578 cp_parser_error (parser,
13579 "global qualification of class name is invalid");
13580 else if (invalid_nested_name_p)
13581 cp_parser_error (parser,
13582 "qualified name does not name a class");
13583 else if (nested_name_specifier)
13585 tree scope;
13587 /* Reject typedef-names in class heads. */
13588 if (!DECL_IMPLICIT_TYPEDEF_P (type))
13590 error ("invalid class name in declaration of %qD", type);
13591 type = NULL_TREE;
13592 goto done;
13595 /* Figure out in what scope the declaration is being placed. */
13596 scope = current_scope ();
13597 /* If that scope does not contain the scope in which the
13598 class was originally declared, the program is invalid. */
13599 if (scope && !is_ancestor (scope, nested_name_specifier))
13601 error ("declaration of %qD in %qD which does not enclose %qD",
13602 type, scope, nested_name_specifier);
13603 type = NULL_TREE;
13604 goto done;
13606 /* [dcl.meaning]
13608 A declarator-id shall not be qualified exception of the
13609 definition of a ... nested class outside of its class
13610 ... [or] a the definition or explicit instantiation of a
13611 class member of a namespace outside of its namespace. */
13612 if (scope == nested_name_specifier)
13614 pedwarn ("extra qualification ignored");
13615 nested_name_specifier = NULL_TREE;
13616 num_templates = 0;
13619 /* An explicit-specialization must be preceded by "template <>". If
13620 it is not, try to recover gracefully. */
13621 if (at_namespace_scope_p ()
13622 && parser->num_template_parameter_lists == 0
13623 && template_id_p)
13625 error ("an explicit specialization must be preceded by %<template <>%>");
13626 invalid_explicit_specialization_p = true;
13627 /* Take the same action that would have been taken by
13628 cp_parser_explicit_specialization. */
13629 ++parser->num_template_parameter_lists;
13630 begin_specialization ();
13632 /* There must be no "return" statements between this point and the
13633 end of this function; set "type "to the correct return value and
13634 use "goto done;" to return. */
13635 /* Make sure that the right number of template parameters were
13636 present. */
13637 if (!cp_parser_check_template_parameters (parser, num_templates))
13639 /* If something went wrong, there is no point in even trying to
13640 process the class-definition. */
13641 type = NULL_TREE;
13642 goto done;
13645 /* Look up the type. */
13646 if (template_id_p)
13648 type = TREE_TYPE (id);
13649 type = maybe_process_partial_specialization (type);
13650 if (nested_name_specifier)
13651 pushed_scope = push_scope (nested_name_specifier);
13653 else if (nested_name_specifier)
13655 tree class_type;
13657 /* Given:
13659 template <typename T> struct S { struct T };
13660 template <typename T> struct S<T>::T { };
13662 we will get a TYPENAME_TYPE when processing the definition of
13663 `S::T'. We need to resolve it to the actual type before we
13664 try to define it. */
13665 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
13667 class_type = resolve_typename_type (TREE_TYPE (type),
13668 /*only_current_p=*/false);
13669 if (class_type != error_mark_node)
13670 type = TYPE_NAME (class_type);
13671 else
13673 cp_parser_error (parser, "could not resolve typename type");
13674 type = error_mark_node;
13678 maybe_process_partial_specialization (TREE_TYPE (type));
13679 class_type = current_class_type;
13680 /* Enter the scope indicated by the nested-name-specifier. */
13681 pushed_scope = push_scope (nested_name_specifier);
13682 /* Get the canonical version of this type. */
13683 type = TYPE_MAIN_DECL (TREE_TYPE (type));
13684 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13685 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
13687 type = push_template_decl (type);
13688 if (type == error_mark_node)
13690 type = NULL_TREE;
13691 goto done;
13695 type = TREE_TYPE (type);
13696 *nested_name_specifier_p = true;
13698 else /* The name is not a nested name. */
13700 /* If the class was unnamed, create a dummy name. */
13701 if (!id)
13702 id = make_anon_name ();
13703 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
13704 parser->num_template_parameter_lists);
13707 /* Indicate whether this class was declared as a `class' or as a
13708 `struct'. */
13709 if (TREE_CODE (type) == RECORD_TYPE)
13710 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
13711 cp_parser_check_class_key (class_key, type);
13713 /* If this type was already complete, and we see another definition,
13714 that's an error. */
13715 if (type != error_mark_node && COMPLETE_TYPE_P (type))
13717 error ("redefinition of %q#T", type);
13718 error ("previous definition of %q+#T", type);
13719 type = NULL_TREE;
13720 goto done;
13722 else if (type == error_mark_node)
13723 type = NULL_TREE;
13725 /* We will have entered the scope containing the class; the names of
13726 base classes should be looked up in that context. For example:
13728 struct A { struct B {}; struct C; };
13729 struct A::C : B {};
13731 is valid. */
13733 /* Get the list of base-classes, if there is one. */
13734 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13735 *bases = cp_parser_base_clause (parser);
13737 done:
13738 /* Leave the scope given by the nested-name-specifier. We will
13739 enter the class scope itself while processing the members. */
13740 if (pushed_scope)
13741 pop_scope (pushed_scope);
13743 if (invalid_explicit_specialization_p)
13745 end_specialization ();
13746 --parser->num_template_parameter_lists;
13748 *attributes_p = attributes;
13749 return type;
13752 /* Parse a class-key.
13754 class-key:
13755 class
13756 struct
13757 union
13759 Returns the kind of class-key specified, or none_type to indicate
13760 error. */
13762 static enum tag_types
13763 cp_parser_class_key (cp_parser* parser)
13765 cp_token *token;
13766 enum tag_types tag_type;
13768 /* Look for the class-key. */
13769 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
13770 if (!token)
13771 return none_type;
13773 /* Check to see if the TOKEN is a class-key. */
13774 tag_type = cp_parser_token_is_class_key (token);
13775 if (!tag_type)
13776 cp_parser_error (parser, "expected class-key");
13777 return tag_type;
13780 /* Parse an (optional) member-specification.
13782 member-specification:
13783 member-declaration member-specification [opt]
13784 access-specifier : member-specification [opt] */
13786 static void
13787 cp_parser_member_specification_opt (cp_parser* parser)
13789 while (true)
13791 cp_token *token;
13792 enum rid keyword;
13794 /* Peek at the next token. */
13795 token = cp_lexer_peek_token (parser->lexer);
13796 /* If it's a `}', or EOF then we've seen all the members. */
13797 if (token->type == CPP_CLOSE_BRACE
13798 || token->type == CPP_EOF
13799 || token->type == CPP_PRAGMA_EOL)
13800 break;
13802 /* See if this token is a keyword. */
13803 keyword = token->keyword;
13804 switch (keyword)
13806 case RID_PUBLIC:
13807 case RID_PROTECTED:
13808 case RID_PRIVATE:
13809 /* Consume the access-specifier. */
13810 cp_lexer_consume_token (parser->lexer);
13811 /* Remember which access-specifier is active. */
13812 current_access_specifier = token->u.value;
13813 /* Look for the `:'. */
13814 cp_parser_require (parser, CPP_COLON, "`:'");
13815 break;
13817 default:
13818 /* Accept #pragmas at class scope. */
13819 if (token->type == CPP_PRAGMA)
13821 cp_parser_pragma (parser, pragma_external);
13822 break;
13825 /* Otherwise, the next construction must be a
13826 member-declaration. */
13827 cp_parser_member_declaration (parser);
13832 /* Parse a member-declaration.
13834 member-declaration:
13835 decl-specifier-seq [opt] member-declarator-list [opt] ;
13836 function-definition ; [opt]
13837 :: [opt] nested-name-specifier template [opt] unqualified-id ;
13838 using-declaration
13839 template-declaration
13841 member-declarator-list:
13842 member-declarator
13843 member-declarator-list , member-declarator
13845 member-declarator:
13846 declarator pure-specifier [opt]
13847 declarator constant-initializer [opt]
13848 identifier [opt] : constant-expression
13850 GNU Extensions:
13852 member-declaration:
13853 __extension__ member-declaration
13855 member-declarator:
13856 declarator attributes [opt] pure-specifier [opt]
13857 declarator attributes [opt] constant-initializer [opt]
13858 identifier [opt] attributes [opt] : constant-expression
13860 C++0x Extensions:
13862 member-declaration:
13863 static_assert-declaration */
13865 static void
13866 cp_parser_member_declaration (cp_parser* parser)
13868 cp_decl_specifier_seq decl_specifiers;
13869 tree prefix_attributes;
13870 tree decl;
13871 int declares_class_or_enum;
13872 bool friend_p;
13873 cp_token *token;
13874 int saved_pedantic;
13876 /* Check for the `__extension__' keyword. */
13877 if (cp_parser_extension_opt (parser, &saved_pedantic))
13879 /* Recurse. */
13880 cp_parser_member_declaration (parser);
13881 /* Restore the old value of the PEDANTIC flag. */
13882 pedantic = saved_pedantic;
13884 return;
13887 /* Check for a template-declaration. */
13888 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13890 /* An explicit specialization here is an error condition, and we
13891 expect the specialization handler to detect and report this. */
13892 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13893 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
13894 cp_parser_explicit_specialization (parser);
13895 else
13896 cp_parser_template_declaration (parser, /*member_p=*/true);
13898 return;
13901 /* Check for a using-declaration. */
13902 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
13904 /* Parse the using-declaration. */
13905 cp_parser_using_declaration (parser,
13906 /*access_declaration_p=*/false);
13907 return;
13910 /* Check for @defs. */
13911 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
13913 tree ivar, member;
13914 tree ivar_chains = cp_parser_objc_defs_expression (parser);
13915 ivar = ivar_chains;
13916 while (ivar)
13918 member = ivar;
13919 ivar = TREE_CHAIN (member);
13920 TREE_CHAIN (member) = NULL_TREE;
13921 finish_member_declaration (member);
13923 return;
13926 /* If the next token is `static_assert' we have a static assertion. */
13927 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
13929 cp_parser_static_assert (parser, /*member_p=*/true);
13930 return;
13933 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
13934 return;
13936 /* Parse the decl-specifier-seq. */
13937 cp_parser_decl_specifier_seq (parser,
13938 CP_PARSER_FLAGS_OPTIONAL,
13939 &decl_specifiers,
13940 &declares_class_or_enum);
13941 prefix_attributes = decl_specifiers.attributes;
13942 decl_specifiers.attributes = NULL_TREE;
13943 /* Check for an invalid type-name. */
13944 if (!decl_specifiers.type
13945 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13946 return;
13947 /* If there is no declarator, then the decl-specifier-seq should
13948 specify a type. */
13949 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13951 /* If there was no decl-specifier-seq, and the next token is a
13952 `;', then we have something like:
13954 struct S { ; };
13956 [class.mem]
13958 Each member-declaration shall declare at least one member
13959 name of the class. */
13960 if (!decl_specifiers.any_specifiers_p)
13962 cp_token *token = cp_lexer_peek_token (parser->lexer);
13963 if (pedantic && !token->in_system_header)
13964 pedwarn ("%Hextra %<;%>", &token->location);
13966 else
13968 tree type;
13970 /* See if this declaration is a friend. */
13971 friend_p = cp_parser_friend_p (&decl_specifiers);
13972 /* If there were decl-specifiers, check to see if there was
13973 a class-declaration. */
13974 type = check_tag_decl (&decl_specifiers);
13975 /* Nested classes have already been added to the class, but
13976 a `friend' needs to be explicitly registered. */
13977 if (friend_p)
13979 /* If the `friend' keyword was present, the friend must
13980 be introduced with a class-key. */
13981 if (!declares_class_or_enum)
13982 error ("a class-key must be used when declaring a friend");
13983 /* In this case:
13985 template <typename T> struct A {
13986 friend struct A<T>::B;
13989 A<T>::B will be represented by a TYPENAME_TYPE, and
13990 therefore not recognized by check_tag_decl. */
13991 if (!type
13992 && decl_specifiers.type
13993 && TYPE_P (decl_specifiers.type))
13994 type = decl_specifiers.type;
13995 if (!type || !TYPE_P (type))
13996 error ("friend declaration does not name a class or "
13997 "function");
13998 else
13999 make_friend_class (current_class_type, type,
14000 /*complain=*/true);
14002 /* If there is no TYPE, an error message will already have
14003 been issued. */
14004 else if (!type || type == error_mark_node)
14006 /* An anonymous aggregate has to be handled specially; such
14007 a declaration really declares a data member (with a
14008 particular type), as opposed to a nested class. */
14009 else if (ANON_AGGR_TYPE_P (type))
14011 /* Remove constructors and such from TYPE, now that we
14012 know it is an anonymous aggregate. */
14013 fixup_anonymous_aggr (type);
14014 /* And make the corresponding data member. */
14015 decl = build_decl (FIELD_DECL, NULL_TREE, type);
14016 /* Add it to the class. */
14017 finish_member_declaration (decl);
14019 else
14020 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
14023 else
14025 /* See if these declarations will be friends. */
14026 friend_p = cp_parser_friend_p (&decl_specifiers);
14028 /* Keep going until we hit the `;' at the end of the
14029 declaration. */
14030 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14032 tree attributes = NULL_TREE;
14033 tree first_attribute;
14035 /* Peek at the next token. */
14036 token = cp_lexer_peek_token (parser->lexer);
14038 /* Check for a bitfield declaration. */
14039 if (token->type == CPP_COLON
14040 || (token->type == CPP_NAME
14041 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
14042 == CPP_COLON))
14044 tree identifier;
14045 tree width;
14047 /* Get the name of the bitfield. Note that we cannot just
14048 check TOKEN here because it may have been invalidated by
14049 the call to cp_lexer_peek_nth_token above. */
14050 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
14051 identifier = cp_parser_identifier (parser);
14052 else
14053 identifier = NULL_TREE;
14055 /* Consume the `:' token. */
14056 cp_lexer_consume_token (parser->lexer);
14057 /* Get the width of the bitfield. */
14058 width
14059 = cp_parser_constant_expression (parser,
14060 /*allow_non_constant=*/false,
14061 NULL);
14063 /* Look for attributes that apply to the bitfield. */
14064 attributes = cp_parser_attributes_opt (parser);
14065 /* Remember which attributes are prefix attributes and
14066 which are not. */
14067 first_attribute = attributes;
14068 /* Combine the attributes. */
14069 attributes = chainon (prefix_attributes, attributes);
14071 /* Create the bitfield declaration. */
14072 decl = grokbitfield (identifier
14073 ? make_id_declarator (NULL_TREE,
14074 identifier,
14075 sfk_none)
14076 : NULL,
14077 &decl_specifiers,
14078 width);
14079 /* Apply the attributes. */
14080 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
14082 else
14084 cp_declarator *declarator;
14085 tree initializer;
14086 tree asm_specification;
14087 int ctor_dtor_or_conv_p;
14089 /* Parse the declarator. */
14090 declarator
14091 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14092 &ctor_dtor_or_conv_p,
14093 /*parenthesized_p=*/NULL,
14094 /*member_p=*/true);
14096 /* If something went wrong parsing the declarator, make sure
14097 that we at least consume some tokens. */
14098 if (declarator == cp_error_declarator)
14100 /* Skip to the end of the statement. */
14101 cp_parser_skip_to_end_of_statement (parser);
14102 /* If the next token is not a semicolon, that is
14103 probably because we just skipped over the body of
14104 a function. So, we consume a semicolon if
14105 present, but do not issue an error message if it
14106 is not present. */
14107 if (cp_lexer_next_token_is (parser->lexer,
14108 CPP_SEMICOLON))
14109 cp_lexer_consume_token (parser->lexer);
14110 return;
14113 if (declares_class_or_enum & 2)
14114 cp_parser_check_for_definition_in_return_type
14115 (declarator, decl_specifiers.type);
14117 /* Look for an asm-specification. */
14118 asm_specification = cp_parser_asm_specification_opt (parser);
14119 /* Look for attributes that apply to the declaration. */
14120 attributes = cp_parser_attributes_opt (parser);
14121 /* Remember which attributes are prefix attributes and
14122 which are not. */
14123 first_attribute = attributes;
14124 /* Combine the attributes. */
14125 attributes = chainon (prefix_attributes, attributes);
14127 /* If it's an `=', then we have a constant-initializer or a
14128 pure-specifier. It is not correct to parse the
14129 initializer before registering the member declaration
14130 since the member declaration should be in scope while
14131 its initializer is processed. However, the rest of the
14132 front end does not yet provide an interface that allows
14133 us to handle this correctly. */
14134 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14136 /* In [class.mem]:
14138 A pure-specifier shall be used only in the declaration of
14139 a virtual function.
14141 A member-declarator can contain a constant-initializer
14142 only if it declares a static member of integral or
14143 enumeration type.
14145 Therefore, if the DECLARATOR is for a function, we look
14146 for a pure-specifier; otherwise, we look for a
14147 constant-initializer. When we call `grokfield', it will
14148 perform more stringent semantics checks. */
14149 if (function_declarator_p (declarator))
14150 initializer = cp_parser_pure_specifier (parser);
14151 else
14152 /* Parse the initializer. */
14153 initializer = cp_parser_constant_initializer (parser);
14155 /* Otherwise, there is no initializer. */
14156 else
14157 initializer = NULL_TREE;
14159 /* See if we are probably looking at a function
14160 definition. We are certainly not looking at a
14161 member-declarator. Calling `grokfield' has
14162 side-effects, so we must not do it unless we are sure
14163 that we are looking at a member-declarator. */
14164 if (cp_parser_token_starts_function_definition_p
14165 (cp_lexer_peek_token (parser->lexer)))
14167 /* The grammar does not allow a pure-specifier to be
14168 used when a member function is defined. (It is
14169 possible that this fact is an oversight in the
14170 standard, since a pure function may be defined
14171 outside of the class-specifier. */
14172 if (initializer)
14173 error ("pure-specifier on function-definition");
14174 decl = cp_parser_save_member_function_body (parser,
14175 &decl_specifiers,
14176 declarator,
14177 attributes);
14178 /* If the member was not a friend, declare it here. */
14179 if (!friend_p)
14180 finish_member_declaration (decl);
14181 /* Peek at the next token. */
14182 token = cp_lexer_peek_token (parser->lexer);
14183 /* If the next token is a semicolon, consume it. */
14184 if (token->type == CPP_SEMICOLON)
14185 cp_lexer_consume_token (parser->lexer);
14186 return;
14188 else
14189 /* Create the declaration. */
14190 decl = grokfield (declarator, &decl_specifiers,
14191 initializer, /*init_const_expr_p=*/true,
14192 asm_specification,
14193 attributes);
14196 /* Reset PREFIX_ATTRIBUTES. */
14197 while (attributes && TREE_CHAIN (attributes) != first_attribute)
14198 attributes = TREE_CHAIN (attributes);
14199 if (attributes)
14200 TREE_CHAIN (attributes) = NULL_TREE;
14202 /* If there is any qualification still in effect, clear it
14203 now; we will be starting fresh with the next declarator. */
14204 parser->scope = NULL_TREE;
14205 parser->qualifying_scope = NULL_TREE;
14206 parser->object_scope = NULL_TREE;
14207 /* If it's a `,', then there are more declarators. */
14208 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14209 cp_lexer_consume_token (parser->lexer);
14210 /* If the next token isn't a `;', then we have a parse error. */
14211 else if (cp_lexer_next_token_is_not (parser->lexer,
14212 CPP_SEMICOLON))
14214 cp_parser_error (parser, "expected %<;%>");
14215 /* Skip tokens until we find a `;'. */
14216 cp_parser_skip_to_end_of_statement (parser);
14218 break;
14221 if (decl)
14223 /* Add DECL to the list of members. */
14224 if (!friend_p)
14225 finish_member_declaration (decl);
14227 if (TREE_CODE (decl) == FUNCTION_DECL)
14228 cp_parser_save_default_args (parser, decl);
14233 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14236 /* Parse a pure-specifier.
14238 pure-specifier:
14241 Returns INTEGER_ZERO_NODE if a pure specifier is found.
14242 Otherwise, ERROR_MARK_NODE is returned. */
14244 static tree
14245 cp_parser_pure_specifier (cp_parser* parser)
14247 cp_token *token;
14249 /* Look for the `=' token. */
14250 if (!cp_parser_require (parser, CPP_EQ, "`='"))
14251 return error_mark_node;
14252 /* Look for the `0' token. */
14253 token = cp_lexer_consume_token (parser->lexer);
14254 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
14255 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
14257 cp_parser_error (parser,
14258 "invalid pure specifier (only `= 0' is allowed)");
14259 cp_parser_skip_to_end_of_statement (parser);
14260 return error_mark_node;
14262 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
14264 error ("templates may not be %<virtual%>");
14265 return error_mark_node;
14268 return integer_zero_node;
14271 /* Parse a constant-initializer.
14273 constant-initializer:
14274 = constant-expression
14276 Returns a representation of the constant-expression. */
14278 static tree
14279 cp_parser_constant_initializer (cp_parser* parser)
14281 /* Look for the `=' token. */
14282 if (!cp_parser_require (parser, CPP_EQ, "`='"))
14283 return error_mark_node;
14285 /* It is invalid to write:
14287 struct S { static const int i = { 7 }; };
14290 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14292 cp_parser_error (parser,
14293 "a brace-enclosed initializer is not allowed here");
14294 /* Consume the opening brace. */
14295 cp_lexer_consume_token (parser->lexer);
14296 /* Skip the initializer. */
14297 cp_parser_skip_to_closing_brace (parser);
14298 /* Look for the trailing `}'. */
14299 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
14301 return error_mark_node;
14304 return cp_parser_constant_expression (parser,
14305 /*allow_non_constant=*/false,
14306 NULL);
14309 /* Derived classes [gram.class.derived] */
14311 /* Parse a base-clause.
14313 base-clause:
14314 : base-specifier-list
14316 base-specifier-list:
14317 base-specifier
14318 base-specifier-list , base-specifier
14320 Returns a TREE_LIST representing the base-classes, in the order in
14321 which they were declared. The representation of each node is as
14322 described by cp_parser_base_specifier.
14324 In the case that no bases are specified, this function will return
14325 NULL_TREE, not ERROR_MARK_NODE. */
14327 static tree
14328 cp_parser_base_clause (cp_parser* parser)
14330 tree bases = NULL_TREE;
14332 /* Look for the `:' that begins the list. */
14333 cp_parser_require (parser, CPP_COLON, "`:'");
14335 /* Scan the base-specifier-list. */
14336 while (true)
14338 cp_token *token;
14339 tree base;
14341 /* Look for the base-specifier. */
14342 base = cp_parser_base_specifier (parser);
14343 /* Add BASE to the front of the list. */
14344 if (base != error_mark_node)
14346 TREE_CHAIN (base) = bases;
14347 bases = base;
14349 /* Peek at the next token. */
14350 token = cp_lexer_peek_token (parser->lexer);
14351 /* If it's not a comma, then the list is complete. */
14352 if (token->type != CPP_COMMA)
14353 break;
14354 /* Consume the `,'. */
14355 cp_lexer_consume_token (parser->lexer);
14358 /* PARSER->SCOPE may still be non-NULL at this point, if the last
14359 base class had a qualified name. However, the next name that
14360 appears is certainly not qualified. */
14361 parser->scope = NULL_TREE;
14362 parser->qualifying_scope = NULL_TREE;
14363 parser->object_scope = NULL_TREE;
14365 return nreverse (bases);
14368 /* Parse a base-specifier.
14370 base-specifier:
14371 :: [opt] nested-name-specifier [opt] class-name
14372 virtual access-specifier [opt] :: [opt] nested-name-specifier
14373 [opt] class-name
14374 access-specifier virtual [opt] :: [opt] nested-name-specifier
14375 [opt] class-name
14377 Returns a TREE_LIST. The TREE_PURPOSE will be one of
14378 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
14379 indicate the specifiers provided. The TREE_VALUE will be a TYPE
14380 (or the ERROR_MARK_NODE) indicating the type that was specified. */
14382 static tree
14383 cp_parser_base_specifier (cp_parser* parser)
14385 cp_token *token;
14386 bool done = false;
14387 bool virtual_p = false;
14388 bool duplicate_virtual_error_issued_p = false;
14389 bool duplicate_access_error_issued_p = false;
14390 bool class_scope_p, template_p;
14391 tree access = access_default_node;
14392 tree type;
14394 /* Process the optional `virtual' and `access-specifier'. */
14395 while (!done)
14397 /* Peek at the next token. */
14398 token = cp_lexer_peek_token (parser->lexer);
14399 /* Process `virtual'. */
14400 switch (token->keyword)
14402 case RID_VIRTUAL:
14403 /* If `virtual' appears more than once, issue an error. */
14404 if (virtual_p && !duplicate_virtual_error_issued_p)
14406 cp_parser_error (parser,
14407 "%<virtual%> specified more than once in base-specified");
14408 duplicate_virtual_error_issued_p = true;
14411 virtual_p = true;
14413 /* Consume the `virtual' token. */
14414 cp_lexer_consume_token (parser->lexer);
14416 break;
14418 case RID_PUBLIC:
14419 case RID_PROTECTED:
14420 case RID_PRIVATE:
14421 /* If more than one access specifier appears, issue an
14422 error. */
14423 if (access != access_default_node
14424 && !duplicate_access_error_issued_p)
14426 cp_parser_error (parser,
14427 "more than one access specifier in base-specified");
14428 duplicate_access_error_issued_p = true;
14431 access = ridpointers[(int) token->keyword];
14433 /* Consume the access-specifier. */
14434 cp_lexer_consume_token (parser->lexer);
14436 break;
14438 default:
14439 done = true;
14440 break;
14443 /* It is not uncommon to see programs mechanically, erroneously, use
14444 the 'typename' keyword to denote (dependent) qualified types
14445 as base classes. */
14446 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14448 if (!processing_template_decl)
14449 error ("keyword %<typename%> not allowed outside of templates");
14450 else
14451 error ("keyword %<typename%> not allowed in this context "
14452 "(the base class is implicitly a type)");
14453 cp_lexer_consume_token (parser->lexer);
14456 /* Look for the optional `::' operator. */
14457 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14458 /* Look for the nested-name-specifier. The simplest way to
14459 implement:
14461 [temp.res]
14463 The keyword `typename' is not permitted in a base-specifier or
14464 mem-initializer; in these contexts a qualified name that
14465 depends on a template-parameter is implicitly assumed to be a
14466 type name.
14468 is to pretend that we have seen the `typename' keyword at this
14469 point. */
14470 cp_parser_nested_name_specifier_opt (parser,
14471 /*typename_keyword_p=*/true,
14472 /*check_dependency_p=*/true,
14473 typename_type,
14474 /*is_declaration=*/true);
14475 /* If the base class is given by a qualified name, assume that names
14476 we see are type names or templates, as appropriate. */
14477 class_scope_p = (parser->scope && TYPE_P (parser->scope));
14478 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
14480 /* Finally, look for the class-name. */
14481 type = cp_parser_class_name (parser,
14482 class_scope_p,
14483 template_p,
14484 typename_type,
14485 /*check_dependency_p=*/true,
14486 /*class_head_p=*/false,
14487 /*is_declaration=*/true);
14489 if (type == error_mark_node)
14490 return error_mark_node;
14492 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
14495 /* Exception handling [gram.exception] */
14497 /* Parse an (optional) exception-specification.
14499 exception-specification:
14500 throw ( type-id-list [opt] )
14502 Returns a TREE_LIST representing the exception-specification. The
14503 TREE_VALUE of each node is a type. */
14505 static tree
14506 cp_parser_exception_specification_opt (cp_parser* parser)
14508 cp_token *token;
14509 tree type_id_list;
14511 /* Peek at the next token. */
14512 token = cp_lexer_peek_token (parser->lexer);
14513 /* If it's not `throw', then there's no exception-specification. */
14514 if (!cp_parser_is_keyword (token, RID_THROW))
14515 return NULL_TREE;
14517 /* Consume the `throw'. */
14518 cp_lexer_consume_token (parser->lexer);
14520 /* Look for the `('. */
14521 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14523 /* Peek at the next token. */
14524 token = cp_lexer_peek_token (parser->lexer);
14525 /* If it's not a `)', then there is a type-id-list. */
14526 if (token->type != CPP_CLOSE_PAREN)
14528 const char *saved_message;
14530 /* Types may not be defined in an exception-specification. */
14531 saved_message = parser->type_definition_forbidden_message;
14532 parser->type_definition_forbidden_message
14533 = "types may not be defined in an exception-specification";
14534 /* Parse the type-id-list. */
14535 type_id_list = cp_parser_type_id_list (parser);
14536 /* Restore the saved message. */
14537 parser->type_definition_forbidden_message = saved_message;
14539 else
14540 type_id_list = empty_except_spec;
14542 /* Look for the `)'. */
14543 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14545 return type_id_list;
14548 /* Parse an (optional) type-id-list.
14550 type-id-list:
14551 type-id
14552 type-id-list , type-id
14554 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
14555 in the order that the types were presented. */
14557 static tree
14558 cp_parser_type_id_list (cp_parser* parser)
14560 tree types = NULL_TREE;
14562 while (true)
14564 cp_token *token;
14565 tree type;
14567 /* Get the next type-id. */
14568 type = cp_parser_type_id (parser);
14569 /* Add it to the list. */
14570 types = add_exception_specifier (types, type, /*complain=*/1);
14571 /* Peek at the next token. */
14572 token = cp_lexer_peek_token (parser->lexer);
14573 /* If it is not a `,', we are done. */
14574 if (token->type != CPP_COMMA)
14575 break;
14576 /* Consume the `,'. */
14577 cp_lexer_consume_token (parser->lexer);
14580 return nreverse (types);
14583 /* Parse a try-block.
14585 try-block:
14586 try compound-statement handler-seq */
14588 static tree
14589 cp_parser_try_block (cp_parser* parser)
14591 tree try_block;
14593 cp_parser_require_keyword (parser, RID_TRY, "`try'");
14594 try_block = begin_try_block ();
14595 cp_parser_compound_statement (parser, NULL, true);
14596 finish_try_block (try_block);
14597 cp_parser_handler_seq (parser);
14598 finish_handler_sequence (try_block);
14600 return try_block;
14603 /* Parse a function-try-block.
14605 function-try-block:
14606 try ctor-initializer [opt] function-body handler-seq */
14608 static bool
14609 cp_parser_function_try_block (cp_parser* parser)
14611 tree compound_stmt;
14612 tree try_block;
14613 bool ctor_initializer_p;
14615 /* Look for the `try' keyword. */
14616 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
14617 return false;
14618 /* Let the rest of the front end know where we are. */
14619 try_block = begin_function_try_block (&compound_stmt);
14620 /* Parse the function-body. */
14621 ctor_initializer_p
14622 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14623 /* We're done with the `try' part. */
14624 finish_function_try_block (try_block);
14625 /* Parse the handlers. */
14626 cp_parser_handler_seq (parser);
14627 /* We're done with the handlers. */
14628 finish_function_handler_sequence (try_block, compound_stmt);
14630 return ctor_initializer_p;
14633 /* Parse a handler-seq.
14635 handler-seq:
14636 handler handler-seq [opt] */
14638 static void
14639 cp_parser_handler_seq (cp_parser* parser)
14641 while (true)
14643 cp_token *token;
14645 /* Parse the handler. */
14646 cp_parser_handler (parser);
14647 /* Peek at the next token. */
14648 token = cp_lexer_peek_token (parser->lexer);
14649 /* If it's not `catch' then there are no more handlers. */
14650 if (!cp_parser_is_keyword (token, RID_CATCH))
14651 break;
14655 /* Parse a handler.
14657 handler:
14658 catch ( exception-declaration ) compound-statement */
14660 static void
14661 cp_parser_handler (cp_parser* parser)
14663 tree handler;
14664 tree declaration;
14666 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
14667 handler = begin_handler ();
14668 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14669 declaration = cp_parser_exception_declaration (parser);
14670 finish_handler_parms (declaration, handler);
14671 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14672 cp_parser_compound_statement (parser, NULL, false);
14673 finish_handler (handler);
14676 /* Parse an exception-declaration.
14678 exception-declaration:
14679 type-specifier-seq declarator
14680 type-specifier-seq abstract-declarator
14681 type-specifier-seq
14684 Returns a VAR_DECL for the declaration, or NULL_TREE if the
14685 ellipsis variant is used. */
14687 static tree
14688 cp_parser_exception_declaration (cp_parser* parser)
14690 cp_decl_specifier_seq type_specifiers;
14691 cp_declarator *declarator;
14692 const char *saved_message;
14694 /* If it's an ellipsis, it's easy to handle. */
14695 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14697 /* Consume the `...' token. */
14698 cp_lexer_consume_token (parser->lexer);
14699 return NULL_TREE;
14702 /* Types may not be defined in exception-declarations. */
14703 saved_message = parser->type_definition_forbidden_message;
14704 parser->type_definition_forbidden_message
14705 = "types may not be defined in exception-declarations";
14707 /* Parse the type-specifier-seq. */
14708 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
14709 &type_specifiers);
14710 /* If it's a `)', then there is no declarator. */
14711 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
14712 declarator = NULL;
14713 else
14714 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
14715 /*ctor_dtor_or_conv_p=*/NULL,
14716 /*parenthesized_p=*/NULL,
14717 /*member_p=*/false);
14719 /* Restore the saved message. */
14720 parser->type_definition_forbidden_message = saved_message;
14722 if (!type_specifiers.any_specifiers_p)
14723 return error_mark_node;
14725 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
14728 /* Parse a throw-expression.
14730 throw-expression:
14731 throw assignment-expression [opt]
14733 Returns a THROW_EXPR representing the throw-expression. */
14735 static tree
14736 cp_parser_throw_expression (cp_parser* parser)
14738 tree expression;
14739 cp_token* token;
14741 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
14742 token = cp_lexer_peek_token (parser->lexer);
14743 /* Figure out whether or not there is an assignment-expression
14744 following the "throw" keyword. */
14745 if (token->type == CPP_COMMA
14746 || token->type == CPP_SEMICOLON
14747 || token->type == CPP_CLOSE_PAREN
14748 || token->type == CPP_CLOSE_SQUARE
14749 || token->type == CPP_CLOSE_BRACE
14750 || token->type == CPP_COLON)
14751 expression = NULL_TREE;
14752 else
14753 expression = cp_parser_assignment_expression (parser,
14754 /*cast_p=*/false);
14756 return build_throw (expression);
14759 /* GNU Extensions */
14761 /* Parse an (optional) asm-specification.
14763 asm-specification:
14764 asm ( string-literal )
14766 If the asm-specification is present, returns a STRING_CST
14767 corresponding to the string-literal. Otherwise, returns
14768 NULL_TREE. */
14770 static tree
14771 cp_parser_asm_specification_opt (cp_parser* parser)
14773 cp_token *token;
14774 tree asm_specification;
14776 /* Peek at the next token. */
14777 token = cp_lexer_peek_token (parser->lexer);
14778 /* If the next token isn't the `asm' keyword, then there's no
14779 asm-specification. */
14780 if (!cp_parser_is_keyword (token, RID_ASM))
14781 return NULL_TREE;
14783 /* Consume the `asm' token. */
14784 cp_lexer_consume_token (parser->lexer);
14785 /* Look for the `('. */
14786 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14788 /* Look for the string-literal. */
14789 asm_specification = cp_parser_string_literal (parser, false, false);
14791 /* Look for the `)'. */
14792 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
14794 return asm_specification;
14797 /* Parse an asm-operand-list.
14799 asm-operand-list:
14800 asm-operand
14801 asm-operand-list , asm-operand
14803 asm-operand:
14804 string-literal ( expression )
14805 [ string-literal ] string-literal ( expression )
14807 Returns a TREE_LIST representing the operands. The TREE_VALUE of
14808 each node is the expression. The TREE_PURPOSE is itself a
14809 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
14810 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
14811 is a STRING_CST for the string literal before the parenthesis. */
14813 static tree
14814 cp_parser_asm_operand_list (cp_parser* parser)
14816 tree asm_operands = NULL_TREE;
14818 while (true)
14820 tree string_literal;
14821 tree expression;
14822 tree name;
14824 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
14826 /* Consume the `[' token. */
14827 cp_lexer_consume_token (parser->lexer);
14828 /* Read the operand name. */
14829 name = cp_parser_identifier (parser);
14830 if (name != error_mark_node)
14831 name = build_string (IDENTIFIER_LENGTH (name),
14832 IDENTIFIER_POINTER (name));
14833 /* Look for the closing `]'. */
14834 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
14836 else
14837 name = NULL_TREE;
14838 /* Look for the string-literal. */
14839 string_literal = cp_parser_string_literal (parser, false, false);
14841 /* Look for the `('. */
14842 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14843 /* Parse the expression. */
14844 expression = cp_parser_expression (parser, /*cast_p=*/false);
14845 /* Look for the `)'. */
14846 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14848 /* Add this operand to the list. */
14849 asm_operands = tree_cons (build_tree_list (name, string_literal),
14850 expression,
14851 asm_operands);
14852 /* If the next token is not a `,', there are no more
14853 operands. */
14854 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14855 break;
14856 /* Consume the `,'. */
14857 cp_lexer_consume_token (parser->lexer);
14860 return nreverse (asm_operands);
14863 /* Parse an asm-clobber-list.
14865 asm-clobber-list:
14866 string-literal
14867 asm-clobber-list , string-literal
14869 Returns a TREE_LIST, indicating the clobbers in the order that they
14870 appeared. The TREE_VALUE of each node is a STRING_CST. */
14872 static tree
14873 cp_parser_asm_clobber_list (cp_parser* parser)
14875 tree clobbers = NULL_TREE;
14877 while (true)
14879 tree string_literal;
14881 /* Look for the string literal. */
14882 string_literal = cp_parser_string_literal (parser, false, false);
14883 /* Add it to the list. */
14884 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
14885 /* If the next token is not a `,', then the list is
14886 complete. */
14887 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14888 break;
14889 /* Consume the `,' token. */
14890 cp_lexer_consume_token (parser->lexer);
14893 return clobbers;
14896 /* Parse an (optional) series of attributes.
14898 attributes:
14899 attributes attribute
14901 attribute:
14902 __attribute__ (( attribute-list [opt] ))
14904 The return value is as for cp_parser_attribute_list. */
14906 static tree
14907 cp_parser_attributes_opt (cp_parser* parser)
14909 tree attributes = NULL_TREE;
14911 while (true)
14913 cp_token *token;
14914 tree attribute_list;
14916 /* Peek at the next token. */
14917 token = cp_lexer_peek_token (parser->lexer);
14918 /* If it's not `__attribute__', then we're done. */
14919 if (token->keyword != RID_ATTRIBUTE)
14920 break;
14922 /* Consume the `__attribute__' keyword. */
14923 cp_lexer_consume_token (parser->lexer);
14924 /* Look for the two `(' tokens. */
14925 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14926 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14928 /* Peek at the next token. */
14929 token = cp_lexer_peek_token (parser->lexer);
14930 if (token->type != CPP_CLOSE_PAREN)
14931 /* Parse the attribute-list. */
14932 attribute_list = cp_parser_attribute_list (parser);
14933 else
14934 /* If the next token is a `)', then there is no attribute
14935 list. */
14936 attribute_list = NULL;
14938 /* Look for the two `)' tokens. */
14939 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14940 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14942 /* Add these new attributes to the list. */
14943 attributes = chainon (attributes, attribute_list);
14946 return attributes;
14949 /* Parse an attribute-list.
14951 attribute-list:
14952 attribute
14953 attribute-list , attribute
14955 attribute:
14956 identifier
14957 identifier ( identifier )
14958 identifier ( identifier , expression-list )
14959 identifier ( expression-list )
14961 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
14962 to an attribute. The TREE_PURPOSE of each node is the identifier
14963 indicating which attribute is in use. The TREE_VALUE represents
14964 the arguments, if any. */
14966 static tree
14967 cp_parser_attribute_list (cp_parser* parser)
14969 tree attribute_list = NULL_TREE;
14970 bool save_translate_strings_p = parser->translate_strings_p;
14972 parser->translate_strings_p = false;
14973 while (true)
14975 cp_token *token;
14976 tree identifier;
14977 tree attribute;
14979 /* Look for the identifier. We also allow keywords here; for
14980 example `__attribute__ ((const))' is legal. */
14981 token = cp_lexer_peek_token (parser->lexer);
14982 if (token->type == CPP_NAME
14983 || token->type == CPP_KEYWORD)
14985 tree arguments = NULL_TREE;
14987 /* Consume the token. */
14988 token = cp_lexer_consume_token (parser->lexer);
14990 /* Save away the identifier that indicates which attribute
14991 this is. */
14992 identifier = token->u.value;
14993 attribute = build_tree_list (identifier, NULL_TREE);
14995 /* Peek at the next token. */
14996 token = cp_lexer_peek_token (parser->lexer);
14997 /* If it's an `(', then parse the attribute arguments. */
14998 if (token->type == CPP_OPEN_PAREN)
15000 arguments = cp_parser_parenthesized_expression_list
15001 (parser, true, /*cast_p=*/false,
15002 /*non_constant_p=*/NULL);
15003 /* Save the arguments away. */
15004 TREE_VALUE (attribute) = arguments;
15007 if (arguments != error_mark_node)
15009 /* Add this attribute to the list. */
15010 TREE_CHAIN (attribute) = attribute_list;
15011 attribute_list = attribute;
15014 token = cp_lexer_peek_token (parser->lexer);
15016 /* Now, look for more attributes. If the next token isn't a
15017 `,', we're done. */
15018 if (token->type != CPP_COMMA)
15019 break;
15021 /* Consume the comma and keep going. */
15022 cp_lexer_consume_token (parser->lexer);
15024 parser->translate_strings_p = save_translate_strings_p;
15026 /* We built up the list in reverse order. */
15027 return nreverse (attribute_list);
15030 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
15031 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
15032 current value of the PEDANTIC flag, regardless of whether or not
15033 the `__extension__' keyword is present. The caller is responsible
15034 for restoring the value of the PEDANTIC flag. */
15036 static bool
15037 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
15039 /* Save the old value of the PEDANTIC flag. */
15040 *saved_pedantic = pedantic;
15042 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
15044 /* Consume the `__extension__' token. */
15045 cp_lexer_consume_token (parser->lexer);
15046 /* We're not being pedantic while the `__extension__' keyword is
15047 in effect. */
15048 pedantic = 0;
15050 return true;
15053 return false;
15056 /* Parse a label declaration.
15058 label-declaration:
15059 __label__ label-declarator-seq ;
15061 label-declarator-seq:
15062 identifier , label-declarator-seq
15063 identifier */
15065 static void
15066 cp_parser_label_declaration (cp_parser* parser)
15068 /* Look for the `__label__' keyword. */
15069 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
15071 while (true)
15073 tree identifier;
15075 /* Look for an identifier. */
15076 identifier = cp_parser_identifier (parser);
15077 /* If we failed, stop. */
15078 if (identifier == error_mark_node)
15079 break;
15080 /* Declare it as a label. */
15081 finish_label_decl (identifier);
15082 /* If the next token is a `;', stop. */
15083 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15084 break;
15085 /* Look for the `,' separating the label declarations. */
15086 cp_parser_require (parser, CPP_COMMA, "`,'");
15089 /* Look for the final `;'. */
15090 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
15093 /* Support Functions */
15095 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
15096 NAME should have one of the representations used for an
15097 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
15098 is returned. If PARSER->SCOPE is a dependent type, then a
15099 SCOPE_REF is returned.
15101 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
15102 returned; the name was already resolved when the TEMPLATE_ID_EXPR
15103 was formed. Abstractly, such entities should not be passed to this
15104 function, because they do not need to be looked up, but it is
15105 simpler to check for this special case here, rather than at the
15106 call-sites.
15108 In cases not explicitly covered above, this function returns a
15109 DECL, OVERLOAD, or baselink representing the result of the lookup.
15110 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
15111 is returned.
15113 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
15114 (e.g., "struct") that was used. In that case bindings that do not
15115 refer to types are ignored.
15117 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
15118 ignored.
15120 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
15121 are ignored.
15123 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
15124 types.
15126 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
15127 TREE_LIST of candidates if name-lookup results in an ambiguity, and
15128 NULL_TREE otherwise. */
15130 static tree
15131 cp_parser_lookup_name (cp_parser *parser, tree name,
15132 enum tag_types tag_type,
15133 bool is_template,
15134 bool is_namespace,
15135 bool check_dependency,
15136 tree *ambiguous_decls)
15138 int flags = 0;
15139 tree decl;
15140 tree object_type = parser->context->object_type;
15142 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15143 flags |= LOOKUP_COMPLAIN;
15145 /* Assume that the lookup will be unambiguous. */
15146 if (ambiguous_decls)
15147 *ambiguous_decls = NULL_TREE;
15149 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
15150 no longer valid. Note that if we are parsing tentatively, and
15151 the parse fails, OBJECT_TYPE will be automatically restored. */
15152 parser->context->object_type = NULL_TREE;
15154 if (name == error_mark_node)
15155 return error_mark_node;
15157 /* A template-id has already been resolved; there is no lookup to
15158 do. */
15159 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
15160 return name;
15161 if (BASELINK_P (name))
15163 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
15164 == TEMPLATE_ID_EXPR);
15165 return name;
15168 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
15169 it should already have been checked to make sure that the name
15170 used matches the type being destroyed. */
15171 if (TREE_CODE (name) == BIT_NOT_EXPR)
15173 tree type;
15175 /* Figure out to which type this destructor applies. */
15176 if (parser->scope)
15177 type = parser->scope;
15178 else if (object_type)
15179 type = object_type;
15180 else
15181 type = current_class_type;
15182 /* If that's not a class type, there is no destructor. */
15183 if (!type || !CLASS_TYPE_P (type))
15184 return error_mark_node;
15185 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
15186 lazily_declare_fn (sfk_destructor, type);
15187 if (!CLASSTYPE_DESTRUCTORS (type))
15188 return error_mark_node;
15189 /* If it was a class type, return the destructor. */
15190 return CLASSTYPE_DESTRUCTORS (type);
15193 /* By this point, the NAME should be an ordinary identifier. If
15194 the id-expression was a qualified name, the qualifying scope is
15195 stored in PARSER->SCOPE at this point. */
15196 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
15198 /* Perform the lookup. */
15199 if (parser->scope)
15201 bool dependent_p;
15203 if (parser->scope == error_mark_node)
15204 return error_mark_node;
15206 /* If the SCOPE is dependent, the lookup must be deferred until
15207 the template is instantiated -- unless we are explicitly
15208 looking up names in uninstantiated templates. Even then, we
15209 cannot look up the name if the scope is not a class type; it
15210 might, for example, be a template type parameter. */
15211 dependent_p = (TYPE_P (parser->scope)
15212 && !(parser->in_declarator_p
15213 && currently_open_class (parser->scope))
15214 && dependent_type_p (parser->scope));
15215 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
15216 && dependent_p)
15218 if (tag_type)
15220 tree type;
15222 /* The resolution to Core Issue 180 says that `struct
15223 A::B' should be considered a type-name, even if `A'
15224 is dependent. */
15225 type = make_typename_type (parser->scope, name, tag_type,
15226 /*complain=*/tf_error);
15227 decl = TYPE_NAME (type);
15229 else if (is_template
15230 && (cp_parser_next_token_ends_template_argument_p (parser)
15231 || cp_lexer_next_token_is (parser->lexer,
15232 CPP_CLOSE_PAREN)))
15233 decl = make_unbound_class_template (parser->scope,
15234 name, NULL_TREE,
15235 /*complain=*/tf_error);
15236 else
15237 decl = build_qualified_name (/*type=*/NULL_TREE,
15238 parser->scope, name,
15239 is_template);
15241 else
15243 tree pushed_scope = NULL_TREE;
15245 /* If PARSER->SCOPE is a dependent type, then it must be a
15246 class type, and we must not be checking dependencies;
15247 otherwise, we would have processed this lookup above. So
15248 that PARSER->SCOPE is not considered a dependent base by
15249 lookup_member, we must enter the scope here. */
15250 if (dependent_p)
15251 pushed_scope = push_scope (parser->scope);
15252 /* If the PARSER->SCOPE is a template specialization, it
15253 may be instantiated during name lookup. In that case,
15254 errors may be issued. Even if we rollback the current
15255 tentative parse, those errors are valid. */
15256 decl = lookup_qualified_name (parser->scope, name,
15257 tag_type != none_type,
15258 /*complain=*/true);
15259 if (pushed_scope)
15260 pop_scope (pushed_scope);
15262 parser->qualifying_scope = parser->scope;
15263 parser->object_scope = NULL_TREE;
15265 else if (object_type)
15267 tree object_decl = NULL_TREE;
15268 /* Look up the name in the scope of the OBJECT_TYPE, unless the
15269 OBJECT_TYPE is not a class. */
15270 if (CLASS_TYPE_P (object_type))
15271 /* If the OBJECT_TYPE is a template specialization, it may
15272 be instantiated during name lookup. In that case, errors
15273 may be issued. Even if we rollback the current tentative
15274 parse, those errors are valid. */
15275 object_decl = lookup_member (object_type,
15276 name,
15277 /*protect=*/0,
15278 tag_type != none_type);
15279 /* Look it up in the enclosing context, too. */
15280 decl = lookup_name_real (name, tag_type != none_type,
15281 /*nonclass=*/0,
15282 /*block_p=*/true, is_namespace, flags);
15283 parser->object_scope = object_type;
15284 parser->qualifying_scope = NULL_TREE;
15285 if (object_decl)
15286 decl = object_decl;
15288 else
15290 decl = lookup_name_real (name, tag_type != none_type,
15291 /*nonclass=*/0,
15292 /*block_p=*/true, is_namespace, flags);
15293 parser->qualifying_scope = NULL_TREE;
15294 parser->object_scope = NULL_TREE;
15297 /* If the lookup failed, let our caller know. */
15298 if (!decl || decl == error_mark_node)
15299 return error_mark_node;
15301 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
15302 if (TREE_CODE (decl) == TREE_LIST)
15304 if (ambiguous_decls)
15305 *ambiguous_decls = decl;
15306 /* The error message we have to print is too complicated for
15307 cp_parser_error, so we incorporate its actions directly. */
15308 if (!cp_parser_simulate_error (parser))
15310 error ("reference to %qD is ambiguous", name);
15311 print_candidates (decl);
15313 return error_mark_node;
15316 gcc_assert (DECL_P (decl)
15317 || TREE_CODE (decl) == OVERLOAD
15318 || TREE_CODE (decl) == SCOPE_REF
15319 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
15320 || BASELINK_P (decl));
15322 /* If we have resolved the name of a member declaration, check to
15323 see if the declaration is accessible. When the name resolves to
15324 set of overloaded functions, accessibility is checked when
15325 overload resolution is done.
15327 During an explicit instantiation, access is not checked at all,
15328 as per [temp.explicit]. */
15329 if (DECL_P (decl))
15330 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
15332 return decl;
15335 /* Like cp_parser_lookup_name, but for use in the typical case where
15336 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
15337 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
15339 static tree
15340 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
15342 return cp_parser_lookup_name (parser, name,
15343 none_type,
15344 /*is_template=*/false,
15345 /*is_namespace=*/false,
15346 /*check_dependency=*/true,
15347 /*ambiguous_decls=*/NULL);
15350 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
15351 the current context, return the TYPE_DECL. If TAG_NAME_P is
15352 true, the DECL indicates the class being defined in a class-head,
15353 or declared in an elaborated-type-specifier.
15355 Otherwise, return DECL. */
15357 static tree
15358 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
15360 /* If the TEMPLATE_DECL is being declared as part of a class-head,
15361 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
15363 struct A {
15364 template <typename T> struct B;
15367 template <typename T> struct A::B {};
15369 Similarly, in an elaborated-type-specifier:
15371 namespace N { struct X{}; }
15373 struct A {
15374 template <typename T> friend struct N::X;
15377 However, if the DECL refers to a class type, and we are in
15378 the scope of the class, then the name lookup automatically
15379 finds the TYPE_DECL created by build_self_reference rather
15380 than a TEMPLATE_DECL. For example, in:
15382 template <class T> struct S {
15383 S s;
15386 there is no need to handle such case. */
15388 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
15389 return DECL_TEMPLATE_RESULT (decl);
15391 return decl;
15394 /* If too many, or too few, template-parameter lists apply to the
15395 declarator, issue an error message. Returns TRUE if all went well,
15396 and FALSE otherwise. */
15398 static bool
15399 cp_parser_check_declarator_template_parameters (cp_parser* parser,
15400 cp_declarator *declarator)
15402 unsigned num_templates;
15404 /* We haven't seen any classes that involve template parameters yet. */
15405 num_templates = 0;
15407 switch (declarator->kind)
15409 case cdk_id:
15410 if (declarator->u.id.qualifying_scope)
15412 tree scope;
15413 tree member;
15415 scope = declarator->u.id.qualifying_scope;
15416 member = declarator->u.id.unqualified_name;
15418 while (scope && CLASS_TYPE_P (scope))
15420 /* You're supposed to have one `template <...>'
15421 for every template class, but you don't need one
15422 for a full specialization. For example:
15424 template <class T> struct S{};
15425 template <> struct S<int> { void f(); };
15426 void S<int>::f () {}
15428 is correct; there shouldn't be a `template <>' for
15429 the definition of `S<int>::f'. */
15430 if (!CLASSTYPE_TEMPLATE_INFO (scope))
15431 /* If SCOPE does not have template information of any
15432 kind, then it is not a template, nor is it nested
15433 within a template. */
15434 break;
15435 if (explicit_class_specialization_p (scope))
15436 break;
15437 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
15438 ++num_templates;
15440 scope = TYPE_CONTEXT (scope);
15443 else if (TREE_CODE (declarator->u.id.unqualified_name)
15444 == TEMPLATE_ID_EXPR)
15445 /* If the DECLARATOR has the form `X<y>' then it uses one
15446 additional level of template parameters. */
15447 ++num_templates;
15449 return cp_parser_check_template_parameters (parser,
15450 num_templates);
15452 case cdk_function:
15453 case cdk_array:
15454 case cdk_pointer:
15455 case cdk_reference:
15456 case cdk_ptrmem:
15457 return (cp_parser_check_declarator_template_parameters
15458 (parser, declarator->declarator));
15460 case cdk_error:
15461 return true;
15463 default:
15464 gcc_unreachable ();
15466 return false;
15469 /* NUM_TEMPLATES were used in the current declaration. If that is
15470 invalid, return FALSE and issue an error messages. Otherwise,
15471 return TRUE. */
15473 static bool
15474 cp_parser_check_template_parameters (cp_parser* parser,
15475 unsigned num_templates)
15477 /* If there are more template classes than parameter lists, we have
15478 something like:
15480 template <class T> void S<T>::R<T>::f (); */
15481 if (parser->num_template_parameter_lists < num_templates)
15483 error ("too few template-parameter-lists");
15484 return false;
15486 /* If there are the same number of template classes and parameter
15487 lists, that's OK. */
15488 if (parser->num_template_parameter_lists == num_templates)
15489 return true;
15490 /* If there are more, but only one more, then we are referring to a
15491 member template. That's OK too. */
15492 if (parser->num_template_parameter_lists == num_templates + 1)
15493 return true;
15494 /* Otherwise, there are too many template parameter lists. We have
15495 something like:
15497 template <class T> template <class U> void S::f(); */
15498 error ("too many template-parameter-lists");
15499 return false;
15502 /* Parse an optional `::' token indicating that the following name is
15503 from the global namespace. If so, PARSER->SCOPE is set to the
15504 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
15505 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
15506 Returns the new value of PARSER->SCOPE, if the `::' token is
15507 present, and NULL_TREE otherwise. */
15509 static tree
15510 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
15512 cp_token *token;
15514 /* Peek at the next token. */
15515 token = cp_lexer_peek_token (parser->lexer);
15516 /* If we're looking at a `::' token then we're starting from the
15517 global namespace, not our current location. */
15518 if (token->type == CPP_SCOPE)
15520 /* Consume the `::' token. */
15521 cp_lexer_consume_token (parser->lexer);
15522 /* Set the SCOPE so that we know where to start the lookup. */
15523 parser->scope = global_namespace;
15524 parser->qualifying_scope = global_namespace;
15525 parser->object_scope = NULL_TREE;
15527 return parser->scope;
15529 else if (!current_scope_valid_p)
15531 parser->scope = NULL_TREE;
15532 parser->qualifying_scope = NULL_TREE;
15533 parser->object_scope = NULL_TREE;
15536 return NULL_TREE;
15539 /* Returns TRUE if the upcoming token sequence is the start of a
15540 constructor declarator. If FRIEND_P is true, the declarator is
15541 preceded by the `friend' specifier. */
15543 static bool
15544 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
15546 bool constructor_p;
15547 tree type_decl = NULL_TREE;
15548 bool nested_name_p;
15549 cp_token *next_token;
15551 /* The common case is that this is not a constructor declarator, so
15552 try to avoid doing lots of work if at all possible. It's not
15553 valid declare a constructor at function scope. */
15554 if (parser->in_function_body)
15555 return false;
15556 /* And only certain tokens can begin a constructor declarator. */
15557 next_token = cp_lexer_peek_token (parser->lexer);
15558 if (next_token->type != CPP_NAME
15559 && next_token->type != CPP_SCOPE
15560 && next_token->type != CPP_NESTED_NAME_SPECIFIER
15561 && next_token->type != CPP_TEMPLATE_ID)
15562 return false;
15564 /* Parse tentatively; we are going to roll back all of the tokens
15565 consumed here. */
15566 cp_parser_parse_tentatively (parser);
15567 /* Assume that we are looking at a constructor declarator. */
15568 constructor_p = true;
15570 /* Look for the optional `::' operator. */
15571 cp_parser_global_scope_opt (parser,
15572 /*current_scope_valid_p=*/false);
15573 /* Look for the nested-name-specifier. */
15574 nested_name_p
15575 = (cp_parser_nested_name_specifier_opt (parser,
15576 /*typename_keyword_p=*/false,
15577 /*check_dependency_p=*/false,
15578 /*type_p=*/false,
15579 /*is_declaration=*/false)
15580 != NULL_TREE);
15581 /* Outside of a class-specifier, there must be a
15582 nested-name-specifier. */
15583 if (!nested_name_p &&
15584 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
15585 || friend_p))
15586 constructor_p = false;
15587 /* If we still think that this might be a constructor-declarator,
15588 look for a class-name. */
15589 if (constructor_p)
15591 /* If we have:
15593 template <typename T> struct S { S(); };
15594 template <typename T> S<T>::S ();
15596 we must recognize that the nested `S' names a class.
15597 Similarly, for:
15599 template <typename T> S<T>::S<T> ();
15601 we must recognize that the nested `S' names a template. */
15602 type_decl = cp_parser_class_name (parser,
15603 /*typename_keyword_p=*/false,
15604 /*template_keyword_p=*/false,
15605 none_type,
15606 /*check_dependency_p=*/false,
15607 /*class_head_p=*/false,
15608 /*is_declaration=*/false);
15609 /* If there was no class-name, then this is not a constructor. */
15610 constructor_p = !cp_parser_error_occurred (parser);
15613 /* If we're still considering a constructor, we have to see a `(',
15614 to begin the parameter-declaration-clause, followed by either a
15615 `)', an `...', or a decl-specifier. We need to check for a
15616 type-specifier to avoid being fooled into thinking that:
15618 S::S (f) (int);
15620 is a constructor. (It is actually a function named `f' that
15621 takes one parameter (of type `int') and returns a value of type
15622 `S::S'. */
15623 if (constructor_p
15624 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
15626 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
15627 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
15628 /* A parameter declaration begins with a decl-specifier,
15629 which is either the "attribute" keyword, a storage class
15630 specifier, or (usually) a type-specifier. */
15631 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
15633 tree type;
15634 tree pushed_scope = NULL_TREE;
15635 unsigned saved_num_template_parameter_lists;
15637 /* Names appearing in the type-specifier should be looked up
15638 in the scope of the class. */
15639 if (current_class_type)
15640 type = NULL_TREE;
15641 else
15643 type = TREE_TYPE (type_decl);
15644 if (TREE_CODE (type) == TYPENAME_TYPE)
15646 type = resolve_typename_type (type,
15647 /*only_current_p=*/false);
15648 if (type == error_mark_node)
15650 cp_parser_abort_tentative_parse (parser);
15651 return false;
15654 pushed_scope = push_scope (type);
15657 /* Inside the constructor parameter list, surrounding
15658 template-parameter-lists do not apply. */
15659 saved_num_template_parameter_lists
15660 = parser->num_template_parameter_lists;
15661 parser->num_template_parameter_lists = 0;
15663 /* Look for the type-specifier. */
15664 cp_parser_type_specifier (parser,
15665 CP_PARSER_FLAGS_NONE,
15666 /*decl_specs=*/NULL,
15667 /*is_declarator=*/true,
15668 /*declares_class_or_enum=*/NULL,
15669 /*is_cv_qualifier=*/NULL);
15671 parser->num_template_parameter_lists
15672 = saved_num_template_parameter_lists;
15674 /* Leave the scope of the class. */
15675 if (pushed_scope)
15676 pop_scope (pushed_scope);
15678 constructor_p = !cp_parser_error_occurred (parser);
15681 else
15682 constructor_p = false;
15683 /* We did not really want to consume any tokens. */
15684 cp_parser_abort_tentative_parse (parser);
15686 return constructor_p;
15689 /* Parse the definition of the function given by the DECL_SPECIFIERS,
15690 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
15691 they must be performed once we are in the scope of the function.
15693 Returns the function defined. */
15695 static tree
15696 cp_parser_function_definition_from_specifiers_and_declarator
15697 (cp_parser* parser,
15698 cp_decl_specifier_seq *decl_specifiers,
15699 tree attributes,
15700 const cp_declarator *declarator)
15702 tree fn;
15703 bool success_p;
15705 /* Begin the function-definition. */
15706 success_p = start_function (decl_specifiers, declarator, attributes);
15708 /* The things we're about to see are not directly qualified by any
15709 template headers we've seen thus far. */
15710 reset_specialization ();
15712 /* If there were names looked up in the decl-specifier-seq that we
15713 did not check, check them now. We must wait until we are in the
15714 scope of the function to perform the checks, since the function
15715 might be a friend. */
15716 perform_deferred_access_checks ();
15718 if (!success_p)
15720 /* Skip the entire function. */
15721 cp_parser_skip_to_end_of_block_or_statement (parser);
15722 fn = error_mark_node;
15724 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
15726 /* Seen already, skip it. An error message has already been output. */
15727 cp_parser_skip_to_end_of_block_or_statement (parser);
15728 fn = current_function_decl;
15729 current_function_decl = NULL_TREE;
15730 /* If this is a function from a class, pop the nested class. */
15731 if (current_class_name)
15732 pop_nested_class ();
15734 else
15735 fn = cp_parser_function_definition_after_declarator (parser,
15736 /*inline_p=*/false);
15738 return fn;
15741 /* Parse the part of a function-definition that follows the
15742 declarator. INLINE_P is TRUE iff this function is an inline
15743 function defined with a class-specifier.
15745 Returns the function defined. */
15747 static tree
15748 cp_parser_function_definition_after_declarator (cp_parser* parser,
15749 bool inline_p)
15751 tree fn;
15752 bool ctor_initializer_p = false;
15753 bool saved_in_unbraced_linkage_specification_p;
15754 bool saved_in_function_body;
15755 unsigned saved_num_template_parameter_lists;
15757 saved_in_function_body = parser->in_function_body;
15758 parser->in_function_body = true;
15759 /* If the next token is `return', then the code may be trying to
15760 make use of the "named return value" extension that G++ used to
15761 support. */
15762 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
15764 /* Consume the `return' keyword. */
15765 cp_lexer_consume_token (parser->lexer);
15766 /* Look for the identifier that indicates what value is to be
15767 returned. */
15768 cp_parser_identifier (parser);
15769 /* Issue an error message. */
15770 error ("named return values are no longer supported");
15771 /* Skip tokens until we reach the start of the function body. */
15772 while (true)
15774 cp_token *token = cp_lexer_peek_token (parser->lexer);
15775 if (token->type == CPP_OPEN_BRACE
15776 || token->type == CPP_EOF
15777 || token->type == CPP_PRAGMA_EOL)
15778 break;
15779 cp_lexer_consume_token (parser->lexer);
15782 /* The `extern' in `extern "C" void f () { ... }' does not apply to
15783 anything declared inside `f'. */
15784 saved_in_unbraced_linkage_specification_p
15785 = parser->in_unbraced_linkage_specification_p;
15786 parser->in_unbraced_linkage_specification_p = false;
15787 /* Inside the function, surrounding template-parameter-lists do not
15788 apply. */
15789 saved_num_template_parameter_lists
15790 = parser->num_template_parameter_lists;
15791 parser->num_template_parameter_lists = 0;
15792 /* If the next token is `try', then we are looking at a
15793 function-try-block. */
15794 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
15795 ctor_initializer_p = cp_parser_function_try_block (parser);
15796 /* A function-try-block includes the function-body, so we only do
15797 this next part if we're not processing a function-try-block. */
15798 else
15799 ctor_initializer_p
15800 = cp_parser_ctor_initializer_opt_and_function_body (parser);
15802 /* Finish the function. */
15803 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
15804 (inline_p ? 2 : 0));
15805 /* Generate code for it, if necessary. */
15806 expand_or_defer_fn (fn);
15807 /* Restore the saved values. */
15808 parser->in_unbraced_linkage_specification_p
15809 = saved_in_unbraced_linkage_specification_p;
15810 parser->num_template_parameter_lists
15811 = saved_num_template_parameter_lists;
15812 parser->in_function_body = saved_in_function_body;
15814 return fn;
15817 /* Parse a template-declaration, assuming that the `export' (and
15818 `extern') keywords, if present, has already been scanned. MEMBER_P
15819 is as for cp_parser_template_declaration. */
15821 static void
15822 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
15824 tree decl = NULL_TREE;
15825 VEC (deferred_access_check,gc) *checks;
15826 tree parameter_list;
15827 bool friend_p = false;
15828 bool need_lang_pop;
15830 /* Look for the `template' keyword. */
15831 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
15832 return;
15834 /* And the `<'. */
15835 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
15836 return;
15837 if (at_class_scope_p () && current_function_decl)
15839 /* 14.5.2.2 [temp.mem]
15841 A local class shall not have member templates. */
15842 error ("invalid declaration of member template in local class");
15843 cp_parser_skip_to_end_of_block_or_statement (parser);
15844 return;
15846 /* [temp]
15848 A template ... shall not have C linkage. */
15849 if (current_lang_name == lang_name_c)
15851 error ("template with C linkage");
15852 /* Give it C++ linkage to avoid confusing other parts of the
15853 front end. */
15854 push_lang_context (lang_name_cplusplus);
15855 need_lang_pop = true;
15857 else
15858 need_lang_pop = false;
15860 /* We cannot perform access checks on the template parameter
15861 declarations until we know what is being declared, just as we
15862 cannot check the decl-specifier list. */
15863 push_deferring_access_checks (dk_deferred);
15865 /* If the next token is `>', then we have an invalid
15866 specialization. Rather than complain about an invalid template
15867 parameter, issue an error message here. */
15868 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15870 cp_parser_error (parser, "invalid explicit specialization");
15871 begin_specialization ();
15872 parameter_list = NULL_TREE;
15874 else
15875 /* Parse the template parameters. */
15876 parameter_list = cp_parser_template_parameter_list (parser);
15878 /* Get the deferred access checks from the parameter list. These
15879 will be checked once we know what is being declared, as for a
15880 member template the checks must be performed in the scope of the
15881 class containing the member. */
15882 checks = get_deferred_access_checks ();
15884 /* Look for the `>'. */
15885 cp_parser_skip_to_end_of_template_parameter_list (parser);
15886 /* We just processed one more parameter list. */
15887 ++parser->num_template_parameter_lists;
15888 /* If the next token is `template', there are more template
15889 parameters. */
15890 if (cp_lexer_next_token_is_keyword (parser->lexer,
15891 RID_TEMPLATE))
15892 cp_parser_template_declaration_after_export (parser, member_p);
15893 else
15895 /* There are no access checks when parsing a template, as we do not
15896 know if a specialization will be a friend. */
15897 push_deferring_access_checks (dk_no_check);
15898 decl = cp_parser_single_declaration (parser,
15899 checks,
15900 member_p,
15901 &friend_p);
15902 pop_deferring_access_checks ();
15904 /* If this is a member template declaration, let the front
15905 end know. */
15906 if (member_p && !friend_p && decl)
15908 if (TREE_CODE (decl) == TYPE_DECL)
15909 cp_parser_check_access_in_redeclaration (decl);
15911 decl = finish_member_template_decl (decl);
15913 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
15914 make_friend_class (current_class_type, TREE_TYPE (decl),
15915 /*complain=*/true);
15917 /* We are done with the current parameter list. */
15918 --parser->num_template_parameter_lists;
15920 pop_deferring_access_checks ();
15922 /* Finish up. */
15923 finish_template_decl (parameter_list);
15925 /* Register member declarations. */
15926 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
15927 finish_member_declaration (decl);
15928 /* For the erroneous case of a template with C linkage, we pushed an
15929 implicit C++ linkage scope; exit that scope now. */
15930 if (need_lang_pop)
15931 pop_lang_context ();
15932 /* If DECL is a function template, we must return to parse it later.
15933 (Even though there is no definition, there might be default
15934 arguments that need handling.) */
15935 if (member_p && decl
15936 && (TREE_CODE (decl) == FUNCTION_DECL
15937 || DECL_FUNCTION_TEMPLATE_P (decl)))
15938 TREE_VALUE (parser->unparsed_functions_queues)
15939 = tree_cons (NULL_TREE, decl,
15940 TREE_VALUE (parser->unparsed_functions_queues));
15943 /* Perform the deferred access checks from a template-parameter-list.
15944 CHECKS is a TREE_LIST of access checks, as returned by
15945 get_deferred_access_checks. */
15947 static void
15948 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
15950 ++processing_template_parmlist;
15951 perform_access_checks (checks);
15952 --processing_template_parmlist;
15955 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
15956 `function-definition' sequence. MEMBER_P is true, this declaration
15957 appears in a class scope.
15959 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
15960 *FRIEND_P is set to TRUE iff the declaration is a friend. */
15962 static tree
15963 cp_parser_single_declaration (cp_parser* parser,
15964 VEC (deferred_access_check,gc)* checks,
15965 bool member_p,
15966 bool* friend_p)
15968 int declares_class_or_enum;
15969 tree decl = NULL_TREE;
15970 cp_decl_specifier_seq decl_specifiers;
15971 bool function_definition_p = false;
15973 /* This function is only used when processing a template
15974 declaration. */
15975 gcc_assert (innermost_scope_kind () == sk_template_parms
15976 || innermost_scope_kind () == sk_template_spec);
15978 /* Defer access checks until we know what is being declared. */
15979 push_deferring_access_checks (dk_deferred);
15981 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
15982 alternative. */
15983 cp_parser_decl_specifier_seq (parser,
15984 CP_PARSER_FLAGS_OPTIONAL,
15985 &decl_specifiers,
15986 &declares_class_or_enum);
15987 if (friend_p)
15988 *friend_p = cp_parser_friend_p (&decl_specifiers);
15990 /* There are no template typedefs. */
15991 if (decl_specifiers.specs[(int) ds_typedef])
15993 error ("template declaration of %qs", "typedef");
15994 decl = error_mark_node;
15997 /* Gather up the access checks that occurred the
15998 decl-specifier-seq. */
15999 stop_deferring_access_checks ();
16001 /* Check for the declaration of a template class. */
16002 if (declares_class_or_enum)
16004 if (cp_parser_declares_only_class_p (parser))
16006 decl = shadow_tag (&decl_specifiers);
16008 /* In this case:
16010 struct C {
16011 friend template <typename T> struct A<T>::B;
16014 A<T>::B will be represented by a TYPENAME_TYPE, and
16015 therefore not recognized by shadow_tag. */
16016 if (friend_p && *friend_p
16017 && !decl
16018 && decl_specifiers.type
16019 && TYPE_P (decl_specifiers.type))
16020 decl = decl_specifiers.type;
16022 if (decl && decl != error_mark_node)
16023 decl = TYPE_NAME (decl);
16024 else
16025 decl = error_mark_node;
16027 /* Perform access checks for template parameters. */
16028 cp_parser_perform_template_parameter_access_checks (checks);
16031 /* If it's not a template class, try for a template function. If
16032 the next token is a `;', then this declaration does not declare
16033 anything. But, if there were errors in the decl-specifiers, then
16034 the error might well have come from an attempted class-specifier.
16035 In that case, there's no need to warn about a missing declarator. */
16036 if (!decl
16037 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
16038 || decl_specifiers.type != error_mark_node))
16039 decl = cp_parser_init_declarator (parser,
16040 &decl_specifiers,
16041 checks,
16042 /*function_definition_allowed_p=*/true,
16043 member_p,
16044 declares_class_or_enum,
16045 &function_definition_p);
16047 pop_deferring_access_checks ();
16049 /* Clear any current qualification; whatever comes next is the start
16050 of something new. */
16051 parser->scope = NULL_TREE;
16052 parser->qualifying_scope = NULL_TREE;
16053 parser->object_scope = NULL_TREE;
16054 /* Look for a trailing `;' after the declaration. */
16055 if (!function_definition_p
16056 && (decl == error_mark_node
16057 || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
16058 cp_parser_skip_to_end_of_block_or_statement (parser);
16060 return decl;
16063 /* Parse a cast-expression that is not the operand of a unary "&". */
16065 static tree
16066 cp_parser_simple_cast_expression (cp_parser *parser)
16068 return cp_parser_cast_expression (parser, /*address_p=*/false,
16069 /*cast_p=*/false);
16072 /* Parse a functional cast to TYPE. Returns an expression
16073 representing the cast. */
16075 static tree
16076 cp_parser_functional_cast (cp_parser* parser, tree type)
16078 tree expression_list;
16079 tree cast;
16081 expression_list
16082 = cp_parser_parenthesized_expression_list (parser, false,
16083 /*cast_p=*/true,
16084 /*non_constant_p=*/NULL);
16086 cast = build_functional_cast (type, expression_list);
16087 /* [expr.const]/1: In an integral constant expression "only type
16088 conversions to integral or enumeration type can be used". */
16089 if (TREE_CODE (type) == TYPE_DECL)
16090 type = TREE_TYPE (type);
16091 if (cast != error_mark_node
16092 && !cast_valid_in_integral_constant_expression_p (type)
16093 && (cp_parser_non_integral_constant_expression
16094 (parser, "a call to a constructor")))
16095 return error_mark_node;
16096 return cast;
16099 /* Save the tokens that make up the body of a member function defined
16100 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
16101 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
16102 specifiers applied to the declaration. Returns the FUNCTION_DECL
16103 for the member function. */
16105 static tree
16106 cp_parser_save_member_function_body (cp_parser* parser,
16107 cp_decl_specifier_seq *decl_specifiers,
16108 cp_declarator *declarator,
16109 tree attributes)
16111 cp_token *first;
16112 cp_token *last;
16113 tree fn;
16115 /* Create the function-declaration. */
16116 fn = start_method (decl_specifiers, declarator, attributes);
16117 /* If something went badly wrong, bail out now. */
16118 if (fn == error_mark_node)
16120 /* If there's a function-body, skip it. */
16121 if (cp_parser_token_starts_function_definition_p
16122 (cp_lexer_peek_token (parser->lexer)))
16123 cp_parser_skip_to_end_of_block_or_statement (parser);
16124 return error_mark_node;
16127 /* Remember it, if there default args to post process. */
16128 cp_parser_save_default_args (parser, fn);
16130 /* Save away the tokens that make up the body of the
16131 function. */
16132 first = parser->lexer->next_token;
16133 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
16134 /* Handle function try blocks. */
16135 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
16136 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
16137 last = parser->lexer->next_token;
16139 /* Save away the inline definition; we will process it when the
16140 class is complete. */
16141 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
16142 DECL_PENDING_INLINE_P (fn) = 1;
16144 /* We need to know that this was defined in the class, so that
16145 friend templates are handled correctly. */
16146 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
16148 /* We're done with the inline definition. */
16149 finish_method (fn);
16151 /* Add FN to the queue of functions to be parsed later. */
16152 TREE_VALUE (parser->unparsed_functions_queues)
16153 = tree_cons (NULL_TREE, fn,
16154 TREE_VALUE (parser->unparsed_functions_queues));
16156 return fn;
16159 /* Parse a template-argument-list, as well as the trailing ">" (but
16160 not the opening ">"). See cp_parser_template_argument_list for the
16161 return value. */
16163 static tree
16164 cp_parser_enclosed_template_argument_list (cp_parser* parser)
16166 tree arguments;
16167 tree saved_scope;
16168 tree saved_qualifying_scope;
16169 tree saved_object_scope;
16170 bool saved_greater_than_is_operator_p;
16171 bool saved_skip_evaluation;
16173 /* [temp.names]
16175 When parsing a template-id, the first non-nested `>' is taken as
16176 the end of the template-argument-list rather than a greater-than
16177 operator. */
16178 saved_greater_than_is_operator_p
16179 = parser->greater_than_is_operator_p;
16180 parser->greater_than_is_operator_p = false;
16181 /* Parsing the argument list may modify SCOPE, so we save it
16182 here. */
16183 saved_scope = parser->scope;
16184 saved_qualifying_scope = parser->qualifying_scope;
16185 saved_object_scope = parser->object_scope;
16186 /* We need to evaluate the template arguments, even though this
16187 template-id may be nested within a "sizeof". */
16188 saved_skip_evaluation = skip_evaluation;
16189 skip_evaluation = false;
16190 /* Parse the template-argument-list itself. */
16191 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
16192 arguments = NULL_TREE;
16193 else
16194 arguments = cp_parser_template_argument_list (parser);
16195 /* Look for the `>' that ends the template-argument-list. If we find
16196 a '>>' instead, it's probably just a typo. */
16197 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16199 if (!saved_greater_than_is_operator_p)
16201 /* If we're in a nested template argument list, the '>>' has
16202 to be a typo for '> >'. We emit the error message, but we
16203 continue parsing and we push a '>' as next token, so that
16204 the argument list will be parsed correctly. Note that the
16205 global source location is still on the token before the
16206 '>>', so we need to say explicitly where we want it. */
16207 cp_token *token = cp_lexer_peek_token (parser->lexer);
16208 error ("%H%<>>%> should be %<> >%> "
16209 "within a nested template argument list",
16210 &token->location);
16212 /* ??? Proper recovery should terminate two levels of
16213 template argument list here. */
16214 token->type = CPP_GREATER;
16216 else
16218 /* If this is not a nested template argument list, the '>>'
16219 is a typo for '>'. Emit an error message and continue.
16220 Same deal about the token location, but here we can get it
16221 right by consuming the '>>' before issuing the diagnostic. */
16222 cp_lexer_consume_token (parser->lexer);
16223 error ("spurious %<>>%>, use %<>%> to terminate "
16224 "a template argument list");
16227 else
16228 cp_parser_skip_to_end_of_template_parameter_list (parser);
16229 /* The `>' token might be a greater-than operator again now. */
16230 parser->greater_than_is_operator_p
16231 = saved_greater_than_is_operator_p;
16232 /* Restore the SAVED_SCOPE. */
16233 parser->scope = saved_scope;
16234 parser->qualifying_scope = saved_qualifying_scope;
16235 parser->object_scope = saved_object_scope;
16236 skip_evaluation = saved_skip_evaluation;
16238 return arguments;
16241 /* MEMBER_FUNCTION is a member function, or a friend. If default
16242 arguments, or the body of the function have not yet been parsed,
16243 parse them now. */
16245 static void
16246 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
16248 /* If this member is a template, get the underlying
16249 FUNCTION_DECL. */
16250 if (DECL_FUNCTION_TEMPLATE_P (member_function))
16251 member_function = DECL_TEMPLATE_RESULT (member_function);
16253 /* There should not be any class definitions in progress at this
16254 point; the bodies of members are only parsed outside of all class
16255 definitions. */
16256 gcc_assert (parser->num_classes_being_defined == 0);
16257 /* While we're parsing the member functions we might encounter more
16258 classes. We want to handle them right away, but we don't want
16259 them getting mixed up with functions that are currently in the
16260 queue. */
16261 parser->unparsed_functions_queues
16262 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
16264 /* Make sure that any template parameters are in scope. */
16265 maybe_begin_member_template_processing (member_function);
16267 /* If the body of the function has not yet been parsed, parse it
16268 now. */
16269 if (DECL_PENDING_INLINE_P (member_function))
16271 tree function_scope;
16272 cp_token_cache *tokens;
16274 /* The function is no longer pending; we are processing it. */
16275 tokens = DECL_PENDING_INLINE_INFO (member_function);
16276 DECL_PENDING_INLINE_INFO (member_function) = NULL;
16277 DECL_PENDING_INLINE_P (member_function) = 0;
16279 /* If this is a local class, enter the scope of the containing
16280 function. */
16281 function_scope = current_function_decl;
16282 if (function_scope)
16283 push_function_context_to (function_scope);
16286 /* Push the body of the function onto the lexer stack. */
16287 cp_parser_push_lexer_for_tokens (parser, tokens);
16289 /* Let the front end know that we going to be defining this
16290 function. */
16291 start_preparsed_function (member_function, NULL_TREE,
16292 SF_PRE_PARSED | SF_INCLASS_INLINE);
16294 /* Don't do access checking if it is a templated function. */
16295 if (processing_template_decl)
16296 push_deferring_access_checks (dk_no_check);
16298 /* Now, parse the body of the function. */
16299 cp_parser_function_definition_after_declarator (parser,
16300 /*inline_p=*/true);
16302 if (processing_template_decl)
16303 pop_deferring_access_checks ();
16305 /* Leave the scope of the containing function. */
16306 if (function_scope)
16307 pop_function_context_from (function_scope);
16308 cp_parser_pop_lexer (parser);
16311 /* Remove any template parameters from the symbol table. */
16312 maybe_end_member_template_processing ();
16314 /* Restore the queue. */
16315 parser->unparsed_functions_queues
16316 = TREE_CHAIN (parser->unparsed_functions_queues);
16319 /* If DECL contains any default args, remember it on the unparsed
16320 functions queue. */
16322 static void
16323 cp_parser_save_default_args (cp_parser* parser, tree decl)
16325 tree probe;
16327 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
16328 probe;
16329 probe = TREE_CHAIN (probe))
16330 if (TREE_PURPOSE (probe))
16332 TREE_PURPOSE (parser->unparsed_functions_queues)
16333 = tree_cons (current_class_type, decl,
16334 TREE_PURPOSE (parser->unparsed_functions_queues));
16335 break;
16339 /* FN is a FUNCTION_DECL which may contains a parameter with an
16340 unparsed DEFAULT_ARG. Parse the default args now. This function
16341 assumes that the current scope is the scope in which the default
16342 argument should be processed. */
16344 static void
16345 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
16347 bool saved_local_variables_forbidden_p;
16348 tree parm;
16350 /* While we're parsing the default args, we might (due to the
16351 statement expression extension) encounter more classes. We want
16352 to handle them right away, but we don't want them getting mixed
16353 up with default args that are currently in the queue. */
16354 parser->unparsed_functions_queues
16355 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
16357 /* Local variable names (and the `this' keyword) may not appear
16358 in a default argument. */
16359 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
16360 parser->local_variables_forbidden_p = true;
16362 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
16363 parm;
16364 parm = TREE_CHAIN (parm))
16366 cp_token_cache *tokens;
16367 tree default_arg = TREE_PURPOSE (parm);
16368 tree parsed_arg;
16369 VEC(tree,gc) *insts;
16370 tree copy;
16371 unsigned ix;
16373 if (!default_arg)
16374 continue;
16376 if (TREE_CODE (default_arg) != DEFAULT_ARG)
16377 /* This can happen for a friend declaration for a function
16378 already declared with default arguments. */
16379 continue;
16381 /* Push the saved tokens for the default argument onto the parser's
16382 lexer stack. */
16383 tokens = DEFARG_TOKENS (default_arg);
16384 cp_parser_push_lexer_for_tokens (parser, tokens);
16386 /* Parse the assignment-expression. */
16387 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
16389 if (!processing_template_decl)
16390 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
16392 TREE_PURPOSE (parm) = parsed_arg;
16394 /* Update any instantiations we've already created. */
16395 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
16396 VEC_iterate (tree, insts, ix, copy); ix++)
16397 TREE_PURPOSE (copy) = parsed_arg;
16399 /* If the token stream has not been completely used up, then
16400 there was extra junk after the end of the default
16401 argument. */
16402 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
16403 cp_parser_error (parser, "expected %<,%>");
16405 /* Revert to the main lexer. */
16406 cp_parser_pop_lexer (parser);
16409 /* Make sure no default arg is missing. */
16410 check_default_args (fn);
16412 /* Restore the state of local_variables_forbidden_p. */
16413 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16415 /* Restore the queue. */
16416 parser->unparsed_functions_queues
16417 = TREE_CHAIN (parser->unparsed_functions_queues);
16420 /* Parse the operand of `sizeof' (or a similar operator). Returns
16421 either a TYPE or an expression, depending on the form of the
16422 input. The KEYWORD indicates which kind of expression we have
16423 encountered. */
16425 static tree
16426 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
16428 static const char *format;
16429 tree expr = NULL_TREE;
16430 const char *saved_message;
16431 bool saved_integral_constant_expression_p;
16432 bool saved_non_integral_constant_expression_p;
16434 /* Initialize FORMAT the first time we get here. */
16435 if (!format)
16436 format = "types may not be defined in '%s' expressions";
16438 /* Types cannot be defined in a `sizeof' expression. Save away the
16439 old message. */
16440 saved_message = parser->type_definition_forbidden_message;
16441 /* And create the new one. */
16442 parser->type_definition_forbidden_message
16443 = XNEWVEC (const char, strlen (format)
16444 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
16445 + 1 /* `\0' */);
16446 sprintf ((char *) parser->type_definition_forbidden_message,
16447 format, IDENTIFIER_POINTER (ridpointers[keyword]));
16449 /* The restrictions on constant-expressions do not apply inside
16450 sizeof expressions. */
16451 saved_integral_constant_expression_p
16452 = parser->integral_constant_expression_p;
16453 saved_non_integral_constant_expression_p
16454 = parser->non_integral_constant_expression_p;
16455 parser->integral_constant_expression_p = false;
16457 /* Do not actually evaluate the expression. */
16458 ++skip_evaluation;
16459 /* If it's a `(', then we might be looking at the type-id
16460 construction. */
16461 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16463 tree type;
16464 bool saved_in_type_id_in_expr_p;
16466 /* We can't be sure yet whether we're looking at a type-id or an
16467 expression. */
16468 cp_parser_parse_tentatively (parser);
16469 /* Consume the `('. */
16470 cp_lexer_consume_token (parser->lexer);
16471 /* Parse the type-id. */
16472 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
16473 parser->in_type_id_in_expr_p = true;
16474 type = cp_parser_type_id (parser);
16475 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
16476 /* Now, look for the trailing `)'. */
16477 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16478 /* If all went well, then we're done. */
16479 if (cp_parser_parse_definitely (parser))
16481 cp_decl_specifier_seq decl_specs;
16483 /* Build a trivial decl-specifier-seq. */
16484 clear_decl_specs (&decl_specs);
16485 decl_specs.type = type;
16487 /* Call grokdeclarator to figure out what type this is. */
16488 expr = grokdeclarator (NULL,
16489 &decl_specs,
16490 TYPENAME,
16491 /*initialized=*/0,
16492 /*attrlist=*/NULL);
16496 /* If the type-id production did not work out, then we must be
16497 looking at the unary-expression production. */
16498 if (!expr)
16499 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
16500 /*cast_p=*/false);
16501 /* Go back to evaluating expressions. */
16502 --skip_evaluation;
16504 /* Free the message we created. */
16505 free ((char *) parser->type_definition_forbidden_message);
16506 /* And restore the old one. */
16507 parser->type_definition_forbidden_message = saved_message;
16508 parser->integral_constant_expression_p
16509 = saved_integral_constant_expression_p;
16510 parser->non_integral_constant_expression_p
16511 = saved_non_integral_constant_expression_p;
16513 return expr;
16516 /* If the current declaration has no declarator, return true. */
16518 static bool
16519 cp_parser_declares_only_class_p (cp_parser *parser)
16521 /* If the next token is a `;' or a `,' then there is no
16522 declarator. */
16523 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16524 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16527 /* Update the DECL_SPECS to reflect the storage class indicated by
16528 KEYWORD. */
16530 static void
16531 cp_parser_set_storage_class (cp_parser *parser,
16532 cp_decl_specifier_seq *decl_specs,
16533 enum rid keyword)
16535 cp_storage_class storage_class;
16537 if (parser->in_unbraced_linkage_specification_p)
16539 error ("invalid use of %qD in linkage specification",
16540 ridpointers[keyword]);
16541 return;
16543 else if (decl_specs->storage_class != sc_none)
16545 decl_specs->conflicting_specifiers_p = true;
16546 return;
16549 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
16550 && decl_specs->specs[(int) ds_thread])
16552 error ("%<__thread%> before %qD", ridpointers[keyword]);
16553 decl_specs->specs[(int) ds_thread] = 0;
16556 switch (keyword)
16558 case RID_AUTO:
16559 storage_class = sc_auto;
16560 break;
16561 case RID_REGISTER:
16562 storage_class = sc_register;
16563 break;
16564 case RID_STATIC:
16565 storage_class = sc_static;
16566 break;
16567 case RID_EXTERN:
16568 storage_class = sc_extern;
16569 break;
16570 case RID_MUTABLE:
16571 storage_class = sc_mutable;
16572 break;
16573 default:
16574 gcc_unreachable ();
16576 decl_specs->storage_class = storage_class;
16578 /* A storage class specifier cannot be applied alongside a typedef
16579 specifier. If there is a typedef specifier present then set
16580 conflicting_specifiers_p which will trigger an error later
16581 on in grokdeclarator. */
16582 if (decl_specs->specs[(int)ds_typedef])
16583 decl_specs->conflicting_specifiers_p = true;
16586 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
16587 is true, the type is a user-defined type; otherwise it is a
16588 built-in type specified by a keyword. */
16590 static void
16591 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
16592 tree type_spec,
16593 bool user_defined_p)
16595 decl_specs->any_specifiers_p = true;
16597 /* If the user tries to redeclare bool or wchar_t (with, for
16598 example, in "typedef int wchar_t;") we remember that this is what
16599 happened. In system headers, we ignore these declarations so
16600 that G++ can work with system headers that are not C++-safe. */
16601 if (decl_specs->specs[(int) ds_typedef]
16602 && !user_defined_p
16603 && (type_spec == boolean_type_node
16604 || type_spec == wchar_type_node)
16605 && (decl_specs->type
16606 || decl_specs->specs[(int) ds_long]
16607 || decl_specs->specs[(int) ds_short]
16608 || decl_specs->specs[(int) ds_unsigned]
16609 || decl_specs->specs[(int) ds_signed]))
16611 decl_specs->redefined_builtin_type = type_spec;
16612 if (!decl_specs->type)
16614 decl_specs->type = type_spec;
16615 decl_specs->user_defined_type_p = false;
16618 else if (decl_specs->type)
16619 decl_specs->multiple_types_p = true;
16620 else
16622 decl_specs->type = type_spec;
16623 decl_specs->user_defined_type_p = user_defined_p;
16624 decl_specs->redefined_builtin_type = NULL_TREE;
16628 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
16629 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
16631 static bool
16632 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
16634 return decl_specifiers->specs[(int) ds_friend] != 0;
16637 /* If the next token is of the indicated TYPE, consume it. Otherwise,
16638 issue an error message indicating that TOKEN_DESC was expected.
16640 Returns the token consumed, if the token had the appropriate type.
16641 Otherwise, returns NULL. */
16643 static cp_token *
16644 cp_parser_require (cp_parser* parser,
16645 enum cpp_ttype type,
16646 const char* token_desc)
16648 if (cp_lexer_next_token_is (parser->lexer, type))
16649 return cp_lexer_consume_token (parser->lexer);
16650 else
16652 /* Output the MESSAGE -- unless we're parsing tentatively. */
16653 if (!cp_parser_simulate_error (parser))
16655 char *message = concat ("expected ", token_desc, NULL);
16656 cp_parser_error (parser, message);
16657 free (message);
16659 return NULL;
16663 /* An error message is produced if the next token is not '>'.
16664 All further tokens are skipped until the desired token is
16665 found or '{', '}', ';' or an unbalanced ')' or ']'. */
16667 static void
16668 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
16670 /* Current level of '< ... >'. */
16671 unsigned level = 0;
16672 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
16673 unsigned nesting_depth = 0;
16675 /* Are we ready, yet? If not, issue error message. */
16676 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
16677 return;
16679 /* Skip tokens until the desired token is found. */
16680 while (true)
16682 /* Peek at the next token. */
16683 switch (cp_lexer_peek_token (parser->lexer)->type)
16685 case CPP_LESS:
16686 if (!nesting_depth)
16687 ++level;
16688 break;
16690 case CPP_GREATER:
16691 if (!nesting_depth && level-- == 0)
16693 /* We've reached the token we want, consume it and stop. */
16694 cp_lexer_consume_token (parser->lexer);
16695 return;
16697 break;
16699 case CPP_OPEN_PAREN:
16700 case CPP_OPEN_SQUARE:
16701 ++nesting_depth;
16702 break;
16704 case CPP_CLOSE_PAREN:
16705 case CPP_CLOSE_SQUARE:
16706 if (nesting_depth-- == 0)
16707 return;
16708 break;
16710 case CPP_EOF:
16711 case CPP_PRAGMA_EOL:
16712 case CPP_SEMICOLON:
16713 case CPP_OPEN_BRACE:
16714 case CPP_CLOSE_BRACE:
16715 /* The '>' was probably forgotten, don't look further. */
16716 return;
16718 default:
16719 break;
16722 /* Consume this token. */
16723 cp_lexer_consume_token (parser->lexer);
16727 /* If the next token is the indicated keyword, consume it. Otherwise,
16728 issue an error message indicating that TOKEN_DESC was expected.
16730 Returns the token consumed, if the token had the appropriate type.
16731 Otherwise, returns NULL. */
16733 static cp_token *
16734 cp_parser_require_keyword (cp_parser* parser,
16735 enum rid keyword,
16736 const char* token_desc)
16738 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
16740 if (token && token->keyword != keyword)
16742 dyn_string_t error_msg;
16744 /* Format the error message. */
16745 error_msg = dyn_string_new (0);
16746 dyn_string_append_cstr (error_msg, "expected ");
16747 dyn_string_append_cstr (error_msg, token_desc);
16748 cp_parser_error (parser, error_msg->s);
16749 dyn_string_delete (error_msg);
16750 return NULL;
16753 return token;
16756 /* Returns TRUE iff TOKEN is a token that can begin the body of a
16757 function-definition. */
16759 static bool
16760 cp_parser_token_starts_function_definition_p (cp_token* token)
16762 return (/* An ordinary function-body begins with an `{'. */
16763 token->type == CPP_OPEN_BRACE
16764 /* A ctor-initializer begins with a `:'. */
16765 || token->type == CPP_COLON
16766 /* A function-try-block begins with `try'. */
16767 || token->keyword == RID_TRY
16768 /* The named return value extension begins with `return'. */
16769 || token->keyword == RID_RETURN);
16772 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
16773 definition. */
16775 static bool
16776 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
16778 cp_token *token;
16780 token = cp_lexer_peek_token (parser->lexer);
16781 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
16784 /* Returns TRUE iff the next token is the "," or ">" ending a
16785 template-argument. */
16787 static bool
16788 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
16790 cp_token *token;
16792 token = cp_lexer_peek_token (parser->lexer);
16793 return (token->type == CPP_COMMA || token->type == CPP_GREATER);
16796 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
16797 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
16799 static bool
16800 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
16801 size_t n)
16803 cp_token *token;
16805 token = cp_lexer_peek_nth_token (parser->lexer, n);
16806 if (token->type == CPP_LESS)
16807 return true;
16808 /* Check for the sequence `<::' in the original code. It would be lexed as
16809 `[:', where `[' is a digraph, and there is no whitespace before
16810 `:'. */
16811 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
16813 cp_token *token2;
16814 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
16815 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
16816 return true;
16818 return false;
16821 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
16822 or none_type otherwise. */
16824 static enum tag_types
16825 cp_parser_token_is_class_key (cp_token* token)
16827 switch (token->keyword)
16829 case RID_CLASS:
16830 return class_type;
16831 case RID_STRUCT:
16832 return record_type;
16833 case RID_UNION:
16834 return union_type;
16836 default:
16837 return none_type;
16841 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
16843 static void
16844 cp_parser_check_class_key (enum tag_types class_key, tree type)
16846 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
16847 pedwarn ("%qs tag used in naming %q#T",
16848 class_key == union_type ? "union"
16849 : class_key == record_type ? "struct" : "class",
16850 type);
16853 /* Issue an error message if DECL is redeclared with different
16854 access than its original declaration [class.access.spec/3].
16855 This applies to nested classes and nested class templates.
16856 [class.mem/1]. */
16858 static void
16859 cp_parser_check_access_in_redeclaration (tree decl)
16861 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
16862 return;
16864 if ((TREE_PRIVATE (decl)
16865 != (current_access_specifier == access_private_node))
16866 || (TREE_PROTECTED (decl)
16867 != (current_access_specifier == access_protected_node)))
16868 error ("%qD redeclared with different access", decl);
16871 /* Look for the `template' keyword, as a syntactic disambiguator.
16872 Return TRUE iff it is present, in which case it will be
16873 consumed. */
16875 static bool
16876 cp_parser_optional_template_keyword (cp_parser *parser)
16878 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16880 /* The `template' keyword can only be used within templates;
16881 outside templates the parser can always figure out what is a
16882 template and what is not. */
16883 if (!processing_template_decl)
16885 error ("%<template%> (as a disambiguator) is only allowed "
16886 "within templates");
16887 /* If this part of the token stream is rescanned, the same
16888 error message would be generated. So, we purge the token
16889 from the stream. */
16890 cp_lexer_purge_token (parser->lexer);
16891 return false;
16893 else
16895 /* Consume the `template' keyword. */
16896 cp_lexer_consume_token (parser->lexer);
16897 return true;
16901 return false;
16904 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
16905 set PARSER->SCOPE, and perform other related actions. */
16907 static void
16908 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
16910 int i;
16911 struct tree_check *check_value;
16912 deferred_access_check *chk;
16913 VEC (deferred_access_check,gc) *checks;
16915 /* Get the stored value. */
16916 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
16917 /* Perform any access checks that were deferred. */
16918 checks = check_value->checks;
16919 if (checks)
16921 for (i = 0 ;
16922 VEC_iterate (deferred_access_check, checks, i, chk) ;
16923 ++i)
16925 perform_or_defer_access_check (chk->binfo,
16926 chk->decl,
16927 chk->diag_decl);
16930 /* Set the scope from the stored value. */
16931 parser->scope = check_value->value;
16932 parser->qualifying_scope = check_value->qualifying_scope;
16933 parser->object_scope = NULL_TREE;
16936 /* Consume tokens up through a non-nested END token. */
16938 static void
16939 cp_parser_cache_group (cp_parser *parser,
16940 enum cpp_ttype end,
16941 unsigned depth)
16943 while (true)
16945 cp_token *token;
16947 /* Abort a parenthesized expression if we encounter a brace. */
16948 if ((end == CPP_CLOSE_PAREN || depth == 0)
16949 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16950 return;
16951 /* If we've reached the end of the file, stop. */
16952 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)
16953 || (end != CPP_PRAGMA_EOL
16954 && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)))
16955 return;
16956 /* Consume the next token. */
16957 token = cp_lexer_consume_token (parser->lexer);
16958 /* See if it starts a new group. */
16959 if (token->type == CPP_OPEN_BRACE)
16961 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
16962 if (depth == 0)
16963 return;
16965 else if (token->type == CPP_OPEN_PAREN)
16966 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
16967 else if (token->type == CPP_PRAGMA)
16968 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
16969 else if (token->type == end)
16970 return;
16974 /* Begin parsing tentatively. We always save tokens while parsing
16975 tentatively so that if the tentative parsing fails we can restore the
16976 tokens. */
16978 static void
16979 cp_parser_parse_tentatively (cp_parser* parser)
16981 /* Enter a new parsing context. */
16982 parser->context = cp_parser_context_new (parser->context);
16983 /* Begin saving tokens. */
16984 cp_lexer_save_tokens (parser->lexer);
16985 /* In order to avoid repetitive access control error messages,
16986 access checks are queued up until we are no longer parsing
16987 tentatively. */
16988 push_deferring_access_checks (dk_deferred);
16991 /* Commit to the currently active tentative parse. */
16993 static void
16994 cp_parser_commit_to_tentative_parse (cp_parser* parser)
16996 cp_parser_context *context;
16997 cp_lexer *lexer;
16999 /* Mark all of the levels as committed. */
17000 lexer = parser->lexer;
17001 for (context = parser->context; context->next; context = context->next)
17003 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
17004 break;
17005 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
17006 while (!cp_lexer_saving_tokens (lexer))
17007 lexer = lexer->next;
17008 cp_lexer_commit_tokens (lexer);
17012 /* Abort the currently active tentative parse. All consumed tokens
17013 will be rolled back, and no diagnostics will be issued. */
17015 static void
17016 cp_parser_abort_tentative_parse (cp_parser* parser)
17018 cp_parser_simulate_error (parser);
17019 /* Now, pretend that we want to see if the construct was
17020 successfully parsed. */
17021 cp_parser_parse_definitely (parser);
17024 /* Stop parsing tentatively. If a parse error has occurred, restore the
17025 token stream. Otherwise, commit to the tokens we have consumed.
17026 Returns true if no error occurred; false otherwise. */
17028 static bool
17029 cp_parser_parse_definitely (cp_parser* parser)
17031 bool error_occurred;
17032 cp_parser_context *context;
17034 /* Remember whether or not an error occurred, since we are about to
17035 destroy that information. */
17036 error_occurred = cp_parser_error_occurred (parser);
17037 /* Remove the topmost context from the stack. */
17038 context = parser->context;
17039 parser->context = context->next;
17040 /* If no parse errors occurred, commit to the tentative parse. */
17041 if (!error_occurred)
17043 /* Commit to the tokens read tentatively, unless that was
17044 already done. */
17045 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
17046 cp_lexer_commit_tokens (parser->lexer);
17048 pop_to_parent_deferring_access_checks ();
17050 /* Otherwise, if errors occurred, roll back our state so that things
17051 are just as they were before we began the tentative parse. */
17052 else
17054 cp_lexer_rollback_tokens (parser->lexer);
17055 pop_deferring_access_checks ();
17057 /* Add the context to the front of the free list. */
17058 context->next = cp_parser_context_free_list;
17059 cp_parser_context_free_list = context;
17061 return !error_occurred;
17064 /* Returns true if we are parsing tentatively and are not committed to
17065 this tentative parse. */
17067 static bool
17068 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
17070 return (cp_parser_parsing_tentatively (parser)
17071 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
17074 /* Returns nonzero iff an error has occurred during the most recent
17075 tentative parse. */
17077 static bool
17078 cp_parser_error_occurred (cp_parser* parser)
17080 return (cp_parser_parsing_tentatively (parser)
17081 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
17084 /* Returns nonzero if GNU extensions are allowed. */
17086 static bool
17087 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
17089 return parser->allow_gnu_extensions_p;
17092 /* Objective-C++ Productions */
17095 /* Parse an Objective-C expression, which feeds into a primary-expression
17096 above.
17098 objc-expression:
17099 objc-message-expression
17100 objc-string-literal
17101 objc-encode-expression
17102 objc-protocol-expression
17103 objc-selector-expression
17105 Returns a tree representation of the expression. */
17107 static tree
17108 cp_parser_objc_expression (cp_parser* parser)
17110 /* Try to figure out what kind of declaration is present. */
17111 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17113 switch (kwd->type)
17115 case CPP_OPEN_SQUARE:
17116 return cp_parser_objc_message_expression (parser);
17118 case CPP_OBJC_STRING:
17119 kwd = cp_lexer_consume_token (parser->lexer);
17120 return objc_build_string_object (kwd->u.value);
17122 case CPP_KEYWORD:
17123 switch (kwd->keyword)
17125 case RID_AT_ENCODE:
17126 return cp_parser_objc_encode_expression (parser);
17128 case RID_AT_PROTOCOL:
17129 return cp_parser_objc_protocol_expression (parser);
17131 case RID_AT_SELECTOR:
17132 return cp_parser_objc_selector_expression (parser);
17134 default:
17135 break;
17137 default:
17138 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
17139 cp_parser_skip_to_end_of_block_or_statement (parser);
17142 return error_mark_node;
17145 /* Parse an Objective-C message expression.
17147 objc-message-expression:
17148 [ objc-message-receiver objc-message-args ]
17150 Returns a representation of an Objective-C message. */
17152 static tree
17153 cp_parser_objc_message_expression (cp_parser* parser)
17155 tree receiver, messageargs;
17157 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
17158 receiver = cp_parser_objc_message_receiver (parser);
17159 messageargs = cp_parser_objc_message_args (parser);
17160 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
17162 return objc_build_message_expr (build_tree_list (receiver, messageargs));
17165 /* Parse an objc-message-receiver.
17167 objc-message-receiver:
17168 expression
17169 simple-type-specifier
17171 Returns a representation of the type or expression. */
17173 static tree
17174 cp_parser_objc_message_receiver (cp_parser* parser)
17176 tree rcv;
17178 /* An Objective-C message receiver may be either (1) a type
17179 or (2) an expression. */
17180 cp_parser_parse_tentatively (parser);
17181 rcv = cp_parser_expression (parser, false);
17183 if (cp_parser_parse_definitely (parser))
17184 return rcv;
17186 rcv = cp_parser_simple_type_specifier (parser,
17187 /*decl_specs=*/NULL,
17188 CP_PARSER_FLAGS_NONE);
17190 return objc_get_class_reference (rcv);
17193 /* Parse the arguments and selectors comprising an Objective-C message.
17195 objc-message-args:
17196 objc-selector
17197 objc-selector-args
17198 objc-selector-args , objc-comma-args
17200 objc-selector-args:
17201 objc-selector [opt] : assignment-expression
17202 objc-selector-args objc-selector [opt] : assignment-expression
17204 objc-comma-args:
17205 assignment-expression
17206 objc-comma-args , assignment-expression
17208 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
17209 selector arguments and TREE_VALUE containing a list of comma
17210 arguments. */
17212 static tree
17213 cp_parser_objc_message_args (cp_parser* parser)
17215 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
17216 bool maybe_unary_selector_p = true;
17217 cp_token *token = cp_lexer_peek_token (parser->lexer);
17219 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
17221 tree selector = NULL_TREE, arg;
17223 if (token->type != CPP_COLON)
17224 selector = cp_parser_objc_selector (parser);
17226 /* Detect if we have a unary selector. */
17227 if (maybe_unary_selector_p
17228 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17229 return build_tree_list (selector, NULL_TREE);
17231 maybe_unary_selector_p = false;
17232 cp_parser_require (parser, CPP_COLON, "`:'");
17233 arg = cp_parser_assignment_expression (parser, false);
17235 sel_args
17236 = chainon (sel_args,
17237 build_tree_list (selector, arg));
17239 token = cp_lexer_peek_token (parser->lexer);
17242 /* Handle non-selector arguments, if any. */
17243 while (token->type == CPP_COMMA)
17245 tree arg;
17247 cp_lexer_consume_token (parser->lexer);
17248 arg = cp_parser_assignment_expression (parser, false);
17250 addl_args
17251 = chainon (addl_args,
17252 build_tree_list (NULL_TREE, arg));
17254 token = cp_lexer_peek_token (parser->lexer);
17257 return build_tree_list (sel_args, addl_args);
17260 /* Parse an Objective-C encode expression.
17262 objc-encode-expression:
17263 @encode objc-typename
17265 Returns an encoded representation of the type argument. */
17267 static tree
17268 cp_parser_objc_encode_expression (cp_parser* parser)
17270 tree type;
17272 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
17273 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17274 type = complete_type (cp_parser_type_id (parser));
17275 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17277 if (!type)
17279 error ("%<@encode%> must specify a type as an argument");
17280 return error_mark_node;
17283 return objc_build_encode_expr (type);
17286 /* Parse an Objective-C @defs expression. */
17288 static tree
17289 cp_parser_objc_defs_expression (cp_parser *parser)
17291 tree name;
17293 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
17294 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17295 name = cp_parser_identifier (parser);
17296 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17298 return objc_get_class_ivars (name);
17301 /* Parse an Objective-C protocol expression.
17303 objc-protocol-expression:
17304 @protocol ( identifier )
17306 Returns a representation of the protocol expression. */
17308 static tree
17309 cp_parser_objc_protocol_expression (cp_parser* parser)
17311 tree proto;
17313 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
17314 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17315 proto = cp_parser_identifier (parser);
17316 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17318 return objc_build_protocol_expr (proto);
17321 /* Parse an Objective-C selector expression.
17323 objc-selector-expression:
17324 @selector ( objc-method-signature )
17326 objc-method-signature:
17327 objc-selector
17328 objc-selector-seq
17330 objc-selector-seq:
17331 objc-selector :
17332 objc-selector-seq objc-selector :
17334 Returns a representation of the method selector. */
17336 static tree
17337 cp_parser_objc_selector_expression (cp_parser* parser)
17339 tree sel_seq = NULL_TREE;
17340 bool maybe_unary_selector_p = true;
17341 cp_token *token;
17343 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
17344 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17345 token = cp_lexer_peek_token (parser->lexer);
17347 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
17348 || token->type == CPP_SCOPE)
17350 tree selector = NULL_TREE;
17352 if (token->type != CPP_COLON
17353 || token->type == CPP_SCOPE)
17354 selector = cp_parser_objc_selector (parser);
17356 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
17357 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
17359 /* Detect if we have a unary selector. */
17360 if (maybe_unary_selector_p)
17362 sel_seq = selector;
17363 goto finish_selector;
17365 else
17367 cp_parser_error (parser, "expected %<:%>");
17370 maybe_unary_selector_p = false;
17371 token = cp_lexer_consume_token (parser->lexer);
17373 if (token->type == CPP_SCOPE)
17375 sel_seq
17376 = chainon (sel_seq,
17377 build_tree_list (selector, NULL_TREE));
17378 sel_seq
17379 = chainon (sel_seq,
17380 build_tree_list (NULL_TREE, NULL_TREE));
17382 else
17383 sel_seq
17384 = chainon (sel_seq,
17385 build_tree_list (selector, NULL_TREE));
17387 token = cp_lexer_peek_token (parser->lexer);
17390 finish_selector:
17391 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17393 return objc_build_selector_expr (sel_seq);
17396 /* Parse a list of identifiers.
17398 objc-identifier-list:
17399 identifier
17400 objc-identifier-list , identifier
17402 Returns a TREE_LIST of identifier nodes. */
17404 static tree
17405 cp_parser_objc_identifier_list (cp_parser* parser)
17407 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
17408 cp_token *sep = cp_lexer_peek_token (parser->lexer);
17410 while (sep->type == CPP_COMMA)
17412 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17413 list = chainon (list,
17414 build_tree_list (NULL_TREE,
17415 cp_parser_identifier (parser)));
17416 sep = cp_lexer_peek_token (parser->lexer);
17419 return list;
17422 /* Parse an Objective-C alias declaration.
17424 objc-alias-declaration:
17425 @compatibility_alias identifier identifier ;
17427 This function registers the alias mapping with the Objective-C front end.
17428 It returns nothing. */
17430 static void
17431 cp_parser_objc_alias_declaration (cp_parser* parser)
17433 tree alias, orig;
17435 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
17436 alias = cp_parser_identifier (parser);
17437 orig = cp_parser_identifier (parser);
17438 objc_declare_alias (alias, orig);
17439 cp_parser_consume_semicolon_at_end_of_statement (parser);
17442 /* Parse an Objective-C class forward-declaration.
17444 objc-class-declaration:
17445 @class objc-identifier-list ;
17447 The function registers the forward declarations with the Objective-C
17448 front end. It returns nothing. */
17450 static void
17451 cp_parser_objc_class_declaration (cp_parser* parser)
17453 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
17454 objc_declare_class (cp_parser_objc_identifier_list (parser));
17455 cp_parser_consume_semicolon_at_end_of_statement (parser);
17458 /* Parse a list of Objective-C protocol references.
17460 objc-protocol-refs-opt:
17461 objc-protocol-refs [opt]
17463 objc-protocol-refs:
17464 < objc-identifier-list >
17466 Returns a TREE_LIST of identifiers, if any. */
17468 static tree
17469 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
17471 tree protorefs = NULL_TREE;
17473 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
17475 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
17476 protorefs = cp_parser_objc_identifier_list (parser);
17477 cp_parser_require (parser, CPP_GREATER, "`>'");
17480 return protorefs;
17483 /* Parse a Objective-C visibility specification. */
17485 static void
17486 cp_parser_objc_visibility_spec (cp_parser* parser)
17488 cp_token *vis = cp_lexer_peek_token (parser->lexer);
17490 switch (vis->keyword)
17492 case RID_AT_PRIVATE:
17493 objc_set_visibility (2);
17494 break;
17495 case RID_AT_PROTECTED:
17496 objc_set_visibility (0);
17497 break;
17498 case RID_AT_PUBLIC:
17499 objc_set_visibility (1);
17500 break;
17501 default:
17502 return;
17505 /* Eat '@private'/'@protected'/'@public'. */
17506 cp_lexer_consume_token (parser->lexer);
17509 /* Parse an Objective-C method type. */
17511 static void
17512 cp_parser_objc_method_type (cp_parser* parser)
17514 objc_set_method_type
17515 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
17516 ? PLUS_EXPR
17517 : MINUS_EXPR);
17520 /* Parse an Objective-C protocol qualifier. */
17522 static tree
17523 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
17525 tree quals = NULL_TREE, node;
17526 cp_token *token = cp_lexer_peek_token (parser->lexer);
17528 node = token->u.value;
17530 while (node && TREE_CODE (node) == IDENTIFIER_NODE
17531 && (node == ridpointers [(int) RID_IN]
17532 || node == ridpointers [(int) RID_OUT]
17533 || node == ridpointers [(int) RID_INOUT]
17534 || node == ridpointers [(int) RID_BYCOPY]
17535 || node == ridpointers [(int) RID_BYREF]
17536 || node == ridpointers [(int) RID_ONEWAY]))
17538 quals = tree_cons (NULL_TREE, node, quals);
17539 cp_lexer_consume_token (parser->lexer);
17540 token = cp_lexer_peek_token (parser->lexer);
17541 node = token->u.value;
17544 return quals;
17547 /* Parse an Objective-C typename. */
17549 static tree
17550 cp_parser_objc_typename (cp_parser* parser)
17552 tree typename = NULL_TREE;
17554 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17556 tree proto_quals, cp_type = NULL_TREE;
17558 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
17559 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
17561 /* An ObjC type name may consist of just protocol qualifiers, in which
17562 case the type shall default to 'id'. */
17563 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
17564 cp_type = cp_parser_type_id (parser);
17566 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17567 typename = build_tree_list (proto_quals, cp_type);
17570 return typename;
17573 /* Check to see if TYPE refers to an Objective-C selector name. */
17575 static bool
17576 cp_parser_objc_selector_p (enum cpp_ttype type)
17578 return (type == CPP_NAME || type == CPP_KEYWORD
17579 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
17580 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
17581 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
17582 || type == CPP_XOR || type == CPP_XOR_EQ);
17585 /* Parse an Objective-C selector. */
17587 static tree
17588 cp_parser_objc_selector (cp_parser* parser)
17590 cp_token *token = cp_lexer_consume_token (parser->lexer);
17592 if (!cp_parser_objc_selector_p (token->type))
17594 error ("invalid Objective-C++ selector name");
17595 return error_mark_node;
17598 /* C++ operator names are allowed to appear in ObjC selectors. */
17599 switch (token->type)
17601 case CPP_AND_AND: return get_identifier ("and");
17602 case CPP_AND_EQ: return get_identifier ("and_eq");
17603 case CPP_AND: return get_identifier ("bitand");
17604 case CPP_OR: return get_identifier ("bitor");
17605 case CPP_COMPL: return get_identifier ("compl");
17606 case CPP_NOT: return get_identifier ("not");
17607 case CPP_NOT_EQ: return get_identifier ("not_eq");
17608 case CPP_OR_OR: return get_identifier ("or");
17609 case CPP_OR_EQ: return get_identifier ("or_eq");
17610 case CPP_XOR: return get_identifier ("xor");
17611 case CPP_XOR_EQ: return get_identifier ("xor_eq");
17612 default: return token->u.value;
17616 /* Parse an Objective-C params list. */
17618 static tree
17619 cp_parser_objc_method_keyword_params (cp_parser* parser)
17621 tree params = NULL_TREE;
17622 bool maybe_unary_selector_p = true;
17623 cp_token *token = cp_lexer_peek_token (parser->lexer);
17625 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
17627 tree selector = NULL_TREE, typename, identifier;
17629 if (token->type != CPP_COLON)
17630 selector = cp_parser_objc_selector (parser);
17632 /* Detect if we have a unary selector. */
17633 if (maybe_unary_selector_p
17634 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17635 return selector;
17637 maybe_unary_selector_p = false;
17638 cp_parser_require (parser, CPP_COLON, "`:'");
17639 typename = cp_parser_objc_typename (parser);
17640 identifier = cp_parser_identifier (parser);
17642 params
17643 = chainon (params,
17644 objc_build_keyword_decl (selector,
17645 typename,
17646 identifier));
17648 token = cp_lexer_peek_token (parser->lexer);
17651 return params;
17654 /* Parse the non-keyword Objective-C params. */
17656 static tree
17657 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
17659 tree params = make_node (TREE_LIST);
17660 cp_token *token = cp_lexer_peek_token (parser->lexer);
17661 *ellipsisp = false; /* Initially, assume no ellipsis. */
17663 while (token->type == CPP_COMMA)
17665 cp_parameter_declarator *parmdecl;
17666 tree parm;
17668 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17669 token = cp_lexer_peek_token (parser->lexer);
17671 if (token->type == CPP_ELLIPSIS)
17673 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
17674 *ellipsisp = true;
17675 break;
17678 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17679 parm = grokdeclarator (parmdecl->declarator,
17680 &parmdecl->decl_specifiers,
17681 PARM, /*initialized=*/0,
17682 /*attrlist=*/NULL);
17684 chainon (params, build_tree_list (NULL_TREE, parm));
17685 token = cp_lexer_peek_token (parser->lexer);
17688 return params;
17691 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
17693 static void
17694 cp_parser_objc_interstitial_code (cp_parser* parser)
17696 cp_token *token = cp_lexer_peek_token (parser->lexer);
17698 /* If the next token is `extern' and the following token is a string
17699 literal, then we have a linkage specification. */
17700 if (token->keyword == RID_EXTERN
17701 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
17702 cp_parser_linkage_specification (parser);
17703 /* Handle #pragma, if any. */
17704 else if (token->type == CPP_PRAGMA)
17705 cp_parser_pragma (parser, pragma_external);
17706 /* Allow stray semicolons. */
17707 else if (token->type == CPP_SEMICOLON)
17708 cp_lexer_consume_token (parser->lexer);
17709 /* Finally, try to parse a block-declaration, or a function-definition. */
17710 else
17711 cp_parser_block_declaration (parser, /*statement_p=*/false);
17714 /* Parse a method signature. */
17716 static tree
17717 cp_parser_objc_method_signature (cp_parser* parser)
17719 tree rettype, kwdparms, optparms;
17720 bool ellipsis = false;
17722 cp_parser_objc_method_type (parser);
17723 rettype = cp_parser_objc_typename (parser);
17724 kwdparms = cp_parser_objc_method_keyword_params (parser);
17725 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
17727 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
17730 /* Pars an Objective-C method prototype list. */
17732 static void
17733 cp_parser_objc_method_prototype_list (cp_parser* parser)
17735 cp_token *token = cp_lexer_peek_token (parser->lexer);
17737 while (token->keyword != RID_AT_END)
17739 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17741 objc_add_method_declaration
17742 (cp_parser_objc_method_signature (parser));
17743 cp_parser_consume_semicolon_at_end_of_statement (parser);
17745 else
17746 /* Allow for interspersed non-ObjC++ code. */
17747 cp_parser_objc_interstitial_code (parser);
17749 token = cp_lexer_peek_token (parser->lexer);
17752 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17753 objc_finish_interface ();
17756 /* Parse an Objective-C method definition list. */
17758 static void
17759 cp_parser_objc_method_definition_list (cp_parser* parser)
17761 cp_token *token = cp_lexer_peek_token (parser->lexer);
17763 while (token->keyword != RID_AT_END)
17765 tree meth;
17767 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17769 push_deferring_access_checks (dk_deferred);
17770 objc_start_method_definition
17771 (cp_parser_objc_method_signature (parser));
17773 /* For historical reasons, we accept an optional semicolon. */
17774 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17775 cp_lexer_consume_token (parser->lexer);
17777 perform_deferred_access_checks ();
17778 stop_deferring_access_checks ();
17779 meth = cp_parser_function_definition_after_declarator (parser,
17780 false);
17781 pop_deferring_access_checks ();
17782 objc_finish_method_definition (meth);
17784 else
17785 /* Allow for interspersed non-ObjC++ code. */
17786 cp_parser_objc_interstitial_code (parser);
17788 token = cp_lexer_peek_token (parser->lexer);
17791 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17792 objc_finish_implementation ();
17795 /* Parse Objective-C ivars. */
17797 static void
17798 cp_parser_objc_class_ivars (cp_parser* parser)
17800 cp_token *token = cp_lexer_peek_token (parser->lexer);
17802 if (token->type != CPP_OPEN_BRACE)
17803 return; /* No ivars specified. */
17805 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
17806 token = cp_lexer_peek_token (parser->lexer);
17808 while (token->type != CPP_CLOSE_BRACE)
17810 cp_decl_specifier_seq declspecs;
17811 int decl_class_or_enum_p;
17812 tree prefix_attributes;
17814 cp_parser_objc_visibility_spec (parser);
17816 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17817 break;
17819 cp_parser_decl_specifier_seq (parser,
17820 CP_PARSER_FLAGS_OPTIONAL,
17821 &declspecs,
17822 &decl_class_or_enum_p);
17823 prefix_attributes = declspecs.attributes;
17824 declspecs.attributes = NULL_TREE;
17826 /* Keep going until we hit the `;' at the end of the
17827 declaration. */
17828 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17830 tree width = NULL_TREE, attributes, first_attribute, decl;
17831 cp_declarator *declarator = NULL;
17832 int ctor_dtor_or_conv_p;
17834 /* Check for a (possibly unnamed) bitfield declaration. */
17835 token = cp_lexer_peek_token (parser->lexer);
17836 if (token->type == CPP_COLON)
17837 goto eat_colon;
17839 if (token->type == CPP_NAME
17840 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
17841 == CPP_COLON))
17843 /* Get the name of the bitfield. */
17844 declarator = make_id_declarator (NULL_TREE,
17845 cp_parser_identifier (parser),
17846 sfk_none);
17848 eat_colon:
17849 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
17850 /* Get the width of the bitfield. */
17851 width
17852 = cp_parser_constant_expression (parser,
17853 /*allow_non_constant=*/false,
17854 NULL);
17856 else
17858 /* Parse the declarator. */
17859 declarator
17860 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17861 &ctor_dtor_or_conv_p,
17862 /*parenthesized_p=*/NULL,
17863 /*member_p=*/false);
17866 /* Look for attributes that apply to the ivar. */
17867 attributes = cp_parser_attributes_opt (parser);
17868 /* Remember which attributes are prefix attributes and
17869 which are not. */
17870 first_attribute = attributes;
17871 /* Combine the attributes. */
17872 attributes = chainon (prefix_attributes, attributes);
17874 if (width)
17876 /* Create the bitfield declaration. */
17877 decl = grokbitfield (declarator, &declspecs, width);
17878 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
17880 else
17881 decl = grokfield (declarator, &declspecs,
17882 NULL_TREE, /*init_const_expr_p=*/false,
17883 NULL_TREE, attributes);
17885 /* Add the instance variable. */
17886 objc_add_instance_variable (decl);
17888 /* Reset PREFIX_ATTRIBUTES. */
17889 while (attributes && TREE_CHAIN (attributes) != first_attribute)
17890 attributes = TREE_CHAIN (attributes);
17891 if (attributes)
17892 TREE_CHAIN (attributes) = NULL_TREE;
17894 token = cp_lexer_peek_token (parser->lexer);
17896 if (token->type == CPP_COMMA)
17898 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17899 continue;
17901 break;
17904 cp_parser_consume_semicolon_at_end_of_statement (parser);
17905 token = cp_lexer_peek_token (parser->lexer);
17908 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
17909 /* For historical reasons, we accept an optional semicolon. */
17910 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17911 cp_lexer_consume_token (parser->lexer);
17914 /* Parse an Objective-C protocol declaration. */
17916 static void
17917 cp_parser_objc_protocol_declaration (cp_parser* parser)
17919 tree proto, protorefs;
17920 cp_token *tok;
17922 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
17923 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
17925 error ("identifier expected after %<@protocol%>");
17926 goto finish;
17929 /* See if we have a forward declaration or a definition. */
17930 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
17932 /* Try a forward declaration first. */
17933 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
17935 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
17936 finish:
17937 cp_parser_consume_semicolon_at_end_of_statement (parser);
17940 /* Ok, we got a full-fledged definition (or at least should). */
17941 else
17943 proto = cp_parser_identifier (parser);
17944 protorefs = cp_parser_objc_protocol_refs_opt (parser);
17945 objc_start_protocol (proto, protorefs);
17946 cp_parser_objc_method_prototype_list (parser);
17950 /* Parse an Objective-C superclass or category. */
17952 static void
17953 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
17954 tree *categ)
17956 cp_token *next = cp_lexer_peek_token (parser->lexer);
17958 *super = *categ = NULL_TREE;
17959 if (next->type == CPP_COLON)
17961 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
17962 *super = cp_parser_identifier (parser);
17964 else if (next->type == CPP_OPEN_PAREN)
17966 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
17967 *categ = cp_parser_identifier (parser);
17968 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17972 /* Parse an Objective-C class interface. */
17974 static void
17975 cp_parser_objc_class_interface (cp_parser* parser)
17977 tree name, super, categ, protos;
17979 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
17980 name = cp_parser_identifier (parser);
17981 cp_parser_objc_superclass_or_category (parser, &super, &categ);
17982 protos = cp_parser_objc_protocol_refs_opt (parser);
17984 /* We have either a class or a category on our hands. */
17985 if (categ)
17986 objc_start_category_interface (name, categ, protos);
17987 else
17989 objc_start_class_interface (name, super, protos);
17990 /* Handle instance variable declarations, if any. */
17991 cp_parser_objc_class_ivars (parser);
17992 objc_continue_interface ();
17995 cp_parser_objc_method_prototype_list (parser);
17998 /* Parse an Objective-C class implementation. */
18000 static void
18001 cp_parser_objc_class_implementation (cp_parser* parser)
18003 tree name, super, categ;
18005 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
18006 name = cp_parser_identifier (parser);
18007 cp_parser_objc_superclass_or_category (parser, &super, &categ);
18009 /* We have either a class or a category on our hands. */
18010 if (categ)
18011 objc_start_category_implementation (name, categ);
18012 else
18014 objc_start_class_implementation (name, super);
18015 /* Handle instance variable declarations, if any. */
18016 cp_parser_objc_class_ivars (parser);
18017 objc_continue_implementation ();
18020 cp_parser_objc_method_definition_list (parser);
18023 /* Consume the @end token and finish off the implementation. */
18025 static void
18026 cp_parser_objc_end_implementation (cp_parser* parser)
18028 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
18029 objc_finish_implementation ();
18032 /* Parse an Objective-C declaration. */
18034 static void
18035 cp_parser_objc_declaration (cp_parser* parser)
18037 /* Try to figure out what kind of declaration is present. */
18038 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18040 switch (kwd->keyword)
18042 case RID_AT_ALIAS:
18043 cp_parser_objc_alias_declaration (parser);
18044 break;
18045 case RID_AT_CLASS:
18046 cp_parser_objc_class_declaration (parser);
18047 break;
18048 case RID_AT_PROTOCOL:
18049 cp_parser_objc_protocol_declaration (parser);
18050 break;
18051 case RID_AT_INTERFACE:
18052 cp_parser_objc_class_interface (parser);
18053 break;
18054 case RID_AT_IMPLEMENTATION:
18055 cp_parser_objc_class_implementation (parser);
18056 break;
18057 case RID_AT_END:
18058 cp_parser_objc_end_implementation (parser);
18059 break;
18060 default:
18061 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
18062 cp_parser_skip_to_end_of_block_or_statement (parser);
18066 /* Parse an Objective-C try-catch-finally statement.
18068 objc-try-catch-finally-stmt:
18069 @try compound-statement objc-catch-clause-seq [opt]
18070 objc-finally-clause [opt]
18072 objc-catch-clause-seq:
18073 objc-catch-clause objc-catch-clause-seq [opt]
18075 objc-catch-clause:
18076 @catch ( exception-declaration ) compound-statement
18078 objc-finally-clause
18079 @finally compound-statement
18081 Returns NULL_TREE. */
18083 static tree
18084 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
18085 location_t location;
18086 tree stmt;
18088 cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
18089 location = cp_lexer_peek_token (parser->lexer)->location;
18090 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
18091 node, lest it get absorbed into the surrounding block. */
18092 stmt = push_stmt_list ();
18093 cp_parser_compound_statement (parser, NULL, false);
18094 objc_begin_try_stmt (location, pop_stmt_list (stmt));
18096 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
18098 cp_parameter_declarator *parmdecl;
18099 tree parm;
18101 cp_lexer_consume_token (parser->lexer);
18102 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18103 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
18104 parm = grokdeclarator (parmdecl->declarator,
18105 &parmdecl->decl_specifiers,
18106 PARM, /*initialized=*/0,
18107 /*attrlist=*/NULL);
18108 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18109 objc_begin_catch_clause (parm);
18110 cp_parser_compound_statement (parser, NULL, false);
18111 objc_finish_catch_clause ();
18114 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
18116 cp_lexer_consume_token (parser->lexer);
18117 location = cp_lexer_peek_token (parser->lexer)->location;
18118 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
18119 node, lest it get absorbed into the surrounding block. */
18120 stmt = push_stmt_list ();
18121 cp_parser_compound_statement (parser, NULL, false);
18122 objc_build_finally_clause (location, pop_stmt_list (stmt));
18125 return objc_finish_try_stmt ();
18128 /* Parse an Objective-C synchronized statement.
18130 objc-synchronized-stmt:
18131 @synchronized ( expression ) compound-statement
18133 Returns NULL_TREE. */
18135 static tree
18136 cp_parser_objc_synchronized_statement (cp_parser *parser) {
18137 location_t location;
18138 tree lock, stmt;
18140 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
18142 location = cp_lexer_peek_token (parser->lexer)->location;
18143 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18144 lock = cp_parser_expression (parser, false);
18145 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18147 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
18148 node, lest it get absorbed into the surrounding block. */
18149 stmt = push_stmt_list ();
18150 cp_parser_compound_statement (parser, NULL, false);
18152 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
18155 /* Parse an Objective-C throw statement.
18157 objc-throw-stmt:
18158 @throw assignment-expression [opt] ;
18160 Returns a constructed '@throw' statement. */
18162 static tree
18163 cp_parser_objc_throw_statement (cp_parser *parser) {
18164 tree expr = NULL_TREE;
18166 cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
18168 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18169 expr = cp_parser_assignment_expression (parser, false);
18171 cp_parser_consume_semicolon_at_end_of_statement (parser);
18173 return objc_build_throw_stmt (expr);
18176 /* Parse an Objective-C statement. */
18178 static tree
18179 cp_parser_objc_statement (cp_parser * parser) {
18180 /* Try to figure out what kind of declaration is present. */
18181 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18183 switch (kwd->keyword)
18185 case RID_AT_TRY:
18186 return cp_parser_objc_try_catch_finally_statement (parser);
18187 case RID_AT_SYNCHRONIZED:
18188 return cp_parser_objc_synchronized_statement (parser);
18189 case RID_AT_THROW:
18190 return cp_parser_objc_throw_statement (parser);
18191 default:
18192 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
18193 cp_parser_skip_to_end_of_block_or_statement (parser);
18196 return error_mark_node;
18199 /* OpenMP 2.5 parsing routines. */
18201 /* Returns name of the next clause.
18202 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
18203 the token is not consumed. Otherwise appropriate pragma_omp_clause is
18204 returned and the token is consumed. */
18206 static pragma_omp_clause
18207 cp_parser_omp_clause_name (cp_parser *parser)
18209 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
18211 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
18212 result = PRAGMA_OMP_CLAUSE_IF;
18213 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
18214 result = PRAGMA_OMP_CLAUSE_DEFAULT;
18215 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
18216 result = PRAGMA_OMP_CLAUSE_PRIVATE;
18217 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18219 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
18220 const char *p = IDENTIFIER_POINTER (id);
18222 switch (p[0])
18224 case 'c':
18225 if (!strcmp ("copyin", p))
18226 result = PRAGMA_OMP_CLAUSE_COPYIN;
18227 else if (!strcmp ("copyprivate", p))
18228 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
18229 break;
18230 case 'f':
18231 if (!strcmp ("firstprivate", p))
18232 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
18233 break;
18234 case 'l':
18235 if (!strcmp ("lastprivate", p))
18236 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
18237 break;
18238 case 'n':
18239 if (!strcmp ("nowait", p))
18240 result = PRAGMA_OMP_CLAUSE_NOWAIT;
18241 else if (!strcmp ("num_threads", p))
18242 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
18243 break;
18244 case 'o':
18245 if (!strcmp ("ordered", p))
18246 result = PRAGMA_OMP_CLAUSE_ORDERED;
18247 break;
18248 case 'r':
18249 if (!strcmp ("reduction", p))
18250 result = PRAGMA_OMP_CLAUSE_REDUCTION;
18251 break;
18252 case 's':
18253 if (!strcmp ("schedule", p))
18254 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
18255 else if (!strcmp ("shared", p))
18256 result = PRAGMA_OMP_CLAUSE_SHARED;
18257 break;
18261 if (result != PRAGMA_OMP_CLAUSE_NONE)
18262 cp_lexer_consume_token (parser->lexer);
18264 return result;
18267 /* Validate that a clause of the given type does not already exist. */
18269 static void
18270 check_no_duplicate_clause (tree clauses, enum tree_code code, const char *name)
18272 tree c;
18274 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
18275 if (OMP_CLAUSE_CODE (c) == code)
18277 error ("too many %qs clauses", name);
18278 break;
18282 /* OpenMP 2.5:
18283 variable-list:
18284 identifier
18285 variable-list , identifier
18287 In addition, we match a closing parenthesis. An opening parenthesis
18288 will have been consumed by the caller.
18290 If KIND is nonzero, create the appropriate node and install the decl
18291 in OMP_CLAUSE_DECL and add the node to the head of the list.
18293 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
18294 return the list created. */
18296 static tree
18297 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
18298 tree list)
18300 while (1)
18302 tree name, decl;
18304 name = cp_parser_id_expression (parser, /*template_p=*/false,
18305 /*check_dependency_p=*/true,
18306 /*template_p=*/NULL,
18307 /*declarator_p=*/false,
18308 /*optional_p=*/false);
18309 if (name == error_mark_node)
18310 goto skip_comma;
18312 decl = cp_parser_lookup_name_simple (parser, name);
18313 if (decl == error_mark_node)
18314 cp_parser_name_lookup_error (parser, name, decl, NULL);
18315 else if (kind != 0)
18317 tree u = build_omp_clause (kind);
18318 OMP_CLAUSE_DECL (u) = decl;
18319 OMP_CLAUSE_CHAIN (u) = list;
18320 list = u;
18322 else
18323 list = tree_cons (decl, NULL_TREE, list);
18325 get_comma:
18326 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18327 break;
18328 cp_lexer_consume_token (parser->lexer);
18331 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18333 int ending;
18335 /* Try to resync to an unnested comma. Copied from
18336 cp_parser_parenthesized_expression_list. */
18337 skip_comma:
18338 ending = cp_parser_skip_to_closing_parenthesis (parser,
18339 /*recovering=*/true,
18340 /*or_comma=*/true,
18341 /*consume_paren=*/true);
18342 if (ending < 0)
18343 goto get_comma;
18346 return list;
18349 /* Similarly, but expect leading and trailing parenthesis. This is a very
18350 common case for omp clauses. */
18352 static tree
18353 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
18355 if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18356 return cp_parser_omp_var_list_no_open (parser, kind, list);
18357 return list;
18360 /* OpenMP 2.5:
18361 default ( shared | none ) */
18363 static tree
18364 cp_parser_omp_clause_default (cp_parser *parser, tree list)
18366 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
18367 tree c;
18369 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18370 return list;
18371 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18373 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
18374 const char *p = IDENTIFIER_POINTER (id);
18376 switch (p[0])
18378 case 'n':
18379 if (strcmp ("none", p) != 0)
18380 goto invalid_kind;
18381 kind = OMP_CLAUSE_DEFAULT_NONE;
18382 break;
18384 case 's':
18385 if (strcmp ("shared", p) != 0)
18386 goto invalid_kind;
18387 kind = OMP_CLAUSE_DEFAULT_SHARED;
18388 break;
18390 default:
18391 goto invalid_kind;
18394 cp_lexer_consume_token (parser->lexer);
18396 else
18398 invalid_kind:
18399 cp_parser_error (parser, "expected %<none%> or %<shared%>");
18402 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18403 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18404 /*or_comma=*/false,
18405 /*consume_paren=*/true);
18407 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
18408 return list;
18410 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
18411 c = build_omp_clause (OMP_CLAUSE_DEFAULT);
18412 OMP_CLAUSE_CHAIN (c) = list;
18413 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
18415 return c;
18418 /* OpenMP 2.5:
18419 if ( expression ) */
18421 static tree
18422 cp_parser_omp_clause_if (cp_parser *parser, tree list)
18424 tree t, c;
18426 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18427 return list;
18429 t = cp_parser_condition (parser);
18431 if (t == error_mark_node
18432 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18433 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18434 /*or_comma=*/false,
18435 /*consume_paren=*/true);
18437 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
18439 c = build_omp_clause (OMP_CLAUSE_IF);
18440 OMP_CLAUSE_IF_EXPR (c) = t;
18441 OMP_CLAUSE_CHAIN (c) = list;
18443 return c;
18446 /* OpenMP 2.5:
18447 nowait */
18449 static tree
18450 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
18452 tree c;
18454 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
18456 c = build_omp_clause (OMP_CLAUSE_NOWAIT);
18457 OMP_CLAUSE_CHAIN (c) = list;
18458 return c;
18461 /* OpenMP 2.5:
18462 num_threads ( expression ) */
18464 static tree
18465 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list)
18467 tree t, c;
18469 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18470 return list;
18472 t = cp_parser_expression (parser, false);
18474 if (t == error_mark_node
18475 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18476 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18477 /*or_comma=*/false,
18478 /*consume_paren=*/true);
18480 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
18482 c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
18483 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
18484 OMP_CLAUSE_CHAIN (c) = list;
18486 return c;
18489 /* OpenMP 2.5:
18490 ordered */
18492 static tree
18493 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
18495 tree c;
18497 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
18499 c = build_omp_clause (OMP_CLAUSE_ORDERED);
18500 OMP_CLAUSE_CHAIN (c) = list;
18501 return c;
18504 /* OpenMP 2.5:
18505 reduction ( reduction-operator : variable-list )
18507 reduction-operator:
18508 One of: + * - & ^ | && || */
18510 static tree
18511 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
18513 enum tree_code code;
18514 tree nlist, c;
18516 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18517 return list;
18519 switch (cp_lexer_peek_token (parser->lexer)->type)
18521 case CPP_PLUS:
18522 code = PLUS_EXPR;
18523 break;
18524 case CPP_MULT:
18525 code = MULT_EXPR;
18526 break;
18527 case CPP_MINUS:
18528 code = MINUS_EXPR;
18529 break;
18530 case CPP_AND:
18531 code = BIT_AND_EXPR;
18532 break;
18533 case CPP_XOR:
18534 code = BIT_XOR_EXPR;
18535 break;
18536 case CPP_OR:
18537 code = BIT_IOR_EXPR;
18538 break;
18539 case CPP_AND_AND:
18540 code = TRUTH_ANDIF_EXPR;
18541 break;
18542 case CPP_OR_OR:
18543 code = TRUTH_ORIF_EXPR;
18544 break;
18545 default:
18546 cp_parser_error (parser, "`+', `*', `-', `&', `^', `|', `&&', or `||'");
18547 resync_fail:
18548 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18549 /*or_comma=*/false,
18550 /*consume_paren=*/true);
18551 return list;
18553 cp_lexer_consume_token (parser->lexer);
18555 if (!cp_parser_require (parser, CPP_COLON, "`:'"))
18556 goto resync_fail;
18558 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
18559 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
18560 OMP_CLAUSE_REDUCTION_CODE (c) = code;
18562 return nlist;
18565 /* OpenMP 2.5:
18566 schedule ( schedule-kind )
18567 schedule ( schedule-kind , expression )
18569 schedule-kind:
18570 static | dynamic | guided | runtime */
18572 static tree
18573 cp_parser_omp_clause_schedule (cp_parser *parser, tree list)
18575 tree c, t;
18577 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
18578 return list;
18580 c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
18582 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18584 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
18585 const char *p = IDENTIFIER_POINTER (id);
18587 switch (p[0])
18589 case 'd':
18590 if (strcmp ("dynamic", p) != 0)
18591 goto invalid_kind;
18592 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
18593 break;
18595 case 'g':
18596 if (strcmp ("guided", p) != 0)
18597 goto invalid_kind;
18598 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
18599 break;
18601 case 'r':
18602 if (strcmp ("runtime", p) != 0)
18603 goto invalid_kind;
18604 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
18605 break;
18607 default:
18608 goto invalid_kind;
18611 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
18612 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
18613 else
18614 goto invalid_kind;
18615 cp_lexer_consume_token (parser->lexer);
18617 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18619 cp_lexer_consume_token (parser->lexer);
18621 t = cp_parser_assignment_expression (parser, false);
18623 if (t == error_mark_node)
18624 goto resync_fail;
18625 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
18626 error ("schedule %<runtime%> does not take "
18627 "a %<chunk_size%> parameter");
18628 else
18629 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
18631 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18632 goto resync_fail;
18634 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`,' or `)'"))
18635 goto resync_fail;
18637 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
18638 OMP_CLAUSE_CHAIN (c) = list;
18639 return c;
18641 invalid_kind:
18642 cp_parser_error (parser, "invalid schedule kind");
18643 resync_fail:
18644 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18645 /*or_comma=*/false,
18646 /*consume_paren=*/true);
18647 return list;
18650 /* Parse all OpenMP clauses. The set clauses allowed by the directive
18651 is a bitmask in MASK. Return the list of clauses found; the result
18652 of clause default goes in *pdefault. */
18654 static tree
18655 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
18656 const char *where, cp_token *pragma_tok)
18658 tree clauses = NULL;
18660 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
18662 pragma_omp_clause c_kind = cp_parser_omp_clause_name (parser);
18663 const char *c_name;
18664 tree prev = clauses;
18666 switch (c_kind)
18668 case PRAGMA_OMP_CLAUSE_COPYIN:
18669 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
18670 c_name = "copyin";
18671 break;
18672 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
18673 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
18674 clauses);
18675 c_name = "copyprivate";
18676 break;
18677 case PRAGMA_OMP_CLAUSE_DEFAULT:
18678 clauses = cp_parser_omp_clause_default (parser, clauses);
18679 c_name = "default";
18680 break;
18681 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
18682 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
18683 clauses);
18684 c_name = "firstprivate";
18685 break;
18686 case PRAGMA_OMP_CLAUSE_IF:
18687 clauses = cp_parser_omp_clause_if (parser, clauses);
18688 c_name = "if";
18689 break;
18690 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
18691 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
18692 clauses);
18693 c_name = "lastprivate";
18694 break;
18695 case PRAGMA_OMP_CLAUSE_NOWAIT:
18696 clauses = cp_parser_omp_clause_nowait (parser, clauses);
18697 c_name = "nowait";
18698 break;
18699 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
18700 clauses = cp_parser_omp_clause_num_threads (parser, clauses);
18701 c_name = "num_threads";
18702 break;
18703 case PRAGMA_OMP_CLAUSE_ORDERED:
18704 clauses = cp_parser_omp_clause_ordered (parser, clauses);
18705 c_name = "ordered";
18706 break;
18707 case PRAGMA_OMP_CLAUSE_PRIVATE:
18708 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
18709 clauses);
18710 c_name = "private";
18711 break;
18712 case PRAGMA_OMP_CLAUSE_REDUCTION:
18713 clauses = cp_parser_omp_clause_reduction (parser, clauses);
18714 c_name = "reduction";
18715 break;
18716 case PRAGMA_OMP_CLAUSE_SCHEDULE:
18717 clauses = cp_parser_omp_clause_schedule (parser, clauses);
18718 c_name = "schedule";
18719 break;
18720 case PRAGMA_OMP_CLAUSE_SHARED:
18721 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
18722 clauses);
18723 c_name = "shared";
18724 break;
18725 default:
18726 cp_parser_error (parser, "expected %<#pragma omp%> clause");
18727 goto saw_error;
18730 if (((mask >> c_kind) & 1) == 0)
18732 /* Remove the invalid clause(s) from the list to avoid
18733 confusing the rest of the compiler. */
18734 clauses = prev;
18735 error ("%qs is not valid for %qs", c_name, where);
18738 saw_error:
18739 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
18740 return finish_omp_clauses (clauses);
18743 /* OpenMP 2.5:
18744 structured-block:
18745 statement
18747 In practice, we're also interested in adding the statement to an
18748 outer node. So it is convenient if we work around the fact that
18749 cp_parser_statement calls add_stmt. */
18751 static unsigned
18752 cp_parser_begin_omp_structured_block (cp_parser *parser)
18754 unsigned save = parser->in_statement;
18756 /* Only move the values to IN_OMP_BLOCK if they weren't false.
18757 This preserves the "not within loop or switch" style error messages
18758 for nonsense cases like
18759 void foo() {
18760 #pragma omp single
18761 break;
18764 if (parser->in_statement)
18765 parser->in_statement = IN_OMP_BLOCK;
18767 return save;
18770 static void
18771 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
18773 parser->in_statement = save;
18776 static tree
18777 cp_parser_omp_structured_block (cp_parser *parser)
18779 tree stmt = begin_omp_structured_block ();
18780 unsigned int save = cp_parser_begin_omp_structured_block (parser);
18782 cp_parser_statement (parser, NULL_TREE, false, NULL);
18784 cp_parser_end_omp_structured_block (parser, save);
18785 return finish_omp_structured_block (stmt);
18788 /* OpenMP 2.5:
18789 # pragma omp atomic new-line
18790 expression-stmt
18792 expression-stmt:
18793 x binop= expr | x++ | ++x | x-- | --x
18794 binop:
18795 +, *, -, /, &, ^, |, <<, >>
18797 where x is an lvalue expression with scalar type. */
18799 static void
18800 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
18802 tree lhs, rhs;
18803 enum tree_code code;
18805 cp_parser_require_pragma_eol (parser, pragma_tok);
18807 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
18808 /*cast_p=*/false);
18809 switch (TREE_CODE (lhs))
18811 case ERROR_MARK:
18812 goto saw_error;
18814 case PREINCREMENT_EXPR:
18815 case POSTINCREMENT_EXPR:
18816 lhs = TREE_OPERAND (lhs, 0);
18817 code = PLUS_EXPR;
18818 rhs = integer_one_node;
18819 break;
18821 case PREDECREMENT_EXPR:
18822 case POSTDECREMENT_EXPR:
18823 lhs = TREE_OPERAND (lhs, 0);
18824 code = MINUS_EXPR;
18825 rhs = integer_one_node;
18826 break;
18828 default:
18829 switch (cp_lexer_peek_token (parser->lexer)->type)
18831 case CPP_MULT_EQ:
18832 code = MULT_EXPR;
18833 break;
18834 case CPP_DIV_EQ:
18835 code = TRUNC_DIV_EXPR;
18836 break;
18837 case CPP_PLUS_EQ:
18838 code = PLUS_EXPR;
18839 break;
18840 case CPP_MINUS_EQ:
18841 code = MINUS_EXPR;
18842 break;
18843 case CPP_LSHIFT_EQ:
18844 code = LSHIFT_EXPR;
18845 break;
18846 case CPP_RSHIFT_EQ:
18847 code = RSHIFT_EXPR;
18848 break;
18849 case CPP_AND_EQ:
18850 code = BIT_AND_EXPR;
18851 break;
18852 case CPP_OR_EQ:
18853 code = BIT_IOR_EXPR;
18854 break;
18855 case CPP_XOR_EQ:
18856 code = BIT_XOR_EXPR;
18857 break;
18858 default:
18859 cp_parser_error (parser,
18860 "invalid operator for %<#pragma omp atomic%>");
18861 goto saw_error;
18863 cp_lexer_consume_token (parser->lexer);
18865 rhs = cp_parser_expression (parser, false);
18866 if (rhs == error_mark_node)
18867 goto saw_error;
18868 break;
18870 finish_omp_atomic (code, lhs, rhs);
18871 cp_parser_consume_semicolon_at_end_of_statement (parser);
18872 return;
18874 saw_error:
18875 cp_parser_skip_to_end_of_block_or_statement (parser);
18879 /* OpenMP 2.5:
18880 # pragma omp barrier new-line */
18882 static void
18883 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
18885 cp_parser_require_pragma_eol (parser, pragma_tok);
18886 finish_omp_barrier ();
18889 /* OpenMP 2.5:
18890 # pragma omp critical [(name)] new-line
18891 structured-block */
18893 static tree
18894 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
18896 tree stmt, name = NULL;
18898 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18900 cp_lexer_consume_token (parser->lexer);
18902 name = cp_parser_identifier (parser);
18904 if (name == error_mark_node
18905 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18906 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18907 /*or_comma=*/false,
18908 /*consume_paren=*/true);
18909 if (name == error_mark_node)
18910 name = NULL;
18912 cp_parser_require_pragma_eol (parser, pragma_tok);
18914 stmt = cp_parser_omp_structured_block (parser);
18915 return c_finish_omp_critical (stmt, name);
18918 /* OpenMP 2.5:
18919 # pragma omp flush flush-vars[opt] new-line
18921 flush-vars:
18922 ( variable-list ) */
18924 static void
18925 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
18927 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18928 (void) cp_parser_omp_var_list (parser, 0, NULL);
18929 cp_parser_require_pragma_eol (parser, pragma_tok);
18931 finish_omp_flush ();
18934 /* Parse the restricted form of the for statment allowed by OpenMP. */
18936 static tree
18937 cp_parser_omp_for_loop (cp_parser *parser)
18939 tree init, cond, incr, body, decl, pre_body;
18940 location_t loc;
18942 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
18944 cp_parser_error (parser, "for statement expected");
18945 return NULL;
18947 loc = cp_lexer_consume_token (parser->lexer)->location;
18948 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18949 return NULL;
18951 init = decl = NULL;
18952 pre_body = push_stmt_list ();
18953 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18955 cp_decl_specifier_seq type_specifiers;
18957 /* First, try to parse as an initialized declaration. See
18958 cp_parser_condition, from whence the bulk of this is copied. */
18960 cp_parser_parse_tentatively (parser);
18961 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
18962 &type_specifiers);
18963 if (!cp_parser_error_occurred (parser))
18965 tree asm_specification, attributes;
18966 cp_declarator *declarator;
18968 declarator = cp_parser_declarator (parser,
18969 CP_PARSER_DECLARATOR_NAMED,
18970 /*ctor_dtor_or_conv_p=*/NULL,
18971 /*parenthesized_p=*/NULL,
18972 /*member_p=*/false);
18973 attributes = cp_parser_attributes_opt (parser);
18974 asm_specification = cp_parser_asm_specification_opt (parser);
18976 cp_parser_require (parser, CPP_EQ, "`='");
18977 if (cp_parser_parse_definitely (parser))
18979 tree pushed_scope;
18981 decl = start_decl (declarator, &type_specifiers,
18982 /*initialized_p=*/false, attributes,
18983 /*prefix_attributes=*/NULL_TREE,
18984 &pushed_scope);
18986 init = cp_parser_assignment_expression (parser, false);
18988 cp_finish_decl (decl, NULL_TREE, /*init_const_expr_p=*/false,
18989 asm_specification, LOOKUP_ONLYCONVERTING);
18991 if (pushed_scope)
18992 pop_scope (pushed_scope);
18995 else
18996 cp_parser_abort_tentative_parse (parser);
18998 /* If parsing as an initialized declaration failed, try again as
18999 a simple expression. */
19000 if (decl == NULL)
19001 init = cp_parser_expression (parser, false);
19003 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
19004 pre_body = pop_stmt_list (pre_body);
19006 cond = NULL;
19007 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19008 cond = cp_parser_condition (parser);
19009 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
19011 incr = NULL;
19012 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
19013 incr = cp_parser_expression (parser, false);
19015 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19016 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19017 /*or_comma=*/false,
19018 /*consume_paren=*/true);
19020 /* Note that we saved the original contents of this flag when we entered
19021 the structured block, and so we don't need to re-save it here. */
19022 parser->in_statement = IN_OMP_FOR;
19024 /* Note that the grammar doesn't call for a structured block here,
19025 though the loop as a whole is a structured block. */
19026 body = push_stmt_list ();
19027 cp_parser_statement (parser, NULL_TREE, false, NULL);
19028 body = pop_stmt_list (body);
19030 return finish_omp_for (loc, decl, init, cond, incr, body, pre_body);
19033 /* OpenMP 2.5:
19034 #pragma omp for for-clause[optseq] new-line
19035 for-loop */
19037 #define OMP_FOR_CLAUSE_MASK \
19038 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19039 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19040 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
19041 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
19042 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
19043 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
19044 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19046 static tree
19047 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
19049 tree clauses, sb, ret;
19050 unsigned int save;
19052 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
19053 "#pragma omp for", pragma_tok);
19055 sb = begin_omp_structured_block ();
19056 save = cp_parser_begin_omp_structured_block (parser);
19058 ret = cp_parser_omp_for_loop (parser);
19059 if (ret)
19060 OMP_FOR_CLAUSES (ret) = clauses;
19062 cp_parser_end_omp_structured_block (parser, save);
19063 add_stmt (finish_omp_structured_block (sb));
19065 return ret;
19068 /* OpenMP 2.5:
19069 # pragma omp master new-line
19070 structured-block */
19072 static tree
19073 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
19075 cp_parser_require_pragma_eol (parser, pragma_tok);
19076 return c_finish_omp_master (cp_parser_omp_structured_block (parser));
19079 /* OpenMP 2.5:
19080 # pragma omp ordered new-line
19081 structured-block */
19083 static tree
19084 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
19086 cp_parser_require_pragma_eol (parser, pragma_tok);
19087 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
19090 /* OpenMP 2.5:
19092 section-scope:
19093 { section-sequence }
19095 section-sequence:
19096 section-directive[opt] structured-block
19097 section-sequence section-directive structured-block */
19099 static tree
19100 cp_parser_omp_sections_scope (cp_parser *parser)
19102 tree stmt, substmt;
19103 bool error_suppress = false;
19104 cp_token *tok;
19106 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
19107 return NULL_TREE;
19109 stmt = push_stmt_list ();
19111 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
19113 unsigned save;
19115 substmt = begin_omp_structured_block ();
19116 save = cp_parser_begin_omp_structured_block (parser);
19118 while (1)
19120 cp_parser_statement (parser, NULL_TREE, false, NULL);
19122 tok = cp_lexer_peek_token (parser->lexer);
19123 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
19124 break;
19125 if (tok->type == CPP_CLOSE_BRACE)
19126 break;
19127 if (tok->type == CPP_EOF)
19128 break;
19131 cp_parser_end_omp_structured_block (parser, save);
19132 substmt = finish_omp_structured_block (substmt);
19133 substmt = build1 (OMP_SECTION, void_type_node, substmt);
19134 add_stmt (substmt);
19137 while (1)
19139 tok = cp_lexer_peek_token (parser->lexer);
19140 if (tok->type == CPP_CLOSE_BRACE)
19141 break;
19142 if (tok->type == CPP_EOF)
19143 break;
19145 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
19147 cp_lexer_consume_token (parser->lexer);
19148 cp_parser_require_pragma_eol (parser, tok);
19149 error_suppress = false;
19151 else if (!error_suppress)
19153 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
19154 error_suppress = true;
19157 substmt = cp_parser_omp_structured_block (parser);
19158 substmt = build1 (OMP_SECTION, void_type_node, substmt);
19159 add_stmt (substmt);
19161 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
19163 substmt = pop_stmt_list (stmt);
19165 stmt = make_node (OMP_SECTIONS);
19166 TREE_TYPE (stmt) = void_type_node;
19167 OMP_SECTIONS_BODY (stmt) = substmt;
19169 add_stmt (stmt);
19170 return stmt;
19173 /* OpenMP 2.5:
19174 # pragma omp sections sections-clause[optseq] newline
19175 sections-scope */
19177 #define OMP_SECTIONS_CLAUSE_MASK \
19178 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19179 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19180 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
19181 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
19182 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19184 static tree
19185 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
19187 tree clauses, ret;
19189 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
19190 "#pragma omp sections", pragma_tok);
19192 ret = cp_parser_omp_sections_scope (parser);
19193 if (ret)
19194 OMP_SECTIONS_CLAUSES (ret) = clauses;
19196 return ret;
19199 /* OpenMP 2.5:
19200 # pragma parallel parallel-clause new-line
19201 # pragma parallel for parallel-for-clause new-line
19202 # pragma parallel sections parallel-sections-clause new-line */
19204 #define OMP_PARALLEL_CLAUSE_MASK \
19205 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
19206 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19207 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19208 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
19209 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
19210 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
19211 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
19212 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
19214 static tree
19215 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
19217 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
19218 const char *p_name = "#pragma omp parallel";
19219 tree stmt, clauses, par_clause, ws_clause, block;
19220 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
19221 unsigned int save;
19223 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
19225 cp_lexer_consume_token (parser->lexer);
19226 p_kind = PRAGMA_OMP_PARALLEL_FOR;
19227 p_name = "#pragma omp parallel for";
19228 mask |= OMP_FOR_CLAUSE_MASK;
19229 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
19231 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19233 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19234 const char *p = IDENTIFIER_POINTER (id);
19235 if (strcmp (p, "sections") == 0)
19237 cp_lexer_consume_token (parser->lexer);
19238 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
19239 p_name = "#pragma omp parallel sections";
19240 mask |= OMP_SECTIONS_CLAUSE_MASK;
19241 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
19245 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
19246 block = begin_omp_parallel ();
19247 save = cp_parser_begin_omp_structured_block (parser);
19249 switch (p_kind)
19251 case PRAGMA_OMP_PARALLEL:
19252 cp_parser_already_scoped_statement (parser);
19253 par_clause = clauses;
19254 break;
19256 case PRAGMA_OMP_PARALLEL_FOR:
19257 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
19258 stmt = cp_parser_omp_for_loop (parser);
19259 if (stmt)
19260 OMP_FOR_CLAUSES (stmt) = ws_clause;
19261 break;
19263 case PRAGMA_OMP_PARALLEL_SECTIONS:
19264 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
19265 stmt = cp_parser_omp_sections_scope (parser);
19266 if (stmt)
19267 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
19268 break;
19270 default:
19271 gcc_unreachable ();
19274 cp_parser_end_omp_structured_block (parser, save);
19275 stmt = finish_omp_parallel (par_clause, block);
19276 if (p_kind != PRAGMA_OMP_PARALLEL)
19277 OMP_PARALLEL_COMBINED (stmt) = 1;
19278 return stmt;
19281 /* OpenMP 2.5:
19282 # pragma omp single single-clause[optseq] new-line
19283 structured-block */
19285 #define OMP_SINGLE_CLAUSE_MASK \
19286 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19287 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19288 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
19289 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19291 static tree
19292 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
19294 tree stmt = make_node (OMP_SINGLE);
19295 TREE_TYPE (stmt) = void_type_node;
19297 OMP_SINGLE_CLAUSES (stmt)
19298 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
19299 "#pragma omp single", pragma_tok);
19300 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
19302 return add_stmt (stmt);
19305 /* OpenMP 2.5:
19306 # pragma omp threadprivate (variable-list) */
19308 static void
19309 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
19311 tree vars;
19313 vars = cp_parser_omp_var_list (parser, 0, NULL);
19314 cp_parser_require_pragma_eol (parser, pragma_tok);
19316 finish_omp_threadprivate (vars);
19319 /* Main entry point to OpenMP statement pragmas. */
19321 static void
19322 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
19324 tree stmt;
19326 switch (pragma_tok->pragma_kind)
19328 case PRAGMA_OMP_ATOMIC:
19329 cp_parser_omp_atomic (parser, pragma_tok);
19330 return;
19331 case PRAGMA_OMP_CRITICAL:
19332 stmt = cp_parser_omp_critical (parser, pragma_tok);
19333 break;
19334 case PRAGMA_OMP_FOR:
19335 stmt = cp_parser_omp_for (parser, pragma_tok);
19336 break;
19337 case PRAGMA_OMP_MASTER:
19338 stmt = cp_parser_omp_master (parser, pragma_tok);
19339 break;
19340 case PRAGMA_OMP_ORDERED:
19341 stmt = cp_parser_omp_ordered (parser, pragma_tok);
19342 break;
19343 case PRAGMA_OMP_PARALLEL:
19344 stmt = cp_parser_omp_parallel (parser, pragma_tok);
19345 break;
19346 case PRAGMA_OMP_SECTIONS:
19347 stmt = cp_parser_omp_sections (parser, pragma_tok);
19348 break;
19349 case PRAGMA_OMP_SINGLE:
19350 stmt = cp_parser_omp_single (parser, pragma_tok);
19351 break;
19352 default:
19353 gcc_unreachable ();
19356 if (stmt)
19357 SET_EXPR_LOCATION (stmt, pragma_tok->location);
19360 /* The parser. */
19362 static GTY (()) cp_parser *the_parser;
19365 /* Special handling for the first token or line in the file. The first
19366 thing in the file might be #pragma GCC pch_preprocess, which loads a
19367 PCH file, which is a GC collection point. So we need to handle this
19368 first pragma without benefit of an existing lexer structure.
19370 Always returns one token to the caller in *FIRST_TOKEN. This is
19371 either the true first token of the file, or the first token after
19372 the initial pragma. */
19374 static void
19375 cp_parser_initial_pragma (cp_token *first_token)
19377 tree name = NULL;
19379 cp_lexer_get_preprocessor_token (NULL, first_token);
19380 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
19381 return;
19383 cp_lexer_get_preprocessor_token (NULL, first_token);
19384 if (first_token->type == CPP_STRING)
19386 name = first_token->u.value;
19388 cp_lexer_get_preprocessor_token (NULL, first_token);
19389 if (first_token->type != CPP_PRAGMA_EOL)
19390 error ("junk at end of %<#pragma GCC pch_preprocess%>");
19392 else
19393 error ("expected string literal");
19395 /* Skip to the end of the pragma. */
19396 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
19397 cp_lexer_get_preprocessor_token (NULL, first_token);
19399 /* Now actually load the PCH file. */
19400 if (name)
19401 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
19403 /* Read one more token to return to our caller. We have to do this
19404 after reading the PCH file in, since its pointers have to be
19405 live. */
19406 cp_lexer_get_preprocessor_token (NULL, first_token);
19409 /* Normal parsing of a pragma token. Here we can (and must) use the
19410 regular lexer. */
19412 static bool
19413 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
19415 cp_token *pragma_tok;
19416 unsigned int id;
19418 pragma_tok = cp_lexer_consume_token (parser->lexer);
19419 gcc_assert (pragma_tok->type == CPP_PRAGMA);
19420 parser->lexer->in_pragma = true;
19422 id = pragma_tok->pragma_kind;
19423 switch (id)
19425 case PRAGMA_GCC_PCH_PREPROCESS:
19426 error ("%<#pragma GCC pch_preprocess%> must be first");
19427 break;
19429 case PRAGMA_OMP_BARRIER:
19430 switch (context)
19432 case pragma_compound:
19433 cp_parser_omp_barrier (parser, pragma_tok);
19434 return false;
19435 case pragma_stmt:
19436 error ("%<#pragma omp barrier%> may only be "
19437 "used in compound statements");
19438 break;
19439 default:
19440 goto bad_stmt;
19442 break;
19444 case PRAGMA_OMP_FLUSH:
19445 switch (context)
19447 case pragma_compound:
19448 cp_parser_omp_flush (parser, pragma_tok);
19449 return false;
19450 case pragma_stmt:
19451 error ("%<#pragma omp flush%> may only be "
19452 "used in compound statements");
19453 break;
19454 default:
19455 goto bad_stmt;
19457 break;
19459 case PRAGMA_OMP_THREADPRIVATE:
19460 cp_parser_omp_threadprivate (parser, pragma_tok);
19461 return false;
19463 case PRAGMA_OMP_ATOMIC:
19464 case PRAGMA_OMP_CRITICAL:
19465 case PRAGMA_OMP_FOR:
19466 case PRAGMA_OMP_MASTER:
19467 case PRAGMA_OMP_ORDERED:
19468 case PRAGMA_OMP_PARALLEL:
19469 case PRAGMA_OMP_SECTIONS:
19470 case PRAGMA_OMP_SINGLE:
19471 if (context == pragma_external)
19472 goto bad_stmt;
19473 cp_parser_omp_construct (parser, pragma_tok);
19474 return true;
19476 case PRAGMA_OMP_SECTION:
19477 error ("%<#pragma omp section%> may only be used in "
19478 "%<#pragma omp sections%> construct");
19479 break;
19481 default:
19482 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
19483 c_invoke_pragma_handler (id);
19484 break;
19486 bad_stmt:
19487 cp_parser_error (parser, "expected declaration specifiers");
19488 break;
19491 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
19492 return false;
19495 /* The interface the pragma parsers have to the lexer. */
19497 enum cpp_ttype
19498 pragma_lex (tree *value)
19500 cp_token *tok;
19501 enum cpp_ttype ret;
19503 tok = cp_lexer_peek_token (the_parser->lexer);
19505 ret = tok->type;
19506 *value = tok->u.value;
19508 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
19509 ret = CPP_EOF;
19510 else if (ret == CPP_STRING)
19511 *value = cp_parser_string_literal (the_parser, false, false);
19512 else
19514 cp_lexer_consume_token (the_parser->lexer);
19515 if (ret == CPP_KEYWORD)
19516 ret = CPP_NAME;
19519 return ret;
19523 /* External interface. */
19525 /* Parse one entire translation unit. */
19527 void
19528 c_parse_file (void)
19530 bool error_occurred;
19531 static bool already_called = false;
19533 if (already_called)
19535 sorry ("inter-module optimizations not implemented for C++");
19536 return;
19538 already_called = true;
19540 the_parser = cp_parser_new ();
19541 push_deferring_access_checks (flag_access_control
19542 ? dk_no_deferred : dk_no_check);
19543 error_occurred = cp_parser_translation_unit (the_parser);
19544 the_parser = NULL;
19547 #include "gt-cp-parser.h"