Factor uses of build_pairwise_scheduling.
[official-gcc/Ramakrishna.git] / gcc / cp / parser.c
blob5c8dbcb155e70f6f95e5277e0dd2255888a82cf2
1 /* C++ Parser.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005, 2007, 2008, 2009 Free Software Foundation, Inc.
4 Written by Mark Mitchell <mark@codesourcery.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "dyn-string.h"
27 #include "varray.h"
28 #include "cpplib.h"
29 #include "tree.h"
30 #include "cp-tree.h"
31 #include "c-pragma.h"
32 #include "decl.h"
33 #include "flags.h"
34 #include "diagnostic.h"
35 #include "toplev.h"
36 #include "output.h"
37 #include "target.h"
38 #include "cgraph.h"
39 #include "c-common.h"
40 #include "plugin.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 GTY(()) tree_check {
52 /* The value associated with the token. */
53 tree value;
54 /* The checks that have been associated with value. */
55 VEC (deferred_access_check, gc)* checks;
56 /* The token's qualifying scope (used when it is a
57 CPP_NESTED_NAME_SPECIFIER). */
58 tree qualifying_scope;
61 /* A C++ token. */
63 typedef struct GTY (()) cp_token {
64 /* The kind of token. */
65 ENUM_BITFIELD (cpp_ttype) type : 8;
66 /* If this token is a keyword, this value indicates which keyword.
67 Otherwise, this value is RID_MAX. */
68 ENUM_BITFIELD (rid) keyword : 8;
69 /* Token flags. */
70 unsigned char flags;
71 /* Identifier for the pragma. */
72 ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
73 /* True if this token is from a context where it is implicitly extern "C" */
74 BOOL_BITFIELD implicit_extern_c : 1;
75 /* True for a CPP_NAME token that is not a keyword (i.e., for which
76 KEYWORD is RID_MAX) iff this name was looked up and found to be
77 ambiguous. An error has already been reported. */
78 BOOL_BITFIELD ambiguous_p : 1;
79 /* The location at which this token was found. */
80 location_t location;
81 /* The value associated with this token, if any. */
82 union cp_token_value {
83 /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID. */
84 struct tree_check* GTY((tag ("1"))) tree_check_value;
85 /* Use for all other tokens. */
86 tree GTY((tag ("0"))) value;
87 } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
88 } cp_token;
90 /* We use a stack of token pointer for saving token sets. */
91 typedef struct cp_token *cp_token_position;
92 DEF_VEC_P (cp_token_position);
93 DEF_VEC_ALLOC_P (cp_token_position,heap);
95 static cp_token eof_token =
97 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, 0, 0, { NULL }
100 /* The cp_lexer structure represents the C++ lexer. It is responsible
101 for managing the token stream from the preprocessor and supplying
102 it to the parser. Tokens are never added to the cp_lexer after
103 it is created. */
105 typedef struct GTY (()) cp_lexer {
106 /* The memory allocated for the buffer. NULL if this lexer does not
107 own the token buffer. */
108 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
109 /* If the lexer owns the buffer, this is the number of tokens in the
110 buffer. */
111 size_t buffer_length;
113 /* A pointer just past the last available token. The tokens
114 in this lexer are [buffer, last_token). */
115 cp_token_position GTY ((skip)) last_token;
117 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
118 no more available tokens. */
119 cp_token_position GTY ((skip)) next_token;
121 /* A stack indicating positions at which cp_lexer_save_tokens was
122 called. The top entry is the most recent position at which we
123 began saving tokens. If the stack is non-empty, we are saving
124 tokens. */
125 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
127 /* The next lexer in a linked list of lexers. */
128 struct cp_lexer *next;
130 /* True if we should output debugging information. */
131 bool debugging_p;
133 /* True if we're in the context of parsing a pragma, and should not
134 increment past the end-of-line marker. */
135 bool in_pragma;
136 } cp_lexer;
138 /* cp_token_cache is a range of tokens. There is no need to represent
139 allocate heap memory for it, since tokens are never removed from the
140 lexer's array. There is also no need for the GC to walk through
141 a cp_token_cache, since everything in here is referenced through
142 a lexer. */
144 typedef struct GTY(()) cp_token_cache {
145 /* The beginning of the token range. */
146 cp_token * GTY((skip)) first;
148 /* Points immediately after the last token in the range. */
149 cp_token * GTY ((skip)) last;
150 } cp_token_cache;
152 /* Prototypes. */
154 static cp_lexer *cp_lexer_new_main
155 (void);
156 static cp_lexer *cp_lexer_new_from_tokens
157 (cp_token_cache *tokens);
158 static void cp_lexer_destroy
159 (cp_lexer *);
160 static int cp_lexer_saving_tokens
161 (const cp_lexer *);
162 static cp_token_position cp_lexer_token_position
163 (cp_lexer *, bool);
164 static cp_token *cp_lexer_token_at
165 (cp_lexer *, cp_token_position);
166 static void cp_lexer_get_preprocessor_token
167 (cp_lexer *, cp_token *);
168 static inline cp_token *cp_lexer_peek_token
169 (cp_lexer *);
170 static cp_token *cp_lexer_peek_nth_token
171 (cp_lexer *, size_t);
172 static inline bool cp_lexer_next_token_is
173 (cp_lexer *, enum cpp_ttype);
174 static bool cp_lexer_next_token_is_not
175 (cp_lexer *, enum cpp_ttype);
176 static bool cp_lexer_next_token_is_keyword
177 (cp_lexer *, enum rid);
178 static cp_token *cp_lexer_consume_token
179 (cp_lexer *);
180 static void cp_lexer_purge_token
181 (cp_lexer *);
182 static void cp_lexer_purge_tokens_after
183 (cp_lexer *, cp_token_position);
184 static void cp_lexer_save_tokens
185 (cp_lexer *);
186 static void cp_lexer_commit_tokens
187 (cp_lexer *);
188 static void cp_lexer_rollback_tokens
189 (cp_lexer *);
190 #ifdef ENABLE_CHECKING
191 static void cp_lexer_print_token
192 (FILE *, cp_token *);
193 static inline bool cp_lexer_debugging_p
194 (cp_lexer *);
195 static void cp_lexer_start_debugging
196 (cp_lexer *) ATTRIBUTE_UNUSED;
197 static void cp_lexer_stop_debugging
198 (cp_lexer *) ATTRIBUTE_UNUSED;
199 #else
200 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
201 about passing NULL to functions that require non-NULL arguments
202 (fputs, fprintf). It will never be used, so all we need is a value
203 of the right type that's guaranteed not to be NULL. */
204 #define cp_lexer_debug_stream stdout
205 #define cp_lexer_print_token(str, tok) (void) 0
206 #define cp_lexer_debugging_p(lexer) 0
207 #endif /* ENABLE_CHECKING */
209 static cp_token_cache *cp_token_cache_new
210 (cp_token *, cp_token *);
212 static void cp_parser_initial_pragma
213 (cp_token *);
215 /* Manifest constants. */
216 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
217 #define CP_SAVED_TOKEN_STACK 5
219 /* A token type for keywords, as opposed to ordinary identifiers. */
220 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
222 /* A token type for template-ids. If a template-id is processed while
223 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
224 the value of the CPP_TEMPLATE_ID is whatever was returned by
225 cp_parser_template_id. */
226 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
228 /* A token type for nested-name-specifiers. If a
229 nested-name-specifier is processed while parsing tentatively, it is
230 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
231 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
232 cp_parser_nested_name_specifier_opt. */
233 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
235 /* A token type for tokens that are not tokens at all; these are used
236 to represent slots in the array where there used to be a token
237 that has now been deleted. */
238 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
240 /* The number of token types, including C++-specific ones. */
241 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
243 /* Variables. */
245 #ifdef ENABLE_CHECKING
246 /* The stream to which debugging output should be written. */
247 static FILE *cp_lexer_debug_stream;
248 #endif /* ENABLE_CHECKING */
250 /* Nonzero if we are parsing an unevaluated operand: an operand to
251 sizeof, typeof, or alignof. */
252 int cp_unevaluated_operand;
254 /* Create a new main C++ lexer, the lexer that gets tokens from the
255 preprocessor. */
257 static cp_lexer *
258 cp_lexer_new_main (void)
260 cp_token first_token;
261 cp_lexer *lexer;
262 cp_token *pos;
263 size_t alloc;
264 size_t space;
265 cp_token *buffer;
267 /* It's possible that parsing the first pragma will load a PCH file,
268 which is a GC collection point. So we have to do that before
269 allocating any memory. */
270 cp_parser_initial_pragma (&first_token);
272 c_common_no_more_pch ();
274 /* Allocate the memory. */
275 lexer = GGC_CNEW (cp_lexer);
277 #ifdef ENABLE_CHECKING
278 /* Initially we are not debugging. */
279 lexer->debugging_p = false;
280 #endif /* ENABLE_CHECKING */
281 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
282 CP_SAVED_TOKEN_STACK);
284 /* Create the buffer. */
285 alloc = CP_LEXER_BUFFER_SIZE;
286 buffer = GGC_NEWVEC (cp_token, alloc);
288 /* Put the first token in the buffer. */
289 space = alloc;
290 pos = buffer;
291 *pos = first_token;
293 /* Get the remaining tokens from the preprocessor. */
294 while (pos->type != CPP_EOF)
296 pos++;
297 if (!--space)
299 space = alloc;
300 alloc *= 2;
301 buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
302 pos = buffer + space;
304 cp_lexer_get_preprocessor_token (lexer, pos);
306 lexer->buffer = buffer;
307 lexer->buffer_length = alloc - space;
308 lexer->last_token = pos;
309 lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
311 /* Subsequent preprocessor diagnostics should use compiler
312 diagnostic functions to get the compiler source location. */
313 done_lexing = true;
315 gcc_assert (lexer->next_token->type != CPP_PURGED);
316 return lexer;
319 /* Create a new lexer whose token stream is primed with the tokens in
320 CACHE. When these tokens are exhausted, no new tokens will be read. */
322 static cp_lexer *
323 cp_lexer_new_from_tokens (cp_token_cache *cache)
325 cp_token *first = cache->first;
326 cp_token *last = cache->last;
327 cp_lexer *lexer = GGC_CNEW (cp_lexer);
329 /* We do not own the buffer. */
330 lexer->buffer = NULL;
331 lexer->buffer_length = 0;
332 lexer->next_token = first == last ? &eof_token : first;
333 lexer->last_token = last;
335 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
336 CP_SAVED_TOKEN_STACK);
338 #ifdef ENABLE_CHECKING
339 /* Initially we are not debugging. */
340 lexer->debugging_p = false;
341 #endif
343 gcc_assert (lexer->next_token->type != CPP_PURGED);
344 return lexer;
347 /* Frees all resources associated with LEXER. */
349 static void
350 cp_lexer_destroy (cp_lexer *lexer)
352 if (lexer->buffer)
353 ggc_free (lexer->buffer);
354 VEC_free (cp_token_position, heap, lexer->saved_tokens);
355 ggc_free (lexer);
358 /* Returns nonzero if debugging information should be output. */
360 #ifdef ENABLE_CHECKING
362 static inline bool
363 cp_lexer_debugging_p (cp_lexer *lexer)
365 return lexer->debugging_p;
368 #endif /* ENABLE_CHECKING */
370 static inline cp_token_position
371 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
373 gcc_assert (!previous_p || lexer->next_token != &eof_token);
375 return lexer->next_token - previous_p;
378 static inline cp_token *
379 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
381 return pos;
384 /* nonzero if we are presently saving tokens. */
386 static inline int
387 cp_lexer_saving_tokens (const cp_lexer* lexer)
389 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
392 /* Store the next token from the preprocessor in *TOKEN. Return true
393 if we reach EOF. If LEXER is NULL, assume we are handling an
394 initial #pragma pch_preprocess, and thus want the lexer to return
395 processed strings. */
397 static void
398 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
400 static int is_extern_c = 0;
402 /* Get a new token from the preprocessor. */
403 token->type
404 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
405 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
406 token->keyword = RID_MAX;
407 token->pragma_kind = PRAGMA_NONE;
409 /* On some systems, some header files are surrounded by an
410 implicit extern "C" block. Set a flag in the token if it
411 comes from such a header. */
412 is_extern_c += pending_lang_change;
413 pending_lang_change = 0;
414 token->implicit_extern_c = is_extern_c > 0;
416 /* Check to see if this token is a keyword. */
417 if (token->type == CPP_NAME)
419 if (C_IS_RESERVED_WORD (token->u.value))
421 /* Mark this token as a keyword. */
422 token->type = CPP_KEYWORD;
423 /* Record which keyword. */
424 token->keyword = C_RID_CODE (token->u.value);
426 else
428 if (warn_cxx0x_compat
429 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
430 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
432 /* Warn about the C++0x keyword (but still treat it as
433 an identifier). */
434 warning (OPT_Wc__0x_compat,
435 "identifier %qE will become a keyword in C++0x",
436 token->u.value);
438 /* Clear out the C_RID_CODE so we don't warn about this
439 particular identifier-turned-keyword again. */
440 C_SET_RID_CODE (token->u.value, RID_MAX);
443 token->ambiguous_p = false;
444 token->keyword = RID_MAX;
447 /* Handle Objective-C++ keywords. */
448 else if (token->type == CPP_AT_NAME)
450 token->type = CPP_KEYWORD;
451 switch (C_RID_CODE (token->u.value))
453 /* Map 'class' to '@class', 'private' to '@private', etc. */
454 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
455 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
456 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
457 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
458 case RID_THROW: token->keyword = RID_AT_THROW; break;
459 case RID_TRY: token->keyword = RID_AT_TRY; break;
460 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
461 default: token->keyword = C_RID_CODE (token->u.value);
464 else if (token->type == CPP_PRAGMA)
466 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
467 token->pragma_kind = ((enum pragma_kind)
468 TREE_INT_CST_LOW (token->u.value));
469 token->u.value = NULL_TREE;
473 /* Update the globals input_location and the input file stack from TOKEN. */
474 static inline void
475 cp_lexer_set_source_position_from_token (cp_token *token)
477 if (token->type != CPP_EOF)
479 input_location = token->location;
483 /* Return a pointer to the next token in the token stream, but do not
484 consume it. */
486 static inline cp_token *
487 cp_lexer_peek_token (cp_lexer *lexer)
489 if (cp_lexer_debugging_p (lexer))
491 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
492 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
493 putc ('\n', cp_lexer_debug_stream);
495 return lexer->next_token;
498 /* Return true if the next token has the indicated TYPE. */
500 static inline bool
501 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
503 return cp_lexer_peek_token (lexer)->type == type;
506 /* Return true if the next token does not have the indicated TYPE. */
508 static inline bool
509 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
511 return !cp_lexer_next_token_is (lexer, type);
514 /* Return true if the next token is the indicated KEYWORD. */
516 static inline bool
517 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
519 return cp_lexer_peek_token (lexer)->keyword == keyword;
522 /* Return true if the next token is not the indicated KEYWORD. */
524 static inline bool
525 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
527 return cp_lexer_peek_token (lexer)->keyword != keyword;
530 /* Return true if the next token is a keyword for a decl-specifier. */
532 static bool
533 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
535 cp_token *token;
537 token = cp_lexer_peek_token (lexer);
538 switch (token->keyword)
540 /* auto specifier: storage-class-specifier in C++,
541 simple-type-specifier in C++0x. */
542 case RID_AUTO:
543 /* Storage classes. */
544 case RID_REGISTER:
545 case RID_STATIC:
546 case RID_EXTERN:
547 case RID_MUTABLE:
548 case RID_THREAD:
549 /* Elaborated type specifiers. */
550 case RID_ENUM:
551 case RID_CLASS:
552 case RID_STRUCT:
553 case RID_UNION:
554 case RID_TYPENAME:
555 /* Simple type specifiers. */
556 case RID_CHAR:
557 case RID_CHAR16:
558 case RID_CHAR32:
559 case RID_WCHAR:
560 case RID_BOOL:
561 case RID_SHORT:
562 case RID_INT:
563 case RID_LONG:
564 case RID_SIGNED:
565 case RID_UNSIGNED:
566 case RID_FLOAT:
567 case RID_DOUBLE:
568 case RID_VOID:
569 /* GNU extensions. */
570 case RID_ATTRIBUTE:
571 case RID_TYPEOF:
572 /* C++0x extensions. */
573 case RID_DECLTYPE:
574 return true;
576 default:
577 return false;
581 /* Return a pointer to the Nth token in the token stream. If N is 1,
582 then this is precisely equivalent to cp_lexer_peek_token (except
583 that it is not inline). One would like to disallow that case, but
584 there is one case (cp_parser_nth_token_starts_template_id) where
585 the caller passes a variable for N and it might be 1. */
587 static cp_token *
588 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
590 cp_token *token;
592 /* N is 1-based, not zero-based. */
593 gcc_assert (n > 0);
595 if (cp_lexer_debugging_p (lexer))
596 fprintf (cp_lexer_debug_stream,
597 "cp_lexer: peeking ahead %ld at token: ", (long)n);
599 --n;
600 token = lexer->next_token;
601 gcc_assert (!n || token != &eof_token);
602 while (n != 0)
604 ++token;
605 if (token == lexer->last_token)
607 token = &eof_token;
608 break;
611 if (token->type != CPP_PURGED)
612 --n;
615 if (cp_lexer_debugging_p (lexer))
617 cp_lexer_print_token (cp_lexer_debug_stream, token);
618 putc ('\n', cp_lexer_debug_stream);
621 return token;
624 /* Return the next token, and advance the lexer's next_token pointer
625 to point to the next non-purged token. */
627 static cp_token *
628 cp_lexer_consume_token (cp_lexer* lexer)
630 cp_token *token = lexer->next_token;
632 gcc_assert (token != &eof_token);
633 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
637 lexer->next_token++;
638 if (lexer->next_token == lexer->last_token)
640 lexer->next_token = &eof_token;
641 break;
645 while (lexer->next_token->type == CPP_PURGED);
647 cp_lexer_set_source_position_from_token (token);
649 /* Provide debugging output. */
650 if (cp_lexer_debugging_p (lexer))
652 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
653 cp_lexer_print_token (cp_lexer_debug_stream, token);
654 putc ('\n', cp_lexer_debug_stream);
657 return token;
660 /* Permanently remove the next token from the token stream, and
661 advance the next_token pointer to refer to the next non-purged
662 token. */
664 static void
665 cp_lexer_purge_token (cp_lexer *lexer)
667 cp_token *tok = lexer->next_token;
669 gcc_assert (tok != &eof_token);
670 tok->type = CPP_PURGED;
671 tok->location = UNKNOWN_LOCATION;
672 tok->u.value = NULL_TREE;
673 tok->keyword = RID_MAX;
677 tok++;
678 if (tok == lexer->last_token)
680 tok = &eof_token;
681 break;
684 while (tok->type == CPP_PURGED);
685 lexer->next_token = tok;
688 /* Permanently remove all tokens after TOK, up to, but not
689 including, the token that will be returned next by
690 cp_lexer_peek_token. */
692 static void
693 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
695 cp_token *peek = lexer->next_token;
697 if (peek == &eof_token)
698 peek = lexer->last_token;
700 gcc_assert (tok < peek);
702 for ( tok += 1; tok != peek; tok += 1)
704 tok->type = CPP_PURGED;
705 tok->location = UNKNOWN_LOCATION;
706 tok->u.value = NULL_TREE;
707 tok->keyword = RID_MAX;
711 /* Begin saving tokens. All tokens consumed after this point will be
712 preserved. */
714 static void
715 cp_lexer_save_tokens (cp_lexer* lexer)
717 /* Provide debugging output. */
718 if (cp_lexer_debugging_p (lexer))
719 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
721 VEC_safe_push (cp_token_position, heap,
722 lexer->saved_tokens, lexer->next_token);
725 /* Commit to the portion of the token stream most recently saved. */
727 static void
728 cp_lexer_commit_tokens (cp_lexer* lexer)
730 /* Provide debugging output. */
731 if (cp_lexer_debugging_p (lexer))
732 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
734 VEC_pop (cp_token_position, lexer->saved_tokens);
737 /* Return all tokens saved since the last call to cp_lexer_save_tokens
738 to the token stream. Stop saving tokens. */
740 static void
741 cp_lexer_rollback_tokens (cp_lexer* lexer)
743 /* Provide debugging output. */
744 if (cp_lexer_debugging_p (lexer))
745 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
747 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
750 /* Print a representation of the TOKEN on the STREAM. */
752 #ifdef ENABLE_CHECKING
754 static void
755 cp_lexer_print_token (FILE * stream, cp_token *token)
757 /* We don't use cpp_type2name here because the parser defines
758 a few tokens of its own. */
759 static const char *const token_names[] = {
760 /* cpplib-defined token types */
761 #define OP(e, s) #e,
762 #define TK(e, s) #e,
763 TTYPE_TABLE
764 #undef OP
765 #undef TK
766 /* C++ parser token types - see "Manifest constants", above. */
767 "KEYWORD",
768 "TEMPLATE_ID",
769 "NESTED_NAME_SPECIFIER",
770 "PURGED"
773 /* If we have a name for the token, print it out. Otherwise, we
774 simply give the numeric code. */
775 gcc_assert (token->type < ARRAY_SIZE(token_names));
776 fputs (token_names[token->type], stream);
778 /* For some tokens, print the associated data. */
779 switch (token->type)
781 case CPP_KEYWORD:
782 /* Some keywords have a value that is not an IDENTIFIER_NODE.
783 For example, `struct' is mapped to an INTEGER_CST. */
784 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
785 break;
786 /* else fall through */
787 case CPP_NAME:
788 fputs (IDENTIFIER_POINTER (token->u.value), stream);
789 break;
791 case CPP_STRING:
792 case CPP_STRING16:
793 case CPP_STRING32:
794 case CPP_WSTRING:
795 case CPP_UTF8STRING:
796 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
797 break;
799 default:
800 break;
804 /* Start emitting debugging information. */
806 static void
807 cp_lexer_start_debugging (cp_lexer* lexer)
809 lexer->debugging_p = true;
812 /* Stop emitting debugging information. */
814 static void
815 cp_lexer_stop_debugging (cp_lexer* lexer)
817 lexer->debugging_p = false;
820 #endif /* ENABLE_CHECKING */
822 /* Create a new cp_token_cache, representing a range of tokens. */
824 static cp_token_cache *
825 cp_token_cache_new (cp_token *first, cp_token *last)
827 cp_token_cache *cache = GGC_NEW (cp_token_cache);
828 cache->first = first;
829 cache->last = last;
830 return cache;
834 /* Decl-specifiers. */
836 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
838 static void
839 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
841 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
844 /* Declarators. */
846 /* Nothing other than the parser should be creating declarators;
847 declarators are a semi-syntactic representation of C++ entities.
848 Other parts of the front end that need to create entities (like
849 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
851 static cp_declarator *make_call_declarator
852 (cp_declarator *, tree, cp_cv_quals, tree, tree);
853 static cp_declarator *make_array_declarator
854 (cp_declarator *, tree);
855 static cp_declarator *make_pointer_declarator
856 (cp_cv_quals, cp_declarator *);
857 static cp_declarator *make_reference_declarator
858 (cp_cv_quals, cp_declarator *, bool);
859 static cp_parameter_declarator *make_parameter_declarator
860 (cp_decl_specifier_seq *, cp_declarator *, tree);
861 static cp_declarator *make_ptrmem_declarator
862 (cp_cv_quals, tree, cp_declarator *);
864 /* An erroneous declarator. */
865 static cp_declarator *cp_error_declarator;
867 /* The obstack on which declarators and related data structures are
868 allocated. */
869 static struct obstack declarator_obstack;
871 /* Alloc BYTES from the declarator memory pool. */
873 static inline void *
874 alloc_declarator (size_t bytes)
876 return obstack_alloc (&declarator_obstack, bytes);
879 /* Allocate a declarator of the indicated KIND. Clear fields that are
880 common to all declarators. */
882 static cp_declarator *
883 make_declarator (cp_declarator_kind kind)
885 cp_declarator *declarator;
887 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
888 declarator->kind = kind;
889 declarator->attributes = NULL_TREE;
890 declarator->declarator = NULL;
891 declarator->parameter_pack_p = false;
893 return declarator;
896 /* Make a declarator for a generalized identifier. If
897 QUALIFYING_SCOPE is non-NULL, the identifier is
898 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
899 UNQUALIFIED_NAME. SFK indicates the kind of special function this
900 is, if any. */
902 static cp_declarator *
903 make_id_declarator (tree qualifying_scope, tree unqualified_name,
904 special_function_kind sfk)
906 cp_declarator *declarator;
908 /* It is valid to write:
910 class C { void f(); };
911 typedef C D;
912 void D::f();
914 The standard is not clear about whether `typedef const C D' is
915 legal; as of 2002-09-15 the committee is considering that
916 question. EDG 3.0 allows that syntax. Therefore, we do as
917 well. */
918 if (qualifying_scope && TYPE_P (qualifying_scope))
919 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
921 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
922 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
923 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
925 declarator = make_declarator (cdk_id);
926 declarator->u.id.qualifying_scope = qualifying_scope;
927 declarator->u.id.unqualified_name = unqualified_name;
928 declarator->u.id.sfk = sfk;
930 return declarator;
933 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
934 of modifiers such as const or volatile to apply to the pointer
935 type, represented as identifiers. */
937 cp_declarator *
938 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
940 cp_declarator *declarator;
942 declarator = make_declarator (cdk_pointer);
943 declarator->declarator = target;
944 declarator->u.pointer.qualifiers = cv_qualifiers;
945 declarator->u.pointer.class_type = NULL_TREE;
946 if (target)
948 declarator->parameter_pack_p = target->parameter_pack_p;
949 target->parameter_pack_p = false;
951 else
952 declarator->parameter_pack_p = false;
954 return declarator;
957 /* Like make_pointer_declarator -- but for references. */
959 cp_declarator *
960 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
961 bool rvalue_ref)
963 cp_declarator *declarator;
965 declarator = make_declarator (cdk_reference);
966 declarator->declarator = target;
967 declarator->u.reference.qualifiers = cv_qualifiers;
968 declarator->u.reference.rvalue_ref = rvalue_ref;
969 if (target)
971 declarator->parameter_pack_p = target->parameter_pack_p;
972 target->parameter_pack_p = false;
974 else
975 declarator->parameter_pack_p = false;
977 return declarator;
980 /* Like make_pointer_declarator -- but for a pointer to a non-static
981 member of CLASS_TYPE. */
983 cp_declarator *
984 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
985 cp_declarator *pointee)
987 cp_declarator *declarator;
989 declarator = make_declarator (cdk_ptrmem);
990 declarator->declarator = pointee;
991 declarator->u.pointer.qualifiers = cv_qualifiers;
992 declarator->u.pointer.class_type = class_type;
994 if (pointee)
996 declarator->parameter_pack_p = pointee->parameter_pack_p;
997 pointee->parameter_pack_p = false;
999 else
1000 declarator->parameter_pack_p = false;
1002 return declarator;
1005 /* Make a declarator for the function given by TARGET, with the
1006 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1007 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1008 indicates what exceptions can be thrown. */
1010 cp_declarator *
1011 make_call_declarator (cp_declarator *target,
1012 tree parms,
1013 cp_cv_quals cv_qualifiers,
1014 tree exception_specification,
1015 tree late_return_type)
1017 cp_declarator *declarator;
1019 declarator = make_declarator (cdk_function);
1020 declarator->declarator = target;
1021 declarator->u.function.parameters = parms;
1022 declarator->u.function.qualifiers = cv_qualifiers;
1023 declarator->u.function.exception_specification = exception_specification;
1024 declarator->u.function.late_return_type = late_return_type;
1025 if (target)
1027 declarator->parameter_pack_p = target->parameter_pack_p;
1028 target->parameter_pack_p = false;
1030 else
1031 declarator->parameter_pack_p = false;
1033 return declarator;
1036 /* Make a declarator for an array of BOUNDS elements, each of which is
1037 defined by ELEMENT. */
1039 cp_declarator *
1040 make_array_declarator (cp_declarator *element, tree bounds)
1042 cp_declarator *declarator;
1044 declarator = make_declarator (cdk_array);
1045 declarator->declarator = element;
1046 declarator->u.array.bounds = bounds;
1047 if (element)
1049 declarator->parameter_pack_p = element->parameter_pack_p;
1050 element->parameter_pack_p = false;
1052 else
1053 declarator->parameter_pack_p = false;
1055 return declarator;
1058 /* Determine whether the declarator we've seen so far can be a
1059 parameter pack, when followed by an ellipsis. */
1060 static bool
1061 declarator_can_be_parameter_pack (cp_declarator *declarator)
1063 /* Search for a declarator name, or any other declarator that goes
1064 after the point where the ellipsis could appear in a parameter
1065 pack. If we find any of these, then this declarator can not be
1066 made into a parameter pack. */
1067 bool found = false;
1068 while (declarator && !found)
1070 switch ((int)declarator->kind)
1072 case cdk_id:
1073 case cdk_array:
1074 found = true;
1075 break;
1077 case cdk_error:
1078 return true;
1080 default:
1081 declarator = declarator->declarator;
1082 break;
1086 return !found;
1089 cp_parameter_declarator *no_parameters;
1091 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1092 DECLARATOR and DEFAULT_ARGUMENT. */
1094 cp_parameter_declarator *
1095 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1096 cp_declarator *declarator,
1097 tree default_argument)
1099 cp_parameter_declarator *parameter;
1101 parameter = ((cp_parameter_declarator *)
1102 alloc_declarator (sizeof (cp_parameter_declarator)));
1103 parameter->next = NULL;
1104 if (decl_specifiers)
1105 parameter->decl_specifiers = *decl_specifiers;
1106 else
1107 clear_decl_specs (&parameter->decl_specifiers);
1108 parameter->declarator = declarator;
1109 parameter->default_argument = default_argument;
1110 parameter->ellipsis_p = false;
1112 return parameter;
1115 /* Returns true iff DECLARATOR is a declaration for a function. */
1117 static bool
1118 function_declarator_p (const cp_declarator *declarator)
1120 while (declarator)
1122 if (declarator->kind == cdk_function
1123 && declarator->declarator->kind == cdk_id)
1124 return true;
1125 if (declarator->kind == cdk_id
1126 || declarator->kind == cdk_error)
1127 return false;
1128 declarator = declarator->declarator;
1130 return false;
1133 /* The parser. */
1135 /* Overview
1136 --------
1138 A cp_parser parses the token stream as specified by the C++
1139 grammar. Its job is purely parsing, not semantic analysis. For
1140 example, the parser breaks the token stream into declarators,
1141 expressions, statements, and other similar syntactic constructs.
1142 It does not check that the types of the expressions on either side
1143 of an assignment-statement are compatible, or that a function is
1144 not declared with a parameter of type `void'.
1146 The parser invokes routines elsewhere in the compiler to perform
1147 semantic analysis and to build up the abstract syntax tree for the
1148 code processed.
1150 The parser (and the template instantiation code, which is, in a
1151 way, a close relative of parsing) are the only parts of the
1152 compiler that should be calling push_scope and pop_scope, or
1153 related functions. The parser (and template instantiation code)
1154 keeps track of what scope is presently active; everything else
1155 should simply honor that. (The code that generates static
1156 initializers may also need to set the scope, in order to check
1157 access control correctly when emitting the initializers.)
1159 Methodology
1160 -----------
1162 The parser is of the standard recursive-descent variety. Upcoming
1163 tokens in the token stream are examined in order to determine which
1164 production to use when parsing a non-terminal. Some C++ constructs
1165 require arbitrary look ahead to disambiguate. For example, it is
1166 impossible, in the general case, to tell whether a statement is an
1167 expression or declaration without scanning the entire statement.
1168 Therefore, the parser is capable of "parsing tentatively." When the
1169 parser is not sure what construct comes next, it enters this mode.
1170 Then, while we attempt to parse the construct, the parser queues up
1171 error messages, rather than issuing them immediately, and saves the
1172 tokens it consumes. If the construct is parsed successfully, the
1173 parser "commits", i.e., it issues any queued error messages and
1174 the tokens that were being preserved are permanently discarded.
1175 If, however, the construct is not parsed successfully, the parser
1176 rolls back its state completely so that it can resume parsing using
1177 a different alternative.
1179 Future Improvements
1180 -------------------
1182 The performance of the parser could probably be improved substantially.
1183 We could often eliminate the need to parse tentatively by looking ahead
1184 a little bit. In some places, this approach might not entirely eliminate
1185 the need to parse tentatively, but it might still speed up the average
1186 case. */
1188 /* Flags that are passed to some parsing functions. These values can
1189 be bitwise-ored together. */
1191 enum
1193 /* No flags. */
1194 CP_PARSER_FLAGS_NONE = 0x0,
1195 /* The construct is optional. If it is not present, then no error
1196 should be issued. */
1197 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1198 /* When parsing a type-specifier, treat user-defined type-names
1199 as non-type identifiers. */
1200 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1201 /* When parsing a type-specifier, do not try to parse a class-specifier
1202 or enum-specifier. */
1203 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4
1206 /* This type is used for parameters and variables which hold
1207 combinations of the above flags. */
1208 typedef int cp_parser_flags;
1210 /* The different kinds of declarators we want to parse. */
1212 typedef enum cp_parser_declarator_kind
1214 /* We want an abstract declarator. */
1215 CP_PARSER_DECLARATOR_ABSTRACT,
1216 /* We want a named declarator. */
1217 CP_PARSER_DECLARATOR_NAMED,
1218 /* We don't mind, but the name must be an unqualified-id. */
1219 CP_PARSER_DECLARATOR_EITHER
1220 } cp_parser_declarator_kind;
1222 /* The precedence values used to parse binary expressions. The minimum value
1223 of PREC must be 1, because zero is reserved to quickly discriminate
1224 binary operators from other tokens. */
1226 enum cp_parser_prec
1228 PREC_NOT_OPERATOR,
1229 PREC_LOGICAL_OR_EXPRESSION,
1230 PREC_LOGICAL_AND_EXPRESSION,
1231 PREC_INCLUSIVE_OR_EXPRESSION,
1232 PREC_EXCLUSIVE_OR_EXPRESSION,
1233 PREC_AND_EXPRESSION,
1234 PREC_EQUALITY_EXPRESSION,
1235 PREC_RELATIONAL_EXPRESSION,
1236 PREC_SHIFT_EXPRESSION,
1237 PREC_ADDITIVE_EXPRESSION,
1238 PREC_MULTIPLICATIVE_EXPRESSION,
1239 PREC_PM_EXPRESSION,
1240 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1243 /* A mapping from a token type to a corresponding tree node type, with a
1244 precedence value. */
1246 typedef struct cp_parser_binary_operations_map_node
1248 /* The token type. */
1249 enum cpp_ttype token_type;
1250 /* The corresponding tree code. */
1251 enum tree_code tree_type;
1252 /* The precedence of this operator. */
1253 enum cp_parser_prec prec;
1254 } cp_parser_binary_operations_map_node;
1256 /* The status of a tentative parse. */
1258 typedef enum cp_parser_status_kind
1260 /* No errors have occurred. */
1261 CP_PARSER_STATUS_KIND_NO_ERROR,
1262 /* An error has occurred. */
1263 CP_PARSER_STATUS_KIND_ERROR,
1264 /* We are committed to this tentative parse, whether or not an error
1265 has occurred. */
1266 CP_PARSER_STATUS_KIND_COMMITTED
1267 } cp_parser_status_kind;
1269 typedef struct cp_parser_expression_stack_entry
1271 /* Left hand side of the binary operation we are currently
1272 parsing. */
1273 tree lhs;
1274 /* Original tree code for left hand side, if it was a binary
1275 expression itself (used for -Wparentheses). */
1276 enum tree_code lhs_type;
1277 /* Tree code for the binary operation we are parsing. */
1278 enum tree_code tree_type;
1279 /* Precedence of the binary operation we are parsing. */
1280 enum cp_parser_prec prec;
1281 } cp_parser_expression_stack_entry;
1283 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1284 entries because precedence levels on the stack are monotonically
1285 increasing. */
1286 typedef struct cp_parser_expression_stack_entry
1287 cp_parser_expression_stack[NUM_PREC_VALUES];
1289 /* Context that is saved and restored when parsing tentatively. */
1290 typedef struct GTY (()) cp_parser_context {
1291 /* If this is a tentative parsing context, the status of the
1292 tentative parse. */
1293 enum cp_parser_status_kind status;
1294 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1295 that are looked up in this context must be looked up both in the
1296 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1297 the context of the containing expression. */
1298 tree object_type;
1300 /* The next parsing context in the stack. */
1301 struct cp_parser_context *next;
1302 } cp_parser_context;
1304 /* Prototypes. */
1306 /* Constructors and destructors. */
1308 static cp_parser_context *cp_parser_context_new
1309 (cp_parser_context *);
1311 /* Class variables. */
1313 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1315 /* The operator-precedence table used by cp_parser_binary_expression.
1316 Transformed into an associative array (binops_by_token) by
1317 cp_parser_new. */
1319 static const cp_parser_binary_operations_map_node binops[] = {
1320 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1321 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1323 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1324 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1325 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1327 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1328 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1330 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1331 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1333 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1334 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1335 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1336 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1338 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1339 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1341 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1343 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1345 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1347 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1349 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1352 /* The same as binops, but initialized by cp_parser_new so that
1353 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1354 for speed. */
1355 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1357 /* Constructors and destructors. */
1359 /* Construct a new context. The context below this one on the stack
1360 is given by NEXT. */
1362 static cp_parser_context *
1363 cp_parser_context_new (cp_parser_context* next)
1365 cp_parser_context *context;
1367 /* Allocate the storage. */
1368 if (cp_parser_context_free_list != NULL)
1370 /* Pull the first entry from the free list. */
1371 context = cp_parser_context_free_list;
1372 cp_parser_context_free_list = context->next;
1373 memset (context, 0, sizeof (*context));
1375 else
1376 context = GGC_CNEW (cp_parser_context);
1378 /* No errors have occurred yet in this context. */
1379 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1380 /* If this is not the bottommost context, copy information that we
1381 need from the previous context. */
1382 if (next)
1384 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1385 expression, then we are parsing one in this context, too. */
1386 context->object_type = next->object_type;
1387 /* Thread the stack. */
1388 context->next = next;
1391 return context;
1394 /* The cp_parser structure represents the C++ parser. */
1396 typedef struct GTY(()) cp_parser {
1397 /* The lexer from which we are obtaining tokens. */
1398 cp_lexer *lexer;
1400 /* The scope in which names should be looked up. If NULL_TREE, then
1401 we look up names in the scope that is currently open in the
1402 source program. If non-NULL, this is either a TYPE or
1403 NAMESPACE_DECL for the scope in which we should look. It can
1404 also be ERROR_MARK, when we've parsed a bogus scope.
1406 This value is not cleared automatically after a name is looked
1407 up, so we must be careful to clear it before starting a new look
1408 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1409 will look up `Z' in the scope of `X', rather than the current
1410 scope.) Unfortunately, it is difficult to tell when name lookup
1411 is complete, because we sometimes peek at a token, look it up,
1412 and then decide not to consume it. */
1413 tree scope;
1415 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1416 last lookup took place. OBJECT_SCOPE is used if an expression
1417 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1418 respectively. QUALIFYING_SCOPE is used for an expression of the
1419 form "X::Y"; it refers to X. */
1420 tree object_scope;
1421 tree qualifying_scope;
1423 /* A stack of parsing contexts. All but the bottom entry on the
1424 stack will be tentative contexts.
1426 We parse tentatively in order to determine which construct is in
1427 use in some situations. For example, in order to determine
1428 whether a statement is an expression-statement or a
1429 declaration-statement we parse it tentatively as a
1430 declaration-statement. If that fails, we then reparse the same
1431 token stream as an expression-statement. */
1432 cp_parser_context *context;
1434 /* True if we are parsing GNU C++. If this flag is not set, then
1435 GNU extensions are not recognized. */
1436 bool allow_gnu_extensions_p;
1438 /* TRUE if the `>' token should be interpreted as the greater-than
1439 operator. FALSE if it is the end of a template-id or
1440 template-parameter-list. In C++0x mode, this flag also applies to
1441 `>>' tokens, which are viewed as two consecutive `>' tokens when
1442 this flag is FALSE. */
1443 bool greater_than_is_operator_p;
1445 /* TRUE if default arguments are allowed within a parameter list
1446 that starts at this point. FALSE if only a gnu extension makes
1447 them permissible. */
1448 bool default_arg_ok_p;
1450 /* TRUE if we are parsing an integral constant-expression. See
1451 [expr.const] for a precise definition. */
1452 bool integral_constant_expression_p;
1454 /* TRUE if we are parsing an integral constant-expression -- but a
1455 non-constant expression should be permitted as well. This flag
1456 is used when parsing an array bound so that GNU variable-length
1457 arrays are tolerated. */
1458 bool allow_non_integral_constant_expression_p;
1460 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1461 been seen that makes the expression non-constant. */
1462 bool non_integral_constant_expression_p;
1464 /* TRUE if local variable names and `this' are forbidden in the
1465 current context. */
1466 bool local_variables_forbidden_p;
1468 /* TRUE if the declaration we are parsing is part of a
1469 linkage-specification of the form `extern string-literal
1470 declaration'. */
1471 bool in_unbraced_linkage_specification_p;
1473 /* TRUE if we are presently parsing a declarator, after the
1474 direct-declarator. */
1475 bool in_declarator_p;
1477 /* TRUE if we are presently parsing a template-argument-list. */
1478 bool in_template_argument_list_p;
1480 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1481 to IN_OMP_BLOCK if parsing OpenMP structured block and
1482 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1483 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1484 iteration-statement, OpenMP block or loop within that switch. */
1485 #define IN_SWITCH_STMT 1
1486 #define IN_ITERATION_STMT 2
1487 #define IN_OMP_BLOCK 4
1488 #define IN_OMP_FOR 8
1489 #define IN_IF_STMT 16
1490 unsigned char in_statement;
1492 /* TRUE if we are presently parsing the body of a switch statement.
1493 Note that this doesn't quite overlap with in_statement above.
1494 The difference relates to giving the right sets of error messages:
1495 "case not in switch" vs "break statement used with OpenMP...". */
1496 bool in_switch_statement_p;
1498 /* TRUE if we are parsing a type-id in an expression context. In
1499 such a situation, both "type (expr)" and "type (type)" are valid
1500 alternatives. */
1501 bool in_type_id_in_expr_p;
1503 /* TRUE if we are currently in a header file where declarations are
1504 implicitly extern "C". */
1505 bool implicit_extern_c;
1507 /* TRUE if strings in expressions should be translated to the execution
1508 character set. */
1509 bool translate_strings_p;
1511 /* TRUE if we are presently parsing the body of a function, but not
1512 a local class. */
1513 bool in_function_body;
1515 /* If non-NULL, then we are parsing a construct where new type
1516 definitions are not permitted. The string stored here will be
1517 issued as an error message if a type is defined. */
1518 const char *type_definition_forbidden_message;
1520 /* A list of lists. The outer list is a stack, used for member
1521 functions of local classes. At each level there are two sub-list,
1522 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1523 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1524 TREE_VALUE's. The functions are chained in reverse declaration
1525 order.
1527 The TREE_PURPOSE sublist contains those functions with default
1528 arguments that need post processing, and the TREE_VALUE sublist
1529 contains those functions with definitions that need post
1530 processing.
1532 These lists can only be processed once the outermost class being
1533 defined is complete. */
1534 tree unparsed_functions_queues;
1536 /* The number of classes whose definitions are currently in
1537 progress. */
1538 unsigned num_classes_being_defined;
1540 /* The number of template parameter lists that apply directly to the
1541 current declaration. */
1542 unsigned num_template_parameter_lists;
1543 } cp_parser;
1545 /* Prototypes. */
1547 /* Constructors and destructors. */
1549 static cp_parser *cp_parser_new
1550 (void);
1552 /* Routines to parse various constructs.
1554 Those that return `tree' will return the error_mark_node (rather
1555 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1556 Sometimes, they will return an ordinary node if error-recovery was
1557 attempted, even though a parse error occurred. So, to check
1558 whether or not a parse error occurred, you should always use
1559 cp_parser_error_occurred. If the construct is optional (indicated
1560 either by an `_opt' in the name of the function that does the
1561 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1562 the construct is not present. */
1564 /* Lexical conventions [gram.lex] */
1566 static tree cp_parser_identifier
1567 (cp_parser *);
1568 static tree cp_parser_string_literal
1569 (cp_parser *, bool, bool);
1571 /* Basic concepts [gram.basic] */
1573 static bool cp_parser_translation_unit
1574 (cp_parser *);
1576 /* Expressions [gram.expr] */
1578 static tree cp_parser_primary_expression
1579 (cp_parser *, bool, bool, bool, cp_id_kind *);
1580 static tree cp_parser_id_expression
1581 (cp_parser *, bool, bool, bool *, bool, bool);
1582 static tree cp_parser_unqualified_id
1583 (cp_parser *, bool, bool, bool, bool);
1584 static tree cp_parser_nested_name_specifier_opt
1585 (cp_parser *, bool, bool, bool, bool);
1586 static tree cp_parser_nested_name_specifier
1587 (cp_parser *, bool, bool, bool, bool);
1588 static tree cp_parser_qualifying_entity
1589 (cp_parser *, bool, bool, bool, bool, bool);
1590 static tree cp_parser_postfix_expression
1591 (cp_parser *, bool, bool, bool, cp_id_kind *);
1592 static tree cp_parser_postfix_open_square_expression
1593 (cp_parser *, tree, bool);
1594 static tree cp_parser_postfix_dot_deref_expression
1595 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1596 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1597 (cp_parser *, bool, bool, bool, bool *);
1598 static void cp_parser_pseudo_destructor_name
1599 (cp_parser *, tree *, tree *);
1600 static tree cp_parser_unary_expression
1601 (cp_parser *, bool, bool, cp_id_kind *);
1602 static enum tree_code cp_parser_unary_operator
1603 (cp_token *);
1604 static tree cp_parser_new_expression
1605 (cp_parser *);
1606 static VEC(tree,gc) *cp_parser_new_placement
1607 (cp_parser *);
1608 static tree cp_parser_new_type_id
1609 (cp_parser *, tree *);
1610 static cp_declarator *cp_parser_new_declarator_opt
1611 (cp_parser *);
1612 static cp_declarator *cp_parser_direct_new_declarator
1613 (cp_parser *);
1614 static VEC(tree,gc) *cp_parser_new_initializer
1615 (cp_parser *);
1616 static tree cp_parser_delete_expression
1617 (cp_parser *);
1618 static tree cp_parser_cast_expression
1619 (cp_parser *, bool, bool, cp_id_kind *);
1620 static tree cp_parser_binary_expression
1621 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1622 static tree cp_parser_question_colon_clause
1623 (cp_parser *, tree);
1624 static tree cp_parser_assignment_expression
1625 (cp_parser *, bool, cp_id_kind *);
1626 static enum tree_code cp_parser_assignment_operator_opt
1627 (cp_parser *);
1628 static tree cp_parser_expression
1629 (cp_parser *, bool, cp_id_kind *);
1630 static tree cp_parser_constant_expression
1631 (cp_parser *, bool, bool *);
1632 static tree cp_parser_builtin_offsetof
1633 (cp_parser *);
1634 static tree cp_parser_lambda_expression
1635 (cp_parser *);
1636 static void cp_parser_lambda_introducer
1637 (cp_parser *, tree);
1638 static void cp_parser_lambda_declarator_opt
1639 (cp_parser *, tree);
1640 static void cp_parser_lambda_body
1641 (cp_parser *, tree);
1643 /* Statements [gram.stmt.stmt] */
1645 static void cp_parser_statement
1646 (cp_parser *, tree, bool, bool *);
1647 static void cp_parser_label_for_labeled_statement
1648 (cp_parser *);
1649 static tree cp_parser_expression_statement
1650 (cp_parser *, tree);
1651 static tree cp_parser_compound_statement
1652 (cp_parser *, tree, bool);
1653 static void cp_parser_statement_seq_opt
1654 (cp_parser *, tree);
1655 static tree cp_parser_selection_statement
1656 (cp_parser *, bool *);
1657 static tree cp_parser_condition
1658 (cp_parser *);
1659 static tree cp_parser_iteration_statement
1660 (cp_parser *);
1661 static void cp_parser_for_init_statement
1662 (cp_parser *);
1663 static tree cp_parser_jump_statement
1664 (cp_parser *);
1665 static void cp_parser_declaration_statement
1666 (cp_parser *);
1668 static tree cp_parser_implicitly_scoped_statement
1669 (cp_parser *, bool *);
1670 static void cp_parser_already_scoped_statement
1671 (cp_parser *);
1673 /* Declarations [gram.dcl.dcl] */
1675 static void cp_parser_declaration_seq_opt
1676 (cp_parser *);
1677 static void cp_parser_declaration
1678 (cp_parser *);
1679 static void cp_parser_block_declaration
1680 (cp_parser *, bool);
1681 static void cp_parser_simple_declaration
1682 (cp_parser *, bool);
1683 static void cp_parser_decl_specifier_seq
1684 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1685 static tree cp_parser_storage_class_specifier_opt
1686 (cp_parser *);
1687 static tree cp_parser_function_specifier_opt
1688 (cp_parser *, cp_decl_specifier_seq *);
1689 static tree cp_parser_type_specifier
1690 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1691 int *, bool *);
1692 static tree cp_parser_simple_type_specifier
1693 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1694 static tree cp_parser_type_name
1695 (cp_parser *);
1696 static tree cp_parser_nonclass_name
1697 (cp_parser* parser);
1698 static tree cp_parser_elaborated_type_specifier
1699 (cp_parser *, bool, bool);
1700 static tree cp_parser_enum_specifier
1701 (cp_parser *);
1702 static void cp_parser_enumerator_list
1703 (cp_parser *, tree);
1704 static void cp_parser_enumerator_definition
1705 (cp_parser *, tree);
1706 static tree cp_parser_namespace_name
1707 (cp_parser *);
1708 static void cp_parser_namespace_definition
1709 (cp_parser *);
1710 static void cp_parser_namespace_body
1711 (cp_parser *);
1712 static tree cp_parser_qualified_namespace_specifier
1713 (cp_parser *);
1714 static void cp_parser_namespace_alias_definition
1715 (cp_parser *);
1716 static bool cp_parser_using_declaration
1717 (cp_parser *, bool);
1718 static void cp_parser_using_directive
1719 (cp_parser *);
1720 static void cp_parser_asm_definition
1721 (cp_parser *);
1722 static void cp_parser_linkage_specification
1723 (cp_parser *);
1724 static void cp_parser_static_assert
1725 (cp_parser *, bool);
1726 static tree cp_parser_decltype
1727 (cp_parser *);
1729 /* Declarators [gram.dcl.decl] */
1731 static tree cp_parser_init_declarator
1732 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1733 static cp_declarator *cp_parser_declarator
1734 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1735 static cp_declarator *cp_parser_direct_declarator
1736 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1737 static enum tree_code cp_parser_ptr_operator
1738 (cp_parser *, tree *, cp_cv_quals *);
1739 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1740 (cp_parser *);
1741 static tree cp_parser_late_return_type_opt
1742 (cp_parser *);
1743 static tree cp_parser_declarator_id
1744 (cp_parser *, bool);
1745 static tree cp_parser_type_id
1746 (cp_parser *);
1747 static tree cp_parser_template_type_arg
1748 (cp_parser *);
1749 static tree cp_parser_trailing_type_id (cp_parser *);
1750 static tree cp_parser_type_id_1
1751 (cp_parser *, bool, bool);
1752 static void cp_parser_type_specifier_seq
1753 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1754 static tree cp_parser_parameter_declaration_clause
1755 (cp_parser *);
1756 static tree cp_parser_parameter_declaration_list
1757 (cp_parser *, bool *);
1758 static cp_parameter_declarator *cp_parser_parameter_declaration
1759 (cp_parser *, bool, bool *);
1760 static tree cp_parser_default_argument
1761 (cp_parser *, bool);
1762 static void cp_parser_function_body
1763 (cp_parser *);
1764 static tree cp_parser_initializer
1765 (cp_parser *, bool *, bool *);
1766 static tree cp_parser_initializer_clause
1767 (cp_parser *, bool *);
1768 static tree cp_parser_braced_list
1769 (cp_parser*, bool*);
1770 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1771 (cp_parser *, bool *);
1773 static bool cp_parser_ctor_initializer_opt_and_function_body
1774 (cp_parser *);
1776 /* Classes [gram.class] */
1778 static tree cp_parser_class_name
1779 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1780 static tree cp_parser_class_specifier
1781 (cp_parser *);
1782 static tree cp_parser_class_head
1783 (cp_parser *, bool *, tree *, tree *);
1784 static enum tag_types cp_parser_class_key
1785 (cp_parser *);
1786 static void cp_parser_member_specification_opt
1787 (cp_parser *);
1788 static void cp_parser_member_declaration
1789 (cp_parser *);
1790 static tree cp_parser_pure_specifier
1791 (cp_parser *);
1792 static tree cp_parser_constant_initializer
1793 (cp_parser *);
1795 /* Derived classes [gram.class.derived] */
1797 static tree cp_parser_base_clause
1798 (cp_parser *);
1799 static tree cp_parser_base_specifier
1800 (cp_parser *);
1802 /* Special member functions [gram.special] */
1804 static tree cp_parser_conversion_function_id
1805 (cp_parser *);
1806 static tree cp_parser_conversion_type_id
1807 (cp_parser *);
1808 static cp_declarator *cp_parser_conversion_declarator_opt
1809 (cp_parser *);
1810 static bool cp_parser_ctor_initializer_opt
1811 (cp_parser *);
1812 static void cp_parser_mem_initializer_list
1813 (cp_parser *);
1814 static tree cp_parser_mem_initializer
1815 (cp_parser *);
1816 static tree cp_parser_mem_initializer_id
1817 (cp_parser *);
1819 /* Overloading [gram.over] */
1821 static tree cp_parser_operator_function_id
1822 (cp_parser *);
1823 static tree cp_parser_operator
1824 (cp_parser *);
1826 /* Templates [gram.temp] */
1828 static void cp_parser_template_declaration
1829 (cp_parser *, bool);
1830 static tree cp_parser_template_parameter_list
1831 (cp_parser *);
1832 static tree cp_parser_template_parameter
1833 (cp_parser *, bool *, bool *);
1834 static tree cp_parser_type_parameter
1835 (cp_parser *, bool *);
1836 static tree cp_parser_template_id
1837 (cp_parser *, bool, bool, bool);
1838 static tree cp_parser_template_name
1839 (cp_parser *, bool, bool, bool, bool *);
1840 static tree cp_parser_template_argument_list
1841 (cp_parser *);
1842 static tree cp_parser_template_argument
1843 (cp_parser *);
1844 static void cp_parser_explicit_instantiation
1845 (cp_parser *);
1846 static void cp_parser_explicit_specialization
1847 (cp_parser *);
1849 /* Exception handling [gram.exception] */
1851 static tree cp_parser_try_block
1852 (cp_parser *);
1853 static bool cp_parser_function_try_block
1854 (cp_parser *);
1855 static void cp_parser_handler_seq
1856 (cp_parser *);
1857 static void cp_parser_handler
1858 (cp_parser *);
1859 static tree cp_parser_exception_declaration
1860 (cp_parser *);
1861 static tree cp_parser_throw_expression
1862 (cp_parser *);
1863 static tree cp_parser_exception_specification_opt
1864 (cp_parser *);
1865 static tree cp_parser_type_id_list
1866 (cp_parser *);
1868 /* GNU Extensions */
1870 static tree cp_parser_asm_specification_opt
1871 (cp_parser *);
1872 static tree cp_parser_asm_operand_list
1873 (cp_parser *);
1874 static tree cp_parser_asm_clobber_list
1875 (cp_parser *);
1876 static tree cp_parser_asm_label_list
1877 (cp_parser *);
1878 static tree cp_parser_attributes_opt
1879 (cp_parser *);
1880 static tree cp_parser_attribute_list
1881 (cp_parser *);
1882 static bool cp_parser_extension_opt
1883 (cp_parser *, int *);
1884 static void cp_parser_label_declaration
1885 (cp_parser *);
1887 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1888 static bool cp_parser_pragma
1889 (cp_parser *, enum pragma_context);
1891 /* Objective-C++ Productions */
1893 static tree cp_parser_objc_message_receiver
1894 (cp_parser *);
1895 static tree cp_parser_objc_message_args
1896 (cp_parser *);
1897 static tree cp_parser_objc_message_expression
1898 (cp_parser *);
1899 static tree cp_parser_objc_encode_expression
1900 (cp_parser *);
1901 static tree cp_parser_objc_defs_expression
1902 (cp_parser *);
1903 static tree cp_parser_objc_protocol_expression
1904 (cp_parser *);
1905 static tree cp_parser_objc_selector_expression
1906 (cp_parser *);
1907 static tree cp_parser_objc_expression
1908 (cp_parser *);
1909 static bool cp_parser_objc_selector_p
1910 (enum cpp_ttype);
1911 static tree cp_parser_objc_selector
1912 (cp_parser *);
1913 static tree cp_parser_objc_protocol_refs_opt
1914 (cp_parser *);
1915 static void cp_parser_objc_declaration
1916 (cp_parser *);
1917 static tree cp_parser_objc_statement
1918 (cp_parser *);
1920 /* Utility Routines */
1922 static tree cp_parser_lookup_name
1923 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1924 static tree cp_parser_lookup_name_simple
1925 (cp_parser *, tree, location_t);
1926 static tree cp_parser_maybe_treat_template_as_class
1927 (tree, bool);
1928 static bool cp_parser_check_declarator_template_parameters
1929 (cp_parser *, cp_declarator *, location_t);
1930 static bool cp_parser_check_template_parameters
1931 (cp_parser *, unsigned, location_t, cp_declarator *);
1932 static tree cp_parser_simple_cast_expression
1933 (cp_parser *);
1934 static tree cp_parser_global_scope_opt
1935 (cp_parser *, bool);
1936 static bool cp_parser_constructor_declarator_p
1937 (cp_parser *, bool);
1938 static tree cp_parser_function_definition_from_specifiers_and_declarator
1939 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1940 static tree cp_parser_function_definition_after_declarator
1941 (cp_parser *, bool);
1942 static void cp_parser_template_declaration_after_export
1943 (cp_parser *, bool);
1944 static void cp_parser_perform_template_parameter_access_checks
1945 (VEC (deferred_access_check,gc)*);
1946 static tree cp_parser_single_declaration
1947 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1948 static tree cp_parser_functional_cast
1949 (cp_parser *, tree);
1950 static tree cp_parser_save_member_function_body
1951 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1952 static tree cp_parser_enclosed_template_argument_list
1953 (cp_parser *);
1954 static void cp_parser_save_default_args
1955 (cp_parser *, tree);
1956 static void cp_parser_late_parsing_for_member
1957 (cp_parser *, tree);
1958 static void cp_parser_late_parsing_default_args
1959 (cp_parser *, tree);
1960 static tree cp_parser_sizeof_operand
1961 (cp_parser *, enum rid);
1962 static tree cp_parser_trait_expr
1963 (cp_parser *, enum rid);
1964 static bool cp_parser_declares_only_class_p
1965 (cp_parser *);
1966 static void cp_parser_set_storage_class
1967 (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1968 static void cp_parser_set_decl_spec_type
1969 (cp_decl_specifier_seq *, tree, location_t, bool);
1970 static bool cp_parser_friend_p
1971 (const cp_decl_specifier_seq *);
1972 static cp_token *cp_parser_require
1973 (cp_parser *, enum cpp_ttype, const char *);
1974 static cp_token *cp_parser_require_keyword
1975 (cp_parser *, enum rid, const char *);
1976 static bool cp_parser_token_starts_function_definition_p
1977 (cp_token *);
1978 static bool cp_parser_next_token_starts_class_definition_p
1979 (cp_parser *);
1980 static bool cp_parser_next_token_ends_template_argument_p
1981 (cp_parser *);
1982 static bool cp_parser_nth_token_starts_template_argument_list_p
1983 (cp_parser *, size_t);
1984 static enum tag_types cp_parser_token_is_class_key
1985 (cp_token *);
1986 static void cp_parser_check_class_key
1987 (enum tag_types, tree type);
1988 static void cp_parser_check_access_in_redeclaration
1989 (tree type, location_t location);
1990 static bool cp_parser_optional_template_keyword
1991 (cp_parser *);
1992 static void cp_parser_pre_parsed_nested_name_specifier
1993 (cp_parser *);
1994 static bool cp_parser_cache_group
1995 (cp_parser *, enum cpp_ttype, unsigned);
1996 static void cp_parser_parse_tentatively
1997 (cp_parser *);
1998 static void cp_parser_commit_to_tentative_parse
1999 (cp_parser *);
2000 static void cp_parser_abort_tentative_parse
2001 (cp_parser *);
2002 static bool cp_parser_parse_definitely
2003 (cp_parser *);
2004 static inline bool cp_parser_parsing_tentatively
2005 (cp_parser *);
2006 static bool cp_parser_uncommitted_to_tentative_parse_p
2007 (cp_parser *);
2008 static void cp_parser_error
2009 (cp_parser *, const char *);
2010 static void cp_parser_name_lookup_error
2011 (cp_parser *, tree, tree, const char *, location_t);
2012 static bool cp_parser_simulate_error
2013 (cp_parser *);
2014 static bool cp_parser_check_type_definition
2015 (cp_parser *);
2016 static void cp_parser_check_for_definition_in_return_type
2017 (cp_declarator *, tree, location_t type_location);
2018 static void cp_parser_check_for_invalid_template_id
2019 (cp_parser *, tree, location_t location);
2020 static bool cp_parser_non_integral_constant_expression
2021 (cp_parser *, const char *);
2022 static void cp_parser_diagnose_invalid_type_name
2023 (cp_parser *, tree, tree, location_t);
2024 static bool cp_parser_parse_and_diagnose_invalid_type_name
2025 (cp_parser *);
2026 static int cp_parser_skip_to_closing_parenthesis
2027 (cp_parser *, bool, bool, bool);
2028 static void cp_parser_skip_to_end_of_statement
2029 (cp_parser *);
2030 static void cp_parser_consume_semicolon_at_end_of_statement
2031 (cp_parser *);
2032 static void cp_parser_skip_to_end_of_block_or_statement
2033 (cp_parser *);
2034 static bool cp_parser_skip_to_closing_brace
2035 (cp_parser *);
2036 static void cp_parser_skip_to_end_of_template_parameter_list
2037 (cp_parser *);
2038 static void cp_parser_skip_to_pragma_eol
2039 (cp_parser*, cp_token *);
2040 static bool cp_parser_error_occurred
2041 (cp_parser *);
2042 static bool cp_parser_allow_gnu_extensions_p
2043 (cp_parser *);
2044 static bool cp_parser_is_string_literal
2045 (cp_token *);
2046 static bool cp_parser_is_keyword
2047 (cp_token *, enum rid);
2048 static tree cp_parser_make_typename_type
2049 (cp_parser *, tree, tree, location_t location);
2050 static cp_declarator * cp_parser_make_indirect_declarator
2051 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2053 /* Returns nonzero if we are parsing tentatively. */
2055 static inline bool
2056 cp_parser_parsing_tentatively (cp_parser* parser)
2058 return parser->context->next != NULL;
2061 /* Returns nonzero if TOKEN is a string literal. */
2063 static bool
2064 cp_parser_is_string_literal (cp_token* token)
2066 return (token->type == CPP_STRING ||
2067 token->type == CPP_STRING16 ||
2068 token->type == CPP_STRING32 ||
2069 token->type == CPP_WSTRING ||
2070 token->type == CPP_UTF8STRING);
2073 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2075 static bool
2076 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2078 return token->keyword == keyword;
2081 /* If not parsing tentatively, issue a diagnostic of the form
2082 FILE:LINE: MESSAGE before TOKEN
2083 where TOKEN is the next token in the input stream. MESSAGE
2084 (specified by the caller) is usually of the form "expected
2085 OTHER-TOKEN". */
2087 static void
2088 cp_parser_error (cp_parser* parser, const char* message)
2090 if (!cp_parser_simulate_error (parser))
2092 cp_token *token = cp_lexer_peek_token (parser->lexer);
2093 /* This diagnostic makes more sense if it is tagged to the line
2094 of the token we just peeked at. */
2095 cp_lexer_set_source_position_from_token (token);
2097 if (token->type == CPP_PRAGMA)
2099 error_at (token->location,
2100 "%<#pragma%> is not allowed here");
2101 cp_parser_skip_to_pragma_eol (parser, token);
2102 return;
2105 c_parse_error (message,
2106 /* Because c_parser_error does not understand
2107 CPP_KEYWORD, keywords are treated like
2108 identifiers. */
2109 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2110 token->u.value, token->flags);
2114 /* Issue an error about name-lookup failing. NAME is the
2115 IDENTIFIER_NODE DECL is the result of
2116 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2117 the thing that we hoped to find. */
2119 static void
2120 cp_parser_name_lookup_error (cp_parser* parser,
2121 tree name,
2122 tree decl,
2123 const char* desired,
2124 location_t location)
2126 /* If name lookup completely failed, tell the user that NAME was not
2127 declared. */
2128 if (decl == error_mark_node)
2130 if (parser->scope && parser->scope != global_namespace)
2131 error_at (location, "%<%E::%E%> has not been declared",
2132 parser->scope, name);
2133 else if (parser->scope == global_namespace)
2134 error_at (location, "%<::%E%> has not been declared", name);
2135 else if (parser->object_scope
2136 && !CLASS_TYPE_P (parser->object_scope))
2137 error_at (location, "request for member %qE in non-class type %qT",
2138 name, parser->object_scope);
2139 else if (parser->object_scope)
2140 error_at (location, "%<%T::%E%> has not been declared",
2141 parser->object_scope, name);
2142 else
2143 error_at (location, "%qE has not been declared", name);
2145 else if (parser->scope && parser->scope != global_namespace)
2146 error_at (location, "%<%E::%E%> %s", parser->scope, name, desired);
2147 else if (parser->scope == global_namespace)
2148 error_at (location, "%<::%E%> %s", name, desired);
2149 else
2150 error_at (location, "%qE %s", name, desired);
2153 /* If we are parsing tentatively, remember that an error has occurred
2154 during this tentative parse. Returns true if the error was
2155 simulated; false if a message should be issued by the caller. */
2157 static bool
2158 cp_parser_simulate_error (cp_parser* parser)
2160 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2162 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2163 return true;
2165 return false;
2168 /* Check for repeated decl-specifiers. */
2170 static void
2171 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2172 location_t location)
2174 int ds;
2176 for (ds = ds_first; ds != ds_last; ++ds)
2178 unsigned count = decl_specs->specs[ds];
2179 if (count < 2)
2180 continue;
2181 /* The "long" specifier is a special case because of "long long". */
2182 if (ds == ds_long)
2184 if (count > 2)
2185 error_at (location, "%<long long long%> is too long for GCC");
2186 else
2187 pedwarn_cxx98 (location, OPT_Wlong_long,
2188 "ISO C++ 1998 does not support %<long long%>");
2190 else if (count > 1)
2192 static const char *const decl_spec_names[] = {
2193 "signed",
2194 "unsigned",
2195 "short",
2196 "long",
2197 "const",
2198 "volatile",
2199 "restrict",
2200 "inline",
2201 "virtual",
2202 "explicit",
2203 "friend",
2204 "typedef",
2205 "constexpr",
2206 "__complex",
2207 "__thread"
2209 error_at (location, "duplicate %qs", decl_spec_names[ds]);
2214 /* This function is called when a type is defined. If type
2215 definitions are forbidden at this point, an error message is
2216 issued. */
2218 static bool
2219 cp_parser_check_type_definition (cp_parser* parser)
2221 /* If types are forbidden here, issue a message. */
2222 if (parser->type_definition_forbidden_message)
2224 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2225 in the message need to be interpreted. */
2226 error (parser->type_definition_forbidden_message);
2227 return false;
2229 return true;
2232 /* This function is called when the DECLARATOR is processed. The TYPE
2233 was a type defined in the decl-specifiers. If it is invalid to
2234 define a type in the decl-specifiers for DECLARATOR, an error is
2235 issued. TYPE_LOCATION is the location of TYPE and is used
2236 for error reporting. */
2238 static void
2239 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2240 tree type, location_t type_location)
2242 /* [dcl.fct] forbids type definitions in return types.
2243 Unfortunately, it's not easy to know whether or not we are
2244 processing a return type until after the fact. */
2245 while (declarator
2246 && (declarator->kind == cdk_pointer
2247 || declarator->kind == cdk_reference
2248 || declarator->kind == cdk_ptrmem))
2249 declarator = declarator->declarator;
2250 if (declarator
2251 && declarator->kind == cdk_function)
2253 error_at (type_location,
2254 "new types may not be defined in a return type");
2255 inform (type_location,
2256 "(perhaps a semicolon is missing after the definition of %qT)",
2257 type);
2261 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2262 "<" in any valid C++ program. If the next token is indeed "<",
2263 issue a message warning the user about what appears to be an
2264 invalid attempt to form a template-id. LOCATION is the location
2265 of the type-specifier (TYPE) */
2267 static void
2268 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2269 tree type, location_t location)
2271 cp_token_position start = 0;
2273 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2275 if (TYPE_P (type))
2276 error_at (location, "%qT is not a template", type);
2277 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2278 error_at (location, "%qE is not a template", type);
2279 else
2280 error_at (location, "invalid template-id");
2281 /* Remember the location of the invalid "<". */
2282 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2283 start = cp_lexer_token_position (parser->lexer, true);
2284 /* Consume the "<". */
2285 cp_lexer_consume_token (parser->lexer);
2286 /* Parse the template arguments. */
2287 cp_parser_enclosed_template_argument_list (parser);
2288 /* Permanently remove the invalid template arguments so that
2289 this error message is not issued again. */
2290 if (start)
2291 cp_lexer_purge_tokens_after (parser->lexer, start);
2295 /* If parsing an integral constant-expression, issue an error message
2296 about the fact that THING appeared and return true. Otherwise,
2297 return false. In either case, set
2298 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2300 static bool
2301 cp_parser_non_integral_constant_expression (cp_parser *parser,
2302 const char *thing)
2304 parser->non_integral_constant_expression_p = true;
2305 if (parser->integral_constant_expression_p)
2307 if (!parser->allow_non_integral_constant_expression_p)
2309 /* Don't use `%s' to print THING, because quotations (`%<', `%>')
2310 in the message need to be interpreted. */
2311 char *message = concat (thing,
2312 " cannot appear in a constant-expression",
2313 NULL);
2314 error (message);
2315 free (message);
2316 return true;
2319 return false;
2322 /* Emit a diagnostic for an invalid type name. SCOPE is the
2323 qualifying scope (or NULL, if none) for ID. This function commits
2324 to the current active tentative parse, if any. (Otherwise, the
2325 problematic construct might be encountered again later, resulting
2326 in duplicate error messages.) LOCATION is the location of ID. */
2328 static void
2329 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2330 tree scope, tree id,
2331 location_t location)
2333 tree decl, old_scope;
2334 /* Try to lookup the identifier. */
2335 old_scope = parser->scope;
2336 parser->scope = scope;
2337 decl = cp_parser_lookup_name_simple (parser, id, location);
2338 parser->scope = old_scope;
2339 /* If the lookup found a template-name, it means that the user forgot
2340 to specify an argument list. Emit a useful error message. */
2341 if (TREE_CODE (decl) == TEMPLATE_DECL)
2342 error_at (location,
2343 "invalid use of template-name %qE without an argument list",
2344 decl);
2345 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2346 error_at (location, "invalid use of destructor %qD as a type", id);
2347 else if (TREE_CODE (decl) == TYPE_DECL)
2348 /* Something like 'unsigned A a;' */
2349 error_at (location, "invalid combination of multiple type-specifiers");
2350 else if (!parser->scope)
2352 /* Issue an error message. */
2353 error_at (location, "%qE does not name a type", id);
2354 /* If we're in a template class, it's possible that the user was
2355 referring to a type from a base class. For example:
2357 template <typename T> struct A { typedef T X; };
2358 template <typename T> struct B : public A<T> { X x; };
2360 The user should have said "typename A<T>::X". */
2361 if (processing_template_decl && current_class_type
2362 && TYPE_BINFO (current_class_type))
2364 tree b;
2366 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2368 b = TREE_CHAIN (b))
2370 tree base_type = BINFO_TYPE (b);
2371 if (CLASS_TYPE_P (base_type)
2372 && dependent_type_p (base_type))
2374 tree field;
2375 /* Go from a particular instantiation of the
2376 template (which will have an empty TYPE_FIELDs),
2377 to the main version. */
2378 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2379 for (field = TYPE_FIELDS (base_type);
2380 field;
2381 field = TREE_CHAIN (field))
2382 if (TREE_CODE (field) == TYPE_DECL
2383 && DECL_NAME (field) == id)
2385 inform (location,
2386 "(perhaps %<typename %T::%E%> was intended)",
2387 BINFO_TYPE (b), id);
2388 break;
2390 if (field)
2391 break;
2396 /* Here we diagnose qualified-ids where the scope is actually correct,
2397 but the identifier does not resolve to a valid type name. */
2398 else if (parser->scope != error_mark_node)
2400 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2401 error_at (location, "%qE in namespace %qE does not name a type",
2402 id, parser->scope);
2403 else if (CLASS_TYPE_P (parser->scope)
2404 && constructor_name_p (id, parser->scope))
2406 /* A<T>::A<T>() */
2407 error_at (location, "%<%T::%E%> names the constructor, not"
2408 " the type", parser->scope, id);
2409 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2410 error_at (location, "and %qT has no template constructors",
2411 parser->scope);
2413 else if (TYPE_P (parser->scope)
2414 && dependent_scope_p (parser->scope))
2415 error_at (location, "need %<typename%> before %<%T::%E%> because "
2416 "%qT is a dependent scope",
2417 parser->scope, id, parser->scope);
2418 else if (TYPE_P (parser->scope))
2419 error_at (location, "%qE in class %qT does not name a type",
2420 id, parser->scope);
2421 else
2422 gcc_unreachable ();
2424 cp_parser_commit_to_tentative_parse (parser);
2427 /* Check for a common situation where a type-name should be present,
2428 but is not, and issue a sensible error message. Returns true if an
2429 invalid type-name was detected.
2431 The situation handled by this function are variable declarations of the
2432 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2433 Usually, `ID' should name a type, but if we got here it means that it
2434 does not. We try to emit the best possible error message depending on
2435 how exactly the id-expression looks like. */
2437 static bool
2438 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2440 tree id;
2441 cp_token *token = cp_lexer_peek_token (parser->lexer);
2443 /* Avoid duplicate error about ambiguous lookup. */
2444 if (token->type == CPP_NESTED_NAME_SPECIFIER)
2446 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2447 if (next->type == CPP_NAME && next->ambiguous_p)
2448 goto out;
2451 cp_parser_parse_tentatively (parser);
2452 id = cp_parser_id_expression (parser,
2453 /*template_keyword_p=*/false,
2454 /*check_dependency_p=*/true,
2455 /*template_p=*/NULL,
2456 /*declarator_p=*/true,
2457 /*optional_p=*/false);
2458 /* If the next token is a (, this is a function with no explicit return
2459 type, i.e. constructor, destructor or conversion op. */
2460 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2461 || TREE_CODE (id) == TYPE_DECL)
2463 cp_parser_abort_tentative_parse (parser);
2464 return false;
2466 if (!cp_parser_parse_definitely (parser))
2467 return false;
2469 /* Emit a diagnostic for the invalid type. */
2470 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2471 id, token->location);
2472 out:
2473 /* If we aren't in the middle of a declarator (i.e. in a
2474 parameter-declaration-clause), skip to the end of the declaration;
2475 there's no point in trying to process it. */
2476 if (!parser->in_declarator_p)
2477 cp_parser_skip_to_end_of_block_or_statement (parser);
2478 return true;
2481 /* Consume tokens up to, and including, the next non-nested closing `)'.
2482 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2483 are doing error recovery. Returns -1 if OR_COMMA is true and we
2484 found an unnested comma. */
2486 static int
2487 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2488 bool recovering,
2489 bool or_comma,
2490 bool consume_paren)
2492 unsigned paren_depth = 0;
2493 unsigned brace_depth = 0;
2494 unsigned square_depth = 0;
2496 if (recovering && !or_comma
2497 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2498 return 0;
2500 while (true)
2502 cp_token * token = cp_lexer_peek_token (parser->lexer);
2504 switch (token->type)
2506 case CPP_EOF:
2507 case CPP_PRAGMA_EOL:
2508 /* If we've run out of tokens, then there is no closing `)'. */
2509 return 0;
2511 /* This is good for lambda expression capture-lists. */
2512 case CPP_OPEN_SQUARE:
2513 ++square_depth;
2514 break;
2515 case CPP_CLOSE_SQUARE:
2516 if (!square_depth--)
2517 return 0;
2518 break;
2520 case CPP_SEMICOLON:
2521 /* This matches the processing in skip_to_end_of_statement. */
2522 if (!brace_depth)
2523 return 0;
2524 break;
2526 case CPP_OPEN_BRACE:
2527 ++brace_depth;
2528 break;
2529 case CPP_CLOSE_BRACE:
2530 if (!brace_depth--)
2531 return 0;
2532 break;
2534 case CPP_COMMA:
2535 if (recovering && or_comma && !brace_depth && !paren_depth
2536 && !square_depth)
2537 return -1;
2538 break;
2540 case CPP_OPEN_PAREN:
2541 if (!brace_depth)
2542 ++paren_depth;
2543 break;
2545 case CPP_CLOSE_PAREN:
2546 if (!brace_depth && !paren_depth--)
2548 if (consume_paren)
2549 cp_lexer_consume_token (parser->lexer);
2550 return 1;
2552 break;
2554 default:
2555 break;
2558 /* Consume the token. */
2559 cp_lexer_consume_token (parser->lexer);
2563 /* Consume tokens until we reach the end of the current statement.
2564 Normally, that will be just before consuming a `;'. However, if a
2565 non-nested `}' comes first, then we stop before consuming that. */
2567 static void
2568 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2570 unsigned nesting_depth = 0;
2572 while (true)
2574 cp_token *token = cp_lexer_peek_token (parser->lexer);
2576 switch (token->type)
2578 case CPP_EOF:
2579 case CPP_PRAGMA_EOL:
2580 /* If we've run out of tokens, stop. */
2581 return;
2583 case CPP_SEMICOLON:
2584 /* If the next token is a `;', we have reached the end of the
2585 statement. */
2586 if (!nesting_depth)
2587 return;
2588 break;
2590 case CPP_CLOSE_BRACE:
2591 /* If this is a non-nested '}', stop before consuming it.
2592 That way, when confronted with something like:
2594 { 3 + }
2596 we stop before consuming the closing '}', even though we
2597 have not yet reached a `;'. */
2598 if (nesting_depth == 0)
2599 return;
2601 /* If it is the closing '}' for a block that we have
2602 scanned, stop -- but only after consuming the token.
2603 That way given:
2605 void f g () { ... }
2606 typedef int I;
2608 we will stop after the body of the erroneously declared
2609 function, but before consuming the following `typedef'
2610 declaration. */
2611 if (--nesting_depth == 0)
2613 cp_lexer_consume_token (parser->lexer);
2614 return;
2617 case CPP_OPEN_BRACE:
2618 ++nesting_depth;
2619 break;
2621 default:
2622 break;
2625 /* Consume the token. */
2626 cp_lexer_consume_token (parser->lexer);
2630 /* This function is called at the end of a statement or declaration.
2631 If the next token is a semicolon, it is consumed; otherwise, error
2632 recovery is attempted. */
2634 static void
2635 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2637 /* Look for the trailing `;'. */
2638 if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2640 /* If there is additional (erroneous) input, skip to the end of
2641 the statement. */
2642 cp_parser_skip_to_end_of_statement (parser);
2643 /* If the next token is now a `;', consume it. */
2644 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2645 cp_lexer_consume_token (parser->lexer);
2649 /* Skip tokens until we have consumed an entire block, or until we
2650 have consumed a non-nested `;'. */
2652 static void
2653 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2655 int nesting_depth = 0;
2657 while (nesting_depth >= 0)
2659 cp_token *token = cp_lexer_peek_token (parser->lexer);
2661 switch (token->type)
2663 case CPP_EOF:
2664 case CPP_PRAGMA_EOL:
2665 /* If we've run out of tokens, stop. */
2666 return;
2668 case CPP_SEMICOLON:
2669 /* Stop if this is an unnested ';'. */
2670 if (!nesting_depth)
2671 nesting_depth = -1;
2672 break;
2674 case CPP_CLOSE_BRACE:
2675 /* Stop if this is an unnested '}', or closes the outermost
2676 nesting level. */
2677 nesting_depth--;
2678 if (nesting_depth < 0)
2679 return;
2680 if (!nesting_depth)
2681 nesting_depth = -1;
2682 break;
2684 case CPP_OPEN_BRACE:
2685 /* Nest. */
2686 nesting_depth++;
2687 break;
2689 default:
2690 break;
2693 /* Consume the token. */
2694 cp_lexer_consume_token (parser->lexer);
2698 /* Skip tokens until a non-nested closing curly brace is the next
2699 token, or there are no more tokens. Return true in the first case,
2700 false otherwise. */
2702 static bool
2703 cp_parser_skip_to_closing_brace (cp_parser *parser)
2705 unsigned nesting_depth = 0;
2707 while (true)
2709 cp_token *token = cp_lexer_peek_token (parser->lexer);
2711 switch (token->type)
2713 case CPP_EOF:
2714 case CPP_PRAGMA_EOL:
2715 /* If we've run out of tokens, stop. */
2716 return false;
2718 case CPP_CLOSE_BRACE:
2719 /* If the next token is a non-nested `}', then we have reached
2720 the end of the current block. */
2721 if (nesting_depth-- == 0)
2722 return true;
2723 break;
2725 case CPP_OPEN_BRACE:
2726 /* If it the next token is a `{', then we are entering a new
2727 block. Consume the entire block. */
2728 ++nesting_depth;
2729 break;
2731 default:
2732 break;
2735 /* Consume the token. */
2736 cp_lexer_consume_token (parser->lexer);
2740 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2741 parameter is the PRAGMA token, allowing us to purge the entire pragma
2742 sequence. */
2744 static void
2745 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2747 cp_token *token;
2749 parser->lexer->in_pragma = false;
2752 token = cp_lexer_consume_token (parser->lexer);
2753 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2755 /* Ensure that the pragma is not parsed again. */
2756 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2759 /* Require pragma end of line, resyncing with it as necessary. The
2760 arguments are as for cp_parser_skip_to_pragma_eol. */
2762 static void
2763 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2765 parser->lexer->in_pragma = false;
2766 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2767 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2770 /* This is a simple wrapper around make_typename_type. When the id is
2771 an unresolved identifier node, we can provide a superior diagnostic
2772 using cp_parser_diagnose_invalid_type_name. */
2774 static tree
2775 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2776 tree id, location_t id_location)
2778 tree result;
2779 if (TREE_CODE (id) == IDENTIFIER_NODE)
2781 result = make_typename_type (scope, id, typename_type,
2782 /*complain=*/tf_none);
2783 if (result == error_mark_node)
2784 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2785 return result;
2787 return make_typename_type (scope, id, typename_type, tf_error);
2790 /* This is a wrapper around the
2791 make_{pointer,ptrmem,reference}_declarator functions that decides
2792 which one to call based on the CODE and CLASS_TYPE arguments. The
2793 CODE argument should be one of the values returned by
2794 cp_parser_ptr_operator. */
2795 static cp_declarator *
2796 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2797 cp_cv_quals cv_qualifiers,
2798 cp_declarator *target)
2800 if (code == ERROR_MARK)
2801 return cp_error_declarator;
2803 if (code == INDIRECT_REF)
2804 if (class_type == NULL_TREE)
2805 return make_pointer_declarator (cv_qualifiers, target);
2806 else
2807 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2808 else if (code == ADDR_EXPR && class_type == NULL_TREE)
2809 return make_reference_declarator (cv_qualifiers, target, false);
2810 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2811 return make_reference_declarator (cv_qualifiers, target, true);
2812 gcc_unreachable ();
2815 /* Create a new C++ parser. */
2817 static cp_parser *
2818 cp_parser_new (void)
2820 cp_parser *parser;
2821 cp_lexer *lexer;
2822 unsigned i;
2824 /* cp_lexer_new_main is called before calling ggc_alloc because
2825 cp_lexer_new_main might load a PCH file. */
2826 lexer = cp_lexer_new_main ();
2828 /* Initialize the binops_by_token so that we can get the tree
2829 directly from the token. */
2830 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2831 binops_by_token[binops[i].token_type] = binops[i];
2833 parser = GGC_CNEW (cp_parser);
2834 parser->lexer = lexer;
2835 parser->context = cp_parser_context_new (NULL);
2837 /* For now, we always accept GNU extensions. */
2838 parser->allow_gnu_extensions_p = 1;
2840 /* The `>' token is a greater-than operator, not the end of a
2841 template-id. */
2842 parser->greater_than_is_operator_p = true;
2844 parser->default_arg_ok_p = true;
2846 /* We are not parsing a constant-expression. */
2847 parser->integral_constant_expression_p = false;
2848 parser->allow_non_integral_constant_expression_p = false;
2849 parser->non_integral_constant_expression_p = false;
2851 /* Local variable names are not forbidden. */
2852 parser->local_variables_forbidden_p = false;
2854 /* We are not processing an `extern "C"' declaration. */
2855 parser->in_unbraced_linkage_specification_p = false;
2857 /* We are not processing a declarator. */
2858 parser->in_declarator_p = false;
2860 /* We are not processing a template-argument-list. */
2861 parser->in_template_argument_list_p = false;
2863 /* We are not in an iteration statement. */
2864 parser->in_statement = 0;
2866 /* We are not in a switch statement. */
2867 parser->in_switch_statement_p = false;
2869 /* We are not parsing a type-id inside an expression. */
2870 parser->in_type_id_in_expr_p = false;
2872 /* Declarations aren't implicitly extern "C". */
2873 parser->implicit_extern_c = false;
2875 /* String literals should be translated to the execution character set. */
2876 parser->translate_strings_p = true;
2878 /* We are not parsing a function body. */
2879 parser->in_function_body = false;
2881 /* The unparsed function queue is empty. */
2882 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2884 /* There are no classes being defined. */
2885 parser->num_classes_being_defined = 0;
2887 /* No template parameters apply. */
2888 parser->num_template_parameter_lists = 0;
2890 return parser;
2893 /* Create a cp_lexer structure which will emit the tokens in CACHE
2894 and push it onto the parser's lexer stack. This is used for delayed
2895 parsing of in-class method bodies and default arguments, and should
2896 not be confused with tentative parsing. */
2897 static void
2898 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2900 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2901 lexer->next = parser->lexer;
2902 parser->lexer = lexer;
2904 /* Move the current source position to that of the first token in the
2905 new lexer. */
2906 cp_lexer_set_source_position_from_token (lexer->next_token);
2909 /* Pop the top lexer off the parser stack. This is never used for the
2910 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2911 static void
2912 cp_parser_pop_lexer (cp_parser *parser)
2914 cp_lexer *lexer = parser->lexer;
2915 parser->lexer = lexer->next;
2916 cp_lexer_destroy (lexer);
2918 /* Put the current source position back where it was before this
2919 lexer was pushed. */
2920 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2923 /* Lexical conventions [gram.lex] */
2925 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2926 identifier. */
2928 static tree
2929 cp_parser_identifier (cp_parser* parser)
2931 cp_token *token;
2933 /* Look for the identifier. */
2934 token = cp_parser_require (parser, CPP_NAME, "identifier");
2935 /* Return the value. */
2936 return token ? token->u.value : error_mark_node;
2939 /* Parse a sequence of adjacent string constants. Returns a
2940 TREE_STRING representing the combined, nul-terminated string
2941 constant. If TRANSLATE is true, translate the string to the
2942 execution character set. If WIDE_OK is true, a wide string is
2943 invalid here.
2945 C++98 [lex.string] says that if a narrow string literal token is
2946 adjacent to a wide string literal token, the behavior is undefined.
2947 However, C99 6.4.5p4 says that this results in a wide string literal.
2948 We follow C99 here, for consistency with the C front end.
2950 This code is largely lifted from lex_string() in c-lex.c.
2952 FUTURE: ObjC++ will need to handle @-strings here. */
2953 static tree
2954 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2956 tree value;
2957 size_t count;
2958 struct obstack str_ob;
2959 cpp_string str, istr, *strs;
2960 cp_token *tok;
2961 enum cpp_ttype type;
2963 tok = cp_lexer_peek_token (parser->lexer);
2964 if (!cp_parser_is_string_literal (tok))
2966 cp_parser_error (parser, "expected string-literal");
2967 return error_mark_node;
2970 type = tok->type;
2972 /* Try to avoid the overhead of creating and destroying an obstack
2973 for the common case of just one string. */
2974 if (!cp_parser_is_string_literal
2975 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2977 cp_lexer_consume_token (parser->lexer);
2979 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2980 str.len = TREE_STRING_LENGTH (tok->u.value);
2981 count = 1;
2983 strs = &str;
2985 else
2987 gcc_obstack_init (&str_ob);
2988 count = 0;
2992 cp_lexer_consume_token (parser->lexer);
2993 count++;
2994 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2995 str.len = TREE_STRING_LENGTH (tok->u.value);
2997 if (type != tok->type)
2999 if (type == CPP_STRING)
3000 type = tok->type;
3001 else if (tok->type != CPP_STRING)
3002 error_at (tok->location,
3003 "unsupported non-standard concatenation "
3004 "of string literals");
3007 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3009 tok = cp_lexer_peek_token (parser->lexer);
3011 while (cp_parser_is_string_literal (tok));
3013 strs = (cpp_string *) obstack_finish (&str_ob);
3016 if (type != CPP_STRING && !wide_ok)
3018 cp_parser_error (parser, "a wide string is invalid in this context");
3019 type = CPP_STRING;
3022 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3023 (parse_in, strs, count, &istr, type))
3025 value = build_string (istr.len, (const char *)istr.text);
3026 free (CONST_CAST (unsigned char *, istr.text));
3028 switch (type)
3030 default:
3031 case CPP_STRING:
3032 case CPP_UTF8STRING:
3033 TREE_TYPE (value) = char_array_type_node;
3034 break;
3035 case CPP_STRING16:
3036 TREE_TYPE (value) = char16_array_type_node;
3037 break;
3038 case CPP_STRING32:
3039 TREE_TYPE (value) = char32_array_type_node;
3040 break;
3041 case CPP_WSTRING:
3042 TREE_TYPE (value) = wchar_array_type_node;
3043 break;
3046 value = fix_string_type (value);
3048 else
3049 /* cpp_interpret_string has issued an error. */
3050 value = error_mark_node;
3052 if (count > 1)
3053 obstack_free (&str_ob, 0);
3055 return value;
3059 /* Basic concepts [gram.basic] */
3061 /* Parse a translation-unit.
3063 translation-unit:
3064 declaration-seq [opt]
3066 Returns TRUE if all went well. */
3068 static bool
3069 cp_parser_translation_unit (cp_parser* parser)
3071 /* The address of the first non-permanent object on the declarator
3072 obstack. */
3073 static void *declarator_obstack_base;
3075 bool success;
3077 /* Create the declarator obstack, if necessary. */
3078 if (!cp_error_declarator)
3080 gcc_obstack_init (&declarator_obstack);
3081 /* Create the error declarator. */
3082 cp_error_declarator = make_declarator (cdk_error);
3083 /* Create the empty parameter list. */
3084 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3085 /* Remember where the base of the declarator obstack lies. */
3086 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3089 cp_parser_declaration_seq_opt (parser);
3091 /* If there are no tokens left then all went well. */
3092 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3094 /* Get rid of the token array; we don't need it any more. */
3095 cp_lexer_destroy (parser->lexer);
3096 parser->lexer = NULL;
3098 /* This file might have been a context that's implicitly extern
3099 "C". If so, pop the lang context. (Only relevant for PCH.) */
3100 if (parser->implicit_extern_c)
3102 pop_lang_context ();
3103 parser->implicit_extern_c = false;
3106 /* Finish up. */
3107 finish_translation_unit ();
3109 success = true;
3111 else
3113 cp_parser_error (parser, "expected declaration");
3114 success = false;
3117 /* Make sure the declarator obstack was fully cleaned up. */
3118 gcc_assert (obstack_next_free (&declarator_obstack)
3119 == declarator_obstack_base);
3121 /* All went well. */
3122 return success;
3125 /* Expressions [gram.expr] */
3127 /* Parse a primary-expression.
3129 primary-expression:
3130 literal
3131 this
3132 ( expression )
3133 id-expression
3135 GNU Extensions:
3137 primary-expression:
3138 ( compound-statement )
3139 __builtin_va_arg ( assignment-expression , type-id )
3140 __builtin_offsetof ( type-id , offsetof-expression )
3142 C++ Extensions:
3143 __has_nothrow_assign ( type-id )
3144 __has_nothrow_constructor ( type-id )
3145 __has_nothrow_copy ( type-id )
3146 __has_trivial_assign ( type-id )
3147 __has_trivial_constructor ( type-id )
3148 __has_trivial_copy ( type-id )
3149 __has_trivial_destructor ( type-id )
3150 __has_virtual_destructor ( type-id )
3151 __is_abstract ( type-id )
3152 __is_base_of ( type-id , type-id )
3153 __is_class ( type-id )
3154 __is_convertible_to ( type-id , type-id )
3155 __is_empty ( type-id )
3156 __is_enum ( type-id )
3157 __is_pod ( type-id )
3158 __is_polymorphic ( type-id )
3159 __is_union ( type-id )
3161 Objective-C++ Extension:
3163 primary-expression:
3164 objc-expression
3166 literal:
3167 __null
3169 ADDRESS_P is true iff this expression was immediately preceded by
3170 "&" and therefore might denote a pointer-to-member. CAST_P is true
3171 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3172 true iff this expression is a template argument.
3174 Returns a representation of the expression. Upon return, *IDK
3175 indicates what kind of id-expression (if any) was present. */
3177 static tree
3178 cp_parser_primary_expression (cp_parser *parser,
3179 bool address_p,
3180 bool cast_p,
3181 bool template_arg_p,
3182 cp_id_kind *idk)
3184 cp_token *token = NULL;
3186 /* Assume the primary expression is not an id-expression. */
3187 *idk = CP_ID_KIND_NONE;
3189 /* Peek at the next token. */
3190 token = cp_lexer_peek_token (parser->lexer);
3191 switch (token->type)
3193 /* literal:
3194 integer-literal
3195 character-literal
3196 floating-literal
3197 string-literal
3198 boolean-literal */
3199 case CPP_CHAR:
3200 case CPP_CHAR16:
3201 case CPP_CHAR32:
3202 case CPP_WCHAR:
3203 case CPP_NUMBER:
3204 token = cp_lexer_consume_token (parser->lexer);
3205 if (TREE_CODE (token->u.value) == FIXED_CST)
3207 error_at (token->location,
3208 "fixed-point types not supported in C++");
3209 return error_mark_node;
3211 /* Floating-point literals are only allowed in an integral
3212 constant expression if they are cast to an integral or
3213 enumeration type. */
3214 if (TREE_CODE (token->u.value) == REAL_CST
3215 && parser->integral_constant_expression_p
3216 && pedantic)
3218 /* CAST_P will be set even in invalid code like "int(2.7 +
3219 ...)". Therefore, we have to check that the next token
3220 is sure to end the cast. */
3221 if (cast_p)
3223 cp_token *next_token;
3225 next_token = cp_lexer_peek_token (parser->lexer);
3226 if (/* The comma at the end of an
3227 enumerator-definition. */
3228 next_token->type != CPP_COMMA
3229 /* The curly brace at the end of an enum-specifier. */
3230 && next_token->type != CPP_CLOSE_BRACE
3231 /* The end of a statement. */
3232 && next_token->type != CPP_SEMICOLON
3233 /* The end of the cast-expression. */
3234 && next_token->type != CPP_CLOSE_PAREN
3235 /* The end of an array bound. */
3236 && next_token->type != CPP_CLOSE_SQUARE
3237 /* The closing ">" in a template-argument-list. */
3238 && (next_token->type != CPP_GREATER
3239 || parser->greater_than_is_operator_p)
3240 /* C++0x only: A ">>" treated like two ">" tokens,
3241 in a template-argument-list. */
3242 && (next_token->type != CPP_RSHIFT
3243 || (cxx_dialect == cxx98)
3244 || parser->greater_than_is_operator_p))
3245 cast_p = false;
3248 /* If we are within a cast, then the constraint that the
3249 cast is to an integral or enumeration type will be
3250 checked at that point. If we are not within a cast, then
3251 this code is invalid. */
3252 if (!cast_p)
3253 cp_parser_non_integral_constant_expression
3254 (parser, "floating-point literal");
3256 return token->u.value;
3258 case CPP_STRING:
3259 case CPP_STRING16:
3260 case CPP_STRING32:
3261 case CPP_WSTRING:
3262 case CPP_UTF8STRING:
3263 /* ??? Should wide strings be allowed when parser->translate_strings_p
3264 is false (i.e. in attributes)? If not, we can kill the third
3265 argument to cp_parser_string_literal. */
3266 return cp_parser_string_literal (parser,
3267 parser->translate_strings_p,
3268 true);
3270 case CPP_OPEN_PAREN:
3272 tree expr;
3273 bool saved_greater_than_is_operator_p;
3275 /* Consume the `('. */
3276 cp_lexer_consume_token (parser->lexer);
3277 /* Within a parenthesized expression, a `>' token is always
3278 the greater-than operator. */
3279 saved_greater_than_is_operator_p
3280 = parser->greater_than_is_operator_p;
3281 parser->greater_than_is_operator_p = true;
3282 /* If we see `( { ' then we are looking at the beginning of
3283 a GNU statement-expression. */
3284 if (cp_parser_allow_gnu_extensions_p (parser)
3285 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3287 /* Statement-expressions are not allowed by the standard. */
3288 pedwarn (token->location, OPT_pedantic,
3289 "ISO C++ forbids braced-groups within expressions");
3291 /* And they're not allowed outside of a function-body; you
3292 cannot, for example, write:
3294 int i = ({ int j = 3; j + 1; });
3296 at class or namespace scope. */
3297 if (!parser->in_function_body
3298 || parser->in_template_argument_list_p)
3300 error_at (token->location,
3301 "statement-expressions are not allowed outside "
3302 "functions nor in template-argument lists");
3303 cp_parser_skip_to_end_of_block_or_statement (parser);
3304 expr = error_mark_node;
3306 else
3308 /* Start the statement-expression. */
3309 expr = begin_stmt_expr ();
3310 /* Parse the compound-statement. */
3311 cp_parser_compound_statement (parser, expr, false);
3312 /* Finish up. */
3313 expr = finish_stmt_expr (expr, false);
3316 else
3318 /* Parse the parenthesized expression. */
3319 expr = cp_parser_expression (parser, cast_p, idk);
3320 /* Let the front end know that this expression was
3321 enclosed in parentheses. This matters in case, for
3322 example, the expression is of the form `A::B', since
3323 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3324 not. */
3325 finish_parenthesized_expr (expr);
3327 /* The `>' token might be the end of a template-id or
3328 template-parameter-list now. */
3329 parser->greater_than_is_operator_p
3330 = saved_greater_than_is_operator_p;
3331 /* Consume the `)'. */
3332 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3333 cp_parser_skip_to_end_of_statement (parser);
3335 return expr;
3338 case CPP_OPEN_SQUARE:
3339 if (c_dialect_objc ())
3340 /* We have an Objective-C++ message. */
3341 return cp_parser_objc_expression (parser);
3342 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
3343 return cp_parser_lambda_expression (parser);
3345 case CPP_OBJC_STRING:
3346 if (c_dialect_objc ())
3347 /* We have an Objective-C++ string literal. */
3348 return cp_parser_objc_expression (parser);
3349 cp_parser_error (parser, "expected primary-expression");
3350 return error_mark_node;
3352 case CPP_KEYWORD:
3353 switch (token->keyword)
3355 /* These two are the boolean literals. */
3356 case RID_TRUE:
3357 cp_lexer_consume_token (parser->lexer);
3358 return boolean_true_node;
3359 case RID_FALSE:
3360 cp_lexer_consume_token (parser->lexer);
3361 return boolean_false_node;
3363 /* The `__null' literal. */
3364 case RID_NULL:
3365 cp_lexer_consume_token (parser->lexer);
3366 return null_node;
3368 /* Recognize the `this' keyword. */
3369 case RID_THIS:
3370 cp_lexer_consume_token (parser->lexer);
3371 if (parser->local_variables_forbidden_p)
3373 error_at (token->location,
3374 "%<this%> may not be used in this context");
3375 return error_mark_node;
3377 /* Pointers cannot appear in constant-expressions. */
3378 if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3379 return error_mark_node;
3380 return finish_this_expr ();
3382 /* The `operator' keyword can be the beginning of an
3383 id-expression. */
3384 case RID_OPERATOR:
3385 goto id_expression;
3387 case RID_FUNCTION_NAME:
3388 case RID_PRETTY_FUNCTION_NAME:
3389 case RID_C99_FUNCTION_NAME:
3391 const char *name;
3393 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3394 __func__ are the names of variables -- but they are
3395 treated specially. Therefore, they are handled here,
3396 rather than relying on the generic id-expression logic
3397 below. Grammatically, these names are id-expressions.
3399 Consume the token. */
3400 token = cp_lexer_consume_token (parser->lexer);
3402 switch (token->keyword)
3404 case RID_FUNCTION_NAME:
3405 name = "%<__FUNCTION__%>";
3406 break;
3407 case RID_PRETTY_FUNCTION_NAME:
3408 name = "%<__PRETTY_FUNCTION__%>";
3409 break;
3410 case RID_C99_FUNCTION_NAME:
3411 name = "%<__func__%>";
3412 break;
3413 default:
3414 gcc_unreachable ();
3417 if (cp_parser_non_integral_constant_expression (parser, name))
3418 return error_mark_node;
3420 /* Look up the name. */
3421 return finish_fname (token->u.value);
3424 case RID_VA_ARG:
3426 tree expression;
3427 tree type;
3429 /* The `__builtin_va_arg' construct is used to handle
3430 `va_arg'. Consume the `__builtin_va_arg' token. */
3431 cp_lexer_consume_token (parser->lexer);
3432 /* Look for the opening `('. */
3433 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3434 /* Now, parse the assignment-expression. */
3435 expression = cp_parser_assignment_expression (parser,
3436 /*cast_p=*/false, NULL);
3437 /* Look for the `,'. */
3438 cp_parser_require (parser, CPP_COMMA, "%<,%>");
3439 /* Parse the type-id. */
3440 type = cp_parser_type_id (parser);
3441 /* Look for the closing `)'. */
3442 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3443 /* Using `va_arg' in a constant-expression is not
3444 allowed. */
3445 if (cp_parser_non_integral_constant_expression (parser,
3446 "%<va_arg%>"))
3447 return error_mark_node;
3448 return build_x_va_arg (expression, type);
3451 case RID_OFFSETOF:
3452 return cp_parser_builtin_offsetof (parser);
3454 case RID_HAS_NOTHROW_ASSIGN:
3455 case RID_HAS_NOTHROW_CONSTRUCTOR:
3456 case RID_HAS_NOTHROW_COPY:
3457 case RID_HAS_TRIVIAL_ASSIGN:
3458 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3459 case RID_HAS_TRIVIAL_COPY:
3460 case RID_HAS_TRIVIAL_DESTRUCTOR:
3461 case RID_HAS_VIRTUAL_DESTRUCTOR:
3462 case RID_IS_ABSTRACT:
3463 case RID_IS_BASE_OF:
3464 case RID_IS_CLASS:
3465 case RID_IS_CONVERTIBLE_TO:
3466 case RID_IS_EMPTY:
3467 case RID_IS_ENUM:
3468 case RID_IS_POD:
3469 case RID_IS_POLYMORPHIC:
3470 case RID_IS_STD_LAYOUT:
3471 case RID_IS_TRIVIAL:
3472 case RID_IS_UNION:
3473 return cp_parser_trait_expr (parser, token->keyword);
3475 /* Objective-C++ expressions. */
3476 case RID_AT_ENCODE:
3477 case RID_AT_PROTOCOL:
3478 case RID_AT_SELECTOR:
3479 return cp_parser_objc_expression (parser);
3481 default:
3482 cp_parser_error (parser, "expected primary-expression");
3483 return error_mark_node;
3486 /* An id-expression can start with either an identifier, a
3487 `::' as the beginning of a qualified-id, or the "operator"
3488 keyword. */
3489 case CPP_NAME:
3490 case CPP_SCOPE:
3491 case CPP_TEMPLATE_ID:
3492 case CPP_NESTED_NAME_SPECIFIER:
3494 tree id_expression;
3495 tree decl;
3496 const char *error_msg;
3497 bool template_p;
3498 bool done;
3499 cp_token *id_expr_token;
3501 id_expression:
3502 /* Parse the id-expression. */
3503 id_expression
3504 = cp_parser_id_expression (parser,
3505 /*template_keyword_p=*/false,
3506 /*check_dependency_p=*/true,
3507 &template_p,
3508 /*declarator_p=*/false,
3509 /*optional_p=*/false);
3510 if (id_expression == error_mark_node)
3511 return error_mark_node;
3512 id_expr_token = token;
3513 token = cp_lexer_peek_token (parser->lexer);
3514 done = (token->type != CPP_OPEN_SQUARE
3515 && token->type != CPP_OPEN_PAREN
3516 && token->type != CPP_DOT
3517 && token->type != CPP_DEREF
3518 && token->type != CPP_PLUS_PLUS
3519 && token->type != CPP_MINUS_MINUS);
3520 /* If we have a template-id, then no further lookup is
3521 required. If the template-id was for a template-class, we
3522 will sometimes have a TYPE_DECL at this point. */
3523 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3524 || TREE_CODE (id_expression) == TYPE_DECL)
3525 decl = id_expression;
3526 /* Look up the name. */
3527 else
3529 tree ambiguous_decls;
3531 /* If we already know that this lookup is ambiguous, then
3532 we've already issued an error message; there's no reason
3533 to check again. */
3534 if (id_expr_token->type == CPP_NAME
3535 && id_expr_token->ambiguous_p)
3537 cp_parser_simulate_error (parser);
3538 return error_mark_node;
3541 decl = cp_parser_lookup_name (parser, id_expression,
3542 none_type,
3543 template_p,
3544 /*is_namespace=*/false,
3545 /*check_dependency=*/true,
3546 &ambiguous_decls,
3547 id_expr_token->location);
3548 /* If the lookup was ambiguous, an error will already have
3549 been issued. */
3550 if (ambiguous_decls)
3551 return error_mark_node;
3553 /* In Objective-C++, an instance variable (ivar) may be preferred
3554 to whatever cp_parser_lookup_name() found. */
3555 decl = objc_lookup_ivar (decl, id_expression);
3557 /* If name lookup gives us a SCOPE_REF, then the
3558 qualifying scope was dependent. */
3559 if (TREE_CODE (decl) == SCOPE_REF)
3561 /* At this point, we do not know if DECL is a valid
3562 integral constant expression. We assume that it is
3563 in fact such an expression, so that code like:
3565 template <int N> struct A {
3566 int a[B<N>::i];
3569 is accepted. At template-instantiation time, we
3570 will check that B<N>::i is actually a constant. */
3571 return decl;
3573 /* Check to see if DECL is a local variable in a context
3574 where that is forbidden. */
3575 if (parser->local_variables_forbidden_p
3576 && local_variable_p (decl))
3578 /* It might be that we only found DECL because we are
3579 trying to be generous with pre-ISO scoping rules.
3580 For example, consider:
3582 int i;
3583 void g() {
3584 for (int i = 0; i < 10; ++i) {}
3585 extern void f(int j = i);
3588 Here, name look up will originally find the out
3589 of scope `i'. We need to issue a warning message,
3590 but then use the global `i'. */
3591 decl = check_for_out_of_scope_variable (decl);
3592 if (local_variable_p (decl))
3594 error_at (id_expr_token->location,
3595 "local variable %qD may not appear in this context",
3596 decl);
3597 return error_mark_node;
3602 decl = (finish_id_expression
3603 (id_expression, decl, parser->scope,
3604 idk,
3605 parser->integral_constant_expression_p,
3606 parser->allow_non_integral_constant_expression_p,
3607 &parser->non_integral_constant_expression_p,
3608 template_p, done, address_p,
3609 template_arg_p,
3610 &error_msg,
3611 id_expr_token->location));
3612 if (error_msg)
3613 cp_parser_error (parser, error_msg);
3614 return decl;
3617 /* Anything else is an error. */
3618 default:
3619 cp_parser_error (parser, "expected primary-expression");
3620 return error_mark_node;
3624 /* Parse an id-expression.
3626 id-expression:
3627 unqualified-id
3628 qualified-id
3630 qualified-id:
3631 :: [opt] nested-name-specifier template [opt] unqualified-id
3632 :: identifier
3633 :: operator-function-id
3634 :: template-id
3636 Return a representation of the unqualified portion of the
3637 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3638 a `::' or nested-name-specifier.
3640 Often, if the id-expression was a qualified-id, the caller will
3641 want to make a SCOPE_REF to represent the qualified-id. This
3642 function does not do this in order to avoid wastefully creating
3643 SCOPE_REFs when they are not required.
3645 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3646 `template' keyword.
3648 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3649 uninstantiated templates.
3651 If *TEMPLATE_P is non-NULL, it is set to true iff the
3652 `template' keyword is used to explicitly indicate that the entity
3653 named is a template.
3655 If DECLARATOR_P is true, the id-expression is appearing as part of
3656 a declarator, rather than as part of an expression. */
3658 static tree
3659 cp_parser_id_expression (cp_parser *parser,
3660 bool template_keyword_p,
3661 bool check_dependency_p,
3662 bool *template_p,
3663 bool declarator_p,
3664 bool optional_p)
3666 bool global_scope_p;
3667 bool nested_name_specifier_p;
3669 /* Assume the `template' keyword was not used. */
3670 if (template_p)
3671 *template_p = template_keyword_p;
3673 /* Look for the optional `::' operator. */
3674 global_scope_p
3675 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3676 != NULL_TREE);
3677 /* Look for the optional nested-name-specifier. */
3678 nested_name_specifier_p
3679 = (cp_parser_nested_name_specifier_opt (parser,
3680 /*typename_keyword_p=*/false,
3681 check_dependency_p,
3682 /*type_p=*/false,
3683 declarator_p)
3684 != NULL_TREE);
3685 /* If there is a nested-name-specifier, then we are looking at
3686 the first qualified-id production. */
3687 if (nested_name_specifier_p)
3689 tree saved_scope;
3690 tree saved_object_scope;
3691 tree saved_qualifying_scope;
3692 tree unqualified_id;
3693 bool is_template;
3695 /* See if the next token is the `template' keyword. */
3696 if (!template_p)
3697 template_p = &is_template;
3698 *template_p = cp_parser_optional_template_keyword (parser);
3699 /* Name lookup we do during the processing of the
3700 unqualified-id might obliterate SCOPE. */
3701 saved_scope = parser->scope;
3702 saved_object_scope = parser->object_scope;
3703 saved_qualifying_scope = parser->qualifying_scope;
3704 /* Process the final unqualified-id. */
3705 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3706 check_dependency_p,
3707 declarator_p,
3708 /*optional_p=*/false);
3709 /* Restore the SAVED_SCOPE for our caller. */
3710 parser->scope = saved_scope;
3711 parser->object_scope = saved_object_scope;
3712 parser->qualifying_scope = saved_qualifying_scope;
3714 return unqualified_id;
3716 /* Otherwise, if we are in global scope, then we are looking at one
3717 of the other qualified-id productions. */
3718 else if (global_scope_p)
3720 cp_token *token;
3721 tree id;
3723 /* Peek at the next token. */
3724 token = cp_lexer_peek_token (parser->lexer);
3726 /* If it's an identifier, and the next token is not a "<", then
3727 we can avoid the template-id case. This is an optimization
3728 for this common case. */
3729 if (token->type == CPP_NAME
3730 && !cp_parser_nth_token_starts_template_argument_list_p
3731 (parser, 2))
3732 return cp_parser_identifier (parser);
3734 cp_parser_parse_tentatively (parser);
3735 /* Try a template-id. */
3736 id = cp_parser_template_id (parser,
3737 /*template_keyword_p=*/false,
3738 /*check_dependency_p=*/true,
3739 declarator_p);
3740 /* If that worked, we're done. */
3741 if (cp_parser_parse_definitely (parser))
3742 return id;
3744 /* Peek at the next token. (Changes in the token buffer may
3745 have invalidated the pointer obtained above.) */
3746 token = cp_lexer_peek_token (parser->lexer);
3748 switch (token->type)
3750 case CPP_NAME:
3751 return cp_parser_identifier (parser);
3753 case CPP_KEYWORD:
3754 if (token->keyword == RID_OPERATOR)
3755 return cp_parser_operator_function_id (parser);
3756 /* Fall through. */
3758 default:
3759 cp_parser_error (parser, "expected id-expression");
3760 return error_mark_node;
3763 else
3764 return cp_parser_unqualified_id (parser, template_keyword_p,
3765 /*check_dependency_p=*/true,
3766 declarator_p,
3767 optional_p);
3770 /* Parse an unqualified-id.
3772 unqualified-id:
3773 identifier
3774 operator-function-id
3775 conversion-function-id
3776 ~ class-name
3777 template-id
3779 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3780 keyword, in a construct like `A::template ...'.
3782 Returns a representation of unqualified-id. For the `identifier'
3783 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3784 production a BIT_NOT_EXPR is returned; the operand of the
3785 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3786 other productions, see the documentation accompanying the
3787 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3788 names are looked up in uninstantiated templates. If DECLARATOR_P
3789 is true, the unqualified-id is appearing as part of a declarator,
3790 rather than as part of an expression. */
3792 static tree
3793 cp_parser_unqualified_id (cp_parser* parser,
3794 bool template_keyword_p,
3795 bool check_dependency_p,
3796 bool declarator_p,
3797 bool optional_p)
3799 cp_token *token;
3801 /* Peek at the next token. */
3802 token = cp_lexer_peek_token (parser->lexer);
3804 switch (token->type)
3806 case CPP_NAME:
3808 tree id;
3810 /* We don't know yet whether or not this will be a
3811 template-id. */
3812 cp_parser_parse_tentatively (parser);
3813 /* Try a template-id. */
3814 id = cp_parser_template_id (parser, template_keyword_p,
3815 check_dependency_p,
3816 declarator_p);
3817 /* If it worked, we're done. */
3818 if (cp_parser_parse_definitely (parser))
3819 return id;
3820 /* Otherwise, it's an ordinary identifier. */
3821 return cp_parser_identifier (parser);
3824 case CPP_TEMPLATE_ID:
3825 return cp_parser_template_id (parser, template_keyword_p,
3826 check_dependency_p,
3827 declarator_p);
3829 case CPP_COMPL:
3831 tree type_decl;
3832 tree qualifying_scope;
3833 tree object_scope;
3834 tree scope;
3835 bool done;
3837 /* Consume the `~' token. */
3838 cp_lexer_consume_token (parser->lexer);
3839 /* Parse the class-name. The standard, as written, seems to
3840 say that:
3842 template <typename T> struct S { ~S (); };
3843 template <typename T> S<T>::~S() {}
3845 is invalid, since `~' must be followed by a class-name, but
3846 `S<T>' is dependent, and so not known to be a class.
3847 That's not right; we need to look in uninstantiated
3848 templates. A further complication arises from:
3850 template <typename T> void f(T t) {
3851 t.T::~T();
3854 Here, it is not possible to look up `T' in the scope of `T'
3855 itself. We must look in both the current scope, and the
3856 scope of the containing complete expression.
3858 Yet another issue is:
3860 struct S {
3861 int S;
3862 ~S();
3865 S::~S() {}
3867 The standard does not seem to say that the `S' in `~S'
3868 should refer to the type `S' and not the data member
3869 `S::S'. */
3871 /* DR 244 says that we look up the name after the "~" in the
3872 same scope as we looked up the qualifying name. That idea
3873 isn't fully worked out; it's more complicated than that. */
3874 scope = parser->scope;
3875 object_scope = parser->object_scope;
3876 qualifying_scope = parser->qualifying_scope;
3878 /* Check for invalid scopes. */
3879 if (scope == error_mark_node)
3881 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3882 cp_lexer_consume_token (parser->lexer);
3883 return error_mark_node;
3885 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3887 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3888 error_at (token->location,
3889 "scope %qT before %<~%> is not a class-name",
3890 scope);
3891 cp_parser_simulate_error (parser);
3892 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3893 cp_lexer_consume_token (parser->lexer);
3894 return error_mark_node;
3896 gcc_assert (!scope || TYPE_P (scope));
3898 /* If the name is of the form "X::~X" it's OK. */
3899 token = cp_lexer_peek_token (parser->lexer);
3900 if (scope
3901 && token->type == CPP_NAME
3902 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3903 != CPP_LESS)
3904 && constructor_name_p (token->u.value, scope))
3906 cp_lexer_consume_token (parser->lexer);
3907 return build_nt (BIT_NOT_EXPR, scope);
3910 /* If there was an explicit qualification (S::~T), first look
3911 in the scope given by the qualification (i.e., S).
3913 Note: in the calls to cp_parser_class_name below we pass
3914 typename_type so that lookup finds the injected-class-name
3915 rather than the constructor. */
3916 done = false;
3917 type_decl = NULL_TREE;
3918 if (scope)
3920 cp_parser_parse_tentatively (parser);
3921 type_decl = cp_parser_class_name (parser,
3922 /*typename_keyword_p=*/false,
3923 /*template_keyword_p=*/false,
3924 typename_type,
3925 /*check_dependency=*/false,
3926 /*class_head_p=*/false,
3927 declarator_p);
3928 if (cp_parser_parse_definitely (parser))
3929 done = true;
3931 /* In "N::S::~S", look in "N" as well. */
3932 if (!done && scope && qualifying_scope)
3934 cp_parser_parse_tentatively (parser);
3935 parser->scope = qualifying_scope;
3936 parser->object_scope = NULL_TREE;
3937 parser->qualifying_scope = NULL_TREE;
3938 type_decl
3939 = cp_parser_class_name (parser,
3940 /*typename_keyword_p=*/false,
3941 /*template_keyword_p=*/false,
3942 typename_type,
3943 /*check_dependency=*/false,
3944 /*class_head_p=*/false,
3945 declarator_p);
3946 if (cp_parser_parse_definitely (parser))
3947 done = true;
3949 /* In "p->S::~T", look in the scope given by "*p" as well. */
3950 else if (!done && object_scope)
3952 cp_parser_parse_tentatively (parser);
3953 parser->scope = object_scope;
3954 parser->object_scope = NULL_TREE;
3955 parser->qualifying_scope = NULL_TREE;
3956 type_decl
3957 = cp_parser_class_name (parser,
3958 /*typename_keyword_p=*/false,
3959 /*template_keyword_p=*/false,
3960 typename_type,
3961 /*check_dependency=*/false,
3962 /*class_head_p=*/false,
3963 declarator_p);
3964 if (cp_parser_parse_definitely (parser))
3965 done = true;
3967 /* Look in the surrounding context. */
3968 if (!done)
3970 parser->scope = NULL_TREE;
3971 parser->object_scope = NULL_TREE;
3972 parser->qualifying_scope = NULL_TREE;
3973 if (processing_template_decl)
3974 cp_parser_parse_tentatively (parser);
3975 type_decl
3976 = cp_parser_class_name (parser,
3977 /*typename_keyword_p=*/false,
3978 /*template_keyword_p=*/false,
3979 typename_type,
3980 /*check_dependency=*/false,
3981 /*class_head_p=*/false,
3982 declarator_p);
3983 if (processing_template_decl
3984 && ! cp_parser_parse_definitely (parser))
3986 /* We couldn't find a type with this name, so just accept
3987 it and check for a match at instantiation time. */
3988 type_decl = cp_parser_identifier (parser);
3989 if (type_decl != error_mark_node)
3990 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
3991 return type_decl;
3994 /* If an error occurred, assume that the name of the
3995 destructor is the same as the name of the qualifying
3996 class. That allows us to keep parsing after running
3997 into ill-formed destructor names. */
3998 if (type_decl == error_mark_node && scope)
3999 return build_nt (BIT_NOT_EXPR, scope);
4000 else if (type_decl == error_mark_node)
4001 return error_mark_node;
4003 /* Check that destructor name and scope match. */
4004 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4006 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4007 error_at (token->location,
4008 "declaration of %<~%T%> as member of %qT",
4009 type_decl, scope);
4010 cp_parser_simulate_error (parser);
4011 return error_mark_node;
4014 /* [class.dtor]
4016 A typedef-name that names a class shall not be used as the
4017 identifier in the declarator for a destructor declaration. */
4018 if (declarator_p
4019 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4020 && !DECL_SELF_REFERENCE_P (type_decl)
4021 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4022 error_at (token->location,
4023 "typedef-name %qD used as destructor declarator",
4024 type_decl);
4026 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4029 case CPP_KEYWORD:
4030 if (token->keyword == RID_OPERATOR)
4032 tree id;
4034 /* This could be a template-id, so we try that first. */
4035 cp_parser_parse_tentatively (parser);
4036 /* Try a template-id. */
4037 id = cp_parser_template_id (parser, template_keyword_p,
4038 /*check_dependency_p=*/true,
4039 declarator_p);
4040 /* If that worked, we're done. */
4041 if (cp_parser_parse_definitely (parser))
4042 return id;
4043 /* We still don't know whether we're looking at an
4044 operator-function-id or a conversion-function-id. */
4045 cp_parser_parse_tentatively (parser);
4046 /* Try an operator-function-id. */
4047 id = cp_parser_operator_function_id (parser);
4048 /* If that didn't work, try a conversion-function-id. */
4049 if (!cp_parser_parse_definitely (parser))
4050 id = cp_parser_conversion_function_id (parser);
4052 return id;
4054 /* Fall through. */
4056 default:
4057 if (optional_p)
4058 return NULL_TREE;
4059 cp_parser_error (parser, "expected unqualified-id");
4060 return error_mark_node;
4064 /* Parse an (optional) nested-name-specifier.
4066 nested-name-specifier: [C++98]
4067 class-or-namespace-name :: nested-name-specifier [opt]
4068 class-or-namespace-name :: template nested-name-specifier [opt]
4070 nested-name-specifier: [C++0x]
4071 type-name ::
4072 namespace-name ::
4073 nested-name-specifier identifier ::
4074 nested-name-specifier template [opt] simple-template-id ::
4076 PARSER->SCOPE should be set appropriately before this function is
4077 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4078 effect. TYPE_P is TRUE if we non-type bindings should be ignored
4079 in name lookups.
4081 Sets PARSER->SCOPE to the class (TYPE) or namespace
4082 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4083 it unchanged if there is no nested-name-specifier. Returns the new
4084 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4086 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4087 part of a declaration and/or decl-specifier. */
4089 static tree
4090 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4091 bool typename_keyword_p,
4092 bool check_dependency_p,
4093 bool type_p,
4094 bool is_declaration)
4096 bool success = false;
4097 cp_token_position start = 0;
4098 cp_token *token;
4100 /* Remember where the nested-name-specifier starts. */
4101 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4103 start = cp_lexer_token_position (parser->lexer, false);
4104 push_deferring_access_checks (dk_deferred);
4107 while (true)
4109 tree new_scope;
4110 tree old_scope;
4111 tree saved_qualifying_scope;
4112 bool template_keyword_p;
4114 /* Spot cases that cannot be the beginning of a
4115 nested-name-specifier. */
4116 token = cp_lexer_peek_token (parser->lexer);
4118 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4119 the already parsed nested-name-specifier. */
4120 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4122 /* Grab the nested-name-specifier and continue the loop. */
4123 cp_parser_pre_parsed_nested_name_specifier (parser);
4124 /* If we originally encountered this nested-name-specifier
4125 with IS_DECLARATION set to false, we will not have
4126 resolved TYPENAME_TYPEs, so we must do so here. */
4127 if (is_declaration
4128 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4130 new_scope = resolve_typename_type (parser->scope,
4131 /*only_current_p=*/false);
4132 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4133 parser->scope = new_scope;
4135 success = true;
4136 continue;
4139 /* Spot cases that cannot be the beginning of a
4140 nested-name-specifier. On the second and subsequent times
4141 through the loop, we look for the `template' keyword. */
4142 if (success && token->keyword == RID_TEMPLATE)
4144 /* A template-id can start a nested-name-specifier. */
4145 else if (token->type == CPP_TEMPLATE_ID)
4147 else
4149 /* If the next token is not an identifier, then it is
4150 definitely not a type-name or namespace-name. */
4151 if (token->type != CPP_NAME)
4152 break;
4153 /* If the following token is neither a `<' (to begin a
4154 template-id), nor a `::', then we are not looking at a
4155 nested-name-specifier. */
4156 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4157 if (token->type != CPP_SCOPE
4158 && !cp_parser_nth_token_starts_template_argument_list_p
4159 (parser, 2))
4160 break;
4163 /* The nested-name-specifier is optional, so we parse
4164 tentatively. */
4165 cp_parser_parse_tentatively (parser);
4167 /* Look for the optional `template' keyword, if this isn't the
4168 first time through the loop. */
4169 if (success)
4170 template_keyword_p = cp_parser_optional_template_keyword (parser);
4171 else
4172 template_keyword_p = false;
4174 /* Save the old scope since the name lookup we are about to do
4175 might destroy it. */
4176 old_scope = parser->scope;
4177 saved_qualifying_scope = parser->qualifying_scope;
4178 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4179 look up names in "X<T>::I" in order to determine that "Y" is
4180 a template. So, if we have a typename at this point, we make
4181 an effort to look through it. */
4182 if (is_declaration
4183 && !typename_keyword_p
4184 && parser->scope
4185 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4186 parser->scope = resolve_typename_type (parser->scope,
4187 /*only_current_p=*/false);
4188 /* Parse the qualifying entity. */
4189 new_scope
4190 = cp_parser_qualifying_entity (parser,
4191 typename_keyword_p,
4192 template_keyword_p,
4193 check_dependency_p,
4194 type_p,
4195 is_declaration);
4196 /* Look for the `::' token. */
4197 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4199 /* If we found what we wanted, we keep going; otherwise, we're
4200 done. */
4201 if (!cp_parser_parse_definitely (parser))
4203 bool error_p = false;
4205 /* Restore the OLD_SCOPE since it was valid before the
4206 failed attempt at finding the last
4207 class-or-namespace-name. */
4208 parser->scope = old_scope;
4209 parser->qualifying_scope = saved_qualifying_scope;
4210 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4211 break;
4212 /* If the next token is an identifier, and the one after
4213 that is a `::', then any valid interpretation would have
4214 found a class-or-namespace-name. */
4215 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4216 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4217 == CPP_SCOPE)
4218 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4219 != CPP_COMPL))
4221 token = cp_lexer_consume_token (parser->lexer);
4222 if (!error_p)
4224 if (!token->ambiguous_p)
4226 tree decl;
4227 tree ambiguous_decls;
4229 decl = cp_parser_lookup_name (parser, token->u.value,
4230 none_type,
4231 /*is_template=*/false,
4232 /*is_namespace=*/false,
4233 /*check_dependency=*/true,
4234 &ambiguous_decls,
4235 token->location);
4236 if (TREE_CODE (decl) == TEMPLATE_DECL)
4237 error_at (token->location,
4238 "%qD used without template parameters",
4239 decl);
4240 else if (ambiguous_decls)
4242 error_at (token->location,
4243 "reference to %qD is ambiguous",
4244 token->u.value);
4245 print_candidates (ambiguous_decls);
4246 decl = error_mark_node;
4248 else
4250 const char* msg = "is not a class or namespace";
4251 if (cxx_dialect != cxx98)
4252 msg = "is not a class, namespace, or enumeration";
4253 cp_parser_name_lookup_error
4254 (parser, token->u.value, decl, msg,
4255 token->location);
4258 parser->scope = error_mark_node;
4259 error_p = true;
4260 /* Treat this as a successful nested-name-specifier
4261 due to:
4263 [basic.lookup.qual]
4265 If the name found is not a class-name (clause
4266 _class_) or namespace-name (_namespace.def_), the
4267 program is ill-formed. */
4268 success = true;
4270 cp_lexer_consume_token (parser->lexer);
4272 break;
4274 /* We've found one valid nested-name-specifier. */
4275 success = true;
4276 /* Name lookup always gives us a DECL. */
4277 if (TREE_CODE (new_scope) == TYPE_DECL)
4278 new_scope = TREE_TYPE (new_scope);
4279 /* Uses of "template" must be followed by actual templates. */
4280 if (template_keyword_p
4281 && !(CLASS_TYPE_P (new_scope)
4282 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4283 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4284 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4285 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4286 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4287 == TEMPLATE_ID_EXPR)))
4288 permerror (input_location, TYPE_P (new_scope)
4289 ? "%qT is not a template"
4290 : "%qD is not a template",
4291 new_scope);
4292 /* If it is a class scope, try to complete it; we are about to
4293 be looking up names inside the class. */
4294 if (TYPE_P (new_scope)
4295 /* Since checking types for dependency can be expensive,
4296 avoid doing it if the type is already complete. */
4297 && !COMPLETE_TYPE_P (new_scope)
4298 /* Do not try to complete dependent types. */
4299 && !dependent_type_p (new_scope))
4301 new_scope = complete_type (new_scope);
4302 /* If it is a typedef to current class, use the current
4303 class instead, as the typedef won't have any names inside
4304 it yet. */
4305 if (!COMPLETE_TYPE_P (new_scope)
4306 && currently_open_class (new_scope))
4307 new_scope = TYPE_MAIN_VARIANT (new_scope);
4309 /* Make sure we look in the right scope the next time through
4310 the loop. */
4311 parser->scope = new_scope;
4314 /* If parsing tentatively, replace the sequence of tokens that makes
4315 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4316 token. That way, should we re-parse the token stream, we will
4317 not have to repeat the effort required to do the parse, nor will
4318 we issue duplicate error messages. */
4319 if (success && start)
4321 cp_token *token;
4323 token = cp_lexer_token_at (parser->lexer, start);
4324 /* Reset the contents of the START token. */
4325 token->type = CPP_NESTED_NAME_SPECIFIER;
4326 /* Retrieve any deferred checks. Do not pop this access checks yet
4327 so the memory will not be reclaimed during token replacing below. */
4328 token->u.tree_check_value = GGC_CNEW (struct tree_check);
4329 token->u.tree_check_value->value = parser->scope;
4330 token->u.tree_check_value->checks = get_deferred_access_checks ();
4331 token->u.tree_check_value->qualifying_scope =
4332 parser->qualifying_scope;
4333 token->keyword = RID_MAX;
4335 /* Purge all subsequent tokens. */
4336 cp_lexer_purge_tokens_after (parser->lexer, start);
4339 if (start)
4340 pop_to_parent_deferring_access_checks ();
4342 return success ? parser->scope : NULL_TREE;
4345 /* Parse a nested-name-specifier. See
4346 cp_parser_nested_name_specifier_opt for details. This function
4347 behaves identically, except that it will an issue an error if no
4348 nested-name-specifier is present. */
4350 static tree
4351 cp_parser_nested_name_specifier (cp_parser *parser,
4352 bool typename_keyword_p,
4353 bool check_dependency_p,
4354 bool type_p,
4355 bool is_declaration)
4357 tree scope;
4359 /* Look for the nested-name-specifier. */
4360 scope = cp_parser_nested_name_specifier_opt (parser,
4361 typename_keyword_p,
4362 check_dependency_p,
4363 type_p,
4364 is_declaration);
4365 /* If it was not present, issue an error message. */
4366 if (!scope)
4368 cp_parser_error (parser, "expected nested-name-specifier");
4369 parser->scope = NULL_TREE;
4372 return scope;
4375 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4376 this is either a class-name or a namespace-name (which corresponds
4377 to the class-or-namespace-name production in the grammar). For
4378 C++0x, it can also be a type-name that refers to an enumeration
4379 type.
4381 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4382 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4383 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4384 TYPE_P is TRUE iff the next name should be taken as a class-name,
4385 even the same name is declared to be another entity in the same
4386 scope.
4388 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4389 specified by the class-or-namespace-name. If neither is found the
4390 ERROR_MARK_NODE is returned. */
4392 static tree
4393 cp_parser_qualifying_entity (cp_parser *parser,
4394 bool typename_keyword_p,
4395 bool template_keyword_p,
4396 bool check_dependency_p,
4397 bool type_p,
4398 bool is_declaration)
4400 tree saved_scope;
4401 tree saved_qualifying_scope;
4402 tree saved_object_scope;
4403 tree scope;
4404 bool only_class_p;
4405 bool successful_parse_p;
4407 /* Before we try to parse the class-name, we must save away the
4408 current PARSER->SCOPE since cp_parser_class_name will destroy
4409 it. */
4410 saved_scope = parser->scope;
4411 saved_qualifying_scope = parser->qualifying_scope;
4412 saved_object_scope = parser->object_scope;
4413 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4414 there is no need to look for a namespace-name. */
4415 only_class_p = template_keyword_p
4416 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4417 if (!only_class_p)
4418 cp_parser_parse_tentatively (parser);
4419 scope = cp_parser_class_name (parser,
4420 typename_keyword_p,
4421 template_keyword_p,
4422 type_p ? class_type : none_type,
4423 check_dependency_p,
4424 /*class_head_p=*/false,
4425 is_declaration);
4426 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4427 /* If that didn't work and we're in C++0x mode, try for a type-name. */
4428 if (!only_class_p
4429 && cxx_dialect != cxx98
4430 && !successful_parse_p)
4432 /* Restore the saved scope. */
4433 parser->scope = saved_scope;
4434 parser->qualifying_scope = saved_qualifying_scope;
4435 parser->object_scope = saved_object_scope;
4437 /* Parse tentatively. */
4438 cp_parser_parse_tentatively (parser);
4440 /* Parse a typedef-name or enum-name. */
4441 scope = cp_parser_nonclass_name (parser);
4442 successful_parse_p = cp_parser_parse_definitely (parser);
4444 /* If that didn't work, try for a namespace-name. */
4445 if (!only_class_p && !successful_parse_p)
4447 /* Restore the saved scope. */
4448 parser->scope = saved_scope;
4449 parser->qualifying_scope = saved_qualifying_scope;
4450 parser->object_scope = saved_object_scope;
4451 /* If we are not looking at an identifier followed by the scope
4452 resolution operator, then this is not part of a
4453 nested-name-specifier. (Note that this function is only used
4454 to parse the components of a nested-name-specifier.) */
4455 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4456 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4457 return error_mark_node;
4458 scope = cp_parser_namespace_name (parser);
4461 return scope;
4464 /* Parse a postfix-expression.
4466 postfix-expression:
4467 primary-expression
4468 postfix-expression [ expression ]
4469 postfix-expression ( expression-list [opt] )
4470 simple-type-specifier ( expression-list [opt] )
4471 typename :: [opt] nested-name-specifier identifier
4472 ( expression-list [opt] )
4473 typename :: [opt] nested-name-specifier template [opt] template-id
4474 ( expression-list [opt] )
4475 postfix-expression . template [opt] id-expression
4476 postfix-expression -> template [opt] id-expression
4477 postfix-expression . pseudo-destructor-name
4478 postfix-expression -> pseudo-destructor-name
4479 postfix-expression ++
4480 postfix-expression --
4481 dynamic_cast < type-id > ( expression )
4482 static_cast < type-id > ( expression )
4483 reinterpret_cast < type-id > ( expression )
4484 const_cast < type-id > ( expression )
4485 typeid ( expression )
4486 typeid ( type-id )
4488 GNU Extension:
4490 postfix-expression:
4491 ( type-id ) { initializer-list , [opt] }
4493 This extension is a GNU version of the C99 compound-literal
4494 construct. (The C99 grammar uses `type-name' instead of `type-id',
4495 but they are essentially the same concept.)
4497 If ADDRESS_P is true, the postfix expression is the operand of the
4498 `&' operator. CAST_P is true if this expression is the target of a
4499 cast.
4501 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4502 class member access expressions [expr.ref].
4504 Returns a representation of the expression. */
4506 static tree
4507 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4508 bool member_access_only_p,
4509 cp_id_kind * pidk_return)
4511 cp_token *token;
4512 enum rid keyword;
4513 cp_id_kind idk = CP_ID_KIND_NONE;
4514 tree postfix_expression = NULL_TREE;
4515 bool is_member_access = false;
4517 /* Peek at the next token. */
4518 token = cp_lexer_peek_token (parser->lexer);
4519 /* Some of the productions are determined by keywords. */
4520 keyword = token->keyword;
4521 switch (keyword)
4523 case RID_DYNCAST:
4524 case RID_STATCAST:
4525 case RID_REINTCAST:
4526 case RID_CONSTCAST:
4528 tree type;
4529 tree expression;
4530 const char *saved_message;
4532 /* All of these can be handled in the same way from the point
4533 of view of parsing. Begin by consuming the token
4534 identifying the cast. */
4535 cp_lexer_consume_token (parser->lexer);
4537 /* New types cannot be defined in the cast. */
4538 saved_message = parser->type_definition_forbidden_message;
4539 parser->type_definition_forbidden_message
4540 = "types may not be defined in casts";
4542 /* Look for the opening `<'. */
4543 cp_parser_require (parser, CPP_LESS, "%<<%>");
4544 /* Parse the type to which we are casting. */
4545 type = cp_parser_type_id (parser);
4546 /* Look for the closing `>'. */
4547 cp_parser_require (parser, CPP_GREATER, "%<>%>");
4548 /* Restore the old message. */
4549 parser->type_definition_forbidden_message = saved_message;
4551 /* And the expression which is being cast. */
4552 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4553 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4554 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4556 /* Only type conversions to integral or enumeration types
4557 can be used in constant-expressions. */
4558 if (!cast_valid_in_integral_constant_expression_p (type)
4559 && (cp_parser_non_integral_constant_expression
4560 (parser,
4561 "a cast to a type other than an integral or "
4562 "enumeration type")))
4563 return error_mark_node;
4565 switch (keyword)
4567 case RID_DYNCAST:
4568 postfix_expression
4569 = build_dynamic_cast (type, expression, tf_warning_or_error);
4570 break;
4571 case RID_STATCAST:
4572 postfix_expression
4573 = build_static_cast (type, expression, tf_warning_or_error);
4574 break;
4575 case RID_REINTCAST:
4576 postfix_expression
4577 = build_reinterpret_cast (type, expression,
4578 tf_warning_or_error);
4579 break;
4580 case RID_CONSTCAST:
4581 postfix_expression
4582 = build_const_cast (type, expression, tf_warning_or_error);
4583 break;
4584 default:
4585 gcc_unreachable ();
4588 break;
4590 case RID_TYPEID:
4592 tree type;
4593 const char *saved_message;
4594 bool saved_in_type_id_in_expr_p;
4596 /* Consume the `typeid' token. */
4597 cp_lexer_consume_token (parser->lexer);
4598 /* Look for the `(' token. */
4599 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4600 /* Types cannot be defined in a `typeid' expression. */
4601 saved_message = parser->type_definition_forbidden_message;
4602 parser->type_definition_forbidden_message
4603 = "types may not be defined in a %<typeid%> expression";
4604 /* We can't be sure yet whether we're looking at a type-id or an
4605 expression. */
4606 cp_parser_parse_tentatively (parser);
4607 /* Try a type-id first. */
4608 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4609 parser->in_type_id_in_expr_p = true;
4610 type = cp_parser_type_id (parser);
4611 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4612 /* Look for the `)' token. Otherwise, we can't be sure that
4613 we're not looking at an expression: consider `typeid (int
4614 (3))', for example. */
4615 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4616 /* If all went well, simply lookup the type-id. */
4617 if (cp_parser_parse_definitely (parser))
4618 postfix_expression = get_typeid (type);
4619 /* Otherwise, fall back to the expression variant. */
4620 else
4622 tree expression;
4624 /* Look for an expression. */
4625 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4626 /* Compute its typeid. */
4627 postfix_expression = build_typeid (expression);
4628 /* Look for the `)' token. */
4629 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4631 /* Restore the saved message. */
4632 parser->type_definition_forbidden_message = saved_message;
4633 /* `typeid' may not appear in an integral constant expression. */
4634 if (cp_parser_non_integral_constant_expression(parser,
4635 "%<typeid%> operator"))
4636 return error_mark_node;
4638 break;
4640 case RID_TYPENAME:
4642 tree type;
4643 /* The syntax permitted here is the same permitted for an
4644 elaborated-type-specifier. */
4645 type = cp_parser_elaborated_type_specifier (parser,
4646 /*is_friend=*/false,
4647 /*is_declaration=*/false);
4648 postfix_expression = cp_parser_functional_cast (parser, type);
4650 break;
4652 default:
4654 tree type;
4656 /* If the next thing is a simple-type-specifier, we may be
4657 looking at a functional cast. We could also be looking at
4658 an id-expression. So, we try the functional cast, and if
4659 that doesn't work we fall back to the primary-expression. */
4660 cp_parser_parse_tentatively (parser);
4661 /* Look for the simple-type-specifier. */
4662 type = cp_parser_simple_type_specifier (parser,
4663 /*decl_specs=*/NULL,
4664 CP_PARSER_FLAGS_NONE);
4665 /* Parse the cast itself. */
4666 if (!cp_parser_error_occurred (parser))
4667 postfix_expression
4668 = cp_parser_functional_cast (parser, type);
4669 /* If that worked, we're done. */
4670 if (cp_parser_parse_definitely (parser))
4671 break;
4673 /* If the functional-cast didn't work out, try a
4674 compound-literal. */
4675 if (cp_parser_allow_gnu_extensions_p (parser)
4676 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4678 VEC(constructor_elt,gc) *initializer_list = NULL;
4679 bool saved_in_type_id_in_expr_p;
4681 cp_parser_parse_tentatively (parser);
4682 /* Consume the `('. */
4683 cp_lexer_consume_token (parser->lexer);
4684 /* Parse the type. */
4685 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4686 parser->in_type_id_in_expr_p = true;
4687 type = cp_parser_type_id (parser);
4688 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4689 /* Look for the `)'. */
4690 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4691 /* Look for the `{'. */
4692 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4693 /* If things aren't going well, there's no need to
4694 keep going. */
4695 if (!cp_parser_error_occurred (parser))
4697 bool non_constant_p;
4698 /* Parse the initializer-list. */
4699 initializer_list
4700 = cp_parser_initializer_list (parser, &non_constant_p);
4701 /* Allow a trailing `,'. */
4702 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4703 cp_lexer_consume_token (parser->lexer);
4704 /* Look for the final `}'. */
4705 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4707 /* If that worked, we're definitely looking at a
4708 compound-literal expression. */
4709 if (cp_parser_parse_definitely (parser))
4711 /* Warn the user that a compound literal is not
4712 allowed in standard C++. */
4713 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4714 /* For simplicity, we disallow compound literals in
4715 constant-expressions. We could
4716 allow compound literals of integer type, whose
4717 initializer was a constant, in constant
4718 expressions. Permitting that usage, as a further
4719 extension, would not change the meaning of any
4720 currently accepted programs. (Of course, as
4721 compound literals are not part of ISO C++, the
4722 standard has nothing to say.) */
4723 if (cp_parser_non_integral_constant_expression
4724 (parser, "non-constant compound literals"))
4726 postfix_expression = error_mark_node;
4727 break;
4729 /* Form the representation of the compound-literal. */
4730 postfix_expression
4731 = (finish_compound_literal
4732 (type, build_constructor (init_list_type_node,
4733 initializer_list)));
4734 break;
4738 /* It must be a primary-expression. */
4739 postfix_expression
4740 = cp_parser_primary_expression (parser, address_p, cast_p,
4741 /*template_arg_p=*/false,
4742 &idk);
4744 break;
4747 /* Keep looping until the postfix-expression is complete. */
4748 while (true)
4750 if (idk == CP_ID_KIND_UNQUALIFIED
4751 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4752 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4753 /* It is not a Koenig lookup function call. */
4754 postfix_expression
4755 = unqualified_name_lookup_error (postfix_expression);
4757 /* Peek at the next token. */
4758 token = cp_lexer_peek_token (parser->lexer);
4760 switch (token->type)
4762 case CPP_OPEN_SQUARE:
4763 postfix_expression
4764 = cp_parser_postfix_open_square_expression (parser,
4765 postfix_expression,
4766 false);
4767 idk = CP_ID_KIND_NONE;
4768 is_member_access = false;
4769 break;
4771 case CPP_OPEN_PAREN:
4772 /* postfix-expression ( expression-list [opt] ) */
4774 bool koenig_p;
4775 bool is_builtin_constant_p;
4776 bool saved_integral_constant_expression_p = false;
4777 bool saved_non_integral_constant_expression_p = false;
4778 VEC(tree,gc) *args;
4780 is_member_access = false;
4782 is_builtin_constant_p
4783 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4784 if (is_builtin_constant_p)
4786 /* The whole point of __builtin_constant_p is to allow
4787 non-constant expressions to appear as arguments. */
4788 saved_integral_constant_expression_p
4789 = parser->integral_constant_expression_p;
4790 saved_non_integral_constant_expression_p
4791 = parser->non_integral_constant_expression_p;
4792 parser->integral_constant_expression_p = false;
4794 args = (cp_parser_parenthesized_expression_list
4795 (parser, /*is_attribute_list=*/false,
4796 /*cast_p=*/false, /*allow_expansion_p=*/true,
4797 /*non_constant_p=*/NULL));
4798 if (is_builtin_constant_p)
4800 parser->integral_constant_expression_p
4801 = saved_integral_constant_expression_p;
4802 parser->non_integral_constant_expression_p
4803 = saved_non_integral_constant_expression_p;
4806 if (args == NULL)
4808 postfix_expression = error_mark_node;
4809 break;
4812 /* Function calls are not permitted in
4813 constant-expressions. */
4814 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4815 && cp_parser_non_integral_constant_expression (parser,
4816 "a function call"))
4818 postfix_expression = error_mark_node;
4819 release_tree_vector (args);
4820 break;
4823 koenig_p = false;
4824 if (idk == CP_ID_KIND_UNQUALIFIED
4825 || idk == CP_ID_KIND_TEMPLATE_ID)
4827 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4829 if (!VEC_empty (tree, args))
4831 koenig_p = true;
4832 if (!any_type_dependent_arguments_p (args))
4833 postfix_expression
4834 = perform_koenig_lookup (postfix_expression, args);
4836 else
4837 postfix_expression
4838 = unqualified_fn_lookup_error (postfix_expression);
4840 /* We do not perform argument-dependent lookup if
4841 normal lookup finds a non-function, in accordance
4842 with the expected resolution of DR 218. */
4843 else if (!VEC_empty (tree, args)
4844 && is_overloaded_fn (postfix_expression))
4846 tree fn = get_first_fn (postfix_expression);
4847 fn = STRIP_TEMPLATE (fn);
4849 /* Do not do argument dependent lookup if regular
4850 lookup finds a member function or a block-scope
4851 function declaration. [basic.lookup.argdep]/3 */
4852 if (!DECL_FUNCTION_MEMBER_P (fn)
4853 && !DECL_LOCAL_FUNCTION_P (fn))
4855 koenig_p = true;
4856 if (!any_type_dependent_arguments_p (args))
4857 postfix_expression
4858 = perform_koenig_lookup (postfix_expression, args);
4863 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4865 tree instance = TREE_OPERAND (postfix_expression, 0);
4866 tree fn = TREE_OPERAND (postfix_expression, 1);
4868 if (processing_template_decl
4869 && (type_dependent_expression_p (instance)
4870 || (!BASELINK_P (fn)
4871 && TREE_CODE (fn) != FIELD_DECL)
4872 || type_dependent_expression_p (fn)
4873 || any_type_dependent_arguments_p (args)))
4875 postfix_expression
4876 = build_nt_call_vec (postfix_expression, args);
4877 release_tree_vector (args);
4878 break;
4881 if (BASELINK_P (fn))
4883 postfix_expression
4884 = (build_new_method_call
4885 (instance, fn, &args, NULL_TREE,
4886 (idk == CP_ID_KIND_QUALIFIED
4887 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4888 /*fn_p=*/NULL,
4889 tf_warning_or_error));
4891 else
4892 postfix_expression
4893 = finish_call_expr (postfix_expression, &args,
4894 /*disallow_virtual=*/false,
4895 /*koenig_p=*/false,
4896 tf_warning_or_error);
4898 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4899 || TREE_CODE (postfix_expression) == MEMBER_REF
4900 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4901 postfix_expression = (build_offset_ref_call_from_tree
4902 (postfix_expression, &args));
4903 else if (idk == CP_ID_KIND_QUALIFIED)
4904 /* A call to a static class member, or a namespace-scope
4905 function. */
4906 postfix_expression
4907 = finish_call_expr (postfix_expression, &args,
4908 /*disallow_virtual=*/true,
4909 koenig_p,
4910 tf_warning_or_error);
4911 else
4912 /* All other function calls. */
4913 postfix_expression
4914 = finish_call_expr (postfix_expression, &args,
4915 /*disallow_virtual=*/false,
4916 koenig_p,
4917 tf_warning_or_error);
4919 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4920 idk = CP_ID_KIND_NONE;
4922 release_tree_vector (args);
4924 break;
4926 case CPP_DOT:
4927 case CPP_DEREF:
4928 /* postfix-expression . template [opt] id-expression
4929 postfix-expression . pseudo-destructor-name
4930 postfix-expression -> template [opt] id-expression
4931 postfix-expression -> pseudo-destructor-name */
4933 /* Consume the `.' or `->' operator. */
4934 cp_lexer_consume_token (parser->lexer);
4936 postfix_expression
4937 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4938 postfix_expression,
4939 false, &idk,
4940 token->location);
4942 is_member_access = true;
4943 break;
4945 case CPP_PLUS_PLUS:
4946 /* postfix-expression ++ */
4947 /* Consume the `++' token. */
4948 cp_lexer_consume_token (parser->lexer);
4949 /* Generate a representation for the complete expression. */
4950 postfix_expression
4951 = finish_increment_expr (postfix_expression,
4952 POSTINCREMENT_EXPR);
4953 /* Increments may not appear in constant-expressions. */
4954 if (cp_parser_non_integral_constant_expression (parser,
4955 "an increment"))
4956 postfix_expression = error_mark_node;
4957 idk = CP_ID_KIND_NONE;
4958 is_member_access = false;
4959 break;
4961 case CPP_MINUS_MINUS:
4962 /* postfix-expression -- */
4963 /* Consume the `--' token. */
4964 cp_lexer_consume_token (parser->lexer);
4965 /* Generate a representation for the complete expression. */
4966 postfix_expression
4967 = finish_increment_expr (postfix_expression,
4968 POSTDECREMENT_EXPR);
4969 /* Decrements may not appear in constant-expressions. */
4970 if (cp_parser_non_integral_constant_expression (parser,
4971 "a decrement"))
4972 postfix_expression = error_mark_node;
4973 idk = CP_ID_KIND_NONE;
4974 is_member_access = false;
4975 break;
4977 default:
4978 if (pidk_return != NULL)
4979 * pidk_return = idk;
4980 if (member_access_only_p)
4981 return is_member_access? postfix_expression : error_mark_node;
4982 else
4983 return postfix_expression;
4987 /* We should never get here. */
4988 gcc_unreachable ();
4989 return error_mark_node;
4992 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4993 by cp_parser_builtin_offsetof. We're looking for
4995 postfix-expression [ expression ]
4997 FOR_OFFSETOF is set if we're being called in that context, which
4998 changes how we deal with integer constant expressions. */
5000 static tree
5001 cp_parser_postfix_open_square_expression (cp_parser *parser,
5002 tree postfix_expression,
5003 bool for_offsetof)
5005 tree index;
5007 /* Consume the `[' token. */
5008 cp_lexer_consume_token (parser->lexer);
5010 /* Parse the index expression. */
5011 /* ??? For offsetof, there is a question of what to allow here. If
5012 offsetof is not being used in an integral constant expression context,
5013 then we *could* get the right answer by computing the value at runtime.
5014 If we are in an integral constant expression context, then we might
5015 could accept any constant expression; hard to say without analysis.
5016 Rather than open the barn door too wide right away, allow only integer
5017 constant expressions here. */
5018 if (for_offsetof)
5019 index = cp_parser_constant_expression (parser, false, NULL);
5020 else
5021 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5023 /* Look for the closing `]'. */
5024 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5026 /* Build the ARRAY_REF. */
5027 postfix_expression = grok_array_decl (postfix_expression, index);
5029 /* When not doing offsetof, array references are not permitted in
5030 constant-expressions. */
5031 if (!for_offsetof
5032 && (cp_parser_non_integral_constant_expression
5033 (parser, "an array reference")))
5034 postfix_expression = error_mark_node;
5036 return postfix_expression;
5039 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5040 by cp_parser_builtin_offsetof. We're looking for
5042 postfix-expression . template [opt] id-expression
5043 postfix-expression . pseudo-destructor-name
5044 postfix-expression -> template [opt] id-expression
5045 postfix-expression -> pseudo-destructor-name
5047 FOR_OFFSETOF is set if we're being called in that context. That sorta
5048 limits what of the above we'll actually accept, but nevermind.
5049 TOKEN_TYPE is the "." or "->" token, which will already have been
5050 removed from the stream. */
5052 static tree
5053 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5054 enum cpp_ttype token_type,
5055 tree postfix_expression,
5056 bool for_offsetof, cp_id_kind *idk,
5057 location_t location)
5059 tree name;
5060 bool dependent_p;
5061 bool pseudo_destructor_p;
5062 tree scope = NULL_TREE;
5064 /* If this is a `->' operator, dereference the pointer. */
5065 if (token_type == CPP_DEREF)
5066 postfix_expression = build_x_arrow (postfix_expression);
5067 /* Check to see whether or not the expression is type-dependent. */
5068 dependent_p = type_dependent_expression_p (postfix_expression);
5069 /* The identifier following the `->' or `.' is not qualified. */
5070 parser->scope = NULL_TREE;
5071 parser->qualifying_scope = NULL_TREE;
5072 parser->object_scope = NULL_TREE;
5073 *idk = CP_ID_KIND_NONE;
5075 /* Enter the scope corresponding to the type of the object
5076 given by the POSTFIX_EXPRESSION. */
5077 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5079 scope = TREE_TYPE (postfix_expression);
5080 /* According to the standard, no expression should ever have
5081 reference type. Unfortunately, we do not currently match
5082 the standard in this respect in that our internal representation
5083 of an expression may have reference type even when the standard
5084 says it does not. Therefore, we have to manually obtain the
5085 underlying type here. */
5086 scope = non_reference (scope);
5087 /* The type of the POSTFIX_EXPRESSION must be complete. */
5088 if (scope == unknown_type_node)
5090 error_at (location, "%qE does not have class type",
5091 postfix_expression);
5092 scope = NULL_TREE;
5094 else
5095 scope = complete_type_or_else (scope, NULL_TREE);
5096 /* Let the name lookup machinery know that we are processing a
5097 class member access expression. */
5098 parser->context->object_type = scope;
5099 /* If something went wrong, we want to be able to discern that case,
5100 as opposed to the case where there was no SCOPE due to the type
5101 of expression being dependent. */
5102 if (!scope)
5103 scope = error_mark_node;
5104 /* If the SCOPE was erroneous, make the various semantic analysis
5105 functions exit quickly -- and without issuing additional error
5106 messages. */
5107 if (scope == error_mark_node)
5108 postfix_expression = error_mark_node;
5111 /* Assume this expression is not a pseudo-destructor access. */
5112 pseudo_destructor_p = false;
5114 /* If the SCOPE is a scalar type, then, if this is a valid program,
5115 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5116 is type dependent, it can be pseudo-destructor-name or something else.
5117 Try to parse it as pseudo-destructor-name first. */
5118 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5120 tree s;
5121 tree type;
5123 cp_parser_parse_tentatively (parser);
5124 /* Parse the pseudo-destructor-name. */
5125 s = NULL_TREE;
5126 cp_parser_pseudo_destructor_name (parser, &s, &type);
5127 if (dependent_p
5128 && (cp_parser_error_occurred (parser)
5129 || TREE_CODE (type) != TYPE_DECL
5130 || !SCALAR_TYPE_P (TREE_TYPE (type))))
5131 cp_parser_abort_tentative_parse (parser);
5132 else if (cp_parser_parse_definitely (parser))
5134 pseudo_destructor_p = true;
5135 postfix_expression
5136 = finish_pseudo_destructor_expr (postfix_expression,
5137 s, TREE_TYPE (type));
5141 if (!pseudo_destructor_p)
5143 /* If the SCOPE is not a scalar type, we are looking at an
5144 ordinary class member access expression, rather than a
5145 pseudo-destructor-name. */
5146 bool template_p;
5147 cp_token *token = cp_lexer_peek_token (parser->lexer);
5148 /* Parse the id-expression. */
5149 name = (cp_parser_id_expression
5150 (parser,
5151 cp_parser_optional_template_keyword (parser),
5152 /*check_dependency_p=*/true,
5153 &template_p,
5154 /*declarator_p=*/false,
5155 /*optional_p=*/false));
5156 /* In general, build a SCOPE_REF if the member name is qualified.
5157 However, if the name was not dependent and has already been
5158 resolved; there is no need to build the SCOPE_REF. For example;
5160 struct X { void f(); };
5161 template <typename T> void f(T* t) { t->X::f(); }
5163 Even though "t" is dependent, "X::f" is not and has been resolved
5164 to a BASELINK; there is no need to include scope information. */
5166 /* But we do need to remember that there was an explicit scope for
5167 virtual function calls. */
5168 if (parser->scope)
5169 *idk = CP_ID_KIND_QUALIFIED;
5171 /* If the name is a template-id that names a type, we will get a
5172 TYPE_DECL here. That is invalid code. */
5173 if (TREE_CODE (name) == TYPE_DECL)
5175 error_at (token->location, "invalid use of %qD", name);
5176 postfix_expression = error_mark_node;
5178 else
5180 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5182 name = build_qualified_name (/*type=*/NULL_TREE,
5183 parser->scope,
5184 name,
5185 template_p);
5186 parser->scope = NULL_TREE;
5187 parser->qualifying_scope = NULL_TREE;
5188 parser->object_scope = NULL_TREE;
5190 if (scope && name && BASELINK_P (name))
5191 adjust_result_of_qualified_name_lookup
5192 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5193 postfix_expression
5194 = finish_class_member_access_expr (postfix_expression, name,
5195 template_p,
5196 tf_warning_or_error);
5200 /* We no longer need to look up names in the scope of the object on
5201 the left-hand side of the `.' or `->' operator. */
5202 parser->context->object_type = NULL_TREE;
5204 /* Outside of offsetof, these operators may not appear in
5205 constant-expressions. */
5206 if (!for_offsetof
5207 && (cp_parser_non_integral_constant_expression
5208 (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5209 postfix_expression = error_mark_node;
5211 return postfix_expression;
5214 /* Parse a parenthesized expression-list.
5216 expression-list:
5217 assignment-expression
5218 expression-list, assignment-expression
5220 attribute-list:
5221 expression-list
5222 identifier
5223 identifier, expression-list
5225 CAST_P is true if this expression is the target of a cast.
5227 ALLOW_EXPANSION_P is true if this expression allows expansion of an
5228 argument pack.
5230 Returns a vector of trees. Each element is a representation of an
5231 assignment-expression. NULL is returned if the ( and or ) are
5232 missing. An empty, but allocated, vector is returned on no
5233 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is true
5234 if this is really an attribute list being parsed. If
5235 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5236 not all of the expressions in the list were constant. */
5238 static VEC(tree,gc) *
5239 cp_parser_parenthesized_expression_list (cp_parser* parser,
5240 bool is_attribute_list,
5241 bool cast_p,
5242 bool allow_expansion_p,
5243 bool *non_constant_p)
5245 VEC(tree,gc) *expression_list;
5246 bool fold_expr_p = is_attribute_list;
5247 tree identifier = NULL_TREE;
5248 bool saved_greater_than_is_operator_p;
5250 /* Assume all the expressions will be constant. */
5251 if (non_constant_p)
5252 *non_constant_p = false;
5254 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5255 return NULL;
5257 expression_list = make_tree_vector ();
5259 /* Within a parenthesized expression, a `>' token is always
5260 the greater-than operator. */
5261 saved_greater_than_is_operator_p
5262 = parser->greater_than_is_operator_p;
5263 parser->greater_than_is_operator_p = true;
5265 /* Consume expressions until there are no more. */
5266 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5267 while (true)
5269 tree expr;
5271 /* At the beginning of attribute lists, check to see if the
5272 next token is an identifier. */
5273 if (is_attribute_list
5274 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5276 cp_token *token;
5278 /* Consume the identifier. */
5279 token = cp_lexer_consume_token (parser->lexer);
5280 /* Save the identifier. */
5281 identifier = token->u.value;
5283 else
5285 bool expr_non_constant_p;
5287 /* Parse the next assignment-expression. */
5288 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5290 /* A braced-init-list. */
5291 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5292 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5293 if (non_constant_p && expr_non_constant_p)
5294 *non_constant_p = true;
5296 else if (non_constant_p)
5298 expr = (cp_parser_constant_expression
5299 (parser, /*allow_non_constant_p=*/true,
5300 &expr_non_constant_p));
5301 if (expr_non_constant_p)
5302 *non_constant_p = true;
5304 else
5305 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5307 if (fold_expr_p)
5308 expr = fold_non_dependent_expr (expr);
5310 /* If we have an ellipsis, then this is an expression
5311 expansion. */
5312 if (allow_expansion_p
5313 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5315 /* Consume the `...'. */
5316 cp_lexer_consume_token (parser->lexer);
5318 /* Build the argument pack. */
5319 expr = make_pack_expansion (expr);
5322 /* Add it to the list. We add error_mark_node
5323 expressions to the list, so that we can still tell if
5324 the correct form for a parenthesized expression-list
5325 is found. That gives better errors. */
5326 VEC_safe_push (tree, gc, expression_list, expr);
5328 if (expr == error_mark_node)
5329 goto skip_comma;
5332 /* After the first item, attribute lists look the same as
5333 expression lists. */
5334 is_attribute_list = false;
5336 get_comma:;
5337 /* If the next token isn't a `,', then we are done. */
5338 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5339 break;
5341 /* Otherwise, consume the `,' and keep going. */
5342 cp_lexer_consume_token (parser->lexer);
5345 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5347 int ending;
5349 skip_comma:;
5350 /* We try and resync to an unnested comma, as that will give the
5351 user better diagnostics. */
5352 ending = cp_parser_skip_to_closing_parenthesis (parser,
5353 /*recovering=*/true,
5354 /*or_comma=*/true,
5355 /*consume_paren=*/true);
5356 if (ending < 0)
5357 goto get_comma;
5358 if (!ending)
5360 parser->greater_than_is_operator_p
5361 = saved_greater_than_is_operator_p;
5362 return NULL;
5366 parser->greater_than_is_operator_p
5367 = saved_greater_than_is_operator_p;
5369 if (identifier)
5370 VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5372 return expression_list;
5375 /* Parse a pseudo-destructor-name.
5377 pseudo-destructor-name:
5378 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5379 :: [opt] nested-name-specifier template template-id :: ~ type-name
5380 :: [opt] nested-name-specifier [opt] ~ type-name
5382 If either of the first two productions is used, sets *SCOPE to the
5383 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5384 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5385 or ERROR_MARK_NODE if the parse fails. */
5387 static void
5388 cp_parser_pseudo_destructor_name (cp_parser* parser,
5389 tree* scope,
5390 tree* type)
5392 bool nested_name_specifier_p;
5394 /* Assume that things will not work out. */
5395 *type = error_mark_node;
5397 /* Look for the optional `::' operator. */
5398 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5399 /* Look for the optional nested-name-specifier. */
5400 nested_name_specifier_p
5401 = (cp_parser_nested_name_specifier_opt (parser,
5402 /*typename_keyword_p=*/false,
5403 /*check_dependency_p=*/true,
5404 /*type_p=*/false,
5405 /*is_declaration=*/false)
5406 != NULL_TREE);
5407 /* Now, if we saw a nested-name-specifier, we might be doing the
5408 second production. */
5409 if (nested_name_specifier_p
5410 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5412 /* Consume the `template' keyword. */
5413 cp_lexer_consume_token (parser->lexer);
5414 /* Parse the template-id. */
5415 cp_parser_template_id (parser,
5416 /*template_keyword_p=*/true,
5417 /*check_dependency_p=*/false,
5418 /*is_declaration=*/true);
5419 /* Look for the `::' token. */
5420 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5422 /* If the next token is not a `~', then there might be some
5423 additional qualification. */
5424 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5426 /* At this point, we're looking for "type-name :: ~". The type-name
5427 must not be a class-name, since this is a pseudo-destructor. So,
5428 it must be either an enum-name, or a typedef-name -- both of which
5429 are just identifiers. So, we peek ahead to check that the "::"
5430 and "~" tokens are present; if they are not, then we can avoid
5431 calling type_name. */
5432 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5433 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5434 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5436 cp_parser_error (parser, "non-scalar type");
5437 return;
5440 /* Look for the type-name. */
5441 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5442 if (*scope == error_mark_node)
5443 return;
5445 /* Look for the `::' token. */
5446 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5448 else
5449 *scope = NULL_TREE;
5451 /* Look for the `~'. */
5452 cp_parser_require (parser, CPP_COMPL, "%<~%>");
5453 /* Look for the type-name again. We are not responsible for
5454 checking that it matches the first type-name. */
5455 *type = cp_parser_nonclass_name (parser);
5458 /* Parse a unary-expression.
5460 unary-expression:
5461 postfix-expression
5462 ++ cast-expression
5463 -- cast-expression
5464 unary-operator cast-expression
5465 sizeof unary-expression
5466 sizeof ( type-id )
5467 new-expression
5468 delete-expression
5470 GNU Extensions:
5472 unary-expression:
5473 __extension__ cast-expression
5474 __alignof__ unary-expression
5475 __alignof__ ( type-id )
5476 __real__ cast-expression
5477 __imag__ cast-expression
5478 && identifier
5480 ADDRESS_P is true iff the unary-expression is appearing as the
5481 operand of the `&' operator. CAST_P is true if this expression is
5482 the target of a cast.
5484 Returns a representation of the expression. */
5486 static tree
5487 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5488 cp_id_kind * pidk)
5490 cp_token *token;
5491 enum tree_code unary_operator;
5493 /* Peek at the next token. */
5494 token = cp_lexer_peek_token (parser->lexer);
5495 /* Some keywords give away the kind of expression. */
5496 if (token->type == CPP_KEYWORD)
5498 enum rid keyword = token->keyword;
5500 switch (keyword)
5502 case RID_ALIGNOF:
5503 case RID_SIZEOF:
5505 tree operand;
5506 enum tree_code op;
5508 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5509 /* Consume the token. */
5510 cp_lexer_consume_token (parser->lexer);
5511 /* Parse the operand. */
5512 operand = cp_parser_sizeof_operand (parser, keyword);
5514 if (TYPE_P (operand))
5515 return cxx_sizeof_or_alignof_type (operand, op, true);
5516 else
5517 return cxx_sizeof_or_alignof_expr (operand, op, true);
5520 case RID_NEW:
5521 return cp_parser_new_expression (parser);
5523 case RID_DELETE:
5524 return cp_parser_delete_expression (parser);
5526 case RID_EXTENSION:
5528 /* The saved value of the PEDANTIC flag. */
5529 int saved_pedantic;
5530 tree expr;
5532 /* Save away the PEDANTIC flag. */
5533 cp_parser_extension_opt (parser, &saved_pedantic);
5534 /* Parse the cast-expression. */
5535 expr = cp_parser_simple_cast_expression (parser);
5536 /* Restore the PEDANTIC flag. */
5537 pedantic = saved_pedantic;
5539 return expr;
5542 case RID_REALPART:
5543 case RID_IMAGPART:
5545 tree expression;
5547 /* Consume the `__real__' or `__imag__' token. */
5548 cp_lexer_consume_token (parser->lexer);
5549 /* Parse the cast-expression. */
5550 expression = cp_parser_simple_cast_expression (parser);
5551 /* Create the complete representation. */
5552 return build_x_unary_op ((keyword == RID_REALPART
5553 ? REALPART_EXPR : IMAGPART_EXPR),
5554 expression,
5555 tf_warning_or_error);
5557 break;
5559 default:
5560 break;
5564 /* Look for the `:: new' and `:: delete', which also signal the
5565 beginning of a new-expression, or delete-expression,
5566 respectively. If the next token is `::', then it might be one of
5567 these. */
5568 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5570 enum rid keyword;
5572 /* See if the token after the `::' is one of the keywords in
5573 which we're interested. */
5574 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5575 /* If it's `new', we have a new-expression. */
5576 if (keyword == RID_NEW)
5577 return cp_parser_new_expression (parser);
5578 /* Similarly, for `delete'. */
5579 else if (keyword == RID_DELETE)
5580 return cp_parser_delete_expression (parser);
5583 /* Look for a unary operator. */
5584 unary_operator = cp_parser_unary_operator (token);
5585 /* The `++' and `--' operators can be handled similarly, even though
5586 they are not technically unary-operators in the grammar. */
5587 if (unary_operator == ERROR_MARK)
5589 if (token->type == CPP_PLUS_PLUS)
5590 unary_operator = PREINCREMENT_EXPR;
5591 else if (token->type == CPP_MINUS_MINUS)
5592 unary_operator = PREDECREMENT_EXPR;
5593 /* Handle the GNU address-of-label extension. */
5594 else if (cp_parser_allow_gnu_extensions_p (parser)
5595 && token->type == CPP_AND_AND)
5597 tree identifier;
5598 tree expression;
5599 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5601 /* Consume the '&&' token. */
5602 cp_lexer_consume_token (parser->lexer);
5603 /* Look for the identifier. */
5604 identifier = cp_parser_identifier (parser);
5605 /* Create an expression representing the address. */
5606 expression = finish_label_address_expr (identifier, loc);
5607 if (cp_parser_non_integral_constant_expression (parser,
5608 "the address of a label"))
5609 expression = error_mark_node;
5610 return expression;
5613 if (unary_operator != ERROR_MARK)
5615 tree cast_expression;
5616 tree expression = error_mark_node;
5617 const char *non_constant_p = NULL;
5619 /* Consume the operator token. */
5620 token = cp_lexer_consume_token (parser->lexer);
5621 /* Parse the cast-expression. */
5622 cast_expression
5623 = cp_parser_cast_expression (parser,
5624 unary_operator == ADDR_EXPR,
5625 /*cast_p=*/false, pidk);
5626 /* Now, build an appropriate representation. */
5627 switch (unary_operator)
5629 case INDIRECT_REF:
5630 non_constant_p = "%<*%>";
5631 expression = build_x_indirect_ref (cast_expression, "unary *",
5632 tf_warning_or_error);
5633 break;
5635 case ADDR_EXPR:
5636 non_constant_p = "%<&%>";
5637 /* Fall through. */
5638 case BIT_NOT_EXPR:
5639 expression = build_x_unary_op (unary_operator, cast_expression,
5640 tf_warning_or_error);
5641 break;
5643 case PREINCREMENT_EXPR:
5644 case PREDECREMENT_EXPR:
5645 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5646 ? "%<++%>" : "%<--%>");
5647 /* Fall through. */
5648 case UNARY_PLUS_EXPR:
5649 case NEGATE_EXPR:
5650 case TRUTH_NOT_EXPR:
5651 expression = finish_unary_op_expr (unary_operator, cast_expression);
5652 break;
5654 default:
5655 gcc_unreachable ();
5658 if (non_constant_p
5659 && cp_parser_non_integral_constant_expression (parser,
5660 non_constant_p))
5661 expression = error_mark_node;
5663 return expression;
5666 return cp_parser_postfix_expression (parser, address_p, cast_p,
5667 /*member_access_only_p=*/false,
5668 pidk);
5671 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5672 unary-operator, the corresponding tree code is returned. */
5674 static enum tree_code
5675 cp_parser_unary_operator (cp_token* token)
5677 switch (token->type)
5679 case CPP_MULT:
5680 return INDIRECT_REF;
5682 case CPP_AND:
5683 return ADDR_EXPR;
5685 case CPP_PLUS:
5686 return UNARY_PLUS_EXPR;
5688 case CPP_MINUS:
5689 return NEGATE_EXPR;
5691 case CPP_NOT:
5692 return TRUTH_NOT_EXPR;
5694 case CPP_COMPL:
5695 return BIT_NOT_EXPR;
5697 default:
5698 return ERROR_MARK;
5702 /* Parse a new-expression.
5704 new-expression:
5705 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5706 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5708 Returns a representation of the expression. */
5710 static tree
5711 cp_parser_new_expression (cp_parser* parser)
5713 bool global_scope_p;
5714 VEC(tree,gc) *placement;
5715 tree type;
5716 VEC(tree,gc) *initializer;
5717 tree nelts;
5718 tree ret;
5720 /* Look for the optional `::' operator. */
5721 global_scope_p
5722 = (cp_parser_global_scope_opt (parser,
5723 /*current_scope_valid_p=*/false)
5724 != NULL_TREE);
5725 /* Look for the `new' operator. */
5726 cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5727 /* There's no easy way to tell a new-placement from the
5728 `( type-id )' construct. */
5729 cp_parser_parse_tentatively (parser);
5730 /* Look for a new-placement. */
5731 placement = cp_parser_new_placement (parser);
5732 /* If that didn't work out, there's no new-placement. */
5733 if (!cp_parser_parse_definitely (parser))
5735 if (placement != NULL)
5736 release_tree_vector (placement);
5737 placement = NULL;
5740 /* If the next token is a `(', then we have a parenthesized
5741 type-id. */
5742 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5744 cp_token *token;
5745 /* Consume the `('. */
5746 cp_lexer_consume_token (parser->lexer);
5747 /* Parse the type-id. */
5748 type = cp_parser_type_id (parser);
5749 /* Look for the closing `)'. */
5750 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5751 token = cp_lexer_peek_token (parser->lexer);
5752 /* There should not be a direct-new-declarator in this production,
5753 but GCC used to allowed this, so we check and emit a sensible error
5754 message for this case. */
5755 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5757 error_at (token->location,
5758 "array bound forbidden after parenthesized type-id");
5759 inform (token->location,
5760 "try removing the parentheses around the type-id");
5761 cp_parser_direct_new_declarator (parser);
5763 nelts = NULL_TREE;
5765 /* Otherwise, there must be a new-type-id. */
5766 else
5767 type = cp_parser_new_type_id (parser, &nelts);
5769 /* If the next token is a `(' or '{', then we have a new-initializer. */
5770 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5771 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5772 initializer = cp_parser_new_initializer (parser);
5773 else
5774 initializer = NULL;
5776 /* A new-expression may not appear in an integral constant
5777 expression. */
5778 if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5779 ret = error_mark_node;
5780 else
5782 /* Create a representation of the new-expression. */
5783 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
5784 tf_warning_or_error);
5787 if (placement != NULL)
5788 release_tree_vector (placement);
5789 if (initializer != NULL)
5790 release_tree_vector (initializer);
5792 return ret;
5795 /* Parse a new-placement.
5797 new-placement:
5798 ( expression-list )
5800 Returns the same representation as for an expression-list. */
5802 static VEC(tree,gc) *
5803 cp_parser_new_placement (cp_parser* parser)
5805 VEC(tree,gc) *expression_list;
5807 /* Parse the expression-list. */
5808 expression_list = (cp_parser_parenthesized_expression_list
5809 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5810 /*non_constant_p=*/NULL));
5812 return expression_list;
5815 /* Parse a new-type-id.
5817 new-type-id:
5818 type-specifier-seq new-declarator [opt]
5820 Returns the TYPE allocated. If the new-type-id indicates an array
5821 type, *NELTS is set to the number of elements in the last array
5822 bound; the TYPE will not include the last array bound. */
5824 static tree
5825 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5827 cp_decl_specifier_seq type_specifier_seq;
5828 cp_declarator *new_declarator;
5829 cp_declarator *declarator;
5830 cp_declarator *outer_declarator;
5831 const char *saved_message;
5832 tree type;
5834 /* The type-specifier sequence must not contain type definitions.
5835 (It cannot contain declarations of new types either, but if they
5836 are not definitions we will catch that because they are not
5837 complete.) */
5838 saved_message = parser->type_definition_forbidden_message;
5839 parser->type_definition_forbidden_message
5840 = "types may not be defined in a new-type-id";
5841 /* Parse the type-specifier-seq. */
5842 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
5843 /*is_trailing_return=*/false,
5844 &type_specifier_seq);
5845 /* Restore the old message. */
5846 parser->type_definition_forbidden_message = saved_message;
5847 /* Parse the new-declarator. */
5848 new_declarator = cp_parser_new_declarator_opt (parser);
5850 /* Determine the number of elements in the last array dimension, if
5851 any. */
5852 *nelts = NULL_TREE;
5853 /* Skip down to the last array dimension. */
5854 declarator = new_declarator;
5855 outer_declarator = NULL;
5856 while (declarator && (declarator->kind == cdk_pointer
5857 || declarator->kind == cdk_ptrmem))
5859 outer_declarator = declarator;
5860 declarator = declarator->declarator;
5862 while (declarator
5863 && declarator->kind == cdk_array
5864 && declarator->declarator
5865 && declarator->declarator->kind == cdk_array)
5867 outer_declarator = declarator;
5868 declarator = declarator->declarator;
5871 if (declarator && declarator->kind == cdk_array)
5873 *nelts = declarator->u.array.bounds;
5874 if (*nelts == error_mark_node)
5875 *nelts = integer_one_node;
5877 if (outer_declarator)
5878 outer_declarator->declarator = declarator->declarator;
5879 else
5880 new_declarator = NULL;
5883 type = groktypename (&type_specifier_seq, new_declarator, false);
5884 return type;
5887 /* Parse an (optional) new-declarator.
5889 new-declarator:
5890 ptr-operator new-declarator [opt]
5891 direct-new-declarator
5893 Returns the declarator. */
5895 static cp_declarator *
5896 cp_parser_new_declarator_opt (cp_parser* parser)
5898 enum tree_code code;
5899 tree type;
5900 cp_cv_quals cv_quals;
5902 /* We don't know if there's a ptr-operator next, or not. */
5903 cp_parser_parse_tentatively (parser);
5904 /* Look for a ptr-operator. */
5905 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5906 /* If that worked, look for more new-declarators. */
5907 if (cp_parser_parse_definitely (parser))
5909 cp_declarator *declarator;
5911 /* Parse another optional declarator. */
5912 declarator = cp_parser_new_declarator_opt (parser);
5914 return cp_parser_make_indirect_declarator
5915 (code, type, cv_quals, declarator);
5918 /* If the next token is a `[', there is a direct-new-declarator. */
5919 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5920 return cp_parser_direct_new_declarator (parser);
5922 return NULL;
5925 /* Parse a direct-new-declarator.
5927 direct-new-declarator:
5928 [ expression ]
5929 direct-new-declarator [constant-expression]
5933 static cp_declarator *
5934 cp_parser_direct_new_declarator (cp_parser* parser)
5936 cp_declarator *declarator = NULL;
5938 while (true)
5940 tree expression;
5942 /* Look for the opening `['. */
5943 cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5944 /* The first expression is not required to be constant. */
5945 if (!declarator)
5947 cp_token *token = cp_lexer_peek_token (parser->lexer);
5948 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5949 /* The standard requires that the expression have integral
5950 type. DR 74 adds enumeration types. We believe that the
5951 real intent is that these expressions be handled like the
5952 expression in a `switch' condition, which also allows
5953 classes with a single conversion to integral or
5954 enumeration type. */
5955 if (!processing_template_decl)
5957 expression
5958 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5959 expression,
5960 /*complain=*/true);
5961 if (!expression)
5963 error_at (token->location,
5964 "expression in new-declarator must have integral "
5965 "or enumeration type");
5966 expression = error_mark_node;
5970 /* But all the other expressions must be. */
5971 else
5972 expression
5973 = cp_parser_constant_expression (parser,
5974 /*allow_non_constant=*/false,
5975 NULL);
5976 /* Look for the closing `]'. */
5977 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5979 /* Add this bound to the declarator. */
5980 declarator = make_array_declarator (declarator, expression);
5982 /* If the next token is not a `[', then there are no more
5983 bounds. */
5984 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5985 break;
5988 return declarator;
5991 /* Parse a new-initializer.
5993 new-initializer:
5994 ( expression-list [opt] )
5995 braced-init-list
5997 Returns a representation of the expression-list. */
5999 static VEC(tree,gc) *
6000 cp_parser_new_initializer (cp_parser* parser)
6002 VEC(tree,gc) *expression_list;
6004 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6006 tree t;
6007 bool expr_non_constant_p;
6008 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6009 t = cp_parser_braced_list (parser, &expr_non_constant_p);
6010 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6011 expression_list = make_tree_vector_single (t);
6013 else
6014 expression_list = (cp_parser_parenthesized_expression_list
6015 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
6016 /*non_constant_p=*/NULL));
6018 return expression_list;
6021 /* Parse a delete-expression.
6023 delete-expression:
6024 :: [opt] delete cast-expression
6025 :: [opt] delete [ ] cast-expression
6027 Returns a representation of the expression. */
6029 static tree
6030 cp_parser_delete_expression (cp_parser* parser)
6032 bool global_scope_p;
6033 bool array_p;
6034 tree expression;
6036 /* Look for the optional `::' operator. */
6037 global_scope_p
6038 = (cp_parser_global_scope_opt (parser,
6039 /*current_scope_valid_p=*/false)
6040 != NULL_TREE);
6041 /* Look for the `delete' keyword. */
6042 cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
6043 /* See if the array syntax is in use. */
6044 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6046 /* Consume the `[' token. */
6047 cp_lexer_consume_token (parser->lexer);
6048 /* Look for the `]' token. */
6049 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
6050 /* Remember that this is the `[]' construct. */
6051 array_p = true;
6053 else
6054 array_p = false;
6056 /* Parse the cast-expression. */
6057 expression = cp_parser_simple_cast_expression (parser);
6059 /* A delete-expression may not appear in an integral constant
6060 expression. */
6061 if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
6062 return error_mark_node;
6064 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
6067 /* Returns true if TOKEN may start a cast-expression and false
6068 otherwise. */
6070 static bool
6071 cp_parser_token_starts_cast_expression (cp_token *token)
6073 switch (token->type)
6075 case CPP_COMMA:
6076 case CPP_SEMICOLON:
6077 case CPP_QUERY:
6078 case CPP_COLON:
6079 case CPP_CLOSE_SQUARE:
6080 case CPP_CLOSE_PAREN:
6081 case CPP_CLOSE_BRACE:
6082 case CPP_DOT:
6083 case CPP_DOT_STAR:
6084 case CPP_DEREF:
6085 case CPP_DEREF_STAR:
6086 case CPP_DIV:
6087 case CPP_MOD:
6088 case CPP_LSHIFT:
6089 case CPP_RSHIFT:
6090 case CPP_LESS:
6091 case CPP_GREATER:
6092 case CPP_LESS_EQ:
6093 case CPP_GREATER_EQ:
6094 case CPP_EQ_EQ:
6095 case CPP_NOT_EQ:
6096 case CPP_EQ:
6097 case CPP_MULT_EQ:
6098 case CPP_DIV_EQ:
6099 case CPP_MOD_EQ:
6100 case CPP_PLUS_EQ:
6101 case CPP_MINUS_EQ:
6102 case CPP_RSHIFT_EQ:
6103 case CPP_LSHIFT_EQ:
6104 case CPP_AND_EQ:
6105 case CPP_XOR_EQ:
6106 case CPP_OR_EQ:
6107 case CPP_XOR:
6108 case CPP_OR:
6109 case CPP_OR_OR:
6110 case CPP_EOF:
6111 return false;
6113 /* '[' may start a primary-expression in obj-c++. */
6114 case CPP_OPEN_SQUARE:
6115 return c_dialect_objc ();
6117 default:
6118 return true;
6122 /* Parse a cast-expression.
6124 cast-expression:
6125 unary-expression
6126 ( type-id ) cast-expression
6128 ADDRESS_P is true iff the unary-expression is appearing as the
6129 operand of the `&' operator. CAST_P is true if this expression is
6130 the target of a cast.
6132 Returns a representation of the expression. */
6134 static tree
6135 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6136 cp_id_kind * pidk)
6138 /* If it's a `(', then we might be looking at a cast. */
6139 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6141 tree type = NULL_TREE;
6142 tree expr = NULL_TREE;
6143 bool compound_literal_p;
6144 const char *saved_message;
6146 /* There's no way to know yet whether or not this is a cast.
6147 For example, `(int (3))' is a unary-expression, while `(int)
6148 3' is a cast. So, we resort to parsing tentatively. */
6149 cp_parser_parse_tentatively (parser);
6150 /* Types may not be defined in a cast. */
6151 saved_message = parser->type_definition_forbidden_message;
6152 parser->type_definition_forbidden_message
6153 = "types may not be defined in casts";
6154 /* Consume the `('. */
6155 cp_lexer_consume_token (parser->lexer);
6156 /* A very tricky bit is that `(struct S) { 3 }' is a
6157 compound-literal (which we permit in C++ as an extension).
6158 But, that construct is not a cast-expression -- it is a
6159 postfix-expression. (The reason is that `(struct S) { 3 }.i'
6160 is legal; if the compound-literal were a cast-expression,
6161 you'd need an extra set of parentheses.) But, if we parse
6162 the type-id, and it happens to be a class-specifier, then we
6163 will commit to the parse at that point, because we cannot
6164 undo the action that is done when creating a new class. So,
6165 then we cannot back up and do a postfix-expression.
6167 Therefore, we scan ahead to the closing `)', and check to see
6168 if the token after the `)' is a `{'. If so, we are not
6169 looking at a cast-expression.
6171 Save tokens so that we can put them back. */
6172 cp_lexer_save_tokens (parser->lexer);
6173 /* Skip tokens until the next token is a closing parenthesis.
6174 If we find the closing `)', and the next token is a `{', then
6175 we are looking at a compound-literal. */
6176 compound_literal_p
6177 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6178 /*consume_paren=*/true)
6179 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6180 /* Roll back the tokens we skipped. */
6181 cp_lexer_rollback_tokens (parser->lexer);
6182 /* If we were looking at a compound-literal, simulate an error
6183 so that the call to cp_parser_parse_definitely below will
6184 fail. */
6185 if (compound_literal_p)
6186 cp_parser_simulate_error (parser);
6187 else
6189 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6190 parser->in_type_id_in_expr_p = true;
6191 /* Look for the type-id. */
6192 type = cp_parser_type_id (parser);
6193 /* Look for the closing `)'. */
6194 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6195 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6198 /* Restore the saved message. */
6199 parser->type_definition_forbidden_message = saved_message;
6201 /* At this point this can only be either a cast or a
6202 parenthesized ctor such as `(T ())' that looks like a cast to
6203 function returning T. */
6204 if (!cp_parser_error_occurred (parser)
6205 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6206 (parser->lexer)))
6208 cp_parser_parse_definitely (parser);
6209 expr = cp_parser_cast_expression (parser,
6210 /*address_p=*/false,
6211 /*cast_p=*/true, pidk);
6213 /* Warn about old-style casts, if so requested. */
6214 if (warn_old_style_cast
6215 && !in_system_header
6216 && !VOID_TYPE_P (type)
6217 && current_lang_name != lang_name_c)
6218 warning (OPT_Wold_style_cast, "use of old-style cast");
6220 /* Only type conversions to integral or enumeration types
6221 can be used in constant-expressions. */
6222 if (!cast_valid_in_integral_constant_expression_p (type)
6223 && (cp_parser_non_integral_constant_expression
6224 (parser,
6225 "a cast to a type other than an integral or "
6226 "enumeration type")))
6227 return error_mark_node;
6229 /* Perform the cast. */
6230 expr = build_c_cast (input_location, type, expr);
6231 return expr;
6233 else
6234 cp_parser_abort_tentative_parse (parser);
6237 /* If we get here, then it's not a cast, so it must be a
6238 unary-expression. */
6239 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6242 /* Parse a binary expression of the general form:
6244 pm-expression:
6245 cast-expression
6246 pm-expression .* cast-expression
6247 pm-expression ->* cast-expression
6249 multiplicative-expression:
6250 pm-expression
6251 multiplicative-expression * pm-expression
6252 multiplicative-expression / pm-expression
6253 multiplicative-expression % pm-expression
6255 additive-expression:
6256 multiplicative-expression
6257 additive-expression + multiplicative-expression
6258 additive-expression - multiplicative-expression
6260 shift-expression:
6261 additive-expression
6262 shift-expression << additive-expression
6263 shift-expression >> additive-expression
6265 relational-expression:
6266 shift-expression
6267 relational-expression < shift-expression
6268 relational-expression > shift-expression
6269 relational-expression <= shift-expression
6270 relational-expression >= shift-expression
6272 GNU Extension:
6274 relational-expression:
6275 relational-expression <? shift-expression
6276 relational-expression >? shift-expression
6278 equality-expression:
6279 relational-expression
6280 equality-expression == relational-expression
6281 equality-expression != relational-expression
6283 and-expression:
6284 equality-expression
6285 and-expression & equality-expression
6287 exclusive-or-expression:
6288 and-expression
6289 exclusive-or-expression ^ and-expression
6291 inclusive-or-expression:
6292 exclusive-or-expression
6293 inclusive-or-expression | exclusive-or-expression
6295 logical-and-expression:
6296 inclusive-or-expression
6297 logical-and-expression && inclusive-or-expression
6299 logical-or-expression:
6300 logical-and-expression
6301 logical-or-expression || logical-and-expression
6303 All these are implemented with a single function like:
6305 binary-expression:
6306 simple-cast-expression
6307 binary-expression <token> binary-expression
6309 CAST_P is true if this expression is the target of a cast.
6311 The binops_by_token map is used to get the tree codes for each <token> type.
6312 binary-expressions are associated according to a precedence table. */
6314 #define TOKEN_PRECEDENCE(token) \
6315 (((token->type == CPP_GREATER \
6316 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6317 && !parser->greater_than_is_operator_p) \
6318 ? PREC_NOT_OPERATOR \
6319 : binops_by_token[token->type].prec)
6321 static tree
6322 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6323 bool no_toplevel_fold_p,
6324 enum cp_parser_prec prec,
6325 cp_id_kind * pidk)
6327 cp_parser_expression_stack stack;
6328 cp_parser_expression_stack_entry *sp = &stack[0];
6329 tree lhs, rhs;
6330 cp_token *token;
6331 enum tree_code tree_type, lhs_type, rhs_type;
6332 enum cp_parser_prec new_prec, lookahead_prec;
6333 bool overloaded_p;
6335 /* Parse the first expression. */
6336 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6337 lhs_type = ERROR_MARK;
6339 for (;;)
6341 /* Get an operator token. */
6342 token = cp_lexer_peek_token (parser->lexer);
6344 if (warn_cxx0x_compat
6345 && token->type == CPP_RSHIFT
6346 && !parser->greater_than_is_operator_p)
6348 if (warning_at (token->location, OPT_Wc__0x_compat,
6349 "%<>>%> operator will be treated as"
6350 " two right angle brackets in C++0x"))
6351 inform (token->location,
6352 "suggest parentheses around %<>>%> expression");
6355 new_prec = TOKEN_PRECEDENCE (token);
6357 /* Popping an entry off the stack means we completed a subexpression:
6358 - either we found a token which is not an operator (`>' where it is not
6359 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6360 will happen repeatedly;
6361 - or, we found an operator which has lower priority. This is the case
6362 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6363 parsing `3 * 4'. */
6364 if (new_prec <= prec)
6366 if (sp == stack)
6367 break;
6368 else
6369 goto pop;
6372 get_rhs:
6373 tree_type = binops_by_token[token->type].tree_type;
6375 /* We used the operator token. */
6376 cp_lexer_consume_token (parser->lexer);
6378 /* For "false && x" or "true || x", x will never be executed;
6379 disable warnings while evaluating it. */
6380 if (tree_type == TRUTH_ANDIF_EXPR)
6381 c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6382 else if (tree_type == TRUTH_ORIF_EXPR)
6383 c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6385 /* Extract another operand. It may be the RHS of this expression
6386 or the LHS of a new, higher priority expression. */
6387 rhs = cp_parser_simple_cast_expression (parser);
6388 rhs_type = ERROR_MARK;
6390 /* Get another operator token. Look up its precedence to avoid
6391 building a useless (immediately popped) stack entry for common
6392 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6393 token = cp_lexer_peek_token (parser->lexer);
6394 lookahead_prec = TOKEN_PRECEDENCE (token);
6395 if (lookahead_prec > new_prec)
6397 /* ... and prepare to parse the RHS of the new, higher priority
6398 expression. Since precedence levels on the stack are
6399 monotonically increasing, we do not have to care about
6400 stack overflows. */
6401 sp->prec = prec;
6402 sp->tree_type = tree_type;
6403 sp->lhs = lhs;
6404 sp->lhs_type = lhs_type;
6405 sp++;
6406 lhs = rhs;
6407 lhs_type = rhs_type;
6408 prec = new_prec;
6409 new_prec = lookahead_prec;
6410 goto get_rhs;
6412 pop:
6413 lookahead_prec = new_prec;
6414 /* If the stack is not empty, we have parsed into LHS the right side
6415 (`4' in the example above) of an expression we had suspended.
6416 We can use the information on the stack to recover the LHS (`3')
6417 from the stack together with the tree code (`MULT_EXPR'), and
6418 the precedence of the higher level subexpression
6419 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6420 which will be used to actually build the additive expression. */
6421 --sp;
6422 prec = sp->prec;
6423 tree_type = sp->tree_type;
6424 rhs = lhs;
6425 rhs_type = lhs_type;
6426 lhs = sp->lhs;
6427 lhs_type = sp->lhs_type;
6430 /* Undo the disabling of warnings done above. */
6431 if (tree_type == TRUTH_ANDIF_EXPR)
6432 c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6433 else if (tree_type == TRUTH_ORIF_EXPR)
6434 c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6436 overloaded_p = false;
6437 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6438 ERROR_MARK for everything that is not a binary expression.
6439 This makes warn_about_parentheses miss some warnings that
6440 involve unary operators. For unary expressions we should
6441 pass the correct tree_code unless the unary expression was
6442 surrounded by parentheses.
6444 if (no_toplevel_fold_p
6445 && lookahead_prec <= prec
6446 && sp == stack
6447 && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6448 lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6449 else
6450 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6451 &overloaded_p, tf_warning_or_error);
6452 lhs_type = tree_type;
6454 /* If the binary operator required the use of an overloaded operator,
6455 then this expression cannot be an integral constant-expression.
6456 An overloaded operator can be used even if both operands are
6457 otherwise permissible in an integral constant-expression if at
6458 least one of the operands is of enumeration type. */
6460 if (overloaded_p
6461 && (cp_parser_non_integral_constant_expression
6462 (parser, "calls to overloaded operators")))
6463 return error_mark_node;
6466 return lhs;
6470 /* Parse the `? expression : assignment-expression' part of a
6471 conditional-expression. The LOGICAL_OR_EXPR is the
6472 logical-or-expression that started the conditional-expression.
6473 Returns a representation of the entire conditional-expression.
6475 This routine is used by cp_parser_assignment_expression.
6477 ? expression : assignment-expression
6479 GNU Extensions:
6481 ? : assignment-expression */
6483 static tree
6484 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6486 tree expr;
6487 tree assignment_expr;
6489 /* Consume the `?' token. */
6490 cp_lexer_consume_token (parser->lexer);
6491 if (cp_parser_allow_gnu_extensions_p (parser)
6492 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6494 /* Implicit true clause. */
6495 expr = NULL_TREE;
6496 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6498 else
6500 /* Parse the expression. */
6501 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6502 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6503 c_inhibit_evaluation_warnings +=
6504 ((logical_or_expr == truthvalue_true_node)
6505 - (logical_or_expr == truthvalue_false_node));
6508 /* The next token should be a `:'. */
6509 cp_parser_require (parser, CPP_COLON, "%<:%>");
6510 /* Parse the assignment-expression. */
6511 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6512 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6514 /* Build the conditional-expression. */
6515 return build_x_conditional_expr (logical_or_expr,
6516 expr,
6517 assignment_expr,
6518 tf_warning_or_error);
6521 /* Parse an assignment-expression.
6523 assignment-expression:
6524 conditional-expression
6525 logical-or-expression assignment-operator assignment_expression
6526 throw-expression
6528 CAST_P is true if this expression is the target of a cast.
6530 Returns a representation for the expression. */
6532 static tree
6533 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6534 cp_id_kind * pidk)
6536 tree expr;
6538 /* If the next token is the `throw' keyword, then we're looking at
6539 a throw-expression. */
6540 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6541 expr = cp_parser_throw_expression (parser);
6542 /* Otherwise, it must be that we are looking at a
6543 logical-or-expression. */
6544 else
6546 /* Parse the binary expressions (logical-or-expression). */
6547 expr = cp_parser_binary_expression (parser, cast_p, false,
6548 PREC_NOT_OPERATOR, pidk);
6549 /* If the next token is a `?' then we're actually looking at a
6550 conditional-expression. */
6551 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6552 return cp_parser_question_colon_clause (parser, expr);
6553 else
6555 enum tree_code assignment_operator;
6557 /* If it's an assignment-operator, we're using the second
6558 production. */
6559 assignment_operator
6560 = cp_parser_assignment_operator_opt (parser);
6561 if (assignment_operator != ERROR_MARK)
6563 bool non_constant_p;
6565 /* Parse the right-hand side of the assignment. */
6566 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6568 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6569 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6571 /* An assignment may not appear in a
6572 constant-expression. */
6573 if (cp_parser_non_integral_constant_expression (parser,
6574 "an assignment"))
6575 return error_mark_node;
6576 /* Build the assignment expression. */
6577 expr = build_x_modify_expr (expr,
6578 assignment_operator,
6579 rhs,
6580 tf_warning_or_error);
6585 return expr;
6588 /* Parse an (optional) assignment-operator.
6590 assignment-operator: one of
6591 = *= /= %= += -= >>= <<= &= ^= |=
6593 GNU Extension:
6595 assignment-operator: one of
6596 <?= >?=
6598 If the next token is an assignment operator, the corresponding tree
6599 code is returned, and the token is consumed. For example, for
6600 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6601 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6602 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6603 operator, ERROR_MARK is returned. */
6605 static enum tree_code
6606 cp_parser_assignment_operator_opt (cp_parser* parser)
6608 enum tree_code op;
6609 cp_token *token;
6611 /* Peek at the next token. */
6612 token = cp_lexer_peek_token (parser->lexer);
6614 switch (token->type)
6616 case CPP_EQ:
6617 op = NOP_EXPR;
6618 break;
6620 case CPP_MULT_EQ:
6621 op = MULT_EXPR;
6622 break;
6624 case CPP_DIV_EQ:
6625 op = TRUNC_DIV_EXPR;
6626 break;
6628 case CPP_MOD_EQ:
6629 op = TRUNC_MOD_EXPR;
6630 break;
6632 case CPP_PLUS_EQ:
6633 op = PLUS_EXPR;
6634 break;
6636 case CPP_MINUS_EQ:
6637 op = MINUS_EXPR;
6638 break;
6640 case CPP_RSHIFT_EQ:
6641 op = RSHIFT_EXPR;
6642 break;
6644 case CPP_LSHIFT_EQ:
6645 op = LSHIFT_EXPR;
6646 break;
6648 case CPP_AND_EQ:
6649 op = BIT_AND_EXPR;
6650 break;
6652 case CPP_XOR_EQ:
6653 op = BIT_XOR_EXPR;
6654 break;
6656 case CPP_OR_EQ:
6657 op = BIT_IOR_EXPR;
6658 break;
6660 default:
6661 /* Nothing else is an assignment operator. */
6662 op = ERROR_MARK;
6665 /* If it was an assignment operator, consume it. */
6666 if (op != ERROR_MARK)
6667 cp_lexer_consume_token (parser->lexer);
6669 return op;
6672 /* Parse an expression.
6674 expression:
6675 assignment-expression
6676 expression , assignment-expression
6678 CAST_P is true if this expression is the target of a cast.
6680 Returns a representation of the expression. */
6682 static tree
6683 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6685 tree expression = NULL_TREE;
6687 while (true)
6689 tree assignment_expression;
6691 /* Parse the next assignment-expression. */
6692 assignment_expression
6693 = cp_parser_assignment_expression (parser, cast_p, pidk);
6694 /* If this is the first assignment-expression, we can just
6695 save it away. */
6696 if (!expression)
6697 expression = assignment_expression;
6698 else
6699 expression = build_x_compound_expr (expression,
6700 assignment_expression,
6701 tf_warning_or_error);
6702 /* If the next token is not a comma, then we are done with the
6703 expression. */
6704 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6705 break;
6706 /* Consume the `,'. */
6707 cp_lexer_consume_token (parser->lexer);
6708 /* A comma operator cannot appear in a constant-expression. */
6709 if (cp_parser_non_integral_constant_expression (parser,
6710 "a comma operator"))
6711 expression = error_mark_node;
6714 return expression;
6717 /* Parse a constant-expression.
6719 constant-expression:
6720 conditional-expression
6722 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6723 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6724 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6725 is false, NON_CONSTANT_P should be NULL. */
6727 static tree
6728 cp_parser_constant_expression (cp_parser* parser,
6729 bool allow_non_constant_p,
6730 bool *non_constant_p)
6732 bool saved_integral_constant_expression_p;
6733 bool saved_allow_non_integral_constant_expression_p;
6734 bool saved_non_integral_constant_expression_p;
6735 tree expression;
6737 /* It might seem that we could simply parse the
6738 conditional-expression, and then check to see if it were
6739 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6740 one that the compiler can figure out is constant, possibly after
6741 doing some simplifications or optimizations. The standard has a
6742 precise definition of constant-expression, and we must honor
6743 that, even though it is somewhat more restrictive.
6745 For example:
6747 int i[(2, 3)];
6749 is not a legal declaration, because `(2, 3)' is not a
6750 constant-expression. The `,' operator is forbidden in a
6751 constant-expression. However, GCC's constant-folding machinery
6752 will fold this operation to an INTEGER_CST for `3'. */
6754 /* Save the old settings. */
6755 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6756 saved_allow_non_integral_constant_expression_p
6757 = parser->allow_non_integral_constant_expression_p;
6758 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6759 /* We are now parsing a constant-expression. */
6760 parser->integral_constant_expression_p = true;
6761 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6762 parser->non_integral_constant_expression_p = false;
6763 /* Although the grammar says "conditional-expression", we parse an
6764 "assignment-expression", which also permits "throw-expression"
6765 and the use of assignment operators. In the case that
6766 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6767 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6768 actually essential that we look for an assignment-expression.
6769 For example, cp_parser_initializer_clauses uses this function to
6770 determine whether a particular assignment-expression is in fact
6771 constant. */
6772 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6773 /* Restore the old settings. */
6774 parser->integral_constant_expression_p
6775 = saved_integral_constant_expression_p;
6776 parser->allow_non_integral_constant_expression_p
6777 = saved_allow_non_integral_constant_expression_p;
6778 if (allow_non_constant_p)
6779 *non_constant_p = parser->non_integral_constant_expression_p;
6780 else if (parser->non_integral_constant_expression_p)
6781 expression = error_mark_node;
6782 parser->non_integral_constant_expression_p
6783 = saved_non_integral_constant_expression_p;
6785 return expression;
6788 /* Parse __builtin_offsetof.
6790 offsetof-expression:
6791 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6793 offsetof-member-designator:
6794 id-expression
6795 | offsetof-member-designator "." id-expression
6796 | offsetof-member-designator "[" expression "]"
6797 | offsetof-member-designator "->" id-expression */
6799 static tree
6800 cp_parser_builtin_offsetof (cp_parser *parser)
6802 int save_ice_p, save_non_ice_p;
6803 tree type, expr;
6804 cp_id_kind dummy;
6805 cp_token *token;
6807 /* We're about to accept non-integral-constant things, but will
6808 definitely yield an integral constant expression. Save and
6809 restore these values around our local parsing. */
6810 save_ice_p = parser->integral_constant_expression_p;
6811 save_non_ice_p = parser->non_integral_constant_expression_p;
6813 /* Consume the "__builtin_offsetof" token. */
6814 cp_lexer_consume_token (parser->lexer);
6815 /* Consume the opening `('. */
6816 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6817 /* Parse the type-id. */
6818 type = cp_parser_type_id (parser);
6819 /* Look for the `,'. */
6820 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6821 token = cp_lexer_peek_token (parser->lexer);
6823 /* Build the (type *)null that begins the traditional offsetof macro. */
6824 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6825 tf_warning_or_error);
6827 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6828 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6829 true, &dummy, token->location);
6830 while (true)
6832 token = cp_lexer_peek_token (parser->lexer);
6833 switch (token->type)
6835 case CPP_OPEN_SQUARE:
6836 /* offsetof-member-designator "[" expression "]" */
6837 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6838 break;
6840 case CPP_DEREF:
6841 /* offsetof-member-designator "->" identifier */
6842 expr = grok_array_decl (expr, integer_zero_node);
6843 /* FALLTHRU */
6845 case CPP_DOT:
6846 /* offsetof-member-designator "." identifier */
6847 cp_lexer_consume_token (parser->lexer);
6848 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
6849 expr, true, &dummy,
6850 token->location);
6851 break;
6853 case CPP_CLOSE_PAREN:
6854 /* Consume the ")" token. */
6855 cp_lexer_consume_token (parser->lexer);
6856 goto success;
6858 default:
6859 /* Error. We know the following require will fail, but
6860 that gives the proper error message. */
6861 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6862 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6863 expr = error_mark_node;
6864 goto failure;
6868 success:
6869 /* If we're processing a template, we can't finish the semantics yet.
6870 Otherwise we can fold the entire expression now. */
6871 if (processing_template_decl)
6872 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6873 else
6874 expr = finish_offsetof (expr);
6876 failure:
6877 parser->integral_constant_expression_p = save_ice_p;
6878 parser->non_integral_constant_expression_p = save_non_ice_p;
6880 return expr;
6883 /* Parse a trait expression. */
6885 static tree
6886 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6888 cp_trait_kind kind;
6889 tree type1, type2 = NULL_TREE;
6890 bool binary = false;
6891 cp_decl_specifier_seq decl_specs;
6893 switch (keyword)
6895 case RID_HAS_NOTHROW_ASSIGN:
6896 kind = CPTK_HAS_NOTHROW_ASSIGN;
6897 break;
6898 case RID_HAS_NOTHROW_CONSTRUCTOR:
6899 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6900 break;
6901 case RID_HAS_NOTHROW_COPY:
6902 kind = CPTK_HAS_NOTHROW_COPY;
6903 break;
6904 case RID_HAS_TRIVIAL_ASSIGN:
6905 kind = CPTK_HAS_TRIVIAL_ASSIGN;
6906 break;
6907 case RID_HAS_TRIVIAL_CONSTRUCTOR:
6908 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6909 break;
6910 case RID_HAS_TRIVIAL_COPY:
6911 kind = CPTK_HAS_TRIVIAL_COPY;
6912 break;
6913 case RID_HAS_TRIVIAL_DESTRUCTOR:
6914 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6915 break;
6916 case RID_HAS_VIRTUAL_DESTRUCTOR:
6917 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6918 break;
6919 case RID_IS_ABSTRACT:
6920 kind = CPTK_IS_ABSTRACT;
6921 break;
6922 case RID_IS_BASE_OF:
6923 kind = CPTK_IS_BASE_OF;
6924 binary = true;
6925 break;
6926 case RID_IS_CLASS:
6927 kind = CPTK_IS_CLASS;
6928 break;
6929 case RID_IS_CONVERTIBLE_TO:
6930 kind = CPTK_IS_CONVERTIBLE_TO;
6931 binary = true;
6932 break;
6933 case RID_IS_EMPTY:
6934 kind = CPTK_IS_EMPTY;
6935 break;
6936 case RID_IS_ENUM:
6937 kind = CPTK_IS_ENUM;
6938 break;
6939 case RID_IS_POD:
6940 kind = CPTK_IS_POD;
6941 break;
6942 case RID_IS_POLYMORPHIC:
6943 kind = CPTK_IS_POLYMORPHIC;
6944 break;
6945 case RID_IS_STD_LAYOUT:
6946 kind = CPTK_IS_STD_LAYOUT;
6947 break;
6948 case RID_IS_TRIVIAL:
6949 kind = CPTK_IS_TRIVIAL;
6950 break;
6951 case RID_IS_UNION:
6952 kind = CPTK_IS_UNION;
6953 break;
6954 default:
6955 gcc_unreachable ();
6958 /* Consume the token. */
6959 cp_lexer_consume_token (parser->lexer);
6961 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6963 type1 = cp_parser_type_id (parser);
6965 if (type1 == error_mark_node)
6966 return error_mark_node;
6968 /* Build a trivial decl-specifier-seq. */
6969 clear_decl_specs (&decl_specs);
6970 decl_specs.type = type1;
6972 /* Call grokdeclarator to figure out what type this is. */
6973 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6974 /*initialized=*/0, /*attrlist=*/NULL);
6976 if (binary)
6978 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6980 type2 = cp_parser_type_id (parser);
6982 if (type2 == error_mark_node)
6983 return error_mark_node;
6985 /* Build a trivial decl-specifier-seq. */
6986 clear_decl_specs (&decl_specs);
6987 decl_specs.type = type2;
6989 /* Call grokdeclarator to figure out what type this is. */
6990 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6991 /*initialized=*/0, /*attrlist=*/NULL);
6994 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6996 /* Complete the trait expression, which may mean either processing
6997 the trait expr now or saving it for template instantiation. */
6998 return finish_trait_expr (kind, type1, type2);
7001 /* Lambdas that appear in variable initializer or default argument scope
7002 get that in their mangling, so we need to record it. We might as well
7003 use the count for function and namespace scopes as well. */
7004 static GTY(()) tree lambda_scope;
7005 static GTY(()) int lambda_count;
7006 typedef struct GTY(()) tree_int
7008 tree t;
7009 int i;
7010 } tree_int;
7011 DEF_VEC_O(tree_int);
7012 DEF_VEC_ALLOC_O(tree_int,gc);
7013 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7015 static void
7016 start_lambda_scope (tree decl)
7018 tree_int ti;
7019 gcc_assert (decl);
7020 /* Once we're inside a function, we ignore other scopes and just push
7021 the function again so that popping works properly. */
7022 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7023 decl = current_function_decl;
7024 ti.t = lambda_scope;
7025 ti.i = lambda_count;
7026 VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7027 if (lambda_scope != decl)
7029 /* Don't reset the count if we're still in the same function. */
7030 lambda_scope = decl;
7031 lambda_count = 0;
7035 static void
7036 record_lambda_scope (tree lambda)
7038 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7039 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7042 static void
7043 finish_lambda_scope (void)
7045 tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7046 if (lambda_scope != p->t)
7048 lambda_scope = p->t;
7049 lambda_count = p->i;
7051 VEC_pop (tree_int, lambda_scope_stack);
7054 /* Parse a lambda expression.
7056 lambda-expression:
7057 lambda-introducer lambda-declarator [opt] compound-statement
7059 Returns a representation of the expression. */
7061 static tree
7062 cp_parser_lambda_expression (cp_parser* parser)
7064 tree lambda_expr = build_lambda_expr ();
7065 tree type;
7067 LAMBDA_EXPR_LOCATION (lambda_expr)
7068 = cp_lexer_peek_token (parser->lexer)->location;
7070 /* We may be in the middle of deferred access check. Disable
7071 it now. */
7072 push_deferring_access_checks (dk_no_deferred);
7074 type = begin_lambda_type (lambda_expr);
7076 record_lambda_scope (lambda_expr);
7078 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
7079 determine_visibility (TYPE_NAME (type));
7082 /* Inside the class, surrounding template-parameter-lists do not apply. */
7083 unsigned int saved_num_template_parameter_lists
7084 = parser->num_template_parameter_lists;
7086 parser->num_template_parameter_lists = 0;
7088 cp_parser_lambda_introducer (parser, lambda_expr);
7090 /* By virtue of defining a local class, a lambda expression has access to
7091 the private variables of enclosing classes. */
7093 cp_parser_lambda_declarator_opt (parser, lambda_expr);
7095 cp_parser_lambda_body (parser, lambda_expr);
7097 /* The capture list was built up in reverse order; fix that now. */
7099 tree newlist = NULL_TREE;
7100 tree elt, next;
7102 for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7103 elt; elt = next)
7105 tree field = TREE_PURPOSE (elt);
7106 char *buf;
7108 next = TREE_CHAIN (elt);
7109 TREE_CHAIN (elt) = newlist;
7110 newlist = elt;
7112 /* Also add __ to the beginning of the field name so that code
7113 outside the lambda body can't see the captured name. We could
7114 just remove the name entirely, but this is more useful for
7115 debugging. */
7116 if (field == LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
7117 /* The 'this' capture already starts with __. */
7118 continue;
7120 buf = (char *) alloca (IDENTIFIER_LENGTH (DECL_NAME (field)) + 3);
7121 buf[1] = buf[0] = '_';
7122 memcpy (buf + 2, IDENTIFIER_POINTER (DECL_NAME (field)),
7123 IDENTIFIER_LENGTH (DECL_NAME (field)) + 1);
7124 DECL_NAME (field) = get_identifier (buf);
7126 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7129 maybe_add_lambda_conv_op (type);
7131 type = finish_struct (type, /*attributes=*/NULL_TREE);
7133 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7136 pop_deferring_access_checks ();
7138 return build_lambda_object (lambda_expr);
7141 /* Parse the beginning of a lambda expression.
7143 lambda-introducer:
7144 [ lambda-capture [opt] ]
7146 LAMBDA_EXPR is the current representation of the lambda expression. */
7148 static void
7149 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7151 /* Need commas after the first capture. */
7152 bool first = true;
7154 /* Eat the leading `['. */
7155 cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
7157 /* Record default capture mode. "[&" "[=" "[&," "[=," */
7158 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7159 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7160 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7161 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7162 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7164 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7166 cp_lexer_consume_token (parser->lexer);
7167 first = false;
7170 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7172 cp_token* capture_token;
7173 tree capture_id;
7174 tree capture_init_expr;
7175 cp_id_kind idk = CP_ID_KIND_NONE;
7176 bool explicit_init_p = false;
7178 enum capture_kind_type
7180 BY_COPY,
7181 BY_REFERENCE
7183 enum capture_kind_type capture_kind = BY_COPY;
7185 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7187 error ("expected end of capture-list");
7188 return;
7191 if (first)
7192 first = false;
7193 else
7194 cp_parser_require (parser, CPP_COMMA, "%<,%>");
7196 /* Possibly capture `this'. */
7197 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7199 cp_lexer_consume_token (parser->lexer);
7200 add_capture (lambda_expr,
7201 /*id=*/get_identifier ("__this"),
7202 /*initializer=*/finish_this_expr(),
7203 /*by_reference_p=*/false,
7204 explicit_init_p);
7205 continue;
7208 /* Remember whether we want to capture as a reference or not. */
7209 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7211 capture_kind = BY_REFERENCE;
7212 cp_lexer_consume_token (parser->lexer);
7215 /* Get the identifier. */
7216 capture_token = cp_lexer_peek_token (parser->lexer);
7217 capture_id = cp_parser_identifier (parser);
7219 if (capture_id == error_mark_node)
7220 /* Would be nice to have a cp_parser_skip_to_closing_x for general
7221 delimiters, but I modified this to stop on unnested ']' as well. It
7222 was already changed to stop on unnested '}', so the
7223 "closing_parenthesis" name is no more misleading with my change. */
7225 cp_parser_skip_to_closing_parenthesis (parser,
7226 /*recovering=*/true,
7227 /*or_comma=*/true,
7228 /*consume_paren=*/true);
7229 break;
7232 /* Find the initializer for this capture. */
7233 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7235 /* An explicit expression exists. */
7236 cp_lexer_consume_token (parser->lexer);
7237 pedwarn (input_location, OPT_pedantic,
7238 "ISO C++ does not allow initializers "
7239 "in lambda expression capture lists");
7240 capture_init_expr = cp_parser_assignment_expression (parser,
7241 /*cast_p=*/true,
7242 &idk);
7243 explicit_init_p = true;
7245 else
7247 const char* error_msg;
7249 /* Turn the identifier into an id-expression. */
7250 capture_init_expr
7251 = cp_parser_lookup_name
7252 (parser,
7253 capture_id,
7254 none_type,
7255 /*is_template=*/false,
7256 /*is_namespace=*/false,
7257 /*check_dependency=*/true,
7258 /*ambiguous_decls=*/NULL,
7259 capture_token->location);
7261 capture_init_expr
7262 = finish_id_expression
7263 (capture_id,
7264 capture_init_expr,
7265 parser->scope,
7266 &idk,
7267 /*integral_constant_expression_p=*/false,
7268 /*allow_non_integral_constant_expression_p=*/false,
7269 /*non_integral_constant_expression_p=*/NULL,
7270 /*template_p=*/false,
7271 /*done=*/true,
7272 /*address_p=*/false,
7273 /*template_arg_p=*/false,
7274 &error_msg,
7275 capture_token->location);
7278 if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7279 capture_init_expr
7280 = unqualified_name_lookup_error (capture_init_expr);
7282 add_capture (lambda_expr,
7283 capture_id,
7284 capture_init_expr,
7285 /*by_reference_p=*/capture_kind == BY_REFERENCE,
7286 explicit_init_p);
7289 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
7292 /* Parse the (optional) middle of a lambda expression.
7294 lambda-declarator:
7295 ( parameter-declaration-clause [opt] )
7296 attribute-specifier [opt]
7297 mutable [opt]
7298 exception-specification [opt]
7299 lambda-return-type-clause [opt]
7301 LAMBDA_EXPR is the current representation of the lambda expression. */
7303 static void
7304 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7306 /* 5.1.1.4 of the standard says:
7307 If a lambda-expression does not include a lambda-declarator, it is as if
7308 the lambda-declarator were ().
7309 This means an empty parameter list, no attributes, and no exception
7310 specification. */
7311 tree param_list = void_list_node;
7312 tree attributes = NULL_TREE;
7313 tree exception_spec = NULL_TREE;
7314 tree t;
7316 /* The lambda-declarator is optional, but must begin with an opening
7317 parenthesis if present. */
7318 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7320 cp_lexer_consume_token (parser->lexer);
7322 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7324 /* Parse parameters. */
7325 param_list = cp_parser_parameter_declaration_clause (parser);
7327 /* Default arguments shall not be specified in the
7328 parameter-declaration-clause of a lambda-declarator. */
7329 for (t = param_list; t; t = TREE_CHAIN (t))
7330 if (TREE_PURPOSE (t))
7331 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7332 "default argument specified for lambda parameter");
7334 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7336 attributes = cp_parser_attributes_opt (parser);
7338 /* Parse optional `mutable' keyword. */
7339 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7341 cp_lexer_consume_token (parser->lexer);
7342 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7345 /* Parse optional exception specification. */
7346 exception_spec = cp_parser_exception_specification_opt (parser);
7348 /* Parse optional trailing return type. */
7349 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7351 cp_lexer_consume_token (parser->lexer);
7352 LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7355 /* The function parameters must be in scope all the way until after the
7356 trailing-return-type in case of decltype. */
7357 for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
7358 pop_binding (DECL_NAME (t), t);
7360 leave_scope ();
7363 /* Create the function call operator.
7365 Messing with declarators like this is no uglier than building up the
7366 FUNCTION_DECL by hand, and this is less likely to get out of sync with
7367 other code. */
7369 cp_decl_specifier_seq return_type_specs;
7370 cp_declarator* declarator;
7371 tree fco;
7372 int quals;
7373 void *p;
7375 clear_decl_specs (&return_type_specs);
7376 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7377 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7378 else
7379 /* Maybe we will deduce the return type later, but we can use void
7380 as a placeholder return type anyways. */
7381 return_type_specs.type = void_type_node;
7383 p = obstack_alloc (&declarator_obstack, 0);
7385 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7386 sfk_none);
7388 quals = TYPE_UNQUALIFIED;
7389 if (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) == NULL_TREE
7390 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_NONE)
7392 /* A lambda with no captures has a static op() and a conversion op
7393 to function type. */
7394 if (LAMBDA_EXPR_MUTABLE_P (lambda_expr))
7395 error ("lambda expression with no captures declared mutable");
7396 return_type_specs.storage_class = sc_static;
7398 else if (!LAMBDA_EXPR_MUTABLE_P (lambda_expr))
7399 quals = TYPE_QUAL_CONST;
7400 declarator = make_call_declarator (declarator, param_list, quals,
7401 exception_spec,
7402 /*late_return_type=*/NULL_TREE);
7404 fco = grokmethod (&return_type_specs,
7405 declarator,
7406 attributes);
7407 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7408 DECL_ARTIFICIAL (fco) = 1;
7410 finish_member_declaration (fco);
7412 obstack_free (&declarator_obstack, p);
7416 /* Parse the body of a lambda expression, which is simply
7418 compound-statement
7420 but which requires special handling.
7421 LAMBDA_EXPR is the current representation of the lambda expression. */
7423 static void
7424 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7426 bool nested = (current_function_decl != NULL_TREE);
7427 if (nested)
7428 push_function_context ();
7430 /* Finish the function call operator
7431 - class_specifier
7432 + late_parsing_for_member
7433 + function_definition_after_declarator
7434 + ctor_initializer_opt_and_function_body */
7436 tree fco = lambda_function (lambda_expr);
7437 tree body;
7438 bool done = false;
7440 /* Let the front end know that we are going to be defining this
7441 function. */
7442 start_preparsed_function (fco,
7443 NULL_TREE,
7444 SF_PRE_PARSED | SF_INCLASS_INLINE);
7446 start_lambda_scope (fco);
7447 body = begin_function_body ();
7449 /* 5.1.1.4 of the standard says:
7450 If a lambda-expression does not include a trailing-return-type, it
7451 is as if the trailing-return-type denotes the following type:
7452 * if the compound-statement is of the form
7453 { return attribute-specifier [opt] expression ; }
7454 the type of the returned expression after lvalue-to-rvalue
7455 conversion (_conv.lval_ 4.1), array-to-pointer conversion
7456 (_conv.array_ 4.2), and function-to-pointer conversion
7457 (_conv.func_ 4.3);
7458 * otherwise, void. */
7460 /* In a lambda that has neither a lambda-return-type-clause
7461 nor a deducible form, errors should be reported for return statements
7462 in the body. Since we used void as the placeholder return type, parsing
7463 the body as usual will give such desired behavior. */
7464 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7465 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
7466 && cp_lexer_peek_nth_token (parser->lexer, 2)->keyword == RID_RETURN
7467 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_SEMICOLON)
7469 tree compound_stmt;
7470 tree expr = NULL_TREE;
7471 cp_id_kind idk = CP_ID_KIND_NONE;
7473 /* Parse tentatively in case there's more after the initial return
7474 statement. */
7475 cp_parser_parse_tentatively (parser);
7477 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7478 cp_parser_require_keyword (parser, RID_RETURN, "%<return%>");
7480 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7482 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7483 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7485 if (cp_parser_parse_definitely (parser))
7487 apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7489 compound_stmt = begin_compound_stmt (0);
7490 /* Will get error here if type not deduced yet. */
7491 finish_return_stmt (expr);
7492 finish_compound_stmt (compound_stmt);
7494 done = true;
7498 if (!done)
7500 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7501 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
7502 /* TODO: does begin_compound_stmt want BCS_FN_BODY?
7503 cp_parser_compound_stmt does not pass it. */
7504 cp_parser_function_body (parser);
7505 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
7508 finish_function_body (body);
7509 finish_lambda_scope ();
7511 /* Finish the function and generate code for it if necessary. */
7512 expand_or_defer_fn (finish_function (/*inline*/2));
7515 if (nested)
7516 pop_function_context();
7519 /* Statements [gram.stmt.stmt] */
7521 /* Parse a statement.
7523 statement:
7524 labeled-statement
7525 expression-statement
7526 compound-statement
7527 selection-statement
7528 iteration-statement
7529 jump-statement
7530 declaration-statement
7531 try-block
7533 IN_COMPOUND is true when the statement is nested inside a
7534 cp_parser_compound_statement; this matters for certain pragmas.
7536 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7537 is a (possibly labeled) if statement which is not enclosed in braces
7538 and has an else clause. This is used to implement -Wparentheses. */
7540 static void
7541 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
7542 bool in_compound, bool *if_p)
7544 tree statement;
7545 cp_token *token;
7546 location_t statement_location;
7548 restart:
7549 if (if_p != NULL)
7550 *if_p = false;
7551 /* There is no statement yet. */
7552 statement = NULL_TREE;
7553 /* Peek at the next token. */
7554 token = cp_lexer_peek_token (parser->lexer);
7555 /* Remember the location of the first token in the statement. */
7556 statement_location = token->location;
7557 /* If this is a keyword, then that will often determine what kind of
7558 statement we have. */
7559 if (token->type == CPP_KEYWORD)
7561 enum rid keyword = token->keyword;
7563 switch (keyword)
7565 case RID_CASE:
7566 case RID_DEFAULT:
7567 /* Looks like a labeled-statement with a case label.
7568 Parse the label, and then use tail recursion to parse
7569 the statement. */
7570 cp_parser_label_for_labeled_statement (parser);
7571 goto restart;
7573 case RID_IF:
7574 case RID_SWITCH:
7575 statement = cp_parser_selection_statement (parser, if_p);
7576 break;
7578 case RID_WHILE:
7579 case RID_DO:
7580 case RID_FOR:
7581 statement = cp_parser_iteration_statement (parser);
7582 break;
7584 case RID_BREAK:
7585 case RID_CONTINUE:
7586 case RID_RETURN:
7587 case RID_GOTO:
7588 statement = cp_parser_jump_statement (parser);
7589 break;
7591 /* Objective-C++ exception-handling constructs. */
7592 case RID_AT_TRY:
7593 case RID_AT_CATCH:
7594 case RID_AT_FINALLY:
7595 case RID_AT_SYNCHRONIZED:
7596 case RID_AT_THROW:
7597 statement = cp_parser_objc_statement (parser);
7598 break;
7600 case RID_TRY:
7601 statement = cp_parser_try_block (parser);
7602 break;
7604 case RID_NAMESPACE:
7605 /* This must be a namespace alias definition. */
7606 cp_parser_declaration_statement (parser);
7607 return;
7609 default:
7610 /* It might be a keyword like `int' that can start a
7611 declaration-statement. */
7612 break;
7615 else if (token->type == CPP_NAME)
7617 /* If the next token is a `:', then we are looking at a
7618 labeled-statement. */
7619 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7620 if (token->type == CPP_COLON)
7622 /* Looks like a labeled-statement with an ordinary label.
7623 Parse the label, and then use tail recursion to parse
7624 the statement. */
7625 cp_parser_label_for_labeled_statement (parser);
7626 goto restart;
7629 /* Anything that starts with a `{' must be a compound-statement. */
7630 else if (token->type == CPP_OPEN_BRACE)
7631 statement = cp_parser_compound_statement (parser, NULL, false);
7632 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
7633 a statement all its own. */
7634 else if (token->type == CPP_PRAGMA)
7636 /* Only certain OpenMP pragmas are attached to statements, and thus
7637 are considered statements themselves. All others are not. In
7638 the context of a compound, accept the pragma as a "statement" and
7639 return so that we can check for a close brace. Otherwise we
7640 require a real statement and must go back and read one. */
7641 if (in_compound)
7642 cp_parser_pragma (parser, pragma_compound);
7643 else if (!cp_parser_pragma (parser, pragma_stmt))
7644 goto restart;
7645 return;
7647 else if (token->type == CPP_EOF)
7649 cp_parser_error (parser, "expected statement");
7650 return;
7653 /* Everything else must be a declaration-statement or an
7654 expression-statement. Try for the declaration-statement
7655 first, unless we are looking at a `;', in which case we know that
7656 we have an expression-statement. */
7657 if (!statement)
7659 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7661 cp_parser_parse_tentatively (parser);
7662 /* Try to parse the declaration-statement. */
7663 cp_parser_declaration_statement (parser);
7664 /* If that worked, we're done. */
7665 if (cp_parser_parse_definitely (parser))
7666 return;
7668 /* Look for an expression-statement instead. */
7669 statement = cp_parser_expression_statement (parser, in_statement_expr);
7672 /* Set the line number for the statement. */
7673 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7674 SET_EXPR_LOCATION (statement, statement_location);
7677 /* Parse the label for a labeled-statement, i.e.
7679 identifier :
7680 case constant-expression :
7681 default :
7683 GNU Extension:
7684 case constant-expression ... constant-expression : statement
7686 When a label is parsed without errors, the label is added to the
7687 parse tree by the finish_* functions, so this function doesn't
7688 have to return the label. */
7690 static void
7691 cp_parser_label_for_labeled_statement (cp_parser* parser)
7693 cp_token *token;
7694 tree label = NULL_TREE;
7696 /* The next token should be an identifier. */
7697 token = cp_lexer_peek_token (parser->lexer);
7698 if (token->type != CPP_NAME
7699 && token->type != CPP_KEYWORD)
7701 cp_parser_error (parser, "expected labeled-statement");
7702 return;
7705 switch (token->keyword)
7707 case RID_CASE:
7709 tree expr, expr_hi;
7710 cp_token *ellipsis;
7712 /* Consume the `case' token. */
7713 cp_lexer_consume_token (parser->lexer);
7714 /* Parse the constant-expression. */
7715 expr = cp_parser_constant_expression (parser,
7716 /*allow_non_constant_p=*/false,
7717 NULL);
7719 ellipsis = cp_lexer_peek_token (parser->lexer);
7720 if (ellipsis->type == CPP_ELLIPSIS)
7722 /* Consume the `...' token. */
7723 cp_lexer_consume_token (parser->lexer);
7724 expr_hi =
7725 cp_parser_constant_expression (parser,
7726 /*allow_non_constant_p=*/false,
7727 NULL);
7728 /* We don't need to emit warnings here, as the common code
7729 will do this for us. */
7731 else
7732 expr_hi = NULL_TREE;
7734 if (parser->in_switch_statement_p)
7735 finish_case_label (token->location, expr, expr_hi);
7736 else
7737 error_at (token->location,
7738 "case label %qE not within a switch statement",
7739 expr);
7741 break;
7743 case RID_DEFAULT:
7744 /* Consume the `default' token. */
7745 cp_lexer_consume_token (parser->lexer);
7747 if (parser->in_switch_statement_p)
7748 finish_case_label (token->location, NULL_TREE, NULL_TREE);
7749 else
7750 error_at (token->location, "case label not within a switch statement");
7751 break;
7753 default:
7754 /* Anything else must be an ordinary label. */
7755 label = finish_label_stmt (cp_parser_identifier (parser));
7756 break;
7759 /* Require the `:' token. */
7760 cp_parser_require (parser, CPP_COLON, "%<:%>");
7762 /* An ordinary label may optionally be followed by attributes.
7763 However, this is only permitted if the attributes are then
7764 followed by a semicolon. This is because, for backward
7765 compatibility, when parsing
7766 lab: __attribute__ ((unused)) int i;
7767 we want the attribute to attach to "i", not "lab". */
7768 if (label != NULL_TREE
7769 && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
7771 tree attrs;
7773 cp_parser_parse_tentatively (parser);
7774 attrs = cp_parser_attributes_opt (parser);
7775 if (attrs == NULL_TREE
7776 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7777 cp_parser_abort_tentative_parse (parser);
7778 else if (!cp_parser_parse_definitely (parser))
7780 else
7781 cplus_decl_attributes (&label, attrs, 0);
7785 /* Parse an expression-statement.
7787 expression-statement:
7788 expression [opt] ;
7790 Returns the new EXPR_STMT -- or NULL_TREE if the expression
7791 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
7792 indicates whether this expression-statement is part of an
7793 expression statement. */
7795 static tree
7796 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
7798 tree statement = NULL_TREE;
7799 cp_token *token = cp_lexer_peek_token (parser->lexer);
7801 /* If the next token is a ';', then there is no expression
7802 statement. */
7803 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7804 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7806 /* Give a helpful message for "A<T>::type t;" and the like. */
7807 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
7808 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
7810 if (TREE_CODE (statement) == SCOPE_REF)
7811 error_at (token->location, "need %<typename%> before %qE because "
7812 "%qT is a dependent scope",
7813 statement, TREE_OPERAND (statement, 0));
7814 else if (is_overloaded_fn (statement)
7815 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
7817 /* A::A a; */
7818 tree fn = get_first_fn (statement);
7819 error_at (token->location,
7820 "%<%T::%D%> names the constructor, not the type",
7821 DECL_CONTEXT (fn), DECL_NAME (fn));
7825 /* Consume the final `;'. */
7826 cp_parser_consume_semicolon_at_end_of_statement (parser);
7828 if (in_statement_expr
7829 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
7830 /* This is the final expression statement of a statement
7831 expression. */
7832 statement = finish_stmt_expr_expr (statement, in_statement_expr);
7833 else if (statement)
7834 statement = finish_expr_stmt (statement);
7835 else
7836 finish_stmt ();
7838 return statement;
7841 /* Parse a compound-statement.
7843 compound-statement:
7844 { statement-seq [opt] }
7846 GNU extension:
7848 compound-statement:
7849 { label-declaration-seq [opt] statement-seq [opt] }
7851 label-declaration-seq:
7852 label-declaration
7853 label-declaration-seq label-declaration
7855 Returns a tree representing the statement. */
7857 static tree
7858 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7859 bool in_try)
7861 tree compound_stmt;
7863 /* Consume the `{'. */
7864 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7865 return error_mark_node;
7866 /* Begin the compound-statement. */
7867 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7868 /* If the next keyword is `__label__' we have a label declaration. */
7869 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7870 cp_parser_label_declaration (parser);
7871 /* Parse an (optional) statement-seq. */
7872 cp_parser_statement_seq_opt (parser, in_statement_expr);
7873 /* Finish the compound-statement. */
7874 finish_compound_stmt (compound_stmt);
7875 /* Consume the `}'. */
7876 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7878 return compound_stmt;
7881 /* Parse an (optional) statement-seq.
7883 statement-seq:
7884 statement
7885 statement-seq [opt] statement */
7887 static void
7888 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7890 /* Scan statements until there aren't any more. */
7891 while (true)
7893 cp_token *token = cp_lexer_peek_token (parser->lexer);
7895 /* If we're looking at a `}', then we've run out of statements. */
7896 if (token->type == CPP_CLOSE_BRACE
7897 || token->type == CPP_EOF
7898 || token->type == CPP_PRAGMA_EOL)
7899 break;
7901 /* If we are in a compound statement and find 'else' then
7902 something went wrong. */
7903 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7905 if (parser->in_statement & IN_IF_STMT)
7906 break;
7907 else
7909 token = cp_lexer_consume_token (parser->lexer);
7910 error_at (token->location, "%<else%> without a previous %<if%>");
7914 /* Parse the statement. */
7915 cp_parser_statement (parser, in_statement_expr, true, NULL);
7919 /* Parse a selection-statement.
7921 selection-statement:
7922 if ( condition ) statement
7923 if ( condition ) statement else statement
7924 switch ( condition ) statement
7926 Returns the new IF_STMT or SWITCH_STMT.
7928 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7929 is a (possibly labeled) if statement which is not enclosed in
7930 braces and has an else clause. This is used to implement
7931 -Wparentheses. */
7933 static tree
7934 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7936 cp_token *token;
7937 enum rid keyword;
7939 if (if_p != NULL)
7940 *if_p = false;
7942 /* Peek at the next token. */
7943 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7945 /* See what kind of keyword it is. */
7946 keyword = token->keyword;
7947 switch (keyword)
7949 case RID_IF:
7950 case RID_SWITCH:
7952 tree statement;
7953 tree condition;
7955 /* Look for the `('. */
7956 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7958 cp_parser_skip_to_end_of_statement (parser);
7959 return error_mark_node;
7962 /* Begin the selection-statement. */
7963 if (keyword == RID_IF)
7964 statement = begin_if_stmt ();
7965 else
7966 statement = begin_switch_stmt ();
7968 /* Parse the condition. */
7969 condition = cp_parser_condition (parser);
7970 /* Look for the `)'. */
7971 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7972 cp_parser_skip_to_closing_parenthesis (parser, true, false,
7973 /*consume_paren=*/true);
7975 if (keyword == RID_IF)
7977 bool nested_if;
7978 unsigned char in_statement;
7980 /* Add the condition. */
7981 finish_if_stmt_cond (condition, statement);
7983 /* Parse the then-clause. */
7984 in_statement = parser->in_statement;
7985 parser->in_statement |= IN_IF_STMT;
7986 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7988 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7989 add_stmt (build_empty_stmt (loc));
7990 cp_lexer_consume_token (parser->lexer);
7991 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
7992 warning_at (loc, OPT_Wempty_body, "suggest braces around "
7993 "empty body in an %<if%> statement");
7994 nested_if = false;
7996 else
7997 cp_parser_implicitly_scoped_statement (parser, &nested_if);
7998 parser->in_statement = in_statement;
8000 finish_then_clause (statement);
8002 /* If the next token is `else', parse the else-clause. */
8003 if (cp_lexer_next_token_is_keyword (parser->lexer,
8004 RID_ELSE))
8006 /* Consume the `else' keyword. */
8007 cp_lexer_consume_token (parser->lexer);
8008 begin_else_clause (statement);
8009 /* Parse the else-clause. */
8010 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8012 location_t loc;
8013 loc = cp_lexer_peek_token (parser->lexer)->location;
8014 warning_at (loc,
8015 OPT_Wempty_body, "suggest braces around "
8016 "empty body in an %<else%> statement");
8017 add_stmt (build_empty_stmt (loc));
8018 cp_lexer_consume_token (parser->lexer);
8020 else
8021 cp_parser_implicitly_scoped_statement (parser, NULL);
8023 finish_else_clause (statement);
8025 /* If we are currently parsing a then-clause, then
8026 IF_P will not be NULL. We set it to true to
8027 indicate that this if statement has an else clause.
8028 This may trigger the Wparentheses warning below
8029 when we get back up to the parent if statement. */
8030 if (if_p != NULL)
8031 *if_p = true;
8033 else
8035 /* This if statement does not have an else clause. If
8036 NESTED_IF is true, then the then-clause is an if
8037 statement which does have an else clause. We warn
8038 about the potential ambiguity. */
8039 if (nested_if)
8040 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
8041 "suggest explicit braces to avoid ambiguous"
8042 " %<else%>");
8045 /* Now we're all done with the if-statement. */
8046 finish_if_stmt (statement);
8048 else
8050 bool in_switch_statement_p;
8051 unsigned char in_statement;
8053 /* Add the condition. */
8054 finish_switch_cond (condition, statement);
8056 /* Parse the body of the switch-statement. */
8057 in_switch_statement_p = parser->in_switch_statement_p;
8058 in_statement = parser->in_statement;
8059 parser->in_switch_statement_p = true;
8060 parser->in_statement |= IN_SWITCH_STMT;
8061 cp_parser_implicitly_scoped_statement (parser, NULL);
8062 parser->in_switch_statement_p = in_switch_statement_p;
8063 parser->in_statement = in_statement;
8065 /* Now we're all done with the switch-statement. */
8066 finish_switch_stmt (statement);
8069 return statement;
8071 break;
8073 default:
8074 cp_parser_error (parser, "expected selection-statement");
8075 return error_mark_node;
8079 /* Parse a condition.
8081 condition:
8082 expression
8083 type-specifier-seq declarator = initializer-clause
8084 type-specifier-seq declarator braced-init-list
8086 GNU Extension:
8088 condition:
8089 type-specifier-seq declarator asm-specification [opt]
8090 attributes [opt] = assignment-expression
8092 Returns the expression that should be tested. */
8094 static tree
8095 cp_parser_condition (cp_parser* parser)
8097 cp_decl_specifier_seq type_specifiers;
8098 const char *saved_message;
8100 /* Try the declaration first. */
8101 cp_parser_parse_tentatively (parser);
8102 /* New types are not allowed in the type-specifier-seq for a
8103 condition. */
8104 saved_message = parser->type_definition_forbidden_message;
8105 parser->type_definition_forbidden_message
8106 = "types may not be defined in conditions";
8107 /* Parse the type-specifier-seq. */
8108 cp_parser_type_specifier_seq (parser, /*is_declaration==*/true,
8109 /*is_trailing_return=*/false,
8110 &type_specifiers);
8111 /* Restore the saved message. */
8112 parser->type_definition_forbidden_message = saved_message;
8113 /* If all is well, we might be looking at a declaration. */
8114 if (!cp_parser_error_occurred (parser))
8116 tree decl;
8117 tree asm_specification;
8118 tree attributes;
8119 cp_declarator *declarator;
8120 tree initializer = NULL_TREE;
8122 /* Parse the declarator. */
8123 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8124 /*ctor_dtor_or_conv_p=*/NULL,
8125 /*parenthesized_p=*/NULL,
8126 /*member_p=*/false);
8127 /* Parse the attributes. */
8128 attributes = cp_parser_attributes_opt (parser);
8129 /* Parse the asm-specification. */
8130 asm_specification = cp_parser_asm_specification_opt (parser);
8131 /* If the next token is not an `=' or '{', then we might still be
8132 looking at an expression. For example:
8134 if (A(a).x)
8136 looks like a decl-specifier-seq and a declarator -- but then
8137 there is no `=', so this is an expression. */
8138 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8139 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8140 cp_parser_simulate_error (parser);
8142 /* If we did see an `=' or '{', then we are looking at a declaration
8143 for sure. */
8144 if (cp_parser_parse_definitely (parser))
8146 tree pushed_scope;
8147 bool non_constant_p;
8148 bool flags = LOOKUP_ONLYCONVERTING;
8150 /* Create the declaration. */
8151 decl = start_decl (declarator, &type_specifiers,
8152 /*initialized_p=*/true,
8153 attributes, /*prefix_attributes=*/NULL_TREE,
8154 &pushed_scope);
8156 /* Parse the initializer. */
8157 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8159 initializer = cp_parser_braced_list (parser, &non_constant_p);
8160 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8161 flags = 0;
8163 else
8165 /* Consume the `='. */
8166 cp_parser_require (parser, CPP_EQ, "%<=%>");
8167 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8169 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
8170 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8172 if (!non_constant_p)
8173 initializer = fold_non_dependent_expr (initializer);
8175 /* Process the initializer. */
8176 cp_finish_decl (decl,
8177 initializer, !non_constant_p,
8178 asm_specification,
8179 flags);
8181 if (pushed_scope)
8182 pop_scope (pushed_scope);
8184 return convert_from_reference (decl);
8187 /* If we didn't even get past the declarator successfully, we are
8188 definitely not looking at a declaration. */
8189 else
8190 cp_parser_abort_tentative_parse (parser);
8192 /* Otherwise, we are looking at an expression. */
8193 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
8196 /* Parse an iteration-statement.
8198 iteration-statement:
8199 while ( condition ) statement
8200 do statement while ( expression ) ;
8201 for ( for-init-statement condition [opt] ; expression [opt] )
8202 statement
8204 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
8206 static tree
8207 cp_parser_iteration_statement (cp_parser* parser)
8209 cp_token *token;
8210 enum rid keyword;
8211 tree statement;
8212 unsigned char in_statement;
8214 /* Peek at the next token. */
8215 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
8216 if (!token)
8217 return error_mark_node;
8219 /* Remember whether or not we are already within an iteration
8220 statement. */
8221 in_statement = parser->in_statement;
8223 /* See what kind of keyword it is. */
8224 keyword = token->keyword;
8225 switch (keyword)
8227 case RID_WHILE:
8229 tree condition;
8231 /* Begin the while-statement. */
8232 statement = begin_while_stmt ();
8233 /* Look for the `('. */
8234 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8235 /* Parse the condition. */
8236 condition = cp_parser_condition (parser);
8237 finish_while_stmt_cond (condition, statement);
8238 /* Look for the `)'. */
8239 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8240 /* Parse the dependent statement. */
8241 parser->in_statement = IN_ITERATION_STMT;
8242 cp_parser_already_scoped_statement (parser);
8243 parser->in_statement = in_statement;
8244 /* We're done with the while-statement. */
8245 finish_while_stmt (statement);
8247 break;
8249 case RID_DO:
8251 tree expression;
8253 /* Begin the do-statement. */
8254 statement = begin_do_stmt ();
8255 /* Parse the body of the do-statement. */
8256 parser->in_statement = IN_ITERATION_STMT;
8257 cp_parser_implicitly_scoped_statement (parser, NULL);
8258 parser->in_statement = in_statement;
8259 finish_do_body (statement);
8260 /* Look for the `while' keyword. */
8261 cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
8262 /* Look for the `('. */
8263 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8264 /* Parse the expression. */
8265 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8266 /* We're done with the do-statement. */
8267 finish_do_stmt (expression, statement);
8268 /* Look for the `)'. */
8269 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8270 /* Look for the `;'. */
8271 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8273 break;
8275 case RID_FOR:
8277 tree condition = NULL_TREE;
8278 tree expression = NULL_TREE;
8280 /* Begin the for-statement. */
8281 statement = begin_for_stmt ();
8282 /* Look for the `('. */
8283 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8284 /* Parse the initialization. */
8285 cp_parser_for_init_statement (parser);
8286 finish_for_init_stmt (statement);
8288 /* If there's a condition, process it. */
8289 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8290 condition = cp_parser_condition (parser);
8291 finish_for_cond (condition, statement);
8292 /* Look for the `;'. */
8293 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8295 /* If there's an expression, process it. */
8296 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8297 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8298 finish_for_expr (expression, statement);
8299 /* Look for the `)'. */
8300 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8302 /* Parse the body of the for-statement. */
8303 parser->in_statement = IN_ITERATION_STMT;
8304 cp_parser_already_scoped_statement (parser);
8305 parser->in_statement = in_statement;
8307 /* We're done with the for-statement. */
8308 finish_for_stmt (statement);
8310 break;
8312 default:
8313 cp_parser_error (parser, "expected iteration-statement");
8314 statement = error_mark_node;
8315 break;
8318 return statement;
8321 /* Parse a for-init-statement.
8323 for-init-statement:
8324 expression-statement
8325 simple-declaration */
8327 static void
8328 cp_parser_for_init_statement (cp_parser* parser)
8330 /* If the next token is a `;', then we have an empty
8331 expression-statement. Grammatically, this is also a
8332 simple-declaration, but an invalid one, because it does not
8333 declare anything. Therefore, if we did not handle this case
8334 specially, we would issue an error message about an invalid
8335 declaration. */
8336 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8338 /* We're going to speculatively look for a declaration, falling back
8339 to an expression, if necessary. */
8340 cp_parser_parse_tentatively (parser);
8341 /* Parse the declaration. */
8342 cp_parser_simple_declaration (parser,
8343 /*function_definition_allowed_p=*/false);
8344 /* If the tentative parse failed, then we shall need to look for an
8345 expression-statement. */
8346 if (cp_parser_parse_definitely (parser))
8347 return;
8350 cp_parser_expression_statement (parser, false);
8353 /* Parse a jump-statement.
8355 jump-statement:
8356 break ;
8357 continue ;
8358 return expression [opt] ;
8359 return braced-init-list ;
8360 goto identifier ;
8362 GNU extension:
8364 jump-statement:
8365 goto * expression ;
8367 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
8369 static tree
8370 cp_parser_jump_statement (cp_parser* parser)
8372 tree statement = error_mark_node;
8373 cp_token *token;
8374 enum rid keyword;
8375 unsigned char in_statement;
8377 /* Peek at the next token. */
8378 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
8379 if (!token)
8380 return error_mark_node;
8382 /* See what kind of keyword it is. */
8383 keyword = token->keyword;
8384 switch (keyword)
8386 case RID_BREAK:
8387 in_statement = parser->in_statement & ~IN_IF_STMT;
8388 switch (in_statement)
8390 case 0:
8391 error_at (token->location, "break statement not within loop or switch");
8392 break;
8393 default:
8394 gcc_assert ((in_statement & IN_SWITCH_STMT)
8395 || in_statement == IN_ITERATION_STMT);
8396 statement = finish_break_stmt ();
8397 break;
8398 case IN_OMP_BLOCK:
8399 error_at (token->location, "invalid exit from OpenMP structured block");
8400 break;
8401 case IN_OMP_FOR:
8402 error_at (token->location, "break statement used with OpenMP for loop");
8403 break;
8405 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8406 break;
8408 case RID_CONTINUE:
8409 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
8411 case 0:
8412 error_at (token->location, "continue statement not within a loop");
8413 break;
8414 case IN_ITERATION_STMT:
8415 case IN_OMP_FOR:
8416 statement = finish_continue_stmt ();
8417 break;
8418 case IN_OMP_BLOCK:
8419 error_at (token->location, "invalid exit from OpenMP structured block");
8420 break;
8421 default:
8422 gcc_unreachable ();
8424 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8425 break;
8427 case RID_RETURN:
8429 tree expr;
8430 bool expr_non_constant_p;
8432 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8434 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8435 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8437 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8438 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8439 else
8440 /* If the next token is a `;', then there is no
8441 expression. */
8442 expr = NULL_TREE;
8443 /* Build the return-statement. */
8444 statement = finish_return_stmt (expr);
8445 /* Look for the final `;'. */
8446 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8448 break;
8450 case RID_GOTO:
8451 /* Create the goto-statement. */
8452 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
8454 /* Issue a warning about this use of a GNU extension. */
8455 pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
8456 /* Consume the '*' token. */
8457 cp_lexer_consume_token (parser->lexer);
8458 /* Parse the dependent expression. */
8459 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
8461 else
8462 finish_goto_stmt (cp_parser_identifier (parser));
8463 /* Look for the final `;'. */
8464 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8465 break;
8467 default:
8468 cp_parser_error (parser, "expected jump-statement");
8469 break;
8472 return statement;
8475 /* Parse a declaration-statement.
8477 declaration-statement:
8478 block-declaration */
8480 static void
8481 cp_parser_declaration_statement (cp_parser* parser)
8483 void *p;
8485 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
8486 p = obstack_alloc (&declarator_obstack, 0);
8488 /* Parse the block-declaration. */
8489 cp_parser_block_declaration (parser, /*statement_p=*/true);
8491 /* Free any declarators allocated. */
8492 obstack_free (&declarator_obstack, p);
8494 /* Finish off the statement. */
8495 finish_stmt ();
8498 /* Some dependent statements (like `if (cond) statement'), are
8499 implicitly in their own scope. In other words, if the statement is
8500 a single statement (as opposed to a compound-statement), it is
8501 none-the-less treated as if it were enclosed in braces. Any
8502 declarations appearing in the dependent statement are out of scope
8503 after control passes that point. This function parses a statement,
8504 but ensures that is in its own scope, even if it is not a
8505 compound-statement.
8507 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8508 is a (possibly labeled) if statement which is not enclosed in
8509 braces and has an else clause. This is used to implement
8510 -Wparentheses.
8512 Returns the new statement. */
8514 static tree
8515 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
8517 tree statement;
8519 if (if_p != NULL)
8520 *if_p = false;
8522 /* Mark if () ; with a special NOP_EXPR. */
8523 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8525 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8526 cp_lexer_consume_token (parser->lexer);
8527 statement = add_stmt (build_empty_stmt (loc));
8529 /* if a compound is opened, we simply parse the statement directly. */
8530 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8531 statement = cp_parser_compound_statement (parser, NULL, false);
8532 /* If the token is not a `{', then we must take special action. */
8533 else
8535 /* Create a compound-statement. */
8536 statement = begin_compound_stmt (0);
8537 /* Parse the dependent-statement. */
8538 cp_parser_statement (parser, NULL_TREE, false, if_p);
8539 /* Finish the dummy compound-statement. */
8540 finish_compound_stmt (statement);
8543 /* Return the statement. */
8544 return statement;
8547 /* For some dependent statements (like `while (cond) statement'), we
8548 have already created a scope. Therefore, even if the dependent
8549 statement is a compound-statement, we do not want to create another
8550 scope. */
8552 static void
8553 cp_parser_already_scoped_statement (cp_parser* parser)
8555 /* If the token is a `{', then we must take special action. */
8556 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8557 cp_parser_statement (parser, NULL_TREE, false, NULL);
8558 else
8560 /* Avoid calling cp_parser_compound_statement, so that we
8561 don't create a new scope. Do everything else by hand. */
8562 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
8563 /* If the next keyword is `__label__' we have a label declaration. */
8564 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8565 cp_parser_label_declaration (parser);
8566 /* Parse an (optional) statement-seq. */
8567 cp_parser_statement_seq_opt (parser, NULL_TREE);
8568 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8572 /* Declarations [gram.dcl.dcl] */
8574 /* Parse an optional declaration-sequence.
8576 declaration-seq:
8577 declaration
8578 declaration-seq declaration */
8580 static void
8581 cp_parser_declaration_seq_opt (cp_parser* parser)
8583 while (true)
8585 cp_token *token;
8587 token = cp_lexer_peek_token (parser->lexer);
8589 if (token->type == CPP_CLOSE_BRACE
8590 || token->type == CPP_EOF
8591 || token->type == CPP_PRAGMA_EOL)
8592 break;
8594 if (token->type == CPP_SEMICOLON)
8596 /* A declaration consisting of a single semicolon is
8597 invalid. Allow it unless we're being pedantic. */
8598 cp_lexer_consume_token (parser->lexer);
8599 if (!in_system_header)
8600 pedwarn (input_location, OPT_pedantic, "extra %<;%>");
8601 continue;
8604 /* If we're entering or exiting a region that's implicitly
8605 extern "C", modify the lang context appropriately. */
8606 if (!parser->implicit_extern_c && token->implicit_extern_c)
8608 push_lang_context (lang_name_c);
8609 parser->implicit_extern_c = true;
8611 else if (parser->implicit_extern_c && !token->implicit_extern_c)
8613 pop_lang_context ();
8614 parser->implicit_extern_c = false;
8617 if (token->type == CPP_PRAGMA)
8619 /* A top-level declaration can consist solely of a #pragma.
8620 A nested declaration cannot, so this is done here and not
8621 in cp_parser_declaration. (A #pragma at block scope is
8622 handled in cp_parser_statement.) */
8623 cp_parser_pragma (parser, pragma_external);
8624 continue;
8627 /* Parse the declaration itself. */
8628 cp_parser_declaration (parser);
8632 /* Parse a declaration.
8634 declaration:
8635 block-declaration
8636 function-definition
8637 template-declaration
8638 explicit-instantiation
8639 explicit-specialization
8640 linkage-specification
8641 namespace-definition
8643 GNU extension:
8645 declaration:
8646 __extension__ declaration */
8648 static void
8649 cp_parser_declaration (cp_parser* parser)
8651 cp_token token1;
8652 cp_token token2;
8653 int saved_pedantic;
8654 void *p;
8656 /* Check for the `__extension__' keyword. */
8657 if (cp_parser_extension_opt (parser, &saved_pedantic))
8659 /* Parse the qualified declaration. */
8660 cp_parser_declaration (parser);
8661 /* Restore the PEDANTIC flag. */
8662 pedantic = saved_pedantic;
8664 return;
8667 /* Try to figure out what kind of declaration is present. */
8668 token1 = *cp_lexer_peek_token (parser->lexer);
8670 if (token1.type != CPP_EOF)
8671 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
8672 else
8674 token2.type = CPP_EOF;
8675 token2.keyword = RID_MAX;
8678 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
8679 p = obstack_alloc (&declarator_obstack, 0);
8681 /* If the next token is `extern' and the following token is a string
8682 literal, then we have a linkage specification. */
8683 if (token1.keyword == RID_EXTERN
8684 && cp_parser_is_string_literal (&token2))
8685 cp_parser_linkage_specification (parser);
8686 /* If the next token is `template', then we have either a template
8687 declaration, an explicit instantiation, or an explicit
8688 specialization. */
8689 else if (token1.keyword == RID_TEMPLATE)
8691 /* `template <>' indicates a template specialization. */
8692 if (token2.type == CPP_LESS
8693 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
8694 cp_parser_explicit_specialization (parser);
8695 /* `template <' indicates a template declaration. */
8696 else if (token2.type == CPP_LESS)
8697 cp_parser_template_declaration (parser, /*member_p=*/false);
8698 /* Anything else must be an explicit instantiation. */
8699 else
8700 cp_parser_explicit_instantiation (parser);
8702 /* If the next token is `export', then we have a template
8703 declaration. */
8704 else if (token1.keyword == RID_EXPORT)
8705 cp_parser_template_declaration (parser, /*member_p=*/false);
8706 /* If the next token is `extern', 'static' or 'inline' and the one
8707 after that is `template', we have a GNU extended explicit
8708 instantiation directive. */
8709 else if (cp_parser_allow_gnu_extensions_p (parser)
8710 && (token1.keyword == RID_EXTERN
8711 || token1.keyword == RID_STATIC
8712 || token1.keyword == RID_INLINE)
8713 && token2.keyword == RID_TEMPLATE)
8714 cp_parser_explicit_instantiation (parser);
8715 /* If the next token is `namespace', check for a named or unnamed
8716 namespace definition. */
8717 else if (token1.keyword == RID_NAMESPACE
8718 && (/* A named namespace definition. */
8719 (token2.type == CPP_NAME
8720 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
8721 != CPP_EQ))
8722 /* An unnamed namespace definition. */
8723 || token2.type == CPP_OPEN_BRACE
8724 || token2.keyword == RID_ATTRIBUTE))
8725 cp_parser_namespace_definition (parser);
8726 /* An inline (associated) namespace definition. */
8727 else if (token1.keyword == RID_INLINE
8728 && token2.keyword == RID_NAMESPACE)
8729 cp_parser_namespace_definition (parser);
8730 /* Objective-C++ declaration/definition. */
8731 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
8732 cp_parser_objc_declaration (parser);
8733 /* We must have either a block declaration or a function
8734 definition. */
8735 else
8736 /* Try to parse a block-declaration, or a function-definition. */
8737 cp_parser_block_declaration (parser, /*statement_p=*/false);
8739 /* Free any declarators allocated. */
8740 obstack_free (&declarator_obstack, p);
8743 /* Parse a block-declaration.
8745 block-declaration:
8746 simple-declaration
8747 asm-definition
8748 namespace-alias-definition
8749 using-declaration
8750 using-directive
8752 GNU Extension:
8754 block-declaration:
8755 __extension__ block-declaration
8757 C++0x Extension:
8759 block-declaration:
8760 static_assert-declaration
8762 If STATEMENT_P is TRUE, then this block-declaration is occurring as
8763 part of a declaration-statement. */
8765 static void
8766 cp_parser_block_declaration (cp_parser *parser,
8767 bool statement_p)
8769 cp_token *token1;
8770 int saved_pedantic;
8772 /* Check for the `__extension__' keyword. */
8773 if (cp_parser_extension_opt (parser, &saved_pedantic))
8775 /* Parse the qualified declaration. */
8776 cp_parser_block_declaration (parser, statement_p);
8777 /* Restore the PEDANTIC flag. */
8778 pedantic = saved_pedantic;
8780 return;
8783 /* Peek at the next token to figure out which kind of declaration is
8784 present. */
8785 token1 = cp_lexer_peek_token (parser->lexer);
8787 /* If the next keyword is `asm', we have an asm-definition. */
8788 if (token1->keyword == RID_ASM)
8790 if (statement_p)
8791 cp_parser_commit_to_tentative_parse (parser);
8792 cp_parser_asm_definition (parser);
8794 /* If the next keyword is `namespace', we have a
8795 namespace-alias-definition. */
8796 else if (token1->keyword == RID_NAMESPACE)
8797 cp_parser_namespace_alias_definition (parser);
8798 /* If the next keyword is `using', we have either a
8799 using-declaration or a using-directive. */
8800 else if (token1->keyword == RID_USING)
8802 cp_token *token2;
8804 if (statement_p)
8805 cp_parser_commit_to_tentative_parse (parser);
8806 /* If the token after `using' is `namespace', then we have a
8807 using-directive. */
8808 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8809 if (token2->keyword == RID_NAMESPACE)
8810 cp_parser_using_directive (parser);
8811 /* Otherwise, it's a using-declaration. */
8812 else
8813 cp_parser_using_declaration (parser,
8814 /*access_declaration_p=*/false);
8816 /* If the next keyword is `__label__' we have a misplaced label
8817 declaration. */
8818 else if (token1->keyword == RID_LABEL)
8820 cp_lexer_consume_token (parser->lexer);
8821 error_at (token1->location, "%<__label__%> not at the beginning of a block");
8822 cp_parser_skip_to_end_of_statement (parser);
8823 /* If the next token is now a `;', consume it. */
8824 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8825 cp_lexer_consume_token (parser->lexer);
8827 /* If the next token is `static_assert' we have a static assertion. */
8828 else if (token1->keyword == RID_STATIC_ASSERT)
8829 cp_parser_static_assert (parser, /*member_p=*/false);
8830 /* Anything else must be a simple-declaration. */
8831 else
8832 cp_parser_simple_declaration (parser, !statement_p);
8835 /* Parse a simple-declaration.
8837 simple-declaration:
8838 decl-specifier-seq [opt] init-declarator-list [opt] ;
8840 init-declarator-list:
8841 init-declarator
8842 init-declarator-list , init-declarator
8844 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8845 function-definition as a simple-declaration. */
8847 static void
8848 cp_parser_simple_declaration (cp_parser* parser,
8849 bool function_definition_allowed_p)
8851 cp_decl_specifier_seq decl_specifiers;
8852 int declares_class_or_enum;
8853 bool saw_declarator;
8855 /* Defer access checks until we know what is being declared; the
8856 checks for names appearing in the decl-specifier-seq should be
8857 done as if we were in the scope of the thing being declared. */
8858 push_deferring_access_checks (dk_deferred);
8860 /* Parse the decl-specifier-seq. We have to keep track of whether
8861 or not the decl-specifier-seq declares a named class or
8862 enumeration type, since that is the only case in which the
8863 init-declarator-list is allowed to be empty.
8865 [dcl.dcl]
8867 In a simple-declaration, the optional init-declarator-list can be
8868 omitted only when declaring a class or enumeration, that is when
8869 the decl-specifier-seq contains either a class-specifier, an
8870 elaborated-type-specifier, or an enum-specifier. */
8871 cp_parser_decl_specifier_seq (parser,
8872 CP_PARSER_FLAGS_OPTIONAL,
8873 &decl_specifiers,
8874 &declares_class_or_enum);
8875 /* We no longer need to defer access checks. */
8876 stop_deferring_access_checks ();
8878 /* In a block scope, a valid declaration must always have a
8879 decl-specifier-seq. By not trying to parse declarators, we can
8880 resolve the declaration/expression ambiguity more quickly. */
8881 if (!function_definition_allowed_p
8882 && !decl_specifiers.any_specifiers_p)
8884 cp_parser_error (parser, "expected declaration");
8885 goto done;
8888 /* If the next two tokens are both identifiers, the code is
8889 erroneous. The usual cause of this situation is code like:
8891 T t;
8893 where "T" should name a type -- but does not. */
8894 if (!decl_specifiers.any_type_specifiers_p
8895 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8897 /* If parsing tentatively, we should commit; we really are
8898 looking at a declaration. */
8899 cp_parser_commit_to_tentative_parse (parser);
8900 /* Give up. */
8901 goto done;
8904 /* If we have seen at least one decl-specifier, and the next token
8905 is not a parenthesis, then we must be looking at a declaration.
8906 (After "int (" we might be looking at a functional cast.) */
8907 if (decl_specifiers.any_specifiers_p
8908 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8909 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
8910 && !cp_parser_error_occurred (parser))
8911 cp_parser_commit_to_tentative_parse (parser);
8913 /* Keep going until we hit the `;' at the end of the simple
8914 declaration. */
8915 saw_declarator = false;
8916 while (cp_lexer_next_token_is_not (parser->lexer,
8917 CPP_SEMICOLON))
8919 cp_token *token;
8920 bool function_definition_p;
8921 tree decl;
8923 if (saw_declarator)
8925 /* If we are processing next declarator, coma is expected */
8926 token = cp_lexer_peek_token (parser->lexer);
8927 gcc_assert (token->type == CPP_COMMA);
8928 cp_lexer_consume_token (parser->lexer);
8930 else
8931 saw_declarator = true;
8933 /* Parse the init-declarator. */
8934 decl = cp_parser_init_declarator (parser, &decl_specifiers,
8935 /*checks=*/NULL,
8936 function_definition_allowed_p,
8937 /*member_p=*/false,
8938 declares_class_or_enum,
8939 &function_definition_p);
8940 /* If an error occurred while parsing tentatively, exit quickly.
8941 (That usually happens when in the body of a function; each
8942 statement is treated as a declaration-statement until proven
8943 otherwise.) */
8944 if (cp_parser_error_occurred (parser))
8945 goto done;
8946 /* Handle function definitions specially. */
8947 if (function_definition_p)
8949 /* If the next token is a `,', then we are probably
8950 processing something like:
8952 void f() {}, *p;
8954 which is erroneous. */
8955 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8957 cp_token *token = cp_lexer_peek_token (parser->lexer);
8958 error_at (token->location,
8959 "mixing"
8960 " declarations and function-definitions is forbidden");
8962 /* Otherwise, we're done with the list of declarators. */
8963 else
8965 pop_deferring_access_checks ();
8966 return;
8969 /* The next token should be either a `,' or a `;'. */
8970 token = cp_lexer_peek_token (parser->lexer);
8971 /* If it's a `,', there are more declarators to come. */
8972 if (token->type == CPP_COMMA)
8973 /* will be consumed next time around */;
8974 /* If it's a `;', we are done. */
8975 else if (token->type == CPP_SEMICOLON)
8976 break;
8977 /* Anything else is an error. */
8978 else
8980 /* If we have already issued an error message we don't need
8981 to issue another one. */
8982 if (decl != error_mark_node
8983 || cp_parser_uncommitted_to_tentative_parse_p (parser))
8984 cp_parser_error (parser, "expected %<,%> or %<;%>");
8985 /* Skip tokens until we reach the end of the statement. */
8986 cp_parser_skip_to_end_of_statement (parser);
8987 /* If the next token is now a `;', consume it. */
8988 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8989 cp_lexer_consume_token (parser->lexer);
8990 goto done;
8992 /* After the first time around, a function-definition is not
8993 allowed -- even if it was OK at first. For example:
8995 int i, f() {}
8997 is not valid. */
8998 function_definition_allowed_p = false;
9001 /* Issue an error message if no declarators are present, and the
9002 decl-specifier-seq does not itself declare a class or
9003 enumeration. */
9004 if (!saw_declarator)
9006 if (cp_parser_declares_only_class_p (parser))
9007 shadow_tag (&decl_specifiers);
9008 /* Perform any deferred access checks. */
9009 perform_deferred_access_checks ();
9012 /* Consume the `;'. */
9013 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
9015 done:
9016 pop_deferring_access_checks ();
9019 /* Parse a decl-specifier-seq.
9021 decl-specifier-seq:
9022 decl-specifier-seq [opt] decl-specifier
9024 decl-specifier:
9025 storage-class-specifier
9026 type-specifier
9027 function-specifier
9028 friend
9029 typedef
9031 GNU Extension:
9033 decl-specifier:
9034 attributes
9036 Set *DECL_SPECS to a representation of the decl-specifier-seq.
9038 The parser flags FLAGS is used to control type-specifier parsing.
9040 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
9041 flags:
9043 1: one of the decl-specifiers is an elaborated-type-specifier
9044 (i.e., a type declaration)
9045 2: one of the decl-specifiers is an enum-specifier or a
9046 class-specifier (i.e., a type definition)
9050 static void
9051 cp_parser_decl_specifier_seq (cp_parser* parser,
9052 cp_parser_flags flags,
9053 cp_decl_specifier_seq *decl_specs,
9054 int* declares_class_or_enum)
9056 bool constructor_possible_p = !parser->in_declarator_p;
9057 cp_token *start_token = NULL;
9059 /* Clear DECL_SPECS. */
9060 clear_decl_specs (decl_specs);
9062 /* Assume no class or enumeration type is declared. */
9063 *declares_class_or_enum = 0;
9065 /* Keep reading specifiers until there are no more to read. */
9066 while (true)
9068 bool constructor_p;
9069 bool found_decl_spec;
9070 cp_token *token;
9072 /* Peek at the next token. */
9073 token = cp_lexer_peek_token (parser->lexer);
9075 /* Save the first token of the decl spec list for error
9076 reporting. */
9077 if (!start_token)
9078 start_token = token;
9079 /* Handle attributes. */
9080 if (token->keyword == RID_ATTRIBUTE)
9082 /* Parse the attributes. */
9083 decl_specs->attributes
9084 = chainon (decl_specs->attributes,
9085 cp_parser_attributes_opt (parser));
9086 continue;
9088 /* Assume we will find a decl-specifier keyword. */
9089 found_decl_spec = true;
9090 /* If the next token is an appropriate keyword, we can simply
9091 add it to the list. */
9092 switch (token->keyword)
9094 /* decl-specifier:
9095 friend
9096 constexpr */
9097 case RID_FRIEND:
9098 if (!at_class_scope_p ())
9100 error_at (token->location, "%<friend%> used outside of class");
9101 cp_lexer_purge_token (parser->lexer);
9103 else
9105 ++decl_specs->specs[(int) ds_friend];
9106 /* Consume the token. */
9107 cp_lexer_consume_token (parser->lexer);
9109 break;
9111 case RID_CONSTEXPR:
9112 ++decl_specs->specs[(int) ds_constexpr];
9113 cp_lexer_consume_token (parser->lexer);
9114 break;
9116 /* function-specifier:
9117 inline
9118 virtual
9119 explicit */
9120 case RID_INLINE:
9121 case RID_VIRTUAL:
9122 case RID_EXPLICIT:
9123 cp_parser_function_specifier_opt (parser, decl_specs);
9124 break;
9126 /* decl-specifier:
9127 typedef */
9128 case RID_TYPEDEF:
9129 ++decl_specs->specs[(int) ds_typedef];
9130 /* Consume the token. */
9131 cp_lexer_consume_token (parser->lexer);
9132 /* A constructor declarator cannot appear in a typedef. */
9133 constructor_possible_p = false;
9134 /* The "typedef" keyword can only occur in a declaration; we
9135 may as well commit at this point. */
9136 cp_parser_commit_to_tentative_parse (parser);
9138 if (decl_specs->storage_class != sc_none)
9139 decl_specs->conflicting_specifiers_p = true;
9140 break;
9142 /* storage-class-specifier:
9143 auto
9144 register
9145 static
9146 extern
9147 mutable
9149 GNU Extension:
9150 thread */
9151 case RID_AUTO:
9152 if (cxx_dialect == cxx98)
9154 /* Consume the token. */
9155 cp_lexer_consume_token (parser->lexer);
9157 /* Complain about `auto' as a storage specifier, if
9158 we're complaining about C++0x compatibility. */
9159 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9160 " will change meaning in C++0x; please remove it");
9162 /* Set the storage class anyway. */
9163 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9164 token->location);
9166 else
9167 /* C++0x auto type-specifier. */
9168 found_decl_spec = false;
9169 break;
9171 case RID_REGISTER:
9172 case RID_STATIC:
9173 case RID_EXTERN:
9174 case RID_MUTABLE:
9175 /* Consume the token. */
9176 cp_lexer_consume_token (parser->lexer);
9177 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9178 token->location);
9179 break;
9180 case RID_THREAD:
9181 /* Consume the token. */
9182 cp_lexer_consume_token (parser->lexer);
9183 ++decl_specs->specs[(int) ds_thread];
9184 break;
9186 default:
9187 /* We did not yet find a decl-specifier yet. */
9188 found_decl_spec = false;
9189 break;
9192 /* Constructors are a special case. The `S' in `S()' is not a
9193 decl-specifier; it is the beginning of the declarator. */
9194 constructor_p
9195 = (!found_decl_spec
9196 && constructor_possible_p
9197 && (cp_parser_constructor_declarator_p
9198 (parser, decl_specs->specs[(int) ds_friend] != 0)));
9200 /* If we don't have a DECL_SPEC yet, then we must be looking at
9201 a type-specifier. */
9202 if (!found_decl_spec && !constructor_p)
9204 int decl_spec_declares_class_or_enum;
9205 bool is_cv_qualifier;
9206 tree type_spec;
9208 type_spec
9209 = cp_parser_type_specifier (parser, flags,
9210 decl_specs,
9211 /*is_declaration=*/true,
9212 &decl_spec_declares_class_or_enum,
9213 &is_cv_qualifier);
9214 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
9216 /* If this type-specifier referenced a user-defined type
9217 (a typedef, class-name, etc.), then we can't allow any
9218 more such type-specifiers henceforth.
9220 [dcl.spec]
9222 The longest sequence of decl-specifiers that could
9223 possibly be a type name is taken as the
9224 decl-specifier-seq of a declaration. The sequence shall
9225 be self-consistent as described below.
9227 [dcl.type]
9229 As a general rule, at most one type-specifier is allowed
9230 in the complete decl-specifier-seq of a declaration. The
9231 only exceptions are the following:
9233 -- const or volatile can be combined with any other
9234 type-specifier.
9236 -- signed or unsigned can be combined with char, long,
9237 short, or int.
9239 -- ..
9241 Example:
9243 typedef char* Pc;
9244 void g (const int Pc);
9246 Here, Pc is *not* part of the decl-specifier seq; it's
9247 the declarator. Therefore, once we see a type-specifier
9248 (other than a cv-qualifier), we forbid any additional
9249 user-defined types. We *do* still allow things like `int
9250 int' to be considered a decl-specifier-seq, and issue the
9251 error message later. */
9252 if (type_spec && !is_cv_qualifier)
9253 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
9254 /* A constructor declarator cannot follow a type-specifier. */
9255 if (type_spec)
9257 constructor_possible_p = false;
9258 found_decl_spec = true;
9259 if (!is_cv_qualifier)
9260 decl_specs->any_type_specifiers_p = true;
9264 /* If we still do not have a DECL_SPEC, then there are no more
9265 decl-specifiers. */
9266 if (!found_decl_spec)
9267 break;
9269 decl_specs->any_specifiers_p = true;
9270 /* After we see one decl-specifier, further decl-specifiers are
9271 always optional. */
9272 flags |= CP_PARSER_FLAGS_OPTIONAL;
9275 cp_parser_check_decl_spec (decl_specs, start_token->location);
9277 /* Don't allow a friend specifier with a class definition. */
9278 if (decl_specs->specs[(int) ds_friend] != 0
9279 && (*declares_class_or_enum & 2))
9280 error_at (start_token->location,
9281 "class definition may not be declared a friend");
9284 /* Parse an (optional) storage-class-specifier.
9286 storage-class-specifier:
9287 auto
9288 register
9289 static
9290 extern
9291 mutable
9293 GNU Extension:
9295 storage-class-specifier:
9296 thread
9298 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
9300 static tree
9301 cp_parser_storage_class_specifier_opt (cp_parser* parser)
9303 switch (cp_lexer_peek_token (parser->lexer)->keyword)
9305 case RID_AUTO:
9306 if (cxx_dialect != cxx98)
9307 return NULL_TREE;
9308 /* Fall through for C++98. */
9310 case RID_REGISTER:
9311 case RID_STATIC:
9312 case RID_EXTERN:
9313 case RID_MUTABLE:
9314 case RID_THREAD:
9315 /* Consume the token. */
9316 return cp_lexer_consume_token (parser->lexer)->u.value;
9318 default:
9319 return NULL_TREE;
9323 /* Parse an (optional) function-specifier.
9325 function-specifier:
9326 inline
9327 virtual
9328 explicit
9330 Returns an IDENTIFIER_NODE corresponding to the keyword used.
9331 Updates DECL_SPECS, if it is non-NULL. */
9333 static tree
9334 cp_parser_function_specifier_opt (cp_parser* parser,
9335 cp_decl_specifier_seq *decl_specs)
9337 cp_token *token = cp_lexer_peek_token (parser->lexer);
9338 switch (token->keyword)
9340 case RID_INLINE:
9341 if (decl_specs)
9342 ++decl_specs->specs[(int) ds_inline];
9343 break;
9345 case RID_VIRTUAL:
9346 /* 14.5.2.3 [temp.mem]
9348 A member function template shall not be virtual. */
9349 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
9350 error_at (token->location, "templates may not be %<virtual%>");
9351 else if (decl_specs)
9352 ++decl_specs->specs[(int) ds_virtual];
9353 break;
9355 case RID_EXPLICIT:
9356 if (decl_specs)
9357 ++decl_specs->specs[(int) ds_explicit];
9358 break;
9360 default:
9361 return NULL_TREE;
9364 /* Consume the token. */
9365 return cp_lexer_consume_token (parser->lexer)->u.value;
9368 /* Parse a linkage-specification.
9370 linkage-specification:
9371 extern string-literal { declaration-seq [opt] }
9372 extern string-literal declaration */
9374 static void
9375 cp_parser_linkage_specification (cp_parser* parser)
9377 tree linkage;
9379 /* Look for the `extern' keyword. */
9380 cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
9382 /* Look for the string-literal. */
9383 linkage = cp_parser_string_literal (parser, false, false);
9385 /* Transform the literal into an identifier. If the literal is a
9386 wide-character string, or contains embedded NULs, then we can't
9387 handle it as the user wants. */
9388 if (strlen (TREE_STRING_POINTER (linkage))
9389 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
9391 cp_parser_error (parser, "invalid linkage-specification");
9392 /* Assume C++ linkage. */
9393 linkage = lang_name_cplusplus;
9395 else
9396 linkage = get_identifier (TREE_STRING_POINTER (linkage));
9398 /* We're now using the new linkage. */
9399 push_lang_context (linkage);
9401 /* If the next token is a `{', then we're using the first
9402 production. */
9403 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9405 /* Consume the `{' token. */
9406 cp_lexer_consume_token (parser->lexer);
9407 /* Parse the declarations. */
9408 cp_parser_declaration_seq_opt (parser);
9409 /* Look for the closing `}'. */
9410 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
9412 /* Otherwise, there's just one declaration. */
9413 else
9415 bool saved_in_unbraced_linkage_specification_p;
9417 saved_in_unbraced_linkage_specification_p
9418 = parser->in_unbraced_linkage_specification_p;
9419 parser->in_unbraced_linkage_specification_p = true;
9420 cp_parser_declaration (parser);
9421 parser->in_unbraced_linkage_specification_p
9422 = saved_in_unbraced_linkage_specification_p;
9425 /* We're done with the linkage-specification. */
9426 pop_lang_context ();
9429 /* Parse a static_assert-declaration.
9431 static_assert-declaration:
9432 static_assert ( constant-expression , string-literal ) ;
9434 If MEMBER_P, this static_assert is a class member. */
9436 static void
9437 cp_parser_static_assert(cp_parser *parser, bool member_p)
9439 tree condition;
9440 tree message;
9441 cp_token *token;
9442 location_t saved_loc;
9444 /* Peek at the `static_assert' token so we can keep track of exactly
9445 where the static assertion started. */
9446 token = cp_lexer_peek_token (parser->lexer);
9447 saved_loc = token->location;
9449 /* Look for the `static_assert' keyword. */
9450 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
9451 "%<static_assert%>"))
9452 return;
9454 /* We know we are in a static assertion; commit to any tentative
9455 parse. */
9456 if (cp_parser_parsing_tentatively (parser))
9457 cp_parser_commit_to_tentative_parse (parser);
9459 /* Parse the `(' starting the static assertion condition. */
9460 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
9462 /* Parse the constant-expression. */
9463 condition =
9464 cp_parser_constant_expression (parser,
9465 /*allow_non_constant_p=*/false,
9466 /*non_constant_p=*/NULL);
9468 /* Parse the separating `,'. */
9469 cp_parser_require (parser, CPP_COMMA, "%<,%>");
9471 /* Parse the string-literal message. */
9472 message = cp_parser_string_literal (parser,
9473 /*translate=*/false,
9474 /*wide_ok=*/true);
9476 /* A `)' completes the static assertion. */
9477 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
9478 cp_parser_skip_to_closing_parenthesis (parser,
9479 /*recovering=*/true,
9480 /*or_comma=*/false,
9481 /*consume_paren=*/true);
9483 /* A semicolon terminates the declaration. */
9484 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
9486 /* Complete the static assertion, which may mean either processing
9487 the static assert now or saving it for template instantiation. */
9488 finish_static_assert (condition, message, saved_loc, member_p);
9491 /* Parse a `decltype' type. Returns the type.
9493 simple-type-specifier:
9494 decltype ( expression ) */
9496 static tree
9497 cp_parser_decltype (cp_parser *parser)
9499 tree expr;
9500 bool id_expression_or_member_access_p = false;
9501 const char *saved_message;
9502 bool saved_integral_constant_expression_p;
9503 bool saved_non_integral_constant_expression_p;
9504 cp_token *id_expr_start_token;
9506 /* Look for the `decltype' token. */
9507 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
9508 return error_mark_node;
9510 /* Types cannot be defined in a `decltype' expression. Save away the
9511 old message. */
9512 saved_message = parser->type_definition_forbidden_message;
9514 /* And create the new one. */
9515 parser->type_definition_forbidden_message
9516 = "types may not be defined in %<decltype%> expressions";
9518 /* The restrictions on constant-expressions do not apply inside
9519 decltype expressions. */
9520 saved_integral_constant_expression_p
9521 = parser->integral_constant_expression_p;
9522 saved_non_integral_constant_expression_p
9523 = parser->non_integral_constant_expression_p;
9524 parser->integral_constant_expression_p = false;
9526 /* Do not actually evaluate the expression. */
9527 ++cp_unevaluated_operand;
9529 /* Do not warn about problems with the expression. */
9530 ++c_inhibit_evaluation_warnings;
9532 /* Parse the opening `('. */
9533 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
9534 return error_mark_node;
9536 /* First, try parsing an id-expression. */
9537 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
9538 cp_parser_parse_tentatively (parser);
9539 expr = cp_parser_id_expression (parser,
9540 /*template_keyword_p=*/false,
9541 /*check_dependency_p=*/true,
9542 /*template_p=*/NULL,
9543 /*declarator_p=*/false,
9544 /*optional_p=*/false);
9546 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
9548 bool non_integral_constant_expression_p = false;
9549 tree id_expression = expr;
9550 cp_id_kind idk;
9551 const char *error_msg;
9553 if (TREE_CODE (expr) == IDENTIFIER_NODE)
9554 /* Lookup the name we got back from the id-expression. */
9555 expr = cp_parser_lookup_name (parser, expr,
9556 none_type,
9557 /*is_template=*/false,
9558 /*is_namespace=*/false,
9559 /*check_dependency=*/true,
9560 /*ambiguous_decls=*/NULL,
9561 id_expr_start_token->location);
9563 if (expr
9564 && expr != error_mark_node
9565 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
9566 && TREE_CODE (expr) != TYPE_DECL
9567 && (TREE_CODE (expr) != BIT_NOT_EXPR
9568 || !TYPE_P (TREE_OPERAND (expr, 0)))
9569 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9571 /* Complete lookup of the id-expression. */
9572 expr = (finish_id_expression
9573 (id_expression, expr, parser->scope, &idk,
9574 /*integral_constant_expression_p=*/false,
9575 /*allow_non_integral_constant_expression_p=*/true,
9576 &non_integral_constant_expression_p,
9577 /*template_p=*/false,
9578 /*done=*/true,
9579 /*address_p=*/false,
9580 /*template_arg_p=*/false,
9581 &error_msg,
9582 id_expr_start_token->location));
9584 if (expr == error_mark_node)
9585 /* We found an id-expression, but it was something that we
9586 should not have found. This is an error, not something
9587 we can recover from, so note that we found an
9588 id-expression and we'll recover as gracefully as
9589 possible. */
9590 id_expression_or_member_access_p = true;
9593 if (expr
9594 && expr != error_mark_node
9595 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9596 /* We have an id-expression. */
9597 id_expression_or_member_access_p = true;
9600 if (!id_expression_or_member_access_p)
9602 /* Abort the id-expression parse. */
9603 cp_parser_abort_tentative_parse (parser);
9605 /* Parsing tentatively, again. */
9606 cp_parser_parse_tentatively (parser);
9608 /* Parse a class member access. */
9609 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
9610 /*cast_p=*/false,
9611 /*member_access_only_p=*/true, NULL);
9613 if (expr
9614 && expr != error_mark_node
9615 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9616 /* We have an id-expression. */
9617 id_expression_or_member_access_p = true;
9620 if (id_expression_or_member_access_p)
9621 /* We have parsed the complete id-expression or member access. */
9622 cp_parser_parse_definitely (parser);
9623 else
9625 bool saved_greater_than_is_operator_p;
9627 /* Abort our attempt to parse an id-expression or member access
9628 expression. */
9629 cp_parser_abort_tentative_parse (parser);
9631 /* Within a parenthesized expression, a `>' token is always
9632 the greater-than operator. */
9633 saved_greater_than_is_operator_p
9634 = parser->greater_than_is_operator_p;
9635 parser->greater_than_is_operator_p = true;
9637 /* Parse a full expression. */
9638 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9640 /* The `>' token might be the end of a template-id or
9641 template-parameter-list now. */
9642 parser->greater_than_is_operator_p
9643 = saved_greater_than_is_operator_p;
9646 /* Go back to evaluating expressions. */
9647 --cp_unevaluated_operand;
9648 --c_inhibit_evaluation_warnings;
9650 /* Restore the old message and the integral constant expression
9651 flags. */
9652 parser->type_definition_forbidden_message = saved_message;
9653 parser->integral_constant_expression_p
9654 = saved_integral_constant_expression_p;
9655 parser->non_integral_constant_expression_p
9656 = saved_non_integral_constant_expression_p;
9658 if (expr == error_mark_node)
9660 /* Skip everything up to the closing `)'. */
9661 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9662 /*consume_paren=*/true);
9663 return error_mark_node;
9666 /* Parse to the closing `)'. */
9667 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
9669 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9670 /*consume_paren=*/true);
9671 return error_mark_node;
9674 return finish_decltype_type (expr, id_expression_or_member_access_p);
9677 /* Special member functions [gram.special] */
9679 /* Parse a conversion-function-id.
9681 conversion-function-id:
9682 operator conversion-type-id
9684 Returns an IDENTIFIER_NODE representing the operator. */
9686 static tree
9687 cp_parser_conversion_function_id (cp_parser* parser)
9689 tree type;
9690 tree saved_scope;
9691 tree saved_qualifying_scope;
9692 tree saved_object_scope;
9693 tree pushed_scope = NULL_TREE;
9695 /* Look for the `operator' token. */
9696 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9697 return error_mark_node;
9698 /* When we parse the conversion-type-id, the current scope will be
9699 reset. However, we need that information in able to look up the
9700 conversion function later, so we save it here. */
9701 saved_scope = parser->scope;
9702 saved_qualifying_scope = parser->qualifying_scope;
9703 saved_object_scope = parser->object_scope;
9704 /* We must enter the scope of the class so that the names of
9705 entities declared within the class are available in the
9706 conversion-type-id. For example, consider:
9708 struct S {
9709 typedef int I;
9710 operator I();
9713 S::operator I() { ... }
9715 In order to see that `I' is a type-name in the definition, we
9716 must be in the scope of `S'. */
9717 if (saved_scope)
9718 pushed_scope = push_scope (saved_scope);
9719 /* Parse the conversion-type-id. */
9720 type = cp_parser_conversion_type_id (parser);
9721 /* Leave the scope of the class, if any. */
9722 if (pushed_scope)
9723 pop_scope (pushed_scope);
9724 /* Restore the saved scope. */
9725 parser->scope = saved_scope;
9726 parser->qualifying_scope = saved_qualifying_scope;
9727 parser->object_scope = saved_object_scope;
9728 /* If the TYPE is invalid, indicate failure. */
9729 if (type == error_mark_node)
9730 return error_mark_node;
9731 return mangle_conv_op_name_for_type (type);
9734 /* Parse a conversion-type-id:
9736 conversion-type-id:
9737 type-specifier-seq conversion-declarator [opt]
9739 Returns the TYPE specified. */
9741 static tree
9742 cp_parser_conversion_type_id (cp_parser* parser)
9744 tree attributes;
9745 cp_decl_specifier_seq type_specifiers;
9746 cp_declarator *declarator;
9747 tree type_specified;
9749 /* Parse the attributes. */
9750 attributes = cp_parser_attributes_opt (parser);
9751 /* Parse the type-specifiers. */
9752 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
9753 /*is_trailing_return=*/false,
9754 &type_specifiers);
9755 /* If that didn't work, stop. */
9756 if (type_specifiers.type == error_mark_node)
9757 return error_mark_node;
9758 /* Parse the conversion-declarator. */
9759 declarator = cp_parser_conversion_declarator_opt (parser);
9761 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
9762 /*initialized=*/0, &attributes);
9763 if (attributes)
9764 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
9766 /* Don't give this error when parsing tentatively. This happens to
9767 work because we always parse this definitively once. */
9768 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
9769 && type_uses_auto (type_specified))
9771 error ("invalid use of %<auto%> in conversion operator");
9772 return error_mark_node;
9775 return type_specified;
9778 /* Parse an (optional) conversion-declarator.
9780 conversion-declarator:
9781 ptr-operator conversion-declarator [opt]
9785 static cp_declarator *
9786 cp_parser_conversion_declarator_opt (cp_parser* parser)
9788 enum tree_code code;
9789 tree class_type;
9790 cp_cv_quals cv_quals;
9792 /* We don't know if there's a ptr-operator next, or not. */
9793 cp_parser_parse_tentatively (parser);
9794 /* Try the ptr-operator. */
9795 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
9796 /* If it worked, look for more conversion-declarators. */
9797 if (cp_parser_parse_definitely (parser))
9799 cp_declarator *declarator;
9801 /* Parse another optional declarator. */
9802 declarator = cp_parser_conversion_declarator_opt (parser);
9804 return cp_parser_make_indirect_declarator
9805 (code, class_type, cv_quals, declarator);
9808 return NULL;
9811 /* Parse an (optional) ctor-initializer.
9813 ctor-initializer:
9814 : mem-initializer-list
9816 Returns TRUE iff the ctor-initializer was actually present. */
9818 static bool
9819 cp_parser_ctor_initializer_opt (cp_parser* parser)
9821 /* If the next token is not a `:', then there is no
9822 ctor-initializer. */
9823 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
9825 /* Do default initialization of any bases and members. */
9826 if (DECL_CONSTRUCTOR_P (current_function_decl))
9827 finish_mem_initializers (NULL_TREE);
9829 return false;
9832 /* Consume the `:' token. */
9833 cp_lexer_consume_token (parser->lexer);
9834 /* And the mem-initializer-list. */
9835 cp_parser_mem_initializer_list (parser);
9837 return true;
9840 /* Parse a mem-initializer-list.
9842 mem-initializer-list:
9843 mem-initializer ... [opt]
9844 mem-initializer ... [opt] , mem-initializer-list */
9846 static void
9847 cp_parser_mem_initializer_list (cp_parser* parser)
9849 tree mem_initializer_list = NULL_TREE;
9850 cp_token *token = cp_lexer_peek_token (parser->lexer);
9852 /* Let the semantic analysis code know that we are starting the
9853 mem-initializer-list. */
9854 if (!DECL_CONSTRUCTOR_P (current_function_decl))
9855 error_at (token->location,
9856 "only constructors take base initializers");
9858 /* Loop through the list. */
9859 while (true)
9861 tree mem_initializer;
9863 token = cp_lexer_peek_token (parser->lexer);
9864 /* Parse the mem-initializer. */
9865 mem_initializer = cp_parser_mem_initializer (parser);
9866 /* If the next token is a `...', we're expanding member initializers. */
9867 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9869 /* Consume the `...'. */
9870 cp_lexer_consume_token (parser->lexer);
9872 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9873 can be expanded but members cannot. */
9874 if (mem_initializer != error_mark_node
9875 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9877 error_at (token->location,
9878 "cannot expand initializer for member %<%D%>",
9879 TREE_PURPOSE (mem_initializer));
9880 mem_initializer = error_mark_node;
9883 /* Construct the pack expansion type. */
9884 if (mem_initializer != error_mark_node)
9885 mem_initializer = make_pack_expansion (mem_initializer);
9887 /* Add it to the list, unless it was erroneous. */
9888 if (mem_initializer != error_mark_node)
9890 TREE_CHAIN (mem_initializer) = mem_initializer_list;
9891 mem_initializer_list = mem_initializer;
9893 /* If the next token is not a `,', we're done. */
9894 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9895 break;
9896 /* Consume the `,' token. */
9897 cp_lexer_consume_token (parser->lexer);
9900 /* Perform semantic analysis. */
9901 if (DECL_CONSTRUCTOR_P (current_function_decl))
9902 finish_mem_initializers (mem_initializer_list);
9905 /* Parse a mem-initializer.
9907 mem-initializer:
9908 mem-initializer-id ( expression-list [opt] )
9909 mem-initializer-id braced-init-list
9911 GNU extension:
9913 mem-initializer:
9914 ( expression-list [opt] )
9916 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
9917 class) or FIELD_DECL (for a non-static data member) to initialize;
9918 the TREE_VALUE is the expression-list. An empty initialization
9919 list is represented by void_list_node. */
9921 static tree
9922 cp_parser_mem_initializer (cp_parser* parser)
9924 tree mem_initializer_id;
9925 tree expression_list;
9926 tree member;
9927 cp_token *token = cp_lexer_peek_token (parser->lexer);
9929 /* Find out what is being initialized. */
9930 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9932 permerror (token->location,
9933 "anachronistic old-style base class initializer");
9934 mem_initializer_id = NULL_TREE;
9936 else
9938 mem_initializer_id = cp_parser_mem_initializer_id (parser);
9939 if (mem_initializer_id == error_mark_node)
9940 return mem_initializer_id;
9942 member = expand_member_init (mem_initializer_id);
9943 if (member && !DECL_P (member))
9944 in_base_initializer = 1;
9946 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9948 bool expr_non_constant_p;
9949 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9950 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9951 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9952 expression_list = build_tree_list (NULL_TREE, expression_list);
9954 else
9956 VEC(tree,gc)* vec;
9957 vec = cp_parser_parenthesized_expression_list (parser, false,
9958 /*cast_p=*/false,
9959 /*allow_expansion_p=*/true,
9960 /*non_constant_p=*/NULL);
9961 if (vec == NULL)
9962 return error_mark_node;
9963 expression_list = build_tree_list_vec (vec);
9964 release_tree_vector (vec);
9967 if (expression_list == error_mark_node)
9968 return error_mark_node;
9969 if (!expression_list)
9970 expression_list = void_type_node;
9972 in_base_initializer = 0;
9974 return member ? build_tree_list (member, expression_list) : error_mark_node;
9977 /* Parse a mem-initializer-id.
9979 mem-initializer-id:
9980 :: [opt] nested-name-specifier [opt] class-name
9981 identifier
9983 Returns a TYPE indicating the class to be initializer for the first
9984 production. Returns an IDENTIFIER_NODE indicating the data member
9985 to be initialized for the second production. */
9987 static tree
9988 cp_parser_mem_initializer_id (cp_parser* parser)
9990 bool global_scope_p;
9991 bool nested_name_specifier_p;
9992 bool template_p = false;
9993 tree id;
9995 cp_token *token = cp_lexer_peek_token (parser->lexer);
9997 /* `typename' is not allowed in this context ([temp.res]). */
9998 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
10000 error_at (token->location,
10001 "keyword %<typename%> not allowed in this context (a qualified "
10002 "member initializer is implicitly a type)");
10003 cp_lexer_consume_token (parser->lexer);
10005 /* Look for the optional `::' operator. */
10006 global_scope_p
10007 = (cp_parser_global_scope_opt (parser,
10008 /*current_scope_valid_p=*/false)
10009 != NULL_TREE);
10010 /* Look for the optional nested-name-specifier. The simplest way to
10011 implement:
10013 [temp.res]
10015 The keyword `typename' is not permitted in a base-specifier or
10016 mem-initializer; in these contexts a qualified name that
10017 depends on a template-parameter is implicitly assumed to be a
10018 type name.
10020 is to assume that we have seen the `typename' keyword at this
10021 point. */
10022 nested_name_specifier_p
10023 = (cp_parser_nested_name_specifier_opt (parser,
10024 /*typename_keyword_p=*/true,
10025 /*check_dependency_p=*/true,
10026 /*type_p=*/true,
10027 /*is_declaration=*/true)
10028 != NULL_TREE);
10029 if (nested_name_specifier_p)
10030 template_p = cp_parser_optional_template_keyword (parser);
10031 /* If there is a `::' operator or a nested-name-specifier, then we
10032 are definitely looking for a class-name. */
10033 if (global_scope_p || nested_name_specifier_p)
10034 return cp_parser_class_name (parser,
10035 /*typename_keyword_p=*/true,
10036 /*template_keyword_p=*/template_p,
10037 typename_type,
10038 /*check_dependency_p=*/true,
10039 /*class_head_p=*/false,
10040 /*is_declaration=*/true);
10041 /* Otherwise, we could also be looking for an ordinary identifier. */
10042 cp_parser_parse_tentatively (parser);
10043 /* Try a class-name. */
10044 id = cp_parser_class_name (parser,
10045 /*typename_keyword_p=*/true,
10046 /*template_keyword_p=*/false,
10047 none_type,
10048 /*check_dependency_p=*/true,
10049 /*class_head_p=*/false,
10050 /*is_declaration=*/true);
10051 /* If we found one, we're done. */
10052 if (cp_parser_parse_definitely (parser))
10053 return id;
10054 /* Otherwise, look for an ordinary identifier. */
10055 return cp_parser_identifier (parser);
10058 /* Overloading [gram.over] */
10060 /* Parse an operator-function-id.
10062 operator-function-id:
10063 operator operator
10065 Returns an IDENTIFIER_NODE for the operator which is a
10066 human-readable spelling of the identifier, e.g., `operator +'. */
10068 static tree
10069 cp_parser_operator_function_id (cp_parser* parser)
10071 /* Look for the `operator' keyword. */
10072 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
10073 return error_mark_node;
10074 /* And then the name of the operator itself. */
10075 return cp_parser_operator (parser);
10078 /* Parse an operator.
10080 operator:
10081 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10082 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10083 || ++ -- , ->* -> () []
10085 GNU Extensions:
10087 operator:
10088 <? >? <?= >?=
10090 Returns an IDENTIFIER_NODE for the operator which is a
10091 human-readable spelling of the identifier, e.g., `operator +'. */
10093 static tree
10094 cp_parser_operator (cp_parser* parser)
10096 tree id = NULL_TREE;
10097 cp_token *token;
10099 /* Peek at the next token. */
10100 token = cp_lexer_peek_token (parser->lexer);
10101 /* Figure out which operator we have. */
10102 switch (token->type)
10104 case CPP_KEYWORD:
10106 enum tree_code op;
10108 /* The keyword should be either `new' or `delete'. */
10109 if (token->keyword == RID_NEW)
10110 op = NEW_EXPR;
10111 else if (token->keyword == RID_DELETE)
10112 op = DELETE_EXPR;
10113 else
10114 break;
10116 /* Consume the `new' or `delete' token. */
10117 cp_lexer_consume_token (parser->lexer);
10119 /* Peek at the next token. */
10120 token = cp_lexer_peek_token (parser->lexer);
10121 /* If it's a `[' token then this is the array variant of the
10122 operator. */
10123 if (token->type == CPP_OPEN_SQUARE)
10125 /* Consume the `[' token. */
10126 cp_lexer_consume_token (parser->lexer);
10127 /* Look for the `]' token. */
10128 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
10129 id = ansi_opname (op == NEW_EXPR
10130 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10132 /* Otherwise, we have the non-array variant. */
10133 else
10134 id = ansi_opname (op);
10136 return id;
10139 case CPP_PLUS:
10140 id = ansi_opname (PLUS_EXPR);
10141 break;
10143 case CPP_MINUS:
10144 id = ansi_opname (MINUS_EXPR);
10145 break;
10147 case CPP_MULT:
10148 id = ansi_opname (MULT_EXPR);
10149 break;
10151 case CPP_DIV:
10152 id = ansi_opname (TRUNC_DIV_EXPR);
10153 break;
10155 case CPP_MOD:
10156 id = ansi_opname (TRUNC_MOD_EXPR);
10157 break;
10159 case CPP_XOR:
10160 id = ansi_opname (BIT_XOR_EXPR);
10161 break;
10163 case CPP_AND:
10164 id = ansi_opname (BIT_AND_EXPR);
10165 break;
10167 case CPP_OR:
10168 id = ansi_opname (BIT_IOR_EXPR);
10169 break;
10171 case CPP_COMPL:
10172 id = ansi_opname (BIT_NOT_EXPR);
10173 break;
10175 case CPP_NOT:
10176 id = ansi_opname (TRUTH_NOT_EXPR);
10177 break;
10179 case CPP_EQ:
10180 id = ansi_assopname (NOP_EXPR);
10181 break;
10183 case CPP_LESS:
10184 id = ansi_opname (LT_EXPR);
10185 break;
10187 case CPP_GREATER:
10188 id = ansi_opname (GT_EXPR);
10189 break;
10191 case CPP_PLUS_EQ:
10192 id = ansi_assopname (PLUS_EXPR);
10193 break;
10195 case CPP_MINUS_EQ:
10196 id = ansi_assopname (MINUS_EXPR);
10197 break;
10199 case CPP_MULT_EQ:
10200 id = ansi_assopname (MULT_EXPR);
10201 break;
10203 case CPP_DIV_EQ:
10204 id = ansi_assopname (TRUNC_DIV_EXPR);
10205 break;
10207 case CPP_MOD_EQ:
10208 id = ansi_assopname (TRUNC_MOD_EXPR);
10209 break;
10211 case CPP_XOR_EQ:
10212 id = ansi_assopname (BIT_XOR_EXPR);
10213 break;
10215 case CPP_AND_EQ:
10216 id = ansi_assopname (BIT_AND_EXPR);
10217 break;
10219 case CPP_OR_EQ:
10220 id = ansi_assopname (BIT_IOR_EXPR);
10221 break;
10223 case CPP_LSHIFT:
10224 id = ansi_opname (LSHIFT_EXPR);
10225 break;
10227 case CPP_RSHIFT:
10228 id = ansi_opname (RSHIFT_EXPR);
10229 break;
10231 case CPP_LSHIFT_EQ:
10232 id = ansi_assopname (LSHIFT_EXPR);
10233 break;
10235 case CPP_RSHIFT_EQ:
10236 id = ansi_assopname (RSHIFT_EXPR);
10237 break;
10239 case CPP_EQ_EQ:
10240 id = ansi_opname (EQ_EXPR);
10241 break;
10243 case CPP_NOT_EQ:
10244 id = ansi_opname (NE_EXPR);
10245 break;
10247 case CPP_LESS_EQ:
10248 id = ansi_opname (LE_EXPR);
10249 break;
10251 case CPP_GREATER_EQ:
10252 id = ansi_opname (GE_EXPR);
10253 break;
10255 case CPP_AND_AND:
10256 id = ansi_opname (TRUTH_ANDIF_EXPR);
10257 break;
10259 case CPP_OR_OR:
10260 id = ansi_opname (TRUTH_ORIF_EXPR);
10261 break;
10263 case CPP_PLUS_PLUS:
10264 id = ansi_opname (POSTINCREMENT_EXPR);
10265 break;
10267 case CPP_MINUS_MINUS:
10268 id = ansi_opname (PREDECREMENT_EXPR);
10269 break;
10271 case CPP_COMMA:
10272 id = ansi_opname (COMPOUND_EXPR);
10273 break;
10275 case CPP_DEREF_STAR:
10276 id = ansi_opname (MEMBER_REF);
10277 break;
10279 case CPP_DEREF:
10280 id = ansi_opname (COMPONENT_REF);
10281 break;
10283 case CPP_OPEN_PAREN:
10284 /* Consume the `('. */
10285 cp_lexer_consume_token (parser->lexer);
10286 /* Look for the matching `)'. */
10287 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
10288 return ansi_opname (CALL_EXPR);
10290 case CPP_OPEN_SQUARE:
10291 /* Consume the `['. */
10292 cp_lexer_consume_token (parser->lexer);
10293 /* Look for the matching `]'. */
10294 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
10295 return ansi_opname (ARRAY_REF);
10297 default:
10298 /* Anything else is an error. */
10299 break;
10302 /* If we have selected an identifier, we need to consume the
10303 operator token. */
10304 if (id)
10305 cp_lexer_consume_token (parser->lexer);
10306 /* Otherwise, no valid operator name was present. */
10307 else
10309 cp_parser_error (parser, "expected operator");
10310 id = error_mark_node;
10313 return id;
10316 /* Parse a template-declaration.
10318 template-declaration:
10319 export [opt] template < template-parameter-list > declaration
10321 If MEMBER_P is TRUE, this template-declaration occurs within a
10322 class-specifier.
10324 The grammar rule given by the standard isn't correct. What
10325 is really meant is:
10327 template-declaration:
10328 export [opt] template-parameter-list-seq
10329 decl-specifier-seq [opt] init-declarator [opt] ;
10330 export [opt] template-parameter-list-seq
10331 function-definition
10333 template-parameter-list-seq:
10334 template-parameter-list-seq [opt]
10335 template < template-parameter-list > */
10337 static void
10338 cp_parser_template_declaration (cp_parser* parser, bool member_p)
10340 /* Check for `export'. */
10341 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
10343 /* Consume the `export' token. */
10344 cp_lexer_consume_token (parser->lexer);
10345 /* Warn that we do not support `export'. */
10346 warning (0, "keyword %<export%> not implemented, and will be ignored");
10349 cp_parser_template_declaration_after_export (parser, member_p);
10352 /* Parse a template-parameter-list.
10354 template-parameter-list:
10355 template-parameter
10356 template-parameter-list , template-parameter
10358 Returns a TREE_LIST. Each node represents a template parameter.
10359 The nodes are connected via their TREE_CHAINs. */
10361 static tree
10362 cp_parser_template_parameter_list (cp_parser* parser)
10364 tree parameter_list = NULL_TREE;
10366 begin_template_parm_list ();
10367 while (true)
10369 tree parameter;
10370 bool is_non_type;
10371 bool is_parameter_pack;
10372 location_t parm_loc;
10374 /* Parse the template-parameter. */
10375 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
10376 parameter = cp_parser_template_parameter (parser,
10377 &is_non_type,
10378 &is_parameter_pack);
10379 /* Add it to the list. */
10380 if (parameter != error_mark_node)
10381 parameter_list = process_template_parm (parameter_list,
10382 parm_loc,
10383 parameter,
10384 is_non_type,
10385 is_parameter_pack);
10386 else
10388 tree err_parm = build_tree_list (parameter, parameter);
10389 TREE_VALUE (err_parm) = error_mark_node;
10390 parameter_list = chainon (parameter_list, err_parm);
10393 /* If the next token is not a `,', we're done. */
10394 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10395 break;
10396 /* Otherwise, consume the `,' token. */
10397 cp_lexer_consume_token (parser->lexer);
10400 return end_template_parm_list (parameter_list);
10403 /* Parse a template-parameter.
10405 template-parameter:
10406 type-parameter
10407 parameter-declaration
10409 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
10410 the parameter. The TREE_PURPOSE is the default value, if any.
10411 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
10412 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
10413 set to true iff this parameter is a parameter pack. */
10415 static tree
10416 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
10417 bool *is_parameter_pack)
10419 cp_token *token;
10420 cp_parameter_declarator *parameter_declarator;
10421 cp_declarator *id_declarator;
10422 tree parm;
10424 /* Assume it is a type parameter or a template parameter. */
10425 *is_non_type = false;
10426 /* Assume it not a parameter pack. */
10427 *is_parameter_pack = false;
10428 /* Peek at the next token. */
10429 token = cp_lexer_peek_token (parser->lexer);
10430 /* If it is `class' or `template', we have a type-parameter. */
10431 if (token->keyword == RID_TEMPLATE)
10432 return cp_parser_type_parameter (parser, is_parameter_pack);
10433 /* If it is `class' or `typename' we do not know yet whether it is a
10434 type parameter or a non-type parameter. Consider:
10436 template <typename T, typename T::X X> ...
10440 template <class C, class D*> ...
10442 Here, the first parameter is a type parameter, and the second is
10443 a non-type parameter. We can tell by looking at the token after
10444 the identifier -- if it is a `,', `=', or `>' then we have a type
10445 parameter. */
10446 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
10448 /* Peek at the token after `class' or `typename'. */
10449 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10450 /* If it's an ellipsis, we have a template type parameter
10451 pack. */
10452 if (token->type == CPP_ELLIPSIS)
10453 return cp_parser_type_parameter (parser, is_parameter_pack);
10454 /* If it's an identifier, skip it. */
10455 if (token->type == CPP_NAME)
10456 token = cp_lexer_peek_nth_token (parser->lexer, 3);
10457 /* Now, see if the token looks like the end of a template
10458 parameter. */
10459 if (token->type == CPP_COMMA
10460 || token->type == CPP_EQ
10461 || token->type == CPP_GREATER)
10462 return cp_parser_type_parameter (parser, is_parameter_pack);
10465 /* Otherwise, it is a non-type parameter.
10467 [temp.param]
10469 When parsing a default template-argument for a non-type
10470 template-parameter, the first non-nested `>' is taken as the end
10471 of the template parameter-list rather than a greater-than
10472 operator. */
10473 *is_non_type = true;
10474 parameter_declarator
10475 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
10476 /*parenthesized_p=*/NULL);
10478 /* If the parameter declaration is marked as a parameter pack, set
10479 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
10480 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
10481 grokdeclarator. */
10482 if (parameter_declarator
10483 && parameter_declarator->declarator
10484 && parameter_declarator->declarator->parameter_pack_p)
10486 *is_parameter_pack = true;
10487 parameter_declarator->declarator->parameter_pack_p = false;
10490 /* If the next token is an ellipsis, and we don't already have it
10491 marked as a parameter pack, then we have a parameter pack (that
10492 has no declarator). */
10493 if (!*is_parameter_pack
10494 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
10495 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
10497 /* Consume the `...'. */
10498 cp_lexer_consume_token (parser->lexer);
10499 maybe_warn_variadic_templates ();
10501 *is_parameter_pack = true;
10503 /* We might end up with a pack expansion as the type of the non-type
10504 template parameter, in which case this is a non-type template
10505 parameter pack. */
10506 else if (parameter_declarator
10507 && parameter_declarator->decl_specifiers.type
10508 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
10510 *is_parameter_pack = true;
10511 parameter_declarator->decl_specifiers.type =
10512 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
10515 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10517 /* Parameter packs cannot have default arguments. However, a
10518 user may try to do so, so we'll parse them and give an
10519 appropriate diagnostic here. */
10521 /* Consume the `='. */
10522 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
10523 cp_lexer_consume_token (parser->lexer);
10525 /* Find the name of the parameter pack. */
10526 id_declarator = parameter_declarator->declarator;
10527 while (id_declarator && id_declarator->kind != cdk_id)
10528 id_declarator = id_declarator->declarator;
10530 if (id_declarator && id_declarator->kind == cdk_id)
10531 error_at (start_token->location,
10532 "template parameter pack %qD cannot have a default argument",
10533 id_declarator->u.id.unqualified_name);
10534 else
10535 error_at (start_token->location,
10536 "template parameter pack cannot have a default argument");
10538 /* Parse the default argument, but throw away the result. */
10539 cp_parser_default_argument (parser, /*template_parm_p=*/true);
10542 parm = grokdeclarator (parameter_declarator->declarator,
10543 &parameter_declarator->decl_specifiers,
10544 TPARM, /*initialized=*/0,
10545 /*attrlist=*/NULL);
10546 if (parm == error_mark_node)
10547 return error_mark_node;
10549 return build_tree_list (parameter_declarator->default_argument, parm);
10552 /* Parse a type-parameter.
10554 type-parameter:
10555 class identifier [opt]
10556 class identifier [opt] = type-id
10557 typename identifier [opt]
10558 typename identifier [opt] = type-id
10559 template < template-parameter-list > class identifier [opt]
10560 template < template-parameter-list > class identifier [opt]
10561 = id-expression
10563 GNU Extension (variadic templates):
10565 type-parameter:
10566 class ... identifier [opt]
10567 typename ... identifier [opt]
10569 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
10570 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
10571 the declaration of the parameter.
10573 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
10575 static tree
10576 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
10578 cp_token *token;
10579 tree parameter;
10581 /* Look for a keyword to tell us what kind of parameter this is. */
10582 token = cp_parser_require (parser, CPP_KEYWORD,
10583 "%<class%>, %<typename%>, or %<template%>");
10584 if (!token)
10585 return error_mark_node;
10587 switch (token->keyword)
10589 case RID_CLASS:
10590 case RID_TYPENAME:
10592 tree identifier;
10593 tree default_argument;
10595 /* If the next token is an ellipsis, we have a template
10596 argument pack. */
10597 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10599 /* Consume the `...' token. */
10600 cp_lexer_consume_token (parser->lexer);
10601 maybe_warn_variadic_templates ();
10603 *is_parameter_pack = true;
10606 /* If the next token is an identifier, then it names the
10607 parameter. */
10608 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10609 identifier = cp_parser_identifier (parser);
10610 else
10611 identifier = NULL_TREE;
10613 /* Create the parameter. */
10614 parameter = finish_template_type_parm (class_type_node, identifier);
10616 /* If the next token is an `=', we have a default argument. */
10617 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10619 /* Consume the `=' token. */
10620 cp_lexer_consume_token (parser->lexer);
10621 /* Parse the default-argument. */
10622 push_deferring_access_checks (dk_no_deferred);
10623 default_argument = cp_parser_type_id (parser);
10625 /* Template parameter packs cannot have default
10626 arguments. */
10627 if (*is_parameter_pack)
10629 if (identifier)
10630 error_at (token->location,
10631 "template parameter pack %qD cannot have a "
10632 "default argument", identifier);
10633 else
10634 error_at (token->location,
10635 "template parameter packs cannot have "
10636 "default arguments");
10637 default_argument = NULL_TREE;
10639 pop_deferring_access_checks ();
10641 else
10642 default_argument = NULL_TREE;
10644 /* Create the combined representation of the parameter and the
10645 default argument. */
10646 parameter = build_tree_list (default_argument, parameter);
10648 break;
10650 case RID_TEMPLATE:
10652 tree parameter_list;
10653 tree identifier;
10654 tree default_argument;
10656 /* Look for the `<'. */
10657 cp_parser_require (parser, CPP_LESS, "%<<%>");
10658 /* Parse the template-parameter-list. */
10659 parameter_list = cp_parser_template_parameter_list (parser);
10660 /* Look for the `>'. */
10661 cp_parser_require (parser, CPP_GREATER, "%<>%>");
10662 /* Look for the `class' keyword. */
10663 cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
10664 /* If the next token is an ellipsis, we have a template
10665 argument pack. */
10666 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10668 /* Consume the `...' token. */
10669 cp_lexer_consume_token (parser->lexer);
10670 maybe_warn_variadic_templates ();
10672 *is_parameter_pack = true;
10674 /* If the next token is an `=', then there is a
10675 default-argument. If the next token is a `>', we are at
10676 the end of the parameter-list. If the next token is a `,',
10677 then we are at the end of this parameter. */
10678 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10679 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
10680 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10682 identifier = cp_parser_identifier (parser);
10683 /* Treat invalid names as if the parameter were nameless. */
10684 if (identifier == error_mark_node)
10685 identifier = NULL_TREE;
10687 else
10688 identifier = NULL_TREE;
10690 /* Create the template parameter. */
10691 parameter = finish_template_template_parm (class_type_node,
10692 identifier);
10694 /* If the next token is an `=', then there is a
10695 default-argument. */
10696 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10698 bool is_template;
10700 /* Consume the `='. */
10701 cp_lexer_consume_token (parser->lexer);
10702 /* Parse the id-expression. */
10703 push_deferring_access_checks (dk_no_deferred);
10704 /* save token before parsing the id-expression, for error
10705 reporting */
10706 token = cp_lexer_peek_token (parser->lexer);
10707 default_argument
10708 = cp_parser_id_expression (parser,
10709 /*template_keyword_p=*/false,
10710 /*check_dependency_p=*/true,
10711 /*template_p=*/&is_template,
10712 /*declarator_p=*/false,
10713 /*optional_p=*/false);
10714 if (TREE_CODE (default_argument) == TYPE_DECL)
10715 /* If the id-expression was a template-id that refers to
10716 a template-class, we already have the declaration here,
10717 so no further lookup is needed. */
10719 else
10720 /* Look up the name. */
10721 default_argument
10722 = cp_parser_lookup_name (parser, default_argument,
10723 none_type,
10724 /*is_template=*/is_template,
10725 /*is_namespace=*/false,
10726 /*check_dependency=*/true,
10727 /*ambiguous_decls=*/NULL,
10728 token->location);
10729 /* See if the default argument is valid. */
10730 default_argument
10731 = check_template_template_default_arg (default_argument);
10733 /* Template parameter packs cannot have default
10734 arguments. */
10735 if (*is_parameter_pack)
10737 if (identifier)
10738 error_at (token->location,
10739 "template parameter pack %qD cannot "
10740 "have a default argument",
10741 identifier);
10742 else
10743 error_at (token->location, "template parameter packs cannot "
10744 "have default arguments");
10745 default_argument = NULL_TREE;
10747 pop_deferring_access_checks ();
10749 else
10750 default_argument = NULL_TREE;
10752 /* Create the combined representation of the parameter and the
10753 default argument. */
10754 parameter = build_tree_list (default_argument, parameter);
10756 break;
10758 default:
10759 gcc_unreachable ();
10760 break;
10763 return parameter;
10766 /* Parse a template-id.
10768 template-id:
10769 template-name < template-argument-list [opt] >
10771 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
10772 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
10773 returned. Otherwise, if the template-name names a function, or set
10774 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
10775 names a class, returns a TYPE_DECL for the specialization.
10777 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
10778 uninstantiated templates. */
10780 static tree
10781 cp_parser_template_id (cp_parser *parser,
10782 bool template_keyword_p,
10783 bool check_dependency_p,
10784 bool is_declaration)
10786 int i;
10787 tree templ;
10788 tree arguments;
10789 tree template_id;
10790 cp_token_position start_of_id = 0;
10791 deferred_access_check *chk;
10792 VEC (deferred_access_check,gc) *access_check;
10793 cp_token *next_token = NULL, *next_token_2 = NULL, *token = NULL;
10794 bool is_identifier;
10796 /* If the next token corresponds to a template-id, there is no need
10797 to reparse it. */
10798 next_token = cp_lexer_peek_token (parser->lexer);
10799 if (next_token->type == CPP_TEMPLATE_ID)
10801 struct tree_check *check_value;
10803 /* Get the stored value. */
10804 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
10805 /* Perform any access checks that were deferred. */
10806 access_check = check_value->checks;
10807 if (access_check)
10809 for (i = 0 ;
10810 VEC_iterate (deferred_access_check, access_check, i, chk) ;
10811 ++i)
10813 perform_or_defer_access_check (chk->binfo,
10814 chk->decl,
10815 chk->diag_decl);
10818 /* Return the stored value. */
10819 return check_value->value;
10822 /* Avoid performing name lookup if there is no possibility of
10823 finding a template-id. */
10824 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
10825 || (next_token->type == CPP_NAME
10826 && !cp_parser_nth_token_starts_template_argument_list_p
10827 (parser, 2)))
10829 cp_parser_error (parser, "expected template-id");
10830 return error_mark_node;
10833 /* Remember where the template-id starts. */
10834 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
10835 start_of_id = cp_lexer_token_position (parser->lexer, false);
10837 push_deferring_access_checks (dk_deferred);
10839 /* Parse the template-name. */
10840 is_identifier = false;
10841 token = cp_lexer_peek_token (parser->lexer);
10842 templ = cp_parser_template_name (parser, template_keyword_p,
10843 check_dependency_p,
10844 is_declaration,
10845 &is_identifier);
10846 if (templ == error_mark_node || is_identifier)
10848 pop_deferring_access_checks ();
10849 return templ;
10852 /* If we find the sequence `[:' after a template-name, it's probably
10853 a digraph-typo for `< ::'. Substitute the tokens and check if we can
10854 parse correctly the argument list. */
10855 next_token = cp_lexer_peek_token (parser->lexer);
10856 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10857 if (next_token->type == CPP_OPEN_SQUARE
10858 && next_token->flags & DIGRAPH
10859 && next_token_2->type == CPP_COLON
10860 && !(next_token_2->flags & PREV_WHITE))
10862 cp_parser_parse_tentatively (parser);
10863 /* Change `:' into `::'. */
10864 next_token_2->type = CPP_SCOPE;
10865 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
10866 CPP_LESS. */
10867 cp_lexer_consume_token (parser->lexer);
10869 /* Parse the arguments. */
10870 arguments = cp_parser_enclosed_template_argument_list (parser);
10871 if (!cp_parser_parse_definitely (parser))
10873 /* If we couldn't parse an argument list, then we revert our changes
10874 and return simply an error. Maybe this is not a template-id
10875 after all. */
10876 next_token_2->type = CPP_COLON;
10877 cp_parser_error (parser, "expected %<<%>");
10878 pop_deferring_access_checks ();
10879 return error_mark_node;
10881 /* Otherwise, emit an error about the invalid digraph, but continue
10882 parsing because we got our argument list. */
10883 if (permerror (next_token->location,
10884 "%<<::%> cannot begin a template-argument list"))
10886 static bool hint = false;
10887 inform (next_token->location,
10888 "%<<:%> is an alternate spelling for %<[%>."
10889 " Insert whitespace between %<<%> and %<::%>");
10890 if (!hint && !flag_permissive)
10892 inform (next_token->location, "(if you use %<-fpermissive%>"
10893 " G++ will accept your code)");
10894 hint = true;
10898 else
10900 /* Look for the `<' that starts the template-argument-list. */
10901 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10903 pop_deferring_access_checks ();
10904 return error_mark_node;
10906 /* Parse the arguments. */
10907 arguments = cp_parser_enclosed_template_argument_list (parser);
10910 /* Build a representation of the specialization. */
10911 if (TREE_CODE (templ) == IDENTIFIER_NODE)
10912 template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10913 else if (DECL_CLASS_TEMPLATE_P (templ)
10914 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10916 bool entering_scope;
10917 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10918 template (rather than some instantiation thereof) only if
10919 is not nested within some other construct. For example, in
10920 "template <typename T> void f(T) { A<T>::", A<T> is just an
10921 instantiation of A. */
10922 entering_scope = (template_parm_scope_p ()
10923 && cp_lexer_next_token_is (parser->lexer,
10924 CPP_SCOPE));
10925 template_id
10926 = finish_template_type (templ, arguments, entering_scope);
10928 else
10930 /* If it's not a class-template or a template-template, it should be
10931 a function-template. */
10932 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10933 || TREE_CODE (templ) == OVERLOAD
10934 || BASELINK_P (templ)));
10936 template_id = lookup_template_function (templ, arguments);
10939 /* If parsing tentatively, replace the sequence of tokens that makes
10940 up the template-id with a CPP_TEMPLATE_ID token. That way,
10941 should we re-parse the token stream, we will not have to repeat
10942 the effort required to do the parse, nor will we issue duplicate
10943 error messages about problems during instantiation of the
10944 template. */
10945 if (start_of_id)
10947 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10949 /* Reset the contents of the START_OF_ID token. */
10950 token->type = CPP_TEMPLATE_ID;
10951 /* Retrieve any deferred checks. Do not pop this access checks yet
10952 so the memory will not be reclaimed during token replacing below. */
10953 token->u.tree_check_value = GGC_CNEW (struct tree_check);
10954 token->u.tree_check_value->value = template_id;
10955 token->u.tree_check_value->checks = get_deferred_access_checks ();
10956 token->keyword = RID_MAX;
10958 /* Purge all subsequent tokens. */
10959 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10961 /* ??? Can we actually assume that, if template_id ==
10962 error_mark_node, we will have issued a diagnostic to the
10963 user, as opposed to simply marking the tentative parse as
10964 failed? */
10965 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10966 error_at (token->location, "parse error in template argument list");
10969 pop_deferring_access_checks ();
10970 return template_id;
10973 /* Parse a template-name.
10975 template-name:
10976 identifier
10978 The standard should actually say:
10980 template-name:
10981 identifier
10982 operator-function-id
10984 A defect report has been filed about this issue.
10986 A conversion-function-id cannot be a template name because they cannot
10987 be part of a template-id. In fact, looking at this code:
10989 a.operator K<int>()
10991 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10992 It is impossible to call a templated conversion-function-id with an
10993 explicit argument list, since the only allowed template parameter is
10994 the type to which it is converting.
10996 If TEMPLATE_KEYWORD_P is true, then we have just seen the
10997 `template' keyword, in a construction like:
10999 T::template f<3>()
11001 In that case `f' is taken to be a template-name, even though there
11002 is no way of knowing for sure.
11004 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
11005 name refers to a set of overloaded functions, at least one of which
11006 is a template, or an IDENTIFIER_NODE with the name of the template,
11007 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
11008 names are looked up inside uninstantiated templates. */
11010 static tree
11011 cp_parser_template_name (cp_parser* parser,
11012 bool template_keyword_p,
11013 bool check_dependency_p,
11014 bool is_declaration,
11015 bool *is_identifier)
11017 tree identifier;
11018 tree decl;
11019 tree fns;
11020 cp_token *token = cp_lexer_peek_token (parser->lexer);
11022 /* If the next token is `operator', then we have either an
11023 operator-function-id or a conversion-function-id. */
11024 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
11026 /* We don't know whether we're looking at an
11027 operator-function-id or a conversion-function-id. */
11028 cp_parser_parse_tentatively (parser);
11029 /* Try an operator-function-id. */
11030 identifier = cp_parser_operator_function_id (parser);
11031 /* If that didn't work, try a conversion-function-id. */
11032 if (!cp_parser_parse_definitely (parser))
11034 cp_parser_error (parser, "expected template-name");
11035 return error_mark_node;
11038 /* Look for the identifier. */
11039 else
11040 identifier = cp_parser_identifier (parser);
11042 /* If we didn't find an identifier, we don't have a template-id. */
11043 if (identifier == error_mark_node)
11044 return error_mark_node;
11046 /* If the name immediately followed the `template' keyword, then it
11047 is a template-name. However, if the next token is not `<', then
11048 we do not treat it as a template-name, since it is not being used
11049 as part of a template-id. This enables us to handle constructs
11050 like:
11052 template <typename T> struct S { S(); };
11053 template <typename T> S<T>::S();
11055 correctly. We would treat `S' as a template -- if it were `S<T>'
11056 -- but we do not if there is no `<'. */
11058 if (processing_template_decl
11059 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
11061 /* In a declaration, in a dependent context, we pretend that the
11062 "template" keyword was present in order to improve error
11063 recovery. For example, given:
11065 template <typename T> void f(T::X<int>);
11067 we want to treat "X<int>" as a template-id. */
11068 if (is_declaration
11069 && !template_keyword_p
11070 && parser->scope && TYPE_P (parser->scope)
11071 && check_dependency_p
11072 && dependent_scope_p (parser->scope)
11073 /* Do not do this for dtors (or ctors), since they never
11074 need the template keyword before their name. */
11075 && !constructor_name_p (identifier, parser->scope))
11077 cp_token_position start = 0;
11079 /* Explain what went wrong. */
11080 error_at (token->location, "non-template %qD used as template",
11081 identifier);
11082 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
11083 parser->scope, identifier);
11084 /* If parsing tentatively, find the location of the "<" token. */
11085 if (cp_parser_simulate_error (parser))
11086 start = cp_lexer_token_position (parser->lexer, true);
11087 /* Parse the template arguments so that we can issue error
11088 messages about them. */
11089 cp_lexer_consume_token (parser->lexer);
11090 cp_parser_enclosed_template_argument_list (parser);
11091 /* Skip tokens until we find a good place from which to
11092 continue parsing. */
11093 cp_parser_skip_to_closing_parenthesis (parser,
11094 /*recovering=*/true,
11095 /*or_comma=*/true,
11096 /*consume_paren=*/false);
11097 /* If parsing tentatively, permanently remove the
11098 template argument list. That will prevent duplicate
11099 error messages from being issued about the missing
11100 "template" keyword. */
11101 if (start)
11102 cp_lexer_purge_tokens_after (parser->lexer, start);
11103 if (is_identifier)
11104 *is_identifier = true;
11105 return identifier;
11108 /* If the "template" keyword is present, then there is generally
11109 no point in doing name-lookup, so we just return IDENTIFIER.
11110 But, if the qualifying scope is non-dependent then we can
11111 (and must) do name-lookup normally. */
11112 if (template_keyword_p
11113 && (!parser->scope
11114 || (TYPE_P (parser->scope)
11115 && dependent_type_p (parser->scope))))
11116 return identifier;
11119 /* Look up the name. */
11120 decl = cp_parser_lookup_name (parser, identifier,
11121 none_type,
11122 /*is_template=*/true,
11123 /*is_namespace=*/false,
11124 check_dependency_p,
11125 /*ambiguous_decls=*/NULL,
11126 token->location);
11128 /* If DECL is a template, then the name was a template-name. */
11129 if (TREE_CODE (decl) == TEMPLATE_DECL)
11131 else
11133 tree fn = NULL_TREE;
11135 /* The standard does not explicitly indicate whether a name that
11136 names a set of overloaded declarations, some of which are
11137 templates, is a template-name. However, such a name should
11138 be a template-name; otherwise, there is no way to form a
11139 template-id for the overloaded templates. */
11140 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11141 if (TREE_CODE (fns) == OVERLOAD)
11142 for (fn = fns; fn; fn = OVL_NEXT (fn))
11143 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11144 break;
11146 if (!fn)
11148 /* The name does not name a template. */
11149 cp_parser_error (parser, "expected template-name");
11150 return error_mark_node;
11154 /* If DECL is dependent, and refers to a function, then just return
11155 its name; we will look it up again during template instantiation. */
11156 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11158 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
11159 if (TYPE_P (scope) && dependent_type_p (scope))
11160 return identifier;
11163 return decl;
11166 /* Parse a template-argument-list.
11168 template-argument-list:
11169 template-argument ... [opt]
11170 template-argument-list , template-argument ... [opt]
11172 Returns a TREE_VEC containing the arguments. */
11174 static tree
11175 cp_parser_template_argument_list (cp_parser* parser)
11177 tree fixed_args[10];
11178 unsigned n_args = 0;
11179 unsigned alloced = 10;
11180 tree *arg_ary = fixed_args;
11181 tree vec;
11182 bool saved_in_template_argument_list_p;
11183 bool saved_ice_p;
11184 bool saved_non_ice_p;
11186 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
11187 parser->in_template_argument_list_p = true;
11188 /* Even if the template-id appears in an integral
11189 constant-expression, the contents of the argument list do
11190 not. */
11191 saved_ice_p = parser->integral_constant_expression_p;
11192 parser->integral_constant_expression_p = false;
11193 saved_non_ice_p = parser->non_integral_constant_expression_p;
11194 parser->non_integral_constant_expression_p = false;
11195 /* Parse the arguments. */
11198 tree argument;
11200 if (n_args)
11201 /* Consume the comma. */
11202 cp_lexer_consume_token (parser->lexer);
11204 /* Parse the template-argument. */
11205 argument = cp_parser_template_argument (parser);
11207 /* If the next token is an ellipsis, we're expanding a template
11208 argument pack. */
11209 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11211 if (argument == error_mark_node)
11213 cp_token *token = cp_lexer_peek_token (parser->lexer);
11214 error_at (token->location,
11215 "expected parameter pack before %<...%>");
11217 /* Consume the `...' token. */
11218 cp_lexer_consume_token (parser->lexer);
11220 /* Make the argument into a TYPE_PACK_EXPANSION or
11221 EXPR_PACK_EXPANSION. */
11222 argument = make_pack_expansion (argument);
11225 if (n_args == alloced)
11227 alloced *= 2;
11229 if (arg_ary == fixed_args)
11231 arg_ary = XNEWVEC (tree, alloced);
11232 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
11234 else
11235 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
11237 arg_ary[n_args++] = argument;
11239 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
11241 vec = make_tree_vec (n_args);
11243 while (n_args--)
11244 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
11246 if (arg_ary != fixed_args)
11247 free (arg_ary);
11248 parser->non_integral_constant_expression_p = saved_non_ice_p;
11249 parser->integral_constant_expression_p = saved_ice_p;
11250 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
11251 return vec;
11254 /* Parse a template-argument.
11256 template-argument:
11257 assignment-expression
11258 type-id
11259 id-expression
11261 The representation is that of an assignment-expression, type-id, or
11262 id-expression -- except that the qualified id-expression is
11263 evaluated, so that the value returned is either a DECL or an
11264 OVERLOAD.
11266 Although the standard says "assignment-expression", it forbids
11267 throw-expressions or assignments in the template argument.
11268 Therefore, we use "conditional-expression" instead. */
11270 static tree
11271 cp_parser_template_argument (cp_parser* parser)
11273 tree argument;
11274 bool template_p;
11275 bool address_p;
11276 bool maybe_type_id = false;
11277 cp_token *token = NULL, *argument_start_token = NULL;
11278 cp_id_kind idk;
11280 /* There's really no way to know what we're looking at, so we just
11281 try each alternative in order.
11283 [temp.arg]
11285 In a template-argument, an ambiguity between a type-id and an
11286 expression is resolved to a type-id, regardless of the form of
11287 the corresponding template-parameter.
11289 Therefore, we try a type-id first. */
11290 cp_parser_parse_tentatively (parser);
11291 argument = cp_parser_template_type_arg (parser);
11292 /* If there was no error parsing the type-id but the next token is a
11293 '>>', our behavior depends on which dialect of C++ we're
11294 parsing. In C++98, we probably found a typo for '> >'. But there
11295 are type-id which are also valid expressions. For instance:
11297 struct X { int operator >> (int); };
11298 template <int V> struct Foo {};
11299 Foo<X () >> 5> r;
11301 Here 'X()' is a valid type-id of a function type, but the user just
11302 wanted to write the expression "X() >> 5". Thus, we remember that we
11303 found a valid type-id, but we still try to parse the argument as an
11304 expression to see what happens.
11306 In C++0x, the '>>' will be considered two separate '>'
11307 tokens. */
11308 if (!cp_parser_error_occurred (parser)
11309 && cxx_dialect == cxx98
11310 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
11312 maybe_type_id = true;
11313 cp_parser_abort_tentative_parse (parser);
11315 else
11317 /* If the next token isn't a `,' or a `>', then this argument wasn't
11318 really finished. This means that the argument is not a valid
11319 type-id. */
11320 if (!cp_parser_next_token_ends_template_argument_p (parser))
11321 cp_parser_error (parser, "expected template-argument");
11322 /* If that worked, we're done. */
11323 if (cp_parser_parse_definitely (parser))
11324 return argument;
11326 /* We're still not sure what the argument will be. */
11327 cp_parser_parse_tentatively (parser);
11328 /* Try a template. */
11329 argument_start_token = cp_lexer_peek_token (parser->lexer);
11330 argument = cp_parser_id_expression (parser,
11331 /*template_keyword_p=*/false,
11332 /*check_dependency_p=*/true,
11333 &template_p,
11334 /*declarator_p=*/false,
11335 /*optional_p=*/false);
11336 /* If the next token isn't a `,' or a `>', then this argument wasn't
11337 really finished. */
11338 if (!cp_parser_next_token_ends_template_argument_p (parser))
11339 cp_parser_error (parser, "expected template-argument");
11340 if (!cp_parser_error_occurred (parser))
11342 /* Figure out what is being referred to. If the id-expression
11343 was for a class template specialization, then we will have a
11344 TYPE_DECL at this point. There is no need to do name lookup
11345 at this point in that case. */
11346 if (TREE_CODE (argument) != TYPE_DECL)
11347 argument = cp_parser_lookup_name (parser, argument,
11348 none_type,
11349 /*is_template=*/template_p,
11350 /*is_namespace=*/false,
11351 /*check_dependency=*/true,
11352 /*ambiguous_decls=*/NULL,
11353 argument_start_token->location);
11354 if (TREE_CODE (argument) != TEMPLATE_DECL
11355 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
11356 cp_parser_error (parser, "expected template-name");
11358 if (cp_parser_parse_definitely (parser))
11359 return argument;
11360 /* It must be a non-type argument. There permitted cases are given
11361 in [temp.arg.nontype]:
11363 -- an integral constant-expression of integral or enumeration
11364 type; or
11366 -- the name of a non-type template-parameter; or
11368 -- the name of an object or function with external linkage...
11370 -- the address of an object or function with external linkage...
11372 -- a pointer to member... */
11373 /* Look for a non-type template parameter. */
11374 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11376 cp_parser_parse_tentatively (parser);
11377 argument = cp_parser_primary_expression (parser,
11378 /*address_p=*/false,
11379 /*cast_p=*/false,
11380 /*template_arg_p=*/true,
11381 &idk);
11382 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
11383 || !cp_parser_next_token_ends_template_argument_p (parser))
11384 cp_parser_simulate_error (parser);
11385 if (cp_parser_parse_definitely (parser))
11386 return argument;
11389 /* If the next token is "&", the argument must be the address of an
11390 object or function with external linkage. */
11391 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
11392 if (address_p)
11393 cp_lexer_consume_token (parser->lexer);
11394 /* See if we might have an id-expression. */
11395 token = cp_lexer_peek_token (parser->lexer);
11396 if (token->type == CPP_NAME
11397 || token->keyword == RID_OPERATOR
11398 || token->type == CPP_SCOPE
11399 || token->type == CPP_TEMPLATE_ID
11400 || token->type == CPP_NESTED_NAME_SPECIFIER)
11402 cp_parser_parse_tentatively (parser);
11403 argument = cp_parser_primary_expression (parser,
11404 address_p,
11405 /*cast_p=*/false,
11406 /*template_arg_p=*/true,
11407 &idk);
11408 if (cp_parser_error_occurred (parser)
11409 || !cp_parser_next_token_ends_template_argument_p (parser))
11410 cp_parser_abort_tentative_parse (parser);
11411 else
11413 tree probe;
11415 if (TREE_CODE (argument) == INDIRECT_REF)
11417 gcc_assert (REFERENCE_REF_P (argument));
11418 argument = TREE_OPERAND (argument, 0);
11421 /* If we're in a template, we represent a qualified-id referring
11422 to a static data member as a SCOPE_REF even if the scope isn't
11423 dependent so that we can check access control later. */
11424 probe = argument;
11425 if (TREE_CODE (probe) == SCOPE_REF)
11426 probe = TREE_OPERAND (probe, 1);
11427 if (TREE_CODE (probe) == VAR_DECL)
11429 /* A variable without external linkage might still be a
11430 valid constant-expression, so no error is issued here
11431 if the external-linkage check fails. */
11432 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
11433 cp_parser_simulate_error (parser);
11435 else if (is_overloaded_fn (argument))
11436 /* All overloaded functions are allowed; if the external
11437 linkage test does not pass, an error will be issued
11438 later. */
11440 else if (address_p
11441 && (TREE_CODE (argument) == OFFSET_REF
11442 || TREE_CODE (argument) == SCOPE_REF))
11443 /* A pointer-to-member. */
11445 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
11447 else
11448 cp_parser_simulate_error (parser);
11450 if (cp_parser_parse_definitely (parser))
11452 if (address_p)
11453 argument = build_x_unary_op (ADDR_EXPR, argument,
11454 tf_warning_or_error);
11455 return argument;
11459 /* If the argument started with "&", there are no other valid
11460 alternatives at this point. */
11461 if (address_p)
11463 cp_parser_error (parser, "invalid non-type template argument");
11464 return error_mark_node;
11467 /* If the argument wasn't successfully parsed as a type-id followed
11468 by '>>', the argument can only be a constant expression now.
11469 Otherwise, we try parsing the constant-expression tentatively,
11470 because the argument could really be a type-id. */
11471 if (maybe_type_id)
11472 cp_parser_parse_tentatively (parser);
11473 argument = cp_parser_constant_expression (parser,
11474 /*allow_non_constant_p=*/false,
11475 /*non_constant_p=*/NULL);
11476 argument = fold_non_dependent_expr (argument);
11477 if (!maybe_type_id)
11478 return argument;
11479 if (!cp_parser_next_token_ends_template_argument_p (parser))
11480 cp_parser_error (parser, "expected template-argument");
11481 if (cp_parser_parse_definitely (parser))
11482 return argument;
11483 /* We did our best to parse the argument as a non type-id, but that
11484 was the only alternative that matched (albeit with a '>' after
11485 it). We can assume it's just a typo from the user, and a
11486 diagnostic will then be issued. */
11487 return cp_parser_template_type_arg (parser);
11490 /* Parse an explicit-instantiation.
11492 explicit-instantiation:
11493 template declaration
11495 Although the standard says `declaration', what it really means is:
11497 explicit-instantiation:
11498 template decl-specifier-seq [opt] declarator [opt] ;
11500 Things like `template int S<int>::i = 5, int S<double>::j;' are not
11501 supposed to be allowed. A defect report has been filed about this
11502 issue.
11504 GNU Extension:
11506 explicit-instantiation:
11507 storage-class-specifier template
11508 decl-specifier-seq [opt] declarator [opt] ;
11509 function-specifier template
11510 decl-specifier-seq [opt] declarator [opt] ; */
11512 static void
11513 cp_parser_explicit_instantiation (cp_parser* parser)
11515 int declares_class_or_enum;
11516 cp_decl_specifier_seq decl_specifiers;
11517 tree extension_specifier = NULL_TREE;
11518 cp_token *token;
11520 /* Look for an (optional) storage-class-specifier or
11521 function-specifier. */
11522 if (cp_parser_allow_gnu_extensions_p (parser))
11524 extension_specifier
11525 = cp_parser_storage_class_specifier_opt (parser);
11526 if (!extension_specifier)
11527 extension_specifier
11528 = cp_parser_function_specifier_opt (parser,
11529 /*decl_specs=*/NULL);
11532 /* Look for the `template' keyword. */
11533 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
11534 /* Let the front end know that we are processing an explicit
11535 instantiation. */
11536 begin_explicit_instantiation ();
11537 /* [temp.explicit] says that we are supposed to ignore access
11538 control while processing explicit instantiation directives. */
11539 push_deferring_access_checks (dk_no_check);
11540 /* Parse a decl-specifier-seq. */
11541 token = cp_lexer_peek_token (parser->lexer);
11542 cp_parser_decl_specifier_seq (parser,
11543 CP_PARSER_FLAGS_OPTIONAL,
11544 &decl_specifiers,
11545 &declares_class_or_enum);
11546 /* If there was exactly one decl-specifier, and it declared a class,
11547 and there's no declarator, then we have an explicit type
11548 instantiation. */
11549 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
11551 tree type;
11553 type = check_tag_decl (&decl_specifiers);
11554 /* Turn access control back on for names used during
11555 template instantiation. */
11556 pop_deferring_access_checks ();
11557 if (type)
11558 do_type_instantiation (type, extension_specifier,
11559 /*complain=*/tf_error);
11561 else
11563 cp_declarator *declarator;
11564 tree decl;
11566 /* Parse the declarator. */
11567 declarator
11568 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11569 /*ctor_dtor_or_conv_p=*/NULL,
11570 /*parenthesized_p=*/NULL,
11571 /*member_p=*/false);
11572 if (declares_class_or_enum & 2)
11573 cp_parser_check_for_definition_in_return_type (declarator,
11574 decl_specifiers.type,
11575 decl_specifiers.type_location);
11576 if (declarator != cp_error_declarator)
11578 decl = grokdeclarator (declarator, &decl_specifiers,
11579 NORMAL, 0, &decl_specifiers.attributes);
11580 /* Turn access control back on for names used during
11581 template instantiation. */
11582 pop_deferring_access_checks ();
11583 /* Do the explicit instantiation. */
11584 do_decl_instantiation (decl, extension_specifier);
11586 else
11588 pop_deferring_access_checks ();
11589 /* Skip the body of the explicit instantiation. */
11590 cp_parser_skip_to_end_of_statement (parser);
11593 /* We're done with the instantiation. */
11594 end_explicit_instantiation ();
11596 cp_parser_consume_semicolon_at_end_of_statement (parser);
11599 /* Parse an explicit-specialization.
11601 explicit-specialization:
11602 template < > declaration
11604 Although the standard says `declaration', what it really means is:
11606 explicit-specialization:
11607 template <> decl-specifier [opt] init-declarator [opt] ;
11608 template <> function-definition
11609 template <> explicit-specialization
11610 template <> template-declaration */
11612 static void
11613 cp_parser_explicit_specialization (cp_parser* parser)
11615 bool need_lang_pop;
11616 cp_token *token = cp_lexer_peek_token (parser->lexer);
11618 /* Look for the `template' keyword. */
11619 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
11620 /* Look for the `<'. */
11621 cp_parser_require (parser, CPP_LESS, "%<<%>");
11622 /* Look for the `>'. */
11623 cp_parser_require (parser, CPP_GREATER, "%<>%>");
11624 /* We have processed another parameter list. */
11625 ++parser->num_template_parameter_lists;
11626 /* [temp]
11628 A template ... explicit specialization ... shall not have C
11629 linkage. */
11630 if (current_lang_name == lang_name_c)
11632 error_at (token->location, "template specialization with C linkage");
11633 /* Give it C++ linkage to avoid confusing other parts of the
11634 front end. */
11635 push_lang_context (lang_name_cplusplus);
11636 need_lang_pop = true;
11638 else
11639 need_lang_pop = false;
11640 /* Let the front end know that we are beginning a specialization. */
11641 if (!begin_specialization ())
11643 end_specialization ();
11644 return;
11647 /* If the next keyword is `template', we need to figure out whether
11648 or not we're looking a template-declaration. */
11649 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
11651 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
11652 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
11653 cp_parser_template_declaration_after_export (parser,
11654 /*member_p=*/false);
11655 else
11656 cp_parser_explicit_specialization (parser);
11658 else
11659 /* Parse the dependent declaration. */
11660 cp_parser_single_declaration (parser,
11661 /*checks=*/NULL,
11662 /*member_p=*/false,
11663 /*explicit_specialization_p=*/true,
11664 /*friend_p=*/NULL);
11665 /* We're done with the specialization. */
11666 end_specialization ();
11667 /* For the erroneous case of a template with C linkage, we pushed an
11668 implicit C++ linkage scope; exit that scope now. */
11669 if (need_lang_pop)
11670 pop_lang_context ();
11671 /* We're done with this parameter list. */
11672 --parser->num_template_parameter_lists;
11675 /* Parse a type-specifier.
11677 type-specifier:
11678 simple-type-specifier
11679 class-specifier
11680 enum-specifier
11681 elaborated-type-specifier
11682 cv-qualifier
11684 GNU Extension:
11686 type-specifier:
11687 __complex__
11689 Returns a representation of the type-specifier. For a
11690 class-specifier, enum-specifier, or elaborated-type-specifier, a
11691 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
11693 The parser flags FLAGS is used to control type-specifier parsing.
11695 If IS_DECLARATION is TRUE, then this type-specifier is appearing
11696 in a decl-specifier-seq.
11698 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
11699 class-specifier, enum-specifier, or elaborated-type-specifier, then
11700 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
11701 if a type is declared; 2 if it is defined. Otherwise, it is set to
11702 zero.
11704 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
11705 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
11706 is set to FALSE. */
11708 static tree
11709 cp_parser_type_specifier (cp_parser* parser,
11710 cp_parser_flags flags,
11711 cp_decl_specifier_seq *decl_specs,
11712 bool is_declaration,
11713 int* declares_class_or_enum,
11714 bool* is_cv_qualifier)
11716 tree type_spec = NULL_TREE;
11717 cp_token *token;
11718 enum rid keyword;
11719 cp_decl_spec ds = ds_last;
11721 /* Assume this type-specifier does not declare a new type. */
11722 if (declares_class_or_enum)
11723 *declares_class_or_enum = 0;
11724 /* And that it does not specify a cv-qualifier. */
11725 if (is_cv_qualifier)
11726 *is_cv_qualifier = false;
11727 /* Peek at the next token. */
11728 token = cp_lexer_peek_token (parser->lexer);
11730 /* If we're looking at a keyword, we can use that to guide the
11731 production we choose. */
11732 keyword = token->keyword;
11733 switch (keyword)
11735 case RID_ENUM:
11736 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
11737 goto elaborated_type_specifier;
11739 /* Look for the enum-specifier. */
11740 type_spec = cp_parser_enum_specifier (parser);
11741 /* If that worked, we're done. */
11742 if (type_spec)
11744 if (declares_class_or_enum)
11745 *declares_class_or_enum = 2;
11746 if (decl_specs)
11747 cp_parser_set_decl_spec_type (decl_specs,
11748 type_spec,
11749 token->location,
11750 /*user_defined_p=*/true);
11751 return type_spec;
11753 else
11754 goto elaborated_type_specifier;
11756 /* Any of these indicate either a class-specifier, or an
11757 elaborated-type-specifier. */
11758 case RID_CLASS:
11759 case RID_STRUCT:
11760 case RID_UNION:
11761 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
11762 goto elaborated_type_specifier;
11764 /* Parse tentatively so that we can back up if we don't find a
11765 class-specifier. */
11766 cp_parser_parse_tentatively (parser);
11767 /* Look for the class-specifier. */
11768 type_spec = cp_parser_class_specifier (parser);
11769 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
11770 /* If that worked, we're done. */
11771 if (cp_parser_parse_definitely (parser))
11773 if (declares_class_or_enum)
11774 *declares_class_or_enum = 2;
11775 if (decl_specs)
11776 cp_parser_set_decl_spec_type (decl_specs,
11777 type_spec,
11778 token->location,
11779 /*user_defined_p=*/true);
11780 return type_spec;
11783 /* Fall through. */
11784 elaborated_type_specifier:
11785 /* We're declaring (not defining) a class or enum. */
11786 if (declares_class_or_enum)
11787 *declares_class_or_enum = 1;
11789 /* Fall through. */
11790 case RID_TYPENAME:
11791 /* Look for an elaborated-type-specifier. */
11792 type_spec
11793 = (cp_parser_elaborated_type_specifier
11794 (parser,
11795 decl_specs && decl_specs->specs[(int) ds_friend],
11796 is_declaration));
11797 if (decl_specs)
11798 cp_parser_set_decl_spec_type (decl_specs,
11799 type_spec,
11800 token->location,
11801 /*user_defined_p=*/true);
11802 return type_spec;
11804 case RID_CONST:
11805 ds = ds_const;
11806 if (is_cv_qualifier)
11807 *is_cv_qualifier = true;
11808 break;
11810 case RID_VOLATILE:
11811 ds = ds_volatile;
11812 if (is_cv_qualifier)
11813 *is_cv_qualifier = true;
11814 break;
11816 case RID_RESTRICT:
11817 ds = ds_restrict;
11818 if (is_cv_qualifier)
11819 *is_cv_qualifier = true;
11820 break;
11822 case RID_COMPLEX:
11823 /* The `__complex__' keyword is a GNU extension. */
11824 ds = ds_complex;
11825 break;
11827 default:
11828 break;
11831 /* Handle simple keywords. */
11832 if (ds != ds_last)
11834 if (decl_specs)
11836 ++decl_specs->specs[(int)ds];
11837 decl_specs->any_specifiers_p = true;
11839 return cp_lexer_consume_token (parser->lexer)->u.value;
11842 /* If we do not already have a type-specifier, assume we are looking
11843 at a simple-type-specifier. */
11844 type_spec = cp_parser_simple_type_specifier (parser,
11845 decl_specs,
11846 flags);
11848 /* If we didn't find a type-specifier, and a type-specifier was not
11849 optional in this context, issue an error message. */
11850 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11852 cp_parser_error (parser, "expected type specifier");
11853 return error_mark_node;
11856 return type_spec;
11859 /* Parse a simple-type-specifier.
11861 simple-type-specifier:
11862 :: [opt] nested-name-specifier [opt] type-name
11863 :: [opt] nested-name-specifier template template-id
11864 char
11865 wchar_t
11866 bool
11867 short
11869 long
11870 signed
11871 unsigned
11872 float
11873 double
11874 void
11876 C++0x Extension:
11878 simple-type-specifier:
11879 auto
11880 decltype ( expression )
11881 char16_t
11882 char32_t
11884 GNU Extension:
11886 simple-type-specifier:
11887 __typeof__ unary-expression
11888 __typeof__ ( type-id )
11890 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
11891 appropriately updated. */
11893 static tree
11894 cp_parser_simple_type_specifier (cp_parser* parser,
11895 cp_decl_specifier_seq *decl_specs,
11896 cp_parser_flags flags)
11898 tree type = NULL_TREE;
11899 cp_token *token;
11901 /* Peek at the next token. */
11902 token = cp_lexer_peek_token (parser->lexer);
11904 /* If we're looking at a keyword, things are easy. */
11905 switch (token->keyword)
11907 case RID_CHAR:
11908 if (decl_specs)
11909 decl_specs->explicit_char_p = true;
11910 type = char_type_node;
11911 break;
11912 case RID_CHAR16:
11913 type = char16_type_node;
11914 break;
11915 case RID_CHAR32:
11916 type = char32_type_node;
11917 break;
11918 case RID_WCHAR:
11919 type = wchar_type_node;
11920 break;
11921 case RID_BOOL:
11922 type = boolean_type_node;
11923 break;
11924 case RID_SHORT:
11925 if (decl_specs)
11926 ++decl_specs->specs[(int) ds_short];
11927 type = short_integer_type_node;
11928 break;
11929 case RID_INT:
11930 if (decl_specs)
11931 decl_specs->explicit_int_p = true;
11932 type = integer_type_node;
11933 break;
11934 case RID_LONG:
11935 if (decl_specs)
11936 ++decl_specs->specs[(int) ds_long];
11937 type = long_integer_type_node;
11938 break;
11939 case RID_SIGNED:
11940 if (decl_specs)
11941 ++decl_specs->specs[(int) ds_signed];
11942 type = integer_type_node;
11943 break;
11944 case RID_UNSIGNED:
11945 if (decl_specs)
11946 ++decl_specs->specs[(int) ds_unsigned];
11947 type = unsigned_type_node;
11948 break;
11949 case RID_FLOAT:
11950 type = float_type_node;
11951 break;
11952 case RID_DOUBLE:
11953 type = double_type_node;
11954 break;
11955 case RID_VOID:
11956 type = void_type_node;
11957 break;
11959 case RID_AUTO:
11960 maybe_warn_cpp0x (CPP0X_AUTO);
11961 type = make_auto ();
11962 break;
11964 case RID_DECLTYPE:
11965 /* Parse the `decltype' type. */
11966 type = cp_parser_decltype (parser);
11968 if (decl_specs)
11969 cp_parser_set_decl_spec_type (decl_specs, type,
11970 token->location,
11971 /*user_defined_p=*/true);
11973 return type;
11975 case RID_TYPEOF:
11976 /* Consume the `typeof' token. */
11977 cp_lexer_consume_token (parser->lexer);
11978 /* Parse the operand to `typeof'. */
11979 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11980 /* If it is not already a TYPE, take its type. */
11981 if (!TYPE_P (type))
11982 type = finish_typeof (type);
11984 if (decl_specs)
11985 cp_parser_set_decl_spec_type (decl_specs, type,
11986 token->location,
11987 /*user_defined_p=*/true);
11989 return type;
11991 default:
11992 break;
11995 /* If the type-specifier was for a built-in type, we're done. */
11996 if (type)
11998 tree id;
12000 /* Record the type. */
12001 if (decl_specs
12002 && (token->keyword != RID_SIGNED
12003 && token->keyword != RID_UNSIGNED
12004 && token->keyword != RID_SHORT
12005 && token->keyword != RID_LONG))
12006 cp_parser_set_decl_spec_type (decl_specs,
12007 type,
12008 token->location,
12009 /*user_defined=*/false);
12010 if (decl_specs)
12011 decl_specs->any_specifiers_p = true;
12013 /* Consume the token. */
12014 id = cp_lexer_consume_token (parser->lexer)->u.value;
12016 /* There is no valid C++ program where a non-template type is
12017 followed by a "<". That usually indicates that the user thought
12018 that the type was a template. */
12019 cp_parser_check_for_invalid_template_id (parser, type, token->location);
12021 return TYPE_NAME (type);
12024 /* The type-specifier must be a user-defined type. */
12025 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
12027 bool qualified_p;
12028 bool global_p;
12030 /* Don't gobble tokens or issue error messages if this is an
12031 optional type-specifier. */
12032 if (flags & CP_PARSER_FLAGS_OPTIONAL)
12033 cp_parser_parse_tentatively (parser);
12035 /* Look for the optional `::' operator. */
12036 global_p
12037 = (cp_parser_global_scope_opt (parser,
12038 /*current_scope_valid_p=*/false)
12039 != NULL_TREE);
12040 /* Look for the nested-name specifier. */
12041 qualified_p
12042 = (cp_parser_nested_name_specifier_opt (parser,
12043 /*typename_keyword_p=*/false,
12044 /*check_dependency_p=*/true,
12045 /*type_p=*/false,
12046 /*is_declaration=*/false)
12047 != NULL_TREE);
12048 token = cp_lexer_peek_token (parser->lexer);
12049 /* If we have seen a nested-name-specifier, and the next token
12050 is `template', then we are using the template-id production. */
12051 if (parser->scope
12052 && cp_parser_optional_template_keyword (parser))
12054 /* Look for the template-id. */
12055 type = cp_parser_template_id (parser,
12056 /*template_keyword_p=*/true,
12057 /*check_dependency_p=*/true,
12058 /*is_declaration=*/false);
12059 /* If the template-id did not name a type, we are out of
12060 luck. */
12061 if (TREE_CODE (type) != TYPE_DECL)
12063 cp_parser_error (parser, "expected template-id for type");
12064 type = NULL_TREE;
12067 /* Otherwise, look for a type-name. */
12068 else
12069 type = cp_parser_type_name (parser);
12070 /* Keep track of all name-lookups performed in class scopes. */
12071 if (type
12072 && !global_p
12073 && !qualified_p
12074 && TREE_CODE (type) == TYPE_DECL
12075 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
12076 maybe_note_name_used_in_class (DECL_NAME (type), type);
12077 /* If it didn't work out, we don't have a TYPE. */
12078 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
12079 && !cp_parser_parse_definitely (parser))
12080 type = NULL_TREE;
12081 if (type && decl_specs)
12082 cp_parser_set_decl_spec_type (decl_specs, type,
12083 token->location,
12084 /*user_defined=*/true);
12087 /* If we didn't get a type-name, issue an error message. */
12088 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12090 cp_parser_error (parser, "expected type-name");
12091 return error_mark_node;
12094 /* There is no valid C++ program where a non-template type is
12095 followed by a "<". That usually indicates that the user thought
12096 that the type was a template. */
12097 if (type && type != error_mark_node)
12099 /* As a last-ditch effort, see if TYPE is an Objective-C type.
12100 If it is, then the '<'...'>' enclose protocol names rather than
12101 template arguments, and so everything is fine. */
12102 if (c_dialect_objc ()
12103 && (objc_is_id (type) || objc_is_class_name (type)))
12105 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12106 tree qual_type = objc_get_protocol_qualified_type (type, protos);
12108 /* Clobber the "unqualified" type previously entered into
12109 DECL_SPECS with the new, improved protocol-qualified version. */
12110 if (decl_specs)
12111 decl_specs->type = qual_type;
12113 return qual_type;
12116 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12117 token->location);
12120 return type;
12123 /* Parse a type-name.
12125 type-name:
12126 class-name
12127 enum-name
12128 typedef-name
12130 enum-name:
12131 identifier
12133 typedef-name:
12134 identifier
12136 Returns a TYPE_DECL for the type. */
12138 static tree
12139 cp_parser_type_name (cp_parser* parser)
12141 tree type_decl;
12143 /* We can't know yet whether it is a class-name or not. */
12144 cp_parser_parse_tentatively (parser);
12145 /* Try a class-name. */
12146 type_decl = cp_parser_class_name (parser,
12147 /*typename_keyword_p=*/false,
12148 /*template_keyword_p=*/false,
12149 none_type,
12150 /*check_dependency_p=*/true,
12151 /*class_head_p=*/false,
12152 /*is_declaration=*/false);
12153 /* If it's not a class-name, keep looking. */
12154 if (!cp_parser_parse_definitely (parser))
12156 /* It must be a typedef-name or an enum-name. */
12157 return cp_parser_nonclass_name (parser);
12160 return type_decl;
12163 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12165 enum-name:
12166 identifier
12168 typedef-name:
12169 identifier
12171 Returns a TYPE_DECL for the type. */
12173 static tree
12174 cp_parser_nonclass_name (cp_parser* parser)
12176 tree type_decl;
12177 tree identifier;
12179 cp_token *token = cp_lexer_peek_token (parser->lexer);
12180 identifier = cp_parser_identifier (parser);
12181 if (identifier == error_mark_node)
12182 return error_mark_node;
12184 /* Look up the type-name. */
12185 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
12187 if (TREE_CODE (type_decl) != TYPE_DECL
12188 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
12190 /* See if this is an Objective-C type. */
12191 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12192 tree type = objc_get_protocol_qualified_type (identifier, protos);
12193 if (type)
12194 type_decl = TYPE_NAME (type);
12197 /* Issue an error if we did not find a type-name. */
12198 if (TREE_CODE (type_decl) != TYPE_DECL)
12200 if (!cp_parser_simulate_error (parser))
12201 cp_parser_name_lookup_error (parser, identifier, type_decl,
12202 "is not a type", token->location);
12203 return error_mark_node;
12205 /* Remember that the name was used in the definition of the
12206 current class so that we can check later to see if the
12207 meaning would have been different after the class was
12208 entirely defined. */
12209 else if (type_decl != error_mark_node
12210 && !parser->scope)
12211 maybe_note_name_used_in_class (identifier, type_decl);
12213 return type_decl;
12216 /* Parse an elaborated-type-specifier. Note that the grammar given
12217 here incorporates the resolution to DR68.
12219 elaborated-type-specifier:
12220 class-key :: [opt] nested-name-specifier [opt] identifier
12221 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
12222 enum-key :: [opt] nested-name-specifier [opt] identifier
12223 typename :: [opt] nested-name-specifier identifier
12224 typename :: [opt] nested-name-specifier template [opt]
12225 template-id
12227 GNU extension:
12229 elaborated-type-specifier:
12230 class-key attributes :: [opt] nested-name-specifier [opt] identifier
12231 class-key attributes :: [opt] nested-name-specifier [opt]
12232 template [opt] template-id
12233 enum attributes :: [opt] nested-name-specifier [opt] identifier
12235 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
12236 declared `friend'. If IS_DECLARATION is TRUE, then this
12237 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
12238 something is being declared.
12240 Returns the TYPE specified. */
12242 static tree
12243 cp_parser_elaborated_type_specifier (cp_parser* parser,
12244 bool is_friend,
12245 bool is_declaration)
12247 enum tag_types tag_type;
12248 tree identifier;
12249 tree type = NULL_TREE;
12250 tree attributes = NULL_TREE;
12251 tree globalscope;
12252 cp_token *token = NULL;
12254 /* See if we're looking at the `enum' keyword. */
12255 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
12257 /* Consume the `enum' token. */
12258 cp_lexer_consume_token (parser->lexer);
12259 /* Remember that it's an enumeration type. */
12260 tag_type = enum_type;
12261 /* Parse the optional `struct' or `class' key (for C++0x scoped
12262 enums). */
12263 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12264 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12266 if (cxx_dialect == cxx98)
12267 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
12269 /* Consume the `struct' or `class'. */
12270 cp_lexer_consume_token (parser->lexer);
12272 /* Parse the attributes. */
12273 attributes = cp_parser_attributes_opt (parser);
12275 /* Or, it might be `typename'. */
12276 else if (cp_lexer_next_token_is_keyword (parser->lexer,
12277 RID_TYPENAME))
12279 /* Consume the `typename' token. */
12280 cp_lexer_consume_token (parser->lexer);
12281 /* Remember that it's a `typename' type. */
12282 tag_type = typename_type;
12284 /* Otherwise it must be a class-key. */
12285 else
12287 tag_type = cp_parser_class_key (parser);
12288 if (tag_type == none_type)
12289 return error_mark_node;
12290 /* Parse the attributes. */
12291 attributes = cp_parser_attributes_opt (parser);
12294 /* Look for the `::' operator. */
12295 globalscope = cp_parser_global_scope_opt (parser,
12296 /*current_scope_valid_p=*/false);
12297 /* Look for the nested-name-specifier. */
12298 if (tag_type == typename_type && !globalscope)
12300 if (!cp_parser_nested_name_specifier (parser,
12301 /*typename_keyword_p=*/true,
12302 /*check_dependency_p=*/true,
12303 /*type_p=*/true,
12304 is_declaration))
12305 return error_mark_node;
12307 else
12308 /* Even though `typename' is not present, the proposed resolution
12309 to Core Issue 180 says that in `class A<T>::B', `B' should be
12310 considered a type-name, even if `A<T>' is dependent. */
12311 cp_parser_nested_name_specifier_opt (parser,
12312 /*typename_keyword_p=*/true,
12313 /*check_dependency_p=*/true,
12314 /*type_p=*/true,
12315 is_declaration);
12316 /* For everything but enumeration types, consider a template-id.
12317 For an enumeration type, consider only a plain identifier. */
12318 if (tag_type != enum_type)
12320 bool template_p = false;
12321 tree decl;
12323 /* Allow the `template' keyword. */
12324 template_p = cp_parser_optional_template_keyword (parser);
12325 /* If we didn't see `template', we don't know if there's a
12326 template-id or not. */
12327 if (!template_p)
12328 cp_parser_parse_tentatively (parser);
12329 /* Parse the template-id. */
12330 token = cp_lexer_peek_token (parser->lexer);
12331 decl = cp_parser_template_id (parser, template_p,
12332 /*check_dependency_p=*/true,
12333 is_declaration);
12334 /* If we didn't find a template-id, look for an ordinary
12335 identifier. */
12336 if (!template_p && !cp_parser_parse_definitely (parser))
12338 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
12339 in effect, then we must assume that, upon instantiation, the
12340 template will correspond to a class. */
12341 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12342 && tag_type == typename_type)
12343 type = make_typename_type (parser->scope, decl,
12344 typename_type,
12345 /*complain=*/tf_error);
12346 /* If the `typename' keyword is in effect and DECL is not a type
12347 decl. Then type is non existant. */
12348 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
12349 type = NULL_TREE;
12350 else
12351 type = TREE_TYPE (decl);
12354 if (!type)
12356 token = cp_lexer_peek_token (parser->lexer);
12357 identifier = cp_parser_identifier (parser);
12359 if (identifier == error_mark_node)
12361 parser->scope = NULL_TREE;
12362 return error_mark_node;
12365 /* For a `typename', we needn't call xref_tag. */
12366 if (tag_type == typename_type
12367 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
12368 return cp_parser_make_typename_type (parser, parser->scope,
12369 identifier,
12370 token->location);
12371 /* Look up a qualified name in the usual way. */
12372 if (parser->scope)
12374 tree decl;
12375 tree ambiguous_decls;
12377 decl = cp_parser_lookup_name (parser, identifier,
12378 tag_type,
12379 /*is_template=*/false,
12380 /*is_namespace=*/false,
12381 /*check_dependency=*/true,
12382 &ambiguous_decls,
12383 token->location);
12385 /* If the lookup was ambiguous, an error will already have been
12386 issued. */
12387 if (ambiguous_decls)
12388 return error_mark_node;
12390 /* If we are parsing friend declaration, DECL may be a
12391 TEMPLATE_DECL tree node here. However, we need to check
12392 whether this TEMPLATE_DECL results in valid code. Consider
12393 the following example:
12395 namespace N {
12396 template <class T> class C {};
12398 class X {
12399 template <class T> friend class N::C; // #1, valid code
12401 template <class T> class Y {
12402 friend class N::C; // #2, invalid code
12405 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
12406 name lookup of `N::C'. We see that friend declaration must
12407 be template for the code to be valid. Note that
12408 processing_template_decl does not work here since it is
12409 always 1 for the above two cases. */
12411 decl = (cp_parser_maybe_treat_template_as_class
12412 (decl, /*tag_name_p=*/is_friend
12413 && parser->num_template_parameter_lists));
12415 if (TREE_CODE (decl) != TYPE_DECL)
12417 cp_parser_diagnose_invalid_type_name (parser,
12418 parser->scope,
12419 identifier,
12420 token->location);
12421 return error_mark_node;
12424 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
12426 bool allow_template = (parser->num_template_parameter_lists
12427 || DECL_SELF_REFERENCE_P (decl));
12428 type = check_elaborated_type_specifier (tag_type, decl,
12429 allow_template);
12431 if (type == error_mark_node)
12432 return error_mark_node;
12435 /* Forward declarations of nested types, such as
12437 class C1::C2;
12438 class C1::C2::C3;
12440 are invalid unless all components preceding the final '::'
12441 are complete. If all enclosing types are complete, these
12442 declarations become merely pointless.
12444 Invalid forward declarations of nested types are errors
12445 caught elsewhere in parsing. Those that are pointless arrive
12446 here. */
12448 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12449 && !is_friend && !processing_explicit_instantiation)
12450 warning (0, "declaration %qD does not declare anything", decl);
12452 type = TREE_TYPE (decl);
12454 else
12456 /* An elaborated-type-specifier sometimes introduces a new type and
12457 sometimes names an existing type. Normally, the rule is that it
12458 introduces a new type only if there is not an existing type of
12459 the same name already in scope. For example, given:
12461 struct S {};
12462 void f() { struct S s; }
12464 the `struct S' in the body of `f' is the same `struct S' as in
12465 the global scope; the existing definition is used. However, if
12466 there were no global declaration, this would introduce a new
12467 local class named `S'.
12469 An exception to this rule applies to the following code:
12471 namespace N { struct S; }
12473 Here, the elaborated-type-specifier names a new type
12474 unconditionally; even if there is already an `S' in the
12475 containing scope this declaration names a new type.
12476 This exception only applies if the elaborated-type-specifier
12477 forms the complete declaration:
12479 [class.name]
12481 A declaration consisting solely of `class-key identifier ;' is
12482 either a redeclaration of the name in the current scope or a
12483 forward declaration of the identifier as a class name. It
12484 introduces the name into the current scope.
12486 We are in this situation precisely when the next token is a `;'.
12488 An exception to the exception is that a `friend' declaration does
12489 *not* name a new type; i.e., given:
12491 struct S { friend struct T; };
12493 `T' is not a new type in the scope of `S'.
12495 Also, `new struct S' or `sizeof (struct S)' never results in the
12496 definition of a new type; a new type can only be declared in a
12497 declaration context. */
12499 tag_scope ts;
12500 bool template_p;
12502 if (is_friend)
12503 /* Friends have special name lookup rules. */
12504 ts = ts_within_enclosing_non_class;
12505 else if (is_declaration
12506 && cp_lexer_next_token_is (parser->lexer,
12507 CPP_SEMICOLON))
12508 /* This is a `class-key identifier ;' */
12509 ts = ts_current;
12510 else
12511 ts = ts_global;
12513 template_p =
12514 (parser->num_template_parameter_lists
12515 && (cp_parser_next_token_starts_class_definition_p (parser)
12516 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
12517 /* An unqualified name was used to reference this type, so
12518 there were no qualifying templates. */
12519 if (!cp_parser_check_template_parameters (parser,
12520 /*num_templates=*/0,
12521 token->location,
12522 /*declarator=*/NULL))
12523 return error_mark_node;
12524 type = xref_tag (tag_type, identifier, ts, template_p);
12528 if (type == error_mark_node)
12529 return error_mark_node;
12531 /* Allow attributes on forward declarations of classes. */
12532 if (attributes)
12534 if (TREE_CODE (type) == TYPENAME_TYPE)
12535 warning (OPT_Wattributes,
12536 "attributes ignored on uninstantiated type");
12537 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
12538 && ! processing_explicit_instantiation)
12539 warning (OPT_Wattributes,
12540 "attributes ignored on template instantiation");
12541 else if (is_declaration && cp_parser_declares_only_class_p (parser))
12542 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
12543 else
12544 warning (OPT_Wattributes,
12545 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
12548 if (tag_type != enum_type)
12549 cp_parser_check_class_key (tag_type, type);
12551 /* A "<" cannot follow an elaborated type specifier. If that
12552 happens, the user was probably trying to form a template-id. */
12553 cp_parser_check_for_invalid_template_id (parser, type, token->location);
12555 return type;
12558 /* Parse an enum-specifier.
12560 enum-specifier:
12561 enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
12563 enum-key:
12564 enum
12565 enum class [C++0x]
12566 enum struct [C++0x]
12568 enum-base: [C++0x]
12569 : type-specifier-seq
12571 GNU Extensions:
12572 enum-key attributes[opt] identifier [opt] enum-base [opt]
12573 { enumerator-list [opt] }attributes[opt]
12575 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
12576 if the token stream isn't an enum-specifier after all. */
12578 static tree
12579 cp_parser_enum_specifier (cp_parser* parser)
12581 tree identifier;
12582 tree type;
12583 tree attributes;
12584 bool scoped_enum_p = false;
12585 bool has_underlying_type = false;
12586 tree underlying_type = NULL_TREE;
12588 /* Parse tentatively so that we can back up if we don't find a
12589 enum-specifier. */
12590 cp_parser_parse_tentatively (parser);
12592 /* Caller guarantees that the current token is 'enum', an identifier
12593 possibly follows, and the token after that is an opening brace.
12594 If we don't have an identifier, fabricate an anonymous name for
12595 the enumeration being defined. */
12596 cp_lexer_consume_token (parser->lexer);
12598 /* Parse the "class" or "struct", which indicates a scoped
12599 enumeration type in C++0x. */
12600 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12601 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12603 if (cxx_dialect == cxx98)
12604 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
12606 /* Consume the `struct' or `class' token. */
12607 cp_lexer_consume_token (parser->lexer);
12609 scoped_enum_p = true;
12612 attributes = cp_parser_attributes_opt (parser);
12614 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12615 identifier = cp_parser_identifier (parser);
12616 else
12617 identifier = make_anon_name ();
12619 /* Check for the `:' that denotes a specified underlying type in C++0x.
12620 Note that a ':' could also indicate a bitfield width, however. */
12621 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12623 cp_decl_specifier_seq type_specifiers;
12625 /* Consume the `:'. */
12626 cp_lexer_consume_token (parser->lexer);
12628 /* Parse the type-specifier-seq. */
12629 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12630 /*is_trailing_return=*/false,
12631 &type_specifiers);
12633 /* At this point this is surely not elaborated type specifier. */
12634 if (!cp_parser_parse_definitely (parser))
12635 return NULL_TREE;
12637 if (cxx_dialect == cxx98)
12638 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
12640 has_underlying_type = true;
12642 /* If that didn't work, stop. */
12643 if (type_specifiers.type != error_mark_node)
12645 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
12646 /*initialized=*/0, NULL);
12647 if (underlying_type == error_mark_node)
12648 underlying_type = NULL_TREE;
12652 /* Look for the `{' but don't consume it yet. */
12653 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12655 cp_parser_error (parser, "expected %<{%>");
12656 if (has_underlying_type)
12657 return NULL_TREE;
12660 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
12661 return NULL_TREE;
12663 /* Issue an error message if type-definitions are forbidden here. */
12664 if (!cp_parser_check_type_definition (parser))
12665 type = error_mark_node;
12666 else
12667 /* Create the new type. We do this before consuming the opening
12668 brace so the enum will be recorded as being on the line of its
12669 tag (or the 'enum' keyword, if there is no tag). */
12670 type = start_enum (identifier, underlying_type, scoped_enum_p);
12672 /* Consume the opening brace. */
12673 cp_lexer_consume_token (parser->lexer);
12675 if (type == error_mark_node)
12677 cp_parser_skip_to_end_of_block_or_statement (parser);
12678 return error_mark_node;
12681 /* If the next token is not '}', then there are some enumerators. */
12682 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12683 cp_parser_enumerator_list (parser, type);
12685 /* Consume the final '}'. */
12686 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12688 /* Look for trailing attributes to apply to this enumeration, and
12689 apply them if appropriate. */
12690 if (cp_parser_allow_gnu_extensions_p (parser))
12692 tree trailing_attr = cp_parser_attributes_opt (parser);
12693 trailing_attr = chainon (trailing_attr, attributes);
12694 cplus_decl_attributes (&type,
12695 trailing_attr,
12696 (int) ATTR_FLAG_TYPE_IN_PLACE);
12699 /* Finish up the enumeration. */
12700 finish_enum (type);
12702 return type;
12705 /* Parse an enumerator-list. The enumerators all have the indicated
12706 TYPE.
12708 enumerator-list:
12709 enumerator-definition
12710 enumerator-list , enumerator-definition */
12712 static void
12713 cp_parser_enumerator_list (cp_parser* parser, tree type)
12715 while (true)
12717 /* Parse an enumerator-definition. */
12718 cp_parser_enumerator_definition (parser, type);
12720 /* If the next token is not a ',', we've reached the end of
12721 the list. */
12722 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12723 break;
12724 /* Otherwise, consume the `,' and keep going. */
12725 cp_lexer_consume_token (parser->lexer);
12726 /* If the next token is a `}', there is a trailing comma. */
12727 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
12729 if (!in_system_header)
12730 pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
12731 break;
12736 /* Parse an enumerator-definition. The enumerator has the indicated
12737 TYPE.
12739 enumerator-definition:
12740 enumerator
12741 enumerator = constant-expression
12743 enumerator:
12744 identifier */
12746 static void
12747 cp_parser_enumerator_definition (cp_parser* parser, tree type)
12749 tree identifier;
12750 tree value;
12752 /* Look for the identifier. */
12753 identifier = cp_parser_identifier (parser);
12754 if (identifier == error_mark_node)
12755 return;
12757 /* If the next token is an '=', then there is an explicit value. */
12758 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12760 /* Consume the `=' token. */
12761 cp_lexer_consume_token (parser->lexer);
12762 /* Parse the value. */
12763 value = cp_parser_constant_expression (parser,
12764 /*allow_non_constant_p=*/false,
12765 NULL);
12767 else
12768 value = NULL_TREE;
12770 /* If we are processing a template, make sure the initializer of the
12771 enumerator doesn't contain any bare template parameter pack. */
12772 if (check_for_bare_parameter_packs (value))
12773 value = error_mark_node;
12775 /* Create the enumerator. */
12776 build_enumerator (identifier, value, type);
12779 /* Parse a namespace-name.
12781 namespace-name:
12782 original-namespace-name
12783 namespace-alias
12785 Returns the NAMESPACE_DECL for the namespace. */
12787 static tree
12788 cp_parser_namespace_name (cp_parser* parser)
12790 tree identifier;
12791 tree namespace_decl;
12793 cp_token *token = cp_lexer_peek_token (parser->lexer);
12795 /* Get the name of the namespace. */
12796 identifier = cp_parser_identifier (parser);
12797 if (identifier == error_mark_node)
12798 return error_mark_node;
12800 /* Look up the identifier in the currently active scope. Look only
12801 for namespaces, due to:
12803 [basic.lookup.udir]
12805 When looking up a namespace-name in a using-directive or alias
12806 definition, only namespace names are considered.
12808 And:
12810 [basic.lookup.qual]
12812 During the lookup of a name preceding the :: scope resolution
12813 operator, object, function, and enumerator names are ignored.
12815 (Note that cp_parser_qualifying_entity only calls this
12816 function if the token after the name is the scope resolution
12817 operator.) */
12818 namespace_decl = cp_parser_lookup_name (parser, identifier,
12819 none_type,
12820 /*is_template=*/false,
12821 /*is_namespace=*/true,
12822 /*check_dependency=*/true,
12823 /*ambiguous_decls=*/NULL,
12824 token->location);
12825 /* If it's not a namespace, issue an error. */
12826 if (namespace_decl == error_mark_node
12827 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
12829 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12830 error_at (token->location, "%qD is not a namespace-name", identifier);
12831 cp_parser_error (parser, "expected namespace-name");
12832 namespace_decl = error_mark_node;
12835 return namespace_decl;
12838 /* Parse a namespace-definition.
12840 namespace-definition:
12841 named-namespace-definition
12842 unnamed-namespace-definition
12844 named-namespace-definition:
12845 original-namespace-definition
12846 extension-namespace-definition
12848 original-namespace-definition:
12849 namespace identifier { namespace-body }
12851 extension-namespace-definition:
12852 namespace original-namespace-name { namespace-body }
12854 unnamed-namespace-definition:
12855 namespace { namespace-body } */
12857 static void
12858 cp_parser_namespace_definition (cp_parser* parser)
12860 tree identifier, attribs;
12861 bool has_visibility;
12862 bool is_inline;
12864 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
12866 is_inline = true;
12867 cp_lexer_consume_token (parser->lexer);
12869 else
12870 is_inline = false;
12872 /* Look for the `namespace' keyword. */
12873 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12875 /* Get the name of the namespace. We do not attempt to distinguish
12876 between an original-namespace-definition and an
12877 extension-namespace-definition at this point. The semantic
12878 analysis routines are responsible for that. */
12879 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12880 identifier = cp_parser_identifier (parser);
12881 else
12882 identifier = NULL_TREE;
12884 /* Parse any specified attributes. */
12885 attribs = cp_parser_attributes_opt (parser);
12887 /* Look for the `{' to start the namespace. */
12888 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
12889 /* Start the namespace. */
12890 push_namespace (identifier);
12892 /* "inline namespace" is equivalent to a stub namespace definition
12893 followed by a strong using directive. */
12894 if (is_inline)
12896 tree name_space = current_namespace;
12897 /* Set up namespace association. */
12898 DECL_NAMESPACE_ASSOCIATIONS (name_space)
12899 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
12900 DECL_NAMESPACE_ASSOCIATIONS (name_space));
12901 /* Import the contents of the inline namespace. */
12902 pop_namespace ();
12903 do_using_directive (name_space);
12904 push_namespace (identifier);
12907 has_visibility = handle_namespace_attrs (current_namespace, attribs);
12909 /* Parse the body of the namespace. */
12910 cp_parser_namespace_body (parser);
12912 #ifdef HANDLE_PRAGMA_VISIBILITY
12913 if (has_visibility)
12914 pop_visibility (1);
12915 #endif
12917 /* Finish the namespace. */
12918 pop_namespace ();
12919 /* Look for the final `}'. */
12920 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12923 /* Parse a namespace-body.
12925 namespace-body:
12926 declaration-seq [opt] */
12928 static void
12929 cp_parser_namespace_body (cp_parser* parser)
12931 cp_parser_declaration_seq_opt (parser);
12934 /* Parse a namespace-alias-definition.
12936 namespace-alias-definition:
12937 namespace identifier = qualified-namespace-specifier ; */
12939 static void
12940 cp_parser_namespace_alias_definition (cp_parser* parser)
12942 tree identifier;
12943 tree namespace_specifier;
12945 cp_token *token = cp_lexer_peek_token (parser->lexer);
12947 /* Look for the `namespace' keyword. */
12948 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12949 /* Look for the identifier. */
12950 identifier = cp_parser_identifier (parser);
12951 if (identifier == error_mark_node)
12952 return;
12953 /* Look for the `=' token. */
12954 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12955 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12957 error_at (token->location, "%<namespace%> definition is not allowed here");
12958 /* Skip the definition. */
12959 cp_lexer_consume_token (parser->lexer);
12960 if (cp_parser_skip_to_closing_brace (parser))
12961 cp_lexer_consume_token (parser->lexer);
12962 return;
12964 cp_parser_require (parser, CPP_EQ, "%<=%>");
12965 /* Look for the qualified-namespace-specifier. */
12966 namespace_specifier
12967 = cp_parser_qualified_namespace_specifier (parser);
12968 /* Look for the `;' token. */
12969 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12971 /* Register the alias in the symbol table. */
12972 do_namespace_alias (identifier, namespace_specifier);
12975 /* Parse a qualified-namespace-specifier.
12977 qualified-namespace-specifier:
12978 :: [opt] nested-name-specifier [opt] namespace-name
12980 Returns a NAMESPACE_DECL corresponding to the specified
12981 namespace. */
12983 static tree
12984 cp_parser_qualified_namespace_specifier (cp_parser* parser)
12986 /* Look for the optional `::'. */
12987 cp_parser_global_scope_opt (parser,
12988 /*current_scope_valid_p=*/false);
12990 /* Look for the optional nested-name-specifier. */
12991 cp_parser_nested_name_specifier_opt (parser,
12992 /*typename_keyword_p=*/false,
12993 /*check_dependency_p=*/true,
12994 /*type_p=*/false,
12995 /*is_declaration=*/true);
12997 return cp_parser_namespace_name (parser);
13000 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
13001 access declaration.
13003 using-declaration:
13004 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
13005 using :: unqualified-id ;
13007 access-declaration:
13008 qualified-id ;
13012 static bool
13013 cp_parser_using_declaration (cp_parser* parser,
13014 bool access_declaration_p)
13016 cp_token *token;
13017 bool typename_p = false;
13018 bool global_scope_p;
13019 tree decl;
13020 tree identifier;
13021 tree qscope;
13023 if (access_declaration_p)
13024 cp_parser_parse_tentatively (parser);
13025 else
13027 /* Look for the `using' keyword. */
13028 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
13030 /* Peek at the next token. */
13031 token = cp_lexer_peek_token (parser->lexer);
13032 /* See if it's `typename'. */
13033 if (token->keyword == RID_TYPENAME)
13035 /* Remember that we've seen it. */
13036 typename_p = true;
13037 /* Consume the `typename' token. */
13038 cp_lexer_consume_token (parser->lexer);
13042 /* Look for the optional global scope qualification. */
13043 global_scope_p
13044 = (cp_parser_global_scope_opt (parser,
13045 /*current_scope_valid_p=*/false)
13046 != NULL_TREE);
13048 /* If we saw `typename', or didn't see `::', then there must be a
13049 nested-name-specifier present. */
13050 if (typename_p || !global_scope_p)
13051 qscope = cp_parser_nested_name_specifier (parser, typename_p,
13052 /*check_dependency_p=*/true,
13053 /*type_p=*/false,
13054 /*is_declaration=*/true);
13055 /* Otherwise, we could be in either of the two productions. In that
13056 case, treat the nested-name-specifier as optional. */
13057 else
13058 qscope = cp_parser_nested_name_specifier_opt (parser,
13059 /*typename_keyword_p=*/false,
13060 /*check_dependency_p=*/true,
13061 /*type_p=*/false,
13062 /*is_declaration=*/true);
13063 if (!qscope)
13064 qscope = global_namespace;
13066 if (access_declaration_p && cp_parser_error_occurred (parser))
13067 /* Something has already gone wrong; there's no need to parse
13068 further. Since an error has occurred, the return value of
13069 cp_parser_parse_definitely will be false, as required. */
13070 return cp_parser_parse_definitely (parser);
13072 token = cp_lexer_peek_token (parser->lexer);
13073 /* Parse the unqualified-id. */
13074 identifier = cp_parser_unqualified_id (parser,
13075 /*template_keyword_p=*/false,
13076 /*check_dependency_p=*/true,
13077 /*declarator_p=*/true,
13078 /*optional_p=*/false);
13080 if (access_declaration_p)
13082 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13083 cp_parser_simulate_error (parser);
13084 if (!cp_parser_parse_definitely (parser))
13085 return false;
13088 /* The function we call to handle a using-declaration is different
13089 depending on what scope we are in. */
13090 if (qscope == error_mark_node || identifier == error_mark_node)
13092 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
13093 && TREE_CODE (identifier) != BIT_NOT_EXPR)
13094 /* [namespace.udecl]
13096 A using declaration shall not name a template-id. */
13097 error_at (token->location,
13098 "a template-id may not appear in a using-declaration");
13099 else
13101 if (at_class_scope_p ())
13103 /* Create the USING_DECL. */
13104 decl = do_class_using_decl (parser->scope, identifier);
13106 if (check_for_bare_parameter_packs (decl))
13107 return false;
13108 else
13109 /* Add it to the list of members in this class. */
13110 finish_member_declaration (decl);
13112 else
13114 decl = cp_parser_lookup_name_simple (parser,
13115 identifier,
13116 token->location);
13117 if (decl == error_mark_node)
13118 cp_parser_name_lookup_error (parser, identifier,
13119 decl, NULL,
13120 token->location);
13121 else if (check_for_bare_parameter_packs (decl))
13122 return false;
13123 else if (!at_namespace_scope_p ())
13124 do_local_using_decl (decl, qscope, identifier);
13125 else
13126 do_toplevel_using_decl (decl, qscope, identifier);
13130 /* Look for the final `;'. */
13131 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13133 return true;
13136 /* Parse a using-directive.
13138 using-directive:
13139 using namespace :: [opt] nested-name-specifier [opt]
13140 namespace-name ; */
13142 static void
13143 cp_parser_using_directive (cp_parser* parser)
13145 tree namespace_decl;
13146 tree attribs;
13148 /* Look for the `using' keyword. */
13149 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
13150 /* And the `namespace' keyword. */
13151 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
13152 /* Look for the optional `::' operator. */
13153 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13154 /* And the optional nested-name-specifier. */
13155 cp_parser_nested_name_specifier_opt (parser,
13156 /*typename_keyword_p=*/false,
13157 /*check_dependency_p=*/true,
13158 /*type_p=*/false,
13159 /*is_declaration=*/true);
13160 /* Get the namespace being used. */
13161 namespace_decl = cp_parser_namespace_name (parser);
13162 /* And any specified attributes. */
13163 attribs = cp_parser_attributes_opt (parser);
13164 /* Update the symbol table. */
13165 parse_using_directive (namespace_decl, attribs);
13166 /* Look for the final `;'. */
13167 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13170 /* Parse an asm-definition.
13172 asm-definition:
13173 asm ( string-literal ) ;
13175 GNU Extension:
13177 asm-definition:
13178 asm volatile [opt] ( string-literal ) ;
13179 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
13180 asm volatile [opt] ( string-literal : asm-operand-list [opt]
13181 : asm-operand-list [opt] ) ;
13182 asm volatile [opt] ( string-literal : asm-operand-list [opt]
13183 : asm-operand-list [opt]
13184 : asm-clobber-list [opt] ) ;
13185 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
13186 : asm-clobber-list [opt]
13187 : asm-goto-list ) ; */
13189 static void
13190 cp_parser_asm_definition (cp_parser* parser)
13192 tree string;
13193 tree outputs = NULL_TREE;
13194 tree inputs = NULL_TREE;
13195 tree clobbers = NULL_TREE;
13196 tree labels = NULL_TREE;
13197 tree asm_stmt;
13198 bool volatile_p = false;
13199 bool extended_p = false;
13200 bool invalid_inputs_p = false;
13201 bool invalid_outputs_p = false;
13202 bool goto_p = false;
13203 const char *missing = NULL;
13205 /* Look for the `asm' keyword. */
13206 cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
13207 /* See if the next token is `volatile'. */
13208 if (cp_parser_allow_gnu_extensions_p (parser)
13209 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
13211 /* Remember that we saw the `volatile' keyword. */
13212 volatile_p = true;
13213 /* Consume the token. */
13214 cp_lexer_consume_token (parser->lexer);
13216 if (cp_parser_allow_gnu_extensions_p (parser)
13217 && parser->in_function_body
13218 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
13220 /* Remember that we saw the `goto' keyword. */
13221 goto_p = true;
13222 /* Consume the token. */
13223 cp_lexer_consume_token (parser->lexer);
13225 /* Look for the opening `('. */
13226 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
13227 return;
13228 /* Look for the string. */
13229 string = cp_parser_string_literal (parser, false, false);
13230 if (string == error_mark_node)
13232 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13233 /*consume_paren=*/true);
13234 return;
13237 /* If we're allowing GNU extensions, check for the extended assembly
13238 syntax. Unfortunately, the `:' tokens need not be separated by
13239 a space in C, and so, for compatibility, we tolerate that here
13240 too. Doing that means that we have to treat the `::' operator as
13241 two `:' tokens. */
13242 if (cp_parser_allow_gnu_extensions_p (parser)
13243 && parser->in_function_body
13244 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
13245 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
13247 bool inputs_p = false;
13248 bool clobbers_p = false;
13249 bool labels_p = false;
13251 /* The extended syntax was used. */
13252 extended_p = true;
13254 /* Look for outputs. */
13255 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13257 /* Consume the `:'. */
13258 cp_lexer_consume_token (parser->lexer);
13259 /* Parse the output-operands. */
13260 if (cp_lexer_next_token_is_not (parser->lexer,
13261 CPP_COLON)
13262 && cp_lexer_next_token_is_not (parser->lexer,
13263 CPP_SCOPE)
13264 && cp_lexer_next_token_is_not (parser->lexer,
13265 CPP_CLOSE_PAREN)
13266 && !goto_p)
13267 outputs = cp_parser_asm_operand_list (parser);
13269 if (outputs == error_mark_node)
13270 invalid_outputs_p = true;
13272 /* If the next token is `::', there are no outputs, and the
13273 next token is the beginning of the inputs. */
13274 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13275 /* The inputs are coming next. */
13276 inputs_p = true;
13278 /* Look for inputs. */
13279 if (inputs_p
13280 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13282 /* Consume the `:' or `::'. */
13283 cp_lexer_consume_token (parser->lexer);
13284 /* Parse the output-operands. */
13285 if (cp_lexer_next_token_is_not (parser->lexer,
13286 CPP_COLON)
13287 && cp_lexer_next_token_is_not (parser->lexer,
13288 CPP_SCOPE)
13289 && cp_lexer_next_token_is_not (parser->lexer,
13290 CPP_CLOSE_PAREN))
13291 inputs = cp_parser_asm_operand_list (parser);
13293 if (inputs == error_mark_node)
13294 invalid_inputs_p = true;
13296 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13297 /* The clobbers are coming next. */
13298 clobbers_p = true;
13300 /* Look for clobbers. */
13301 if (clobbers_p
13302 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13304 clobbers_p = true;
13305 /* Consume the `:' or `::'. */
13306 cp_lexer_consume_token (parser->lexer);
13307 /* Parse the clobbers. */
13308 if (cp_lexer_next_token_is_not (parser->lexer,
13309 CPP_COLON)
13310 && cp_lexer_next_token_is_not (parser->lexer,
13311 CPP_CLOSE_PAREN))
13312 clobbers = cp_parser_asm_clobber_list (parser);
13314 else if (goto_p
13315 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13316 /* The labels are coming next. */
13317 labels_p = true;
13319 /* Look for labels. */
13320 if (labels_p
13321 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
13323 labels_p = true;
13324 /* Consume the `:' or `::'. */
13325 cp_lexer_consume_token (parser->lexer);
13326 /* Parse the labels. */
13327 labels = cp_parser_asm_label_list (parser);
13330 if (goto_p && !labels_p)
13331 missing = clobbers_p ? "%<:%>" : "%<:%> or %<::%>";
13333 else if (goto_p)
13334 missing = "%<:%> or %<::%>";
13336 /* Look for the closing `)'. */
13337 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
13338 missing ? missing : "%<)%>"))
13339 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13340 /*consume_paren=*/true);
13341 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13343 if (!invalid_inputs_p && !invalid_outputs_p)
13345 /* Create the ASM_EXPR. */
13346 if (parser->in_function_body)
13348 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
13349 inputs, clobbers, labels);
13350 /* If the extended syntax was not used, mark the ASM_EXPR. */
13351 if (!extended_p)
13353 tree temp = asm_stmt;
13354 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
13355 temp = TREE_OPERAND (temp, 0);
13357 ASM_INPUT_P (temp) = 1;
13360 else
13361 cgraph_add_asm_node (string);
13365 /* Declarators [gram.dcl.decl] */
13367 /* Parse an init-declarator.
13369 init-declarator:
13370 declarator initializer [opt]
13372 GNU Extension:
13374 init-declarator:
13375 declarator asm-specification [opt] attributes [opt] initializer [opt]
13377 function-definition:
13378 decl-specifier-seq [opt] declarator ctor-initializer [opt]
13379 function-body
13380 decl-specifier-seq [opt] declarator function-try-block
13382 GNU Extension:
13384 function-definition:
13385 __extension__ function-definition
13387 The DECL_SPECIFIERS apply to this declarator. Returns a
13388 representation of the entity declared. If MEMBER_P is TRUE, then
13389 this declarator appears in a class scope. The new DECL created by
13390 this declarator is returned.
13392 The CHECKS are access checks that should be performed once we know
13393 what entity is being declared (and, therefore, what classes have
13394 befriended it).
13396 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
13397 for a function-definition here as well. If the declarator is a
13398 declarator for a function-definition, *FUNCTION_DEFINITION_P will
13399 be TRUE upon return. By that point, the function-definition will
13400 have been completely parsed.
13402 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
13403 is FALSE. */
13405 static tree
13406 cp_parser_init_declarator (cp_parser* parser,
13407 cp_decl_specifier_seq *decl_specifiers,
13408 VEC (deferred_access_check,gc)* checks,
13409 bool function_definition_allowed_p,
13410 bool member_p,
13411 int declares_class_or_enum,
13412 bool* function_definition_p)
13414 cp_token *token = NULL, *asm_spec_start_token = NULL,
13415 *attributes_start_token = NULL;
13416 cp_declarator *declarator;
13417 tree prefix_attributes;
13418 tree attributes;
13419 tree asm_specification;
13420 tree initializer;
13421 tree decl = NULL_TREE;
13422 tree scope;
13423 int is_initialized;
13424 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
13425 initialized with "= ..", CPP_OPEN_PAREN if initialized with
13426 "(...)". */
13427 enum cpp_ttype initialization_kind;
13428 bool is_direct_init = false;
13429 bool is_non_constant_init;
13430 int ctor_dtor_or_conv_p;
13431 bool friend_p;
13432 tree pushed_scope = NULL;
13434 /* Gather the attributes that were provided with the
13435 decl-specifiers. */
13436 prefix_attributes = decl_specifiers->attributes;
13438 /* Assume that this is not the declarator for a function
13439 definition. */
13440 if (function_definition_p)
13441 *function_definition_p = false;
13443 /* Defer access checks while parsing the declarator; we cannot know
13444 what names are accessible until we know what is being
13445 declared. */
13446 resume_deferring_access_checks ();
13448 /* Parse the declarator. */
13449 token = cp_lexer_peek_token (parser->lexer);
13450 declarator
13451 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13452 &ctor_dtor_or_conv_p,
13453 /*parenthesized_p=*/NULL,
13454 /*member_p=*/false);
13455 /* Gather up the deferred checks. */
13456 stop_deferring_access_checks ();
13458 /* If the DECLARATOR was erroneous, there's no need to go
13459 further. */
13460 if (declarator == cp_error_declarator)
13461 return error_mark_node;
13463 /* Check that the number of template-parameter-lists is OK. */
13464 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
13465 token->location))
13466 return error_mark_node;
13468 if (declares_class_or_enum & 2)
13469 cp_parser_check_for_definition_in_return_type (declarator,
13470 decl_specifiers->type,
13471 decl_specifiers->type_location);
13473 /* Figure out what scope the entity declared by the DECLARATOR is
13474 located in. `grokdeclarator' sometimes changes the scope, so
13475 we compute it now. */
13476 scope = get_scope_of_declarator (declarator);
13478 /* If we're allowing GNU extensions, look for an asm-specification
13479 and attributes. */
13480 if (cp_parser_allow_gnu_extensions_p (parser))
13482 /* Look for an asm-specification. */
13483 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
13484 asm_specification = cp_parser_asm_specification_opt (parser);
13485 /* And attributes. */
13486 attributes_start_token = cp_lexer_peek_token (parser->lexer);
13487 attributes = cp_parser_attributes_opt (parser);
13489 else
13491 asm_specification = NULL_TREE;
13492 attributes = NULL_TREE;
13495 /* Peek at the next token. */
13496 token = cp_lexer_peek_token (parser->lexer);
13497 /* Check to see if the token indicates the start of a
13498 function-definition. */
13499 if (function_declarator_p (declarator)
13500 && cp_parser_token_starts_function_definition_p (token))
13502 if (!function_definition_allowed_p)
13504 /* If a function-definition should not appear here, issue an
13505 error message. */
13506 cp_parser_error (parser,
13507 "a function-definition is not allowed here");
13508 return error_mark_node;
13510 else
13512 location_t func_brace_location
13513 = cp_lexer_peek_token (parser->lexer)->location;
13515 /* Neither attributes nor an asm-specification are allowed
13516 on a function-definition. */
13517 if (asm_specification)
13518 error_at (asm_spec_start_token->location,
13519 "an asm-specification is not allowed "
13520 "on a function-definition");
13521 if (attributes)
13522 error_at (attributes_start_token->location,
13523 "attributes are not allowed on a function-definition");
13524 /* This is a function-definition. */
13525 *function_definition_p = true;
13527 /* Parse the function definition. */
13528 if (member_p)
13529 decl = cp_parser_save_member_function_body (parser,
13530 decl_specifiers,
13531 declarator,
13532 prefix_attributes);
13533 else
13534 decl
13535 = (cp_parser_function_definition_from_specifiers_and_declarator
13536 (parser, decl_specifiers, prefix_attributes, declarator));
13538 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
13540 /* This is where the prologue starts... */
13541 DECL_STRUCT_FUNCTION (decl)->function_start_locus
13542 = func_brace_location;
13545 return decl;
13549 /* [dcl.dcl]
13551 Only in function declarations for constructors, destructors, and
13552 type conversions can the decl-specifier-seq be omitted.
13554 We explicitly postpone this check past the point where we handle
13555 function-definitions because we tolerate function-definitions
13556 that are missing their return types in some modes. */
13557 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
13559 cp_parser_error (parser,
13560 "expected constructor, destructor, or type conversion");
13561 return error_mark_node;
13564 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
13565 if (token->type == CPP_EQ
13566 || token->type == CPP_OPEN_PAREN
13567 || token->type == CPP_OPEN_BRACE)
13569 is_initialized = SD_INITIALIZED;
13570 initialization_kind = token->type;
13572 if (token->type == CPP_EQ
13573 && function_declarator_p (declarator))
13575 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13576 if (t2->keyword == RID_DEFAULT)
13577 is_initialized = SD_DEFAULTED;
13578 else if (t2->keyword == RID_DELETE)
13579 is_initialized = SD_DELETED;
13582 else
13584 /* If the init-declarator isn't initialized and isn't followed by a
13585 `,' or `;', it's not a valid init-declarator. */
13586 if (token->type != CPP_COMMA
13587 && token->type != CPP_SEMICOLON)
13589 cp_parser_error (parser, "expected initializer");
13590 return error_mark_node;
13592 is_initialized = SD_UNINITIALIZED;
13593 initialization_kind = CPP_EOF;
13596 /* Because start_decl has side-effects, we should only call it if we
13597 know we're going ahead. By this point, we know that we cannot
13598 possibly be looking at any other construct. */
13599 cp_parser_commit_to_tentative_parse (parser);
13601 /* If the decl specifiers were bad, issue an error now that we're
13602 sure this was intended to be a declarator. Then continue
13603 declaring the variable(s), as int, to try to cut down on further
13604 errors. */
13605 if (decl_specifiers->any_specifiers_p
13606 && decl_specifiers->type == error_mark_node)
13608 cp_parser_error (parser, "invalid type in declaration");
13609 decl_specifiers->type = integer_type_node;
13612 /* Check to see whether or not this declaration is a friend. */
13613 friend_p = cp_parser_friend_p (decl_specifiers);
13615 /* Enter the newly declared entry in the symbol table. If we're
13616 processing a declaration in a class-specifier, we wait until
13617 after processing the initializer. */
13618 if (!member_p)
13620 if (parser->in_unbraced_linkage_specification_p)
13621 decl_specifiers->storage_class = sc_extern;
13622 decl = start_decl (declarator, decl_specifiers,
13623 is_initialized, attributes, prefix_attributes,
13624 &pushed_scope);
13626 else if (scope)
13627 /* Enter the SCOPE. That way unqualified names appearing in the
13628 initializer will be looked up in SCOPE. */
13629 pushed_scope = push_scope (scope);
13631 /* Perform deferred access control checks, now that we know in which
13632 SCOPE the declared entity resides. */
13633 if (!member_p && decl)
13635 tree saved_current_function_decl = NULL_TREE;
13637 /* If the entity being declared is a function, pretend that we
13638 are in its scope. If it is a `friend', it may have access to
13639 things that would not otherwise be accessible. */
13640 if (TREE_CODE (decl) == FUNCTION_DECL)
13642 saved_current_function_decl = current_function_decl;
13643 current_function_decl = decl;
13646 /* Perform access checks for template parameters. */
13647 cp_parser_perform_template_parameter_access_checks (checks);
13649 /* Perform the access control checks for the declarator and the
13650 decl-specifiers. */
13651 perform_deferred_access_checks ();
13653 /* Restore the saved value. */
13654 if (TREE_CODE (decl) == FUNCTION_DECL)
13655 current_function_decl = saved_current_function_decl;
13658 /* Parse the initializer. */
13659 initializer = NULL_TREE;
13660 is_direct_init = false;
13661 is_non_constant_init = true;
13662 if (is_initialized)
13664 if (function_declarator_p (declarator))
13666 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
13667 if (initialization_kind == CPP_EQ)
13668 initializer = cp_parser_pure_specifier (parser);
13669 else
13671 /* If the declaration was erroneous, we don't really
13672 know what the user intended, so just silently
13673 consume the initializer. */
13674 if (decl != error_mark_node)
13675 error_at (initializer_start_token->location,
13676 "initializer provided for function");
13677 cp_parser_skip_to_closing_parenthesis (parser,
13678 /*recovering=*/true,
13679 /*or_comma=*/false,
13680 /*consume_paren=*/true);
13683 else
13685 /* We want to record the extra mangling scope for in-class
13686 initializers of class members and initializers of static data
13687 member templates. The former is a C++0x feature which isn't
13688 implemented yet, and I expect it will involve deferring
13689 parsing of the initializer until end of class as with default
13690 arguments. So right here we only handle the latter. */
13691 if (!member_p && processing_template_decl)
13692 start_lambda_scope (decl);
13693 initializer = cp_parser_initializer (parser,
13694 &is_direct_init,
13695 &is_non_constant_init);
13696 if (!member_p && processing_template_decl)
13697 finish_lambda_scope ();
13701 /* The old parser allows attributes to appear after a parenthesized
13702 initializer. Mark Mitchell proposed removing this functionality
13703 on the GCC mailing lists on 2002-08-13. This parser accepts the
13704 attributes -- but ignores them. */
13705 if (cp_parser_allow_gnu_extensions_p (parser)
13706 && initialization_kind == CPP_OPEN_PAREN)
13707 if (cp_parser_attributes_opt (parser))
13708 warning (OPT_Wattributes,
13709 "attributes after parenthesized initializer ignored");
13711 /* For an in-class declaration, use `grokfield' to create the
13712 declaration. */
13713 if (member_p)
13715 if (pushed_scope)
13717 pop_scope (pushed_scope);
13718 pushed_scope = false;
13720 decl = grokfield (declarator, decl_specifiers,
13721 initializer, !is_non_constant_init,
13722 /*asmspec=*/NULL_TREE,
13723 prefix_attributes);
13724 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
13725 cp_parser_save_default_args (parser, decl);
13728 /* Finish processing the declaration. But, skip friend
13729 declarations. */
13730 if (!friend_p && decl && decl != error_mark_node)
13732 cp_finish_decl (decl,
13733 initializer, !is_non_constant_init,
13734 asm_specification,
13735 /* If the initializer is in parentheses, then this is
13736 a direct-initialization, which means that an
13737 `explicit' constructor is OK. Otherwise, an
13738 `explicit' constructor cannot be used. */
13739 ((is_direct_init || !is_initialized)
13740 ? 0 : LOOKUP_ONLYCONVERTING));
13742 else if ((cxx_dialect != cxx98) && friend_p
13743 && decl && TREE_CODE (decl) == FUNCTION_DECL)
13744 /* Core issue #226 (C++0x only): A default template-argument
13745 shall not be specified in a friend class template
13746 declaration. */
13747 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
13748 /*is_partial=*/0, /*is_friend_decl=*/1);
13750 if (!friend_p && pushed_scope)
13751 pop_scope (pushed_scope);
13753 return decl;
13756 /* Parse a declarator.
13758 declarator:
13759 direct-declarator
13760 ptr-operator declarator
13762 abstract-declarator:
13763 ptr-operator abstract-declarator [opt]
13764 direct-abstract-declarator
13766 GNU Extensions:
13768 declarator:
13769 attributes [opt] direct-declarator
13770 attributes [opt] ptr-operator declarator
13772 abstract-declarator:
13773 attributes [opt] ptr-operator abstract-declarator [opt]
13774 attributes [opt] direct-abstract-declarator
13776 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
13777 detect constructor, destructor or conversion operators. It is set
13778 to -1 if the declarator is a name, and +1 if it is a
13779 function. Otherwise it is set to zero. Usually you just want to
13780 test for >0, but internally the negative value is used.
13782 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
13783 a decl-specifier-seq unless it declares a constructor, destructor,
13784 or conversion. It might seem that we could check this condition in
13785 semantic analysis, rather than parsing, but that makes it difficult
13786 to handle something like `f()'. We want to notice that there are
13787 no decl-specifiers, and therefore realize that this is an
13788 expression, not a declaration.)
13790 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
13791 the declarator is a direct-declarator of the form "(...)".
13793 MEMBER_P is true iff this declarator is a member-declarator. */
13795 static cp_declarator *
13796 cp_parser_declarator (cp_parser* parser,
13797 cp_parser_declarator_kind dcl_kind,
13798 int* ctor_dtor_or_conv_p,
13799 bool* parenthesized_p,
13800 bool member_p)
13802 cp_token *token;
13803 cp_declarator *declarator;
13804 enum tree_code code;
13805 cp_cv_quals cv_quals;
13806 tree class_type;
13807 tree attributes = NULL_TREE;
13809 /* Assume this is not a constructor, destructor, or type-conversion
13810 operator. */
13811 if (ctor_dtor_or_conv_p)
13812 *ctor_dtor_or_conv_p = 0;
13814 if (cp_parser_allow_gnu_extensions_p (parser))
13815 attributes = cp_parser_attributes_opt (parser);
13817 /* Peek at the next token. */
13818 token = cp_lexer_peek_token (parser->lexer);
13820 /* Check for the ptr-operator production. */
13821 cp_parser_parse_tentatively (parser);
13822 /* Parse the ptr-operator. */
13823 code = cp_parser_ptr_operator (parser,
13824 &class_type,
13825 &cv_quals);
13826 /* If that worked, then we have a ptr-operator. */
13827 if (cp_parser_parse_definitely (parser))
13829 /* If a ptr-operator was found, then this declarator was not
13830 parenthesized. */
13831 if (parenthesized_p)
13832 *parenthesized_p = true;
13833 /* The dependent declarator is optional if we are parsing an
13834 abstract-declarator. */
13835 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13836 cp_parser_parse_tentatively (parser);
13838 /* Parse the dependent declarator. */
13839 declarator = cp_parser_declarator (parser, dcl_kind,
13840 /*ctor_dtor_or_conv_p=*/NULL,
13841 /*parenthesized_p=*/NULL,
13842 /*member_p=*/false);
13844 /* If we are parsing an abstract-declarator, we must handle the
13845 case where the dependent declarator is absent. */
13846 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
13847 && !cp_parser_parse_definitely (parser))
13848 declarator = NULL;
13850 declarator = cp_parser_make_indirect_declarator
13851 (code, class_type, cv_quals, declarator);
13853 /* Everything else is a direct-declarator. */
13854 else
13856 if (parenthesized_p)
13857 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
13858 CPP_OPEN_PAREN);
13859 declarator = cp_parser_direct_declarator (parser, dcl_kind,
13860 ctor_dtor_or_conv_p,
13861 member_p);
13864 if (attributes && declarator && declarator != cp_error_declarator)
13865 declarator->attributes = attributes;
13867 return declarator;
13870 /* Parse a direct-declarator or direct-abstract-declarator.
13872 direct-declarator:
13873 declarator-id
13874 direct-declarator ( parameter-declaration-clause )
13875 cv-qualifier-seq [opt]
13876 exception-specification [opt]
13877 direct-declarator [ constant-expression [opt] ]
13878 ( declarator )
13880 direct-abstract-declarator:
13881 direct-abstract-declarator [opt]
13882 ( parameter-declaration-clause )
13883 cv-qualifier-seq [opt]
13884 exception-specification [opt]
13885 direct-abstract-declarator [opt] [ constant-expression [opt] ]
13886 ( abstract-declarator )
13888 Returns a representation of the declarator. DCL_KIND is
13889 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
13890 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
13891 we are parsing a direct-declarator. It is
13892 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
13893 of ambiguity we prefer an abstract declarator, as per
13894 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
13895 cp_parser_declarator. */
13897 static cp_declarator *
13898 cp_parser_direct_declarator (cp_parser* parser,
13899 cp_parser_declarator_kind dcl_kind,
13900 int* ctor_dtor_or_conv_p,
13901 bool member_p)
13903 cp_token *token;
13904 cp_declarator *declarator = NULL;
13905 tree scope = NULL_TREE;
13906 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13907 bool saved_in_declarator_p = parser->in_declarator_p;
13908 bool first = true;
13909 tree pushed_scope = NULL_TREE;
13911 while (true)
13913 /* Peek at the next token. */
13914 token = cp_lexer_peek_token (parser->lexer);
13915 if (token->type == CPP_OPEN_PAREN)
13917 /* This is either a parameter-declaration-clause, or a
13918 parenthesized declarator. When we know we are parsing a
13919 named declarator, it must be a parenthesized declarator
13920 if FIRST is true. For instance, `(int)' is a
13921 parameter-declaration-clause, with an omitted
13922 direct-abstract-declarator. But `((*))', is a
13923 parenthesized abstract declarator. Finally, when T is a
13924 template parameter `(T)' is a
13925 parameter-declaration-clause, and not a parenthesized
13926 named declarator.
13928 We first try and parse a parameter-declaration-clause,
13929 and then try a nested declarator (if FIRST is true).
13931 It is not an error for it not to be a
13932 parameter-declaration-clause, even when FIRST is
13933 false. Consider,
13935 int i (int);
13936 int i (3);
13938 The first is the declaration of a function while the
13939 second is the definition of a variable, including its
13940 initializer.
13942 Having seen only the parenthesis, we cannot know which of
13943 these two alternatives should be selected. Even more
13944 complex are examples like:
13946 int i (int (a));
13947 int i (int (3));
13949 The former is a function-declaration; the latter is a
13950 variable initialization.
13952 Thus again, we try a parameter-declaration-clause, and if
13953 that fails, we back out and return. */
13955 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13957 tree params;
13958 unsigned saved_num_template_parameter_lists;
13959 bool is_declarator = false;
13960 tree t;
13962 /* In a member-declarator, the only valid interpretation
13963 of a parenthesis is the start of a
13964 parameter-declaration-clause. (It is invalid to
13965 initialize a static data member with a parenthesized
13966 initializer; only the "=" form of initialization is
13967 permitted.) */
13968 if (!member_p)
13969 cp_parser_parse_tentatively (parser);
13971 /* Consume the `('. */
13972 cp_lexer_consume_token (parser->lexer);
13973 if (first)
13975 /* If this is going to be an abstract declarator, we're
13976 in a declarator and we can't have default args. */
13977 parser->default_arg_ok_p = false;
13978 parser->in_declarator_p = true;
13981 /* Inside the function parameter list, surrounding
13982 template-parameter-lists do not apply. */
13983 saved_num_template_parameter_lists
13984 = parser->num_template_parameter_lists;
13985 parser->num_template_parameter_lists = 0;
13987 begin_scope (sk_function_parms, NULL_TREE);
13989 /* Parse the parameter-declaration-clause. */
13990 params = cp_parser_parameter_declaration_clause (parser);
13992 parser->num_template_parameter_lists
13993 = saved_num_template_parameter_lists;
13995 /* If all went well, parse the cv-qualifier-seq and the
13996 exception-specification. */
13997 if (member_p || cp_parser_parse_definitely (parser))
13999 cp_cv_quals cv_quals;
14000 tree exception_specification;
14001 tree late_return;
14003 is_declarator = true;
14005 if (ctor_dtor_or_conv_p)
14006 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
14007 first = false;
14008 /* Consume the `)'. */
14009 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
14011 /* Parse the cv-qualifier-seq. */
14012 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14013 /* And the exception-specification. */
14014 exception_specification
14015 = cp_parser_exception_specification_opt (parser);
14017 late_return
14018 = cp_parser_late_return_type_opt (parser);
14020 /* Create the function-declarator. */
14021 declarator = make_call_declarator (declarator,
14022 params,
14023 cv_quals,
14024 exception_specification,
14025 late_return);
14026 /* Any subsequent parameter lists are to do with
14027 return type, so are not those of the declared
14028 function. */
14029 parser->default_arg_ok_p = false;
14032 /* Remove the function parms from scope. */
14033 for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
14034 pop_binding (DECL_NAME (t), t);
14035 leave_scope();
14037 if (is_declarator)
14038 /* Repeat the main loop. */
14039 continue;
14042 /* If this is the first, we can try a parenthesized
14043 declarator. */
14044 if (first)
14046 bool saved_in_type_id_in_expr_p;
14048 parser->default_arg_ok_p = saved_default_arg_ok_p;
14049 parser->in_declarator_p = saved_in_declarator_p;
14051 /* Consume the `('. */
14052 cp_lexer_consume_token (parser->lexer);
14053 /* Parse the nested declarator. */
14054 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14055 parser->in_type_id_in_expr_p = true;
14056 declarator
14057 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
14058 /*parenthesized_p=*/NULL,
14059 member_p);
14060 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14061 first = false;
14062 /* Expect a `)'. */
14063 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
14064 declarator = cp_error_declarator;
14065 if (declarator == cp_error_declarator)
14066 break;
14068 goto handle_declarator;
14070 /* Otherwise, we must be done. */
14071 else
14072 break;
14074 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14075 && token->type == CPP_OPEN_SQUARE)
14077 /* Parse an array-declarator. */
14078 tree bounds;
14080 if (ctor_dtor_or_conv_p)
14081 *ctor_dtor_or_conv_p = 0;
14083 first = false;
14084 parser->default_arg_ok_p = false;
14085 parser->in_declarator_p = true;
14086 /* Consume the `['. */
14087 cp_lexer_consume_token (parser->lexer);
14088 /* Peek at the next token. */
14089 token = cp_lexer_peek_token (parser->lexer);
14090 /* If the next token is `]', then there is no
14091 constant-expression. */
14092 if (token->type != CPP_CLOSE_SQUARE)
14094 bool non_constant_p;
14096 bounds
14097 = cp_parser_constant_expression (parser,
14098 /*allow_non_constant=*/true,
14099 &non_constant_p);
14100 if (!non_constant_p)
14101 bounds = fold_non_dependent_expr (bounds);
14102 /* Normally, the array bound must be an integral constant
14103 expression. However, as an extension, we allow VLAs
14104 in function scopes. */
14105 else if (!parser->in_function_body)
14107 error_at (token->location,
14108 "array bound is not an integer constant");
14109 bounds = error_mark_node;
14111 else if (processing_template_decl && !error_operand_p (bounds))
14113 /* Remember this wasn't a constant-expression. */
14114 bounds = build_nop (TREE_TYPE (bounds), bounds);
14115 TREE_SIDE_EFFECTS (bounds) = 1;
14118 else
14119 bounds = NULL_TREE;
14120 /* Look for the closing `]'. */
14121 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
14123 declarator = cp_error_declarator;
14124 break;
14127 declarator = make_array_declarator (declarator, bounds);
14129 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
14132 tree qualifying_scope;
14133 tree unqualified_name;
14134 special_function_kind sfk;
14135 bool abstract_ok;
14136 bool pack_expansion_p = false;
14137 cp_token *declarator_id_start_token;
14139 /* Parse a declarator-id */
14140 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
14141 if (abstract_ok)
14143 cp_parser_parse_tentatively (parser);
14145 /* If we see an ellipsis, we should be looking at a
14146 parameter pack. */
14147 if (token->type == CPP_ELLIPSIS)
14149 /* Consume the `...' */
14150 cp_lexer_consume_token (parser->lexer);
14152 pack_expansion_p = true;
14156 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
14157 unqualified_name
14158 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
14159 qualifying_scope = parser->scope;
14160 if (abstract_ok)
14162 bool okay = false;
14164 if (!unqualified_name && pack_expansion_p)
14166 /* Check whether an error occurred. */
14167 okay = !cp_parser_error_occurred (parser);
14169 /* We already consumed the ellipsis to mark a
14170 parameter pack, but we have no way to report it,
14171 so abort the tentative parse. We will be exiting
14172 immediately anyway. */
14173 cp_parser_abort_tentative_parse (parser);
14175 else
14176 okay = cp_parser_parse_definitely (parser);
14178 if (!okay)
14179 unqualified_name = error_mark_node;
14180 else if (unqualified_name
14181 && (qualifying_scope
14182 || (TREE_CODE (unqualified_name)
14183 != IDENTIFIER_NODE)))
14185 cp_parser_error (parser, "expected unqualified-id");
14186 unqualified_name = error_mark_node;
14190 if (!unqualified_name)
14191 return NULL;
14192 if (unqualified_name == error_mark_node)
14194 declarator = cp_error_declarator;
14195 pack_expansion_p = false;
14196 declarator->parameter_pack_p = false;
14197 break;
14200 if (qualifying_scope && at_namespace_scope_p ()
14201 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
14203 /* In the declaration of a member of a template class
14204 outside of the class itself, the SCOPE will sometimes
14205 be a TYPENAME_TYPE. For example, given:
14207 template <typename T>
14208 int S<T>::R::i = 3;
14210 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
14211 this context, we must resolve S<T>::R to an ordinary
14212 type, rather than a typename type.
14214 The reason we normally avoid resolving TYPENAME_TYPEs
14215 is that a specialization of `S' might render
14216 `S<T>::R' not a type. However, if `S' is
14217 specialized, then this `i' will not be used, so there
14218 is no harm in resolving the types here. */
14219 tree type;
14221 /* Resolve the TYPENAME_TYPE. */
14222 type = resolve_typename_type (qualifying_scope,
14223 /*only_current_p=*/false);
14224 /* If that failed, the declarator is invalid. */
14225 if (TREE_CODE (type) == TYPENAME_TYPE)
14227 if (typedef_variant_p (type))
14228 error_at (declarator_id_start_token->location,
14229 "cannot define member of dependent typedef "
14230 "%qT", type);
14231 else
14232 error_at (declarator_id_start_token->location,
14233 "%<%T::%E%> is not a type",
14234 TYPE_CONTEXT (qualifying_scope),
14235 TYPE_IDENTIFIER (qualifying_scope));
14237 qualifying_scope = type;
14240 sfk = sfk_none;
14242 if (unqualified_name)
14244 tree class_type;
14246 if (qualifying_scope
14247 && CLASS_TYPE_P (qualifying_scope))
14248 class_type = qualifying_scope;
14249 else
14250 class_type = current_class_type;
14252 if (TREE_CODE (unqualified_name) == TYPE_DECL)
14254 tree name_type = TREE_TYPE (unqualified_name);
14255 if (class_type && same_type_p (name_type, class_type))
14257 if (qualifying_scope
14258 && CLASSTYPE_USE_TEMPLATE (name_type))
14260 error_at (declarator_id_start_token->location,
14261 "invalid use of constructor as a template");
14262 inform (declarator_id_start_token->location,
14263 "use %<%T::%D%> instead of %<%T::%D%> to "
14264 "name the constructor in a qualified name",
14265 class_type,
14266 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
14267 class_type, name_type);
14268 declarator = cp_error_declarator;
14269 break;
14271 else
14272 unqualified_name = constructor_name (class_type);
14274 else
14276 /* We do not attempt to print the declarator
14277 here because we do not have enough
14278 information about its original syntactic
14279 form. */
14280 cp_parser_error (parser, "invalid declarator");
14281 declarator = cp_error_declarator;
14282 break;
14286 if (class_type)
14288 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
14289 sfk = sfk_destructor;
14290 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
14291 sfk = sfk_conversion;
14292 else if (/* There's no way to declare a constructor
14293 for an anonymous type, even if the type
14294 got a name for linkage purposes. */
14295 !TYPE_WAS_ANONYMOUS (class_type)
14296 && constructor_name_p (unqualified_name,
14297 class_type))
14299 unqualified_name = constructor_name (class_type);
14300 sfk = sfk_constructor;
14302 else if (is_overloaded_fn (unqualified_name)
14303 && DECL_CONSTRUCTOR_P (get_first_fn
14304 (unqualified_name)))
14305 sfk = sfk_constructor;
14307 if (ctor_dtor_or_conv_p && sfk != sfk_none)
14308 *ctor_dtor_or_conv_p = -1;
14311 declarator = make_id_declarator (qualifying_scope,
14312 unqualified_name,
14313 sfk);
14314 declarator->id_loc = token->location;
14315 declarator->parameter_pack_p = pack_expansion_p;
14317 if (pack_expansion_p)
14318 maybe_warn_variadic_templates ();
14321 handle_declarator:;
14322 scope = get_scope_of_declarator (declarator);
14323 if (scope)
14324 /* Any names that appear after the declarator-id for a
14325 member are looked up in the containing scope. */
14326 pushed_scope = push_scope (scope);
14327 parser->in_declarator_p = true;
14328 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
14329 || (declarator && declarator->kind == cdk_id))
14330 /* Default args are only allowed on function
14331 declarations. */
14332 parser->default_arg_ok_p = saved_default_arg_ok_p;
14333 else
14334 parser->default_arg_ok_p = false;
14336 first = false;
14338 /* We're done. */
14339 else
14340 break;
14343 /* For an abstract declarator, we might wind up with nothing at this
14344 point. That's an error; the declarator is not optional. */
14345 if (!declarator)
14346 cp_parser_error (parser, "expected declarator");
14348 /* If we entered a scope, we must exit it now. */
14349 if (pushed_scope)
14350 pop_scope (pushed_scope);
14352 parser->default_arg_ok_p = saved_default_arg_ok_p;
14353 parser->in_declarator_p = saved_in_declarator_p;
14355 return declarator;
14358 /* Parse a ptr-operator.
14360 ptr-operator:
14361 * cv-qualifier-seq [opt]
14363 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
14365 GNU Extension:
14367 ptr-operator:
14368 & cv-qualifier-seq [opt]
14370 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
14371 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
14372 an rvalue reference. In the case of a pointer-to-member, *TYPE is
14373 filled in with the TYPE containing the member. *CV_QUALS is
14374 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
14375 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
14376 Note that the tree codes returned by this function have nothing
14377 to do with the types of trees that will be eventually be created
14378 to represent the pointer or reference type being parsed. They are
14379 just constants with suggestive names. */
14380 static enum tree_code
14381 cp_parser_ptr_operator (cp_parser* parser,
14382 tree* type,
14383 cp_cv_quals *cv_quals)
14385 enum tree_code code = ERROR_MARK;
14386 cp_token *token;
14388 /* Assume that it's not a pointer-to-member. */
14389 *type = NULL_TREE;
14390 /* And that there are no cv-qualifiers. */
14391 *cv_quals = TYPE_UNQUALIFIED;
14393 /* Peek at the next token. */
14394 token = cp_lexer_peek_token (parser->lexer);
14396 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
14397 if (token->type == CPP_MULT)
14398 code = INDIRECT_REF;
14399 else if (token->type == CPP_AND)
14400 code = ADDR_EXPR;
14401 else if ((cxx_dialect != cxx98) &&
14402 token->type == CPP_AND_AND) /* C++0x only */
14403 code = NON_LVALUE_EXPR;
14405 if (code != ERROR_MARK)
14407 /* Consume the `*', `&' or `&&'. */
14408 cp_lexer_consume_token (parser->lexer);
14410 /* A `*' can be followed by a cv-qualifier-seq, and so can a
14411 `&', if we are allowing GNU extensions. (The only qualifier
14412 that can legally appear after `&' is `restrict', but that is
14413 enforced during semantic analysis. */
14414 if (code == INDIRECT_REF
14415 || cp_parser_allow_gnu_extensions_p (parser))
14416 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14418 else
14420 /* Try the pointer-to-member case. */
14421 cp_parser_parse_tentatively (parser);
14422 /* Look for the optional `::' operator. */
14423 cp_parser_global_scope_opt (parser,
14424 /*current_scope_valid_p=*/false);
14425 /* Look for the nested-name specifier. */
14426 token = cp_lexer_peek_token (parser->lexer);
14427 cp_parser_nested_name_specifier (parser,
14428 /*typename_keyword_p=*/false,
14429 /*check_dependency_p=*/true,
14430 /*type_p=*/false,
14431 /*is_declaration=*/false);
14432 /* If we found it, and the next token is a `*', then we are
14433 indeed looking at a pointer-to-member operator. */
14434 if (!cp_parser_error_occurred (parser)
14435 && cp_parser_require (parser, CPP_MULT, "%<*%>"))
14437 /* Indicate that the `*' operator was used. */
14438 code = INDIRECT_REF;
14440 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
14441 error_at (token->location, "%qD is a namespace", parser->scope);
14442 else
14444 /* The type of which the member is a member is given by the
14445 current SCOPE. */
14446 *type = parser->scope;
14447 /* The next name will not be qualified. */
14448 parser->scope = NULL_TREE;
14449 parser->qualifying_scope = NULL_TREE;
14450 parser->object_scope = NULL_TREE;
14451 /* Look for the optional cv-qualifier-seq. */
14452 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14455 /* If that didn't work we don't have a ptr-operator. */
14456 if (!cp_parser_parse_definitely (parser))
14457 cp_parser_error (parser, "expected ptr-operator");
14460 return code;
14463 /* Parse an (optional) cv-qualifier-seq.
14465 cv-qualifier-seq:
14466 cv-qualifier cv-qualifier-seq [opt]
14468 cv-qualifier:
14469 const
14470 volatile
14472 GNU Extension:
14474 cv-qualifier:
14475 __restrict__
14477 Returns a bitmask representing the cv-qualifiers. */
14479 static cp_cv_quals
14480 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
14482 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
14484 while (true)
14486 cp_token *token;
14487 cp_cv_quals cv_qualifier;
14489 /* Peek at the next token. */
14490 token = cp_lexer_peek_token (parser->lexer);
14491 /* See if it's a cv-qualifier. */
14492 switch (token->keyword)
14494 case RID_CONST:
14495 cv_qualifier = TYPE_QUAL_CONST;
14496 break;
14498 case RID_VOLATILE:
14499 cv_qualifier = TYPE_QUAL_VOLATILE;
14500 break;
14502 case RID_RESTRICT:
14503 cv_qualifier = TYPE_QUAL_RESTRICT;
14504 break;
14506 default:
14507 cv_qualifier = TYPE_UNQUALIFIED;
14508 break;
14511 if (!cv_qualifier)
14512 break;
14514 if (cv_quals & cv_qualifier)
14516 error_at (token->location, "duplicate cv-qualifier");
14517 cp_lexer_purge_token (parser->lexer);
14519 else
14521 cp_lexer_consume_token (parser->lexer);
14522 cv_quals |= cv_qualifier;
14526 return cv_quals;
14529 /* Parse a late-specified return type, if any. This is not a separate
14530 non-terminal, but part of a function declarator, which looks like
14532 -> trailing-type-specifier-seq abstract-declarator(opt)
14534 Returns the type indicated by the type-id. */
14536 static tree
14537 cp_parser_late_return_type_opt (cp_parser* parser)
14539 cp_token *token;
14541 /* Peek at the next token. */
14542 token = cp_lexer_peek_token (parser->lexer);
14543 /* A late-specified return type is indicated by an initial '->'. */
14544 if (token->type != CPP_DEREF)
14545 return NULL_TREE;
14547 /* Consume the ->. */
14548 cp_lexer_consume_token (parser->lexer);
14550 return cp_parser_trailing_type_id (parser);
14553 /* Parse a declarator-id.
14555 declarator-id:
14556 id-expression
14557 :: [opt] nested-name-specifier [opt] type-name
14559 In the `id-expression' case, the value returned is as for
14560 cp_parser_id_expression if the id-expression was an unqualified-id.
14561 If the id-expression was a qualified-id, then a SCOPE_REF is
14562 returned. The first operand is the scope (either a NAMESPACE_DECL
14563 or TREE_TYPE), but the second is still just a representation of an
14564 unqualified-id. */
14566 static tree
14567 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
14569 tree id;
14570 /* The expression must be an id-expression. Assume that qualified
14571 names are the names of types so that:
14573 template <class T>
14574 int S<T>::R::i = 3;
14576 will work; we must treat `S<T>::R' as the name of a type.
14577 Similarly, assume that qualified names are templates, where
14578 required, so that:
14580 template <class T>
14581 int S<T>::R<T>::i = 3;
14583 will work, too. */
14584 id = cp_parser_id_expression (parser,
14585 /*template_keyword_p=*/false,
14586 /*check_dependency_p=*/false,
14587 /*template_p=*/NULL,
14588 /*declarator_p=*/true,
14589 optional_p);
14590 if (id && BASELINK_P (id))
14591 id = BASELINK_FUNCTIONS (id);
14592 return id;
14595 /* Parse a type-id.
14597 type-id:
14598 type-specifier-seq abstract-declarator [opt]
14600 Returns the TYPE specified. */
14602 static tree
14603 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
14604 bool is_trailing_return)
14606 cp_decl_specifier_seq type_specifier_seq;
14607 cp_declarator *abstract_declarator;
14609 /* Parse the type-specifier-seq. */
14610 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14611 is_trailing_return,
14612 &type_specifier_seq);
14613 if (type_specifier_seq.type == error_mark_node)
14614 return error_mark_node;
14616 /* There might or might not be an abstract declarator. */
14617 cp_parser_parse_tentatively (parser);
14618 /* Look for the declarator. */
14619 abstract_declarator
14620 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
14621 /*parenthesized_p=*/NULL,
14622 /*member_p=*/false);
14623 /* Check to see if there really was a declarator. */
14624 if (!cp_parser_parse_definitely (parser))
14625 abstract_declarator = NULL;
14627 if (type_specifier_seq.type
14628 && type_uses_auto (type_specifier_seq.type))
14630 /* A type-id with type 'auto' is only ok if the abstract declarator
14631 is a function declarator with a late-specified return type. */
14632 if (abstract_declarator
14633 && abstract_declarator->kind == cdk_function
14634 && abstract_declarator->u.function.late_return_type)
14635 /* OK */;
14636 else
14638 error ("invalid use of %<auto%>");
14639 return error_mark_node;
14643 return groktypename (&type_specifier_seq, abstract_declarator,
14644 is_template_arg);
14647 static tree cp_parser_type_id (cp_parser *parser)
14649 return cp_parser_type_id_1 (parser, false, false);
14652 static tree cp_parser_template_type_arg (cp_parser *parser)
14654 return cp_parser_type_id_1 (parser, true, false);
14657 static tree cp_parser_trailing_type_id (cp_parser *parser)
14659 return cp_parser_type_id_1 (parser, false, true);
14662 /* Parse a type-specifier-seq.
14664 type-specifier-seq:
14665 type-specifier type-specifier-seq [opt]
14667 GNU extension:
14669 type-specifier-seq:
14670 attributes type-specifier-seq [opt]
14672 If IS_DECLARATION is true, we are at the start of a "condition" or
14673 exception-declaration, so we might be followed by a declarator-id.
14675 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
14676 i.e. we've just seen "->".
14678 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
14680 static void
14681 cp_parser_type_specifier_seq (cp_parser* parser,
14682 bool is_declaration,
14683 bool is_trailing_return,
14684 cp_decl_specifier_seq *type_specifier_seq)
14686 bool seen_type_specifier = false;
14687 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
14688 cp_token *start_token = NULL;
14690 /* Clear the TYPE_SPECIFIER_SEQ. */
14691 clear_decl_specs (type_specifier_seq);
14693 /* In the context of a trailing return type, enum E { } is an
14694 elaborated-type-specifier followed by a function-body, not an
14695 enum-specifier. */
14696 if (is_trailing_return)
14697 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
14699 /* Parse the type-specifiers and attributes. */
14700 while (true)
14702 tree type_specifier;
14703 bool is_cv_qualifier;
14705 /* Check for attributes first. */
14706 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
14708 type_specifier_seq->attributes =
14709 chainon (type_specifier_seq->attributes,
14710 cp_parser_attributes_opt (parser));
14711 continue;
14714 /* record the token of the beginning of the type specifier seq,
14715 for error reporting purposes*/
14716 if (!start_token)
14717 start_token = cp_lexer_peek_token (parser->lexer);
14719 /* Look for the type-specifier. */
14720 type_specifier = cp_parser_type_specifier (parser,
14721 flags,
14722 type_specifier_seq,
14723 /*is_declaration=*/false,
14724 NULL,
14725 &is_cv_qualifier);
14726 if (!type_specifier)
14728 /* If the first type-specifier could not be found, this is not a
14729 type-specifier-seq at all. */
14730 if (!seen_type_specifier)
14732 cp_parser_error (parser, "expected type-specifier");
14733 type_specifier_seq->type = error_mark_node;
14734 return;
14736 /* If subsequent type-specifiers could not be found, the
14737 type-specifier-seq is complete. */
14738 break;
14741 seen_type_specifier = true;
14742 /* The standard says that a condition can be:
14744 type-specifier-seq declarator = assignment-expression
14746 However, given:
14748 struct S {};
14749 if (int S = ...)
14751 we should treat the "S" as a declarator, not as a
14752 type-specifier. The standard doesn't say that explicitly for
14753 type-specifier-seq, but it does say that for
14754 decl-specifier-seq in an ordinary declaration. Perhaps it
14755 would be clearer just to allow a decl-specifier-seq here, and
14756 then add a semantic restriction that if any decl-specifiers
14757 that are not type-specifiers appear, the program is invalid. */
14758 if (is_declaration && !is_cv_qualifier)
14759 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
14762 cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
14765 /* Parse a parameter-declaration-clause.
14767 parameter-declaration-clause:
14768 parameter-declaration-list [opt] ... [opt]
14769 parameter-declaration-list , ...
14771 Returns a representation for the parameter declarations. A return
14772 value of NULL indicates a parameter-declaration-clause consisting
14773 only of an ellipsis. */
14775 static tree
14776 cp_parser_parameter_declaration_clause (cp_parser* parser)
14778 tree parameters;
14779 cp_token *token;
14780 bool ellipsis_p;
14781 bool is_error;
14783 /* Peek at the next token. */
14784 token = cp_lexer_peek_token (parser->lexer);
14785 /* Check for trivial parameter-declaration-clauses. */
14786 if (token->type == CPP_ELLIPSIS)
14788 /* Consume the `...' token. */
14789 cp_lexer_consume_token (parser->lexer);
14790 return NULL_TREE;
14792 else if (token->type == CPP_CLOSE_PAREN)
14793 /* There are no parameters. */
14795 #ifndef NO_IMPLICIT_EXTERN_C
14796 if (in_system_header && current_class_type == NULL
14797 && current_lang_name == lang_name_c)
14798 return NULL_TREE;
14799 else
14800 #endif
14801 return void_list_node;
14803 /* Check for `(void)', too, which is a special case. */
14804 else if (token->keyword == RID_VOID
14805 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
14806 == CPP_CLOSE_PAREN))
14808 /* Consume the `void' token. */
14809 cp_lexer_consume_token (parser->lexer);
14810 /* There are no parameters. */
14811 return void_list_node;
14814 /* Parse the parameter-declaration-list. */
14815 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
14816 /* If a parse error occurred while parsing the
14817 parameter-declaration-list, then the entire
14818 parameter-declaration-clause is erroneous. */
14819 if (is_error)
14820 return NULL;
14822 /* Peek at the next token. */
14823 token = cp_lexer_peek_token (parser->lexer);
14824 /* If it's a `,', the clause should terminate with an ellipsis. */
14825 if (token->type == CPP_COMMA)
14827 /* Consume the `,'. */
14828 cp_lexer_consume_token (parser->lexer);
14829 /* Expect an ellipsis. */
14830 ellipsis_p
14831 = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
14833 /* It might also be `...' if the optional trailing `,' was
14834 omitted. */
14835 else if (token->type == CPP_ELLIPSIS)
14837 /* Consume the `...' token. */
14838 cp_lexer_consume_token (parser->lexer);
14839 /* And remember that we saw it. */
14840 ellipsis_p = true;
14842 else
14843 ellipsis_p = false;
14845 /* Finish the parameter list. */
14846 if (!ellipsis_p)
14847 parameters = chainon (parameters, void_list_node);
14849 return parameters;
14852 /* Parse a parameter-declaration-list.
14854 parameter-declaration-list:
14855 parameter-declaration
14856 parameter-declaration-list , parameter-declaration
14858 Returns a representation of the parameter-declaration-list, as for
14859 cp_parser_parameter_declaration_clause. However, the
14860 `void_list_node' is never appended to the list. Upon return,
14861 *IS_ERROR will be true iff an error occurred. */
14863 static tree
14864 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
14866 tree parameters = NULL_TREE;
14867 tree *tail = &parameters;
14868 bool saved_in_unbraced_linkage_specification_p;
14869 int index = 0;
14871 /* Assume all will go well. */
14872 *is_error = false;
14873 /* The special considerations that apply to a function within an
14874 unbraced linkage specifications do not apply to the parameters
14875 to the function. */
14876 saved_in_unbraced_linkage_specification_p
14877 = parser->in_unbraced_linkage_specification_p;
14878 parser->in_unbraced_linkage_specification_p = false;
14880 /* Look for more parameters. */
14881 while (true)
14883 cp_parameter_declarator *parameter;
14884 tree decl = error_mark_node;
14885 bool parenthesized_p;
14886 /* Parse the parameter. */
14887 parameter
14888 = cp_parser_parameter_declaration (parser,
14889 /*template_parm_p=*/false,
14890 &parenthesized_p);
14892 /* We don't know yet if the enclosing context is deprecated, so wait
14893 and warn in grokparms if appropriate. */
14894 deprecated_state = DEPRECATED_SUPPRESS;
14896 if (parameter)
14897 decl = grokdeclarator (parameter->declarator,
14898 &parameter->decl_specifiers,
14899 PARM,
14900 parameter->default_argument != NULL_TREE,
14901 &parameter->decl_specifiers.attributes);
14903 deprecated_state = DEPRECATED_NORMAL;
14905 /* If a parse error occurred parsing the parameter declaration,
14906 then the entire parameter-declaration-list is erroneous. */
14907 if (decl == error_mark_node)
14909 *is_error = true;
14910 parameters = error_mark_node;
14911 break;
14914 if (parameter->decl_specifiers.attributes)
14915 cplus_decl_attributes (&decl,
14916 parameter->decl_specifiers.attributes,
14918 if (DECL_NAME (decl))
14919 decl = pushdecl (decl);
14921 if (decl != error_mark_node)
14923 retrofit_lang_decl (decl);
14924 DECL_PARM_INDEX (decl) = ++index;
14927 /* Add the new parameter to the list. */
14928 *tail = build_tree_list (parameter->default_argument, decl);
14929 tail = &TREE_CHAIN (*tail);
14931 /* Peek at the next token. */
14932 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
14933 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
14934 /* These are for Objective-C++ */
14935 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14936 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14937 /* The parameter-declaration-list is complete. */
14938 break;
14939 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14941 cp_token *token;
14943 /* Peek at the next token. */
14944 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14945 /* If it's an ellipsis, then the list is complete. */
14946 if (token->type == CPP_ELLIPSIS)
14947 break;
14948 /* Otherwise, there must be more parameters. Consume the
14949 `,'. */
14950 cp_lexer_consume_token (parser->lexer);
14951 /* When parsing something like:
14953 int i(float f, double d)
14955 we can tell after seeing the declaration for "f" that we
14956 are not looking at an initialization of a variable "i",
14957 but rather at the declaration of a function "i".
14959 Due to the fact that the parsing of template arguments
14960 (as specified to a template-id) requires backtracking we
14961 cannot use this technique when inside a template argument
14962 list. */
14963 if (!parser->in_template_argument_list_p
14964 && !parser->in_type_id_in_expr_p
14965 && cp_parser_uncommitted_to_tentative_parse_p (parser)
14966 /* However, a parameter-declaration of the form
14967 "foat(f)" (which is a valid declaration of a
14968 parameter "f") can also be interpreted as an
14969 expression (the conversion of "f" to "float"). */
14970 && !parenthesized_p)
14971 cp_parser_commit_to_tentative_parse (parser);
14973 else
14975 cp_parser_error (parser, "expected %<,%> or %<...%>");
14976 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14977 cp_parser_skip_to_closing_parenthesis (parser,
14978 /*recovering=*/true,
14979 /*or_comma=*/false,
14980 /*consume_paren=*/false);
14981 break;
14985 parser->in_unbraced_linkage_specification_p
14986 = saved_in_unbraced_linkage_specification_p;
14988 return parameters;
14991 /* Parse a parameter declaration.
14993 parameter-declaration:
14994 decl-specifier-seq ... [opt] declarator
14995 decl-specifier-seq declarator = assignment-expression
14996 decl-specifier-seq ... [opt] abstract-declarator [opt]
14997 decl-specifier-seq abstract-declarator [opt] = assignment-expression
14999 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
15000 declares a template parameter. (In that case, a non-nested `>'
15001 token encountered during the parsing of the assignment-expression
15002 is not interpreted as a greater-than operator.)
15004 Returns a representation of the parameter, or NULL if an error
15005 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
15006 true iff the declarator is of the form "(p)". */
15008 static cp_parameter_declarator *
15009 cp_parser_parameter_declaration (cp_parser *parser,
15010 bool template_parm_p,
15011 bool *parenthesized_p)
15013 int declares_class_or_enum;
15014 bool greater_than_is_operator_p;
15015 cp_decl_specifier_seq decl_specifiers;
15016 cp_declarator *declarator;
15017 tree default_argument;
15018 cp_token *token = NULL, *declarator_token_start = NULL;
15019 const char *saved_message;
15021 /* In a template parameter, `>' is not an operator.
15023 [temp.param]
15025 When parsing a default template-argument for a non-type
15026 template-parameter, the first non-nested `>' is taken as the end
15027 of the template parameter-list rather than a greater-than
15028 operator. */
15029 greater_than_is_operator_p = !template_parm_p;
15031 /* Type definitions may not appear in parameter types. */
15032 saved_message = parser->type_definition_forbidden_message;
15033 parser->type_definition_forbidden_message
15034 = "types may not be defined in parameter types";
15036 /* Parse the declaration-specifiers. */
15037 cp_parser_decl_specifier_seq (parser,
15038 CP_PARSER_FLAGS_NONE,
15039 &decl_specifiers,
15040 &declares_class_or_enum);
15042 /* Complain about missing 'typename' or other invalid type names. */
15043 if (!decl_specifiers.any_type_specifiers_p)
15044 cp_parser_parse_and_diagnose_invalid_type_name (parser);
15046 /* If an error occurred, there's no reason to attempt to parse the
15047 rest of the declaration. */
15048 if (cp_parser_error_occurred (parser))
15050 parser->type_definition_forbidden_message = saved_message;
15051 return NULL;
15054 /* Peek at the next token. */
15055 token = cp_lexer_peek_token (parser->lexer);
15057 /* If the next token is a `)', `,', `=', `>', or `...', then there
15058 is no declarator. However, when variadic templates are enabled,
15059 there may be a declarator following `...'. */
15060 if (token->type == CPP_CLOSE_PAREN
15061 || token->type == CPP_COMMA
15062 || token->type == CPP_EQ
15063 || token->type == CPP_GREATER)
15065 declarator = NULL;
15066 if (parenthesized_p)
15067 *parenthesized_p = false;
15069 /* Otherwise, there should be a declarator. */
15070 else
15072 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
15073 parser->default_arg_ok_p = false;
15075 /* After seeing a decl-specifier-seq, if the next token is not a
15076 "(", there is no possibility that the code is a valid
15077 expression. Therefore, if parsing tentatively, we commit at
15078 this point. */
15079 if (!parser->in_template_argument_list_p
15080 /* In an expression context, having seen:
15082 (int((char ...
15084 we cannot be sure whether we are looking at a
15085 function-type (taking a "char" as a parameter) or a cast
15086 of some object of type "char" to "int". */
15087 && !parser->in_type_id_in_expr_p
15088 && cp_parser_uncommitted_to_tentative_parse_p (parser)
15089 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
15090 cp_parser_commit_to_tentative_parse (parser);
15091 /* Parse the declarator. */
15092 declarator_token_start = token;
15093 declarator = cp_parser_declarator (parser,
15094 CP_PARSER_DECLARATOR_EITHER,
15095 /*ctor_dtor_or_conv_p=*/NULL,
15096 parenthesized_p,
15097 /*member_p=*/false);
15098 parser->default_arg_ok_p = saved_default_arg_ok_p;
15099 /* After the declarator, allow more attributes. */
15100 decl_specifiers.attributes
15101 = chainon (decl_specifiers.attributes,
15102 cp_parser_attributes_opt (parser));
15105 /* If the next token is an ellipsis, and we have not seen a
15106 declarator name, and the type of the declarator contains parameter
15107 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
15108 a parameter pack expansion expression. Otherwise, leave the
15109 ellipsis for a C-style variadic function. */
15110 token = cp_lexer_peek_token (parser->lexer);
15111 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15113 tree type = decl_specifiers.type;
15115 if (type && DECL_P (type))
15116 type = TREE_TYPE (type);
15118 if (type
15119 && TREE_CODE (type) != TYPE_PACK_EXPANSION
15120 && declarator_can_be_parameter_pack (declarator)
15121 && (!declarator || !declarator->parameter_pack_p)
15122 && uses_parameter_packs (type))
15124 /* Consume the `...'. */
15125 cp_lexer_consume_token (parser->lexer);
15126 maybe_warn_variadic_templates ();
15128 /* Build a pack expansion type */
15129 if (declarator)
15130 declarator->parameter_pack_p = true;
15131 else
15132 decl_specifiers.type = make_pack_expansion (type);
15136 /* The restriction on defining new types applies only to the type
15137 of the parameter, not to the default argument. */
15138 parser->type_definition_forbidden_message = saved_message;
15140 /* If the next token is `=', then process a default argument. */
15141 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15143 /* Consume the `='. */
15144 cp_lexer_consume_token (parser->lexer);
15146 /* If we are defining a class, then the tokens that make up the
15147 default argument must be saved and processed later. */
15148 if (!template_parm_p && at_class_scope_p ()
15149 && TYPE_BEING_DEFINED (current_class_type)
15150 && !LAMBDA_TYPE_P (current_class_type))
15152 unsigned depth = 0;
15153 int maybe_template_id = 0;
15154 cp_token *first_token;
15155 cp_token *token;
15157 /* Add tokens until we have processed the entire default
15158 argument. We add the range [first_token, token). */
15159 first_token = cp_lexer_peek_token (parser->lexer);
15160 while (true)
15162 bool done = false;
15164 /* Peek at the next token. */
15165 token = cp_lexer_peek_token (parser->lexer);
15166 /* What we do depends on what token we have. */
15167 switch (token->type)
15169 /* In valid code, a default argument must be
15170 immediately followed by a `,' `)', or `...'. */
15171 case CPP_COMMA:
15172 if (depth == 0 && maybe_template_id)
15174 /* If we've seen a '<', we might be in a
15175 template-argument-list. Until Core issue 325 is
15176 resolved, we don't know how this situation ought
15177 to be handled, so try to DTRT. We check whether
15178 what comes after the comma is a valid parameter
15179 declaration list. If it is, then the comma ends
15180 the default argument; otherwise the default
15181 argument continues. */
15182 bool error = false;
15184 /* Set ITALP so cp_parser_parameter_declaration_list
15185 doesn't decide to commit to this parse. */
15186 bool saved_italp = parser->in_template_argument_list_p;
15187 parser->in_template_argument_list_p = true;
15189 cp_parser_parse_tentatively (parser);
15190 cp_lexer_consume_token (parser->lexer);
15191 cp_parser_parameter_declaration_list (parser, &error);
15192 if (!cp_parser_error_occurred (parser) && !error)
15193 done = true;
15194 cp_parser_abort_tentative_parse (parser);
15196 parser->in_template_argument_list_p = saved_italp;
15197 break;
15199 case CPP_CLOSE_PAREN:
15200 case CPP_ELLIPSIS:
15201 /* If we run into a non-nested `;', `}', or `]',
15202 then the code is invalid -- but the default
15203 argument is certainly over. */
15204 case CPP_SEMICOLON:
15205 case CPP_CLOSE_BRACE:
15206 case CPP_CLOSE_SQUARE:
15207 if (depth == 0)
15208 done = true;
15209 /* Update DEPTH, if necessary. */
15210 else if (token->type == CPP_CLOSE_PAREN
15211 || token->type == CPP_CLOSE_BRACE
15212 || token->type == CPP_CLOSE_SQUARE)
15213 --depth;
15214 break;
15216 case CPP_OPEN_PAREN:
15217 case CPP_OPEN_SQUARE:
15218 case CPP_OPEN_BRACE:
15219 ++depth;
15220 break;
15222 case CPP_LESS:
15223 if (depth == 0)
15224 /* This might be the comparison operator, or it might
15225 start a template argument list. */
15226 ++maybe_template_id;
15227 break;
15229 case CPP_RSHIFT:
15230 if (cxx_dialect == cxx98)
15231 break;
15232 /* Fall through for C++0x, which treats the `>>'
15233 operator like two `>' tokens in certain
15234 cases. */
15236 case CPP_GREATER:
15237 if (depth == 0)
15239 /* This might be an operator, or it might close a
15240 template argument list. But if a previous '<'
15241 started a template argument list, this will have
15242 closed it, so we can't be in one anymore. */
15243 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
15244 if (maybe_template_id < 0)
15245 maybe_template_id = 0;
15247 break;
15249 /* If we run out of tokens, issue an error message. */
15250 case CPP_EOF:
15251 case CPP_PRAGMA_EOL:
15252 error_at (token->location, "file ends in default argument");
15253 done = true;
15254 break;
15256 case CPP_NAME:
15257 case CPP_SCOPE:
15258 /* In these cases, we should look for template-ids.
15259 For example, if the default argument is
15260 `X<int, double>()', we need to do name lookup to
15261 figure out whether or not `X' is a template; if
15262 so, the `,' does not end the default argument.
15264 That is not yet done. */
15265 break;
15267 default:
15268 break;
15271 /* If we've reached the end, stop. */
15272 if (done)
15273 break;
15275 /* Add the token to the token block. */
15276 token = cp_lexer_consume_token (parser->lexer);
15279 /* Create a DEFAULT_ARG to represent the unparsed default
15280 argument. */
15281 default_argument = make_node (DEFAULT_ARG);
15282 DEFARG_TOKENS (default_argument)
15283 = cp_token_cache_new (first_token, token);
15284 DEFARG_INSTANTIATIONS (default_argument) = NULL;
15286 /* Outside of a class definition, we can just parse the
15287 assignment-expression. */
15288 else
15290 token = cp_lexer_peek_token (parser->lexer);
15291 default_argument
15292 = cp_parser_default_argument (parser, template_parm_p);
15295 if (!parser->default_arg_ok_p)
15297 if (flag_permissive)
15298 warning (0, "deprecated use of default argument for parameter of non-function");
15299 else
15301 error_at (token->location,
15302 "default arguments are only "
15303 "permitted for function parameters");
15304 default_argument = NULL_TREE;
15307 else if ((declarator && declarator->parameter_pack_p)
15308 || (decl_specifiers.type
15309 && PACK_EXPANSION_P (decl_specifiers.type)))
15311 /* Find the name of the parameter pack. */
15312 cp_declarator *id_declarator = declarator;
15313 while (id_declarator && id_declarator->kind != cdk_id)
15314 id_declarator = id_declarator->declarator;
15316 if (id_declarator && id_declarator->kind == cdk_id)
15317 error_at (declarator_token_start->location,
15318 template_parm_p
15319 ? "template parameter pack %qD"
15320 " cannot have a default argument"
15321 : "parameter pack %qD cannot have a default argument",
15322 id_declarator->u.id.unqualified_name);
15323 else
15324 error_at (declarator_token_start->location,
15325 template_parm_p
15326 ? "template parameter pack cannot have a default argument"
15327 : "parameter pack cannot have a default argument");
15329 default_argument = NULL_TREE;
15332 else
15333 default_argument = NULL_TREE;
15335 return make_parameter_declarator (&decl_specifiers,
15336 declarator,
15337 default_argument);
15340 /* Parse a default argument and return it.
15342 TEMPLATE_PARM_P is true if this is a default argument for a
15343 non-type template parameter. */
15344 static tree
15345 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
15347 tree default_argument = NULL_TREE;
15348 bool saved_greater_than_is_operator_p;
15349 bool saved_local_variables_forbidden_p;
15351 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
15352 set correctly. */
15353 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
15354 parser->greater_than_is_operator_p = !template_parm_p;
15355 /* Local variable names (and the `this' keyword) may not
15356 appear in a default argument. */
15357 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15358 parser->local_variables_forbidden_p = true;
15359 /* Parse the assignment-expression. */
15360 if (template_parm_p)
15361 push_deferring_access_checks (dk_no_deferred);
15362 default_argument
15363 = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
15364 if (template_parm_p)
15365 pop_deferring_access_checks ();
15366 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
15367 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15369 return default_argument;
15372 /* Parse a function-body.
15374 function-body:
15375 compound_statement */
15377 static void
15378 cp_parser_function_body (cp_parser *parser)
15380 cp_parser_compound_statement (parser, NULL, false);
15383 /* Parse a ctor-initializer-opt followed by a function-body. Return
15384 true if a ctor-initializer was present. */
15386 static bool
15387 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
15389 tree body;
15390 bool ctor_initializer_p;
15392 /* Begin the function body. */
15393 body = begin_function_body ();
15394 /* Parse the optional ctor-initializer. */
15395 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
15396 /* Parse the function-body. */
15397 cp_parser_function_body (parser);
15398 /* Finish the function body. */
15399 finish_function_body (body);
15401 return ctor_initializer_p;
15404 /* Parse an initializer.
15406 initializer:
15407 = initializer-clause
15408 ( expression-list )
15410 Returns an expression representing the initializer. If no
15411 initializer is present, NULL_TREE is returned.
15413 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
15414 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
15415 set to TRUE if there is no initializer present. If there is an
15416 initializer, and it is not a constant-expression, *NON_CONSTANT_P
15417 is set to true; otherwise it is set to false. */
15419 static tree
15420 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
15421 bool* non_constant_p)
15423 cp_token *token;
15424 tree init;
15426 /* Peek at the next token. */
15427 token = cp_lexer_peek_token (parser->lexer);
15429 /* Let our caller know whether or not this initializer was
15430 parenthesized. */
15431 *is_direct_init = (token->type != CPP_EQ);
15432 /* Assume that the initializer is constant. */
15433 *non_constant_p = false;
15435 if (token->type == CPP_EQ)
15437 /* Consume the `='. */
15438 cp_lexer_consume_token (parser->lexer);
15439 /* Parse the initializer-clause. */
15440 init = cp_parser_initializer_clause (parser, non_constant_p);
15442 else if (token->type == CPP_OPEN_PAREN)
15444 VEC(tree,gc) *vec;
15445 vec = cp_parser_parenthesized_expression_list (parser, false,
15446 /*cast_p=*/false,
15447 /*allow_expansion_p=*/true,
15448 non_constant_p);
15449 if (vec == NULL)
15450 return error_mark_node;
15451 init = build_tree_list_vec (vec);
15452 release_tree_vector (vec);
15454 else if (token->type == CPP_OPEN_BRACE)
15456 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
15457 init = cp_parser_braced_list (parser, non_constant_p);
15458 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
15460 else
15462 /* Anything else is an error. */
15463 cp_parser_error (parser, "expected initializer");
15464 init = error_mark_node;
15467 return init;
15470 /* Parse an initializer-clause.
15472 initializer-clause:
15473 assignment-expression
15474 braced-init-list
15476 Returns an expression representing the initializer.
15478 If the `assignment-expression' production is used the value
15479 returned is simply a representation for the expression.
15481 Otherwise, calls cp_parser_braced_list. */
15483 static tree
15484 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
15486 tree initializer;
15488 /* Assume the expression is constant. */
15489 *non_constant_p = false;
15491 /* If it is not a `{', then we are looking at an
15492 assignment-expression. */
15493 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
15495 initializer
15496 = cp_parser_constant_expression (parser,
15497 /*allow_non_constant_p=*/true,
15498 non_constant_p);
15499 if (!*non_constant_p)
15500 initializer = fold_non_dependent_expr (initializer);
15502 else
15503 initializer = cp_parser_braced_list (parser, non_constant_p);
15505 return initializer;
15508 /* Parse a brace-enclosed initializer list.
15510 braced-init-list:
15511 { initializer-list , [opt] }
15514 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
15515 the elements of the initializer-list (or NULL, if the last
15516 production is used). The TREE_TYPE for the CONSTRUCTOR will be
15517 NULL_TREE. There is no way to detect whether or not the optional
15518 trailing `,' was provided. NON_CONSTANT_P is as for
15519 cp_parser_initializer. */
15521 static tree
15522 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
15524 tree initializer;
15526 /* Consume the `{' token. */
15527 cp_lexer_consume_token (parser->lexer);
15528 /* Create a CONSTRUCTOR to represent the braced-initializer. */
15529 initializer = make_node (CONSTRUCTOR);
15530 /* If it's not a `}', then there is a non-trivial initializer. */
15531 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
15533 /* Parse the initializer list. */
15534 CONSTRUCTOR_ELTS (initializer)
15535 = cp_parser_initializer_list (parser, non_constant_p);
15536 /* A trailing `,' token is allowed. */
15537 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15538 cp_lexer_consume_token (parser->lexer);
15540 /* Now, there should be a trailing `}'. */
15541 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15542 TREE_TYPE (initializer) = init_list_type_node;
15543 return initializer;
15546 /* Parse an initializer-list.
15548 initializer-list:
15549 initializer-clause ... [opt]
15550 initializer-list , initializer-clause ... [opt]
15552 GNU Extension:
15554 initializer-list:
15555 identifier : initializer-clause
15556 initializer-list, identifier : initializer-clause
15558 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
15559 for the initializer. If the INDEX of the elt is non-NULL, it is the
15560 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
15561 as for cp_parser_initializer. */
15563 static VEC(constructor_elt,gc) *
15564 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
15566 VEC(constructor_elt,gc) *v = NULL;
15568 /* Assume all of the expressions are constant. */
15569 *non_constant_p = false;
15571 /* Parse the rest of the list. */
15572 while (true)
15574 cp_token *token;
15575 tree identifier;
15576 tree initializer;
15577 bool clause_non_constant_p;
15579 /* If the next token is an identifier and the following one is a
15580 colon, we are looking at the GNU designated-initializer
15581 syntax. */
15582 if (cp_parser_allow_gnu_extensions_p (parser)
15583 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
15584 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
15586 /* Warn the user that they are using an extension. */
15587 pedwarn (input_location, OPT_pedantic,
15588 "ISO C++ does not allow designated initializers");
15589 /* Consume the identifier. */
15590 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
15591 /* Consume the `:'. */
15592 cp_lexer_consume_token (parser->lexer);
15594 else
15595 identifier = NULL_TREE;
15597 /* Parse the initializer. */
15598 initializer = cp_parser_initializer_clause (parser,
15599 &clause_non_constant_p);
15600 /* If any clause is non-constant, so is the entire initializer. */
15601 if (clause_non_constant_p)
15602 *non_constant_p = true;
15604 /* If we have an ellipsis, this is an initializer pack
15605 expansion. */
15606 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15608 /* Consume the `...'. */
15609 cp_lexer_consume_token (parser->lexer);
15611 /* Turn the initializer into an initializer expansion. */
15612 initializer = make_pack_expansion (initializer);
15615 /* Add it to the vector. */
15616 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
15618 /* If the next token is not a comma, we have reached the end of
15619 the list. */
15620 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15621 break;
15623 /* Peek at the next token. */
15624 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15625 /* If the next token is a `}', then we're still done. An
15626 initializer-clause can have a trailing `,' after the
15627 initializer-list and before the closing `}'. */
15628 if (token->type == CPP_CLOSE_BRACE)
15629 break;
15631 /* Consume the `,' token. */
15632 cp_lexer_consume_token (parser->lexer);
15635 return v;
15638 /* Classes [gram.class] */
15640 /* Parse a class-name.
15642 class-name:
15643 identifier
15644 template-id
15646 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
15647 to indicate that names looked up in dependent types should be
15648 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
15649 keyword has been used to indicate that the name that appears next
15650 is a template. TAG_TYPE indicates the explicit tag given before
15651 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
15652 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
15653 is the class being defined in a class-head.
15655 Returns the TYPE_DECL representing the class. */
15657 static tree
15658 cp_parser_class_name (cp_parser *parser,
15659 bool typename_keyword_p,
15660 bool template_keyword_p,
15661 enum tag_types tag_type,
15662 bool check_dependency_p,
15663 bool class_head_p,
15664 bool is_declaration)
15666 tree decl;
15667 tree scope;
15668 bool typename_p;
15669 cp_token *token;
15670 tree identifier = NULL_TREE;
15672 /* All class-names start with an identifier. */
15673 token = cp_lexer_peek_token (parser->lexer);
15674 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
15676 cp_parser_error (parser, "expected class-name");
15677 return error_mark_node;
15680 /* PARSER->SCOPE can be cleared when parsing the template-arguments
15681 to a template-id, so we save it here. */
15682 scope = parser->scope;
15683 if (scope == error_mark_node)
15684 return error_mark_node;
15686 /* Any name names a type if we're following the `typename' keyword
15687 in a qualified name where the enclosing scope is type-dependent. */
15688 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
15689 && dependent_type_p (scope));
15690 /* Handle the common case (an identifier, but not a template-id)
15691 efficiently. */
15692 if (token->type == CPP_NAME
15693 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
15695 cp_token *identifier_token;
15696 bool ambiguous_p;
15698 /* Look for the identifier. */
15699 identifier_token = cp_lexer_peek_token (parser->lexer);
15700 ambiguous_p = identifier_token->ambiguous_p;
15701 identifier = cp_parser_identifier (parser);
15702 /* If the next token isn't an identifier, we are certainly not
15703 looking at a class-name. */
15704 if (identifier == error_mark_node)
15705 decl = error_mark_node;
15706 /* If we know this is a type-name, there's no need to look it
15707 up. */
15708 else if (typename_p)
15709 decl = identifier;
15710 else
15712 tree ambiguous_decls;
15713 /* If we already know that this lookup is ambiguous, then
15714 we've already issued an error message; there's no reason
15715 to check again. */
15716 if (ambiguous_p)
15718 cp_parser_simulate_error (parser);
15719 return error_mark_node;
15721 /* If the next token is a `::', then the name must be a type
15722 name.
15724 [basic.lookup.qual]
15726 During the lookup for a name preceding the :: scope
15727 resolution operator, object, function, and enumerator
15728 names are ignored. */
15729 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15730 tag_type = typename_type;
15731 /* Look up the name. */
15732 decl = cp_parser_lookup_name (parser, identifier,
15733 tag_type,
15734 /*is_template=*/false,
15735 /*is_namespace=*/false,
15736 check_dependency_p,
15737 &ambiguous_decls,
15738 identifier_token->location);
15739 if (ambiguous_decls)
15741 if (cp_parser_parsing_tentatively (parser))
15742 cp_parser_simulate_error (parser);
15743 return error_mark_node;
15747 else
15749 /* Try a template-id. */
15750 decl = cp_parser_template_id (parser, template_keyword_p,
15751 check_dependency_p,
15752 is_declaration);
15753 if (decl == error_mark_node)
15754 return error_mark_node;
15757 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
15759 /* If this is a typename, create a TYPENAME_TYPE. */
15760 if (typename_p && decl != error_mark_node)
15762 decl = make_typename_type (scope, decl, typename_type,
15763 /*complain=*/tf_error);
15764 if (decl != error_mark_node)
15765 decl = TYPE_NAME (decl);
15768 /* Check to see that it is really the name of a class. */
15769 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15770 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
15771 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15772 /* Situations like this:
15774 template <typename T> struct A {
15775 typename T::template X<int>::I i;
15778 are problematic. Is `T::template X<int>' a class-name? The
15779 standard does not seem to be definitive, but there is no other
15780 valid interpretation of the following `::'. Therefore, those
15781 names are considered class-names. */
15783 decl = make_typename_type (scope, decl, tag_type, tf_error);
15784 if (decl != error_mark_node)
15785 decl = TYPE_NAME (decl);
15787 else if (TREE_CODE (decl) != TYPE_DECL
15788 || TREE_TYPE (decl) == error_mark_node
15789 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
15790 decl = error_mark_node;
15792 if (decl == error_mark_node)
15793 cp_parser_error (parser, "expected class-name");
15794 else if (identifier && !parser->scope)
15795 maybe_note_name_used_in_class (identifier, decl);
15797 return decl;
15800 /* Parse a class-specifier.
15802 class-specifier:
15803 class-head { member-specification [opt] }
15805 Returns the TREE_TYPE representing the class. */
15807 static tree
15808 cp_parser_class_specifier (cp_parser* parser)
15810 tree type;
15811 tree attributes = NULL_TREE;
15812 bool nested_name_specifier_p;
15813 unsigned saved_num_template_parameter_lists;
15814 bool saved_in_function_body;
15815 bool saved_in_unbraced_linkage_specification_p;
15816 tree old_scope = NULL_TREE;
15817 tree scope = NULL_TREE;
15818 tree bases;
15820 push_deferring_access_checks (dk_no_deferred);
15822 /* Parse the class-head. */
15823 type = cp_parser_class_head (parser,
15824 &nested_name_specifier_p,
15825 &attributes,
15826 &bases);
15827 /* If the class-head was a semantic disaster, skip the entire body
15828 of the class. */
15829 if (!type)
15831 cp_parser_skip_to_end_of_block_or_statement (parser);
15832 pop_deferring_access_checks ();
15833 return error_mark_node;
15836 /* Look for the `{'. */
15837 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
15839 pop_deferring_access_checks ();
15840 return error_mark_node;
15843 /* Process the base classes. If they're invalid, skip the
15844 entire class body. */
15845 if (!xref_basetypes (type, bases))
15847 /* Consuming the closing brace yields better error messages
15848 later on. */
15849 if (cp_parser_skip_to_closing_brace (parser))
15850 cp_lexer_consume_token (parser->lexer);
15851 pop_deferring_access_checks ();
15852 return error_mark_node;
15855 /* Issue an error message if type-definitions are forbidden here. */
15856 cp_parser_check_type_definition (parser);
15857 /* Remember that we are defining one more class. */
15858 ++parser->num_classes_being_defined;
15859 /* Inside the class, surrounding template-parameter-lists do not
15860 apply. */
15861 saved_num_template_parameter_lists
15862 = parser->num_template_parameter_lists;
15863 parser->num_template_parameter_lists = 0;
15864 /* We are not in a function body. */
15865 saved_in_function_body = parser->in_function_body;
15866 parser->in_function_body = false;
15867 /* We are not immediately inside an extern "lang" block. */
15868 saved_in_unbraced_linkage_specification_p
15869 = parser->in_unbraced_linkage_specification_p;
15870 parser->in_unbraced_linkage_specification_p = false;
15872 /* Start the class. */
15873 if (nested_name_specifier_p)
15875 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
15876 old_scope = push_inner_scope (scope);
15878 type = begin_class_definition (type, attributes);
15880 if (type == error_mark_node)
15881 /* If the type is erroneous, skip the entire body of the class. */
15882 cp_parser_skip_to_closing_brace (parser);
15883 else
15884 /* Parse the member-specification. */
15885 cp_parser_member_specification_opt (parser);
15887 /* Look for the trailing `}'. */
15888 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15889 /* Look for trailing attributes to apply to this class. */
15890 if (cp_parser_allow_gnu_extensions_p (parser))
15891 attributes = cp_parser_attributes_opt (parser);
15892 if (type != error_mark_node)
15893 type = finish_struct (type, attributes);
15894 if (nested_name_specifier_p)
15895 pop_inner_scope (old_scope, scope);
15896 /* If this class is not itself within the scope of another class,
15897 then we need to parse the bodies of all of the queued function
15898 definitions. Note that the queued functions defined in a class
15899 are not always processed immediately following the
15900 class-specifier for that class. Consider:
15902 struct A {
15903 struct B { void f() { sizeof (A); } };
15906 If `f' were processed before the processing of `A' were
15907 completed, there would be no way to compute the size of `A'.
15908 Note that the nesting we are interested in here is lexical --
15909 not the semantic nesting given by TYPE_CONTEXT. In particular,
15910 for:
15912 struct A { struct B; };
15913 struct A::B { void f() { } };
15915 there is no need to delay the parsing of `A::B::f'. */
15916 if (--parser->num_classes_being_defined == 0)
15918 tree queue_entry;
15919 tree fn;
15920 tree class_type = NULL_TREE;
15921 tree pushed_scope = NULL_TREE;
15923 /* In a first pass, parse default arguments to the functions.
15924 Then, in a second pass, parse the bodies of the functions.
15925 This two-phased approach handles cases like:
15927 struct S {
15928 void f() { g(); }
15929 void g(int i = 3);
15933 for (TREE_PURPOSE (parser->unparsed_functions_queues)
15934 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
15935 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
15936 TREE_PURPOSE (parser->unparsed_functions_queues)
15937 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
15939 fn = TREE_VALUE (queue_entry);
15940 /* If there are default arguments that have not yet been processed,
15941 take care of them now. */
15942 if (class_type != TREE_PURPOSE (queue_entry))
15944 if (pushed_scope)
15945 pop_scope (pushed_scope);
15946 class_type = TREE_PURPOSE (queue_entry);
15947 pushed_scope = push_scope (class_type);
15949 /* Make sure that any template parameters are in scope. */
15950 maybe_begin_member_template_processing (fn);
15951 /* Parse the default argument expressions. */
15952 cp_parser_late_parsing_default_args (parser, fn);
15953 /* Remove any template parameters from the symbol table. */
15954 maybe_end_member_template_processing ();
15956 if (pushed_scope)
15957 pop_scope (pushed_scope);
15958 /* Now parse the body of the functions. */
15959 for (TREE_VALUE (parser->unparsed_functions_queues)
15960 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
15961 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
15962 TREE_VALUE (parser->unparsed_functions_queues)
15963 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
15965 /* Figure out which function we need to process. */
15966 fn = TREE_VALUE (queue_entry);
15967 /* Parse the function. */
15968 cp_parser_late_parsing_for_member (parser, fn);
15972 /* Put back any saved access checks. */
15973 pop_deferring_access_checks ();
15975 /* Restore saved state. */
15976 parser->in_function_body = saved_in_function_body;
15977 parser->num_template_parameter_lists
15978 = saved_num_template_parameter_lists;
15979 parser->in_unbraced_linkage_specification_p
15980 = saved_in_unbraced_linkage_specification_p;
15982 return type;
15985 /* Parse a class-head.
15987 class-head:
15988 class-key identifier [opt] base-clause [opt]
15989 class-key nested-name-specifier identifier base-clause [opt]
15990 class-key nested-name-specifier [opt] template-id
15991 base-clause [opt]
15993 GNU Extensions:
15994 class-key attributes identifier [opt] base-clause [opt]
15995 class-key attributes nested-name-specifier identifier base-clause [opt]
15996 class-key attributes nested-name-specifier [opt] template-id
15997 base-clause [opt]
15999 Upon return BASES is initialized to the list of base classes (or
16000 NULL, if there are none) in the same form returned by
16001 cp_parser_base_clause.
16003 Returns the TYPE of the indicated class. Sets
16004 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
16005 involving a nested-name-specifier was used, and FALSE otherwise.
16007 Returns error_mark_node if this is not a class-head.
16009 Returns NULL_TREE if the class-head is syntactically valid, but
16010 semantically invalid in a way that means we should skip the entire
16011 body of the class. */
16013 static tree
16014 cp_parser_class_head (cp_parser* parser,
16015 bool* nested_name_specifier_p,
16016 tree *attributes_p,
16017 tree *bases)
16019 tree nested_name_specifier;
16020 enum tag_types class_key;
16021 tree id = NULL_TREE;
16022 tree type = NULL_TREE;
16023 tree attributes;
16024 bool template_id_p = false;
16025 bool qualified_p = false;
16026 bool invalid_nested_name_p = false;
16027 bool invalid_explicit_specialization_p = false;
16028 tree pushed_scope = NULL_TREE;
16029 unsigned num_templates;
16030 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
16031 /* Assume no nested-name-specifier will be present. */
16032 *nested_name_specifier_p = false;
16033 /* Assume no template parameter lists will be used in defining the
16034 type. */
16035 num_templates = 0;
16037 *bases = NULL_TREE;
16039 /* Look for the class-key. */
16040 class_key = cp_parser_class_key (parser);
16041 if (class_key == none_type)
16042 return error_mark_node;
16044 /* Parse the attributes. */
16045 attributes = cp_parser_attributes_opt (parser);
16047 /* If the next token is `::', that is invalid -- but sometimes
16048 people do try to write:
16050 struct ::S {};
16052 Handle this gracefully by accepting the extra qualifier, and then
16053 issuing an error about it later if this really is a
16054 class-head. If it turns out just to be an elaborated type
16055 specifier, remain silent. */
16056 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
16057 qualified_p = true;
16059 push_deferring_access_checks (dk_no_check);
16061 /* Determine the name of the class. Begin by looking for an
16062 optional nested-name-specifier. */
16063 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
16064 nested_name_specifier
16065 = cp_parser_nested_name_specifier_opt (parser,
16066 /*typename_keyword_p=*/false,
16067 /*check_dependency_p=*/false,
16068 /*type_p=*/false,
16069 /*is_declaration=*/false);
16070 /* If there was a nested-name-specifier, then there *must* be an
16071 identifier. */
16072 if (nested_name_specifier)
16074 type_start_token = cp_lexer_peek_token (parser->lexer);
16075 /* Although the grammar says `identifier', it really means
16076 `class-name' or `template-name'. You are only allowed to
16077 define a class that has already been declared with this
16078 syntax.
16080 The proposed resolution for Core Issue 180 says that wherever
16081 you see `class T::X' you should treat `X' as a type-name.
16083 It is OK to define an inaccessible class; for example:
16085 class A { class B; };
16086 class A::B {};
16088 We do not know if we will see a class-name, or a
16089 template-name. We look for a class-name first, in case the
16090 class-name is a template-id; if we looked for the
16091 template-name first we would stop after the template-name. */
16092 cp_parser_parse_tentatively (parser);
16093 type = cp_parser_class_name (parser,
16094 /*typename_keyword_p=*/false,
16095 /*template_keyword_p=*/false,
16096 class_type,
16097 /*check_dependency_p=*/false,
16098 /*class_head_p=*/true,
16099 /*is_declaration=*/false);
16100 /* If that didn't work, ignore the nested-name-specifier. */
16101 if (!cp_parser_parse_definitely (parser))
16103 invalid_nested_name_p = true;
16104 type_start_token = cp_lexer_peek_token (parser->lexer);
16105 id = cp_parser_identifier (parser);
16106 if (id == error_mark_node)
16107 id = NULL_TREE;
16109 /* If we could not find a corresponding TYPE, treat this
16110 declaration like an unqualified declaration. */
16111 if (type == error_mark_node)
16112 nested_name_specifier = NULL_TREE;
16113 /* Otherwise, count the number of templates used in TYPE and its
16114 containing scopes. */
16115 else
16117 tree scope;
16119 for (scope = TREE_TYPE (type);
16120 scope && TREE_CODE (scope) != NAMESPACE_DECL;
16121 scope = (TYPE_P (scope)
16122 ? TYPE_CONTEXT (scope)
16123 : DECL_CONTEXT (scope)))
16124 if (TYPE_P (scope)
16125 && CLASS_TYPE_P (scope)
16126 && CLASSTYPE_TEMPLATE_INFO (scope)
16127 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
16128 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
16129 ++num_templates;
16132 /* Otherwise, the identifier is optional. */
16133 else
16135 /* We don't know whether what comes next is a template-id,
16136 an identifier, or nothing at all. */
16137 cp_parser_parse_tentatively (parser);
16138 /* Check for a template-id. */
16139 type_start_token = cp_lexer_peek_token (parser->lexer);
16140 id = cp_parser_template_id (parser,
16141 /*template_keyword_p=*/false,
16142 /*check_dependency_p=*/true,
16143 /*is_declaration=*/true);
16144 /* If that didn't work, it could still be an identifier. */
16145 if (!cp_parser_parse_definitely (parser))
16147 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16149 type_start_token = cp_lexer_peek_token (parser->lexer);
16150 id = cp_parser_identifier (parser);
16152 else
16153 id = NULL_TREE;
16155 else
16157 template_id_p = true;
16158 ++num_templates;
16162 pop_deferring_access_checks ();
16164 if (id)
16165 cp_parser_check_for_invalid_template_id (parser, id,
16166 type_start_token->location);
16168 /* If it's not a `:' or a `{' then we can't really be looking at a
16169 class-head, since a class-head only appears as part of a
16170 class-specifier. We have to detect this situation before calling
16171 xref_tag, since that has irreversible side-effects. */
16172 if (!cp_parser_next_token_starts_class_definition_p (parser))
16174 cp_parser_error (parser, "expected %<{%> or %<:%>");
16175 return error_mark_node;
16178 /* At this point, we're going ahead with the class-specifier, even
16179 if some other problem occurs. */
16180 cp_parser_commit_to_tentative_parse (parser);
16181 /* Issue the error about the overly-qualified name now. */
16182 if (qualified_p)
16184 cp_parser_error (parser,
16185 "global qualification of class name is invalid");
16186 return error_mark_node;
16188 else if (invalid_nested_name_p)
16190 cp_parser_error (parser,
16191 "qualified name does not name a class");
16192 return error_mark_node;
16194 else if (nested_name_specifier)
16196 tree scope;
16198 /* Reject typedef-names in class heads. */
16199 if (!DECL_IMPLICIT_TYPEDEF_P (type))
16201 error_at (type_start_token->location,
16202 "invalid class name in declaration of %qD",
16203 type);
16204 type = NULL_TREE;
16205 goto done;
16208 /* Figure out in what scope the declaration is being placed. */
16209 scope = current_scope ();
16210 /* If that scope does not contain the scope in which the
16211 class was originally declared, the program is invalid. */
16212 if (scope && !is_ancestor (scope, nested_name_specifier))
16214 if (at_namespace_scope_p ())
16215 error_at (type_start_token->location,
16216 "declaration of %qD in namespace %qD which does not "
16217 "enclose %qD",
16218 type, scope, nested_name_specifier);
16219 else
16220 error_at (type_start_token->location,
16221 "declaration of %qD in %qD which does not enclose %qD",
16222 type, scope, nested_name_specifier);
16223 type = NULL_TREE;
16224 goto done;
16226 /* [dcl.meaning]
16228 A declarator-id shall not be qualified except for the
16229 definition of a ... nested class outside of its class
16230 ... [or] the definition or explicit instantiation of a
16231 class member of a namespace outside of its namespace. */
16232 if (scope == nested_name_specifier)
16234 permerror (nested_name_specifier_token_start->location,
16235 "extra qualification not allowed");
16236 nested_name_specifier = NULL_TREE;
16237 num_templates = 0;
16240 /* An explicit-specialization must be preceded by "template <>". If
16241 it is not, try to recover gracefully. */
16242 if (at_namespace_scope_p ()
16243 && parser->num_template_parameter_lists == 0
16244 && template_id_p)
16246 error_at (type_start_token->location,
16247 "an explicit specialization must be preceded by %<template <>%>");
16248 invalid_explicit_specialization_p = true;
16249 /* Take the same action that would have been taken by
16250 cp_parser_explicit_specialization. */
16251 ++parser->num_template_parameter_lists;
16252 begin_specialization ();
16254 /* There must be no "return" statements between this point and the
16255 end of this function; set "type "to the correct return value and
16256 use "goto done;" to return. */
16257 /* Make sure that the right number of template parameters were
16258 present. */
16259 if (!cp_parser_check_template_parameters (parser, num_templates,
16260 type_start_token->location,
16261 /*declarator=*/NULL))
16263 /* If something went wrong, there is no point in even trying to
16264 process the class-definition. */
16265 type = NULL_TREE;
16266 goto done;
16269 /* Look up the type. */
16270 if (template_id_p)
16272 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
16273 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
16274 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
16276 error_at (type_start_token->location,
16277 "function template %qD redeclared as a class template", id);
16278 type = error_mark_node;
16280 else
16282 type = TREE_TYPE (id);
16283 type = maybe_process_partial_specialization (type);
16285 if (nested_name_specifier)
16286 pushed_scope = push_scope (nested_name_specifier);
16288 else if (nested_name_specifier)
16290 tree class_type;
16292 /* Given:
16294 template <typename T> struct S { struct T };
16295 template <typename T> struct S<T>::T { };
16297 we will get a TYPENAME_TYPE when processing the definition of
16298 `S::T'. We need to resolve it to the actual type before we
16299 try to define it. */
16300 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
16302 class_type = resolve_typename_type (TREE_TYPE (type),
16303 /*only_current_p=*/false);
16304 if (TREE_CODE (class_type) != TYPENAME_TYPE)
16305 type = TYPE_NAME (class_type);
16306 else
16308 cp_parser_error (parser, "could not resolve typename type");
16309 type = error_mark_node;
16313 if (maybe_process_partial_specialization (TREE_TYPE (type))
16314 == error_mark_node)
16316 type = NULL_TREE;
16317 goto done;
16320 class_type = current_class_type;
16321 /* Enter the scope indicated by the nested-name-specifier. */
16322 pushed_scope = push_scope (nested_name_specifier);
16323 /* Get the canonical version of this type. */
16324 type = TYPE_MAIN_DECL (TREE_TYPE (type));
16325 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
16326 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
16328 type = push_template_decl (type);
16329 if (type == error_mark_node)
16331 type = NULL_TREE;
16332 goto done;
16336 type = TREE_TYPE (type);
16337 *nested_name_specifier_p = true;
16339 else /* The name is not a nested name. */
16341 /* If the class was unnamed, create a dummy name. */
16342 if (!id)
16343 id = make_anon_name ();
16344 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
16345 parser->num_template_parameter_lists);
16348 /* Indicate whether this class was declared as a `class' or as a
16349 `struct'. */
16350 if (TREE_CODE (type) == RECORD_TYPE)
16351 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
16352 cp_parser_check_class_key (class_key, type);
16354 /* If this type was already complete, and we see another definition,
16355 that's an error. */
16356 if (type != error_mark_node && COMPLETE_TYPE_P (type))
16358 error_at (type_start_token->location, "redefinition of %q#T",
16359 type);
16360 error_at (type_start_token->location, "previous definition of %q+#T",
16361 type);
16362 type = NULL_TREE;
16363 goto done;
16365 else if (type == error_mark_node)
16366 type = NULL_TREE;
16368 /* We will have entered the scope containing the class; the names of
16369 base classes should be looked up in that context. For example:
16371 struct A { struct B {}; struct C; };
16372 struct A::C : B {};
16374 is valid. */
16376 /* Get the list of base-classes, if there is one. */
16377 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16378 *bases = cp_parser_base_clause (parser);
16380 done:
16381 /* Leave the scope given by the nested-name-specifier. We will
16382 enter the class scope itself while processing the members. */
16383 if (pushed_scope)
16384 pop_scope (pushed_scope);
16386 if (invalid_explicit_specialization_p)
16388 end_specialization ();
16389 --parser->num_template_parameter_lists;
16391 *attributes_p = attributes;
16392 return type;
16395 /* Parse a class-key.
16397 class-key:
16398 class
16399 struct
16400 union
16402 Returns the kind of class-key specified, or none_type to indicate
16403 error. */
16405 static enum tag_types
16406 cp_parser_class_key (cp_parser* parser)
16408 cp_token *token;
16409 enum tag_types tag_type;
16411 /* Look for the class-key. */
16412 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
16413 if (!token)
16414 return none_type;
16416 /* Check to see if the TOKEN is a class-key. */
16417 tag_type = cp_parser_token_is_class_key (token);
16418 if (!tag_type)
16419 cp_parser_error (parser, "expected class-key");
16420 return tag_type;
16423 /* Parse an (optional) member-specification.
16425 member-specification:
16426 member-declaration member-specification [opt]
16427 access-specifier : member-specification [opt] */
16429 static void
16430 cp_parser_member_specification_opt (cp_parser* parser)
16432 while (true)
16434 cp_token *token;
16435 enum rid keyword;
16437 /* Peek at the next token. */
16438 token = cp_lexer_peek_token (parser->lexer);
16439 /* If it's a `}', or EOF then we've seen all the members. */
16440 if (token->type == CPP_CLOSE_BRACE
16441 || token->type == CPP_EOF
16442 || token->type == CPP_PRAGMA_EOL)
16443 break;
16445 /* See if this token is a keyword. */
16446 keyword = token->keyword;
16447 switch (keyword)
16449 case RID_PUBLIC:
16450 case RID_PROTECTED:
16451 case RID_PRIVATE:
16452 /* Consume the access-specifier. */
16453 cp_lexer_consume_token (parser->lexer);
16454 /* Remember which access-specifier is active. */
16455 current_access_specifier = token->u.value;
16456 /* Look for the `:'. */
16457 cp_parser_require (parser, CPP_COLON, "%<:%>");
16458 break;
16460 default:
16461 /* Accept #pragmas at class scope. */
16462 if (token->type == CPP_PRAGMA)
16464 cp_parser_pragma (parser, pragma_external);
16465 break;
16468 /* Otherwise, the next construction must be a
16469 member-declaration. */
16470 cp_parser_member_declaration (parser);
16475 /* Parse a member-declaration.
16477 member-declaration:
16478 decl-specifier-seq [opt] member-declarator-list [opt] ;
16479 function-definition ; [opt]
16480 :: [opt] nested-name-specifier template [opt] unqualified-id ;
16481 using-declaration
16482 template-declaration
16484 member-declarator-list:
16485 member-declarator
16486 member-declarator-list , member-declarator
16488 member-declarator:
16489 declarator pure-specifier [opt]
16490 declarator constant-initializer [opt]
16491 identifier [opt] : constant-expression
16493 GNU Extensions:
16495 member-declaration:
16496 __extension__ member-declaration
16498 member-declarator:
16499 declarator attributes [opt] pure-specifier [opt]
16500 declarator attributes [opt] constant-initializer [opt]
16501 identifier [opt] attributes [opt] : constant-expression
16503 C++0x Extensions:
16505 member-declaration:
16506 static_assert-declaration */
16508 static void
16509 cp_parser_member_declaration (cp_parser* parser)
16511 cp_decl_specifier_seq decl_specifiers;
16512 tree prefix_attributes;
16513 tree decl;
16514 int declares_class_or_enum;
16515 bool friend_p;
16516 cp_token *token = NULL;
16517 cp_token *decl_spec_token_start = NULL;
16518 cp_token *initializer_token_start = NULL;
16519 int saved_pedantic;
16521 /* Check for the `__extension__' keyword. */
16522 if (cp_parser_extension_opt (parser, &saved_pedantic))
16524 /* Recurse. */
16525 cp_parser_member_declaration (parser);
16526 /* Restore the old value of the PEDANTIC flag. */
16527 pedantic = saved_pedantic;
16529 return;
16532 /* Check for a template-declaration. */
16533 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16535 /* An explicit specialization here is an error condition, and we
16536 expect the specialization handler to detect and report this. */
16537 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16538 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
16539 cp_parser_explicit_specialization (parser);
16540 else
16541 cp_parser_template_declaration (parser, /*member_p=*/true);
16543 return;
16546 /* Check for a using-declaration. */
16547 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
16549 /* Parse the using-declaration. */
16550 cp_parser_using_declaration (parser,
16551 /*access_declaration_p=*/false);
16552 return;
16555 /* Check for @defs. */
16556 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
16558 tree ivar, member;
16559 tree ivar_chains = cp_parser_objc_defs_expression (parser);
16560 ivar = ivar_chains;
16561 while (ivar)
16563 member = ivar;
16564 ivar = TREE_CHAIN (member);
16565 TREE_CHAIN (member) = NULL_TREE;
16566 finish_member_declaration (member);
16568 return;
16571 /* If the next token is `static_assert' we have a static assertion. */
16572 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
16574 cp_parser_static_assert (parser, /*member_p=*/true);
16575 return;
16578 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
16579 return;
16581 /* Parse the decl-specifier-seq. */
16582 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
16583 cp_parser_decl_specifier_seq (parser,
16584 CP_PARSER_FLAGS_OPTIONAL,
16585 &decl_specifiers,
16586 &declares_class_or_enum);
16587 prefix_attributes = decl_specifiers.attributes;
16588 decl_specifiers.attributes = NULL_TREE;
16589 /* Check for an invalid type-name. */
16590 if (!decl_specifiers.any_type_specifiers_p
16591 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
16592 return;
16593 /* If there is no declarator, then the decl-specifier-seq should
16594 specify a type. */
16595 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16597 /* If there was no decl-specifier-seq, and the next token is a
16598 `;', then we have something like:
16600 struct S { ; };
16602 [class.mem]
16604 Each member-declaration shall declare at least one member
16605 name of the class. */
16606 if (!decl_specifiers.any_specifiers_p)
16608 cp_token *token = cp_lexer_peek_token (parser->lexer);
16609 if (!in_system_header_at (token->location))
16610 pedwarn (token->location, OPT_pedantic, "extra %<;%>");
16612 else
16614 tree type;
16616 /* See if this declaration is a friend. */
16617 friend_p = cp_parser_friend_p (&decl_specifiers);
16618 /* If there were decl-specifiers, check to see if there was
16619 a class-declaration. */
16620 type = check_tag_decl (&decl_specifiers);
16621 /* Nested classes have already been added to the class, but
16622 a `friend' needs to be explicitly registered. */
16623 if (friend_p)
16625 /* If the `friend' keyword was present, the friend must
16626 be introduced with a class-key. */
16627 if (!declares_class_or_enum)
16628 error_at (decl_spec_token_start->location,
16629 "a class-key must be used when declaring a friend");
16630 /* In this case:
16632 template <typename T> struct A {
16633 friend struct A<T>::B;
16636 A<T>::B will be represented by a TYPENAME_TYPE, and
16637 therefore not recognized by check_tag_decl. */
16638 if (!type
16639 && decl_specifiers.type
16640 && TYPE_P (decl_specifiers.type))
16641 type = decl_specifiers.type;
16642 if (!type || !TYPE_P (type))
16643 error_at (decl_spec_token_start->location,
16644 "friend declaration does not name a class or "
16645 "function");
16646 else
16647 make_friend_class (current_class_type, type,
16648 /*complain=*/true);
16650 /* If there is no TYPE, an error message will already have
16651 been issued. */
16652 else if (!type || type == error_mark_node)
16654 /* An anonymous aggregate has to be handled specially; such
16655 a declaration really declares a data member (with a
16656 particular type), as opposed to a nested class. */
16657 else if (ANON_AGGR_TYPE_P (type))
16659 /* Remove constructors and such from TYPE, now that we
16660 know it is an anonymous aggregate. */
16661 fixup_anonymous_aggr (type);
16662 /* And make the corresponding data member. */
16663 decl = build_decl (decl_spec_token_start->location,
16664 FIELD_DECL, NULL_TREE, type);
16665 /* Add it to the class. */
16666 finish_member_declaration (decl);
16668 else
16669 cp_parser_check_access_in_redeclaration
16670 (TYPE_NAME (type),
16671 decl_spec_token_start->location);
16674 else
16676 /* See if these declarations will be friends. */
16677 friend_p = cp_parser_friend_p (&decl_specifiers);
16679 /* Keep going until we hit the `;' at the end of the
16680 declaration. */
16681 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16683 tree attributes = NULL_TREE;
16684 tree first_attribute;
16686 /* Peek at the next token. */
16687 token = cp_lexer_peek_token (parser->lexer);
16689 /* Check for a bitfield declaration. */
16690 if (token->type == CPP_COLON
16691 || (token->type == CPP_NAME
16692 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
16693 == CPP_COLON))
16695 tree identifier;
16696 tree width;
16698 /* Get the name of the bitfield. Note that we cannot just
16699 check TOKEN here because it may have been invalidated by
16700 the call to cp_lexer_peek_nth_token above. */
16701 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
16702 identifier = cp_parser_identifier (parser);
16703 else
16704 identifier = NULL_TREE;
16706 /* Consume the `:' token. */
16707 cp_lexer_consume_token (parser->lexer);
16708 /* Get the width of the bitfield. */
16709 width
16710 = cp_parser_constant_expression (parser,
16711 /*allow_non_constant=*/false,
16712 NULL);
16714 /* Look for attributes that apply to the bitfield. */
16715 attributes = cp_parser_attributes_opt (parser);
16716 /* Remember which attributes are prefix attributes and
16717 which are not. */
16718 first_attribute = attributes;
16719 /* Combine the attributes. */
16720 attributes = chainon (prefix_attributes, attributes);
16722 /* Create the bitfield declaration. */
16723 decl = grokbitfield (identifier
16724 ? make_id_declarator (NULL_TREE,
16725 identifier,
16726 sfk_none)
16727 : NULL,
16728 &decl_specifiers,
16729 width,
16730 attributes);
16732 else
16734 cp_declarator *declarator;
16735 tree initializer;
16736 tree asm_specification;
16737 int ctor_dtor_or_conv_p;
16739 /* Parse the declarator. */
16740 declarator
16741 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16742 &ctor_dtor_or_conv_p,
16743 /*parenthesized_p=*/NULL,
16744 /*member_p=*/true);
16746 /* If something went wrong parsing the declarator, make sure
16747 that we at least consume some tokens. */
16748 if (declarator == cp_error_declarator)
16750 /* Skip to the end of the statement. */
16751 cp_parser_skip_to_end_of_statement (parser);
16752 /* If the next token is not a semicolon, that is
16753 probably because we just skipped over the body of
16754 a function. So, we consume a semicolon if
16755 present, but do not issue an error message if it
16756 is not present. */
16757 if (cp_lexer_next_token_is (parser->lexer,
16758 CPP_SEMICOLON))
16759 cp_lexer_consume_token (parser->lexer);
16760 return;
16763 if (declares_class_or_enum & 2)
16764 cp_parser_check_for_definition_in_return_type
16765 (declarator, decl_specifiers.type,
16766 decl_specifiers.type_location);
16768 /* Look for an asm-specification. */
16769 asm_specification = cp_parser_asm_specification_opt (parser);
16770 /* Look for attributes that apply to the declaration. */
16771 attributes = cp_parser_attributes_opt (parser);
16772 /* Remember which attributes are prefix attributes and
16773 which are not. */
16774 first_attribute = attributes;
16775 /* Combine the attributes. */
16776 attributes = chainon (prefix_attributes, attributes);
16778 /* If it's an `=', then we have a constant-initializer or a
16779 pure-specifier. It is not correct to parse the
16780 initializer before registering the member declaration
16781 since the member declaration should be in scope while
16782 its initializer is processed. However, the rest of the
16783 front end does not yet provide an interface that allows
16784 us to handle this correctly. */
16785 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16787 /* In [class.mem]:
16789 A pure-specifier shall be used only in the declaration of
16790 a virtual function.
16792 A member-declarator can contain a constant-initializer
16793 only if it declares a static member of integral or
16794 enumeration type.
16796 Therefore, if the DECLARATOR is for a function, we look
16797 for a pure-specifier; otherwise, we look for a
16798 constant-initializer. When we call `grokfield', it will
16799 perform more stringent semantics checks. */
16800 initializer_token_start = cp_lexer_peek_token (parser->lexer);
16801 if (function_declarator_p (declarator))
16802 initializer = cp_parser_pure_specifier (parser);
16803 else
16804 /* Parse the initializer. */
16805 initializer = cp_parser_constant_initializer (parser);
16807 /* Otherwise, there is no initializer. */
16808 else
16809 initializer = NULL_TREE;
16811 /* See if we are probably looking at a function
16812 definition. We are certainly not looking at a
16813 member-declarator. Calling `grokfield' has
16814 side-effects, so we must not do it unless we are sure
16815 that we are looking at a member-declarator. */
16816 if (cp_parser_token_starts_function_definition_p
16817 (cp_lexer_peek_token (parser->lexer)))
16819 /* The grammar does not allow a pure-specifier to be
16820 used when a member function is defined. (It is
16821 possible that this fact is an oversight in the
16822 standard, since a pure function may be defined
16823 outside of the class-specifier. */
16824 if (initializer)
16825 error_at (initializer_token_start->location,
16826 "pure-specifier on function-definition");
16827 decl = cp_parser_save_member_function_body (parser,
16828 &decl_specifiers,
16829 declarator,
16830 attributes);
16831 /* If the member was not a friend, declare it here. */
16832 if (!friend_p)
16833 finish_member_declaration (decl);
16834 /* Peek at the next token. */
16835 token = cp_lexer_peek_token (parser->lexer);
16836 /* If the next token is a semicolon, consume it. */
16837 if (token->type == CPP_SEMICOLON)
16838 cp_lexer_consume_token (parser->lexer);
16839 return;
16841 else
16842 if (declarator->kind == cdk_function)
16843 declarator->id_loc = token->location;
16844 /* Create the declaration. */
16845 decl = grokfield (declarator, &decl_specifiers,
16846 initializer, /*init_const_expr_p=*/true,
16847 asm_specification,
16848 attributes);
16851 /* Reset PREFIX_ATTRIBUTES. */
16852 while (attributes && TREE_CHAIN (attributes) != first_attribute)
16853 attributes = TREE_CHAIN (attributes);
16854 if (attributes)
16855 TREE_CHAIN (attributes) = NULL_TREE;
16857 /* If there is any qualification still in effect, clear it
16858 now; we will be starting fresh with the next declarator. */
16859 parser->scope = NULL_TREE;
16860 parser->qualifying_scope = NULL_TREE;
16861 parser->object_scope = NULL_TREE;
16862 /* If it's a `,', then there are more declarators. */
16863 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16864 cp_lexer_consume_token (parser->lexer);
16865 /* If the next token isn't a `;', then we have a parse error. */
16866 else if (cp_lexer_next_token_is_not (parser->lexer,
16867 CPP_SEMICOLON))
16869 cp_parser_error (parser, "expected %<;%>");
16870 /* Skip tokens until we find a `;'. */
16871 cp_parser_skip_to_end_of_statement (parser);
16873 break;
16876 if (decl)
16878 /* Add DECL to the list of members. */
16879 if (!friend_p)
16880 finish_member_declaration (decl);
16882 if (TREE_CODE (decl) == FUNCTION_DECL)
16883 cp_parser_save_default_args (parser, decl);
16888 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16891 /* Parse a pure-specifier.
16893 pure-specifier:
16896 Returns INTEGER_ZERO_NODE if a pure specifier is found.
16897 Otherwise, ERROR_MARK_NODE is returned. */
16899 static tree
16900 cp_parser_pure_specifier (cp_parser* parser)
16902 cp_token *token;
16904 /* Look for the `=' token. */
16905 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16906 return error_mark_node;
16907 /* Look for the `0' token. */
16908 token = cp_lexer_peek_token (parser->lexer);
16910 if (token->type == CPP_EOF
16911 || token->type == CPP_PRAGMA_EOL)
16912 return error_mark_node;
16914 cp_lexer_consume_token (parser->lexer);
16916 /* Accept = default or = delete in c++0x mode. */
16917 if (token->keyword == RID_DEFAULT
16918 || token->keyword == RID_DELETE)
16920 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
16921 return token->u.value;
16924 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
16925 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
16927 cp_parser_error (parser,
16928 "invalid pure specifier (only %<= 0%> is allowed)");
16929 cp_parser_skip_to_end_of_statement (parser);
16930 return error_mark_node;
16932 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
16934 error_at (token->location, "templates may not be %<virtual%>");
16935 return error_mark_node;
16938 return integer_zero_node;
16941 /* Parse a constant-initializer.
16943 constant-initializer:
16944 = constant-expression
16946 Returns a representation of the constant-expression. */
16948 static tree
16949 cp_parser_constant_initializer (cp_parser* parser)
16951 /* Look for the `=' token. */
16952 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16953 return error_mark_node;
16955 /* It is invalid to write:
16957 struct S { static const int i = { 7 }; };
16960 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16962 cp_parser_error (parser,
16963 "a brace-enclosed initializer is not allowed here");
16964 /* Consume the opening brace. */
16965 cp_lexer_consume_token (parser->lexer);
16966 /* Skip the initializer. */
16967 cp_parser_skip_to_closing_brace (parser);
16968 /* Look for the trailing `}'. */
16969 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
16971 return error_mark_node;
16974 return cp_parser_constant_expression (parser,
16975 /*allow_non_constant=*/false,
16976 NULL);
16979 /* Derived classes [gram.class.derived] */
16981 /* Parse a base-clause.
16983 base-clause:
16984 : base-specifier-list
16986 base-specifier-list:
16987 base-specifier ... [opt]
16988 base-specifier-list , base-specifier ... [opt]
16990 Returns a TREE_LIST representing the base-classes, in the order in
16991 which they were declared. The representation of each node is as
16992 described by cp_parser_base_specifier.
16994 In the case that no bases are specified, this function will return
16995 NULL_TREE, not ERROR_MARK_NODE. */
16997 static tree
16998 cp_parser_base_clause (cp_parser* parser)
17000 tree bases = NULL_TREE;
17002 /* Look for the `:' that begins the list. */
17003 cp_parser_require (parser, CPP_COLON, "%<:%>");
17005 /* Scan the base-specifier-list. */
17006 while (true)
17008 cp_token *token;
17009 tree base;
17010 bool pack_expansion_p = false;
17012 /* Look for the base-specifier. */
17013 base = cp_parser_base_specifier (parser);
17014 /* Look for the (optional) ellipsis. */
17015 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17017 /* Consume the `...'. */
17018 cp_lexer_consume_token (parser->lexer);
17020 pack_expansion_p = true;
17023 /* Add BASE to the front of the list. */
17024 if (base != error_mark_node)
17026 if (pack_expansion_p)
17027 /* Make this a pack expansion type. */
17028 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
17031 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
17033 TREE_CHAIN (base) = bases;
17034 bases = base;
17037 /* Peek at the next token. */
17038 token = cp_lexer_peek_token (parser->lexer);
17039 /* If it's not a comma, then the list is complete. */
17040 if (token->type != CPP_COMMA)
17041 break;
17042 /* Consume the `,'. */
17043 cp_lexer_consume_token (parser->lexer);
17046 /* PARSER->SCOPE may still be non-NULL at this point, if the last
17047 base class had a qualified name. However, the next name that
17048 appears is certainly not qualified. */
17049 parser->scope = NULL_TREE;
17050 parser->qualifying_scope = NULL_TREE;
17051 parser->object_scope = NULL_TREE;
17053 return nreverse (bases);
17056 /* Parse a base-specifier.
17058 base-specifier:
17059 :: [opt] nested-name-specifier [opt] class-name
17060 virtual access-specifier [opt] :: [opt] nested-name-specifier
17061 [opt] class-name
17062 access-specifier virtual [opt] :: [opt] nested-name-specifier
17063 [opt] class-name
17065 Returns a TREE_LIST. The TREE_PURPOSE will be one of
17066 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
17067 indicate the specifiers provided. The TREE_VALUE will be a TYPE
17068 (or the ERROR_MARK_NODE) indicating the type that was specified. */
17070 static tree
17071 cp_parser_base_specifier (cp_parser* parser)
17073 cp_token *token;
17074 bool done = false;
17075 bool virtual_p = false;
17076 bool duplicate_virtual_error_issued_p = false;
17077 bool duplicate_access_error_issued_p = false;
17078 bool class_scope_p, template_p;
17079 tree access = access_default_node;
17080 tree type;
17082 /* Process the optional `virtual' and `access-specifier'. */
17083 while (!done)
17085 /* Peek at the next token. */
17086 token = cp_lexer_peek_token (parser->lexer);
17087 /* Process `virtual'. */
17088 switch (token->keyword)
17090 case RID_VIRTUAL:
17091 /* If `virtual' appears more than once, issue an error. */
17092 if (virtual_p && !duplicate_virtual_error_issued_p)
17094 cp_parser_error (parser,
17095 "%<virtual%> specified more than once in base-specified");
17096 duplicate_virtual_error_issued_p = true;
17099 virtual_p = true;
17101 /* Consume the `virtual' token. */
17102 cp_lexer_consume_token (parser->lexer);
17104 break;
17106 case RID_PUBLIC:
17107 case RID_PROTECTED:
17108 case RID_PRIVATE:
17109 /* If more than one access specifier appears, issue an
17110 error. */
17111 if (access != access_default_node
17112 && !duplicate_access_error_issued_p)
17114 cp_parser_error (parser,
17115 "more than one access specifier in base-specified");
17116 duplicate_access_error_issued_p = true;
17119 access = ridpointers[(int) token->keyword];
17121 /* Consume the access-specifier. */
17122 cp_lexer_consume_token (parser->lexer);
17124 break;
17126 default:
17127 done = true;
17128 break;
17131 /* It is not uncommon to see programs mechanically, erroneously, use
17132 the 'typename' keyword to denote (dependent) qualified types
17133 as base classes. */
17134 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
17136 token = cp_lexer_peek_token (parser->lexer);
17137 if (!processing_template_decl)
17138 error_at (token->location,
17139 "keyword %<typename%> not allowed outside of templates");
17140 else
17141 error_at (token->location,
17142 "keyword %<typename%> not allowed in this context "
17143 "(the base class is implicitly a type)");
17144 cp_lexer_consume_token (parser->lexer);
17147 /* Look for the optional `::' operator. */
17148 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
17149 /* Look for the nested-name-specifier. The simplest way to
17150 implement:
17152 [temp.res]
17154 The keyword `typename' is not permitted in a base-specifier or
17155 mem-initializer; in these contexts a qualified name that
17156 depends on a template-parameter is implicitly assumed to be a
17157 type name.
17159 is to pretend that we have seen the `typename' keyword at this
17160 point. */
17161 cp_parser_nested_name_specifier_opt (parser,
17162 /*typename_keyword_p=*/true,
17163 /*check_dependency_p=*/true,
17164 typename_type,
17165 /*is_declaration=*/true);
17166 /* If the base class is given by a qualified name, assume that names
17167 we see are type names or templates, as appropriate. */
17168 class_scope_p = (parser->scope && TYPE_P (parser->scope));
17169 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
17171 /* Finally, look for the class-name. */
17172 type = cp_parser_class_name (parser,
17173 class_scope_p,
17174 template_p,
17175 typename_type,
17176 /*check_dependency_p=*/true,
17177 /*class_head_p=*/false,
17178 /*is_declaration=*/true);
17180 if (type == error_mark_node)
17181 return error_mark_node;
17183 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
17186 /* Exception handling [gram.exception] */
17188 /* Parse an (optional) exception-specification.
17190 exception-specification:
17191 throw ( type-id-list [opt] )
17193 Returns a TREE_LIST representing the exception-specification. The
17194 TREE_VALUE of each node is a type. */
17196 static tree
17197 cp_parser_exception_specification_opt (cp_parser* parser)
17199 cp_token *token;
17200 tree type_id_list;
17202 /* Peek at the next token. */
17203 token = cp_lexer_peek_token (parser->lexer);
17204 /* If it's not `throw', then there's no exception-specification. */
17205 if (!cp_parser_is_keyword (token, RID_THROW))
17206 return NULL_TREE;
17208 /* Consume the `throw'. */
17209 cp_lexer_consume_token (parser->lexer);
17211 /* Look for the `('. */
17212 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17214 /* Peek at the next token. */
17215 token = cp_lexer_peek_token (parser->lexer);
17216 /* If it's not a `)', then there is a type-id-list. */
17217 if (token->type != CPP_CLOSE_PAREN)
17219 const char *saved_message;
17221 /* Types may not be defined in an exception-specification. */
17222 saved_message = parser->type_definition_forbidden_message;
17223 parser->type_definition_forbidden_message
17224 = "types may not be defined in an exception-specification";
17225 /* Parse the type-id-list. */
17226 type_id_list = cp_parser_type_id_list (parser);
17227 /* Restore the saved message. */
17228 parser->type_definition_forbidden_message = saved_message;
17230 else
17231 type_id_list = empty_except_spec;
17233 /* Look for the `)'. */
17234 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17236 return type_id_list;
17239 /* Parse an (optional) type-id-list.
17241 type-id-list:
17242 type-id ... [opt]
17243 type-id-list , type-id ... [opt]
17245 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
17246 in the order that the types were presented. */
17248 static tree
17249 cp_parser_type_id_list (cp_parser* parser)
17251 tree types = NULL_TREE;
17253 while (true)
17255 cp_token *token;
17256 tree type;
17258 /* Get the next type-id. */
17259 type = cp_parser_type_id (parser);
17260 /* Parse the optional ellipsis. */
17261 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17263 /* Consume the `...'. */
17264 cp_lexer_consume_token (parser->lexer);
17266 /* Turn the type into a pack expansion expression. */
17267 type = make_pack_expansion (type);
17269 /* Add it to the list. */
17270 types = add_exception_specifier (types, type, /*complain=*/1);
17271 /* Peek at the next token. */
17272 token = cp_lexer_peek_token (parser->lexer);
17273 /* If it is not a `,', we are done. */
17274 if (token->type != CPP_COMMA)
17275 break;
17276 /* Consume the `,'. */
17277 cp_lexer_consume_token (parser->lexer);
17280 return nreverse (types);
17283 /* Parse a try-block.
17285 try-block:
17286 try compound-statement handler-seq */
17288 static tree
17289 cp_parser_try_block (cp_parser* parser)
17291 tree try_block;
17293 cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
17294 try_block = begin_try_block ();
17295 cp_parser_compound_statement (parser, NULL, true);
17296 finish_try_block (try_block);
17297 cp_parser_handler_seq (parser);
17298 finish_handler_sequence (try_block);
17300 return try_block;
17303 /* Parse a function-try-block.
17305 function-try-block:
17306 try ctor-initializer [opt] function-body handler-seq */
17308 static bool
17309 cp_parser_function_try_block (cp_parser* parser)
17311 tree compound_stmt;
17312 tree try_block;
17313 bool ctor_initializer_p;
17315 /* Look for the `try' keyword. */
17316 if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
17317 return false;
17318 /* Let the rest of the front end know where we are. */
17319 try_block = begin_function_try_block (&compound_stmt);
17320 /* Parse the function-body. */
17321 ctor_initializer_p
17322 = cp_parser_ctor_initializer_opt_and_function_body (parser);
17323 /* We're done with the `try' part. */
17324 finish_function_try_block (try_block);
17325 /* Parse the handlers. */
17326 cp_parser_handler_seq (parser);
17327 /* We're done with the handlers. */
17328 finish_function_handler_sequence (try_block, compound_stmt);
17330 return ctor_initializer_p;
17333 /* Parse a handler-seq.
17335 handler-seq:
17336 handler handler-seq [opt] */
17338 static void
17339 cp_parser_handler_seq (cp_parser* parser)
17341 while (true)
17343 cp_token *token;
17345 /* Parse the handler. */
17346 cp_parser_handler (parser);
17347 /* Peek at the next token. */
17348 token = cp_lexer_peek_token (parser->lexer);
17349 /* If it's not `catch' then there are no more handlers. */
17350 if (!cp_parser_is_keyword (token, RID_CATCH))
17351 break;
17355 /* Parse a handler.
17357 handler:
17358 catch ( exception-declaration ) compound-statement */
17360 static void
17361 cp_parser_handler (cp_parser* parser)
17363 tree handler;
17364 tree declaration;
17366 cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
17367 handler = begin_handler ();
17368 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17369 declaration = cp_parser_exception_declaration (parser);
17370 finish_handler_parms (declaration, handler);
17371 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17372 cp_parser_compound_statement (parser, NULL, false);
17373 finish_handler (handler);
17376 /* Parse an exception-declaration.
17378 exception-declaration:
17379 type-specifier-seq declarator
17380 type-specifier-seq abstract-declarator
17381 type-specifier-seq
17384 Returns a VAR_DECL for the declaration, or NULL_TREE if the
17385 ellipsis variant is used. */
17387 static tree
17388 cp_parser_exception_declaration (cp_parser* parser)
17390 cp_decl_specifier_seq type_specifiers;
17391 cp_declarator *declarator;
17392 const char *saved_message;
17394 /* If it's an ellipsis, it's easy to handle. */
17395 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17397 /* Consume the `...' token. */
17398 cp_lexer_consume_token (parser->lexer);
17399 return NULL_TREE;
17402 /* Types may not be defined in exception-declarations. */
17403 saved_message = parser->type_definition_forbidden_message;
17404 parser->type_definition_forbidden_message
17405 = "types may not be defined in exception-declarations";
17407 /* Parse the type-specifier-seq. */
17408 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
17409 /*is_trailing_return=*/false,
17410 &type_specifiers);
17411 /* If it's a `)', then there is no declarator. */
17412 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
17413 declarator = NULL;
17414 else
17415 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
17416 /*ctor_dtor_or_conv_p=*/NULL,
17417 /*parenthesized_p=*/NULL,
17418 /*member_p=*/false);
17420 /* Restore the saved message. */
17421 parser->type_definition_forbidden_message = saved_message;
17423 if (!type_specifiers.any_specifiers_p)
17424 return error_mark_node;
17426 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
17429 /* Parse a throw-expression.
17431 throw-expression:
17432 throw assignment-expression [opt]
17434 Returns a THROW_EXPR representing the throw-expression. */
17436 static tree
17437 cp_parser_throw_expression (cp_parser* parser)
17439 tree expression;
17440 cp_token* token;
17442 cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
17443 token = cp_lexer_peek_token (parser->lexer);
17444 /* Figure out whether or not there is an assignment-expression
17445 following the "throw" keyword. */
17446 if (token->type == CPP_COMMA
17447 || token->type == CPP_SEMICOLON
17448 || token->type == CPP_CLOSE_PAREN
17449 || token->type == CPP_CLOSE_SQUARE
17450 || token->type == CPP_CLOSE_BRACE
17451 || token->type == CPP_COLON)
17452 expression = NULL_TREE;
17453 else
17454 expression = cp_parser_assignment_expression (parser,
17455 /*cast_p=*/false, NULL);
17457 return build_throw (expression);
17460 /* GNU Extensions */
17462 /* Parse an (optional) asm-specification.
17464 asm-specification:
17465 asm ( string-literal )
17467 If the asm-specification is present, returns a STRING_CST
17468 corresponding to the string-literal. Otherwise, returns
17469 NULL_TREE. */
17471 static tree
17472 cp_parser_asm_specification_opt (cp_parser* parser)
17474 cp_token *token;
17475 tree asm_specification;
17477 /* Peek at the next token. */
17478 token = cp_lexer_peek_token (parser->lexer);
17479 /* If the next token isn't the `asm' keyword, then there's no
17480 asm-specification. */
17481 if (!cp_parser_is_keyword (token, RID_ASM))
17482 return NULL_TREE;
17484 /* Consume the `asm' token. */
17485 cp_lexer_consume_token (parser->lexer);
17486 /* Look for the `('. */
17487 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17489 /* Look for the string-literal. */
17490 asm_specification = cp_parser_string_literal (parser, false, false);
17492 /* Look for the `)'. */
17493 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17495 return asm_specification;
17498 /* Parse an asm-operand-list.
17500 asm-operand-list:
17501 asm-operand
17502 asm-operand-list , asm-operand
17504 asm-operand:
17505 string-literal ( expression )
17506 [ string-literal ] string-literal ( expression )
17508 Returns a TREE_LIST representing the operands. The TREE_VALUE of
17509 each node is the expression. The TREE_PURPOSE is itself a
17510 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
17511 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
17512 is a STRING_CST for the string literal before the parenthesis. Returns
17513 ERROR_MARK_NODE if any of the operands are invalid. */
17515 static tree
17516 cp_parser_asm_operand_list (cp_parser* parser)
17518 tree asm_operands = NULL_TREE;
17519 bool invalid_operands = false;
17521 while (true)
17523 tree string_literal;
17524 tree expression;
17525 tree name;
17527 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
17529 /* Consume the `[' token. */
17530 cp_lexer_consume_token (parser->lexer);
17531 /* Read the operand name. */
17532 name = cp_parser_identifier (parser);
17533 if (name != error_mark_node)
17534 name = build_string (IDENTIFIER_LENGTH (name),
17535 IDENTIFIER_POINTER (name));
17536 /* Look for the closing `]'. */
17537 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
17539 else
17540 name = NULL_TREE;
17541 /* Look for the string-literal. */
17542 string_literal = cp_parser_string_literal (parser, false, false);
17544 /* Look for the `('. */
17545 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17546 /* Parse the expression. */
17547 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
17548 /* Look for the `)'. */
17549 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17551 if (name == error_mark_node
17552 || string_literal == error_mark_node
17553 || expression == error_mark_node)
17554 invalid_operands = true;
17556 /* Add this operand to the list. */
17557 asm_operands = tree_cons (build_tree_list (name, string_literal),
17558 expression,
17559 asm_operands);
17560 /* If the next token is not a `,', there are no more
17561 operands. */
17562 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17563 break;
17564 /* Consume the `,'. */
17565 cp_lexer_consume_token (parser->lexer);
17568 return invalid_operands ? error_mark_node : nreverse (asm_operands);
17571 /* Parse an asm-clobber-list.
17573 asm-clobber-list:
17574 string-literal
17575 asm-clobber-list , string-literal
17577 Returns a TREE_LIST, indicating the clobbers in the order that they
17578 appeared. The TREE_VALUE of each node is a STRING_CST. */
17580 static tree
17581 cp_parser_asm_clobber_list (cp_parser* parser)
17583 tree clobbers = NULL_TREE;
17585 while (true)
17587 tree string_literal;
17589 /* Look for the string literal. */
17590 string_literal = cp_parser_string_literal (parser, false, false);
17591 /* Add it to the list. */
17592 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
17593 /* If the next token is not a `,', then the list is
17594 complete. */
17595 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17596 break;
17597 /* Consume the `,' token. */
17598 cp_lexer_consume_token (parser->lexer);
17601 return clobbers;
17604 /* Parse an asm-label-list.
17606 asm-label-list:
17607 identifier
17608 asm-label-list , identifier
17610 Returns a TREE_LIST, indicating the labels in the order that they
17611 appeared. The TREE_VALUE of each node is a label. */
17613 static tree
17614 cp_parser_asm_label_list (cp_parser* parser)
17616 tree labels = NULL_TREE;
17618 while (true)
17620 tree identifier, label, name;
17622 /* Look for the identifier. */
17623 identifier = cp_parser_identifier (parser);
17624 if (!error_operand_p (identifier))
17626 label = lookup_label (identifier);
17627 if (TREE_CODE (label) == LABEL_DECL)
17629 TREE_USED (label) = 1;
17630 check_goto (label);
17631 name = build_string (IDENTIFIER_LENGTH (identifier),
17632 IDENTIFIER_POINTER (identifier));
17633 labels = tree_cons (name, label, labels);
17636 /* If the next token is not a `,', then the list is
17637 complete. */
17638 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17639 break;
17640 /* Consume the `,' token. */
17641 cp_lexer_consume_token (parser->lexer);
17644 return nreverse (labels);
17647 /* Parse an (optional) series of attributes.
17649 attributes:
17650 attributes attribute
17652 attribute:
17653 __attribute__ (( attribute-list [opt] ))
17655 The return value is as for cp_parser_attribute_list. */
17657 static tree
17658 cp_parser_attributes_opt (cp_parser* parser)
17660 tree attributes = NULL_TREE;
17662 while (true)
17664 cp_token *token;
17665 tree attribute_list;
17667 /* Peek at the next token. */
17668 token = cp_lexer_peek_token (parser->lexer);
17669 /* If it's not `__attribute__', then we're done. */
17670 if (token->keyword != RID_ATTRIBUTE)
17671 break;
17673 /* Consume the `__attribute__' keyword. */
17674 cp_lexer_consume_token (parser->lexer);
17675 /* Look for the two `(' tokens. */
17676 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17677 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17679 /* Peek at the next token. */
17680 token = cp_lexer_peek_token (parser->lexer);
17681 if (token->type != CPP_CLOSE_PAREN)
17682 /* Parse the attribute-list. */
17683 attribute_list = cp_parser_attribute_list (parser);
17684 else
17685 /* If the next token is a `)', then there is no attribute
17686 list. */
17687 attribute_list = NULL;
17689 /* Look for the two `)' tokens. */
17690 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17691 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17693 /* Add these new attributes to the list. */
17694 attributes = chainon (attributes, attribute_list);
17697 return attributes;
17700 /* Parse an attribute-list.
17702 attribute-list:
17703 attribute
17704 attribute-list , attribute
17706 attribute:
17707 identifier
17708 identifier ( identifier )
17709 identifier ( identifier , expression-list )
17710 identifier ( expression-list )
17712 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
17713 to an attribute. The TREE_PURPOSE of each node is the identifier
17714 indicating which attribute is in use. The TREE_VALUE represents
17715 the arguments, if any. */
17717 static tree
17718 cp_parser_attribute_list (cp_parser* parser)
17720 tree attribute_list = NULL_TREE;
17721 bool save_translate_strings_p = parser->translate_strings_p;
17723 parser->translate_strings_p = false;
17724 while (true)
17726 cp_token *token;
17727 tree identifier;
17728 tree attribute;
17730 /* Look for the identifier. We also allow keywords here; for
17731 example `__attribute__ ((const))' is legal. */
17732 token = cp_lexer_peek_token (parser->lexer);
17733 if (token->type == CPP_NAME
17734 || token->type == CPP_KEYWORD)
17736 tree arguments = NULL_TREE;
17738 /* Consume the token. */
17739 token = cp_lexer_consume_token (parser->lexer);
17741 /* Save away the identifier that indicates which attribute
17742 this is. */
17743 identifier = (token->type == CPP_KEYWORD)
17744 /* For keywords, use the canonical spelling, not the
17745 parsed identifier. */
17746 ? ridpointers[(int) token->keyword]
17747 : token->u.value;
17749 attribute = build_tree_list (identifier, NULL_TREE);
17751 /* Peek at the next token. */
17752 token = cp_lexer_peek_token (parser->lexer);
17753 /* If it's an `(', then parse the attribute arguments. */
17754 if (token->type == CPP_OPEN_PAREN)
17756 VEC(tree,gc) *vec;
17757 vec = cp_parser_parenthesized_expression_list
17758 (parser, true, /*cast_p=*/false,
17759 /*allow_expansion_p=*/false,
17760 /*non_constant_p=*/NULL);
17761 if (vec == NULL)
17762 arguments = error_mark_node;
17763 else
17765 arguments = build_tree_list_vec (vec);
17766 release_tree_vector (vec);
17768 /* Save the arguments away. */
17769 TREE_VALUE (attribute) = arguments;
17772 if (arguments != error_mark_node)
17774 /* Add this attribute to the list. */
17775 TREE_CHAIN (attribute) = attribute_list;
17776 attribute_list = attribute;
17779 token = cp_lexer_peek_token (parser->lexer);
17781 /* Now, look for more attributes. If the next token isn't a
17782 `,', we're done. */
17783 if (token->type != CPP_COMMA)
17784 break;
17786 /* Consume the comma and keep going. */
17787 cp_lexer_consume_token (parser->lexer);
17789 parser->translate_strings_p = save_translate_strings_p;
17791 /* We built up the list in reverse order. */
17792 return nreverse (attribute_list);
17795 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
17796 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
17797 current value of the PEDANTIC flag, regardless of whether or not
17798 the `__extension__' keyword is present. The caller is responsible
17799 for restoring the value of the PEDANTIC flag. */
17801 static bool
17802 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
17804 /* Save the old value of the PEDANTIC flag. */
17805 *saved_pedantic = pedantic;
17807 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
17809 /* Consume the `__extension__' token. */
17810 cp_lexer_consume_token (parser->lexer);
17811 /* We're not being pedantic while the `__extension__' keyword is
17812 in effect. */
17813 pedantic = 0;
17815 return true;
17818 return false;
17821 /* Parse a label declaration.
17823 label-declaration:
17824 __label__ label-declarator-seq ;
17826 label-declarator-seq:
17827 identifier , label-declarator-seq
17828 identifier */
17830 static void
17831 cp_parser_label_declaration (cp_parser* parser)
17833 /* Look for the `__label__' keyword. */
17834 cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
17836 while (true)
17838 tree identifier;
17840 /* Look for an identifier. */
17841 identifier = cp_parser_identifier (parser);
17842 /* If we failed, stop. */
17843 if (identifier == error_mark_node)
17844 break;
17845 /* Declare it as a label. */
17846 finish_label_decl (identifier);
17847 /* If the next token is a `;', stop. */
17848 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17849 break;
17850 /* Look for the `,' separating the label declarations. */
17851 cp_parser_require (parser, CPP_COMMA, "%<,%>");
17854 /* Look for the final `;'. */
17855 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
17858 /* Support Functions */
17860 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
17861 NAME should have one of the representations used for an
17862 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
17863 is returned. If PARSER->SCOPE is a dependent type, then a
17864 SCOPE_REF is returned.
17866 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
17867 returned; the name was already resolved when the TEMPLATE_ID_EXPR
17868 was formed. Abstractly, such entities should not be passed to this
17869 function, because they do not need to be looked up, but it is
17870 simpler to check for this special case here, rather than at the
17871 call-sites.
17873 In cases not explicitly covered above, this function returns a
17874 DECL, OVERLOAD, or baselink representing the result of the lookup.
17875 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
17876 is returned.
17878 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
17879 (e.g., "struct") that was used. In that case bindings that do not
17880 refer to types are ignored.
17882 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
17883 ignored.
17885 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
17886 are ignored.
17888 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
17889 types.
17891 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
17892 TREE_LIST of candidates if name-lookup results in an ambiguity, and
17893 NULL_TREE otherwise. */
17895 static tree
17896 cp_parser_lookup_name (cp_parser *parser, tree name,
17897 enum tag_types tag_type,
17898 bool is_template,
17899 bool is_namespace,
17900 bool check_dependency,
17901 tree *ambiguous_decls,
17902 location_t name_location)
17904 int flags = 0;
17905 tree decl;
17906 tree object_type = parser->context->object_type;
17908 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17909 flags |= LOOKUP_COMPLAIN;
17911 /* Assume that the lookup will be unambiguous. */
17912 if (ambiguous_decls)
17913 *ambiguous_decls = NULL_TREE;
17915 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
17916 no longer valid. Note that if we are parsing tentatively, and
17917 the parse fails, OBJECT_TYPE will be automatically restored. */
17918 parser->context->object_type = NULL_TREE;
17920 if (name == error_mark_node)
17921 return error_mark_node;
17923 /* A template-id has already been resolved; there is no lookup to
17924 do. */
17925 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
17926 return name;
17927 if (BASELINK_P (name))
17929 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
17930 == TEMPLATE_ID_EXPR);
17931 return name;
17934 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
17935 it should already have been checked to make sure that the name
17936 used matches the type being destroyed. */
17937 if (TREE_CODE (name) == BIT_NOT_EXPR)
17939 tree type;
17941 /* Figure out to which type this destructor applies. */
17942 if (parser->scope)
17943 type = parser->scope;
17944 else if (object_type)
17945 type = object_type;
17946 else
17947 type = current_class_type;
17948 /* If that's not a class type, there is no destructor. */
17949 if (!type || !CLASS_TYPE_P (type))
17950 return error_mark_node;
17951 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
17952 lazily_declare_fn (sfk_destructor, type);
17953 if (!CLASSTYPE_DESTRUCTORS (type))
17954 return error_mark_node;
17955 /* If it was a class type, return the destructor. */
17956 return CLASSTYPE_DESTRUCTORS (type);
17959 /* By this point, the NAME should be an ordinary identifier. If
17960 the id-expression was a qualified name, the qualifying scope is
17961 stored in PARSER->SCOPE at this point. */
17962 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
17964 /* Perform the lookup. */
17965 if (parser->scope)
17967 bool dependent_p;
17969 if (parser->scope == error_mark_node)
17970 return error_mark_node;
17972 /* If the SCOPE is dependent, the lookup must be deferred until
17973 the template is instantiated -- unless we are explicitly
17974 looking up names in uninstantiated templates. Even then, we
17975 cannot look up the name if the scope is not a class type; it
17976 might, for example, be a template type parameter. */
17977 dependent_p = (TYPE_P (parser->scope)
17978 && dependent_scope_p (parser->scope));
17979 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
17980 && dependent_p)
17981 /* Defer lookup. */
17982 decl = error_mark_node;
17983 else
17985 tree pushed_scope = NULL_TREE;
17987 /* If PARSER->SCOPE is a dependent type, then it must be a
17988 class type, and we must not be checking dependencies;
17989 otherwise, we would have processed this lookup above. So
17990 that PARSER->SCOPE is not considered a dependent base by
17991 lookup_member, we must enter the scope here. */
17992 if (dependent_p)
17993 pushed_scope = push_scope (parser->scope);
17995 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
17996 lookup result and the nested-name-specifier nominates a class C:
17997 * if the name specified after the nested-name-specifier, when
17998 looked up in C, is the injected-class-name of C (Clause 9), or
17999 * if the name specified after the nested-name-specifier is the
18000 same as the identifier or the simple-template-id's template-
18001 name in the last component of the nested-name-specifier,
18002 the name is instead considered to name the constructor of
18003 class C. [ Note: for example, the constructor is not an
18004 acceptable lookup result in an elaborated-type-specifier so
18005 the constructor would not be used in place of the
18006 injected-class-name. --end note ] Such a constructor name
18007 shall be used only in the declarator-id of a declaration that
18008 names a constructor or in a using-declaration. */
18009 if (tag_type == none_type
18010 && CLASS_TYPE_P (parser->scope)
18011 && constructor_name_p (name, parser->scope))
18012 name = ctor_identifier;
18014 /* If the PARSER->SCOPE is a template specialization, it
18015 may be instantiated during name lookup. In that case,
18016 errors may be issued. Even if we rollback the current
18017 tentative parse, those errors are valid. */
18018 decl = lookup_qualified_name (parser->scope, name,
18019 tag_type != none_type,
18020 /*complain=*/true);
18022 /* If we have a single function from a using decl, pull it out. */
18023 if (TREE_CODE (decl) == OVERLOAD
18024 && !really_overloaded_fn (decl))
18025 decl = OVL_FUNCTION (decl);
18027 if (pushed_scope)
18028 pop_scope (pushed_scope);
18031 /* If the scope is a dependent type and either we deferred lookup or
18032 we did lookup but didn't find the name, rememeber the name. */
18033 if (decl == error_mark_node && TYPE_P (parser->scope)
18034 && dependent_type_p (parser->scope))
18036 if (tag_type)
18038 tree type;
18040 /* The resolution to Core Issue 180 says that `struct
18041 A::B' should be considered a type-name, even if `A'
18042 is dependent. */
18043 type = make_typename_type (parser->scope, name, tag_type,
18044 /*complain=*/tf_error);
18045 decl = TYPE_NAME (type);
18047 else if (is_template
18048 && (cp_parser_next_token_ends_template_argument_p (parser)
18049 || cp_lexer_next_token_is (parser->lexer,
18050 CPP_CLOSE_PAREN)))
18051 decl = make_unbound_class_template (parser->scope,
18052 name, NULL_TREE,
18053 /*complain=*/tf_error);
18054 else
18055 decl = build_qualified_name (/*type=*/NULL_TREE,
18056 parser->scope, name,
18057 is_template);
18059 parser->qualifying_scope = parser->scope;
18060 parser->object_scope = NULL_TREE;
18062 else if (object_type)
18064 tree object_decl = NULL_TREE;
18065 /* Look up the name in the scope of the OBJECT_TYPE, unless the
18066 OBJECT_TYPE is not a class. */
18067 if (CLASS_TYPE_P (object_type))
18068 /* If the OBJECT_TYPE is a template specialization, it may
18069 be instantiated during name lookup. In that case, errors
18070 may be issued. Even if we rollback the current tentative
18071 parse, those errors are valid. */
18072 object_decl = lookup_member (object_type,
18073 name,
18074 /*protect=*/0,
18075 tag_type != none_type);
18076 /* Look it up in the enclosing context, too. */
18077 decl = lookup_name_real (name, tag_type != none_type,
18078 /*nonclass=*/0,
18079 /*block_p=*/true, is_namespace, flags);
18080 parser->object_scope = object_type;
18081 parser->qualifying_scope = NULL_TREE;
18082 if (object_decl)
18083 decl = object_decl;
18085 else
18087 decl = lookup_name_real (name, tag_type != none_type,
18088 /*nonclass=*/0,
18089 /*block_p=*/true, is_namespace, flags);
18090 parser->qualifying_scope = NULL_TREE;
18091 parser->object_scope = NULL_TREE;
18094 /* If the lookup failed, let our caller know. */
18095 if (!decl || decl == error_mark_node)
18096 return error_mark_node;
18098 /* Pull out the template from an injected-class-name (or multiple). */
18099 if (is_template)
18100 decl = maybe_get_template_decl_from_type_decl (decl);
18102 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
18103 if (TREE_CODE (decl) == TREE_LIST)
18105 if (ambiguous_decls)
18106 *ambiguous_decls = decl;
18107 /* The error message we have to print is too complicated for
18108 cp_parser_error, so we incorporate its actions directly. */
18109 if (!cp_parser_simulate_error (parser))
18111 error_at (name_location, "reference to %qD is ambiguous",
18112 name);
18113 print_candidates (decl);
18115 return error_mark_node;
18118 gcc_assert (DECL_P (decl)
18119 || TREE_CODE (decl) == OVERLOAD
18120 || TREE_CODE (decl) == SCOPE_REF
18121 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
18122 || BASELINK_P (decl));
18124 /* If we have resolved the name of a member declaration, check to
18125 see if the declaration is accessible. When the name resolves to
18126 set of overloaded functions, accessibility is checked when
18127 overload resolution is done.
18129 During an explicit instantiation, access is not checked at all,
18130 as per [temp.explicit]. */
18131 if (DECL_P (decl))
18132 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
18134 return decl;
18137 /* Like cp_parser_lookup_name, but for use in the typical case where
18138 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
18139 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
18141 static tree
18142 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
18144 return cp_parser_lookup_name (parser, name,
18145 none_type,
18146 /*is_template=*/false,
18147 /*is_namespace=*/false,
18148 /*check_dependency=*/true,
18149 /*ambiguous_decls=*/NULL,
18150 location);
18153 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
18154 the current context, return the TYPE_DECL. If TAG_NAME_P is
18155 true, the DECL indicates the class being defined in a class-head,
18156 or declared in an elaborated-type-specifier.
18158 Otherwise, return DECL. */
18160 static tree
18161 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
18163 /* If the TEMPLATE_DECL is being declared as part of a class-head,
18164 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
18166 struct A {
18167 template <typename T> struct B;
18170 template <typename T> struct A::B {};
18172 Similarly, in an elaborated-type-specifier:
18174 namespace N { struct X{}; }
18176 struct A {
18177 template <typename T> friend struct N::X;
18180 However, if the DECL refers to a class type, and we are in
18181 the scope of the class, then the name lookup automatically
18182 finds the TYPE_DECL created by build_self_reference rather
18183 than a TEMPLATE_DECL. For example, in:
18185 template <class T> struct S {
18186 S s;
18189 there is no need to handle such case. */
18191 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
18192 return DECL_TEMPLATE_RESULT (decl);
18194 return decl;
18197 /* If too many, or too few, template-parameter lists apply to the
18198 declarator, issue an error message. Returns TRUE if all went well,
18199 and FALSE otherwise. */
18201 static bool
18202 cp_parser_check_declarator_template_parameters (cp_parser* parser,
18203 cp_declarator *declarator,
18204 location_t declarator_location)
18206 unsigned num_templates;
18208 /* We haven't seen any classes that involve template parameters yet. */
18209 num_templates = 0;
18211 switch (declarator->kind)
18213 case cdk_id:
18214 if (declarator->u.id.qualifying_scope)
18216 tree scope;
18217 tree member;
18219 scope = declarator->u.id.qualifying_scope;
18220 member = declarator->u.id.unqualified_name;
18222 while (scope && CLASS_TYPE_P (scope))
18224 /* You're supposed to have one `template <...>'
18225 for every template class, but you don't need one
18226 for a full specialization. For example:
18228 template <class T> struct S{};
18229 template <> struct S<int> { void f(); };
18230 void S<int>::f () {}
18232 is correct; there shouldn't be a `template <>' for
18233 the definition of `S<int>::f'. */
18234 if (!CLASSTYPE_TEMPLATE_INFO (scope))
18235 /* If SCOPE does not have template information of any
18236 kind, then it is not a template, nor is it nested
18237 within a template. */
18238 break;
18239 if (explicit_class_specialization_p (scope))
18240 break;
18241 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
18242 ++num_templates;
18244 scope = TYPE_CONTEXT (scope);
18247 else if (TREE_CODE (declarator->u.id.unqualified_name)
18248 == TEMPLATE_ID_EXPR)
18249 /* If the DECLARATOR has the form `X<y>' then it uses one
18250 additional level of template parameters. */
18251 ++num_templates;
18253 return cp_parser_check_template_parameters
18254 (parser, num_templates, declarator_location, declarator);
18257 case cdk_function:
18258 case cdk_array:
18259 case cdk_pointer:
18260 case cdk_reference:
18261 case cdk_ptrmem:
18262 return (cp_parser_check_declarator_template_parameters
18263 (parser, declarator->declarator, declarator_location));
18265 case cdk_error:
18266 return true;
18268 default:
18269 gcc_unreachable ();
18271 return false;
18274 /* NUM_TEMPLATES were used in the current declaration. If that is
18275 invalid, return FALSE and issue an error messages. Otherwise,
18276 return TRUE. If DECLARATOR is non-NULL, then we are checking a
18277 declarator and we can print more accurate diagnostics. */
18279 static bool
18280 cp_parser_check_template_parameters (cp_parser* parser,
18281 unsigned num_templates,
18282 location_t location,
18283 cp_declarator *declarator)
18285 /* If there are the same number of template classes and parameter
18286 lists, that's OK. */
18287 if (parser->num_template_parameter_lists == num_templates)
18288 return true;
18289 /* If there are more, but only one more, then we are referring to a
18290 member template. That's OK too. */
18291 if (parser->num_template_parameter_lists == num_templates + 1)
18292 return true;
18293 /* If there are more template classes than parameter lists, we have
18294 something like:
18296 template <class T> void S<T>::R<T>::f (); */
18297 if (parser->num_template_parameter_lists < num_templates)
18299 if (declarator && !current_function_decl)
18300 error_at (location, "specializing member %<%T::%E%> "
18301 "requires %<template<>%> syntax",
18302 declarator->u.id.qualifying_scope,
18303 declarator->u.id.unqualified_name);
18304 else if (declarator)
18305 error_at (location, "invalid declaration of %<%T::%E%>",
18306 declarator->u.id.qualifying_scope,
18307 declarator->u.id.unqualified_name);
18308 else
18309 error_at (location, "too few template-parameter-lists");
18310 return false;
18312 /* Otherwise, there are too many template parameter lists. We have
18313 something like:
18315 template <class T> template <class U> void S::f(); */
18316 error_at (location, "too many template-parameter-lists");
18317 return false;
18320 /* Parse an optional `::' token indicating that the following name is
18321 from the global namespace. If so, PARSER->SCOPE is set to the
18322 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
18323 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
18324 Returns the new value of PARSER->SCOPE, if the `::' token is
18325 present, and NULL_TREE otherwise. */
18327 static tree
18328 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
18330 cp_token *token;
18332 /* Peek at the next token. */
18333 token = cp_lexer_peek_token (parser->lexer);
18334 /* If we're looking at a `::' token then we're starting from the
18335 global namespace, not our current location. */
18336 if (token->type == CPP_SCOPE)
18338 /* Consume the `::' token. */
18339 cp_lexer_consume_token (parser->lexer);
18340 /* Set the SCOPE so that we know where to start the lookup. */
18341 parser->scope = global_namespace;
18342 parser->qualifying_scope = global_namespace;
18343 parser->object_scope = NULL_TREE;
18345 return parser->scope;
18347 else if (!current_scope_valid_p)
18349 parser->scope = NULL_TREE;
18350 parser->qualifying_scope = NULL_TREE;
18351 parser->object_scope = NULL_TREE;
18354 return NULL_TREE;
18357 /* Returns TRUE if the upcoming token sequence is the start of a
18358 constructor declarator. If FRIEND_P is true, the declarator is
18359 preceded by the `friend' specifier. */
18361 static bool
18362 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
18364 bool constructor_p;
18365 tree nested_name_specifier;
18366 cp_token *next_token;
18368 /* The common case is that this is not a constructor declarator, so
18369 try to avoid doing lots of work if at all possible. It's not
18370 valid declare a constructor at function scope. */
18371 if (parser->in_function_body)
18372 return false;
18373 /* And only certain tokens can begin a constructor declarator. */
18374 next_token = cp_lexer_peek_token (parser->lexer);
18375 if (next_token->type != CPP_NAME
18376 && next_token->type != CPP_SCOPE
18377 && next_token->type != CPP_NESTED_NAME_SPECIFIER
18378 && next_token->type != CPP_TEMPLATE_ID)
18379 return false;
18381 /* Parse tentatively; we are going to roll back all of the tokens
18382 consumed here. */
18383 cp_parser_parse_tentatively (parser);
18384 /* Assume that we are looking at a constructor declarator. */
18385 constructor_p = true;
18387 /* Look for the optional `::' operator. */
18388 cp_parser_global_scope_opt (parser,
18389 /*current_scope_valid_p=*/false);
18390 /* Look for the nested-name-specifier. */
18391 nested_name_specifier
18392 = (cp_parser_nested_name_specifier_opt (parser,
18393 /*typename_keyword_p=*/false,
18394 /*check_dependency_p=*/false,
18395 /*type_p=*/false,
18396 /*is_declaration=*/false));
18397 /* Outside of a class-specifier, there must be a
18398 nested-name-specifier. */
18399 if (!nested_name_specifier &&
18400 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
18401 || friend_p))
18402 constructor_p = false;
18403 else if (nested_name_specifier == error_mark_node)
18404 constructor_p = false;
18406 /* If we have a class scope, this is easy; DR 147 says that S::S always
18407 names the constructor, and no other qualified name could. */
18408 if (constructor_p && nested_name_specifier
18409 && TYPE_P (nested_name_specifier))
18411 tree id = cp_parser_unqualified_id (parser,
18412 /*template_keyword_p=*/false,
18413 /*check_dependency_p=*/false,
18414 /*declarator_p=*/true,
18415 /*optional_p=*/false);
18416 if (is_overloaded_fn (id))
18417 id = DECL_NAME (get_first_fn (id));
18418 if (!constructor_name_p (id, nested_name_specifier))
18419 constructor_p = false;
18421 /* If we still think that this might be a constructor-declarator,
18422 look for a class-name. */
18423 else if (constructor_p)
18425 /* If we have:
18427 template <typename T> struct S {
18428 S();
18431 we must recognize that the nested `S' names a class. */
18432 tree type_decl;
18433 type_decl = cp_parser_class_name (parser,
18434 /*typename_keyword_p=*/false,
18435 /*template_keyword_p=*/false,
18436 none_type,
18437 /*check_dependency_p=*/false,
18438 /*class_head_p=*/false,
18439 /*is_declaration=*/false);
18440 /* If there was no class-name, then this is not a constructor. */
18441 constructor_p = !cp_parser_error_occurred (parser);
18443 /* If we're still considering a constructor, we have to see a `(',
18444 to begin the parameter-declaration-clause, followed by either a
18445 `)', an `...', or a decl-specifier. We need to check for a
18446 type-specifier to avoid being fooled into thinking that:
18448 S (f) (int);
18450 is a constructor. (It is actually a function named `f' that
18451 takes one parameter (of type `int') and returns a value of type
18452 `S'. */
18453 if (constructor_p
18454 && !cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
18455 constructor_p = false;
18457 if (constructor_p
18458 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
18459 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
18460 /* A parameter declaration begins with a decl-specifier,
18461 which is either the "attribute" keyword, a storage class
18462 specifier, or (usually) a type-specifier. */
18463 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
18465 tree type;
18466 tree pushed_scope = NULL_TREE;
18467 unsigned saved_num_template_parameter_lists;
18469 /* Names appearing in the type-specifier should be looked up
18470 in the scope of the class. */
18471 if (current_class_type)
18472 type = NULL_TREE;
18473 else
18475 type = TREE_TYPE (type_decl);
18476 if (TREE_CODE (type) == TYPENAME_TYPE)
18478 type = resolve_typename_type (type,
18479 /*only_current_p=*/false);
18480 if (TREE_CODE (type) == TYPENAME_TYPE)
18482 cp_parser_abort_tentative_parse (parser);
18483 return false;
18486 pushed_scope = push_scope (type);
18489 /* Inside the constructor parameter list, surrounding
18490 template-parameter-lists do not apply. */
18491 saved_num_template_parameter_lists
18492 = parser->num_template_parameter_lists;
18493 parser->num_template_parameter_lists = 0;
18495 /* Look for the type-specifier. */
18496 cp_parser_type_specifier (parser,
18497 CP_PARSER_FLAGS_NONE,
18498 /*decl_specs=*/NULL,
18499 /*is_declarator=*/true,
18500 /*declares_class_or_enum=*/NULL,
18501 /*is_cv_qualifier=*/NULL);
18503 parser->num_template_parameter_lists
18504 = saved_num_template_parameter_lists;
18506 /* Leave the scope of the class. */
18507 if (pushed_scope)
18508 pop_scope (pushed_scope);
18510 constructor_p = !cp_parser_error_occurred (parser);
18514 /* We did not really want to consume any tokens. */
18515 cp_parser_abort_tentative_parse (parser);
18517 return constructor_p;
18520 /* Parse the definition of the function given by the DECL_SPECIFIERS,
18521 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
18522 they must be performed once we are in the scope of the function.
18524 Returns the function defined. */
18526 static tree
18527 cp_parser_function_definition_from_specifiers_and_declarator
18528 (cp_parser* parser,
18529 cp_decl_specifier_seq *decl_specifiers,
18530 tree attributes,
18531 const cp_declarator *declarator)
18533 tree fn;
18534 bool success_p;
18536 /* Begin the function-definition. */
18537 success_p = start_function (decl_specifiers, declarator, attributes);
18539 /* The things we're about to see are not directly qualified by any
18540 template headers we've seen thus far. */
18541 reset_specialization ();
18543 /* If there were names looked up in the decl-specifier-seq that we
18544 did not check, check them now. We must wait until we are in the
18545 scope of the function to perform the checks, since the function
18546 might be a friend. */
18547 perform_deferred_access_checks ();
18549 if (!success_p)
18551 /* Skip the entire function. */
18552 cp_parser_skip_to_end_of_block_or_statement (parser);
18553 fn = error_mark_node;
18555 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
18557 /* Seen already, skip it. An error message has already been output. */
18558 cp_parser_skip_to_end_of_block_or_statement (parser);
18559 fn = current_function_decl;
18560 current_function_decl = NULL_TREE;
18561 /* If this is a function from a class, pop the nested class. */
18562 if (current_class_name)
18563 pop_nested_class ();
18565 else
18566 fn = cp_parser_function_definition_after_declarator (parser,
18567 /*inline_p=*/false);
18569 return fn;
18572 /* Parse the part of a function-definition that follows the
18573 declarator. INLINE_P is TRUE iff this function is an inline
18574 function defined within a class-specifier.
18576 Returns the function defined. */
18578 static tree
18579 cp_parser_function_definition_after_declarator (cp_parser* parser,
18580 bool inline_p)
18582 tree fn;
18583 bool ctor_initializer_p = false;
18584 bool saved_in_unbraced_linkage_specification_p;
18585 bool saved_in_function_body;
18586 unsigned saved_num_template_parameter_lists;
18587 cp_token *token;
18589 saved_in_function_body = parser->in_function_body;
18590 parser->in_function_body = true;
18591 /* If the next token is `return', then the code may be trying to
18592 make use of the "named return value" extension that G++ used to
18593 support. */
18594 token = cp_lexer_peek_token (parser->lexer);
18595 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
18597 /* Consume the `return' keyword. */
18598 cp_lexer_consume_token (parser->lexer);
18599 /* Look for the identifier that indicates what value is to be
18600 returned. */
18601 cp_parser_identifier (parser);
18602 /* Issue an error message. */
18603 error_at (token->location,
18604 "named return values are no longer supported");
18605 /* Skip tokens until we reach the start of the function body. */
18606 while (true)
18608 cp_token *token = cp_lexer_peek_token (parser->lexer);
18609 if (token->type == CPP_OPEN_BRACE
18610 || token->type == CPP_EOF
18611 || token->type == CPP_PRAGMA_EOL)
18612 break;
18613 cp_lexer_consume_token (parser->lexer);
18616 /* The `extern' in `extern "C" void f () { ... }' does not apply to
18617 anything declared inside `f'. */
18618 saved_in_unbraced_linkage_specification_p
18619 = parser->in_unbraced_linkage_specification_p;
18620 parser->in_unbraced_linkage_specification_p = false;
18621 /* Inside the function, surrounding template-parameter-lists do not
18622 apply. */
18623 saved_num_template_parameter_lists
18624 = parser->num_template_parameter_lists;
18625 parser->num_template_parameter_lists = 0;
18627 start_lambda_scope (current_function_decl);
18629 /* If the next token is `try', then we are looking at a
18630 function-try-block. */
18631 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
18632 ctor_initializer_p = cp_parser_function_try_block (parser);
18633 /* A function-try-block includes the function-body, so we only do
18634 this next part if we're not processing a function-try-block. */
18635 else
18636 ctor_initializer_p
18637 = cp_parser_ctor_initializer_opt_and_function_body (parser);
18639 finish_lambda_scope ();
18641 /* Finish the function. */
18642 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
18643 (inline_p ? 2 : 0));
18644 /* Generate code for it, if necessary. */
18645 expand_or_defer_fn (fn);
18646 /* Restore the saved values. */
18647 parser->in_unbraced_linkage_specification_p
18648 = saved_in_unbraced_linkage_specification_p;
18649 parser->num_template_parameter_lists
18650 = saved_num_template_parameter_lists;
18651 parser->in_function_body = saved_in_function_body;
18653 return fn;
18656 /* Parse a template-declaration, assuming that the `export' (and
18657 `extern') keywords, if present, has already been scanned. MEMBER_P
18658 is as for cp_parser_template_declaration. */
18660 static void
18661 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
18663 tree decl = NULL_TREE;
18664 VEC (deferred_access_check,gc) *checks;
18665 tree parameter_list;
18666 bool friend_p = false;
18667 bool need_lang_pop;
18668 cp_token *token;
18670 /* Look for the `template' keyword. */
18671 token = cp_lexer_peek_token (parser->lexer);
18672 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
18673 return;
18675 /* And the `<'. */
18676 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
18677 return;
18678 if (at_class_scope_p () && current_function_decl)
18680 /* 14.5.2.2 [temp.mem]
18682 A local class shall not have member templates. */
18683 error_at (token->location,
18684 "invalid declaration of member template in local class");
18685 cp_parser_skip_to_end_of_block_or_statement (parser);
18686 return;
18688 /* [temp]
18690 A template ... shall not have C linkage. */
18691 if (current_lang_name == lang_name_c)
18693 error_at (token->location, "template with C linkage");
18694 /* Give it C++ linkage to avoid confusing other parts of the
18695 front end. */
18696 push_lang_context (lang_name_cplusplus);
18697 need_lang_pop = true;
18699 else
18700 need_lang_pop = false;
18702 /* We cannot perform access checks on the template parameter
18703 declarations until we know what is being declared, just as we
18704 cannot check the decl-specifier list. */
18705 push_deferring_access_checks (dk_deferred);
18707 /* If the next token is `>', then we have an invalid
18708 specialization. Rather than complain about an invalid template
18709 parameter, issue an error message here. */
18710 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
18712 cp_parser_error (parser, "invalid explicit specialization");
18713 begin_specialization ();
18714 parameter_list = NULL_TREE;
18716 else
18717 /* Parse the template parameters. */
18718 parameter_list = cp_parser_template_parameter_list (parser);
18720 /* Get the deferred access checks from the parameter list. These
18721 will be checked once we know what is being declared, as for a
18722 member template the checks must be performed in the scope of the
18723 class containing the member. */
18724 checks = get_deferred_access_checks ();
18726 /* Look for the `>'. */
18727 cp_parser_skip_to_end_of_template_parameter_list (parser);
18728 /* We just processed one more parameter list. */
18729 ++parser->num_template_parameter_lists;
18730 /* If the next token is `template', there are more template
18731 parameters. */
18732 if (cp_lexer_next_token_is_keyword (parser->lexer,
18733 RID_TEMPLATE))
18734 cp_parser_template_declaration_after_export (parser, member_p);
18735 else
18737 /* There are no access checks when parsing a template, as we do not
18738 know if a specialization will be a friend. */
18739 push_deferring_access_checks (dk_no_check);
18740 token = cp_lexer_peek_token (parser->lexer);
18741 decl = cp_parser_single_declaration (parser,
18742 checks,
18743 member_p,
18744 /*explicit_specialization_p=*/false,
18745 &friend_p);
18746 pop_deferring_access_checks ();
18748 /* If this is a member template declaration, let the front
18749 end know. */
18750 if (member_p && !friend_p && decl)
18752 if (TREE_CODE (decl) == TYPE_DECL)
18753 cp_parser_check_access_in_redeclaration (decl, token->location);
18755 decl = finish_member_template_decl (decl);
18757 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
18758 make_friend_class (current_class_type, TREE_TYPE (decl),
18759 /*complain=*/true);
18761 /* We are done with the current parameter list. */
18762 --parser->num_template_parameter_lists;
18764 pop_deferring_access_checks ();
18766 /* Finish up. */
18767 finish_template_decl (parameter_list);
18769 /* Register member declarations. */
18770 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
18771 finish_member_declaration (decl);
18772 /* For the erroneous case of a template with C linkage, we pushed an
18773 implicit C++ linkage scope; exit that scope now. */
18774 if (need_lang_pop)
18775 pop_lang_context ();
18776 /* If DECL is a function template, we must return to parse it later.
18777 (Even though there is no definition, there might be default
18778 arguments that need handling.) */
18779 if (member_p && decl
18780 && (TREE_CODE (decl) == FUNCTION_DECL
18781 || DECL_FUNCTION_TEMPLATE_P (decl)))
18782 TREE_VALUE (parser->unparsed_functions_queues)
18783 = tree_cons (NULL_TREE, decl,
18784 TREE_VALUE (parser->unparsed_functions_queues));
18787 /* Perform the deferred access checks from a template-parameter-list.
18788 CHECKS is a TREE_LIST of access checks, as returned by
18789 get_deferred_access_checks. */
18791 static void
18792 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
18794 ++processing_template_parmlist;
18795 perform_access_checks (checks);
18796 --processing_template_parmlist;
18799 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
18800 `function-definition' sequence. MEMBER_P is true, this declaration
18801 appears in a class scope.
18803 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
18804 *FRIEND_P is set to TRUE iff the declaration is a friend. */
18806 static tree
18807 cp_parser_single_declaration (cp_parser* parser,
18808 VEC (deferred_access_check,gc)* checks,
18809 bool member_p,
18810 bool explicit_specialization_p,
18811 bool* friend_p)
18813 int declares_class_or_enum;
18814 tree decl = NULL_TREE;
18815 cp_decl_specifier_seq decl_specifiers;
18816 bool function_definition_p = false;
18817 cp_token *decl_spec_token_start;
18819 /* This function is only used when processing a template
18820 declaration. */
18821 gcc_assert (innermost_scope_kind () == sk_template_parms
18822 || innermost_scope_kind () == sk_template_spec);
18824 /* Defer access checks until we know what is being declared. */
18825 push_deferring_access_checks (dk_deferred);
18827 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
18828 alternative. */
18829 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
18830 cp_parser_decl_specifier_seq (parser,
18831 CP_PARSER_FLAGS_OPTIONAL,
18832 &decl_specifiers,
18833 &declares_class_or_enum);
18834 if (friend_p)
18835 *friend_p = cp_parser_friend_p (&decl_specifiers);
18837 /* There are no template typedefs. */
18838 if (decl_specifiers.specs[(int) ds_typedef])
18840 error_at (decl_spec_token_start->location,
18841 "template declaration of %<typedef%>");
18842 decl = error_mark_node;
18845 /* Gather up the access checks that occurred the
18846 decl-specifier-seq. */
18847 stop_deferring_access_checks ();
18849 /* Check for the declaration of a template class. */
18850 if (declares_class_or_enum)
18852 if (cp_parser_declares_only_class_p (parser))
18854 decl = shadow_tag (&decl_specifiers);
18856 /* In this case:
18858 struct C {
18859 friend template <typename T> struct A<T>::B;
18862 A<T>::B will be represented by a TYPENAME_TYPE, and
18863 therefore not recognized by shadow_tag. */
18864 if (friend_p && *friend_p
18865 && !decl
18866 && decl_specifiers.type
18867 && TYPE_P (decl_specifiers.type))
18868 decl = decl_specifiers.type;
18870 if (decl && decl != error_mark_node)
18871 decl = TYPE_NAME (decl);
18872 else
18873 decl = error_mark_node;
18875 /* Perform access checks for template parameters. */
18876 cp_parser_perform_template_parameter_access_checks (checks);
18880 /* Complain about missing 'typename' or other invalid type names. */
18881 if (!decl_specifiers.any_type_specifiers_p)
18882 cp_parser_parse_and_diagnose_invalid_type_name (parser);
18884 /* If it's not a template class, try for a template function. If
18885 the next token is a `;', then this declaration does not declare
18886 anything. But, if there were errors in the decl-specifiers, then
18887 the error might well have come from an attempted class-specifier.
18888 In that case, there's no need to warn about a missing declarator. */
18889 if (!decl
18890 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
18891 || decl_specifiers.type != error_mark_node))
18893 decl = cp_parser_init_declarator (parser,
18894 &decl_specifiers,
18895 checks,
18896 /*function_definition_allowed_p=*/true,
18897 member_p,
18898 declares_class_or_enum,
18899 &function_definition_p);
18901 /* 7.1.1-1 [dcl.stc]
18903 A storage-class-specifier shall not be specified in an explicit
18904 specialization... */
18905 if (decl
18906 && explicit_specialization_p
18907 && decl_specifiers.storage_class != sc_none)
18909 error_at (decl_spec_token_start->location,
18910 "explicit template specialization cannot have a storage class");
18911 decl = error_mark_node;
18915 pop_deferring_access_checks ();
18917 /* Clear any current qualification; whatever comes next is the start
18918 of something new. */
18919 parser->scope = NULL_TREE;
18920 parser->qualifying_scope = NULL_TREE;
18921 parser->object_scope = NULL_TREE;
18922 /* Look for a trailing `;' after the declaration. */
18923 if (!function_definition_p
18924 && (decl == error_mark_node
18925 || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
18926 cp_parser_skip_to_end_of_block_or_statement (parser);
18928 return decl;
18931 /* Parse a cast-expression that is not the operand of a unary "&". */
18933 static tree
18934 cp_parser_simple_cast_expression (cp_parser *parser)
18936 return cp_parser_cast_expression (parser, /*address_p=*/false,
18937 /*cast_p=*/false, NULL);
18940 /* Parse a functional cast to TYPE. Returns an expression
18941 representing the cast. */
18943 static tree
18944 cp_parser_functional_cast (cp_parser* parser, tree type)
18946 VEC(tree,gc) *vec;
18947 tree expression_list;
18948 tree cast;
18949 bool nonconst_p;
18951 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18953 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
18954 expression_list = cp_parser_braced_list (parser, &nonconst_p);
18955 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
18956 if (TREE_CODE (type) == TYPE_DECL)
18957 type = TREE_TYPE (type);
18958 return finish_compound_literal (type, expression_list);
18962 vec = cp_parser_parenthesized_expression_list (parser, false,
18963 /*cast_p=*/true,
18964 /*allow_expansion_p=*/true,
18965 /*non_constant_p=*/NULL);
18966 if (vec == NULL)
18967 expression_list = error_mark_node;
18968 else
18970 expression_list = build_tree_list_vec (vec);
18971 release_tree_vector (vec);
18974 cast = build_functional_cast (type, expression_list,
18975 tf_warning_or_error);
18976 /* [expr.const]/1: In an integral constant expression "only type
18977 conversions to integral or enumeration type can be used". */
18978 if (TREE_CODE (type) == TYPE_DECL)
18979 type = TREE_TYPE (type);
18980 if (cast != error_mark_node
18981 && !cast_valid_in_integral_constant_expression_p (type)
18982 && (cp_parser_non_integral_constant_expression
18983 (parser, "a call to a constructor")))
18984 return error_mark_node;
18985 return cast;
18988 /* Save the tokens that make up the body of a member function defined
18989 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
18990 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
18991 specifiers applied to the declaration. Returns the FUNCTION_DECL
18992 for the member function. */
18994 static tree
18995 cp_parser_save_member_function_body (cp_parser* parser,
18996 cp_decl_specifier_seq *decl_specifiers,
18997 cp_declarator *declarator,
18998 tree attributes)
19000 cp_token *first;
19001 cp_token *last;
19002 tree fn;
19004 /* Create the FUNCTION_DECL. */
19005 fn = grokmethod (decl_specifiers, declarator, attributes);
19006 /* If something went badly wrong, bail out now. */
19007 if (fn == error_mark_node)
19009 /* If there's a function-body, skip it. */
19010 if (cp_parser_token_starts_function_definition_p
19011 (cp_lexer_peek_token (parser->lexer)))
19012 cp_parser_skip_to_end_of_block_or_statement (parser);
19013 return error_mark_node;
19016 /* Remember it, if there default args to post process. */
19017 cp_parser_save_default_args (parser, fn);
19019 /* Save away the tokens that make up the body of the
19020 function. */
19021 first = parser->lexer->next_token;
19022 /* We can have braced-init-list mem-initializers before the fn body. */
19023 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19025 cp_lexer_consume_token (parser->lexer);
19026 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
19027 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
19029 /* cache_group will stop after an un-nested { } pair, too. */
19030 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
19031 break;
19033 /* variadic mem-inits have ... after the ')'. */
19034 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19035 cp_lexer_consume_token (parser->lexer);
19038 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
19039 /* Handle function try blocks. */
19040 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
19041 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
19042 last = parser->lexer->next_token;
19044 /* Save away the inline definition; we will process it when the
19045 class is complete. */
19046 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
19047 DECL_PENDING_INLINE_P (fn) = 1;
19049 /* We need to know that this was defined in the class, so that
19050 friend templates are handled correctly. */
19051 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
19053 /* Add FN to the queue of functions to be parsed later. */
19054 TREE_VALUE (parser->unparsed_functions_queues)
19055 = tree_cons (NULL_TREE, fn,
19056 TREE_VALUE (parser->unparsed_functions_queues));
19058 return fn;
19061 /* Parse a template-argument-list, as well as the trailing ">" (but
19062 not the opening ">"). See cp_parser_template_argument_list for the
19063 return value. */
19065 static tree
19066 cp_parser_enclosed_template_argument_list (cp_parser* parser)
19068 tree arguments;
19069 tree saved_scope;
19070 tree saved_qualifying_scope;
19071 tree saved_object_scope;
19072 bool saved_greater_than_is_operator_p;
19073 int saved_unevaluated_operand;
19074 int saved_inhibit_evaluation_warnings;
19076 /* [temp.names]
19078 When parsing a template-id, the first non-nested `>' is taken as
19079 the end of the template-argument-list rather than a greater-than
19080 operator. */
19081 saved_greater_than_is_operator_p
19082 = parser->greater_than_is_operator_p;
19083 parser->greater_than_is_operator_p = false;
19084 /* Parsing the argument list may modify SCOPE, so we save it
19085 here. */
19086 saved_scope = parser->scope;
19087 saved_qualifying_scope = parser->qualifying_scope;
19088 saved_object_scope = parser->object_scope;
19089 /* We need to evaluate the template arguments, even though this
19090 template-id may be nested within a "sizeof". */
19091 saved_unevaluated_operand = cp_unevaluated_operand;
19092 cp_unevaluated_operand = 0;
19093 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
19094 c_inhibit_evaluation_warnings = 0;
19095 /* Parse the template-argument-list itself. */
19096 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
19097 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
19098 arguments = NULL_TREE;
19099 else
19100 arguments = cp_parser_template_argument_list (parser);
19101 /* Look for the `>' that ends the template-argument-list. If we find
19102 a '>>' instead, it's probably just a typo. */
19103 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
19105 if (cxx_dialect != cxx98)
19107 /* In C++0x, a `>>' in a template argument list or cast
19108 expression is considered to be two separate `>'
19109 tokens. So, change the current token to a `>', but don't
19110 consume it: it will be consumed later when the outer
19111 template argument list (or cast expression) is parsed.
19112 Note that this replacement of `>' for `>>' is necessary
19113 even if we are parsing tentatively: in the tentative
19114 case, after calling
19115 cp_parser_enclosed_template_argument_list we will always
19116 throw away all of the template arguments and the first
19117 closing `>', either because the template argument list
19118 was erroneous or because we are replacing those tokens
19119 with a CPP_TEMPLATE_ID token. The second `>' (which will
19120 not have been thrown away) is needed either to close an
19121 outer template argument list or to complete a new-style
19122 cast. */
19123 cp_token *token = cp_lexer_peek_token (parser->lexer);
19124 token->type = CPP_GREATER;
19126 else if (!saved_greater_than_is_operator_p)
19128 /* If we're in a nested template argument list, the '>>' has
19129 to be a typo for '> >'. We emit the error message, but we
19130 continue parsing and we push a '>' as next token, so that
19131 the argument list will be parsed correctly. Note that the
19132 global source location is still on the token before the
19133 '>>', so we need to say explicitly where we want it. */
19134 cp_token *token = cp_lexer_peek_token (parser->lexer);
19135 error_at (token->location, "%<>>%> should be %<> >%> "
19136 "within a nested template argument list");
19138 token->type = CPP_GREATER;
19140 else
19142 /* If this is not a nested template argument list, the '>>'
19143 is a typo for '>'. Emit an error message and continue.
19144 Same deal about the token location, but here we can get it
19145 right by consuming the '>>' before issuing the diagnostic. */
19146 cp_token *token = cp_lexer_consume_token (parser->lexer);
19147 error_at (token->location,
19148 "spurious %<>>%>, use %<>%> to terminate "
19149 "a template argument list");
19152 else
19153 cp_parser_skip_to_end_of_template_parameter_list (parser);
19154 /* The `>' token might be a greater-than operator again now. */
19155 parser->greater_than_is_operator_p
19156 = saved_greater_than_is_operator_p;
19157 /* Restore the SAVED_SCOPE. */
19158 parser->scope = saved_scope;
19159 parser->qualifying_scope = saved_qualifying_scope;
19160 parser->object_scope = saved_object_scope;
19161 cp_unevaluated_operand = saved_unevaluated_operand;
19162 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
19164 return arguments;
19167 /* MEMBER_FUNCTION is a member function, or a friend. If default
19168 arguments, or the body of the function have not yet been parsed,
19169 parse them now. */
19171 static void
19172 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
19174 /* If this member is a template, get the underlying
19175 FUNCTION_DECL. */
19176 if (DECL_FUNCTION_TEMPLATE_P (member_function))
19177 member_function = DECL_TEMPLATE_RESULT (member_function);
19179 /* There should not be any class definitions in progress at this
19180 point; the bodies of members are only parsed outside of all class
19181 definitions. */
19182 gcc_assert (parser->num_classes_being_defined == 0);
19183 /* While we're parsing the member functions we might encounter more
19184 classes. We want to handle them right away, but we don't want
19185 them getting mixed up with functions that are currently in the
19186 queue. */
19187 parser->unparsed_functions_queues
19188 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
19190 /* Make sure that any template parameters are in scope. */
19191 maybe_begin_member_template_processing (member_function);
19193 /* If the body of the function has not yet been parsed, parse it
19194 now. */
19195 if (DECL_PENDING_INLINE_P (member_function))
19197 tree function_scope;
19198 cp_token_cache *tokens;
19200 /* The function is no longer pending; we are processing it. */
19201 tokens = DECL_PENDING_INLINE_INFO (member_function);
19202 DECL_PENDING_INLINE_INFO (member_function) = NULL;
19203 DECL_PENDING_INLINE_P (member_function) = 0;
19205 /* If this is a local class, enter the scope of the containing
19206 function. */
19207 function_scope = current_function_decl;
19208 if (function_scope)
19209 push_function_context ();
19211 /* Push the body of the function onto the lexer stack. */
19212 cp_parser_push_lexer_for_tokens (parser, tokens);
19214 /* Let the front end know that we going to be defining this
19215 function. */
19216 start_preparsed_function (member_function, NULL_TREE,
19217 SF_PRE_PARSED | SF_INCLASS_INLINE);
19219 /* Don't do access checking if it is a templated function. */
19220 if (processing_template_decl)
19221 push_deferring_access_checks (dk_no_check);
19223 /* Now, parse the body of the function. */
19224 cp_parser_function_definition_after_declarator (parser,
19225 /*inline_p=*/true);
19227 if (processing_template_decl)
19228 pop_deferring_access_checks ();
19230 /* Leave the scope of the containing function. */
19231 if (function_scope)
19232 pop_function_context ();
19233 cp_parser_pop_lexer (parser);
19236 /* Remove any template parameters from the symbol table. */
19237 maybe_end_member_template_processing ();
19239 /* Restore the queue. */
19240 parser->unparsed_functions_queues
19241 = TREE_CHAIN (parser->unparsed_functions_queues);
19244 /* If DECL contains any default args, remember it on the unparsed
19245 functions queue. */
19247 static void
19248 cp_parser_save_default_args (cp_parser* parser, tree decl)
19250 tree probe;
19252 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
19253 probe;
19254 probe = TREE_CHAIN (probe))
19255 if (TREE_PURPOSE (probe))
19257 TREE_PURPOSE (parser->unparsed_functions_queues)
19258 = tree_cons (current_class_type, decl,
19259 TREE_PURPOSE (parser->unparsed_functions_queues));
19260 break;
19264 /* FN is a FUNCTION_DECL which may contains a parameter with an
19265 unparsed DEFAULT_ARG. Parse the default args now. This function
19266 assumes that the current scope is the scope in which the default
19267 argument should be processed. */
19269 static void
19270 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
19272 bool saved_local_variables_forbidden_p;
19273 tree parm, parmdecl;
19275 /* While we're parsing the default args, we might (due to the
19276 statement expression extension) encounter more classes. We want
19277 to handle them right away, but we don't want them getting mixed
19278 up with default args that are currently in the queue. */
19279 parser->unparsed_functions_queues
19280 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
19282 /* Local variable names (and the `this' keyword) may not appear
19283 in a default argument. */
19284 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
19285 parser->local_variables_forbidden_p = true;
19287 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
19288 parmdecl = DECL_ARGUMENTS (fn);
19289 parm && parm != void_list_node;
19290 parm = TREE_CHAIN (parm),
19291 parmdecl = TREE_CHAIN (parmdecl))
19293 cp_token_cache *tokens;
19294 tree default_arg = TREE_PURPOSE (parm);
19295 tree parsed_arg;
19296 VEC(tree,gc) *insts;
19297 tree copy;
19298 unsigned ix;
19300 if (!default_arg)
19301 continue;
19303 if (TREE_CODE (default_arg) != DEFAULT_ARG)
19304 /* This can happen for a friend declaration for a function
19305 already declared with default arguments. */
19306 continue;
19308 /* Push the saved tokens for the default argument onto the parser's
19309 lexer stack. */
19310 tokens = DEFARG_TOKENS (default_arg);
19311 cp_parser_push_lexer_for_tokens (parser, tokens);
19313 start_lambda_scope (parmdecl);
19315 /* Parse the assignment-expression. */
19316 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
19317 if (parsed_arg == error_mark_node)
19319 cp_parser_pop_lexer (parser);
19320 continue;
19323 if (!processing_template_decl)
19324 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
19326 TREE_PURPOSE (parm) = parsed_arg;
19328 /* Update any instantiations we've already created. */
19329 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
19330 VEC_iterate (tree, insts, ix, copy); ix++)
19331 TREE_PURPOSE (copy) = parsed_arg;
19333 finish_lambda_scope ();
19335 /* If the token stream has not been completely used up, then
19336 there was extra junk after the end of the default
19337 argument. */
19338 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
19339 cp_parser_error (parser, "expected %<,%>");
19341 /* Revert to the main lexer. */
19342 cp_parser_pop_lexer (parser);
19345 /* Make sure no default arg is missing. */
19346 check_default_args (fn);
19348 /* Restore the state of local_variables_forbidden_p. */
19349 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
19351 /* Restore the queue. */
19352 parser->unparsed_functions_queues
19353 = TREE_CHAIN (parser->unparsed_functions_queues);
19356 /* Parse the operand of `sizeof' (or a similar operator). Returns
19357 either a TYPE or an expression, depending on the form of the
19358 input. The KEYWORD indicates which kind of expression we have
19359 encountered. */
19361 static tree
19362 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
19364 tree expr = NULL_TREE;
19365 const char *saved_message;
19366 char *tmp;
19367 bool saved_integral_constant_expression_p;
19368 bool saved_non_integral_constant_expression_p;
19369 bool pack_expansion_p = false;
19371 /* Types cannot be defined in a `sizeof' expression. Save away the
19372 old message. */
19373 saved_message = parser->type_definition_forbidden_message;
19374 /* And create the new one. */
19375 tmp = concat ("types may not be defined in %<",
19376 IDENTIFIER_POINTER (ridpointers[keyword]),
19377 "%> expressions", NULL);
19378 parser->type_definition_forbidden_message = tmp;
19380 /* The restrictions on constant-expressions do not apply inside
19381 sizeof expressions. */
19382 saved_integral_constant_expression_p
19383 = parser->integral_constant_expression_p;
19384 saved_non_integral_constant_expression_p
19385 = parser->non_integral_constant_expression_p;
19386 parser->integral_constant_expression_p = false;
19388 /* If it's a `...', then we are computing the length of a parameter
19389 pack. */
19390 if (keyword == RID_SIZEOF
19391 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19393 /* Consume the `...'. */
19394 cp_lexer_consume_token (parser->lexer);
19395 maybe_warn_variadic_templates ();
19397 /* Note that this is an expansion. */
19398 pack_expansion_p = true;
19401 /* Do not actually evaluate the expression. */
19402 ++cp_unevaluated_operand;
19403 ++c_inhibit_evaluation_warnings;
19404 /* If it's a `(', then we might be looking at the type-id
19405 construction. */
19406 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19408 tree type;
19409 bool saved_in_type_id_in_expr_p;
19411 /* We can't be sure yet whether we're looking at a type-id or an
19412 expression. */
19413 cp_parser_parse_tentatively (parser);
19414 /* Consume the `('. */
19415 cp_lexer_consume_token (parser->lexer);
19416 /* Parse the type-id. */
19417 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
19418 parser->in_type_id_in_expr_p = true;
19419 type = cp_parser_type_id (parser);
19420 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
19421 /* Now, look for the trailing `)'. */
19422 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19423 /* If all went well, then we're done. */
19424 if (cp_parser_parse_definitely (parser))
19426 cp_decl_specifier_seq decl_specs;
19428 /* Build a trivial decl-specifier-seq. */
19429 clear_decl_specs (&decl_specs);
19430 decl_specs.type = type;
19432 /* Call grokdeclarator to figure out what type this is. */
19433 expr = grokdeclarator (NULL,
19434 &decl_specs,
19435 TYPENAME,
19436 /*initialized=*/0,
19437 /*attrlist=*/NULL);
19441 /* If the type-id production did not work out, then we must be
19442 looking at the unary-expression production. */
19443 if (!expr)
19444 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
19445 /*cast_p=*/false, NULL);
19447 if (pack_expansion_p)
19448 /* Build a pack expansion. */
19449 expr = make_pack_expansion (expr);
19451 /* Go back to evaluating expressions. */
19452 --cp_unevaluated_operand;
19453 --c_inhibit_evaluation_warnings;
19455 /* Free the message we created. */
19456 free (tmp);
19457 /* And restore the old one. */
19458 parser->type_definition_forbidden_message = saved_message;
19459 parser->integral_constant_expression_p
19460 = saved_integral_constant_expression_p;
19461 parser->non_integral_constant_expression_p
19462 = saved_non_integral_constant_expression_p;
19464 return expr;
19467 /* If the current declaration has no declarator, return true. */
19469 static bool
19470 cp_parser_declares_only_class_p (cp_parser *parser)
19472 /* If the next token is a `;' or a `,' then there is no
19473 declarator. */
19474 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
19475 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
19478 /* Update the DECL_SPECS to reflect the storage class indicated by
19479 KEYWORD. */
19481 static void
19482 cp_parser_set_storage_class (cp_parser *parser,
19483 cp_decl_specifier_seq *decl_specs,
19484 enum rid keyword,
19485 location_t location)
19487 cp_storage_class storage_class;
19489 if (parser->in_unbraced_linkage_specification_p)
19491 error_at (location, "invalid use of %qD in linkage specification",
19492 ridpointers[keyword]);
19493 return;
19495 else if (decl_specs->storage_class != sc_none)
19497 decl_specs->conflicting_specifiers_p = true;
19498 return;
19501 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
19502 && decl_specs->specs[(int) ds_thread])
19504 error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
19505 decl_specs->specs[(int) ds_thread] = 0;
19508 switch (keyword)
19510 case RID_AUTO:
19511 storage_class = sc_auto;
19512 break;
19513 case RID_REGISTER:
19514 storage_class = sc_register;
19515 break;
19516 case RID_STATIC:
19517 storage_class = sc_static;
19518 break;
19519 case RID_EXTERN:
19520 storage_class = sc_extern;
19521 break;
19522 case RID_MUTABLE:
19523 storage_class = sc_mutable;
19524 break;
19525 default:
19526 gcc_unreachable ();
19528 decl_specs->storage_class = storage_class;
19530 /* A storage class specifier cannot be applied alongside a typedef
19531 specifier. If there is a typedef specifier present then set
19532 conflicting_specifiers_p which will trigger an error later
19533 on in grokdeclarator. */
19534 if (decl_specs->specs[(int)ds_typedef])
19535 decl_specs->conflicting_specifiers_p = true;
19538 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
19539 is true, the type is a user-defined type; otherwise it is a
19540 built-in type specified by a keyword. */
19542 static void
19543 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
19544 tree type_spec,
19545 location_t location,
19546 bool user_defined_p)
19548 decl_specs->any_specifiers_p = true;
19550 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
19551 (with, for example, in "typedef int wchar_t;") we remember that
19552 this is what happened. In system headers, we ignore these
19553 declarations so that G++ can work with system headers that are not
19554 C++-safe. */
19555 if (decl_specs->specs[(int) ds_typedef]
19556 && !user_defined_p
19557 && (type_spec == boolean_type_node
19558 || type_spec == char16_type_node
19559 || type_spec == char32_type_node
19560 || type_spec == wchar_type_node)
19561 && (decl_specs->type
19562 || decl_specs->specs[(int) ds_long]
19563 || decl_specs->specs[(int) ds_short]
19564 || decl_specs->specs[(int) ds_unsigned]
19565 || decl_specs->specs[(int) ds_signed]))
19567 decl_specs->redefined_builtin_type = type_spec;
19568 if (!decl_specs->type)
19570 decl_specs->type = type_spec;
19571 decl_specs->user_defined_type_p = false;
19572 decl_specs->type_location = location;
19575 else if (decl_specs->type)
19576 decl_specs->multiple_types_p = true;
19577 else
19579 decl_specs->type = type_spec;
19580 decl_specs->user_defined_type_p = user_defined_p;
19581 decl_specs->redefined_builtin_type = NULL_TREE;
19582 decl_specs->type_location = location;
19586 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
19587 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
19589 static bool
19590 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
19592 return decl_specifiers->specs[(int) ds_friend] != 0;
19595 /* If the next token is of the indicated TYPE, consume it. Otherwise,
19596 issue an error message indicating that TOKEN_DESC was expected.
19598 Returns the token consumed, if the token had the appropriate type.
19599 Otherwise, returns NULL. */
19601 static cp_token *
19602 cp_parser_require (cp_parser* parser,
19603 enum cpp_ttype type,
19604 const char* token_desc)
19606 if (cp_lexer_next_token_is (parser->lexer, type))
19607 return cp_lexer_consume_token (parser->lexer);
19608 else
19610 /* Output the MESSAGE -- unless we're parsing tentatively. */
19611 if (!cp_parser_simulate_error (parser))
19613 char *message = concat ("expected ", token_desc, NULL);
19614 cp_parser_error (parser, message);
19615 free (message);
19617 return NULL;
19621 /* An error message is produced if the next token is not '>'.
19622 All further tokens are skipped until the desired token is
19623 found or '{', '}', ';' or an unbalanced ')' or ']'. */
19625 static void
19626 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
19628 /* Current level of '< ... >'. */
19629 unsigned level = 0;
19630 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
19631 unsigned nesting_depth = 0;
19633 /* Are we ready, yet? If not, issue error message. */
19634 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
19635 return;
19637 /* Skip tokens until the desired token is found. */
19638 while (true)
19640 /* Peek at the next token. */
19641 switch (cp_lexer_peek_token (parser->lexer)->type)
19643 case CPP_LESS:
19644 if (!nesting_depth)
19645 ++level;
19646 break;
19648 case CPP_RSHIFT:
19649 if (cxx_dialect == cxx98)
19650 /* C++0x views the `>>' operator as two `>' tokens, but
19651 C++98 does not. */
19652 break;
19653 else if (!nesting_depth && level-- == 0)
19655 /* We've hit a `>>' where the first `>' closes the
19656 template argument list, and the second `>' is
19657 spurious. Just consume the `>>' and stop; we've
19658 already produced at least one error. */
19659 cp_lexer_consume_token (parser->lexer);
19660 return;
19662 /* Fall through for C++0x, so we handle the second `>' in
19663 the `>>'. */
19665 case CPP_GREATER:
19666 if (!nesting_depth && level-- == 0)
19668 /* We've reached the token we want, consume it and stop. */
19669 cp_lexer_consume_token (parser->lexer);
19670 return;
19672 break;
19674 case CPP_OPEN_PAREN:
19675 case CPP_OPEN_SQUARE:
19676 ++nesting_depth;
19677 break;
19679 case CPP_CLOSE_PAREN:
19680 case CPP_CLOSE_SQUARE:
19681 if (nesting_depth-- == 0)
19682 return;
19683 break;
19685 case CPP_EOF:
19686 case CPP_PRAGMA_EOL:
19687 case CPP_SEMICOLON:
19688 case CPP_OPEN_BRACE:
19689 case CPP_CLOSE_BRACE:
19690 /* The '>' was probably forgotten, don't look further. */
19691 return;
19693 default:
19694 break;
19697 /* Consume this token. */
19698 cp_lexer_consume_token (parser->lexer);
19702 /* If the next token is the indicated keyword, consume it. Otherwise,
19703 issue an error message indicating that TOKEN_DESC was expected.
19705 Returns the token consumed, if the token had the appropriate type.
19706 Otherwise, returns NULL. */
19708 static cp_token *
19709 cp_parser_require_keyword (cp_parser* parser,
19710 enum rid keyword,
19711 const char* token_desc)
19713 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
19715 if (token && token->keyword != keyword)
19717 dyn_string_t error_msg;
19719 /* Format the error message. */
19720 error_msg = dyn_string_new (0);
19721 dyn_string_append_cstr (error_msg, "expected ");
19722 dyn_string_append_cstr (error_msg, token_desc);
19723 cp_parser_error (parser, error_msg->s);
19724 dyn_string_delete (error_msg);
19725 return NULL;
19728 return token;
19731 /* Returns TRUE iff TOKEN is a token that can begin the body of a
19732 function-definition. */
19734 static bool
19735 cp_parser_token_starts_function_definition_p (cp_token* token)
19737 return (/* An ordinary function-body begins with an `{'. */
19738 token->type == CPP_OPEN_BRACE
19739 /* A ctor-initializer begins with a `:'. */
19740 || token->type == CPP_COLON
19741 /* A function-try-block begins with `try'. */
19742 || token->keyword == RID_TRY
19743 /* The named return value extension begins with `return'. */
19744 || token->keyword == RID_RETURN);
19747 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
19748 definition. */
19750 static bool
19751 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
19753 cp_token *token;
19755 token = cp_lexer_peek_token (parser->lexer);
19756 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
19759 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
19760 C++0x) ending a template-argument. */
19762 static bool
19763 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
19765 cp_token *token;
19767 token = cp_lexer_peek_token (parser->lexer);
19768 return (token->type == CPP_COMMA
19769 || token->type == CPP_GREATER
19770 || token->type == CPP_ELLIPSIS
19771 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
19774 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
19775 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
19777 static bool
19778 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
19779 size_t n)
19781 cp_token *token;
19783 token = cp_lexer_peek_nth_token (parser->lexer, n);
19784 if (token->type == CPP_LESS)
19785 return true;
19786 /* Check for the sequence `<::' in the original code. It would be lexed as
19787 `[:', where `[' is a digraph, and there is no whitespace before
19788 `:'. */
19789 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
19791 cp_token *token2;
19792 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
19793 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
19794 return true;
19796 return false;
19799 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
19800 or none_type otherwise. */
19802 static enum tag_types
19803 cp_parser_token_is_class_key (cp_token* token)
19805 switch (token->keyword)
19807 case RID_CLASS:
19808 return class_type;
19809 case RID_STRUCT:
19810 return record_type;
19811 case RID_UNION:
19812 return union_type;
19814 default:
19815 return none_type;
19819 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
19821 static void
19822 cp_parser_check_class_key (enum tag_types class_key, tree type)
19824 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
19825 permerror (input_location, "%qs tag used in naming %q#T",
19826 class_key == union_type ? "union"
19827 : class_key == record_type ? "struct" : "class",
19828 type);
19831 /* Issue an error message if DECL is redeclared with different
19832 access than its original declaration [class.access.spec/3].
19833 This applies to nested classes and nested class templates.
19834 [class.mem/1]. */
19836 static void
19837 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
19839 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
19840 return;
19842 if ((TREE_PRIVATE (decl)
19843 != (current_access_specifier == access_private_node))
19844 || (TREE_PROTECTED (decl)
19845 != (current_access_specifier == access_protected_node)))
19846 error_at (location, "%qD redeclared with different access", decl);
19849 /* Look for the `template' keyword, as a syntactic disambiguator.
19850 Return TRUE iff it is present, in which case it will be
19851 consumed. */
19853 static bool
19854 cp_parser_optional_template_keyword (cp_parser *parser)
19856 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
19858 /* The `template' keyword can only be used within templates;
19859 outside templates the parser can always figure out what is a
19860 template and what is not. */
19861 if (!processing_template_decl)
19863 cp_token *token = cp_lexer_peek_token (parser->lexer);
19864 error_at (token->location,
19865 "%<template%> (as a disambiguator) is only allowed "
19866 "within templates");
19867 /* If this part of the token stream is rescanned, the same
19868 error message would be generated. So, we purge the token
19869 from the stream. */
19870 cp_lexer_purge_token (parser->lexer);
19871 return false;
19873 else
19875 /* Consume the `template' keyword. */
19876 cp_lexer_consume_token (parser->lexer);
19877 return true;
19881 return false;
19884 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
19885 set PARSER->SCOPE, and perform other related actions. */
19887 static void
19888 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
19890 int i;
19891 struct tree_check *check_value;
19892 deferred_access_check *chk;
19893 VEC (deferred_access_check,gc) *checks;
19895 /* Get the stored value. */
19896 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
19897 /* Perform any access checks that were deferred. */
19898 checks = check_value->checks;
19899 if (checks)
19901 for (i = 0 ;
19902 VEC_iterate (deferred_access_check, checks, i, chk) ;
19903 ++i)
19905 perform_or_defer_access_check (chk->binfo,
19906 chk->decl,
19907 chk->diag_decl);
19910 /* Set the scope from the stored value. */
19911 parser->scope = check_value->value;
19912 parser->qualifying_scope = check_value->qualifying_scope;
19913 parser->object_scope = NULL_TREE;
19916 /* Consume tokens up through a non-nested END token. Returns TRUE if we
19917 encounter the end of a block before what we were looking for. */
19919 static bool
19920 cp_parser_cache_group (cp_parser *parser,
19921 enum cpp_ttype end,
19922 unsigned depth)
19924 while (true)
19926 cp_token *token = cp_lexer_peek_token (parser->lexer);
19928 /* Abort a parenthesized expression if we encounter a semicolon. */
19929 if ((end == CPP_CLOSE_PAREN || depth == 0)
19930 && token->type == CPP_SEMICOLON)
19931 return true;
19932 /* If we've reached the end of the file, stop. */
19933 if (token->type == CPP_EOF
19934 || (end != CPP_PRAGMA_EOL
19935 && token->type == CPP_PRAGMA_EOL))
19936 return true;
19937 if (token->type == CPP_CLOSE_BRACE && depth == 0)
19938 /* We've hit the end of an enclosing block, so there's been some
19939 kind of syntax error. */
19940 return true;
19942 /* Consume the token. */
19943 cp_lexer_consume_token (parser->lexer);
19944 /* See if it starts a new group. */
19945 if (token->type == CPP_OPEN_BRACE)
19947 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
19948 /* In theory this should probably check end == '}', but
19949 cp_parser_save_member_function_body needs it to exit
19950 after either '}' or ')' when called with ')'. */
19951 if (depth == 0)
19952 return false;
19954 else if (token->type == CPP_OPEN_PAREN)
19956 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
19957 if (depth == 0 && end == CPP_CLOSE_PAREN)
19958 return false;
19960 else if (token->type == CPP_PRAGMA)
19961 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
19962 else if (token->type == end)
19963 return false;
19967 /* Begin parsing tentatively. We always save tokens while parsing
19968 tentatively so that if the tentative parsing fails we can restore the
19969 tokens. */
19971 static void
19972 cp_parser_parse_tentatively (cp_parser* parser)
19974 /* Enter a new parsing context. */
19975 parser->context = cp_parser_context_new (parser->context);
19976 /* Begin saving tokens. */
19977 cp_lexer_save_tokens (parser->lexer);
19978 /* In order to avoid repetitive access control error messages,
19979 access checks are queued up until we are no longer parsing
19980 tentatively. */
19981 push_deferring_access_checks (dk_deferred);
19984 /* Commit to the currently active tentative parse. */
19986 static void
19987 cp_parser_commit_to_tentative_parse (cp_parser* parser)
19989 cp_parser_context *context;
19990 cp_lexer *lexer;
19992 /* Mark all of the levels as committed. */
19993 lexer = parser->lexer;
19994 for (context = parser->context; context->next; context = context->next)
19996 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
19997 break;
19998 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
19999 while (!cp_lexer_saving_tokens (lexer))
20000 lexer = lexer->next;
20001 cp_lexer_commit_tokens (lexer);
20005 /* Abort the currently active tentative parse. All consumed tokens
20006 will be rolled back, and no diagnostics will be issued. */
20008 static void
20009 cp_parser_abort_tentative_parse (cp_parser* parser)
20011 cp_parser_simulate_error (parser);
20012 /* Now, pretend that we want to see if the construct was
20013 successfully parsed. */
20014 cp_parser_parse_definitely (parser);
20017 /* Stop parsing tentatively. If a parse error has occurred, restore the
20018 token stream. Otherwise, commit to the tokens we have consumed.
20019 Returns true if no error occurred; false otherwise. */
20021 static bool
20022 cp_parser_parse_definitely (cp_parser* parser)
20024 bool error_occurred;
20025 cp_parser_context *context;
20027 /* Remember whether or not an error occurred, since we are about to
20028 destroy that information. */
20029 error_occurred = cp_parser_error_occurred (parser);
20030 /* Remove the topmost context from the stack. */
20031 context = parser->context;
20032 parser->context = context->next;
20033 /* If no parse errors occurred, commit to the tentative parse. */
20034 if (!error_occurred)
20036 /* Commit to the tokens read tentatively, unless that was
20037 already done. */
20038 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
20039 cp_lexer_commit_tokens (parser->lexer);
20041 pop_to_parent_deferring_access_checks ();
20043 /* Otherwise, if errors occurred, roll back our state so that things
20044 are just as they were before we began the tentative parse. */
20045 else
20047 cp_lexer_rollback_tokens (parser->lexer);
20048 pop_deferring_access_checks ();
20050 /* Add the context to the front of the free list. */
20051 context->next = cp_parser_context_free_list;
20052 cp_parser_context_free_list = context;
20054 return !error_occurred;
20057 /* Returns true if we are parsing tentatively and are not committed to
20058 this tentative parse. */
20060 static bool
20061 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
20063 return (cp_parser_parsing_tentatively (parser)
20064 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
20067 /* Returns nonzero iff an error has occurred during the most recent
20068 tentative parse. */
20070 static bool
20071 cp_parser_error_occurred (cp_parser* parser)
20073 return (cp_parser_parsing_tentatively (parser)
20074 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
20077 /* Returns nonzero if GNU extensions are allowed. */
20079 static bool
20080 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
20082 return parser->allow_gnu_extensions_p;
20085 /* Objective-C++ Productions */
20088 /* Parse an Objective-C expression, which feeds into a primary-expression
20089 above.
20091 objc-expression:
20092 objc-message-expression
20093 objc-string-literal
20094 objc-encode-expression
20095 objc-protocol-expression
20096 objc-selector-expression
20098 Returns a tree representation of the expression. */
20100 static tree
20101 cp_parser_objc_expression (cp_parser* parser)
20103 /* Try to figure out what kind of declaration is present. */
20104 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20106 switch (kwd->type)
20108 case CPP_OPEN_SQUARE:
20109 return cp_parser_objc_message_expression (parser);
20111 case CPP_OBJC_STRING:
20112 kwd = cp_lexer_consume_token (parser->lexer);
20113 return objc_build_string_object (kwd->u.value);
20115 case CPP_KEYWORD:
20116 switch (kwd->keyword)
20118 case RID_AT_ENCODE:
20119 return cp_parser_objc_encode_expression (parser);
20121 case RID_AT_PROTOCOL:
20122 return cp_parser_objc_protocol_expression (parser);
20124 case RID_AT_SELECTOR:
20125 return cp_parser_objc_selector_expression (parser);
20127 default:
20128 break;
20130 default:
20131 error_at (kwd->location,
20132 "misplaced %<@%D%> Objective-C++ construct",
20133 kwd->u.value);
20134 cp_parser_skip_to_end_of_block_or_statement (parser);
20137 return error_mark_node;
20140 /* Parse an Objective-C message expression.
20142 objc-message-expression:
20143 [ objc-message-receiver objc-message-args ]
20145 Returns a representation of an Objective-C message. */
20147 static tree
20148 cp_parser_objc_message_expression (cp_parser* parser)
20150 tree receiver, messageargs;
20152 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
20153 receiver = cp_parser_objc_message_receiver (parser);
20154 messageargs = cp_parser_objc_message_args (parser);
20155 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
20157 return objc_build_message_expr (build_tree_list (receiver, messageargs));
20160 /* Parse an objc-message-receiver.
20162 objc-message-receiver:
20163 expression
20164 simple-type-specifier
20166 Returns a representation of the type or expression. */
20168 static tree
20169 cp_parser_objc_message_receiver (cp_parser* parser)
20171 tree rcv;
20173 /* An Objective-C message receiver may be either (1) a type
20174 or (2) an expression. */
20175 cp_parser_parse_tentatively (parser);
20176 rcv = cp_parser_expression (parser, false, NULL);
20178 if (cp_parser_parse_definitely (parser))
20179 return rcv;
20181 rcv = cp_parser_simple_type_specifier (parser,
20182 /*decl_specs=*/NULL,
20183 CP_PARSER_FLAGS_NONE);
20185 return objc_get_class_reference (rcv);
20188 /* Parse the arguments and selectors comprising an Objective-C message.
20190 objc-message-args:
20191 objc-selector
20192 objc-selector-args
20193 objc-selector-args , objc-comma-args
20195 objc-selector-args:
20196 objc-selector [opt] : assignment-expression
20197 objc-selector-args objc-selector [opt] : assignment-expression
20199 objc-comma-args:
20200 assignment-expression
20201 objc-comma-args , assignment-expression
20203 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
20204 selector arguments and TREE_VALUE containing a list of comma
20205 arguments. */
20207 static tree
20208 cp_parser_objc_message_args (cp_parser* parser)
20210 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
20211 bool maybe_unary_selector_p = true;
20212 cp_token *token = cp_lexer_peek_token (parser->lexer);
20214 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
20216 tree selector = NULL_TREE, arg;
20218 if (token->type != CPP_COLON)
20219 selector = cp_parser_objc_selector (parser);
20221 /* Detect if we have a unary selector. */
20222 if (maybe_unary_selector_p
20223 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
20224 return build_tree_list (selector, NULL_TREE);
20226 maybe_unary_selector_p = false;
20227 cp_parser_require (parser, CPP_COLON, "%<:%>");
20228 arg = cp_parser_assignment_expression (parser, false, NULL);
20230 sel_args
20231 = chainon (sel_args,
20232 build_tree_list (selector, arg));
20234 token = cp_lexer_peek_token (parser->lexer);
20237 /* Handle non-selector arguments, if any. */
20238 while (token->type == CPP_COMMA)
20240 tree arg;
20242 cp_lexer_consume_token (parser->lexer);
20243 arg = cp_parser_assignment_expression (parser, false, NULL);
20245 addl_args
20246 = chainon (addl_args,
20247 build_tree_list (NULL_TREE, arg));
20249 token = cp_lexer_peek_token (parser->lexer);
20252 return build_tree_list (sel_args, addl_args);
20255 /* Parse an Objective-C encode expression.
20257 objc-encode-expression:
20258 @encode objc-typename
20260 Returns an encoded representation of the type argument. */
20262 static tree
20263 cp_parser_objc_encode_expression (cp_parser* parser)
20265 tree type;
20266 cp_token *token;
20268 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
20269 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20270 token = cp_lexer_peek_token (parser->lexer);
20271 type = complete_type (cp_parser_type_id (parser));
20272 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20274 if (!type)
20276 error_at (token->location,
20277 "%<@encode%> must specify a type as an argument");
20278 return error_mark_node;
20281 return objc_build_encode_expr (type);
20284 /* Parse an Objective-C @defs expression. */
20286 static tree
20287 cp_parser_objc_defs_expression (cp_parser *parser)
20289 tree name;
20291 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
20292 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20293 name = cp_parser_identifier (parser);
20294 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20296 return objc_get_class_ivars (name);
20299 /* Parse an Objective-C protocol expression.
20301 objc-protocol-expression:
20302 @protocol ( identifier )
20304 Returns a representation of the protocol expression. */
20306 static tree
20307 cp_parser_objc_protocol_expression (cp_parser* parser)
20309 tree proto;
20311 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
20312 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20313 proto = cp_parser_identifier (parser);
20314 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20316 return objc_build_protocol_expr (proto);
20319 /* Parse an Objective-C selector expression.
20321 objc-selector-expression:
20322 @selector ( objc-method-signature )
20324 objc-method-signature:
20325 objc-selector
20326 objc-selector-seq
20328 objc-selector-seq:
20329 objc-selector :
20330 objc-selector-seq objc-selector :
20332 Returns a representation of the method selector. */
20334 static tree
20335 cp_parser_objc_selector_expression (cp_parser* parser)
20337 tree sel_seq = NULL_TREE;
20338 bool maybe_unary_selector_p = true;
20339 cp_token *token;
20340 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
20342 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
20343 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20344 token = cp_lexer_peek_token (parser->lexer);
20346 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
20347 || token->type == CPP_SCOPE)
20349 tree selector = NULL_TREE;
20351 if (token->type != CPP_COLON
20352 || token->type == CPP_SCOPE)
20353 selector = cp_parser_objc_selector (parser);
20355 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
20356 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
20358 /* Detect if we have a unary selector. */
20359 if (maybe_unary_selector_p)
20361 sel_seq = selector;
20362 goto finish_selector;
20364 else
20366 cp_parser_error (parser, "expected %<:%>");
20369 maybe_unary_selector_p = false;
20370 token = cp_lexer_consume_token (parser->lexer);
20372 if (token->type == CPP_SCOPE)
20374 sel_seq
20375 = chainon (sel_seq,
20376 build_tree_list (selector, NULL_TREE));
20377 sel_seq
20378 = chainon (sel_seq,
20379 build_tree_list (NULL_TREE, NULL_TREE));
20381 else
20382 sel_seq
20383 = chainon (sel_seq,
20384 build_tree_list (selector, NULL_TREE));
20386 token = cp_lexer_peek_token (parser->lexer);
20389 finish_selector:
20390 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20392 return objc_build_selector_expr (loc, sel_seq);
20395 /* Parse a list of identifiers.
20397 objc-identifier-list:
20398 identifier
20399 objc-identifier-list , identifier
20401 Returns a TREE_LIST of identifier nodes. */
20403 static tree
20404 cp_parser_objc_identifier_list (cp_parser* parser)
20406 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
20407 cp_token *sep = cp_lexer_peek_token (parser->lexer);
20409 while (sep->type == CPP_COMMA)
20411 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
20412 list = chainon (list,
20413 build_tree_list (NULL_TREE,
20414 cp_parser_identifier (parser)));
20415 sep = cp_lexer_peek_token (parser->lexer);
20418 return list;
20421 /* Parse an Objective-C alias declaration.
20423 objc-alias-declaration:
20424 @compatibility_alias identifier identifier ;
20426 This function registers the alias mapping with the Objective-C front end.
20427 It returns nothing. */
20429 static void
20430 cp_parser_objc_alias_declaration (cp_parser* parser)
20432 tree alias, orig;
20434 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
20435 alias = cp_parser_identifier (parser);
20436 orig = cp_parser_identifier (parser);
20437 objc_declare_alias (alias, orig);
20438 cp_parser_consume_semicolon_at_end_of_statement (parser);
20441 /* Parse an Objective-C class forward-declaration.
20443 objc-class-declaration:
20444 @class objc-identifier-list ;
20446 The function registers the forward declarations with the Objective-C
20447 front end. It returns nothing. */
20449 static void
20450 cp_parser_objc_class_declaration (cp_parser* parser)
20452 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
20453 objc_declare_class (cp_parser_objc_identifier_list (parser));
20454 cp_parser_consume_semicolon_at_end_of_statement (parser);
20457 /* Parse a list of Objective-C protocol references.
20459 objc-protocol-refs-opt:
20460 objc-protocol-refs [opt]
20462 objc-protocol-refs:
20463 < objc-identifier-list >
20465 Returns a TREE_LIST of identifiers, if any. */
20467 static tree
20468 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
20470 tree protorefs = NULL_TREE;
20472 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
20474 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
20475 protorefs = cp_parser_objc_identifier_list (parser);
20476 cp_parser_require (parser, CPP_GREATER, "%<>%>");
20479 return protorefs;
20482 /* Parse a Objective-C visibility specification. */
20484 static void
20485 cp_parser_objc_visibility_spec (cp_parser* parser)
20487 cp_token *vis = cp_lexer_peek_token (parser->lexer);
20489 switch (vis->keyword)
20491 case RID_AT_PRIVATE:
20492 objc_set_visibility (2);
20493 break;
20494 case RID_AT_PROTECTED:
20495 objc_set_visibility (0);
20496 break;
20497 case RID_AT_PUBLIC:
20498 objc_set_visibility (1);
20499 break;
20500 default:
20501 return;
20504 /* Eat '@private'/'@protected'/'@public'. */
20505 cp_lexer_consume_token (parser->lexer);
20508 /* Parse an Objective-C method type. */
20510 static void
20511 cp_parser_objc_method_type (cp_parser* parser)
20513 objc_set_method_type
20514 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
20515 ? PLUS_EXPR
20516 : MINUS_EXPR);
20519 /* Parse an Objective-C protocol qualifier. */
20521 static tree
20522 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
20524 tree quals = NULL_TREE, node;
20525 cp_token *token = cp_lexer_peek_token (parser->lexer);
20527 node = token->u.value;
20529 while (node && TREE_CODE (node) == IDENTIFIER_NODE
20530 && (node == ridpointers [(int) RID_IN]
20531 || node == ridpointers [(int) RID_OUT]
20532 || node == ridpointers [(int) RID_INOUT]
20533 || node == ridpointers [(int) RID_BYCOPY]
20534 || node == ridpointers [(int) RID_BYREF]
20535 || node == ridpointers [(int) RID_ONEWAY]))
20537 quals = tree_cons (NULL_TREE, node, quals);
20538 cp_lexer_consume_token (parser->lexer);
20539 token = cp_lexer_peek_token (parser->lexer);
20540 node = token->u.value;
20543 return quals;
20546 /* Parse an Objective-C typename. */
20548 static tree
20549 cp_parser_objc_typename (cp_parser* parser)
20551 tree type_name = NULL_TREE;
20553 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20555 tree proto_quals, cp_type = NULL_TREE;
20557 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
20558 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
20560 /* An ObjC type name may consist of just protocol qualifiers, in which
20561 case the type shall default to 'id'. */
20562 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
20563 cp_type = cp_parser_type_id (parser);
20565 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20566 type_name = build_tree_list (proto_quals, cp_type);
20569 return type_name;
20572 /* Check to see if TYPE refers to an Objective-C selector name. */
20574 static bool
20575 cp_parser_objc_selector_p (enum cpp_ttype type)
20577 return (type == CPP_NAME || type == CPP_KEYWORD
20578 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
20579 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
20580 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
20581 || type == CPP_XOR || type == CPP_XOR_EQ);
20584 /* Parse an Objective-C selector. */
20586 static tree
20587 cp_parser_objc_selector (cp_parser* parser)
20589 cp_token *token = cp_lexer_consume_token (parser->lexer);
20591 if (!cp_parser_objc_selector_p (token->type))
20593 error_at (token->location, "invalid Objective-C++ selector name");
20594 return error_mark_node;
20597 /* C++ operator names are allowed to appear in ObjC selectors. */
20598 switch (token->type)
20600 case CPP_AND_AND: return get_identifier ("and");
20601 case CPP_AND_EQ: return get_identifier ("and_eq");
20602 case CPP_AND: return get_identifier ("bitand");
20603 case CPP_OR: return get_identifier ("bitor");
20604 case CPP_COMPL: return get_identifier ("compl");
20605 case CPP_NOT: return get_identifier ("not");
20606 case CPP_NOT_EQ: return get_identifier ("not_eq");
20607 case CPP_OR_OR: return get_identifier ("or");
20608 case CPP_OR_EQ: return get_identifier ("or_eq");
20609 case CPP_XOR: return get_identifier ("xor");
20610 case CPP_XOR_EQ: return get_identifier ("xor_eq");
20611 default: return token->u.value;
20615 /* Parse an Objective-C params list. */
20617 static tree
20618 cp_parser_objc_method_keyword_params (cp_parser* parser)
20620 tree params = NULL_TREE;
20621 bool maybe_unary_selector_p = true;
20622 cp_token *token = cp_lexer_peek_token (parser->lexer);
20624 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
20626 tree selector = NULL_TREE, type_name, identifier;
20628 if (token->type != CPP_COLON)
20629 selector = cp_parser_objc_selector (parser);
20631 /* Detect if we have a unary selector. */
20632 if (maybe_unary_selector_p
20633 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
20634 return selector;
20636 maybe_unary_selector_p = false;
20637 cp_parser_require (parser, CPP_COLON, "%<:%>");
20638 type_name = cp_parser_objc_typename (parser);
20639 identifier = cp_parser_identifier (parser);
20641 params
20642 = chainon (params,
20643 objc_build_keyword_decl (selector,
20644 type_name,
20645 identifier));
20647 token = cp_lexer_peek_token (parser->lexer);
20650 return params;
20653 /* Parse the non-keyword Objective-C params. */
20655 static tree
20656 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
20658 tree params = make_node (TREE_LIST);
20659 cp_token *token = cp_lexer_peek_token (parser->lexer);
20660 *ellipsisp = false; /* Initially, assume no ellipsis. */
20662 while (token->type == CPP_COMMA)
20664 cp_parameter_declarator *parmdecl;
20665 tree parm;
20667 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
20668 token = cp_lexer_peek_token (parser->lexer);
20670 if (token->type == CPP_ELLIPSIS)
20672 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
20673 *ellipsisp = true;
20674 break;
20677 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20678 parm = grokdeclarator (parmdecl->declarator,
20679 &parmdecl->decl_specifiers,
20680 PARM, /*initialized=*/0,
20681 /*attrlist=*/NULL);
20683 chainon (params, build_tree_list (NULL_TREE, parm));
20684 token = cp_lexer_peek_token (parser->lexer);
20687 return params;
20690 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
20692 static void
20693 cp_parser_objc_interstitial_code (cp_parser* parser)
20695 cp_token *token = cp_lexer_peek_token (parser->lexer);
20697 /* If the next token is `extern' and the following token is a string
20698 literal, then we have a linkage specification. */
20699 if (token->keyword == RID_EXTERN
20700 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
20701 cp_parser_linkage_specification (parser);
20702 /* Handle #pragma, if any. */
20703 else if (token->type == CPP_PRAGMA)
20704 cp_parser_pragma (parser, pragma_external);
20705 /* Allow stray semicolons. */
20706 else if (token->type == CPP_SEMICOLON)
20707 cp_lexer_consume_token (parser->lexer);
20708 /* Finally, try to parse a block-declaration, or a function-definition. */
20709 else
20710 cp_parser_block_declaration (parser, /*statement_p=*/false);
20713 /* Parse a method signature. */
20715 static tree
20716 cp_parser_objc_method_signature (cp_parser* parser)
20718 tree rettype, kwdparms, optparms;
20719 bool ellipsis = false;
20721 cp_parser_objc_method_type (parser);
20722 rettype = cp_parser_objc_typename (parser);
20723 kwdparms = cp_parser_objc_method_keyword_params (parser);
20724 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
20726 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
20729 /* Pars an Objective-C method prototype list. */
20731 static void
20732 cp_parser_objc_method_prototype_list (cp_parser* parser)
20734 cp_token *token = cp_lexer_peek_token (parser->lexer);
20736 while (token->keyword != RID_AT_END)
20738 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
20740 objc_add_method_declaration
20741 (cp_parser_objc_method_signature (parser));
20742 cp_parser_consume_semicolon_at_end_of_statement (parser);
20744 else
20745 /* Allow for interspersed non-ObjC++ code. */
20746 cp_parser_objc_interstitial_code (parser);
20748 token = cp_lexer_peek_token (parser->lexer);
20751 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
20752 objc_finish_interface ();
20755 /* Parse an Objective-C method definition list. */
20757 static void
20758 cp_parser_objc_method_definition_list (cp_parser* parser)
20760 cp_token *token = cp_lexer_peek_token (parser->lexer);
20762 while (token->keyword != RID_AT_END)
20764 tree meth;
20766 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
20768 push_deferring_access_checks (dk_deferred);
20769 objc_start_method_definition
20770 (cp_parser_objc_method_signature (parser));
20772 /* For historical reasons, we accept an optional semicolon. */
20773 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20774 cp_lexer_consume_token (parser->lexer);
20776 perform_deferred_access_checks ();
20777 stop_deferring_access_checks ();
20778 meth = cp_parser_function_definition_after_declarator (parser,
20779 false);
20780 pop_deferring_access_checks ();
20781 objc_finish_method_definition (meth);
20783 else
20784 /* Allow for interspersed non-ObjC++ code. */
20785 cp_parser_objc_interstitial_code (parser);
20787 token = cp_lexer_peek_token (parser->lexer);
20790 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
20791 objc_finish_implementation ();
20794 /* Parse Objective-C ivars. */
20796 static void
20797 cp_parser_objc_class_ivars (cp_parser* parser)
20799 cp_token *token = cp_lexer_peek_token (parser->lexer);
20801 if (token->type != CPP_OPEN_BRACE)
20802 return; /* No ivars specified. */
20804 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
20805 token = cp_lexer_peek_token (parser->lexer);
20807 while (token->type != CPP_CLOSE_BRACE)
20809 cp_decl_specifier_seq declspecs;
20810 int decl_class_or_enum_p;
20811 tree prefix_attributes;
20813 cp_parser_objc_visibility_spec (parser);
20815 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
20816 break;
20818 cp_parser_decl_specifier_seq (parser,
20819 CP_PARSER_FLAGS_OPTIONAL,
20820 &declspecs,
20821 &decl_class_or_enum_p);
20822 prefix_attributes = declspecs.attributes;
20823 declspecs.attributes = NULL_TREE;
20825 /* Keep going until we hit the `;' at the end of the
20826 declaration. */
20827 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20829 tree width = NULL_TREE, attributes, first_attribute, decl;
20830 cp_declarator *declarator = NULL;
20831 int ctor_dtor_or_conv_p;
20833 /* Check for a (possibly unnamed) bitfield declaration. */
20834 token = cp_lexer_peek_token (parser->lexer);
20835 if (token->type == CPP_COLON)
20836 goto eat_colon;
20838 if (token->type == CPP_NAME
20839 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
20840 == CPP_COLON))
20842 /* Get the name of the bitfield. */
20843 declarator = make_id_declarator (NULL_TREE,
20844 cp_parser_identifier (parser),
20845 sfk_none);
20847 eat_colon:
20848 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
20849 /* Get the width of the bitfield. */
20850 width
20851 = cp_parser_constant_expression (parser,
20852 /*allow_non_constant=*/false,
20853 NULL);
20855 else
20857 /* Parse the declarator. */
20858 declarator
20859 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20860 &ctor_dtor_or_conv_p,
20861 /*parenthesized_p=*/NULL,
20862 /*member_p=*/false);
20865 /* Look for attributes that apply to the ivar. */
20866 attributes = cp_parser_attributes_opt (parser);
20867 /* Remember which attributes are prefix attributes and
20868 which are not. */
20869 first_attribute = attributes;
20870 /* Combine the attributes. */
20871 attributes = chainon (prefix_attributes, attributes);
20873 if (width)
20874 /* Create the bitfield declaration. */
20875 decl = grokbitfield (declarator, &declspecs,
20876 width,
20877 attributes);
20878 else
20879 decl = grokfield (declarator, &declspecs,
20880 NULL_TREE, /*init_const_expr_p=*/false,
20881 NULL_TREE, attributes);
20883 /* Add the instance variable. */
20884 objc_add_instance_variable (decl);
20886 /* Reset PREFIX_ATTRIBUTES. */
20887 while (attributes && TREE_CHAIN (attributes) != first_attribute)
20888 attributes = TREE_CHAIN (attributes);
20889 if (attributes)
20890 TREE_CHAIN (attributes) = NULL_TREE;
20892 token = cp_lexer_peek_token (parser->lexer);
20894 if (token->type == CPP_COMMA)
20896 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
20897 continue;
20899 break;
20902 cp_parser_consume_semicolon_at_end_of_statement (parser);
20903 token = cp_lexer_peek_token (parser->lexer);
20906 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
20907 /* For historical reasons, we accept an optional semicolon. */
20908 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20909 cp_lexer_consume_token (parser->lexer);
20912 /* Parse an Objective-C protocol declaration. */
20914 static void
20915 cp_parser_objc_protocol_declaration (cp_parser* parser)
20917 tree proto, protorefs;
20918 cp_token *tok;
20920 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
20921 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
20923 tok = cp_lexer_peek_token (parser->lexer);
20924 error_at (tok->location, "identifier expected after %<@protocol%>");
20925 goto finish;
20928 /* See if we have a forward declaration or a definition. */
20929 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
20931 /* Try a forward declaration first. */
20932 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
20934 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
20935 finish:
20936 cp_parser_consume_semicolon_at_end_of_statement (parser);
20939 /* Ok, we got a full-fledged definition (or at least should). */
20940 else
20942 proto = cp_parser_identifier (parser);
20943 protorefs = cp_parser_objc_protocol_refs_opt (parser);
20944 objc_start_protocol (proto, protorefs);
20945 cp_parser_objc_method_prototype_list (parser);
20949 /* Parse an Objective-C superclass or category. */
20951 static void
20952 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
20953 tree *categ)
20955 cp_token *next = cp_lexer_peek_token (parser->lexer);
20957 *super = *categ = NULL_TREE;
20958 if (next->type == CPP_COLON)
20960 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
20961 *super = cp_parser_identifier (parser);
20963 else if (next->type == CPP_OPEN_PAREN)
20965 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
20966 *categ = cp_parser_identifier (parser);
20967 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20971 /* Parse an Objective-C class interface. */
20973 static void
20974 cp_parser_objc_class_interface (cp_parser* parser)
20976 tree name, super, categ, protos;
20978 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
20979 name = cp_parser_identifier (parser);
20980 cp_parser_objc_superclass_or_category (parser, &super, &categ);
20981 protos = cp_parser_objc_protocol_refs_opt (parser);
20983 /* We have either a class or a category on our hands. */
20984 if (categ)
20985 objc_start_category_interface (name, categ, protos);
20986 else
20988 objc_start_class_interface (name, super, protos);
20989 /* Handle instance variable declarations, if any. */
20990 cp_parser_objc_class_ivars (parser);
20991 objc_continue_interface ();
20994 cp_parser_objc_method_prototype_list (parser);
20997 /* Parse an Objective-C class implementation. */
20999 static void
21000 cp_parser_objc_class_implementation (cp_parser* parser)
21002 tree name, super, categ;
21004 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
21005 name = cp_parser_identifier (parser);
21006 cp_parser_objc_superclass_or_category (parser, &super, &categ);
21008 /* We have either a class or a category on our hands. */
21009 if (categ)
21010 objc_start_category_implementation (name, categ);
21011 else
21013 objc_start_class_implementation (name, super);
21014 /* Handle instance variable declarations, if any. */
21015 cp_parser_objc_class_ivars (parser);
21016 objc_continue_implementation ();
21019 cp_parser_objc_method_definition_list (parser);
21022 /* Consume the @end token and finish off the implementation. */
21024 static void
21025 cp_parser_objc_end_implementation (cp_parser* parser)
21027 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
21028 objc_finish_implementation ();
21031 /* Parse an Objective-C declaration. */
21033 static void
21034 cp_parser_objc_declaration (cp_parser* parser)
21036 /* Try to figure out what kind of declaration is present. */
21037 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21039 switch (kwd->keyword)
21041 case RID_AT_ALIAS:
21042 cp_parser_objc_alias_declaration (parser);
21043 break;
21044 case RID_AT_CLASS:
21045 cp_parser_objc_class_declaration (parser);
21046 break;
21047 case RID_AT_PROTOCOL:
21048 cp_parser_objc_protocol_declaration (parser);
21049 break;
21050 case RID_AT_INTERFACE:
21051 cp_parser_objc_class_interface (parser);
21052 break;
21053 case RID_AT_IMPLEMENTATION:
21054 cp_parser_objc_class_implementation (parser);
21055 break;
21056 case RID_AT_END:
21057 cp_parser_objc_end_implementation (parser);
21058 break;
21059 default:
21060 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
21061 kwd->u.value);
21062 cp_parser_skip_to_end_of_block_or_statement (parser);
21066 /* Parse an Objective-C try-catch-finally statement.
21068 objc-try-catch-finally-stmt:
21069 @try compound-statement objc-catch-clause-seq [opt]
21070 objc-finally-clause [opt]
21072 objc-catch-clause-seq:
21073 objc-catch-clause objc-catch-clause-seq [opt]
21075 objc-catch-clause:
21076 @catch ( exception-declaration ) compound-statement
21078 objc-finally-clause
21079 @finally compound-statement
21081 Returns NULL_TREE. */
21083 static tree
21084 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
21085 location_t location;
21086 tree stmt;
21088 cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
21089 location = cp_lexer_peek_token (parser->lexer)->location;
21090 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
21091 node, lest it get absorbed into the surrounding block. */
21092 stmt = push_stmt_list ();
21093 cp_parser_compound_statement (parser, NULL, false);
21094 objc_begin_try_stmt (location, pop_stmt_list (stmt));
21096 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
21098 cp_parameter_declarator *parmdecl;
21099 tree parm;
21101 cp_lexer_consume_token (parser->lexer);
21102 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
21103 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
21104 parm = grokdeclarator (parmdecl->declarator,
21105 &parmdecl->decl_specifiers,
21106 PARM, /*initialized=*/0,
21107 /*attrlist=*/NULL);
21108 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
21109 objc_begin_catch_clause (parm);
21110 cp_parser_compound_statement (parser, NULL, false);
21111 objc_finish_catch_clause ();
21114 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
21116 cp_lexer_consume_token (parser->lexer);
21117 location = cp_lexer_peek_token (parser->lexer)->location;
21118 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
21119 node, lest it get absorbed into the surrounding block. */
21120 stmt = push_stmt_list ();
21121 cp_parser_compound_statement (parser, NULL, false);
21122 objc_build_finally_clause (location, pop_stmt_list (stmt));
21125 return objc_finish_try_stmt ();
21128 /* Parse an Objective-C synchronized statement.
21130 objc-synchronized-stmt:
21131 @synchronized ( expression ) compound-statement
21133 Returns NULL_TREE. */
21135 static tree
21136 cp_parser_objc_synchronized_statement (cp_parser *parser) {
21137 location_t location;
21138 tree lock, stmt;
21140 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
21142 location = cp_lexer_peek_token (parser->lexer)->location;
21143 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
21144 lock = cp_parser_expression (parser, false, NULL);
21145 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
21147 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
21148 node, lest it get absorbed into the surrounding block. */
21149 stmt = push_stmt_list ();
21150 cp_parser_compound_statement (parser, NULL, false);
21152 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
21155 /* Parse an Objective-C throw statement.
21157 objc-throw-stmt:
21158 @throw assignment-expression [opt] ;
21160 Returns a constructed '@throw' statement. */
21162 static tree
21163 cp_parser_objc_throw_statement (cp_parser *parser) {
21164 tree expr = NULL_TREE;
21165 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21167 cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
21169 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21170 expr = cp_parser_assignment_expression (parser, false, NULL);
21172 cp_parser_consume_semicolon_at_end_of_statement (parser);
21174 return objc_build_throw_stmt (loc, expr);
21177 /* Parse an Objective-C statement. */
21179 static tree
21180 cp_parser_objc_statement (cp_parser * parser) {
21181 /* Try to figure out what kind of declaration is present. */
21182 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21184 switch (kwd->keyword)
21186 case RID_AT_TRY:
21187 return cp_parser_objc_try_catch_finally_statement (parser);
21188 case RID_AT_SYNCHRONIZED:
21189 return cp_parser_objc_synchronized_statement (parser);
21190 case RID_AT_THROW:
21191 return cp_parser_objc_throw_statement (parser);
21192 default:
21193 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
21194 kwd->u.value);
21195 cp_parser_skip_to_end_of_block_or_statement (parser);
21198 return error_mark_node;
21201 /* OpenMP 2.5 parsing routines. */
21203 /* Returns name of the next clause.
21204 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
21205 the token is not consumed. Otherwise appropriate pragma_omp_clause is
21206 returned and the token is consumed. */
21208 static pragma_omp_clause
21209 cp_parser_omp_clause_name (cp_parser *parser)
21211 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
21213 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
21214 result = PRAGMA_OMP_CLAUSE_IF;
21215 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
21216 result = PRAGMA_OMP_CLAUSE_DEFAULT;
21217 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
21218 result = PRAGMA_OMP_CLAUSE_PRIVATE;
21219 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21221 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21222 const char *p = IDENTIFIER_POINTER (id);
21224 switch (p[0])
21226 case 'c':
21227 if (!strcmp ("collapse", p))
21228 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
21229 else if (!strcmp ("copyin", p))
21230 result = PRAGMA_OMP_CLAUSE_COPYIN;
21231 else if (!strcmp ("copyprivate", p))
21232 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
21233 break;
21234 case 'f':
21235 if (!strcmp ("firstprivate", p))
21236 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
21237 break;
21238 case 'l':
21239 if (!strcmp ("lastprivate", p))
21240 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
21241 break;
21242 case 'n':
21243 if (!strcmp ("nowait", p))
21244 result = PRAGMA_OMP_CLAUSE_NOWAIT;
21245 else if (!strcmp ("num_threads", p))
21246 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
21247 break;
21248 case 'o':
21249 if (!strcmp ("ordered", p))
21250 result = PRAGMA_OMP_CLAUSE_ORDERED;
21251 break;
21252 case 'r':
21253 if (!strcmp ("reduction", p))
21254 result = PRAGMA_OMP_CLAUSE_REDUCTION;
21255 break;
21256 case 's':
21257 if (!strcmp ("schedule", p))
21258 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
21259 else if (!strcmp ("shared", p))
21260 result = PRAGMA_OMP_CLAUSE_SHARED;
21261 break;
21262 case 'u':
21263 if (!strcmp ("untied", p))
21264 result = PRAGMA_OMP_CLAUSE_UNTIED;
21265 break;
21269 if (result != PRAGMA_OMP_CLAUSE_NONE)
21270 cp_lexer_consume_token (parser->lexer);
21272 return result;
21275 /* Validate that a clause of the given type does not already exist. */
21277 static void
21278 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
21279 const char *name, location_t location)
21281 tree c;
21283 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21284 if (OMP_CLAUSE_CODE (c) == code)
21286 error_at (location, "too many %qs clauses", name);
21287 break;
21291 /* OpenMP 2.5:
21292 variable-list:
21293 identifier
21294 variable-list , identifier
21296 In addition, we match a closing parenthesis. An opening parenthesis
21297 will have been consumed by the caller.
21299 If KIND is nonzero, create the appropriate node and install the decl
21300 in OMP_CLAUSE_DECL and add the node to the head of the list.
21302 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
21303 return the list created. */
21305 static tree
21306 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
21307 tree list)
21309 cp_token *token;
21310 while (1)
21312 tree name, decl;
21314 token = cp_lexer_peek_token (parser->lexer);
21315 name = cp_parser_id_expression (parser, /*template_p=*/false,
21316 /*check_dependency_p=*/true,
21317 /*template_p=*/NULL,
21318 /*declarator_p=*/false,
21319 /*optional_p=*/false);
21320 if (name == error_mark_node)
21321 goto skip_comma;
21323 decl = cp_parser_lookup_name_simple (parser, name, token->location);
21324 if (decl == error_mark_node)
21325 cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
21326 else if (kind != 0)
21328 tree u = build_omp_clause (token->location, kind);
21329 OMP_CLAUSE_DECL (u) = decl;
21330 OMP_CLAUSE_CHAIN (u) = list;
21331 list = u;
21333 else
21334 list = tree_cons (decl, NULL_TREE, list);
21336 get_comma:
21337 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21338 break;
21339 cp_lexer_consume_token (parser->lexer);
21342 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21344 int ending;
21346 /* Try to resync to an unnested comma. Copied from
21347 cp_parser_parenthesized_expression_list. */
21348 skip_comma:
21349 ending = cp_parser_skip_to_closing_parenthesis (parser,
21350 /*recovering=*/true,
21351 /*or_comma=*/true,
21352 /*consume_paren=*/true);
21353 if (ending < 0)
21354 goto get_comma;
21357 return list;
21360 /* Similarly, but expect leading and trailing parenthesis. This is a very
21361 common case for omp clauses. */
21363 static tree
21364 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
21366 if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21367 return cp_parser_omp_var_list_no_open (parser, kind, list);
21368 return list;
21371 /* OpenMP 3.0:
21372 collapse ( constant-expression ) */
21374 static tree
21375 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
21377 tree c, num;
21378 location_t loc;
21379 HOST_WIDE_INT n;
21381 loc = cp_lexer_peek_token (parser->lexer)->location;
21382 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21383 return list;
21385 num = cp_parser_constant_expression (parser, false, NULL);
21387 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21388 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21389 /*or_comma=*/false,
21390 /*consume_paren=*/true);
21392 if (num == error_mark_node)
21393 return list;
21394 num = fold_non_dependent_expr (num);
21395 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
21396 || !host_integerp (num, 0)
21397 || (n = tree_low_cst (num, 0)) <= 0
21398 || (int) n != n)
21400 error_at (loc, "collapse argument needs positive constant integer expression");
21401 return list;
21404 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
21405 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
21406 OMP_CLAUSE_CHAIN (c) = list;
21407 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
21409 return c;
21412 /* OpenMP 2.5:
21413 default ( shared | none ) */
21415 static tree
21416 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
21418 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
21419 tree c;
21421 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21422 return list;
21423 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21425 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21426 const char *p = IDENTIFIER_POINTER (id);
21428 switch (p[0])
21430 case 'n':
21431 if (strcmp ("none", p) != 0)
21432 goto invalid_kind;
21433 kind = OMP_CLAUSE_DEFAULT_NONE;
21434 break;
21436 case 's':
21437 if (strcmp ("shared", p) != 0)
21438 goto invalid_kind;
21439 kind = OMP_CLAUSE_DEFAULT_SHARED;
21440 break;
21442 default:
21443 goto invalid_kind;
21446 cp_lexer_consume_token (parser->lexer);
21448 else
21450 invalid_kind:
21451 cp_parser_error (parser, "expected %<none%> or %<shared%>");
21454 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21455 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21456 /*or_comma=*/false,
21457 /*consume_paren=*/true);
21459 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
21460 return list;
21462 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
21463 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
21464 OMP_CLAUSE_CHAIN (c) = list;
21465 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
21467 return c;
21470 /* OpenMP 2.5:
21471 if ( expression ) */
21473 static tree
21474 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
21476 tree t, c;
21478 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21479 return list;
21481 t = cp_parser_condition (parser);
21483 if (t == error_mark_node
21484 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21485 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21486 /*or_comma=*/false,
21487 /*consume_paren=*/true);
21489 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
21491 c = build_omp_clause (location, OMP_CLAUSE_IF);
21492 OMP_CLAUSE_IF_EXPR (c) = t;
21493 OMP_CLAUSE_CHAIN (c) = list;
21495 return c;
21498 /* OpenMP 2.5:
21499 nowait */
21501 static tree
21502 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
21503 tree list, location_t location)
21505 tree c;
21507 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
21509 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
21510 OMP_CLAUSE_CHAIN (c) = list;
21511 return c;
21514 /* OpenMP 2.5:
21515 num_threads ( expression ) */
21517 static tree
21518 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
21519 location_t location)
21521 tree t, c;
21523 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21524 return list;
21526 t = cp_parser_expression (parser, false, NULL);
21528 if (t == error_mark_node
21529 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21530 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21531 /*or_comma=*/false,
21532 /*consume_paren=*/true);
21534 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
21535 "num_threads", location);
21537 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
21538 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
21539 OMP_CLAUSE_CHAIN (c) = list;
21541 return c;
21544 /* OpenMP 2.5:
21545 ordered */
21547 static tree
21548 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
21549 tree list, location_t location)
21551 tree c;
21553 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
21554 "ordered", location);
21556 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
21557 OMP_CLAUSE_CHAIN (c) = list;
21558 return c;
21561 /* OpenMP 2.5:
21562 reduction ( reduction-operator : variable-list )
21564 reduction-operator:
21565 One of: + * - & ^ | && || */
21567 static tree
21568 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
21570 enum tree_code code;
21571 tree nlist, c;
21573 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21574 return list;
21576 switch (cp_lexer_peek_token (parser->lexer)->type)
21578 case CPP_PLUS:
21579 code = PLUS_EXPR;
21580 break;
21581 case CPP_MULT:
21582 code = MULT_EXPR;
21583 break;
21584 case CPP_MINUS:
21585 code = MINUS_EXPR;
21586 break;
21587 case CPP_AND:
21588 code = BIT_AND_EXPR;
21589 break;
21590 case CPP_XOR:
21591 code = BIT_XOR_EXPR;
21592 break;
21593 case CPP_OR:
21594 code = BIT_IOR_EXPR;
21595 break;
21596 case CPP_AND_AND:
21597 code = TRUTH_ANDIF_EXPR;
21598 break;
21599 case CPP_OR_OR:
21600 code = TRUTH_ORIF_EXPR;
21601 break;
21602 default:
21603 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
21604 "%<|%>, %<&&%>, or %<||%>");
21605 resync_fail:
21606 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21607 /*or_comma=*/false,
21608 /*consume_paren=*/true);
21609 return list;
21611 cp_lexer_consume_token (parser->lexer);
21613 if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
21614 goto resync_fail;
21616 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
21617 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
21618 OMP_CLAUSE_REDUCTION_CODE (c) = code;
21620 return nlist;
21623 /* OpenMP 2.5:
21624 schedule ( schedule-kind )
21625 schedule ( schedule-kind , expression )
21627 schedule-kind:
21628 static | dynamic | guided | runtime | auto */
21630 static tree
21631 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
21633 tree c, t;
21635 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21636 return list;
21638 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
21640 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21642 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21643 const char *p = IDENTIFIER_POINTER (id);
21645 switch (p[0])
21647 case 'd':
21648 if (strcmp ("dynamic", p) != 0)
21649 goto invalid_kind;
21650 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
21651 break;
21653 case 'g':
21654 if (strcmp ("guided", p) != 0)
21655 goto invalid_kind;
21656 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
21657 break;
21659 case 'r':
21660 if (strcmp ("runtime", p) != 0)
21661 goto invalid_kind;
21662 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
21663 break;
21665 default:
21666 goto invalid_kind;
21669 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
21670 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
21671 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
21672 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
21673 else
21674 goto invalid_kind;
21675 cp_lexer_consume_token (parser->lexer);
21677 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21679 cp_token *token;
21680 cp_lexer_consume_token (parser->lexer);
21682 token = cp_lexer_peek_token (parser->lexer);
21683 t = cp_parser_assignment_expression (parser, false, NULL);
21685 if (t == error_mark_node)
21686 goto resync_fail;
21687 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
21688 error_at (token->location, "schedule %<runtime%> does not take "
21689 "a %<chunk_size%> parameter");
21690 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
21691 error_at (token->location, "schedule %<auto%> does not take "
21692 "a %<chunk_size%> parameter");
21693 else
21694 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
21696 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21697 goto resync_fail;
21699 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
21700 goto resync_fail;
21702 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
21703 OMP_CLAUSE_CHAIN (c) = list;
21704 return c;
21706 invalid_kind:
21707 cp_parser_error (parser, "invalid schedule kind");
21708 resync_fail:
21709 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21710 /*or_comma=*/false,
21711 /*consume_paren=*/true);
21712 return list;
21715 /* OpenMP 3.0:
21716 untied */
21718 static tree
21719 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
21720 tree list, location_t location)
21722 tree c;
21724 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
21726 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
21727 OMP_CLAUSE_CHAIN (c) = list;
21728 return c;
21731 /* Parse all OpenMP clauses. The set clauses allowed by the directive
21732 is a bitmask in MASK. Return the list of clauses found; the result
21733 of clause default goes in *pdefault. */
21735 static tree
21736 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
21737 const char *where, cp_token *pragma_tok)
21739 tree clauses = NULL;
21740 bool first = true;
21741 cp_token *token = NULL;
21743 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
21745 pragma_omp_clause c_kind;
21746 const char *c_name;
21747 tree prev = clauses;
21749 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21750 cp_lexer_consume_token (parser->lexer);
21752 token = cp_lexer_peek_token (parser->lexer);
21753 c_kind = cp_parser_omp_clause_name (parser);
21754 first = false;
21756 switch (c_kind)
21758 case PRAGMA_OMP_CLAUSE_COLLAPSE:
21759 clauses = cp_parser_omp_clause_collapse (parser, clauses,
21760 token->location);
21761 c_name = "collapse";
21762 break;
21763 case PRAGMA_OMP_CLAUSE_COPYIN:
21764 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
21765 c_name = "copyin";
21766 break;
21767 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
21768 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
21769 clauses);
21770 c_name = "copyprivate";
21771 break;
21772 case PRAGMA_OMP_CLAUSE_DEFAULT:
21773 clauses = cp_parser_omp_clause_default (parser, clauses,
21774 token->location);
21775 c_name = "default";
21776 break;
21777 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
21778 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
21779 clauses);
21780 c_name = "firstprivate";
21781 break;
21782 case PRAGMA_OMP_CLAUSE_IF:
21783 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
21784 c_name = "if";
21785 break;
21786 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
21787 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
21788 clauses);
21789 c_name = "lastprivate";
21790 break;
21791 case PRAGMA_OMP_CLAUSE_NOWAIT:
21792 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
21793 c_name = "nowait";
21794 break;
21795 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
21796 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
21797 token->location);
21798 c_name = "num_threads";
21799 break;
21800 case PRAGMA_OMP_CLAUSE_ORDERED:
21801 clauses = cp_parser_omp_clause_ordered (parser, clauses,
21802 token->location);
21803 c_name = "ordered";
21804 break;
21805 case PRAGMA_OMP_CLAUSE_PRIVATE:
21806 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
21807 clauses);
21808 c_name = "private";
21809 break;
21810 case PRAGMA_OMP_CLAUSE_REDUCTION:
21811 clauses = cp_parser_omp_clause_reduction (parser, clauses);
21812 c_name = "reduction";
21813 break;
21814 case PRAGMA_OMP_CLAUSE_SCHEDULE:
21815 clauses = cp_parser_omp_clause_schedule (parser, clauses,
21816 token->location);
21817 c_name = "schedule";
21818 break;
21819 case PRAGMA_OMP_CLAUSE_SHARED:
21820 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
21821 clauses);
21822 c_name = "shared";
21823 break;
21824 case PRAGMA_OMP_CLAUSE_UNTIED:
21825 clauses = cp_parser_omp_clause_untied (parser, clauses,
21826 token->location);
21827 c_name = "nowait";
21828 break;
21829 default:
21830 cp_parser_error (parser, "expected %<#pragma omp%> clause");
21831 goto saw_error;
21834 if (((mask >> c_kind) & 1) == 0)
21836 /* Remove the invalid clause(s) from the list to avoid
21837 confusing the rest of the compiler. */
21838 clauses = prev;
21839 error_at (token->location, "%qs is not valid for %qs", c_name, where);
21842 saw_error:
21843 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
21844 return finish_omp_clauses (clauses);
21847 /* OpenMP 2.5:
21848 structured-block:
21849 statement
21851 In practice, we're also interested in adding the statement to an
21852 outer node. So it is convenient if we work around the fact that
21853 cp_parser_statement calls add_stmt. */
21855 static unsigned
21856 cp_parser_begin_omp_structured_block (cp_parser *parser)
21858 unsigned save = parser->in_statement;
21860 /* Only move the values to IN_OMP_BLOCK if they weren't false.
21861 This preserves the "not within loop or switch" style error messages
21862 for nonsense cases like
21863 void foo() {
21864 #pragma omp single
21865 break;
21868 if (parser->in_statement)
21869 parser->in_statement = IN_OMP_BLOCK;
21871 return save;
21874 static void
21875 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
21877 parser->in_statement = save;
21880 static tree
21881 cp_parser_omp_structured_block (cp_parser *parser)
21883 tree stmt = begin_omp_structured_block ();
21884 unsigned int save = cp_parser_begin_omp_structured_block (parser);
21886 cp_parser_statement (parser, NULL_TREE, false, NULL);
21888 cp_parser_end_omp_structured_block (parser, save);
21889 return finish_omp_structured_block (stmt);
21892 /* OpenMP 2.5:
21893 # pragma omp atomic new-line
21894 expression-stmt
21896 expression-stmt:
21897 x binop= expr | x++ | ++x | x-- | --x
21898 binop:
21899 +, *, -, /, &, ^, |, <<, >>
21901 where x is an lvalue expression with scalar type. */
21903 static void
21904 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
21906 tree lhs, rhs;
21907 enum tree_code code;
21909 cp_parser_require_pragma_eol (parser, pragma_tok);
21911 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
21912 /*cast_p=*/false, NULL);
21913 switch (TREE_CODE (lhs))
21915 case ERROR_MARK:
21916 goto saw_error;
21918 case PREINCREMENT_EXPR:
21919 case POSTINCREMENT_EXPR:
21920 lhs = TREE_OPERAND (lhs, 0);
21921 code = PLUS_EXPR;
21922 rhs = integer_one_node;
21923 break;
21925 case PREDECREMENT_EXPR:
21926 case POSTDECREMENT_EXPR:
21927 lhs = TREE_OPERAND (lhs, 0);
21928 code = MINUS_EXPR;
21929 rhs = integer_one_node;
21930 break;
21932 default:
21933 switch (cp_lexer_peek_token (parser->lexer)->type)
21935 case CPP_MULT_EQ:
21936 code = MULT_EXPR;
21937 break;
21938 case CPP_DIV_EQ:
21939 code = TRUNC_DIV_EXPR;
21940 break;
21941 case CPP_PLUS_EQ:
21942 code = PLUS_EXPR;
21943 break;
21944 case CPP_MINUS_EQ:
21945 code = MINUS_EXPR;
21946 break;
21947 case CPP_LSHIFT_EQ:
21948 code = LSHIFT_EXPR;
21949 break;
21950 case CPP_RSHIFT_EQ:
21951 code = RSHIFT_EXPR;
21952 break;
21953 case CPP_AND_EQ:
21954 code = BIT_AND_EXPR;
21955 break;
21956 case CPP_OR_EQ:
21957 code = BIT_IOR_EXPR;
21958 break;
21959 case CPP_XOR_EQ:
21960 code = BIT_XOR_EXPR;
21961 break;
21962 default:
21963 cp_parser_error (parser,
21964 "invalid operator for %<#pragma omp atomic%>");
21965 goto saw_error;
21967 cp_lexer_consume_token (parser->lexer);
21969 rhs = cp_parser_expression (parser, false, NULL);
21970 if (rhs == error_mark_node)
21971 goto saw_error;
21972 break;
21974 finish_omp_atomic (code, lhs, rhs);
21975 cp_parser_consume_semicolon_at_end_of_statement (parser);
21976 return;
21978 saw_error:
21979 cp_parser_skip_to_end_of_block_or_statement (parser);
21983 /* OpenMP 2.5:
21984 # pragma omp barrier new-line */
21986 static void
21987 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
21989 cp_parser_require_pragma_eol (parser, pragma_tok);
21990 finish_omp_barrier ();
21993 /* OpenMP 2.5:
21994 # pragma omp critical [(name)] new-line
21995 structured-block */
21997 static tree
21998 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
22000 tree stmt, name = NULL;
22002 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22004 cp_lexer_consume_token (parser->lexer);
22006 name = cp_parser_identifier (parser);
22008 if (name == error_mark_node
22009 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
22010 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22011 /*or_comma=*/false,
22012 /*consume_paren=*/true);
22013 if (name == error_mark_node)
22014 name = NULL;
22016 cp_parser_require_pragma_eol (parser, pragma_tok);
22018 stmt = cp_parser_omp_structured_block (parser);
22019 return c_finish_omp_critical (input_location, stmt, name);
22022 /* OpenMP 2.5:
22023 # pragma omp flush flush-vars[opt] new-line
22025 flush-vars:
22026 ( variable-list ) */
22028 static void
22029 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
22031 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22032 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
22033 cp_parser_require_pragma_eol (parser, pragma_tok);
22035 finish_omp_flush ();
22038 /* Helper function, to parse omp for increment expression. */
22040 static tree
22041 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
22043 tree cond = cp_parser_binary_expression (parser, false, true,
22044 PREC_NOT_OPERATOR, NULL);
22045 bool overloaded_p;
22047 if (cond == error_mark_node
22048 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22050 cp_parser_skip_to_end_of_statement (parser);
22051 return error_mark_node;
22054 switch (TREE_CODE (cond))
22056 case GT_EXPR:
22057 case GE_EXPR:
22058 case LT_EXPR:
22059 case LE_EXPR:
22060 break;
22061 default:
22062 return error_mark_node;
22065 /* If decl is an iterator, preserve LHS and RHS of the relational
22066 expr until finish_omp_for. */
22067 if (decl
22068 && (type_dependent_expression_p (decl)
22069 || CLASS_TYPE_P (TREE_TYPE (decl))))
22070 return cond;
22072 return build_x_binary_op (TREE_CODE (cond),
22073 TREE_OPERAND (cond, 0), ERROR_MARK,
22074 TREE_OPERAND (cond, 1), ERROR_MARK,
22075 &overloaded_p, tf_warning_or_error);
22078 /* Helper function, to parse omp for increment expression. */
22080 static tree
22081 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
22083 cp_token *token = cp_lexer_peek_token (parser->lexer);
22084 enum tree_code op;
22085 tree lhs, rhs;
22086 cp_id_kind idk;
22087 bool decl_first;
22089 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
22091 op = (token->type == CPP_PLUS_PLUS
22092 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
22093 cp_lexer_consume_token (parser->lexer);
22094 lhs = cp_parser_cast_expression (parser, false, false, NULL);
22095 if (lhs != decl)
22096 return error_mark_node;
22097 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
22100 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
22101 if (lhs != decl)
22102 return error_mark_node;
22104 token = cp_lexer_peek_token (parser->lexer);
22105 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
22107 op = (token->type == CPP_PLUS_PLUS
22108 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
22109 cp_lexer_consume_token (parser->lexer);
22110 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
22113 op = cp_parser_assignment_operator_opt (parser);
22114 if (op == ERROR_MARK)
22115 return error_mark_node;
22117 if (op != NOP_EXPR)
22119 rhs = cp_parser_assignment_expression (parser, false, NULL);
22120 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
22121 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
22124 lhs = cp_parser_binary_expression (parser, false, false,
22125 PREC_ADDITIVE_EXPRESSION, NULL);
22126 token = cp_lexer_peek_token (parser->lexer);
22127 decl_first = lhs == decl;
22128 if (decl_first)
22129 lhs = NULL_TREE;
22130 if (token->type != CPP_PLUS
22131 && token->type != CPP_MINUS)
22132 return error_mark_node;
22136 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
22137 cp_lexer_consume_token (parser->lexer);
22138 rhs = cp_parser_binary_expression (parser, false, false,
22139 PREC_ADDITIVE_EXPRESSION, NULL);
22140 token = cp_lexer_peek_token (parser->lexer);
22141 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
22143 if (lhs == NULL_TREE)
22145 if (op == PLUS_EXPR)
22146 lhs = rhs;
22147 else
22148 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
22150 else
22151 lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
22152 NULL, tf_warning_or_error);
22155 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
22157 if (!decl_first)
22159 if (rhs != decl || op == MINUS_EXPR)
22160 return error_mark_node;
22161 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
22163 else
22164 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
22166 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
22169 /* Parse the restricted form of the for statement allowed by OpenMP. */
22171 static tree
22172 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
22174 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
22175 tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
22176 tree this_pre_body, cl;
22177 location_t loc_first;
22178 bool collapse_err = false;
22179 int i, collapse = 1, nbraces = 0;
22181 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
22182 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
22183 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
22185 gcc_assert (collapse >= 1);
22187 declv = make_tree_vec (collapse);
22188 initv = make_tree_vec (collapse);
22189 condv = make_tree_vec (collapse);
22190 incrv = make_tree_vec (collapse);
22192 loc_first = cp_lexer_peek_token (parser->lexer)->location;
22194 for (i = 0; i < collapse; i++)
22196 int bracecount = 0;
22197 bool add_private_clause = false;
22198 location_t loc;
22200 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22202 cp_parser_error (parser, "for statement expected");
22203 return NULL;
22205 loc = cp_lexer_consume_token (parser->lexer)->location;
22207 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
22208 return NULL;
22210 init = decl = real_decl = NULL;
22211 this_pre_body = push_stmt_list ();
22212 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22214 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
22216 init-expr:
22217 var = lb
22218 integer-type var = lb
22219 random-access-iterator-type var = lb
22220 pointer-type var = lb
22222 cp_decl_specifier_seq type_specifiers;
22224 /* First, try to parse as an initialized declaration. See
22225 cp_parser_condition, from whence the bulk of this is copied. */
22227 cp_parser_parse_tentatively (parser);
22228 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
22229 /*is_trailing_return=*/false,
22230 &type_specifiers);
22231 if (cp_parser_parse_definitely (parser))
22233 /* If parsing a type specifier seq succeeded, then this
22234 MUST be a initialized declaration. */
22235 tree asm_specification, attributes;
22236 cp_declarator *declarator;
22238 declarator = cp_parser_declarator (parser,
22239 CP_PARSER_DECLARATOR_NAMED,
22240 /*ctor_dtor_or_conv_p=*/NULL,
22241 /*parenthesized_p=*/NULL,
22242 /*member_p=*/false);
22243 attributes = cp_parser_attributes_opt (parser);
22244 asm_specification = cp_parser_asm_specification_opt (parser);
22246 if (declarator == cp_error_declarator)
22247 cp_parser_skip_to_end_of_statement (parser);
22249 else
22251 tree pushed_scope, auto_node;
22253 decl = start_decl (declarator, &type_specifiers,
22254 SD_INITIALIZED, attributes,
22255 /*prefix_attributes=*/NULL_TREE,
22256 &pushed_scope);
22258 auto_node = type_uses_auto (TREE_TYPE (decl));
22259 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
22261 if (cp_lexer_next_token_is (parser->lexer,
22262 CPP_OPEN_PAREN))
22263 error ("parenthesized initialization is not allowed in "
22264 "OpenMP %<for%> loop");
22265 else
22266 /* Trigger an error. */
22267 cp_parser_require (parser, CPP_EQ, "%<=%>");
22269 init = error_mark_node;
22270 cp_parser_skip_to_end_of_statement (parser);
22272 else if (CLASS_TYPE_P (TREE_TYPE (decl))
22273 || type_dependent_expression_p (decl)
22274 || auto_node)
22276 bool is_direct_init, is_non_constant_init;
22278 init = cp_parser_initializer (parser,
22279 &is_direct_init,
22280 &is_non_constant_init);
22282 if (auto_node && describable_type (init))
22284 TREE_TYPE (decl)
22285 = do_auto_deduction (TREE_TYPE (decl), init,
22286 auto_node);
22288 if (!CLASS_TYPE_P (TREE_TYPE (decl))
22289 && !type_dependent_expression_p (decl))
22290 goto non_class;
22293 cp_finish_decl (decl, init, !is_non_constant_init,
22294 asm_specification,
22295 LOOKUP_ONLYCONVERTING);
22296 if (CLASS_TYPE_P (TREE_TYPE (decl)))
22298 for_block
22299 = tree_cons (NULL, this_pre_body, for_block);
22300 init = NULL_TREE;
22302 else
22303 init = pop_stmt_list (this_pre_body);
22304 this_pre_body = NULL_TREE;
22306 else
22308 /* Consume '='. */
22309 cp_lexer_consume_token (parser->lexer);
22310 init = cp_parser_assignment_expression (parser, false, NULL);
22312 non_class:
22313 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
22314 init = error_mark_node;
22315 else
22316 cp_finish_decl (decl, NULL_TREE,
22317 /*init_const_expr_p=*/false,
22318 asm_specification,
22319 LOOKUP_ONLYCONVERTING);
22322 if (pushed_scope)
22323 pop_scope (pushed_scope);
22326 else
22328 cp_id_kind idk;
22329 /* If parsing a type specifier sequence failed, then
22330 this MUST be a simple expression. */
22331 cp_parser_parse_tentatively (parser);
22332 decl = cp_parser_primary_expression (parser, false, false,
22333 false, &idk);
22334 if (!cp_parser_error_occurred (parser)
22335 && decl
22336 && DECL_P (decl)
22337 && CLASS_TYPE_P (TREE_TYPE (decl)))
22339 tree rhs;
22341 cp_parser_parse_definitely (parser);
22342 cp_parser_require (parser, CPP_EQ, "%<=%>");
22343 rhs = cp_parser_assignment_expression (parser, false, NULL);
22344 finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
22345 rhs,
22346 tf_warning_or_error));
22347 add_private_clause = true;
22349 else
22351 decl = NULL;
22352 cp_parser_abort_tentative_parse (parser);
22353 init = cp_parser_expression (parser, false, NULL);
22354 if (init)
22356 if (TREE_CODE (init) == MODIFY_EXPR
22357 || TREE_CODE (init) == MODOP_EXPR)
22358 real_decl = TREE_OPERAND (init, 0);
22363 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
22364 if (this_pre_body)
22366 this_pre_body = pop_stmt_list (this_pre_body);
22367 if (pre_body)
22369 tree t = pre_body;
22370 pre_body = push_stmt_list ();
22371 add_stmt (t);
22372 add_stmt (this_pre_body);
22373 pre_body = pop_stmt_list (pre_body);
22375 else
22376 pre_body = this_pre_body;
22379 if (decl)
22380 real_decl = decl;
22381 if (par_clauses != NULL && real_decl != NULL_TREE)
22383 tree *c;
22384 for (c = par_clauses; *c ; )
22385 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
22386 && OMP_CLAUSE_DECL (*c) == real_decl)
22388 error_at (loc, "iteration variable %qD"
22389 " should not be firstprivate", real_decl);
22390 *c = OMP_CLAUSE_CHAIN (*c);
22392 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
22393 && OMP_CLAUSE_DECL (*c) == real_decl)
22395 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
22396 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
22397 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
22398 OMP_CLAUSE_DECL (l) = real_decl;
22399 OMP_CLAUSE_CHAIN (l) = clauses;
22400 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
22401 clauses = l;
22402 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
22403 CP_OMP_CLAUSE_INFO (*c) = NULL;
22404 add_private_clause = false;
22406 else
22408 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
22409 && OMP_CLAUSE_DECL (*c) == real_decl)
22410 add_private_clause = false;
22411 c = &OMP_CLAUSE_CHAIN (*c);
22415 if (add_private_clause)
22417 tree c;
22418 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
22420 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
22421 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
22422 && OMP_CLAUSE_DECL (c) == decl)
22423 break;
22424 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
22425 && OMP_CLAUSE_DECL (c) == decl)
22426 error_at (loc, "iteration variable %qD "
22427 "should not be firstprivate",
22428 decl);
22429 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
22430 && OMP_CLAUSE_DECL (c) == decl)
22431 error_at (loc, "iteration variable %qD should not be reduction",
22432 decl);
22434 if (c == NULL)
22436 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
22437 OMP_CLAUSE_DECL (c) = decl;
22438 c = finish_omp_clauses (c);
22439 if (c)
22441 OMP_CLAUSE_CHAIN (c) = clauses;
22442 clauses = c;
22447 cond = NULL;
22448 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22449 cond = cp_parser_omp_for_cond (parser, decl);
22450 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
22452 incr = NULL;
22453 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
22455 /* If decl is an iterator, preserve the operator on decl
22456 until finish_omp_for. */
22457 if (decl
22458 && (type_dependent_expression_p (decl)
22459 || CLASS_TYPE_P (TREE_TYPE (decl))))
22460 incr = cp_parser_omp_for_incr (parser, decl);
22461 else
22462 incr = cp_parser_expression (parser, false, NULL);
22465 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
22466 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22467 /*or_comma=*/false,
22468 /*consume_paren=*/true);
22470 TREE_VEC_ELT (declv, i) = decl;
22471 TREE_VEC_ELT (initv, i) = init;
22472 TREE_VEC_ELT (condv, i) = cond;
22473 TREE_VEC_ELT (incrv, i) = incr;
22475 if (i == collapse - 1)
22476 break;
22478 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
22479 in between the collapsed for loops to be still considered perfectly
22480 nested. Hopefully the final version clarifies this.
22481 For now handle (multiple) {'s and empty statements. */
22482 cp_parser_parse_tentatively (parser);
22485 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22486 break;
22487 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22489 cp_lexer_consume_token (parser->lexer);
22490 bracecount++;
22492 else if (bracecount
22493 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22494 cp_lexer_consume_token (parser->lexer);
22495 else
22497 loc = cp_lexer_peek_token (parser->lexer)->location;
22498 error_at (loc, "not enough collapsed for loops");
22499 collapse_err = true;
22500 cp_parser_abort_tentative_parse (parser);
22501 declv = NULL_TREE;
22502 break;
22505 while (1);
22507 if (declv)
22509 cp_parser_parse_definitely (parser);
22510 nbraces += bracecount;
22514 /* Note that we saved the original contents of this flag when we entered
22515 the structured block, and so we don't need to re-save it here. */
22516 parser->in_statement = IN_OMP_FOR;
22518 /* Note that the grammar doesn't call for a structured block here,
22519 though the loop as a whole is a structured block. */
22520 body = push_stmt_list ();
22521 cp_parser_statement (parser, NULL_TREE, false, NULL);
22522 body = pop_stmt_list (body);
22524 if (declv == NULL_TREE)
22525 ret = NULL_TREE;
22526 else
22527 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
22528 pre_body, clauses);
22530 while (nbraces)
22532 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
22534 cp_lexer_consume_token (parser->lexer);
22535 nbraces--;
22537 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22538 cp_lexer_consume_token (parser->lexer);
22539 else
22541 if (!collapse_err)
22543 error_at (cp_lexer_peek_token (parser->lexer)->location,
22544 "collapsed loops not perfectly nested");
22546 collapse_err = true;
22547 cp_parser_statement_seq_opt (parser, NULL);
22548 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
22549 break;
22553 while (for_block)
22555 add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
22556 for_block = TREE_CHAIN (for_block);
22559 return ret;
22562 /* OpenMP 2.5:
22563 #pragma omp for for-clause[optseq] new-line
22564 for-loop */
22566 #define OMP_FOR_CLAUSE_MASK \
22567 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22568 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22569 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
22570 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
22571 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
22572 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
22573 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
22574 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
22576 static tree
22577 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
22579 tree clauses, sb, ret;
22580 unsigned int save;
22582 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
22583 "#pragma omp for", pragma_tok);
22585 sb = begin_omp_structured_block ();
22586 save = cp_parser_begin_omp_structured_block (parser);
22588 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
22590 cp_parser_end_omp_structured_block (parser, save);
22591 add_stmt (finish_omp_structured_block (sb));
22593 return ret;
22596 /* OpenMP 2.5:
22597 # pragma omp master new-line
22598 structured-block */
22600 static tree
22601 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
22603 cp_parser_require_pragma_eol (parser, pragma_tok);
22604 return c_finish_omp_master (input_location,
22605 cp_parser_omp_structured_block (parser));
22608 /* OpenMP 2.5:
22609 # pragma omp ordered new-line
22610 structured-block */
22612 static tree
22613 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
22615 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22616 cp_parser_require_pragma_eol (parser, pragma_tok);
22617 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
22620 /* OpenMP 2.5:
22622 section-scope:
22623 { section-sequence }
22625 section-sequence:
22626 section-directive[opt] structured-block
22627 section-sequence section-directive structured-block */
22629 static tree
22630 cp_parser_omp_sections_scope (cp_parser *parser)
22632 tree stmt, substmt;
22633 bool error_suppress = false;
22634 cp_token *tok;
22636 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
22637 return NULL_TREE;
22639 stmt = push_stmt_list ();
22641 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
22643 unsigned save;
22645 substmt = begin_omp_structured_block ();
22646 save = cp_parser_begin_omp_structured_block (parser);
22648 while (1)
22650 cp_parser_statement (parser, NULL_TREE, false, NULL);
22652 tok = cp_lexer_peek_token (parser->lexer);
22653 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
22654 break;
22655 if (tok->type == CPP_CLOSE_BRACE)
22656 break;
22657 if (tok->type == CPP_EOF)
22658 break;
22661 cp_parser_end_omp_structured_block (parser, save);
22662 substmt = finish_omp_structured_block (substmt);
22663 substmt = build1 (OMP_SECTION, void_type_node, substmt);
22664 add_stmt (substmt);
22667 while (1)
22669 tok = cp_lexer_peek_token (parser->lexer);
22670 if (tok->type == CPP_CLOSE_BRACE)
22671 break;
22672 if (tok->type == CPP_EOF)
22673 break;
22675 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
22677 cp_lexer_consume_token (parser->lexer);
22678 cp_parser_require_pragma_eol (parser, tok);
22679 error_suppress = false;
22681 else if (!error_suppress)
22683 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
22684 error_suppress = true;
22687 substmt = cp_parser_omp_structured_block (parser);
22688 substmt = build1 (OMP_SECTION, void_type_node, substmt);
22689 add_stmt (substmt);
22691 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
22693 substmt = pop_stmt_list (stmt);
22695 stmt = make_node (OMP_SECTIONS);
22696 TREE_TYPE (stmt) = void_type_node;
22697 OMP_SECTIONS_BODY (stmt) = substmt;
22699 add_stmt (stmt);
22700 return stmt;
22703 /* OpenMP 2.5:
22704 # pragma omp sections sections-clause[optseq] newline
22705 sections-scope */
22707 #define OMP_SECTIONS_CLAUSE_MASK \
22708 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22709 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22710 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
22711 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
22712 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
22714 static tree
22715 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
22717 tree clauses, ret;
22719 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
22720 "#pragma omp sections", pragma_tok);
22722 ret = cp_parser_omp_sections_scope (parser);
22723 if (ret)
22724 OMP_SECTIONS_CLAUSES (ret) = clauses;
22726 return ret;
22729 /* OpenMP 2.5:
22730 # pragma parallel parallel-clause new-line
22731 # pragma parallel for parallel-for-clause new-line
22732 # pragma parallel sections parallel-sections-clause new-line */
22734 #define OMP_PARALLEL_CLAUSE_MASK \
22735 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
22736 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22737 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22738 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
22739 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
22740 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
22741 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
22742 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
22744 static tree
22745 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
22747 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
22748 const char *p_name = "#pragma omp parallel";
22749 tree stmt, clauses, par_clause, ws_clause, block;
22750 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
22751 unsigned int save;
22752 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22754 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22756 cp_lexer_consume_token (parser->lexer);
22757 p_kind = PRAGMA_OMP_PARALLEL_FOR;
22758 p_name = "#pragma omp parallel for";
22759 mask |= OMP_FOR_CLAUSE_MASK;
22760 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
22762 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22764 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
22765 const char *p = IDENTIFIER_POINTER (id);
22766 if (strcmp (p, "sections") == 0)
22768 cp_lexer_consume_token (parser->lexer);
22769 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
22770 p_name = "#pragma omp parallel sections";
22771 mask |= OMP_SECTIONS_CLAUSE_MASK;
22772 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
22776 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
22777 block = begin_omp_parallel ();
22778 save = cp_parser_begin_omp_structured_block (parser);
22780 switch (p_kind)
22782 case PRAGMA_OMP_PARALLEL:
22783 cp_parser_statement (parser, NULL_TREE, false, NULL);
22784 par_clause = clauses;
22785 break;
22787 case PRAGMA_OMP_PARALLEL_FOR:
22788 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
22789 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
22790 break;
22792 case PRAGMA_OMP_PARALLEL_SECTIONS:
22793 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
22794 stmt = cp_parser_omp_sections_scope (parser);
22795 if (stmt)
22796 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
22797 break;
22799 default:
22800 gcc_unreachable ();
22803 cp_parser_end_omp_structured_block (parser, save);
22804 stmt = finish_omp_parallel (par_clause, block);
22805 if (p_kind != PRAGMA_OMP_PARALLEL)
22806 OMP_PARALLEL_COMBINED (stmt) = 1;
22807 return stmt;
22810 /* OpenMP 2.5:
22811 # pragma omp single single-clause[optseq] new-line
22812 structured-block */
22814 #define OMP_SINGLE_CLAUSE_MASK \
22815 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22816 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22817 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
22818 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
22820 static tree
22821 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
22823 tree stmt = make_node (OMP_SINGLE);
22824 TREE_TYPE (stmt) = void_type_node;
22826 OMP_SINGLE_CLAUSES (stmt)
22827 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
22828 "#pragma omp single", pragma_tok);
22829 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
22831 return add_stmt (stmt);
22834 /* OpenMP 3.0:
22835 # pragma omp task task-clause[optseq] new-line
22836 structured-block */
22838 #define OMP_TASK_CLAUSE_MASK \
22839 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
22840 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
22841 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
22842 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22843 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22844 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
22846 static tree
22847 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
22849 tree clauses, block;
22850 unsigned int save;
22852 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
22853 "#pragma omp task", pragma_tok);
22854 block = begin_omp_task ();
22855 save = cp_parser_begin_omp_structured_block (parser);
22856 cp_parser_statement (parser, NULL_TREE, false, NULL);
22857 cp_parser_end_omp_structured_block (parser, save);
22858 return finish_omp_task (clauses, block);
22861 /* OpenMP 3.0:
22862 # pragma omp taskwait new-line */
22864 static void
22865 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
22867 cp_parser_require_pragma_eol (parser, pragma_tok);
22868 finish_omp_taskwait ();
22871 /* OpenMP 2.5:
22872 # pragma omp threadprivate (variable-list) */
22874 static void
22875 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
22877 tree vars;
22879 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
22880 cp_parser_require_pragma_eol (parser, pragma_tok);
22882 finish_omp_threadprivate (vars);
22885 /* Main entry point to OpenMP statement pragmas. */
22887 static void
22888 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
22890 tree stmt;
22892 switch (pragma_tok->pragma_kind)
22894 case PRAGMA_OMP_ATOMIC:
22895 cp_parser_omp_atomic (parser, pragma_tok);
22896 return;
22897 case PRAGMA_OMP_CRITICAL:
22898 stmt = cp_parser_omp_critical (parser, pragma_tok);
22899 break;
22900 case PRAGMA_OMP_FOR:
22901 stmt = cp_parser_omp_for (parser, pragma_tok);
22902 break;
22903 case PRAGMA_OMP_MASTER:
22904 stmt = cp_parser_omp_master (parser, pragma_tok);
22905 break;
22906 case PRAGMA_OMP_ORDERED:
22907 stmt = cp_parser_omp_ordered (parser, pragma_tok);
22908 break;
22909 case PRAGMA_OMP_PARALLEL:
22910 stmt = cp_parser_omp_parallel (parser, pragma_tok);
22911 break;
22912 case PRAGMA_OMP_SECTIONS:
22913 stmt = cp_parser_omp_sections (parser, pragma_tok);
22914 break;
22915 case PRAGMA_OMP_SINGLE:
22916 stmt = cp_parser_omp_single (parser, pragma_tok);
22917 break;
22918 case PRAGMA_OMP_TASK:
22919 stmt = cp_parser_omp_task (parser, pragma_tok);
22920 break;
22921 default:
22922 gcc_unreachable ();
22925 if (stmt)
22926 SET_EXPR_LOCATION (stmt, pragma_tok->location);
22929 /* The parser. */
22931 static GTY (()) cp_parser *the_parser;
22934 /* Special handling for the first token or line in the file. The first
22935 thing in the file might be #pragma GCC pch_preprocess, which loads a
22936 PCH file, which is a GC collection point. So we need to handle this
22937 first pragma without benefit of an existing lexer structure.
22939 Always returns one token to the caller in *FIRST_TOKEN. This is
22940 either the true first token of the file, or the first token after
22941 the initial pragma. */
22943 static void
22944 cp_parser_initial_pragma (cp_token *first_token)
22946 tree name = NULL;
22948 cp_lexer_get_preprocessor_token (NULL, first_token);
22949 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
22950 return;
22952 cp_lexer_get_preprocessor_token (NULL, first_token);
22953 if (first_token->type == CPP_STRING)
22955 name = first_token->u.value;
22957 cp_lexer_get_preprocessor_token (NULL, first_token);
22958 if (first_token->type != CPP_PRAGMA_EOL)
22959 error_at (first_token->location,
22960 "junk at end of %<#pragma GCC pch_preprocess%>");
22962 else
22963 error_at (first_token->location, "expected string literal");
22965 /* Skip to the end of the pragma. */
22966 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
22967 cp_lexer_get_preprocessor_token (NULL, first_token);
22969 /* Now actually load the PCH file. */
22970 if (name)
22971 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
22973 /* Read one more token to return to our caller. We have to do this
22974 after reading the PCH file in, since its pointers have to be
22975 live. */
22976 cp_lexer_get_preprocessor_token (NULL, first_token);
22979 /* Normal parsing of a pragma token. Here we can (and must) use the
22980 regular lexer. */
22982 static bool
22983 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
22985 cp_token *pragma_tok;
22986 unsigned int id;
22988 pragma_tok = cp_lexer_consume_token (parser->lexer);
22989 gcc_assert (pragma_tok->type == CPP_PRAGMA);
22990 parser->lexer->in_pragma = true;
22992 id = pragma_tok->pragma_kind;
22993 switch (id)
22995 case PRAGMA_GCC_PCH_PREPROCESS:
22996 error_at (pragma_tok->location,
22997 "%<#pragma GCC pch_preprocess%> must be first");
22998 break;
23000 case PRAGMA_OMP_BARRIER:
23001 switch (context)
23003 case pragma_compound:
23004 cp_parser_omp_barrier (parser, pragma_tok);
23005 return false;
23006 case pragma_stmt:
23007 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
23008 "used in compound statements");
23009 break;
23010 default:
23011 goto bad_stmt;
23013 break;
23015 case PRAGMA_OMP_FLUSH:
23016 switch (context)
23018 case pragma_compound:
23019 cp_parser_omp_flush (parser, pragma_tok);
23020 return false;
23021 case pragma_stmt:
23022 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
23023 "used in compound statements");
23024 break;
23025 default:
23026 goto bad_stmt;
23028 break;
23030 case PRAGMA_OMP_TASKWAIT:
23031 switch (context)
23033 case pragma_compound:
23034 cp_parser_omp_taskwait (parser, pragma_tok);
23035 return false;
23036 case pragma_stmt:
23037 error_at (pragma_tok->location,
23038 "%<#pragma omp taskwait%> may only be "
23039 "used in compound statements");
23040 break;
23041 default:
23042 goto bad_stmt;
23044 break;
23046 case PRAGMA_OMP_THREADPRIVATE:
23047 cp_parser_omp_threadprivate (parser, pragma_tok);
23048 return false;
23050 case PRAGMA_OMP_ATOMIC:
23051 case PRAGMA_OMP_CRITICAL:
23052 case PRAGMA_OMP_FOR:
23053 case PRAGMA_OMP_MASTER:
23054 case PRAGMA_OMP_ORDERED:
23055 case PRAGMA_OMP_PARALLEL:
23056 case PRAGMA_OMP_SECTIONS:
23057 case PRAGMA_OMP_SINGLE:
23058 case PRAGMA_OMP_TASK:
23059 if (context == pragma_external)
23060 goto bad_stmt;
23061 cp_parser_omp_construct (parser, pragma_tok);
23062 return true;
23064 case PRAGMA_OMP_SECTION:
23065 error_at (pragma_tok->location,
23066 "%<#pragma omp section%> may only be used in "
23067 "%<#pragma omp sections%> construct");
23068 break;
23070 default:
23071 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
23072 c_invoke_pragma_handler (id);
23073 break;
23075 bad_stmt:
23076 cp_parser_error (parser, "expected declaration specifiers");
23077 break;
23080 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
23081 return false;
23084 /* The interface the pragma parsers have to the lexer. */
23086 enum cpp_ttype
23087 pragma_lex (tree *value)
23089 cp_token *tok;
23090 enum cpp_ttype ret;
23092 tok = cp_lexer_peek_token (the_parser->lexer);
23094 ret = tok->type;
23095 *value = tok->u.value;
23097 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
23098 ret = CPP_EOF;
23099 else if (ret == CPP_STRING)
23100 *value = cp_parser_string_literal (the_parser, false, false);
23101 else
23103 cp_lexer_consume_token (the_parser->lexer);
23104 if (ret == CPP_KEYWORD)
23105 ret = CPP_NAME;
23108 return ret;
23112 /* External interface. */
23114 /* Parse one entire translation unit. */
23116 void
23117 c_parse_file (void)
23119 bool error_occurred;
23120 static bool already_called = false;
23122 if (already_called)
23124 sorry ("inter-module optimizations not implemented for C++");
23125 return;
23127 already_called = true;
23129 the_parser = cp_parser_new ();
23130 push_deferring_access_checks (flag_access_control
23131 ? dk_no_deferred : dk_no_check);
23132 error_occurred = cp_parser_translation_unit (the_parser);
23133 the_parser = NULL;
23136 #include "gt-cp-parser.h"