2009-08-24 Steven G. Kargl <kargl@gcc.gnu.org>
[official-gcc.git] / gcc / cp / parser.c
bloba3e9f0ebebcdc987ece171995012e59e6fd2069d
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_RAW_STRINGS);
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 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
796 break;
798 default:
799 break;
803 /* Start emitting debugging information. */
805 static void
806 cp_lexer_start_debugging (cp_lexer* lexer)
808 lexer->debugging_p = true;
811 /* Stop emitting debugging information. */
813 static void
814 cp_lexer_stop_debugging (cp_lexer* lexer)
816 lexer->debugging_p = false;
819 #endif /* ENABLE_CHECKING */
821 /* Create a new cp_token_cache, representing a range of tokens. */
823 static cp_token_cache *
824 cp_token_cache_new (cp_token *first, cp_token *last)
826 cp_token_cache *cache = GGC_NEW (cp_token_cache);
827 cache->first = first;
828 cache->last = last;
829 return cache;
833 /* Decl-specifiers. */
835 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
837 static void
838 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
840 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
843 /* Declarators. */
845 /* Nothing other than the parser should be creating declarators;
846 declarators are a semi-syntactic representation of C++ entities.
847 Other parts of the front end that need to create entities (like
848 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
850 static cp_declarator *make_call_declarator
851 (cp_declarator *, tree, cp_cv_quals, tree, tree);
852 static cp_declarator *make_array_declarator
853 (cp_declarator *, tree);
854 static cp_declarator *make_pointer_declarator
855 (cp_cv_quals, cp_declarator *);
856 static cp_declarator *make_reference_declarator
857 (cp_cv_quals, cp_declarator *, bool);
858 static cp_parameter_declarator *make_parameter_declarator
859 (cp_decl_specifier_seq *, cp_declarator *, tree);
860 static cp_declarator *make_ptrmem_declarator
861 (cp_cv_quals, tree, cp_declarator *);
863 /* An erroneous declarator. */
864 static cp_declarator *cp_error_declarator;
866 /* The obstack on which declarators and related data structures are
867 allocated. */
868 static struct obstack declarator_obstack;
870 /* Alloc BYTES from the declarator memory pool. */
872 static inline void *
873 alloc_declarator (size_t bytes)
875 return obstack_alloc (&declarator_obstack, bytes);
878 /* Allocate a declarator of the indicated KIND. Clear fields that are
879 common to all declarators. */
881 static cp_declarator *
882 make_declarator (cp_declarator_kind kind)
884 cp_declarator *declarator;
886 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
887 declarator->kind = kind;
888 declarator->attributes = NULL_TREE;
889 declarator->declarator = NULL;
890 declarator->parameter_pack_p = false;
892 return declarator;
895 /* Make a declarator for a generalized identifier. If
896 QUALIFYING_SCOPE is non-NULL, the identifier is
897 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
898 UNQUALIFIED_NAME. SFK indicates the kind of special function this
899 is, if any. */
901 static cp_declarator *
902 make_id_declarator (tree qualifying_scope, tree unqualified_name,
903 special_function_kind sfk)
905 cp_declarator *declarator;
907 /* It is valid to write:
909 class C { void f(); };
910 typedef C D;
911 void D::f();
913 The standard is not clear about whether `typedef const C D' is
914 legal; as of 2002-09-15 the committee is considering that
915 question. EDG 3.0 allows that syntax. Therefore, we do as
916 well. */
917 if (qualifying_scope && TYPE_P (qualifying_scope))
918 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
920 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
921 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
922 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
924 declarator = make_declarator (cdk_id);
925 declarator->u.id.qualifying_scope = qualifying_scope;
926 declarator->u.id.unqualified_name = unqualified_name;
927 declarator->u.id.sfk = sfk;
929 return declarator;
932 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
933 of modifiers such as const or volatile to apply to the pointer
934 type, represented as identifiers. */
936 cp_declarator *
937 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
939 cp_declarator *declarator;
941 declarator = make_declarator (cdk_pointer);
942 declarator->declarator = target;
943 declarator->u.pointer.qualifiers = cv_qualifiers;
944 declarator->u.pointer.class_type = NULL_TREE;
945 if (target)
947 declarator->parameter_pack_p = target->parameter_pack_p;
948 target->parameter_pack_p = false;
950 else
951 declarator->parameter_pack_p = false;
953 return declarator;
956 /* Like make_pointer_declarator -- but for references. */
958 cp_declarator *
959 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
960 bool rvalue_ref)
962 cp_declarator *declarator;
964 declarator = make_declarator (cdk_reference);
965 declarator->declarator = target;
966 declarator->u.reference.qualifiers = cv_qualifiers;
967 declarator->u.reference.rvalue_ref = rvalue_ref;
968 if (target)
970 declarator->parameter_pack_p = target->parameter_pack_p;
971 target->parameter_pack_p = false;
973 else
974 declarator->parameter_pack_p = false;
976 return declarator;
979 /* Like make_pointer_declarator -- but for a pointer to a non-static
980 member of CLASS_TYPE. */
982 cp_declarator *
983 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
984 cp_declarator *pointee)
986 cp_declarator *declarator;
988 declarator = make_declarator (cdk_ptrmem);
989 declarator->declarator = pointee;
990 declarator->u.pointer.qualifiers = cv_qualifiers;
991 declarator->u.pointer.class_type = class_type;
993 if (pointee)
995 declarator->parameter_pack_p = pointee->parameter_pack_p;
996 pointee->parameter_pack_p = false;
998 else
999 declarator->parameter_pack_p = false;
1001 return declarator;
1004 /* Make a declarator for the function given by TARGET, with the
1005 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1006 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1007 indicates what exceptions can be thrown. */
1009 cp_declarator *
1010 make_call_declarator (cp_declarator *target,
1011 tree parms,
1012 cp_cv_quals cv_qualifiers,
1013 tree exception_specification,
1014 tree late_return_type)
1016 cp_declarator *declarator;
1018 declarator = make_declarator (cdk_function);
1019 declarator->declarator = target;
1020 declarator->u.function.parameters = parms;
1021 declarator->u.function.qualifiers = cv_qualifiers;
1022 declarator->u.function.exception_specification = exception_specification;
1023 declarator->u.function.late_return_type = late_return_type;
1024 if (target)
1026 declarator->parameter_pack_p = target->parameter_pack_p;
1027 target->parameter_pack_p = false;
1029 else
1030 declarator->parameter_pack_p = false;
1032 return declarator;
1035 /* Make a declarator for an array of BOUNDS elements, each of which is
1036 defined by ELEMENT. */
1038 cp_declarator *
1039 make_array_declarator (cp_declarator *element, tree bounds)
1041 cp_declarator *declarator;
1043 declarator = make_declarator (cdk_array);
1044 declarator->declarator = element;
1045 declarator->u.array.bounds = bounds;
1046 if (element)
1048 declarator->parameter_pack_p = element->parameter_pack_p;
1049 element->parameter_pack_p = false;
1051 else
1052 declarator->parameter_pack_p = false;
1054 return declarator;
1057 /* Determine whether the declarator we've seen so far can be a
1058 parameter pack, when followed by an ellipsis. */
1059 static bool
1060 declarator_can_be_parameter_pack (cp_declarator *declarator)
1062 /* Search for a declarator name, or any other declarator that goes
1063 after the point where the ellipsis could appear in a parameter
1064 pack. If we find any of these, then this declarator can not be
1065 made into a parameter pack. */
1066 bool found = false;
1067 while (declarator && !found)
1069 switch ((int)declarator->kind)
1071 case cdk_id:
1072 case cdk_array:
1073 found = true;
1074 break;
1076 case cdk_error:
1077 return true;
1079 default:
1080 declarator = declarator->declarator;
1081 break;
1085 return !found;
1088 cp_parameter_declarator *no_parameters;
1090 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1091 DECLARATOR and DEFAULT_ARGUMENT. */
1093 cp_parameter_declarator *
1094 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1095 cp_declarator *declarator,
1096 tree default_argument)
1098 cp_parameter_declarator *parameter;
1100 parameter = ((cp_parameter_declarator *)
1101 alloc_declarator (sizeof (cp_parameter_declarator)));
1102 parameter->next = NULL;
1103 if (decl_specifiers)
1104 parameter->decl_specifiers = *decl_specifiers;
1105 else
1106 clear_decl_specs (&parameter->decl_specifiers);
1107 parameter->declarator = declarator;
1108 parameter->default_argument = default_argument;
1109 parameter->ellipsis_p = false;
1111 return parameter;
1114 /* Returns true iff DECLARATOR is a declaration for a function. */
1116 static bool
1117 function_declarator_p (const cp_declarator *declarator)
1119 while (declarator)
1121 if (declarator->kind == cdk_function
1122 && declarator->declarator->kind == cdk_id)
1123 return true;
1124 if (declarator->kind == cdk_id
1125 || declarator->kind == cdk_error)
1126 return false;
1127 declarator = declarator->declarator;
1129 return false;
1132 /* The parser. */
1134 /* Overview
1135 --------
1137 A cp_parser parses the token stream as specified by the C++
1138 grammar. Its job is purely parsing, not semantic analysis. For
1139 example, the parser breaks the token stream into declarators,
1140 expressions, statements, and other similar syntactic constructs.
1141 It does not check that the types of the expressions on either side
1142 of an assignment-statement are compatible, or that a function is
1143 not declared with a parameter of type `void'.
1145 The parser invokes routines elsewhere in the compiler to perform
1146 semantic analysis and to build up the abstract syntax tree for the
1147 code processed.
1149 The parser (and the template instantiation code, which is, in a
1150 way, a close relative of parsing) are the only parts of the
1151 compiler that should be calling push_scope and pop_scope, or
1152 related functions. The parser (and template instantiation code)
1153 keeps track of what scope is presently active; everything else
1154 should simply honor that. (The code that generates static
1155 initializers may also need to set the scope, in order to check
1156 access control correctly when emitting the initializers.)
1158 Methodology
1159 -----------
1161 The parser is of the standard recursive-descent variety. Upcoming
1162 tokens in the token stream are examined in order to determine which
1163 production to use when parsing a non-terminal. Some C++ constructs
1164 require arbitrary look ahead to disambiguate. For example, it is
1165 impossible, in the general case, to tell whether a statement is an
1166 expression or declaration without scanning the entire statement.
1167 Therefore, the parser is capable of "parsing tentatively." When the
1168 parser is not sure what construct comes next, it enters this mode.
1169 Then, while we attempt to parse the construct, the parser queues up
1170 error messages, rather than issuing them immediately, and saves the
1171 tokens it consumes. If the construct is parsed successfully, the
1172 parser "commits", i.e., it issues any queued error messages and
1173 the tokens that were being preserved are permanently discarded.
1174 If, however, the construct is not parsed successfully, the parser
1175 rolls back its state completely so that it can resume parsing using
1176 a different alternative.
1178 Future Improvements
1179 -------------------
1181 The performance of the parser could probably be improved substantially.
1182 We could often eliminate the need to parse tentatively by looking ahead
1183 a little bit. In some places, this approach might not entirely eliminate
1184 the need to parse tentatively, but it might still speed up the average
1185 case. */
1187 /* Flags that are passed to some parsing functions. These values can
1188 be bitwise-ored together. */
1190 enum
1192 /* No flags. */
1193 CP_PARSER_FLAGS_NONE = 0x0,
1194 /* The construct is optional. If it is not present, then no error
1195 should be issued. */
1196 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1197 /* When parsing a type-specifier, do not allow user-defined types. */
1198 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1201 /* This type is used for parameters and variables which hold
1202 combinations of the above flags. */
1203 typedef int cp_parser_flags;
1205 /* The different kinds of declarators we want to parse. */
1207 typedef enum cp_parser_declarator_kind
1209 /* We want an abstract declarator. */
1210 CP_PARSER_DECLARATOR_ABSTRACT,
1211 /* We want a named declarator. */
1212 CP_PARSER_DECLARATOR_NAMED,
1213 /* We don't mind, but the name must be an unqualified-id. */
1214 CP_PARSER_DECLARATOR_EITHER
1215 } cp_parser_declarator_kind;
1217 /* The precedence values used to parse binary expressions. The minimum value
1218 of PREC must be 1, because zero is reserved to quickly discriminate
1219 binary operators from other tokens. */
1221 enum cp_parser_prec
1223 PREC_NOT_OPERATOR,
1224 PREC_LOGICAL_OR_EXPRESSION,
1225 PREC_LOGICAL_AND_EXPRESSION,
1226 PREC_INCLUSIVE_OR_EXPRESSION,
1227 PREC_EXCLUSIVE_OR_EXPRESSION,
1228 PREC_AND_EXPRESSION,
1229 PREC_EQUALITY_EXPRESSION,
1230 PREC_RELATIONAL_EXPRESSION,
1231 PREC_SHIFT_EXPRESSION,
1232 PREC_ADDITIVE_EXPRESSION,
1233 PREC_MULTIPLICATIVE_EXPRESSION,
1234 PREC_PM_EXPRESSION,
1235 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1238 /* A mapping from a token type to a corresponding tree node type, with a
1239 precedence value. */
1241 typedef struct cp_parser_binary_operations_map_node
1243 /* The token type. */
1244 enum cpp_ttype token_type;
1245 /* The corresponding tree code. */
1246 enum tree_code tree_type;
1247 /* The precedence of this operator. */
1248 enum cp_parser_prec prec;
1249 } cp_parser_binary_operations_map_node;
1251 /* The status of a tentative parse. */
1253 typedef enum cp_parser_status_kind
1255 /* No errors have occurred. */
1256 CP_PARSER_STATUS_KIND_NO_ERROR,
1257 /* An error has occurred. */
1258 CP_PARSER_STATUS_KIND_ERROR,
1259 /* We are committed to this tentative parse, whether or not an error
1260 has occurred. */
1261 CP_PARSER_STATUS_KIND_COMMITTED
1262 } cp_parser_status_kind;
1264 typedef struct cp_parser_expression_stack_entry
1266 /* Left hand side of the binary operation we are currently
1267 parsing. */
1268 tree lhs;
1269 /* Original tree code for left hand side, if it was a binary
1270 expression itself (used for -Wparentheses). */
1271 enum tree_code lhs_type;
1272 /* Tree code for the binary operation we are parsing. */
1273 enum tree_code tree_type;
1274 /* Precedence of the binary operation we are parsing. */
1275 enum cp_parser_prec prec;
1276 } cp_parser_expression_stack_entry;
1278 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1279 entries because precedence levels on the stack are monotonically
1280 increasing. */
1281 typedef struct cp_parser_expression_stack_entry
1282 cp_parser_expression_stack[NUM_PREC_VALUES];
1284 /* Context that is saved and restored when parsing tentatively. */
1285 typedef struct GTY (()) cp_parser_context {
1286 /* If this is a tentative parsing context, the status of the
1287 tentative parse. */
1288 enum cp_parser_status_kind status;
1289 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1290 that are looked up in this context must be looked up both in the
1291 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1292 the context of the containing expression. */
1293 tree object_type;
1295 /* The next parsing context in the stack. */
1296 struct cp_parser_context *next;
1297 } cp_parser_context;
1299 /* Prototypes. */
1301 /* Constructors and destructors. */
1303 static cp_parser_context *cp_parser_context_new
1304 (cp_parser_context *);
1306 /* Class variables. */
1308 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1310 /* The operator-precedence table used by cp_parser_binary_expression.
1311 Transformed into an associative array (binops_by_token) by
1312 cp_parser_new. */
1314 static const cp_parser_binary_operations_map_node binops[] = {
1315 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1316 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1318 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1319 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1320 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1322 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1323 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1325 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1326 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1328 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1329 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1330 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1331 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1333 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1334 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1336 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1338 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1340 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1342 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1344 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1347 /* The same as binops, but initialized by cp_parser_new so that
1348 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1349 for speed. */
1350 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1352 /* Constructors and destructors. */
1354 /* Construct a new context. The context below this one on the stack
1355 is given by NEXT. */
1357 static cp_parser_context *
1358 cp_parser_context_new (cp_parser_context* next)
1360 cp_parser_context *context;
1362 /* Allocate the storage. */
1363 if (cp_parser_context_free_list != NULL)
1365 /* Pull the first entry from the free list. */
1366 context = cp_parser_context_free_list;
1367 cp_parser_context_free_list = context->next;
1368 memset (context, 0, sizeof (*context));
1370 else
1371 context = GGC_CNEW (cp_parser_context);
1373 /* No errors have occurred yet in this context. */
1374 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1375 /* If this is not the bottommost context, copy information that we
1376 need from the previous context. */
1377 if (next)
1379 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1380 expression, then we are parsing one in this context, too. */
1381 context->object_type = next->object_type;
1382 /* Thread the stack. */
1383 context->next = next;
1386 return context;
1389 /* The cp_parser structure represents the C++ parser. */
1391 typedef struct GTY(()) cp_parser {
1392 /* The lexer from which we are obtaining tokens. */
1393 cp_lexer *lexer;
1395 /* The scope in which names should be looked up. If NULL_TREE, then
1396 we look up names in the scope that is currently open in the
1397 source program. If non-NULL, this is either a TYPE or
1398 NAMESPACE_DECL for the scope in which we should look. It can
1399 also be ERROR_MARK, when we've parsed a bogus scope.
1401 This value is not cleared automatically after a name is looked
1402 up, so we must be careful to clear it before starting a new look
1403 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1404 will look up `Z' in the scope of `X', rather than the current
1405 scope.) Unfortunately, it is difficult to tell when name lookup
1406 is complete, because we sometimes peek at a token, look it up,
1407 and then decide not to consume it. */
1408 tree scope;
1410 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1411 last lookup took place. OBJECT_SCOPE is used if an expression
1412 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1413 respectively. QUALIFYING_SCOPE is used for an expression of the
1414 form "X::Y"; it refers to X. */
1415 tree object_scope;
1416 tree qualifying_scope;
1418 /* A stack of parsing contexts. All but the bottom entry on the
1419 stack will be tentative contexts.
1421 We parse tentatively in order to determine which construct is in
1422 use in some situations. For example, in order to determine
1423 whether a statement is an expression-statement or a
1424 declaration-statement we parse it tentatively as a
1425 declaration-statement. If that fails, we then reparse the same
1426 token stream as an expression-statement. */
1427 cp_parser_context *context;
1429 /* True if we are parsing GNU C++. If this flag is not set, then
1430 GNU extensions are not recognized. */
1431 bool allow_gnu_extensions_p;
1433 /* TRUE if the `>' token should be interpreted as the greater-than
1434 operator. FALSE if it is the end of a template-id or
1435 template-parameter-list. In C++0x mode, this flag also applies to
1436 `>>' tokens, which are viewed as two consecutive `>' tokens when
1437 this flag is FALSE. */
1438 bool greater_than_is_operator_p;
1440 /* TRUE if default arguments are allowed within a parameter list
1441 that starts at this point. FALSE if only a gnu extension makes
1442 them permissible. */
1443 bool default_arg_ok_p;
1445 /* TRUE if we are parsing an integral constant-expression. See
1446 [expr.const] for a precise definition. */
1447 bool integral_constant_expression_p;
1449 /* TRUE if we are parsing an integral constant-expression -- but a
1450 non-constant expression should be permitted as well. This flag
1451 is used when parsing an array bound so that GNU variable-length
1452 arrays are tolerated. */
1453 bool allow_non_integral_constant_expression_p;
1455 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1456 been seen that makes the expression non-constant. */
1457 bool non_integral_constant_expression_p;
1459 /* TRUE if local variable names and `this' are forbidden in the
1460 current context. */
1461 bool local_variables_forbidden_p;
1463 /* TRUE if the declaration we are parsing is part of a
1464 linkage-specification of the form `extern string-literal
1465 declaration'. */
1466 bool in_unbraced_linkage_specification_p;
1468 /* TRUE if we are presently parsing a declarator, after the
1469 direct-declarator. */
1470 bool in_declarator_p;
1472 /* TRUE if we are presently parsing a template-argument-list. */
1473 bool in_template_argument_list_p;
1475 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1476 to IN_OMP_BLOCK if parsing OpenMP structured block and
1477 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1478 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1479 iteration-statement, OpenMP block or loop within that switch. */
1480 #define IN_SWITCH_STMT 1
1481 #define IN_ITERATION_STMT 2
1482 #define IN_OMP_BLOCK 4
1483 #define IN_OMP_FOR 8
1484 #define IN_IF_STMT 16
1485 unsigned char in_statement;
1487 /* TRUE if we are presently parsing the body of a switch statement.
1488 Note that this doesn't quite overlap with in_statement above.
1489 The difference relates to giving the right sets of error messages:
1490 "case not in switch" vs "break statement used with OpenMP...". */
1491 bool in_switch_statement_p;
1493 /* TRUE if we are parsing a type-id in an expression context. In
1494 such a situation, both "type (expr)" and "type (type)" are valid
1495 alternatives. */
1496 bool in_type_id_in_expr_p;
1498 /* TRUE if we are currently in a header file where declarations are
1499 implicitly extern "C". */
1500 bool implicit_extern_c;
1502 /* TRUE if strings in expressions should be translated to the execution
1503 character set. */
1504 bool translate_strings_p;
1506 /* TRUE if we are presently parsing the body of a function, but not
1507 a local class. */
1508 bool in_function_body;
1510 /* If non-NULL, then we are parsing a construct where new type
1511 definitions are not permitted. The string stored here will be
1512 issued as an error message if a type is defined. */
1513 const char *type_definition_forbidden_message;
1515 /* A list of lists. The outer list is a stack, used for member
1516 functions of local classes. At each level there are two sub-list,
1517 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1518 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1519 TREE_VALUE's. The functions are chained in reverse declaration
1520 order.
1522 The TREE_PURPOSE sublist contains those functions with default
1523 arguments that need post processing, and the TREE_VALUE sublist
1524 contains those functions with definitions that need post
1525 processing.
1527 These lists can only be processed once the outermost class being
1528 defined is complete. */
1529 tree unparsed_functions_queues;
1531 /* The number of classes whose definitions are currently in
1532 progress. */
1533 unsigned num_classes_being_defined;
1535 /* The number of template parameter lists that apply directly to the
1536 current declaration. */
1537 unsigned num_template_parameter_lists;
1538 } cp_parser;
1540 /* Prototypes. */
1542 /* Constructors and destructors. */
1544 static cp_parser *cp_parser_new
1545 (void);
1547 /* Routines to parse various constructs.
1549 Those that return `tree' will return the error_mark_node (rather
1550 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1551 Sometimes, they will return an ordinary node if error-recovery was
1552 attempted, even though a parse error occurred. So, to check
1553 whether or not a parse error occurred, you should always use
1554 cp_parser_error_occurred. If the construct is optional (indicated
1555 either by an `_opt' in the name of the function that does the
1556 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1557 the construct is not present. */
1559 /* Lexical conventions [gram.lex] */
1561 static tree cp_parser_identifier
1562 (cp_parser *);
1563 static tree cp_parser_string_literal
1564 (cp_parser *, bool, bool);
1566 /* Basic concepts [gram.basic] */
1568 static bool cp_parser_translation_unit
1569 (cp_parser *);
1571 /* Expressions [gram.expr] */
1573 static tree cp_parser_primary_expression
1574 (cp_parser *, bool, bool, bool, cp_id_kind *);
1575 static tree cp_parser_id_expression
1576 (cp_parser *, bool, bool, bool *, bool, bool);
1577 static tree cp_parser_unqualified_id
1578 (cp_parser *, bool, bool, bool, bool);
1579 static tree cp_parser_nested_name_specifier_opt
1580 (cp_parser *, bool, bool, bool, bool);
1581 static tree cp_parser_nested_name_specifier
1582 (cp_parser *, bool, bool, bool, bool);
1583 static tree cp_parser_qualifying_entity
1584 (cp_parser *, bool, bool, bool, bool, bool);
1585 static tree cp_parser_postfix_expression
1586 (cp_parser *, bool, bool, bool, cp_id_kind *);
1587 static tree cp_parser_postfix_open_square_expression
1588 (cp_parser *, tree, bool);
1589 static tree cp_parser_postfix_dot_deref_expression
1590 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1591 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1592 (cp_parser *, bool, bool, bool, bool *);
1593 static void cp_parser_pseudo_destructor_name
1594 (cp_parser *, tree *, tree *);
1595 static tree cp_parser_unary_expression
1596 (cp_parser *, bool, bool, cp_id_kind *);
1597 static enum tree_code cp_parser_unary_operator
1598 (cp_token *);
1599 static tree cp_parser_new_expression
1600 (cp_parser *);
1601 static VEC(tree,gc) *cp_parser_new_placement
1602 (cp_parser *);
1603 static tree cp_parser_new_type_id
1604 (cp_parser *, tree *);
1605 static cp_declarator *cp_parser_new_declarator_opt
1606 (cp_parser *);
1607 static cp_declarator *cp_parser_direct_new_declarator
1608 (cp_parser *);
1609 static VEC(tree,gc) *cp_parser_new_initializer
1610 (cp_parser *);
1611 static tree cp_parser_delete_expression
1612 (cp_parser *);
1613 static tree cp_parser_cast_expression
1614 (cp_parser *, bool, bool, cp_id_kind *);
1615 static tree cp_parser_binary_expression
1616 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1617 static tree cp_parser_question_colon_clause
1618 (cp_parser *, tree);
1619 static tree cp_parser_assignment_expression
1620 (cp_parser *, bool, cp_id_kind *);
1621 static enum tree_code cp_parser_assignment_operator_opt
1622 (cp_parser *);
1623 static tree cp_parser_expression
1624 (cp_parser *, bool, cp_id_kind *);
1625 static tree cp_parser_constant_expression
1626 (cp_parser *, bool, bool *);
1627 static tree cp_parser_builtin_offsetof
1628 (cp_parser *);
1630 /* Statements [gram.stmt.stmt] */
1632 static void cp_parser_statement
1633 (cp_parser *, tree, bool, bool *);
1634 static void cp_parser_label_for_labeled_statement
1635 (cp_parser *);
1636 static tree cp_parser_expression_statement
1637 (cp_parser *, tree);
1638 static tree cp_parser_compound_statement
1639 (cp_parser *, tree, bool);
1640 static void cp_parser_statement_seq_opt
1641 (cp_parser *, tree);
1642 static tree cp_parser_selection_statement
1643 (cp_parser *, bool *);
1644 static tree cp_parser_condition
1645 (cp_parser *);
1646 static tree cp_parser_iteration_statement
1647 (cp_parser *);
1648 static void cp_parser_for_init_statement
1649 (cp_parser *);
1650 static tree cp_parser_jump_statement
1651 (cp_parser *);
1652 static void cp_parser_declaration_statement
1653 (cp_parser *);
1655 static tree cp_parser_implicitly_scoped_statement
1656 (cp_parser *, bool *);
1657 static void cp_parser_already_scoped_statement
1658 (cp_parser *);
1660 /* Declarations [gram.dcl.dcl] */
1662 static void cp_parser_declaration_seq_opt
1663 (cp_parser *);
1664 static void cp_parser_declaration
1665 (cp_parser *);
1666 static void cp_parser_block_declaration
1667 (cp_parser *, bool);
1668 static void cp_parser_simple_declaration
1669 (cp_parser *, bool);
1670 static void cp_parser_decl_specifier_seq
1671 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1672 static tree cp_parser_storage_class_specifier_opt
1673 (cp_parser *);
1674 static tree cp_parser_function_specifier_opt
1675 (cp_parser *, cp_decl_specifier_seq *);
1676 static tree cp_parser_type_specifier
1677 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1678 int *, bool *);
1679 static tree cp_parser_simple_type_specifier
1680 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1681 static tree cp_parser_type_name
1682 (cp_parser *);
1683 static tree cp_parser_nonclass_name
1684 (cp_parser* parser);
1685 static tree cp_parser_elaborated_type_specifier
1686 (cp_parser *, bool, bool);
1687 static tree cp_parser_enum_specifier
1688 (cp_parser *);
1689 static void cp_parser_enumerator_list
1690 (cp_parser *, tree);
1691 static void cp_parser_enumerator_definition
1692 (cp_parser *, tree);
1693 static tree cp_parser_namespace_name
1694 (cp_parser *);
1695 static void cp_parser_namespace_definition
1696 (cp_parser *);
1697 static void cp_parser_namespace_body
1698 (cp_parser *);
1699 static tree cp_parser_qualified_namespace_specifier
1700 (cp_parser *);
1701 static void cp_parser_namespace_alias_definition
1702 (cp_parser *);
1703 static bool cp_parser_using_declaration
1704 (cp_parser *, bool);
1705 static void cp_parser_using_directive
1706 (cp_parser *);
1707 static void cp_parser_asm_definition
1708 (cp_parser *);
1709 static void cp_parser_linkage_specification
1710 (cp_parser *);
1711 static void cp_parser_static_assert
1712 (cp_parser *, bool);
1713 static tree cp_parser_decltype
1714 (cp_parser *);
1716 /* Declarators [gram.dcl.decl] */
1718 static tree cp_parser_init_declarator
1719 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1720 static cp_declarator *cp_parser_declarator
1721 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1722 static cp_declarator *cp_parser_direct_declarator
1723 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1724 static enum tree_code cp_parser_ptr_operator
1725 (cp_parser *, tree *, cp_cv_quals *);
1726 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1727 (cp_parser *);
1728 static tree cp_parser_late_return_type_opt
1729 (cp_parser *);
1730 static tree cp_parser_declarator_id
1731 (cp_parser *, bool);
1732 static tree cp_parser_type_id
1733 (cp_parser *);
1734 static tree cp_parser_template_type_arg
1735 (cp_parser *);
1736 static tree cp_parser_type_id_1
1737 (cp_parser *, bool);
1738 static void cp_parser_type_specifier_seq
1739 (cp_parser *, bool, cp_decl_specifier_seq *);
1740 static tree cp_parser_parameter_declaration_clause
1741 (cp_parser *);
1742 static tree cp_parser_parameter_declaration_list
1743 (cp_parser *, bool *);
1744 static cp_parameter_declarator *cp_parser_parameter_declaration
1745 (cp_parser *, bool, bool *);
1746 static tree cp_parser_default_argument
1747 (cp_parser *, bool);
1748 static void cp_parser_function_body
1749 (cp_parser *);
1750 static tree cp_parser_initializer
1751 (cp_parser *, bool *, bool *);
1752 static tree cp_parser_initializer_clause
1753 (cp_parser *, bool *);
1754 static tree cp_parser_braced_list
1755 (cp_parser*, bool*);
1756 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1757 (cp_parser *, bool *);
1759 static bool cp_parser_ctor_initializer_opt_and_function_body
1760 (cp_parser *);
1762 /* Classes [gram.class] */
1764 static tree cp_parser_class_name
1765 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1766 static tree cp_parser_class_specifier
1767 (cp_parser *);
1768 static tree cp_parser_class_head
1769 (cp_parser *, bool *, tree *, tree *);
1770 static enum tag_types cp_parser_class_key
1771 (cp_parser *);
1772 static void cp_parser_member_specification_opt
1773 (cp_parser *);
1774 static void cp_parser_member_declaration
1775 (cp_parser *);
1776 static tree cp_parser_pure_specifier
1777 (cp_parser *);
1778 static tree cp_parser_constant_initializer
1779 (cp_parser *);
1781 /* Derived classes [gram.class.derived] */
1783 static tree cp_parser_base_clause
1784 (cp_parser *);
1785 static tree cp_parser_base_specifier
1786 (cp_parser *);
1788 /* Special member functions [gram.special] */
1790 static tree cp_parser_conversion_function_id
1791 (cp_parser *);
1792 static tree cp_parser_conversion_type_id
1793 (cp_parser *);
1794 static cp_declarator *cp_parser_conversion_declarator_opt
1795 (cp_parser *);
1796 static bool cp_parser_ctor_initializer_opt
1797 (cp_parser *);
1798 static void cp_parser_mem_initializer_list
1799 (cp_parser *);
1800 static tree cp_parser_mem_initializer
1801 (cp_parser *);
1802 static tree cp_parser_mem_initializer_id
1803 (cp_parser *);
1805 /* Overloading [gram.over] */
1807 static tree cp_parser_operator_function_id
1808 (cp_parser *);
1809 static tree cp_parser_operator
1810 (cp_parser *);
1812 /* Templates [gram.temp] */
1814 static void cp_parser_template_declaration
1815 (cp_parser *, bool);
1816 static tree cp_parser_template_parameter_list
1817 (cp_parser *);
1818 static tree cp_parser_template_parameter
1819 (cp_parser *, bool *, bool *);
1820 static tree cp_parser_type_parameter
1821 (cp_parser *, bool *);
1822 static tree cp_parser_template_id
1823 (cp_parser *, bool, bool, bool);
1824 static tree cp_parser_template_name
1825 (cp_parser *, bool, bool, bool, bool *);
1826 static tree cp_parser_template_argument_list
1827 (cp_parser *);
1828 static tree cp_parser_template_argument
1829 (cp_parser *);
1830 static void cp_parser_explicit_instantiation
1831 (cp_parser *);
1832 static void cp_parser_explicit_specialization
1833 (cp_parser *);
1835 /* Exception handling [gram.exception] */
1837 static tree cp_parser_try_block
1838 (cp_parser *);
1839 static bool cp_parser_function_try_block
1840 (cp_parser *);
1841 static void cp_parser_handler_seq
1842 (cp_parser *);
1843 static void cp_parser_handler
1844 (cp_parser *);
1845 static tree cp_parser_exception_declaration
1846 (cp_parser *);
1847 static tree cp_parser_throw_expression
1848 (cp_parser *);
1849 static tree cp_parser_exception_specification_opt
1850 (cp_parser *);
1851 static tree cp_parser_type_id_list
1852 (cp_parser *);
1854 /* GNU Extensions */
1856 static tree cp_parser_asm_specification_opt
1857 (cp_parser *);
1858 static tree cp_parser_asm_operand_list
1859 (cp_parser *);
1860 static tree cp_parser_asm_clobber_list
1861 (cp_parser *);
1862 static tree cp_parser_attributes_opt
1863 (cp_parser *);
1864 static tree cp_parser_attribute_list
1865 (cp_parser *);
1866 static bool cp_parser_extension_opt
1867 (cp_parser *, int *);
1868 static void cp_parser_label_declaration
1869 (cp_parser *);
1871 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1872 static bool cp_parser_pragma
1873 (cp_parser *, enum pragma_context);
1875 /* Objective-C++ Productions */
1877 static tree cp_parser_objc_message_receiver
1878 (cp_parser *);
1879 static tree cp_parser_objc_message_args
1880 (cp_parser *);
1881 static tree cp_parser_objc_message_expression
1882 (cp_parser *);
1883 static tree cp_parser_objc_encode_expression
1884 (cp_parser *);
1885 static tree cp_parser_objc_defs_expression
1886 (cp_parser *);
1887 static tree cp_parser_objc_protocol_expression
1888 (cp_parser *);
1889 static tree cp_parser_objc_selector_expression
1890 (cp_parser *);
1891 static tree cp_parser_objc_expression
1892 (cp_parser *);
1893 static bool cp_parser_objc_selector_p
1894 (enum cpp_ttype);
1895 static tree cp_parser_objc_selector
1896 (cp_parser *);
1897 static tree cp_parser_objc_protocol_refs_opt
1898 (cp_parser *);
1899 static void cp_parser_objc_declaration
1900 (cp_parser *);
1901 static tree cp_parser_objc_statement
1902 (cp_parser *);
1904 /* Utility Routines */
1906 static tree cp_parser_lookup_name
1907 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1908 static tree cp_parser_lookup_name_simple
1909 (cp_parser *, tree, location_t);
1910 static tree cp_parser_maybe_treat_template_as_class
1911 (tree, bool);
1912 static bool cp_parser_check_declarator_template_parameters
1913 (cp_parser *, cp_declarator *, location_t);
1914 static bool cp_parser_check_template_parameters
1915 (cp_parser *, unsigned, location_t, cp_declarator *);
1916 static tree cp_parser_simple_cast_expression
1917 (cp_parser *);
1918 static tree cp_parser_global_scope_opt
1919 (cp_parser *, bool);
1920 static bool cp_parser_constructor_declarator_p
1921 (cp_parser *, bool);
1922 static tree cp_parser_function_definition_from_specifiers_and_declarator
1923 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1924 static tree cp_parser_function_definition_after_declarator
1925 (cp_parser *, bool);
1926 static void cp_parser_template_declaration_after_export
1927 (cp_parser *, bool);
1928 static void cp_parser_perform_template_parameter_access_checks
1929 (VEC (deferred_access_check,gc)*);
1930 static tree cp_parser_single_declaration
1931 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1932 static tree cp_parser_functional_cast
1933 (cp_parser *, tree);
1934 static tree cp_parser_save_member_function_body
1935 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1936 static tree cp_parser_enclosed_template_argument_list
1937 (cp_parser *);
1938 static void cp_parser_save_default_args
1939 (cp_parser *, tree);
1940 static void cp_parser_late_parsing_for_member
1941 (cp_parser *, tree);
1942 static void cp_parser_late_parsing_default_args
1943 (cp_parser *, tree);
1944 static tree cp_parser_sizeof_operand
1945 (cp_parser *, enum rid);
1946 static tree cp_parser_trait_expr
1947 (cp_parser *, enum rid);
1948 static bool cp_parser_declares_only_class_p
1949 (cp_parser *);
1950 static void cp_parser_set_storage_class
1951 (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1952 static void cp_parser_set_decl_spec_type
1953 (cp_decl_specifier_seq *, tree, location_t, bool);
1954 static bool cp_parser_friend_p
1955 (const cp_decl_specifier_seq *);
1956 static cp_token *cp_parser_require
1957 (cp_parser *, enum cpp_ttype, const char *);
1958 static cp_token *cp_parser_require_keyword
1959 (cp_parser *, enum rid, const char *);
1960 static bool cp_parser_token_starts_function_definition_p
1961 (cp_token *);
1962 static bool cp_parser_next_token_starts_class_definition_p
1963 (cp_parser *);
1964 static bool cp_parser_next_token_ends_template_argument_p
1965 (cp_parser *);
1966 static bool cp_parser_nth_token_starts_template_argument_list_p
1967 (cp_parser *, size_t);
1968 static enum tag_types cp_parser_token_is_class_key
1969 (cp_token *);
1970 static void cp_parser_check_class_key
1971 (enum tag_types, tree type);
1972 static void cp_parser_check_access_in_redeclaration
1973 (tree type, location_t location);
1974 static bool cp_parser_optional_template_keyword
1975 (cp_parser *);
1976 static void cp_parser_pre_parsed_nested_name_specifier
1977 (cp_parser *);
1978 static bool cp_parser_cache_group
1979 (cp_parser *, enum cpp_ttype, unsigned);
1980 static void cp_parser_parse_tentatively
1981 (cp_parser *);
1982 static void cp_parser_commit_to_tentative_parse
1983 (cp_parser *);
1984 static void cp_parser_abort_tentative_parse
1985 (cp_parser *);
1986 static bool cp_parser_parse_definitely
1987 (cp_parser *);
1988 static inline bool cp_parser_parsing_tentatively
1989 (cp_parser *);
1990 static bool cp_parser_uncommitted_to_tentative_parse_p
1991 (cp_parser *);
1992 static void cp_parser_error
1993 (cp_parser *, const char *);
1994 static void cp_parser_name_lookup_error
1995 (cp_parser *, tree, tree, const char *, location_t);
1996 static bool cp_parser_simulate_error
1997 (cp_parser *);
1998 static bool cp_parser_check_type_definition
1999 (cp_parser *);
2000 static void cp_parser_check_for_definition_in_return_type
2001 (cp_declarator *, tree, location_t type_location);
2002 static void cp_parser_check_for_invalid_template_id
2003 (cp_parser *, tree, location_t location);
2004 static bool cp_parser_non_integral_constant_expression
2005 (cp_parser *, const char *);
2006 static void cp_parser_diagnose_invalid_type_name
2007 (cp_parser *, tree, tree, location_t);
2008 static bool cp_parser_parse_and_diagnose_invalid_type_name
2009 (cp_parser *);
2010 static int cp_parser_skip_to_closing_parenthesis
2011 (cp_parser *, bool, bool, bool);
2012 static void cp_parser_skip_to_end_of_statement
2013 (cp_parser *);
2014 static void cp_parser_consume_semicolon_at_end_of_statement
2015 (cp_parser *);
2016 static void cp_parser_skip_to_end_of_block_or_statement
2017 (cp_parser *);
2018 static bool cp_parser_skip_to_closing_brace
2019 (cp_parser *);
2020 static void cp_parser_skip_to_end_of_template_parameter_list
2021 (cp_parser *);
2022 static void cp_parser_skip_to_pragma_eol
2023 (cp_parser*, cp_token *);
2024 static bool cp_parser_error_occurred
2025 (cp_parser *);
2026 static bool cp_parser_allow_gnu_extensions_p
2027 (cp_parser *);
2028 static bool cp_parser_is_string_literal
2029 (cp_token *);
2030 static bool cp_parser_is_keyword
2031 (cp_token *, enum rid);
2032 static tree cp_parser_make_typename_type
2033 (cp_parser *, tree, tree, location_t location);
2034 static cp_declarator * cp_parser_make_indirect_declarator
2035 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2037 /* Returns nonzero if we are parsing tentatively. */
2039 static inline bool
2040 cp_parser_parsing_tentatively (cp_parser* parser)
2042 return parser->context->next != NULL;
2045 /* Returns nonzero if TOKEN is a string literal. */
2047 static bool
2048 cp_parser_is_string_literal (cp_token* token)
2050 return (token->type == CPP_STRING ||
2051 token->type == CPP_STRING16 ||
2052 token->type == CPP_STRING32 ||
2053 token->type == CPP_WSTRING);
2056 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2058 static bool
2059 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2061 return token->keyword == keyword;
2064 /* If not parsing tentatively, issue a diagnostic of the form
2065 FILE:LINE: MESSAGE before TOKEN
2066 where TOKEN is the next token in the input stream. MESSAGE
2067 (specified by the caller) is usually of the form "expected
2068 OTHER-TOKEN". */
2070 static void
2071 cp_parser_error (cp_parser* parser, const char* message)
2073 if (!cp_parser_simulate_error (parser))
2075 cp_token *token = cp_lexer_peek_token (parser->lexer);
2076 /* This diagnostic makes more sense if it is tagged to the line
2077 of the token we just peeked at. */
2078 cp_lexer_set_source_position_from_token (token);
2080 if (token->type == CPP_PRAGMA)
2082 error_at (token->location,
2083 "%<#pragma%> is not allowed here");
2084 cp_parser_skip_to_pragma_eol (parser, token);
2085 return;
2088 c_parse_error (message,
2089 /* Because c_parser_error does not understand
2090 CPP_KEYWORD, keywords are treated like
2091 identifiers. */
2092 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2093 token->u.value, token->flags);
2097 /* Issue an error about name-lookup failing. NAME is the
2098 IDENTIFIER_NODE DECL is the result of
2099 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2100 the thing that we hoped to find. */
2102 static void
2103 cp_parser_name_lookup_error (cp_parser* parser,
2104 tree name,
2105 tree decl,
2106 const char* desired,
2107 location_t location)
2109 /* If name lookup completely failed, tell the user that NAME was not
2110 declared. */
2111 if (decl == error_mark_node)
2113 if (parser->scope && parser->scope != global_namespace)
2114 error_at (location, "%<%E::%E%> has not been declared",
2115 parser->scope, name);
2116 else if (parser->scope == global_namespace)
2117 error_at (location, "%<::%E%> has not been declared", name);
2118 else if (parser->object_scope
2119 && !CLASS_TYPE_P (parser->object_scope))
2120 error_at (location, "request for member %qE in non-class type %qT",
2121 name, parser->object_scope);
2122 else if (parser->object_scope)
2123 error_at (location, "%<%T::%E%> has not been declared",
2124 parser->object_scope, name);
2125 else
2126 error_at (location, "%qE has not been declared", name);
2128 else if (parser->scope && parser->scope != global_namespace)
2129 error_at (location, "%<%E::%E%> %s", parser->scope, name, desired);
2130 else if (parser->scope == global_namespace)
2131 error_at (location, "%<::%E%> %s", name, desired);
2132 else
2133 error_at (location, "%qE %s", name, desired);
2136 /* If we are parsing tentatively, remember that an error has occurred
2137 during this tentative parse. Returns true if the error was
2138 simulated; false if a message should be issued by the caller. */
2140 static bool
2141 cp_parser_simulate_error (cp_parser* parser)
2143 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2145 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2146 return true;
2148 return false;
2151 /* Check for repeated decl-specifiers. */
2153 static void
2154 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2155 location_t location)
2157 int ds;
2159 for (ds = ds_first; ds != ds_last; ++ds)
2161 unsigned count = decl_specs->specs[ds];
2162 if (count < 2)
2163 continue;
2164 /* The "long" specifier is a special case because of "long long". */
2165 if (ds == ds_long)
2167 if (count > 2)
2168 error_at (location, "%<long long long%> is too long for GCC");
2169 else
2170 pedwarn_cxx98 (location, OPT_Wlong_long,
2171 "ISO C++ 1998 does not support %<long long%>");
2173 else if (count > 1)
2175 static const char *const decl_spec_names[] = {
2176 "signed",
2177 "unsigned",
2178 "short",
2179 "long",
2180 "const",
2181 "volatile",
2182 "restrict",
2183 "inline",
2184 "virtual",
2185 "explicit",
2186 "friend",
2187 "typedef",
2188 "__complex",
2189 "__thread"
2191 error_at (location, "duplicate %qs", decl_spec_names[ds]);
2196 /* This function is called when a type is defined. If type
2197 definitions are forbidden at this point, an error message is
2198 issued. */
2200 static bool
2201 cp_parser_check_type_definition (cp_parser* parser)
2203 /* If types are forbidden here, issue a message. */
2204 if (parser->type_definition_forbidden_message)
2206 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2207 in the message need to be interpreted. */
2208 error (parser->type_definition_forbidden_message);
2209 return false;
2211 return true;
2214 /* This function is called when the DECLARATOR is processed. The TYPE
2215 was a type defined in the decl-specifiers. If it is invalid to
2216 define a type in the decl-specifiers for DECLARATOR, an error is
2217 issued. TYPE_LOCATION is the location of TYPE and is used
2218 for error reporting. */
2220 static void
2221 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2222 tree type, location_t type_location)
2224 /* [dcl.fct] forbids type definitions in return types.
2225 Unfortunately, it's not easy to know whether or not we are
2226 processing a return type until after the fact. */
2227 while (declarator
2228 && (declarator->kind == cdk_pointer
2229 || declarator->kind == cdk_reference
2230 || declarator->kind == cdk_ptrmem))
2231 declarator = declarator->declarator;
2232 if (declarator
2233 && declarator->kind == cdk_function)
2235 error_at (type_location,
2236 "new types may not be defined in a return type");
2237 inform (type_location,
2238 "(perhaps a semicolon is missing after the definition of %qT)",
2239 type);
2243 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2244 "<" in any valid C++ program. If the next token is indeed "<",
2245 issue a message warning the user about what appears to be an
2246 invalid attempt to form a template-id. LOCATION is the location
2247 of the type-specifier (TYPE) */
2249 static void
2250 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2251 tree type, location_t location)
2253 cp_token_position start = 0;
2255 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2257 if (TYPE_P (type))
2258 error_at (location, "%qT is not a template", type);
2259 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2260 error_at (location, "%qE is not a template", type);
2261 else
2262 error_at (location, "invalid template-id");
2263 /* Remember the location of the invalid "<". */
2264 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2265 start = cp_lexer_token_position (parser->lexer, true);
2266 /* Consume the "<". */
2267 cp_lexer_consume_token (parser->lexer);
2268 /* Parse the template arguments. */
2269 cp_parser_enclosed_template_argument_list (parser);
2270 /* Permanently remove the invalid template arguments so that
2271 this error message is not issued again. */
2272 if (start)
2273 cp_lexer_purge_tokens_after (parser->lexer, start);
2277 /* If parsing an integral constant-expression, issue an error message
2278 about the fact that THING appeared and return true. Otherwise,
2279 return false. In either case, set
2280 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2282 static bool
2283 cp_parser_non_integral_constant_expression (cp_parser *parser,
2284 const char *thing)
2286 parser->non_integral_constant_expression_p = true;
2287 if (parser->integral_constant_expression_p)
2289 if (!parser->allow_non_integral_constant_expression_p)
2291 /* Don't use `%s' to print THING, because quotations (`%<', `%>')
2292 in the message need to be interpreted. */
2293 char *message = concat (thing,
2294 " cannot appear in a constant-expression",
2295 NULL);
2296 error (message);
2297 free (message);
2298 return true;
2301 return false;
2304 /* Emit a diagnostic for an invalid type name. SCOPE is the
2305 qualifying scope (or NULL, if none) for ID. This function commits
2306 to the current active tentative parse, if any. (Otherwise, the
2307 problematic construct might be encountered again later, resulting
2308 in duplicate error messages.) LOCATION is the location of ID. */
2310 static void
2311 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2312 tree scope, tree id,
2313 location_t location)
2315 tree decl, old_scope;
2316 /* Try to lookup the identifier. */
2317 old_scope = parser->scope;
2318 parser->scope = scope;
2319 decl = cp_parser_lookup_name_simple (parser, id, location);
2320 parser->scope = old_scope;
2321 /* If the lookup found a template-name, it means that the user forgot
2322 to specify an argument list. Emit a useful error message. */
2323 if (TREE_CODE (decl) == TEMPLATE_DECL)
2324 error_at (location,
2325 "invalid use of template-name %qE without an argument list",
2326 decl);
2327 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2328 error_at (location, "invalid use of destructor %qD as a type", id);
2329 else if (TREE_CODE (decl) == TYPE_DECL)
2330 /* Something like 'unsigned A a;' */
2331 error_at (location, "invalid combination of multiple type-specifiers");
2332 else if (!parser->scope)
2334 /* Issue an error message. */
2335 error_at (location, "%qE does not name a type", id);
2336 /* If we're in a template class, it's possible that the user was
2337 referring to a type from a base class. For example:
2339 template <typename T> struct A { typedef T X; };
2340 template <typename T> struct B : public A<T> { X x; };
2342 The user should have said "typename A<T>::X". */
2343 if (processing_template_decl && current_class_type
2344 && TYPE_BINFO (current_class_type))
2346 tree b;
2348 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2350 b = TREE_CHAIN (b))
2352 tree base_type = BINFO_TYPE (b);
2353 if (CLASS_TYPE_P (base_type)
2354 && dependent_type_p (base_type))
2356 tree field;
2357 /* Go from a particular instantiation of the
2358 template (which will have an empty TYPE_FIELDs),
2359 to the main version. */
2360 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2361 for (field = TYPE_FIELDS (base_type);
2362 field;
2363 field = TREE_CHAIN (field))
2364 if (TREE_CODE (field) == TYPE_DECL
2365 && DECL_NAME (field) == id)
2367 inform (location,
2368 "(perhaps %<typename %T::%E%> was intended)",
2369 BINFO_TYPE (b), id);
2370 break;
2372 if (field)
2373 break;
2378 /* Here we diagnose qualified-ids where the scope is actually correct,
2379 but the identifier does not resolve to a valid type name. */
2380 else if (parser->scope != error_mark_node)
2382 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2383 error_at (location, "%qE in namespace %qE does not name a type",
2384 id, parser->scope);
2385 else if (TYPE_P (parser->scope))
2386 error_at (location, "%qE in class %qT does not name a type",
2387 id, parser->scope);
2388 else
2389 gcc_unreachable ();
2391 cp_parser_commit_to_tentative_parse (parser);
2394 /* Check for a common situation where a type-name should be present,
2395 but is not, and issue a sensible error message. Returns true if an
2396 invalid type-name was detected.
2398 The situation handled by this function are variable declarations of the
2399 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2400 Usually, `ID' should name a type, but if we got here it means that it
2401 does not. We try to emit the best possible error message depending on
2402 how exactly the id-expression looks like. */
2404 static bool
2405 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2407 tree id;
2408 cp_token *token = cp_lexer_peek_token (parser->lexer);
2410 cp_parser_parse_tentatively (parser);
2411 id = cp_parser_id_expression (parser,
2412 /*template_keyword_p=*/false,
2413 /*check_dependency_p=*/true,
2414 /*template_p=*/NULL,
2415 /*declarator_p=*/true,
2416 /*optional_p=*/false);
2417 /* After the id-expression, there should be a plain identifier,
2418 otherwise this is not a simple variable declaration. Also, if
2419 the scope is dependent, we cannot do much. */
2420 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2421 || (parser->scope && TYPE_P (parser->scope)
2422 && dependent_type_p (parser->scope))
2423 || TREE_CODE (id) == TYPE_DECL)
2425 cp_parser_abort_tentative_parse (parser);
2426 return false;
2428 if (!cp_parser_parse_definitely (parser))
2429 return false;
2431 /* Emit a diagnostic for the invalid type. */
2432 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2433 id, token->location);
2434 /* Skip to the end of the declaration; there's no point in
2435 trying to process it. */
2436 cp_parser_skip_to_end_of_block_or_statement (parser);
2437 return true;
2440 /* Consume tokens up to, and including, the next non-nested closing `)'.
2441 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2442 are doing error recovery. Returns -1 if OR_COMMA is true and we
2443 found an unnested comma. */
2445 static int
2446 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2447 bool recovering,
2448 bool or_comma,
2449 bool consume_paren)
2451 unsigned paren_depth = 0;
2452 unsigned brace_depth = 0;
2454 if (recovering && !or_comma
2455 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2456 return 0;
2458 while (true)
2460 cp_token * token = cp_lexer_peek_token (parser->lexer);
2462 switch (token->type)
2464 case CPP_EOF:
2465 case CPP_PRAGMA_EOL:
2466 /* If we've run out of tokens, then there is no closing `)'. */
2467 return 0;
2469 case CPP_SEMICOLON:
2470 /* This matches the processing in skip_to_end_of_statement. */
2471 if (!brace_depth)
2472 return 0;
2473 break;
2475 case CPP_OPEN_BRACE:
2476 ++brace_depth;
2477 break;
2478 case CPP_CLOSE_BRACE:
2479 if (!brace_depth--)
2480 return 0;
2481 break;
2483 case CPP_COMMA:
2484 if (recovering && or_comma && !brace_depth && !paren_depth)
2485 return -1;
2486 break;
2488 case CPP_OPEN_PAREN:
2489 if (!brace_depth)
2490 ++paren_depth;
2491 break;
2493 case CPP_CLOSE_PAREN:
2494 if (!brace_depth && !paren_depth--)
2496 if (consume_paren)
2497 cp_lexer_consume_token (parser->lexer);
2498 return 1;
2500 break;
2502 default:
2503 break;
2506 /* Consume the token. */
2507 cp_lexer_consume_token (parser->lexer);
2511 /* Consume tokens until we reach the end of the current statement.
2512 Normally, that will be just before consuming a `;'. However, if a
2513 non-nested `}' comes first, then we stop before consuming that. */
2515 static void
2516 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2518 unsigned nesting_depth = 0;
2520 while (true)
2522 cp_token *token = cp_lexer_peek_token (parser->lexer);
2524 switch (token->type)
2526 case CPP_EOF:
2527 case CPP_PRAGMA_EOL:
2528 /* If we've run out of tokens, stop. */
2529 return;
2531 case CPP_SEMICOLON:
2532 /* If the next token is a `;', we have reached the end of the
2533 statement. */
2534 if (!nesting_depth)
2535 return;
2536 break;
2538 case CPP_CLOSE_BRACE:
2539 /* If this is a non-nested '}', stop before consuming it.
2540 That way, when confronted with something like:
2542 { 3 + }
2544 we stop before consuming the closing '}', even though we
2545 have not yet reached a `;'. */
2546 if (nesting_depth == 0)
2547 return;
2549 /* If it is the closing '}' for a block that we have
2550 scanned, stop -- but only after consuming the token.
2551 That way given:
2553 void f g () { ... }
2554 typedef int I;
2556 we will stop after the body of the erroneously declared
2557 function, but before consuming the following `typedef'
2558 declaration. */
2559 if (--nesting_depth == 0)
2561 cp_lexer_consume_token (parser->lexer);
2562 return;
2565 case CPP_OPEN_BRACE:
2566 ++nesting_depth;
2567 break;
2569 default:
2570 break;
2573 /* Consume the token. */
2574 cp_lexer_consume_token (parser->lexer);
2578 /* This function is called at the end of a statement or declaration.
2579 If the next token is a semicolon, it is consumed; otherwise, error
2580 recovery is attempted. */
2582 static void
2583 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2585 /* Look for the trailing `;'. */
2586 if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2588 /* If there is additional (erroneous) input, skip to the end of
2589 the statement. */
2590 cp_parser_skip_to_end_of_statement (parser);
2591 /* If the next token is now a `;', consume it. */
2592 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2593 cp_lexer_consume_token (parser->lexer);
2597 /* Skip tokens until we have consumed an entire block, or until we
2598 have consumed a non-nested `;'. */
2600 static void
2601 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2603 int nesting_depth = 0;
2605 while (nesting_depth >= 0)
2607 cp_token *token = cp_lexer_peek_token (parser->lexer);
2609 switch (token->type)
2611 case CPP_EOF:
2612 case CPP_PRAGMA_EOL:
2613 /* If we've run out of tokens, stop. */
2614 return;
2616 case CPP_SEMICOLON:
2617 /* Stop if this is an unnested ';'. */
2618 if (!nesting_depth)
2619 nesting_depth = -1;
2620 break;
2622 case CPP_CLOSE_BRACE:
2623 /* Stop if this is an unnested '}', or closes the outermost
2624 nesting level. */
2625 nesting_depth--;
2626 if (nesting_depth < 0)
2627 return;
2628 if (!nesting_depth)
2629 nesting_depth = -1;
2630 break;
2632 case CPP_OPEN_BRACE:
2633 /* Nest. */
2634 nesting_depth++;
2635 break;
2637 default:
2638 break;
2641 /* Consume the token. */
2642 cp_lexer_consume_token (parser->lexer);
2646 /* Skip tokens until a non-nested closing curly brace is the next
2647 token, or there are no more tokens. Return true in the first case,
2648 false otherwise. */
2650 static bool
2651 cp_parser_skip_to_closing_brace (cp_parser *parser)
2653 unsigned nesting_depth = 0;
2655 while (true)
2657 cp_token *token = cp_lexer_peek_token (parser->lexer);
2659 switch (token->type)
2661 case CPP_EOF:
2662 case CPP_PRAGMA_EOL:
2663 /* If we've run out of tokens, stop. */
2664 return false;
2666 case CPP_CLOSE_BRACE:
2667 /* If the next token is a non-nested `}', then we have reached
2668 the end of the current block. */
2669 if (nesting_depth-- == 0)
2670 return true;
2671 break;
2673 case CPP_OPEN_BRACE:
2674 /* If it the next token is a `{', then we are entering a new
2675 block. Consume the entire block. */
2676 ++nesting_depth;
2677 break;
2679 default:
2680 break;
2683 /* Consume the token. */
2684 cp_lexer_consume_token (parser->lexer);
2688 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2689 parameter is the PRAGMA token, allowing us to purge the entire pragma
2690 sequence. */
2692 static void
2693 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2695 cp_token *token;
2697 parser->lexer->in_pragma = false;
2700 token = cp_lexer_consume_token (parser->lexer);
2701 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2703 /* Ensure that the pragma is not parsed again. */
2704 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2707 /* Require pragma end of line, resyncing with it as necessary. The
2708 arguments are as for cp_parser_skip_to_pragma_eol. */
2710 static void
2711 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2713 parser->lexer->in_pragma = false;
2714 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2715 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2718 /* This is a simple wrapper around make_typename_type. When the id is
2719 an unresolved identifier node, we can provide a superior diagnostic
2720 using cp_parser_diagnose_invalid_type_name. */
2722 static tree
2723 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2724 tree id, location_t id_location)
2726 tree result;
2727 if (TREE_CODE (id) == IDENTIFIER_NODE)
2729 result = make_typename_type (scope, id, typename_type,
2730 /*complain=*/tf_none);
2731 if (result == error_mark_node)
2732 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2733 return result;
2735 return make_typename_type (scope, id, typename_type, tf_error);
2738 /* This is a wrapper around the
2739 make_{pointer,ptrmem,reference}_declarator functions that decides
2740 which one to call based on the CODE and CLASS_TYPE arguments. The
2741 CODE argument should be one of the values returned by
2742 cp_parser_ptr_operator. */
2743 static cp_declarator *
2744 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2745 cp_cv_quals cv_qualifiers,
2746 cp_declarator *target)
2748 if (code == ERROR_MARK)
2749 return cp_error_declarator;
2751 if (code == INDIRECT_REF)
2752 if (class_type == NULL_TREE)
2753 return make_pointer_declarator (cv_qualifiers, target);
2754 else
2755 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2756 else if (code == ADDR_EXPR && class_type == NULL_TREE)
2757 return make_reference_declarator (cv_qualifiers, target, false);
2758 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2759 return make_reference_declarator (cv_qualifiers, target, true);
2760 gcc_unreachable ();
2763 /* Create a new C++ parser. */
2765 static cp_parser *
2766 cp_parser_new (void)
2768 cp_parser *parser;
2769 cp_lexer *lexer;
2770 unsigned i;
2772 /* cp_lexer_new_main is called before calling ggc_alloc because
2773 cp_lexer_new_main might load a PCH file. */
2774 lexer = cp_lexer_new_main ();
2776 /* Initialize the binops_by_token so that we can get the tree
2777 directly from the token. */
2778 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2779 binops_by_token[binops[i].token_type] = binops[i];
2781 parser = GGC_CNEW (cp_parser);
2782 parser->lexer = lexer;
2783 parser->context = cp_parser_context_new (NULL);
2785 /* For now, we always accept GNU extensions. */
2786 parser->allow_gnu_extensions_p = 1;
2788 /* The `>' token is a greater-than operator, not the end of a
2789 template-id. */
2790 parser->greater_than_is_operator_p = true;
2792 parser->default_arg_ok_p = true;
2794 /* We are not parsing a constant-expression. */
2795 parser->integral_constant_expression_p = false;
2796 parser->allow_non_integral_constant_expression_p = false;
2797 parser->non_integral_constant_expression_p = false;
2799 /* Local variable names are not forbidden. */
2800 parser->local_variables_forbidden_p = false;
2802 /* We are not processing an `extern "C"' declaration. */
2803 parser->in_unbraced_linkage_specification_p = false;
2805 /* We are not processing a declarator. */
2806 parser->in_declarator_p = false;
2808 /* We are not processing a template-argument-list. */
2809 parser->in_template_argument_list_p = false;
2811 /* We are not in an iteration statement. */
2812 parser->in_statement = 0;
2814 /* We are not in a switch statement. */
2815 parser->in_switch_statement_p = false;
2817 /* We are not parsing a type-id inside an expression. */
2818 parser->in_type_id_in_expr_p = false;
2820 /* Declarations aren't implicitly extern "C". */
2821 parser->implicit_extern_c = false;
2823 /* String literals should be translated to the execution character set. */
2824 parser->translate_strings_p = true;
2826 /* We are not parsing a function body. */
2827 parser->in_function_body = false;
2829 /* The unparsed function queue is empty. */
2830 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2832 /* There are no classes being defined. */
2833 parser->num_classes_being_defined = 0;
2835 /* No template parameters apply. */
2836 parser->num_template_parameter_lists = 0;
2838 return parser;
2841 /* Create a cp_lexer structure which will emit the tokens in CACHE
2842 and push it onto the parser's lexer stack. This is used for delayed
2843 parsing of in-class method bodies and default arguments, and should
2844 not be confused with tentative parsing. */
2845 static void
2846 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2848 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2849 lexer->next = parser->lexer;
2850 parser->lexer = lexer;
2852 /* Move the current source position to that of the first token in the
2853 new lexer. */
2854 cp_lexer_set_source_position_from_token (lexer->next_token);
2857 /* Pop the top lexer off the parser stack. This is never used for the
2858 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2859 static void
2860 cp_parser_pop_lexer (cp_parser *parser)
2862 cp_lexer *lexer = parser->lexer;
2863 parser->lexer = lexer->next;
2864 cp_lexer_destroy (lexer);
2866 /* Put the current source position back where it was before this
2867 lexer was pushed. */
2868 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2871 /* Lexical conventions [gram.lex] */
2873 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2874 identifier. */
2876 static tree
2877 cp_parser_identifier (cp_parser* parser)
2879 cp_token *token;
2881 /* Look for the identifier. */
2882 token = cp_parser_require (parser, CPP_NAME, "identifier");
2883 /* Return the value. */
2884 return token ? token->u.value : error_mark_node;
2887 /* Parse a sequence of adjacent string constants. Returns a
2888 TREE_STRING representing the combined, nul-terminated string
2889 constant. If TRANSLATE is true, translate the string to the
2890 execution character set. If WIDE_OK is true, a wide string is
2891 invalid here.
2893 C++98 [lex.string] says that if a narrow string literal token is
2894 adjacent to a wide string literal token, the behavior is undefined.
2895 However, C99 6.4.5p4 says that this results in a wide string literal.
2896 We follow C99 here, for consistency with the C front end.
2898 This code is largely lifted from lex_string() in c-lex.c.
2900 FUTURE: ObjC++ will need to handle @-strings here. */
2901 static tree
2902 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2904 tree value;
2905 size_t count;
2906 struct obstack str_ob;
2907 cpp_string str, istr, *strs;
2908 cp_token *tok;
2909 enum cpp_ttype type;
2911 tok = cp_lexer_peek_token (parser->lexer);
2912 if (!cp_parser_is_string_literal (tok))
2914 cp_parser_error (parser, "expected string-literal");
2915 return error_mark_node;
2918 type = tok->type;
2920 /* Try to avoid the overhead of creating and destroying an obstack
2921 for the common case of just one string. */
2922 if (!cp_parser_is_string_literal
2923 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2925 cp_lexer_consume_token (parser->lexer);
2927 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2928 str.len = TREE_STRING_LENGTH (tok->u.value);
2929 count = 1;
2931 strs = &str;
2933 else
2935 gcc_obstack_init (&str_ob);
2936 count = 0;
2940 cp_lexer_consume_token (parser->lexer);
2941 count++;
2942 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2943 str.len = TREE_STRING_LENGTH (tok->u.value);
2945 if (type != tok->type)
2947 if (type == CPP_STRING)
2948 type = tok->type;
2949 else if (tok->type != CPP_STRING)
2950 error_at (tok->location,
2951 "unsupported non-standard concatenation "
2952 "of string literals");
2955 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2957 tok = cp_lexer_peek_token (parser->lexer);
2959 while (cp_parser_is_string_literal (tok));
2961 strs = (cpp_string *) obstack_finish (&str_ob);
2964 if (type != CPP_STRING && !wide_ok)
2966 cp_parser_error (parser, "a wide string is invalid in this context");
2967 type = CPP_STRING;
2970 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2971 (parse_in, strs, count, &istr, type))
2973 value = build_string (istr.len, (const char *)istr.text);
2974 free (CONST_CAST (unsigned char *, istr.text));
2976 switch (type)
2978 default:
2979 case CPP_STRING:
2980 TREE_TYPE (value) = char_array_type_node;
2981 break;
2982 case CPP_STRING16:
2983 TREE_TYPE (value) = char16_array_type_node;
2984 break;
2985 case CPP_STRING32:
2986 TREE_TYPE (value) = char32_array_type_node;
2987 break;
2988 case CPP_WSTRING:
2989 TREE_TYPE (value) = wchar_array_type_node;
2990 break;
2993 value = fix_string_type (value);
2995 else
2996 /* cpp_interpret_string has issued an error. */
2997 value = error_mark_node;
2999 if (count > 1)
3000 obstack_free (&str_ob, 0);
3002 return value;
3006 /* Basic concepts [gram.basic] */
3008 /* Parse a translation-unit.
3010 translation-unit:
3011 declaration-seq [opt]
3013 Returns TRUE if all went well. */
3015 static bool
3016 cp_parser_translation_unit (cp_parser* parser)
3018 /* The address of the first non-permanent object on the declarator
3019 obstack. */
3020 static void *declarator_obstack_base;
3022 bool success;
3024 /* Create the declarator obstack, if necessary. */
3025 if (!cp_error_declarator)
3027 gcc_obstack_init (&declarator_obstack);
3028 /* Create the error declarator. */
3029 cp_error_declarator = make_declarator (cdk_error);
3030 /* Create the empty parameter list. */
3031 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3032 /* Remember where the base of the declarator obstack lies. */
3033 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3036 cp_parser_declaration_seq_opt (parser);
3038 /* If there are no tokens left then all went well. */
3039 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3041 /* Get rid of the token array; we don't need it any more. */
3042 cp_lexer_destroy (parser->lexer);
3043 parser->lexer = NULL;
3045 /* This file might have been a context that's implicitly extern
3046 "C". If so, pop the lang context. (Only relevant for PCH.) */
3047 if (parser->implicit_extern_c)
3049 pop_lang_context ();
3050 parser->implicit_extern_c = false;
3053 /* Finish up. */
3054 finish_translation_unit ();
3056 success = true;
3058 else
3060 cp_parser_error (parser, "expected declaration");
3061 success = false;
3064 /* Make sure the declarator obstack was fully cleaned up. */
3065 gcc_assert (obstack_next_free (&declarator_obstack)
3066 == declarator_obstack_base);
3068 /* All went well. */
3069 return success;
3072 /* Expressions [gram.expr] */
3074 /* Parse a primary-expression.
3076 primary-expression:
3077 literal
3078 this
3079 ( expression )
3080 id-expression
3082 GNU Extensions:
3084 primary-expression:
3085 ( compound-statement )
3086 __builtin_va_arg ( assignment-expression , type-id )
3087 __builtin_offsetof ( type-id , offsetof-expression )
3089 C++ Extensions:
3090 __has_nothrow_assign ( type-id )
3091 __has_nothrow_constructor ( type-id )
3092 __has_nothrow_copy ( type-id )
3093 __has_trivial_assign ( type-id )
3094 __has_trivial_constructor ( type-id )
3095 __has_trivial_copy ( type-id )
3096 __has_trivial_destructor ( type-id )
3097 __has_virtual_destructor ( type-id )
3098 __is_abstract ( type-id )
3099 __is_base_of ( type-id , type-id )
3100 __is_class ( type-id )
3101 __is_convertible_to ( type-id , type-id )
3102 __is_empty ( type-id )
3103 __is_enum ( type-id )
3104 __is_pod ( type-id )
3105 __is_polymorphic ( type-id )
3106 __is_union ( type-id )
3108 Objective-C++ Extension:
3110 primary-expression:
3111 objc-expression
3113 literal:
3114 __null
3116 ADDRESS_P is true iff this expression was immediately preceded by
3117 "&" and therefore might denote a pointer-to-member. CAST_P is true
3118 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3119 true iff this expression is a template argument.
3121 Returns a representation of the expression. Upon return, *IDK
3122 indicates what kind of id-expression (if any) was present. */
3124 static tree
3125 cp_parser_primary_expression (cp_parser *parser,
3126 bool address_p,
3127 bool cast_p,
3128 bool template_arg_p,
3129 cp_id_kind *idk)
3131 cp_token *token = NULL;
3133 /* Assume the primary expression is not an id-expression. */
3134 *idk = CP_ID_KIND_NONE;
3136 /* Peek at the next token. */
3137 token = cp_lexer_peek_token (parser->lexer);
3138 switch (token->type)
3140 /* literal:
3141 integer-literal
3142 character-literal
3143 floating-literal
3144 string-literal
3145 boolean-literal */
3146 case CPP_CHAR:
3147 case CPP_CHAR16:
3148 case CPP_CHAR32:
3149 case CPP_WCHAR:
3150 case CPP_NUMBER:
3151 token = cp_lexer_consume_token (parser->lexer);
3152 if (TREE_CODE (token->u.value) == FIXED_CST)
3154 error_at (token->location,
3155 "fixed-point types not supported in C++");
3156 return error_mark_node;
3158 /* Floating-point literals are only allowed in an integral
3159 constant expression if they are cast to an integral or
3160 enumeration type. */
3161 if (TREE_CODE (token->u.value) == REAL_CST
3162 && parser->integral_constant_expression_p
3163 && pedantic)
3165 /* CAST_P will be set even in invalid code like "int(2.7 +
3166 ...)". Therefore, we have to check that the next token
3167 is sure to end the cast. */
3168 if (cast_p)
3170 cp_token *next_token;
3172 next_token = cp_lexer_peek_token (parser->lexer);
3173 if (/* The comma at the end of an
3174 enumerator-definition. */
3175 next_token->type != CPP_COMMA
3176 /* The curly brace at the end of an enum-specifier. */
3177 && next_token->type != CPP_CLOSE_BRACE
3178 /* The end of a statement. */
3179 && next_token->type != CPP_SEMICOLON
3180 /* The end of the cast-expression. */
3181 && next_token->type != CPP_CLOSE_PAREN
3182 /* The end of an array bound. */
3183 && next_token->type != CPP_CLOSE_SQUARE
3184 /* The closing ">" in a template-argument-list. */
3185 && (next_token->type != CPP_GREATER
3186 || parser->greater_than_is_operator_p)
3187 /* C++0x only: A ">>" treated like two ">" tokens,
3188 in a template-argument-list. */
3189 && (next_token->type != CPP_RSHIFT
3190 || (cxx_dialect == cxx98)
3191 || parser->greater_than_is_operator_p))
3192 cast_p = false;
3195 /* If we are within a cast, then the constraint that the
3196 cast is to an integral or enumeration type will be
3197 checked at that point. If we are not within a cast, then
3198 this code is invalid. */
3199 if (!cast_p)
3200 cp_parser_non_integral_constant_expression
3201 (parser, "floating-point literal");
3203 return token->u.value;
3205 case CPP_STRING:
3206 case CPP_STRING16:
3207 case CPP_STRING32:
3208 case CPP_WSTRING:
3209 /* ??? Should wide strings be allowed when parser->translate_strings_p
3210 is false (i.e. in attributes)? If not, we can kill the third
3211 argument to cp_parser_string_literal. */
3212 return cp_parser_string_literal (parser,
3213 parser->translate_strings_p,
3214 true);
3216 case CPP_OPEN_PAREN:
3218 tree expr;
3219 bool saved_greater_than_is_operator_p;
3221 /* Consume the `('. */
3222 cp_lexer_consume_token (parser->lexer);
3223 /* Within a parenthesized expression, a `>' token is always
3224 the greater-than operator. */
3225 saved_greater_than_is_operator_p
3226 = parser->greater_than_is_operator_p;
3227 parser->greater_than_is_operator_p = true;
3228 /* If we see `( { ' then we are looking at the beginning of
3229 a GNU statement-expression. */
3230 if (cp_parser_allow_gnu_extensions_p (parser)
3231 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3233 /* Statement-expressions are not allowed by the standard. */
3234 pedwarn (token->location, OPT_pedantic,
3235 "ISO C++ forbids braced-groups within expressions");
3237 /* And they're not allowed outside of a function-body; you
3238 cannot, for example, write:
3240 int i = ({ int j = 3; j + 1; });
3242 at class or namespace scope. */
3243 if (!parser->in_function_body
3244 || parser->in_template_argument_list_p)
3246 error_at (token->location,
3247 "statement-expressions are not allowed outside "
3248 "functions nor in template-argument lists");
3249 cp_parser_skip_to_end_of_block_or_statement (parser);
3250 expr = error_mark_node;
3252 else
3254 /* Start the statement-expression. */
3255 expr = begin_stmt_expr ();
3256 /* Parse the compound-statement. */
3257 cp_parser_compound_statement (parser, expr, false);
3258 /* Finish up. */
3259 expr = finish_stmt_expr (expr, false);
3262 else
3264 /* Parse the parenthesized expression. */
3265 expr = cp_parser_expression (parser, cast_p, idk);
3266 /* Let the front end know that this expression was
3267 enclosed in parentheses. This matters in case, for
3268 example, the expression is of the form `A::B', since
3269 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3270 not. */
3271 finish_parenthesized_expr (expr);
3273 /* The `>' token might be the end of a template-id or
3274 template-parameter-list now. */
3275 parser->greater_than_is_operator_p
3276 = saved_greater_than_is_operator_p;
3277 /* Consume the `)'. */
3278 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3279 cp_parser_skip_to_end_of_statement (parser);
3281 return expr;
3284 case CPP_KEYWORD:
3285 switch (token->keyword)
3287 /* These two are the boolean literals. */
3288 case RID_TRUE:
3289 cp_lexer_consume_token (parser->lexer);
3290 return boolean_true_node;
3291 case RID_FALSE:
3292 cp_lexer_consume_token (parser->lexer);
3293 return boolean_false_node;
3295 /* The `__null' literal. */
3296 case RID_NULL:
3297 cp_lexer_consume_token (parser->lexer);
3298 return null_node;
3300 /* Recognize the `this' keyword. */
3301 case RID_THIS:
3302 cp_lexer_consume_token (parser->lexer);
3303 if (parser->local_variables_forbidden_p)
3305 error_at (token->location,
3306 "%<this%> may not be used in this context");
3307 return error_mark_node;
3309 /* Pointers cannot appear in constant-expressions. */
3310 if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3311 return error_mark_node;
3312 return finish_this_expr ();
3314 /* The `operator' keyword can be the beginning of an
3315 id-expression. */
3316 case RID_OPERATOR:
3317 goto id_expression;
3319 case RID_FUNCTION_NAME:
3320 case RID_PRETTY_FUNCTION_NAME:
3321 case RID_C99_FUNCTION_NAME:
3323 const char *name;
3325 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3326 __func__ are the names of variables -- but they are
3327 treated specially. Therefore, they are handled here,
3328 rather than relying on the generic id-expression logic
3329 below. Grammatically, these names are id-expressions.
3331 Consume the token. */
3332 token = cp_lexer_consume_token (parser->lexer);
3334 switch (token->keyword)
3336 case RID_FUNCTION_NAME:
3337 name = "%<__FUNCTION__%>";
3338 break;
3339 case RID_PRETTY_FUNCTION_NAME:
3340 name = "%<__PRETTY_FUNCTION__%>";
3341 break;
3342 case RID_C99_FUNCTION_NAME:
3343 name = "%<__func__%>";
3344 break;
3345 default:
3346 gcc_unreachable ();
3349 if (cp_parser_non_integral_constant_expression (parser, name))
3350 return error_mark_node;
3352 /* Look up the name. */
3353 return finish_fname (token->u.value);
3356 case RID_VA_ARG:
3358 tree expression;
3359 tree type;
3361 /* The `__builtin_va_arg' construct is used to handle
3362 `va_arg'. Consume the `__builtin_va_arg' token. */
3363 cp_lexer_consume_token (parser->lexer);
3364 /* Look for the opening `('. */
3365 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3366 /* Now, parse the assignment-expression. */
3367 expression = cp_parser_assignment_expression (parser,
3368 /*cast_p=*/false, NULL);
3369 /* Look for the `,'. */
3370 cp_parser_require (parser, CPP_COMMA, "%<,%>");
3371 /* Parse the type-id. */
3372 type = cp_parser_type_id (parser);
3373 /* Look for the closing `)'. */
3374 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3375 /* Using `va_arg' in a constant-expression is not
3376 allowed. */
3377 if (cp_parser_non_integral_constant_expression (parser,
3378 "%<va_arg%>"))
3379 return error_mark_node;
3380 return build_x_va_arg (expression, type);
3383 case RID_OFFSETOF:
3384 return cp_parser_builtin_offsetof (parser);
3386 case RID_HAS_NOTHROW_ASSIGN:
3387 case RID_HAS_NOTHROW_CONSTRUCTOR:
3388 case RID_HAS_NOTHROW_COPY:
3389 case RID_HAS_TRIVIAL_ASSIGN:
3390 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3391 case RID_HAS_TRIVIAL_COPY:
3392 case RID_HAS_TRIVIAL_DESTRUCTOR:
3393 case RID_HAS_VIRTUAL_DESTRUCTOR:
3394 case RID_IS_ABSTRACT:
3395 case RID_IS_BASE_OF:
3396 case RID_IS_CLASS:
3397 case RID_IS_CONVERTIBLE_TO:
3398 case RID_IS_EMPTY:
3399 case RID_IS_ENUM:
3400 case RID_IS_POD:
3401 case RID_IS_POLYMORPHIC:
3402 case RID_IS_STD_LAYOUT:
3403 case RID_IS_TRIVIAL:
3404 case RID_IS_UNION:
3405 return cp_parser_trait_expr (parser, token->keyword);
3407 /* Objective-C++ expressions. */
3408 case RID_AT_ENCODE:
3409 case RID_AT_PROTOCOL:
3410 case RID_AT_SELECTOR:
3411 return cp_parser_objc_expression (parser);
3413 default:
3414 cp_parser_error (parser, "expected primary-expression");
3415 return error_mark_node;
3418 /* An id-expression can start with either an identifier, a
3419 `::' as the beginning of a qualified-id, or the "operator"
3420 keyword. */
3421 case CPP_NAME:
3422 case CPP_SCOPE:
3423 case CPP_TEMPLATE_ID:
3424 case CPP_NESTED_NAME_SPECIFIER:
3426 tree id_expression;
3427 tree decl;
3428 const char *error_msg;
3429 bool template_p;
3430 bool done;
3431 cp_token *id_expr_token;
3433 id_expression:
3434 /* Parse the id-expression. */
3435 id_expression
3436 = cp_parser_id_expression (parser,
3437 /*template_keyword_p=*/false,
3438 /*check_dependency_p=*/true,
3439 &template_p,
3440 /*declarator_p=*/false,
3441 /*optional_p=*/false);
3442 if (id_expression == error_mark_node)
3443 return error_mark_node;
3444 id_expr_token = token;
3445 token = cp_lexer_peek_token (parser->lexer);
3446 done = (token->type != CPP_OPEN_SQUARE
3447 && token->type != CPP_OPEN_PAREN
3448 && token->type != CPP_DOT
3449 && token->type != CPP_DEREF
3450 && token->type != CPP_PLUS_PLUS
3451 && token->type != CPP_MINUS_MINUS);
3452 /* If we have a template-id, then no further lookup is
3453 required. If the template-id was for a template-class, we
3454 will sometimes have a TYPE_DECL at this point. */
3455 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3456 || TREE_CODE (id_expression) == TYPE_DECL)
3457 decl = id_expression;
3458 /* Look up the name. */
3459 else
3461 tree ambiguous_decls;
3463 decl = cp_parser_lookup_name (parser, id_expression,
3464 none_type,
3465 template_p,
3466 /*is_namespace=*/false,
3467 /*check_dependency=*/true,
3468 &ambiguous_decls,
3469 id_expr_token->location);
3470 /* If the lookup was ambiguous, an error will already have
3471 been issued. */
3472 if (ambiguous_decls)
3473 return error_mark_node;
3475 /* In Objective-C++, an instance variable (ivar) may be preferred
3476 to whatever cp_parser_lookup_name() found. */
3477 decl = objc_lookup_ivar (decl, id_expression);
3479 /* If name lookup gives us a SCOPE_REF, then the
3480 qualifying scope was dependent. */
3481 if (TREE_CODE (decl) == SCOPE_REF)
3483 /* At this point, we do not know if DECL is a valid
3484 integral constant expression. We assume that it is
3485 in fact such an expression, so that code like:
3487 template <int N> struct A {
3488 int a[B<N>::i];
3491 is accepted. At template-instantiation time, we
3492 will check that B<N>::i is actually a constant. */
3493 return decl;
3495 /* Check to see if DECL is a local variable in a context
3496 where that is forbidden. */
3497 if (parser->local_variables_forbidden_p
3498 && local_variable_p (decl))
3500 /* It might be that we only found DECL because we are
3501 trying to be generous with pre-ISO scoping rules.
3502 For example, consider:
3504 int i;
3505 void g() {
3506 for (int i = 0; i < 10; ++i) {}
3507 extern void f(int j = i);
3510 Here, name look up will originally find the out
3511 of scope `i'. We need to issue a warning message,
3512 but then use the global `i'. */
3513 decl = check_for_out_of_scope_variable (decl);
3514 if (local_variable_p (decl))
3516 error_at (id_expr_token->location,
3517 "local variable %qD may not appear in this context",
3518 decl);
3519 return error_mark_node;
3524 decl = (finish_id_expression
3525 (id_expression, decl, parser->scope,
3526 idk,
3527 parser->integral_constant_expression_p,
3528 parser->allow_non_integral_constant_expression_p,
3529 &parser->non_integral_constant_expression_p,
3530 template_p, done, address_p,
3531 template_arg_p,
3532 &error_msg,
3533 id_expr_token->location));
3534 if (error_msg)
3535 cp_parser_error (parser, error_msg);
3536 return decl;
3539 /* Anything else is an error. */
3540 default:
3541 /* ...unless we have an Objective-C++ message or string literal,
3542 that is. */
3543 if (c_dialect_objc ()
3544 && (token->type == CPP_OPEN_SQUARE
3545 || token->type == CPP_OBJC_STRING))
3546 return cp_parser_objc_expression (parser);
3548 cp_parser_error (parser, "expected primary-expression");
3549 return error_mark_node;
3553 /* Parse an id-expression.
3555 id-expression:
3556 unqualified-id
3557 qualified-id
3559 qualified-id:
3560 :: [opt] nested-name-specifier template [opt] unqualified-id
3561 :: identifier
3562 :: operator-function-id
3563 :: template-id
3565 Return a representation of the unqualified portion of the
3566 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3567 a `::' or nested-name-specifier.
3569 Often, if the id-expression was a qualified-id, the caller will
3570 want to make a SCOPE_REF to represent the qualified-id. This
3571 function does not do this in order to avoid wastefully creating
3572 SCOPE_REFs when they are not required.
3574 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3575 `template' keyword.
3577 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3578 uninstantiated templates.
3580 If *TEMPLATE_P is non-NULL, it is set to true iff the
3581 `template' keyword is used to explicitly indicate that the entity
3582 named is a template.
3584 If DECLARATOR_P is true, the id-expression is appearing as part of
3585 a declarator, rather than as part of an expression. */
3587 static tree
3588 cp_parser_id_expression (cp_parser *parser,
3589 bool template_keyword_p,
3590 bool check_dependency_p,
3591 bool *template_p,
3592 bool declarator_p,
3593 bool optional_p)
3595 bool global_scope_p;
3596 bool nested_name_specifier_p;
3598 /* Assume the `template' keyword was not used. */
3599 if (template_p)
3600 *template_p = template_keyword_p;
3602 /* Look for the optional `::' operator. */
3603 global_scope_p
3604 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3605 != NULL_TREE);
3606 /* Look for the optional nested-name-specifier. */
3607 nested_name_specifier_p
3608 = (cp_parser_nested_name_specifier_opt (parser,
3609 /*typename_keyword_p=*/false,
3610 check_dependency_p,
3611 /*type_p=*/false,
3612 declarator_p)
3613 != NULL_TREE);
3614 /* If there is a nested-name-specifier, then we are looking at
3615 the first qualified-id production. */
3616 if (nested_name_specifier_p)
3618 tree saved_scope;
3619 tree saved_object_scope;
3620 tree saved_qualifying_scope;
3621 tree unqualified_id;
3622 bool is_template;
3624 /* See if the next token is the `template' keyword. */
3625 if (!template_p)
3626 template_p = &is_template;
3627 *template_p = cp_parser_optional_template_keyword (parser);
3628 /* Name lookup we do during the processing of the
3629 unqualified-id might obliterate SCOPE. */
3630 saved_scope = parser->scope;
3631 saved_object_scope = parser->object_scope;
3632 saved_qualifying_scope = parser->qualifying_scope;
3633 /* Process the final unqualified-id. */
3634 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3635 check_dependency_p,
3636 declarator_p,
3637 /*optional_p=*/false);
3638 /* Restore the SAVED_SCOPE for our caller. */
3639 parser->scope = saved_scope;
3640 parser->object_scope = saved_object_scope;
3641 parser->qualifying_scope = saved_qualifying_scope;
3643 return unqualified_id;
3645 /* Otherwise, if we are in global scope, then we are looking at one
3646 of the other qualified-id productions. */
3647 else if (global_scope_p)
3649 cp_token *token;
3650 tree id;
3652 /* Peek at the next token. */
3653 token = cp_lexer_peek_token (parser->lexer);
3655 /* If it's an identifier, and the next token is not a "<", then
3656 we can avoid the template-id case. This is an optimization
3657 for this common case. */
3658 if (token->type == CPP_NAME
3659 && !cp_parser_nth_token_starts_template_argument_list_p
3660 (parser, 2))
3661 return cp_parser_identifier (parser);
3663 cp_parser_parse_tentatively (parser);
3664 /* Try a template-id. */
3665 id = cp_parser_template_id (parser,
3666 /*template_keyword_p=*/false,
3667 /*check_dependency_p=*/true,
3668 declarator_p);
3669 /* If that worked, we're done. */
3670 if (cp_parser_parse_definitely (parser))
3671 return id;
3673 /* Peek at the next token. (Changes in the token buffer may
3674 have invalidated the pointer obtained above.) */
3675 token = cp_lexer_peek_token (parser->lexer);
3677 switch (token->type)
3679 case CPP_NAME:
3680 return cp_parser_identifier (parser);
3682 case CPP_KEYWORD:
3683 if (token->keyword == RID_OPERATOR)
3684 return cp_parser_operator_function_id (parser);
3685 /* Fall through. */
3687 default:
3688 cp_parser_error (parser, "expected id-expression");
3689 return error_mark_node;
3692 else
3693 return cp_parser_unqualified_id (parser, template_keyword_p,
3694 /*check_dependency_p=*/true,
3695 declarator_p,
3696 optional_p);
3699 /* Parse an unqualified-id.
3701 unqualified-id:
3702 identifier
3703 operator-function-id
3704 conversion-function-id
3705 ~ class-name
3706 template-id
3708 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3709 keyword, in a construct like `A::template ...'.
3711 Returns a representation of unqualified-id. For the `identifier'
3712 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3713 production a BIT_NOT_EXPR is returned; the operand of the
3714 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3715 other productions, see the documentation accompanying the
3716 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3717 names are looked up in uninstantiated templates. If DECLARATOR_P
3718 is true, the unqualified-id is appearing as part of a declarator,
3719 rather than as part of an expression. */
3721 static tree
3722 cp_parser_unqualified_id (cp_parser* parser,
3723 bool template_keyword_p,
3724 bool check_dependency_p,
3725 bool declarator_p,
3726 bool optional_p)
3728 cp_token *token;
3730 /* Peek at the next token. */
3731 token = cp_lexer_peek_token (parser->lexer);
3733 switch (token->type)
3735 case CPP_NAME:
3737 tree id;
3739 /* We don't know yet whether or not this will be a
3740 template-id. */
3741 cp_parser_parse_tentatively (parser);
3742 /* Try a template-id. */
3743 id = cp_parser_template_id (parser, template_keyword_p,
3744 check_dependency_p,
3745 declarator_p);
3746 /* If it worked, we're done. */
3747 if (cp_parser_parse_definitely (parser))
3748 return id;
3749 /* Otherwise, it's an ordinary identifier. */
3750 return cp_parser_identifier (parser);
3753 case CPP_TEMPLATE_ID:
3754 return cp_parser_template_id (parser, template_keyword_p,
3755 check_dependency_p,
3756 declarator_p);
3758 case CPP_COMPL:
3760 tree type_decl;
3761 tree qualifying_scope;
3762 tree object_scope;
3763 tree scope;
3764 bool done;
3766 /* Consume the `~' token. */
3767 cp_lexer_consume_token (parser->lexer);
3768 /* Parse the class-name. The standard, as written, seems to
3769 say that:
3771 template <typename T> struct S { ~S (); };
3772 template <typename T> S<T>::~S() {}
3774 is invalid, since `~' must be followed by a class-name, but
3775 `S<T>' is dependent, and so not known to be a class.
3776 That's not right; we need to look in uninstantiated
3777 templates. A further complication arises from:
3779 template <typename T> void f(T t) {
3780 t.T::~T();
3783 Here, it is not possible to look up `T' in the scope of `T'
3784 itself. We must look in both the current scope, and the
3785 scope of the containing complete expression.
3787 Yet another issue is:
3789 struct S {
3790 int S;
3791 ~S();
3794 S::~S() {}
3796 The standard does not seem to say that the `S' in `~S'
3797 should refer to the type `S' and not the data member
3798 `S::S'. */
3800 /* DR 244 says that we look up the name after the "~" in the
3801 same scope as we looked up the qualifying name. That idea
3802 isn't fully worked out; it's more complicated than that. */
3803 scope = parser->scope;
3804 object_scope = parser->object_scope;
3805 qualifying_scope = parser->qualifying_scope;
3807 /* Check for invalid scopes. */
3808 if (scope == error_mark_node)
3810 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3811 cp_lexer_consume_token (parser->lexer);
3812 return error_mark_node;
3814 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3816 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3817 error_at (token->location,
3818 "scope %qT before %<~%> is not a class-name",
3819 scope);
3820 cp_parser_simulate_error (parser);
3821 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3822 cp_lexer_consume_token (parser->lexer);
3823 return error_mark_node;
3825 gcc_assert (!scope || TYPE_P (scope));
3827 /* If the name is of the form "X::~X" it's OK. */
3828 token = cp_lexer_peek_token (parser->lexer);
3829 if (scope
3830 && token->type == CPP_NAME
3831 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3832 == CPP_OPEN_PAREN)
3833 && constructor_name_p (token->u.value, scope))
3835 cp_lexer_consume_token (parser->lexer);
3836 return build_nt (BIT_NOT_EXPR, scope);
3839 /* If there was an explicit qualification (S::~T), first look
3840 in the scope given by the qualification (i.e., S). */
3841 done = false;
3842 type_decl = NULL_TREE;
3843 if (scope)
3845 cp_parser_parse_tentatively (parser);
3846 type_decl = cp_parser_class_name (parser,
3847 /*typename_keyword_p=*/false,
3848 /*template_keyword_p=*/false,
3849 none_type,
3850 /*check_dependency=*/false,
3851 /*class_head_p=*/false,
3852 declarator_p);
3853 if (cp_parser_parse_definitely (parser))
3854 done = true;
3856 /* In "N::S::~S", look in "N" as well. */
3857 if (!done && scope && qualifying_scope)
3859 cp_parser_parse_tentatively (parser);
3860 parser->scope = qualifying_scope;
3861 parser->object_scope = NULL_TREE;
3862 parser->qualifying_scope = NULL_TREE;
3863 type_decl
3864 = cp_parser_class_name (parser,
3865 /*typename_keyword_p=*/false,
3866 /*template_keyword_p=*/false,
3867 none_type,
3868 /*check_dependency=*/false,
3869 /*class_head_p=*/false,
3870 declarator_p);
3871 if (cp_parser_parse_definitely (parser))
3872 done = true;
3874 /* In "p->S::~T", look in the scope given by "*p" as well. */
3875 else if (!done && object_scope)
3877 cp_parser_parse_tentatively (parser);
3878 parser->scope = object_scope;
3879 parser->object_scope = NULL_TREE;
3880 parser->qualifying_scope = NULL_TREE;
3881 type_decl
3882 = cp_parser_class_name (parser,
3883 /*typename_keyword_p=*/false,
3884 /*template_keyword_p=*/false,
3885 none_type,
3886 /*check_dependency=*/false,
3887 /*class_head_p=*/false,
3888 declarator_p);
3889 if (cp_parser_parse_definitely (parser))
3890 done = true;
3892 /* Look in the surrounding context. */
3893 if (!done)
3895 parser->scope = NULL_TREE;
3896 parser->object_scope = NULL_TREE;
3897 parser->qualifying_scope = NULL_TREE;
3898 if (processing_template_decl)
3899 cp_parser_parse_tentatively (parser);
3900 type_decl
3901 = cp_parser_class_name (parser,
3902 /*typename_keyword_p=*/false,
3903 /*template_keyword_p=*/false,
3904 none_type,
3905 /*check_dependency=*/false,
3906 /*class_head_p=*/false,
3907 declarator_p);
3908 if (processing_template_decl
3909 && ! cp_parser_parse_definitely (parser))
3911 /* We couldn't find a type with this name, so just accept
3912 it and check for a match at instantiation time. */
3913 type_decl = cp_parser_identifier (parser);
3914 if (type_decl != error_mark_node)
3915 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
3916 return type_decl;
3919 /* If an error occurred, assume that the name of the
3920 destructor is the same as the name of the qualifying
3921 class. That allows us to keep parsing after running
3922 into ill-formed destructor names. */
3923 if (type_decl == error_mark_node && scope)
3924 return build_nt (BIT_NOT_EXPR, scope);
3925 else if (type_decl == error_mark_node)
3926 return error_mark_node;
3928 /* Check that destructor name and scope match. */
3929 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3931 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3932 error_at (token->location,
3933 "declaration of %<~%T%> as member of %qT",
3934 type_decl, scope);
3935 cp_parser_simulate_error (parser);
3936 return error_mark_node;
3939 /* [class.dtor]
3941 A typedef-name that names a class shall not be used as the
3942 identifier in the declarator for a destructor declaration. */
3943 if (declarator_p
3944 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3945 && !DECL_SELF_REFERENCE_P (type_decl)
3946 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3947 error_at (token->location,
3948 "typedef-name %qD used as destructor declarator",
3949 type_decl);
3951 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3954 case CPP_KEYWORD:
3955 if (token->keyword == RID_OPERATOR)
3957 tree id;
3959 /* This could be a template-id, so we try that first. */
3960 cp_parser_parse_tentatively (parser);
3961 /* Try a template-id. */
3962 id = cp_parser_template_id (parser, template_keyword_p,
3963 /*check_dependency_p=*/true,
3964 declarator_p);
3965 /* If that worked, we're done. */
3966 if (cp_parser_parse_definitely (parser))
3967 return id;
3968 /* We still don't know whether we're looking at an
3969 operator-function-id or a conversion-function-id. */
3970 cp_parser_parse_tentatively (parser);
3971 /* Try an operator-function-id. */
3972 id = cp_parser_operator_function_id (parser);
3973 /* If that didn't work, try a conversion-function-id. */
3974 if (!cp_parser_parse_definitely (parser))
3975 id = cp_parser_conversion_function_id (parser);
3977 return id;
3979 /* Fall through. */
3981 default:
3982 if (optional_p)
3983 return NULL_TREE;
3984 cp_parser_error (parser, "expected unqualified-id");
3985 return error_mark_node;
3989 /* Parse an (optional) nested-name-specifier.
3991 nested-name-specifier: [C++98]
3992 class-or-namespace-name :: nested-name-specifier [opt]
3993 class-or-namespace-name :: template nested-name-specifier [opt]
3995 nested-name-specifier: [C++0x]
3996 type-name ::
3997 namespace-name ::
3998 nested-name-specifier identifier ::
3999 nested-name-specifier template [opt] simple-template-id ::
4001 PARSER->SCOPE should be set appropriately before this function is
4002 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4003 effect. TYPE_P is TRUE if we non-type bindings should be ignored
4004 in name lookups.
4006 Sets PARSER->SCOPE to the class (TYPE) or namespace
4007 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4008 it unchanged if there is no nested-name-specifier. Returns the new
4009 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4011 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4012 part of a declaration and/or decl-specifier. */
4014 static tree
4015 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4016 bool typename_keyword_p,
4017 bool check_dependency_p,
4018 bool type_p,
4019 bool is_declaration)
4021 bool success = false;
4022 cp_token_position start = 0;
4023 cp_token *token;
4025 /* Remember where the nested-name-specifier starts. */
4026 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4028 start = cp_lexer_token_position (parser->lexer, false);
4029 push_deferring_access_checks (dk_deferred);
4032 while (true)
4034 tree new_scope;
4035 tree old_scope;
4036 tree saved_qualifying_scope;
4037 bool template_keyword_p;
4039 /* Spot cases that cannot be the beginning of a
4040 nested-name-specifier. */
4041 token = cp_lexer_peek_token (parser->lexer);
4043 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4044 the already parsed nested-name-specifier. */
4045 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4047 /* Grab the nested-name-specifier and continue the loop. */
4048 cp_parser_pre_parsed_nested_name_specifier (parser);
4049 /* If we originally encountered this nested-name-specifier
4050 with IS_DECLARATION set to false, we will not have
4051 resolved TYPENAME_TYPEs, so we must do so here. */
4052 if (is_declaration
4053 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4055 new_scope = resolve_typename_type (parser->scope,
4056 /*only_current_p=*/false);
4057 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4058 parser->scope = new_scope;
4060 success = true;
4061 continue;
4064 /* Spot cases that cannot be the beginning of a
4065 nested-name-specifier. On the second and subsequent times
4066 through the loop, we look for the `template' keyword. */
4067 if (success && token->keyword == RID_TEMPLATE)
4069 /* A template-id can start a nested-name-specifier. */
4070 else if (token->type == CPP_TEMPLATE_ID)
4072 else
4074 /* If the next token is not an identifier, then it is
4075 definitely not a type-name or namespace-name. */
4076 if (token->type != CPP_NAME)
4077 break;
4078 /* If the following token is neither a `<' (to begin a
4079 template-id), nor a `::', then we are not looking at a
4080 nested-name-specifier. */
4081 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4082 if (token->type != CPP_SCOPE
4083 && !cp_parser_nth_token_starts_template_argument_list_p
4084 (parser, 2))
4085 break;
4088 /* The nested-name-specifier is optional, so we parse
4089 tentatively. */
4090 cp_parser_parse_tentatively (parser);
4092 /* Look for the optional `template' keyword, if this isn't the
4093 first time through the loop. */
4094 if (success)
4095 template_keyword_p = cp_parser_optional_template_keyword (parser);
4096 else
4097 template_keyword_p = false;
4099 /* Save the old scope since the name lookup we are about to do
4100 might destroy it. */
4101 old_scope = parser->scope;
4102 saved_qualifying_scope = parser->qualifying_scope;
4103 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4104 look up names in "X<T>::I" in order to determine that "Y" is
4105 a template. So, if we have a typename at this point, we make
4106 an effort to look through it. */
4107 if (is_declaration
4108 && !typename_keyword_p
4109 && parser->scope
4110 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4111 parser->scope = resolve_typename_type (parser->scope,
4112 /*only_current_p=*/false);
4113 /* Parse the qualifying entity. */
4114 new_scope
4115 = cp_parser_qualifying_entity (parser,
4116 typename_keyword_p,
4117 template_keyword_p,
4118 check_dependency_p,
4119 type_p,
4120 is_declaration);
4121 /* Look for the `::' token. */
4122 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4124 /* If we found what we wanted, we keep going; otherwise, we're
4125 done. */
4126 if (!cp_parser_parse_definitely (parser))
4128 bool error_p = false;
4130 /* Restore the OLD_SCOPE since it was valid before the
4131 failed attempt at finding the last
4132 class-or-namespace-name. */
4133 parser->scope = old_scope;
4134 parser->qualifying_scope = saved_qualifying_scope;
4135 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4136 break;
4137 /* If the next token is an identifier, and the one after
4138 that is a `::', then any valid interpretation would have
4139 found a class-or-namespace-name. */
4140 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4141 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4142 == CPP_SCOPE)
4143 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4144 != CPP_COMPL))
4146 token = cp_lexer_consume_token (parser->lexer);
4147 if (!error_p)
4149 if (!token->ambiguous_p)
4151 tree decl;
4152 tree ambiguous_decls;
4154 decl = cp_parser_lookup_name (parser, token->u.value,
4155 none_type,
4156 /*is_template=*/false,
4157 /*is_namespace=*/false,
4158 /*check_dependency=*/true,
4159 &ambiguous_decls,
4160 token->location);
4161 if (TREE_CODE (decl) == TEMPLATE_DECL)
4162 error_at (token->location,
4163 "%qD used without template parameters",
4164 decl);
4165 else if (ambiguous_decls)
4167 error_at (token->location,
4168 "reference to %qD is ambiguous",
4169 token->u.value);
4170 print_candidates (ambiguous_decls);
4171 decl = error_mark_node;
4173 else
4175 const char* msg = "is not a class or namespace";
4176 if (cxx_dialect != cxx98)
4177 msg = "is not a class, namespace, or enumeration";
4178 cp_parser_name_lookup_error
4179 (parser, token->u.value, decl, msg,
4180 token->location);
4183 parser->scope = error_mark_node;
4184 error_p = true;
4185 /* Treat this as a successful nested-name-specifier
4186 due to:
4188 [basic.lookup.qual]
4190 If the name found is not a class-name (clause
4191 _class_) or namespace-name (_namespace.def_), the
4192 program is ill-formed. */
4193 success = true;
4195 cp_lexer_consume_token (parser->lexer);
4197 break;
4199 /* We've found one valid nested-name-specifier. */
4200 success = true;
4201 /* Name lookup always gives us a DECL. */
4202 if (TREE_CODE (new_scope) == TYPE_DECL)
4203 new_scope = TREE_TYPE (new_scope);
4204 /* Uses of "template" must be followed by actual templates. */
4205 if (template_keyword_p
4206 && !(CLASS_TYPE_P (new_scope)
4207 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4208 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4209 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4210 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4211 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4212 == TEMPLATE_ID_EXPR)))
4213 permerror (input_location, TYPE_P (new_scope)
4214 ? "%qT is not a template"
4215 : "%qD is not a template",
4216 new_scope);
4217 /* If it is a class scope, try to complete it; we are about to
4218 be looking up names inside the class. */
4219 if (TYPE_P (new_scope)
4220 /* Since checking types for dependency can be expensive,
4221 avoid doing it if the type is already complete. */
4222 && !COMPLETE_TYPE_P (new_scope)
4223 /* Do not try to complete dependent types. */
4224 && !dependent_type_p (new_scope))
4226 new_scope = complete_type (new_scope);
4227 /* If it is a typedef to current class, use the current
4228 class instead, as the typedef won't have any names inside
4229 it yet. */
4230 if (!COMPLETE_TYPE_P (new_scope)
4231 && currently_open_class (new_scope))
4232 new_scope = TYPE_MAIN_VARIANT (new_scope);
4234 /* Make sure we look in the right scope the next time through
4235 the loop. */
4236 parser->scope = new_scope;
4239 /* If parsing tentatively, replace the sequence of tokens that makes
4240 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4241 token. That way, should we re-parse the token stream, we will
4242 not have to repeat the effort required to do the parse, nor will
4243 we issue duplicate error messages. */
4244 if (success && start)
4246 cp_token *token;
4248 token = cp_lexer_token_at (parser->lexer, start);
4249 /* Reset the contents of the START token. */
4250 token->type = CPP_NESTED_NAME_SPECIFIER;
4251 /* Retrieve any deferred checks. Do not pop this access checks yet
4252 so the memory will not be reclaimed during token replacing below. */
4253 token->u.tree_check_value = GGC_CNEW (struct tree_check);
4254 token->u.tree_check_value->value = parser->scope;
4255 token->u.tree_check_value->checks = get_deferred_access_checks ();
4256 token->u.tree_check_value->qualifying_scope =
4257 parser->qualifying_scope;
4258 token->keyword = RID_MAX;
4260 /* Purge all subsequent tokens. */
4261 cp_lexer_purge_tokens_after (parser->lexer, start);
4264 if (start)
4265 pop_to_parent_deferring_access_checks ();
4267 return success ? parser->scope : NULL_TREE;
4270 /* Parse a nested-name-specifier. See
4271 cp_parser_nested_name_specifier_opt for details. This function
4272 behaves identically, except that it will an issue an error if no
4273 nested-name-specifier is present. */
4275 static tree
4276 cp_parser_nested_name_specifier (cp_parser *parser,
4277 bool typename_keyword_p,
4278 bool check_dependency_p,
4279 bool type_p,
4280 bool is_declaration)
4282 tree scope;
4284 /* Look for the nested-name-specifier. */
4285 scope = cp_parser_nested_name_specifier_opt (parser,
4286 typename_keyword_p,
4287 check_dependency_p,
4288 type_p,
4289 is_declaration);
4290 /* If it was not present, issue an error message. */
4291 if (!scope)
4293 cp_parser_error (parser, "expected nested-name-specifier");
4294 parser->scope = NULL_TREE;
4297 return scope;
4300 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4301 this is either a class-name or a namespace-name (which corresponds
4302 to the class-or-namespace-name production in the grammar). For
4303 C++0x, it can also be a type-name that refers to an enumeration
4304 type.
4306 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4307 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4308 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4309 TYPE_P is TRUE iff the next name should be taken as a class-name,
4310 even the same name is declared to be another entity in the same
4311 scope.
4313 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4314 specified by the class-or-namespace-name. If neither is found the
4315 ERROR_MARK_NODE is returned. */
4317 static tree
4318 cp_parser_qualifying_entity (cp_parser *parser,
4319 bool typename_keyword_p,
4320 bool template_keyword_p,
4321 bool check_dependency_p,
4322 bool type_p,
4323 bool is_declaration)
4325 tree saved_scope;
4326 tree saved_qualifying_scope;
4327 tree saved_object_scope;
4328 tree scope;
4329 bool only_class_p;
4330 bool successful_parse_p;
4332 /* Before we try to parse the class-name, we must save away the
4333 current PARSER->SCOPE since cp_parser_class_name will destroy
4334 it. */
4335 saved_scope = parser->scope;
4336 saved_qualifying_scope = parser->qualifying_scope;
4337 saved_object_scope = parser->object_scope;
4338 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4339 there is no need to look for a namespace-name. */
4340 only_class_p = template_keyword_p
4341 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4342 if (!only_class_p)
4343 cp_parser_parse_tentatively (parser);
4344 scope = cp_parser_class_name (parser,
4345 typename_keyword_p,
4346 template_keyword_p,
4347 type_p ? class_type : none_type,
4348 check_dependency_p,
4349 /*class_head_p=*/false,
4350 is_declaration);
4351 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4352 /* If that didn't work and we're in C++0x mode, try for a type-name. */
4353 if (!only_class_p
4354 && cxx_dialect != cxx98
4355 && !successful_parse_p)
4357 /* Restore the saved scope. */
4358 parser->scope = saved_scope;
4359 parser->qualifying_scope = saved_qualifying_scope;
4360 parser->object_scope = saved_object_scope;
4362 /* Parse tentatively. */
4363 cp_parser_parse_tentatively (parser);
4365 /* Parse a typedef-name or enum-name. */
4366 scope = cp_parser_nonclass_name (parser);
4367 successful_parse_p = cp_parser_parse_definitely (parser);
4369 /* If that didn't work, try for a namespace-name. */
4370 if (!only_class_p && !successful_parse_p)
4372 /* Restore the saved scope. */
4373 parser->scope = saved_scope;
4374 parser->qualifying_scope = saved_qualifying_scope;
4375 parser->object_scope = saved_object_scope;
4376 /* If we are not looking at an identifier followed by the scope
4377 resolution operator, then this is not part of a
4378 nested-name-specifier. (Note that this function is only used
4379 to parse the components of a nested-name-specifier.) */
4380 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4381 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4382 return error_mark_node;
4383 scope = cp_parser_namespace_name (parser);
4386 return scope;
4389 /* Parse a postfix-expression.
4391 postfix-expression:
4392 primary-expression
4393 postfix-expression [ expression ]
4394 postfix-expression ( expression-list [opt] )
4395 simple-type-specifier ( expression-list [opt] )
4396 typename :: [opt] nested-name-specifier identifier
4397 ( expression-list [opt] )
4398 typename :: [opt] nested-name-specifier template [opt] template-id
4399 ( expression-list [opt] )
4400 postfix-expression . template [opt] id-expression
4401 postfix-expression -> template [opt] id-expression
4402 postfix-expression . pseudo-destructor-name
4403 postfix-expression -> pseudo-destructor-name
4404 postfix-expression ++
4405 postfix-expression --
4406 dynamic_cast < type-id > ( expression )
4407 static_cast < type-id > ( expression )
4408 reinterpret_cast < type-id > ( expression )
4409 const_cast < type-id > ( expression )
4410 typeid ( expression )
4411 typeid ( type-id )
4413 GNU Extension:
4415 postfix-expression:
4416 ( type-id ) { initializer-list , [opt] }
4418 This extension is a GNU version of the C99 compound-literal
4419 construct. (The C99 grammar uses `type-name' instead of `type-id',
4420 but they are essentially the same concept.)
4422 If ADDRESS_P is true, the postfix expression is the operand of the
4423 `&' operator. CAST_P is true if this expression is the target of a
4424 cast.
4426 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4427 class member access expressions [expr.ref].
4429 Returns a representation of the expression. */
4431 static tree
4432 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4433 bool member_access_only_p,
4434 cp_id_kind * pidk_return)
4436 cp_token *token;
4437 enum rid keyword;
4438 cp_id_kind idk = CP_ID_KIND_NONE;
4439 tree postfix_expression = NULL_TREE;
4440 bool is_member_access = false;
4442 /* Peek at the next token. */
4443 token = cp_lexer_peek_token (parser->lexer);
4444 /* Some of the productions are determined by keywords. */
4445 keyword = token->keyword;
4446 switch (keyword)
4448 case RID_DYNCAST:
4449 case RID_STATCAST:
4450 case RID_REINTCAST:
4451 case RID_CONSTCAST:
4453 tree type;
4454 tree expression;
4455 const char *saved_message;
4457 /* All of these can be handled in the same way from the point
4458 of view of parsing. Begin by consuming the token
4459 identifying the cast. */
4460 cp_lexer_consume_token (parser->lexer);
4462 /* New types cannot be defined in the cast. */
4463 saved_message = parser->type_definition_forbidden_message;
4464 parser->type_definition_forbidden_message
4465 = "types may not be defined in casts";
4467 /* Look for the opening `<'. */
4468 cp_parser_require (parser, CPP_LESS, "%<<%>");
4469 /* Parse the type to which we are casting. */
4470 type = cp_parser_type_id (parser);
4471 /* Look for the closing `>'. */
4472 cp_parser_require (parser, CPP_GREATER, "%<>%>");
4473 /* Restore the old message. */
4474 parser->type_definition_forbidden_message = saved_message;
4476 /* And the expression which is being cast. */
4477 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4478 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4479 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4481 /* Only type conversions to integral or enumeration types
4482 can be used in constant-expressions. */
4483 if (!cast_valid_in_integral_constant_expression_p (type)
4484 && (cp_parser_non_integral_constant_expression
4485 (parser,
4486 "a cast to a type other than an integral or "
4487 "enumeration type")))
4488 return error_mark_node;
4490 switch (keyword)
4492 case RID_DYNCAST:
4493 postfix_expression
4494 = build_dynamic_cast (type, expression, tf_warning_or_error);
4495 break;
4496 case RID_STATCAST:
4497 postfix_expression
4498 = build_static_cast (type, expression, tf_warning_or_error);
4499 break;
4500 case RID_REINTCAST:
4501 postfix_expression
4502 = build_reinterpret_cast (type, expression,
4503 tf_warning_or_error);
4504 break;
4505 case RID_CONSTCAST:
4506 postfix_expression
4507 = build_const_cast (type, expression, tf_warning_or_error);
4508 break;
4509 default:
4510 gcc_unreachable ();
4513 break;
4515 case RID_TYPEID:
4517 tree type;
4518 const char *saved_message;
4519 bool saved_in_type_id_in_expr_p;
4521 /* Consume the `typeid' token. */
4522 cp_lexer_consume_token (parser->lexer);
4523 /* Look for the `(' token. */
4524 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4525 /* Types cannot be defined in a `typeid' expression. */
4526 saved_message = parser->type_definition_forbidden_message;
4527 parser->type_definition_forbidden_message
4528 = "types may not be defined in a %<typeid%> expression";
4529 /* We can't be sure yet whether we're looking at a type-id or an
4530 expression. */
4531 cp_parser_parse_tentatively (parser);
4532 /* Try a type-id first. */
4533 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4534 parser->in_type_id_in_expr_p = true;
4535 type = cp_parser_type_id (parser);
4536 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4537 /* Look for the `)' token. Otherwise, we can't be sure that
4538 we're not looking at an expression: consider `typeid (int
4539 (3))', for example. */
4540 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4541 /* If all went well, simply lookup the type-id. */
4542 if (cp_parser_parse_definitely (parser))
4543 postfix_expression = get_typeid (type);
4544 /* Otherwise, fall back to the expression variant. */
4545 else
4547 tree expression;
4549 /* Look for an expression. */
4550 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4551 /* Compute its typeid. */
4552 postfix_expression = build_typeid (expression);
4553 /* Look for the `)' token. */
4554 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4556 /* Restore the saved message. */
4557 parser->type_definition_forbidden_message = saved_message;
4558 /* `typeid' may not appear in an integral constant expression. */
4559 if (cp_parser_non_integral_constant_expression(parser,
4560 "%<typeid%> operator"))
4561 return error_mark_node;
4563 break;
4565 case RID_TYPENAME:
4567 tree type;
4568 /* The syntax permitted here is the same permitted for an
4569 elaborated-type-specifier. */
4570 type = cp_parser_elaborated_type_specifier (parser,
4571 /*is_friend=*/false,
4572 /*is_declaration=*/false);
4573 postfix_expression = cp_parser_functional_cast (parser, type);
4575 break;
4577 default:
4579 tree type;
4581 /* If the next thing is a simple-type-specifier, we may be
4582 looking at a functional cast. We could also be looking at
4583 an id-expression. So, we try the functional cast, and if
4584 that doesn't work we fall back to the primary-expression. */
4585 cp_parser_parse_tentatively (parser);
4586 /* Look for the simple-type-specifier. */
4587 type = cp_parser_simple_type_specifier (parser,
4588 /*decl_specs=*/NULL,
4589 CP_PARSER_FLAGS_NONE);
4590 /* Parse the cast itself. */
4591 if (!cp_parser_error_occurred (parser))
4592 postfix_expression
4593 = cp_parser_functional_cast (parser, type);
4594 /* If that worked, we're done. */
4595 if (cp_parser_parse_definitely (parser))
4596 break;
4598 /* If the functional-cast didn't work out, try a
4599 compound-literal. */
4600 if (cp_parser_allow_gnu_extensions_p (parser)
4601 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4603 VEC(constructor_elt,gc) *initializer_list = NULL;
4604 bool saved_in_type_id_in_expr_p;
4606 cp_parser_parse_tentatively (parser);
4607 /* Consume the `('. */
4608 cp_lexer_consume_token (parser->lexer);
4609 /* Parse the type. */
4610 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4611 parser->in_type_id_in_expr_p = true;
4612 type = cp_parser_type_id (parser);
4613 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4614 /* Look for the `)'. */
4615 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4616 /* Look for the `{'. */
4617 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4618 /* If things aren't going well, there's no need to
4619 keep going. */
4620 if (!cp_parser_error_occurred (parser))
4622 bool non_constant_p;
4623 /* Parse the initializer-list. */
4624 initializer_list
4625 = cp_parser_initializer_list (parser, &non_constant_p);
4626 /* Allow a trailing `,'. */
4627 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4628 cp_lexer_consume_token (parser->lexer);
4629 /* Look for the final `}'. */
4630 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4632 /* If that worked, we're definitely looking at a
4633 compound-literal expression. */
4634 if (cp_parser_parse_definitely (parser))
4636 /* Warn the user that a compound literal is not
4637 allowed in standard C++. */
4638 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4639 /* For simplicity, we disallow compound literals in
4640 constant-expressions. We could
4641 allow compound literals of integer type, whose
4642 initializer was a constant, in constant
4643 expressions. Permitting that usage, as a further
4644 extension, would not change the meaning of any
4645 currently accepted programs. (Of course, as
4646 compound literals are not part of ISO C++, the
4647 standard has nothing to say.) */
4648 if (cp_parser_non_integral_constant_expression
4649 (parser, "non-constant compound literals"))
4651 postfix_expression = error_mark_node;
4652 break;
4654 /* Form the representation of the compound-literal. */
4655 postfix_expression
4656 = (finish_compound_literal
4657 (type, build_constructor (init_list_type_node,
4658 initializer_list)));
4659 break;
4663 /* It must be a primary-expression. */
4664 postfix_expression
4665 = cp_parser_primary_expression (parser, address_p, cast_p,
4666 /*template_arg_p=*/false,
4667 &idk);
4669 break;
4672 /* Keep looping until the postfix-expression is complete. */
4673 while (true)
4675 if (idk == CP_ID_KIND_UNQUALIFIED
4676 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4677 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4678 /* It is not a Koenig lookup function call. */
4679 postfix_expression
4680 = unqualified_name_lookup_error (postfix_expression);
4682 /* Peek at the next token. */
4683 token = cp_lexer_peek_token (parser->lexer);
4685 switch (token->type)
4687 case CPP_OPEN_SQUARE:
4688 postfix_expression
4689 = cp_parser_postfix_open_square_expression (parser,
4690 postfix_expression,
4691 false);
4692 idk = CP_ID_KIND_NONE;
4693 is_member_access = false;
4694 break;
4696 case CPP_OPEN_PAREN:
4697 /* postfix-expression ( expression-list [opt] ) */
4699 bool koenig_p;
4700 bool is_builtin_constant_p;
4701 bool saved_integral_constant_expression_p = false;
4702 bool saved_non_integral_constant_expression_p = false;
4703 VEC(tree,gc) *args;
4705 is_member_access = false;
4707 is_builtin_constant_p
4708 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4709 if (is_builtin_constant_p)
4711 /* The whole point of __builtin_constant_p is to allow
4712 non-constant expressions to appear as arguments. */
4713 saved_integral_constant_expression_p
4714 = parser->integral_constant_expression_p;
4715 saved_non_integral_constant_expression_p
4716 = parser->non_integral_constant_expression_p;
4717 parser->integral_constant_expression_p = false;
4719 args = (cp_parser_parenthesized_expression_list
4720 (parser, /*is_attribute_list=*/false,
4721 /*cast_p=*/false, /*allow_expansion_p=*/true,
4722 /*non_constant_p=*/NULL));
4723 if (is_builtin_constant_p)
4725 parser->integral_constant_expression_p
4726 = saved_integral_constant_expression_p;
4727 parser->non_integral_constant_expression_p
4728 = saved_non_integral_constant_expression_p;
4731 if (args == NULL)
4733 postfix_expression = error_mark_node;
4734 break;
4737 /* Function calls are not permitted in
4738 constant-expressions. */
4739 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4740 && cp_parser_non_integral_constant_expression (parser,
4741 "a function call"))
4743 postfix_expression = error_mark_node;
4744 release_tree_vector (args);
4745 break;
4748 koenig_p = false;
4749 if (idk == CP_ID_KIND_UNQUALIFIED
4750 || idk == CP_ID_KIND_TEMPLATE_ID)
4752 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4754 if (!VEC_empty (tree, args))
4756 koenig_p = true;
4757 if (!any_type_dependent_arguments_p (args))
4758 postfix_expression
4759 = perform_koenig_lookup (postfix_expression, args);
4761 else
4762 postfix_expression
4763 = unqualified_fn_lookup_error (postfix_expression);
4765 /* We do not perform argument-dependent lookup if
4766 normal lookup finds a non-function, in accordance
4767 with the expected resolution of DR 218. */
4768 else if (!VEC_empty (tree, args)
4769 && is_overloaded_fn (postfix_expression))
4771 tree fn = get_first_fn (postfix_expression);
4773 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4774 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4776 /* Only do argument dependent lookup if regular
4777 lookup does not find a set of member functions.
4778 [basic.lookup.koenig]/2a */
4779 if (!DECL_FUNCTION_MEMBER_P (fn))
4781 koenig_p = true;
4782 if (!any_type_dependent_arguments_p (args))
4783 postfix_expression
4784 = perform_koenig_lookup (postfix_expression, args);
4789 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4791 tree instance = TREE_OPERAND (postfix_expression, 0);
4792 tree fn = TREE_OPERAND (postfix_expression, 1);
4794 if (processing_template_decl
4795 && (type_dependent_expression_p (instance)
4796 || (!BASELINK_P (fn)
4797 && TREE_CODE (fn) != FIELD_DECL)
4798 || type_dependent_expression_p (fn)
4799 || any_type_dependent_arguments_p (args)))
4801 postfix_expression
4802 = build_nt_call_vec (postfix_expression, args);
4803 release_tree_vector (args);
4804 break;
4807 if (BASELINK_P (fn))
4809 postfix_expression
4810 = (build_new_method_call
4811 (instance, fn, &args, NULL_TREE,
4812 (idk == CP_ID_KIND_QUALIFIED
4813 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4814 /*fn_p=*/NULL,
4815 tf_warning_or_error));
4817 else
4818 postfix_expression
4819 = finish_call_expr (postfix_expression, &args,
4820 /*disallow_virtual=*/false,
4821 /*koenig_p=*/false,
4822 tf_warning_or_error);
4824 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4825 || TREE_CODE (postfix_expression) == MEMBER_REF
4826 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4827 postfix_expression = (build_offset_ref_call_from_tree
4828 (postfix_expression, &args));
4829 else if (idk == CP_ID_KIND_QUALIFIED)
4830 /* A call to a static class member, or a namespace-scope
4831 function. */
4832 postfix_expression
4833 = finish_call_expr (postfix_expression, &args,
4834 /*disallow_virtual=*/true,
4835 koenig_p,
4836 tf_warning_or_error);
4837 else
4838 /* All other function calls. */
4839 postfix_expression
4840 = finish_call_expr (postfix_expression, &args,
4841 /*disallow_virtual=*/false,
4842 koenig_p,
4843 tf_warning_or_error);
4845 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4846 idk = CP_ID_KIND_NONE;
4848 release_tree_vector (args);
4850 break;
4852 case CPP_DOT:
4853 case CPP_DEREF:
4854 /* postfix-expression . template [opt] id-expression
4855 postfix-expression . pseudo-destructor-name
4856 postfix-expression -> template [opt] id-expression
4857 postfix-expression -> pseudo-destructor-name */
4859 /* Consume the `.' or `->' operator. */
4860 cp_lexer_consume_token (parser->lexer);
4862 postfix_expression
4863 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4864 postfix_expression,
4865 false, &idk,
4866 token->location);
4868 is_member_access = true;
4869 break;
4871 case CPP_PLUS_PLUS:
4872 /* postfix-expression ++ */
4873 /* Consume the `++' token. */
4874 cp_lexer_consume_token (parser->lexer);
4875 /* Generate a representation for the complete expression. */
4876 postfix_expression
4877 = finish_increment_expr (postfix_expression,
4878 POSTINCREMENT_EXPR);
4879 /* Increments may not appear in constant-expressions. */
4880 if (cp_parser_non_integral_constant_expression (parser,
4881 "an increment"))
4882 postfix_expression = error_mark_node;
4883 idk = CP_ID_KIND_NONE;
4884 is_member_access = false;
4885 break;
4887 case CPP_MINUS_MINUS:
4888 /* postfix-expression -- */
4889 /* Consume the `--' token. */
4890 cp_lexer_consume_token (parser->lexer);
4891 /* Generate a representation for the complete expression. */
4892 postfix_expression
4893 = finish_increment_expr (postfix_expression,
4894 POSTDECREMENT_EXPR);
4895 /* Decrements may not appear in constant-expressions. */
4896 if (cp_parser_non_integral_constant_expression (parser,
4897 "a decrement"))
4898 postfix_expression = error_mark_node;
4899 idk = CP_ID_KIND_NONE;
4900 is_member_access = false;
4901 break;
4903 default:
4904 if (pidk_return != NULL)
4905 * pidk_return = idk;
4906 if (member_access_only_p)
4907 return is_member_access? postfix_expression : error_mark_node;
4908 else
4909 return postfix_expression;
4913 /* We should never get here. */
4914 gcc_unreachable ();
4915 return error_mark_node;
4918 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4919 by cp_parser_builtin_offsetof. We're looking for
4921 postfix-expression [ expression ]
4923 FOR_OFFSETOF is set if we're being called in that context, which
4924 changes how we deal with integer constant expressions. */
4926 static tree
4927 cp_parser_postfix_open_square_expression (cp_parser *parser,
4928 tree postfix_expression,
4929 bool for_offsetof)
4931 tree index;
4933 /* Consume the `[' token. */
4934 cp_lexer_consume_token (parser->lexer);
4936 /* Parse the index expression. */
4937 /* ??? For offsetof, there is a question of what to allow here. If
4938 offsetof is not being used in an integral constant expression context,
4939 then we *could* get the right answer by computing the value at runtime.
4940 If we are in an integral constant expression context, then we might
4941 could accept any constant expression; hard to say without analysis.
4942 Rather than open the barn door too wide right away, allow only integer
4943 constant expressions here. */
4944 if (for_offsetof)
4945 index = cp_parser_constant_expression (parser, false, NULL);
4946 else
4947 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
4949 /* Look for the closing `]'. */
4950 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
4952 /* Build the ARRAY_REF. */
4953 postfix_expression = grok_array_decl (postfix_expression, index);
4955 /* When not doing offsetof, array references are not permitted in
4956 constant-expressions. */
4957 if (!for_offsetof
4958 && (cp_parser_non_integral_constant_expression
4959 (parser, "an array reference")))
4960 postfix_expression = error_mark_node;
4962 return postfix_expression;
4965 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4966 by cp_parser_builtin_offsetof. We're looking for
4968 postfix-expression . template [opt] id-expression
4969 postfix-expression . pseudo-destructor-name
4970 postfix-expression -> template [opt] id-expression
4971 postfix-expression -> pseudo-destructor-name
4973 FOR_OFFSETOF is set if we're being called in that context. That sorta
4974 limits what of the above we'll actually accept, but nevermind.
4975 TOKEN_TYPE is the "." or "->" token, which will already have been
4976 removed from the stream. */
4978 static tree
4979 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4980 enum cpp_ttype token_type,
4981 tree postfix_expression,
4982 bool for_offsetof, cp_id_kind *idk,
4983 location_t location)
4985 tree name;
4986 bool dependent_p;
4987 bool pseudo_destructor_p;
4988 tree scope = NULL_TREE;
4990 /* If this is a `->' operator, dereference the pointer. */
4991 if (token_type == CPP_DEREF)
4992 postfix_expression = build_x_arrow (postfix_expression);
4993 /* Check to see whether or not the expression is type-dependent. */
4994 dependent_p = type_dependent_expression_p (postfix_expression);
4995 /* The identifier following the `->' or `.' is not qualified. */
4996 parser->scope = NULL_TREE;
4997 parser->qualifying_scope = NULL_TREE;
4998 parser->object_scope = NULL_TREE;
4999 *idk = CP_ID_KIND_NONE;
5001 /* Enter the scope corresponding to the type of the object
5002 given by the POSTFIX_EXPRESSION. */
5003 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5005 scope = TREE_TYPE (postfix_expression);
5006 /* According to the standard, no expression should ever have
5007 reference type. Unfortunately, we do not currently match
5008 the standard in this respect in that our internal representation
5009 of an expression may have reference type even when the standard
5010 says it does not. Therefore, we have to manually obtain the
5011 underlying type here. */
5012 scope = non_reference (scope);
5013 /* The type of the POSTFIX_EXPRESSION must be complete. */
5014 if (scope == unknown_type_node)
5016 error_at (location, "%qE does not have class type",
5017 postfix_expression);
5018 scope = NULL_TREE;
5020 else
5021 scope = complete_type_or_else (scope, NULL_TREE);
5022 /* Let the name lookup machinery know that we are processing a
5023 class member access expression. */
5024 parser->context->object_type = scope;
5025 /* If something went wrong, we want to be able to discern that case,
5026 as opposed to the case where there was no SCOPE due to the type
5027 of expression being dependent. */
5028 if (!scope)
5029 scope = error_mark_node;
5030 /* If the SCOPE was erroneous, make the various semantic analysis
5031 functions exit quickly -- and without issuing additional error
5032 messages. */
5033 if (scope == error_mark_node)
5034 postfix_expression = error_mark_node;
5037 /* Assume this expression is not a pseudo-destructor access. */
5038 pseudo_destructor_p = false;
5040 /* If the SCOPE is a scalar type, then, if this is a valid program,
5041 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5042 is type dependent, it can be pseudo-destructor-name or something else.
5043 Try to parse it as pseudo-destructor-name first. */
5044 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5046 tree s;
5047 tree type;
5049 cp_parser_parse_tentatively (parser);
5050 /* Parse the pseudo-destructor-name. */
5051 s = NULL_TREE;
5052 cp_parser_pseudo_destructor_name (parser, &s, &type);
5053 if (dependent_p
5054 && (cp_parser_error_occurred (parser)
5055 || TREE_CODE (type) != TYPE_DECL
5056 || !SCALAR_TYPE_P (TREE_TYPE (type))))
5057 cp_parser_abort_tentative_parse (parser);
5058 else if (cp_parser_parse_definitely (parser))
5060 pseudo_destructor_p = true;
5061 postfix_expression
5062 = finish_pseudo_destructor_expr (postfix_expression,
5063 s, TREE_TYPE (type));
5067 if (!pseudo_destructor_p)
5069 /* If the SCOPE is not a scalar type, we are looking at an
5070 ordinary class member access expression, rather than a
5071 pseudo-destructor-name. */
5072 bool template_p;
5073 cp_token *token = cp_lexer_peek_token (parser->lexer);
5074 /* Parse the id-expression. */
5075 name = (cp_parser_id_expression
5076 (parser,
5077 cp_parser_optional_template_keyword (parser),
5078 /*check_dependency_p=*/true,
5079 &template_p,
5080 /*declarator_p=*/false,
5081 /*optional_p=*/false));
5082 /* In general, build a SCOPE_REF if the member name is qualified.
5083 However, if the name was not dependent and has already been
5084 resolved; there is no need to build the SCOPE_REF. For example;
5086 struct X { void f(); };
5087 template <typename T> void f(T* t) { t->X::f(); }
5089 Even though "t" is dependent, "X::f" is not and has been resolved
5090 to a BASELINK; there is no need to include scope information. */
5092 /* But we do need to remember that there was an explicit scope for
5093 virtual function calls. */
5094 if (parser->scope)
5095 *idk = CP_ID_KIND_QUALIFIED;
5097 /* If the name is a template-id that names a type, we will get a
5098 TYPE_DECL here. That is invalid code. */
5099 if (TREE_CODE (name) == TYPE_DECL)
5101 error_at (token->location, "invalid use of %qD", name);
5102 postfix_expression = error_mark_node;
5104 else
5106 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5108 name = build_qualified_name (/*type=*/NULL_TREE,
5109 parser->scope,
5110 name,
5111 template_p);
5112 parser->scope = NULL_TREE;
5113 parser->qualifying_scope = NULL_TREE;
5114 parser->object_scope = NULL_TREE;
5116 if (scope && name && BASELINK_P (name))
5117 adjust_result_of_qualified_name_lookup
5118 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5119 postfix_expression
5120 = finish_class_member_access_expr (postfix_expression, name,
5121 template_p,
5122 tf_warning_or_error);
5126 /* We no longer need to look up names in the scope of the object on
5127 the left-hand side of the `.' or `->' operator. */
5128 parser->context->object_type = NULL_TREE;
5130 /* Outside of offsetof, these operators may not appear in
5131 constant-expressions. */
5132 if (!for_offsetof
5133 && (cp_parser_non_integral_constant_expression
5134 (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5135 postfix_expression = error_mark_node;
5137 return postfix_expression;
5140 /* Parse a parenthesized expression-list.
5142 expression-list:
5143 assignment-expression
5144 expression-list, assignment-expression
5146 attribute-list:
5147 expression-list
5148 identifier
5149 identifier, expression-list
5151 CAST_P is true if this expression is the target of a cast.
5153 ALLOW_EXPANSION_P is true if this expression allows expansion of an
5154 argument pack.
5156 Returns a vector of trees. Each element is a representation of an
5157 assignment-expression. NULL is returned if the ( and or ) are
5158 missing. An empty, but allocated, vector is returned on no
5159 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is true
5160 if this is really an attribute list being parsed. If
5161 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5162 not all of the expressions in the list were constant. */
5164 static VEC(tree,gc) *
5165 cp_parser_parenthesized_expression_list (cp_parser* parser,
5166 bool is_attribute_list,
5167 bool cast_p,
5168 bool allow_expansion_p,
5169 bool *non_constant_p)
5171 VEC(tree,gc) *expression_list;
5172 bool fold_expr_p = is_attribute_list;
5173 tree identifier = NULL_TREE;
5174 bool saved_greater_than_is_operator_p;
5176 /* Assume all the expressions will be constant. */
5177 if (non_constant_p)
5178 *non_constant_p = false;
5180 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5181 return NULL;
5183 expression_list = make_tree_vector ();
5185 /* Within a parenthesized expression, a `>' token is always
5186 the greater-than operator. */
5187 saved_greater_than_is_operator_p
5188 = parser->greater_than_is_operator_p;
5189 parser->greater_than_is_operator_p = true;
5191 /* Consume expressions until there are no more. */
5192 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5193 while (true)
5195 tree expr;
5197 /* At the beginning of attribute lists, check to see if the
5198 next token is an identifier. */
5199 if (is_attribute_list
5200 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5202 cp_token *token;
5204 /* Consume the identifier. */
5205 token = cp_lexer_consume_token (parser->lexer);
5206 /* Save the identifier. */
5207 identifier = token->u.value;
5209 else
5211 bool expr_non_constant_p;
5213 /* Parse the next assignment-expression. */
5214 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5216 /* A braced-init-list. */
5217 maybe_warn_cpp0x ("extended initializer lists");
5218 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5219 if (non_constant_p && expr_non_constant_p)
5220 *non_constant_p = true;
5222 else if (non_constant_p)
5224 expr = (cp_parser_constant_expression
5225 (parser, /*allow_non_constant_p=*/true,
5226 &expr_non_constant_p));
5227 if (expr_non_constant_p)
5228 *non_constant_p = true;
5230 else
5231 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5233 if (fold_expr_p)
5234 expr = fold_non_dependent_expr (expr);
5236 /* If we have an ellipsis, then this is an expression
5237 expansion. */
5238 if (allow_expansion_p
5239 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5241 /* Consume the `...'. */
5242 cp_lexer_consume_token (parser->lexer);
5244 /* Build the argument pack. */
5245 expr = make_pack_expansion (expr);
5248 /* Add it to the list. We add error_mark_node
5249 expressions to the list, so that we can still tell if
5250 the correct form for a parenthesized expression-list
5251 is found. That gives better errors. */
5252 VEC_safe_push (tree, gc, expression_list, expr);
5254 if (expr == error_mark_node)
5255 goto skip_comma;
5258 /* After the first item, attribute lists look the same as
5259 expression lists. */
5260 is_attribute_list = false;
5262 get_comma:;
5263 /* If the next token isn't a `,', then we are done. */
5264 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5265 break;
5267 /* Otherwise, consume the `,' and keep going. */
5268 cp_lexer_consume_token (parser->lexer);
5271 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5273 int ending;
5275 skip_comma:;
5276 /* We try and resync to an unnested comma, as that will give the
5277 user better diagnostics. */
5278 ending = cp_parser_skip_to_closing_parenthesis (parser,
5279 /*recovering=*/true,
5280 /*or_comma=*/true,
5281 /*consume_paren=*/true);
5282 if (ending < 0)
5283 goto get_comma;
5284 if (!ending)
5286 parser->greater_than_is_operator_p
5287 = saved_greater_than_is_operator_p;
5288 return NULL;
5292 parser->greater_than_is_operator_p
5293 = saved_greater_than_is_operator_p;
5295 if (identifier)
5296 VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5298 return expression_list;
5301 /* Parse a pseudo-destructor-name.
5303 pseudo-destructor-name:
5304 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5305 :: [opt] nested-name-specifier template template-id :: ~ type-name
5306 :: [opt] nested-name-specifier [opt] ~ type-name
5308 If either of the first two productions is used, sets *SCOPE to the
5309 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5310 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5311 or ERROR_MARK_NODE if the parse fails. */
5313 static void
5314 cp_parser_pseudo_destructor_name (cp_parser* parser,
5315 tree* scope,
5316 tree* type)
5318 bool nested_name_specifier_p;
5320 /* Assume that things will not work out. */
5321 *type = error_mark_node;
5323 /* Look for the optional `::' operator. */
5324 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5325 /* Look for the optional nested-name-specifier. */
5326 nested_name_specifier_p
5327 = (cp_parser_nested_name_specifier_opt (parser,
5328 /*typename_keyword_p=*/false,
5329 /*check_dependency_p=*/true,
5330 /*type_p=*/false,
5331 /*is_declaration=*/false)
5332 != NULL_TREE);
5333 /* Now, if we saw a nested-name-specifier, we might be doing the
5334 second production. */
5335 if (nested_name_specifier_p
5336 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5338 /* Consume the `template' keyword. */
5339 cp_lexer_consume_token (parser->lexer);
5340 /* Parse the template-id. */
5341 cp_parser_template_id (parser,
5342 /*template_keyword_p=*/true,
5343 /*check_dependency_p=*/false,
5344 /*is_declaration=*/true);
5345 /* Look for the `::' token. */
5346 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5348 /* If the next token is not a `~', then there might be some
5349 additional qualification. */
5350 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5352 /* At this point, we're looking for "type-name :: ~". The type-name
5353 must not be a class-name, since this is a pseudo-destructor. So,
5354 it must be either an enum-name, or a typedef-name -- both of which
5355 are just identifiers. So, we peek ahead to check that the "::"
5356 and "~" tokens are present; if they are not, then we can avoid
5357 calling type_name. */
5358 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5359 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5360 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5362 cp_parser_error (parser, "non-scalar type");
5363 return;
5366 /* Look for the type-name. */
5367 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5368 if (*scope == error_mark_node)
5369 return;
5371 /* Look for the `::' token. */
5372 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5374 else
5375 *scope = NULL_TREE;
5377 /* Look for the `~'. */
5378 cp_parser_require (parser, CPP_COMPL, "%<~%>");
5379 /* Look for the type-name again. We are not responsible for
5380 checking that it matches the first type-name. */
5381 *type = cp_parser_nonclass_name (parser);
5384 /* Parse a unary-expression.
5386 unary-expression:
5387 postfix-expression
5388 ++ cast-expression
5389 -- cast-expression
5390 unary-operator cast-expression
5391 sizeof unary-expression
5392 sizeof ( type-id )
5393 new-expression
5394 delete-expression
5396 GNU Extensions:
5398 unary-expression:
5399 __extension__ cast-expression
5400 __alignof__ unary-expression
5401 __alignof__ ( type-id )
5402 __real__ cast-expression
5403 __imag__ cast-expression
5404 && identifier
5406 ADDRESS_P is true iff the unary-expression is appearing as the
5407 operand of the `&' operator. CAST_P is true if this expression is
5408 the target of a cast.
5410 Returns a representation of the expression. */
5412 static tree
5413 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5414 cp_id_kind * pidk)
5416 cp_token *token;
5417 enum tree_code unary_operator;
5419 /* Peek at the next token. */
5420 token = cp_lexer_peek_token (parser->lexer);
5421 /* Some keywords give away the kind of expression. */
5422 if (token->type == CPP_KEYWORD)
5424 enum rid keyword = token->keyword;
5426 switch (keyword)
5428 case RID_ALIGNOF:
5429 case RID_SIZEOF:
5431 tree operand;
5432 enum tree_code op;
5434 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5435 /* Consume the token. */
5436 cp_lexer_consume_token (parser->lexer);
5437 /* Parse the operand. */
5438 operand = cp_parser_sizeof_operand (parser, keyword);
5440 if (TYPE_P (operand))
5441 return cxx_sizeof_or_alignof_type (operand, op, true);
5442 else
5443 return cxx_sizeof_or_alignof_expr (operand, op, true);
5446 case RID_NEW:
5447 return cp_parser_new_expression (parser);
5449 case RID_DELETE:
5450 return cp_parser_delete_expression (parser);
5452 case RID_EXTENSION:
5454 /* The saved value of the PEDANTIC flag. */
5455 int saved_pedantic;
5456 tree expr;
5458 /* Save away the PEDANTIC flag. */
5459 cp_parser_extension_opt (parser, &saved_pedantic);
5460 /* Parse the cast-expression. */
5461 expr = cp_parser_simple_cast_expression (parser);
5462 /* Restore the PEDANTIC flag. */
5463 pedantic = saved_pedantic;
5465 return expr;
5468 case RID_REALPART:
5469 case RID_IMAGPART:
5471 tree expression;
5473 /* Consume the `__real__' or `__imag__' token. */
5474 cp_lexer_consume_token (parser->lexer);
5475 /* Parse the cast-expression. */
5476 expression = cp_parser_simple_cast_expression (parser);
5477 /* Create the complete representation. */
5478 return build_x_unary_op ((keyword == RID_REALPART
5479 ? REALPART_EXPR : IMAGPART_EXPR),
5480 expression,
5481 tf_warning_or_error);
5483 break;
5485 default:
5486 break;
5490 /* Look for the `:: new' and `:: delete', which also signal the
5491 beginning of a new-expression, or delete-expression,
5492 respectively. If the next token is `::', then it might be one of
5493 these. */
5494 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5496 enum rid keyword;
5498 /* See if the token after the `::' is one of the keywords in
5499 which we're interested. */
5500 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5501 /* If it's `new', we have a new-expression. */
5502 if (keyword == RID_NEW)
5503 return cp_parser_new_expression (parser);
5504 /* Similarly, for `delete'. */
5505 else if (keyword == RID_DELETE)
5506 return cp_parser_delete_expression (parser);
5509 /* Look for a unary operator. */
5510 unary_operator = cp_parser_unary_operator (token);
5511 /* The `++' and `--' operators can be handled similarly, even though
5512 they are not technically unary-operators in the grammar. */
5513 if (unary_operator == ERROR_MARK)
5515 if (token->type == CPP_PLUS_PLUS)
5516 unary_operator = PREINCREMENT_EXPR;
5517 else if (token->type == CPP_MINUS_MINUS)
5518 unary_operator = PREDECREMENT_EXPR;
5519 /* Handle the GNU address-of-label extension. */
5520 else if (cp_parser_allow_gnu_extensions_p (parser)
5521 && token->type == CPP_AND_AND)
5523 tree identifier;
5524 tree expression;
5525 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5527 /* Consume the '&&' token. */
5528 cp_lexer_consume_token (parser->lexer);
5529 /* Look for the identifier. */
5530 identifier = cp_parser_identifier (parser);
5531 /* Create an expression representing the address. */
5532 expression = finish_label_address_expr (identifier, loc);
5533 if (cp_parser_non_integral_constant_expression (parser,
5534 "the address of a label"))
5535 expression = error_mark_node;
5536 return expression;
5539 if (unary_operator != ERROR_MARK)
5541 tree cast_expression;
5542 tree expression = error_mark_node;
5543 const char *non_constant_p = NULL;
5545 /* Consume the operator token. */
5546 token = cp_lexer_consume_token (parser->lexer);
5547 /* Parse the cast-expression. */
5548 cast_expression
5549 = cp_parser_cast_expression (parser,
5550 unary_operator == ADDR_EXPR,
5551 /*cast_p=*/false, pidk);
5552 /* Now, build an appropriate representation. */
5553 switch (unary_operator)
5555 case INDIRECT_REF:
5556 non_constant_p = "%<*%>";
5557 expression = build_x_indirect_ref (cast_expression, "unary *",
5558 tf_warning_or_error);
5559 break;
5561 case ADDR_EXPR:
5562 non_constant_p = "%<&%>";
5563 /* Fall through. */
5564 case BIT_NOT_EXPR:
5565 expression = build_x_unary_op (unary_operator, cast_expression,
5566 tf_warning_or_error);
5567 break;
5569 case PREINCREMENT_EXPR:
5570 case PREDECREMENT_EXPR:
5571 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5572 ? "%<++%>" : "%<--%>");
5573 /* Fall through. */
5574 case UNARY_PLUS_EXPR:
5575 case NEGATE_EXPR:
5576 case TRUTH_NOT_EXPR:
5577 expression = finish_unary_op_expr (unary_operator, cast_expression);
5578 break;
5580 default:
5581 gcc_unreachable ();
5584 if (non_constant_p
5585 && cp_parser_non_integral_constant_expression (parser,
5586 non_constant_p))
5587 expression = error_mark_node;
5589 return expression;
5592 return cp_parser_postfix_expression (parser, address_p, cast_p,
5593 /*member_access_only_p=*/false,
5594 pidk);
5597 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5598 unary-operator, the corresponding tree code is returned. */
5600 static enum tree_code
5601 cp_parser_unary_operator (cp_token* token)
5603 switch (token->type)
5605 case CPP_MULT:
5606 return INDIRECT_REF;
5608 case CPP_AND:
5609 return ADDR_EXPR;
5611 case CPP_PLUS:
5612 return UNARY_PLUS_EXPR;
5614 case CPP_MINUS:
5615 return NEGATE_EXPR;
5617 case CPP_NOT:
5618 return TRUTH_NOT_EXPR;
5620 case CPP_COMPL:
5621 return BIT_NOT_EXPR;
5623 default:
5624 return ERROR_MARK;
5628 /* Parse a new-expression.
5630 new-expression:
5631 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5632 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5634 Returns a representation of the expression. */
5636 static tree
5637 cp_parser_new_expression (cp_parser* parser)
5639 bool global_scope_p;
5640 VEC(tree,gc) *placement;
5641 tree type;
5642 VEC(tree,gc) *initializer;
5643 tree nelts;
5644 tree ret;
5646 /* Look for the optional `::' operator. */
5647 global_scope_p
5648 = (cp_parser_global_scope_opt (parser,
5649 /*current_scope_valid_p=*/false)
5650 != NULL_TREE);
5651 /* Look for the `new' operator. */
5652 cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5653 /* There's no easy way to tell a new-placement from the
5654 `( type-id )' construct. */
5655 cp_parser_parse_tentatively (parser);
5656 /* Look for a new-placement. */
5657 placement = cp_parser_new_placement (parser);
5658 /* If that didn't work out, there's no new-placement. */
5659 if (!cp_parser_parse_definitely (parser))
5661 if (placement != NULL)
5662 release_tree_vector (placement);
5663 placement = NULL;
5666 /* If the next token is a `(', then we have a parenthesized
5667 type-id. */
5668 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5670 cp_token *token;
5671 /* Consume the `('. */
5672 cp_lexer_consume_token (parser->lexer);
5673 /* Parse the type-id. */
5674 type = cp_parser_type_id (parser);
5675 /* Look for the closing `)'. */
5676 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5677 token = cp_lexer_peek_token (parser->lexer);
5678 /* There should not be a direct-new-declarator in this production,
5679 but GCC used to allowed this, so we check and emit a sensible error
5680 message for this case. */
5681 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5683 error_at (token->location,
5684 "array bound forbidden after parenthesized type-id");
5685 inform (token->location,
5686 "try removing the parentheses around the type-id");
5687 cp_parser_direct_new_declarator (parser);
5689 nelts = NULL_TREE;
5691 /* Otherwise, there must be a new-type-id. */
5692 else
5693 type = cp_parser_new_type_id (parser, &nelts);
5695 /* If the next token is a `(' or '{', then we have a new-initializer. */
5696 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5697 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5698 initializer = cp_parser_new_initializer (parser);
5699 else
5700 initializer = NULL;
5702 /* A new-expression may not appear in an integral constant
5703 expression. */
5704 if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5705 ret = error_mark_node;
5706 else
5708 /* Create a representation of the new-expression. */
5709 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
5710 tf_warning_or_error);
5713 if (placement != NULL)
5714 release_tree_vector (placement);
5715 if (initializer != NULL)
5716 release_tree_vector (initializer);
5718 return ret;
5721 /* Parse a new-placement.
5723 new-placement:
5724 ( expression-list )
5726 Returns the same representation as for an expression-list. */
5728 static VEC(tree,gc) *
5729 cp_parser_new_placement (cp_parser* parser)
5731 VEC(tree,gc) *expression_list;
5733 /* Parse the expression-list. */
5734 expression_list = (cp_parser_parenthesized_expression_list
5735 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5736 /*non_constant_p=*/NULL));
5738 return expression_list;
5741 /* Parse a new-type-id.
5743 new-type-id:
5744 type-specifier-seq new-declarator [opt]
5746 Returns the TYPE allocated. If the new-type-id indicates an array
5747 type, *NELTS is set to the number of elements in the last array
5748 bound; the TYPE will not include the last array bound. */
5750 static tree
5751 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5753 cp_decl_specifier_seq type_specifier_seq;
5754 cp_declarator *new_declarator;
5755 cp_declarator *declarator;
5756 cp_declarator *outer_declarator;
5757 const char *saved_message;
5758 tree type;
5760 /* The type-specifier sequence must not contain type definitions.
5761 (It cannot contain declarations of new types either, but if they
5762 are not definitions we will catch that because they are not
5763 complete.) */
5764 saved_message = parser->type_definition_forbidden_message;
5765 parser->type_definition_forbidden_message
5766 = "types may not be defined in a new-type-id";
5767 /* Parse the type-specifier-seq. */
5768 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5769 &type_specifier_seq);
5770 /* Restore the old message. */
5771 parser->type_definition_forbidden_message = saved_message;
5772 /* Parse the new-declarator. */
5773 new_declarator = cp_parser_new_declarator_opt (parser);
5775 /* Determine the number of elements in the last array dimension, if
5776 any. */
5777 *nelts = NULL_TREE;
5778 /* Skip down to the last array dimension. */
5779 declarator = new_declarator;
5780 outer_declarator = NULL;
5781 while (declarator && (declarator->kind == cdk_pointer
5782 || declarator->kind == cdk_ptrmem))
5784 outer_declarator = declarator;
5785 declarator = declarator->declarator;
5787 while (declarator
5788 && declarator->kind == cdk_array
5789 && declarator->declarator
5790 && declarator->declarator->kind == cdk_array)
5792 outer_declarator = declarator;
5793 declarator = declarator->declarator;
5796 if (declarator && declarator->kind == cdk_array)
5798 *nelts = declarator->u.array.bounds;
5799 if (*nelts == error_mark_node)
5800 *nelts = integer_one_node;
5802 if (outer_declarator)
5803 outer_declarator->declarator = declarator->declarator;
5804 else
5805 new_declarator = NULL;
5808 type = groktypename (&type_specifier_seq, new_declarator, false);
5809 return type;
5812 /* Parse an (optional) new-declarator.
5814 new-declarator:
5815 ptr-operator new-declarator [opt]
5816 direct-new-declarator
5818 Returns the declarator. */
5820 static cp_declarator *
5821 cp_parser_new_declarator_opt (cp_parser* parser)
5823 enum tree_code code;
5824 tree type;
5825 cp_cv_quals cv_quals;
5827 /* We don't know if there's a ptr-operator next, or not. */
5828 cp_parser_parse_tentatively (parser);
5829 /* Look for a ptr-operator. */
5830 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5831 /* If that worked, look for more new-declarators. */
5832 if (cp_parser_parse_definitely (parser))
5834 cp_declarator *declarator;
5836 /* Parse another optional declarator. */
5837 declarator = cp_parser_new_declarator_opt (parser);
5839 return cp_parser_make_indirect_declarator
5840 (code, type, cv_quals, declarator);
5843 /* If the next token is a `[', there is a direct-new-declarator. */
5844 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5845 return cp_parser_direct_new_declarator (parser);
5847 return NULL;
5850 /* Parse a direct-new-declarator.
5852 direct-new-declarator:
5853 [ expression ]
5854 direct-new-declarator [constant-expression]
5858 static cp_declarator *
5859 cp_parser_direct_new_declarator (cp_parser* parser)
5861 cp_declarator *declarator = NULL;
5863 while (true)
5865 tree expression;
5867 /* Look for the opening `['. */
5868 cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5869 /* The first expression is not required to be constant. */
5870 if (!declarator)
5872 cp_token *token = cp_lexer_peek_token (parser->lexer);
5873 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5874 /* The standard requires that the expression have integral
5875 type. DR 74 adds enumeration types. We believe that the
5876 real intent is that these expressions be handled like the
5877 expression in a `switch' condition, which also allows
5878 classes with a single conversion to integral or
5879 enumeration type. */
5880 if (!processing_template_decl)
5882 expression
5883 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5884 expression,
5885 /*complain=*/true);
5886 if (!expression)
5888 error_at (token->location,
5889 "expression in new-declarator must have integral "
5890 "or enumeration type");
5891 expression = error_mark_node;
5895 /* But all the other expressions must be. */
5896 else
5897 expression
5898 = cp_parser_constant_expression (parser,
5899 /*allow_non_constant=*/false,
5900 NULL);
5901 /* Look for the closing `]'. */
5902 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5904 /* Add this bound to the declarator. */
5905 declarator = make_array_declarator (declarator, expression);
5907 /* If the next token is not a `[', then there are no more
5908 bounds. */
5909 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5910 break;
5913 return declarator;
5916 /* Parse a new-initializer.
5918 new-initializer:
5919 ( expression-list [opt] )
5920 braced-init-list
5922 Returns a representation of the expression-list. */
5924 static VEC(tree,gc) *
5925 cp_parser_new_initializer (cp_parser* parser)
5927 VEC(tree,gc) *expression_list;
5929 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5931 tree t;
5932 bool expr_non_constant_p;
5933 maybe_warn_cpp0x ("extended initializer lists");
5934 t = cp_parser_braced_list (parser, &expr_non_constant_p);
5935 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
5936 expression_list = make_tree_vector_single (t);
5938 else
5939 expression_list = (cp_parser_parenthesized_expression_list
5940 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5941 /*non_constant_p=*/NULL));
5943 return expression_list;
5946 /* Parse a delete-expression.
5948 delete-expression:
5949 :: [opt] delete cast-expression
5950 :: [opt] delete [ ] cast-expression
5952 Returns a representation of the expression. */
5954 static tree
5955 cp_parser_delete_expression (cp_parser* parser)
5957 bool global_scope_p;
5958 bool array_p;
5959 tree expression;
5961 /* Look for the optional `::' operator. */
5962 global_scope_p
5963 = (cp_parser_global_scope_opt (parser,
5964 /*current_scope_valid_p=*/false)
5965 != NULL_TREE);
5966 /* Look for the `delete' keyword. */
5967 cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
5968 /* See if the array syntax is in use. */
5969 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5971 /* Consume the `[' token. */
5972 cp_lexer_consume_token (parser->lexer);
5973 /* Look for the `]' token. */
5974 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5975 /* Remember that this is the `[]' construct. */
5976 array_p = true;
5978 else
5979 array_p = false;
5981 /* Parse the cast-expression. */
5982 expression = cp_parser_simple_cast_expression (parser);
5984 /* A delete-expression may not appear in an integral constant
5985 expression. */
5986 if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
5987 return error_mark_node;
5989 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5992 /* Returns true if TOKEN may start a cast-expression and false
5993 otherwise. */
5995 static bool
5996 cp_parser_token_starts_cast_expression (cp_token *token)
5998 switch (token->type)
6000 case CPP_COMMA:
6001 case CPP_SEMICOLON:
6002 case CPP_QUERY:
6003 case CPP_COLON:
6004 case CPP_CLOSE_SQUARE:
6005 case CPP_CLOSE_PAREN:
6006 case CPP_CLOSE_BRACE:
6007 case CPP_DOT:
6008 case CPP_DOT_STAR:
6009 case CPP_DEREF:
6010 case CPP_DEREF_STAR:
6011 case CPP_DIV:
6012 case CPP_MOD:
6013 case CPP_LSHIFT:
6014 case CPP_RSHIFT:
6015 case CPP_LESS:
6016 case CPP_GREATER:
6017 case CPP_LESS_EQ:
6018 case CPP_GREATER_EQ:
6019 case CPP_EQ_EQ:
6020 case CPP_NOT_EQ:
6021 case CPP_EQ:
6022 case CPP_MULT_EQ:
6023 case CPP_DIV_EQ:
6024 case CPP_MOD_EQ:
6025 case CPP_PLUS_EQ:
6026 case CPP_MINUS_EQ:
6027 case CPP_RSHIFT_EQ:
6028 case CPP_LSHIFT_EQ:
6029 case CPP_AND_EQ:
6030 case CPP_XOR_EQ:
6031 case CPP_OR_EQ:
6032 case CPP_XOR:
6033 case CPP_OR:
6034 case CPP_OR_OR:
6035 case CPP_EOF:
6036 return false;
6038 /* '[' may start a primary-expression in obj-c++. */
6039 case CPP_OPEN_SQUARE:
6040 return c_dialect_objc ();
6042 default:
6043 return true;
6047 /* Parse a cast-expression.
6049 cast-expression:
6050 unary-expression
6051 ( type-id ) cast-expression
6053 ADDRESS_P is true iff the unary-expression is appearing as the
6054 operand of the `&' operator. CAST_P is true if this expression is
6055 the target of a cast.
6057 Returns a representation of the expression. */
6059 static tree
6060 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6061 cp_id_kind * pidk)
6063 /* If it's a `(', then we might be looking at a cast. */
6064 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6066 tree type = NULL_TREE;
6067 tree expr = NULL_TREE;
6068 bool compound_literal_p;
6069 const char *saved_message;
6071 /* There's no way to know yet whether or not this is a cast.
6072 For example, `(int (3))' is a unary-expression, while `(int)
6073 3' is a cast. So, we resort to parsing tentatively. */
6074 cp_parser_parse_tentatively (parser);
6075 /* Types may not be defined in a cast. */
6076 saved_message = parser->type_definition_forbidden_message;
6077 parser->type_definition_forbidden_message
6078 = "types may not be defined in casts";
6079 /* Consume the `('. */
6080 cp_lexer_consume_token (parser->lexer);
6081 /* A very tricky bit is that `(struct S) { 3 }' is a
6082 compound-literal (which we permit in C++ as an extension).
6083 But, that construct is not a cast-expression -- it is a
6084 postfix-expression. (The reason is that `(struct S) { 3 }.i'
6085 is legal; if the compound-literal were a cast-expression,
6086 you'd need an extra set of parentheses.) But, if we parse
6087 the type-id, and it happens to be a class-specifier, then we
6088 will commit to the parse at that point, because we cannot
6089 undo the action that is done when creating a new class. So,
6090 then we cannot back up and do a postfix-expression.
6092 Therefore, we scan ahead to the closing `)', and check to see
6093 if the token after the `)' is a `{'. If so, we are not
6094 looking at a cast-expression.
6096 Save tokens so that we can put them back. */
6097 cp_lexer_save_tokens (parser->lexer);
6098 /* Skip tokens until the next token is a closing parenthesis.
6099 If we find the closing `)', and the next token is a `{', then
6100 we are looking at a compound-literal. */
6101 compound_literal_p
6102 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6103 /*consume_paren=*/true)
6104 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6105 /* Roll back the tokens we skipped. */
6106 cp_lexer_rollback_tokens (parser->lexer);
6107 /* If we were looking at a compound-literal, simulate an error
6108 so that the call to cp_parser_parse_definitely below will
6109 fail. */
6110 if (compound_literal_p)
6111 cp_parser_simulate_error (parser);
6112 else
6114 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6115 parser->in_type_id_in_expr_p = true;
6116 /* Look for the type-id. */
6117 type = cp_parser_type_id (parser);
6118 /* Look for the closing `)'. */
6119 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6120 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6123 /* Restore the saved message. */
6124 parser->type_definition_forbidden_message = saved_message;
6126 /* At this point this can only be either a cast or a
6127 parenthesized ctor such as `(T ())' that looks like a cast to
6128 function returning T. */
6129 if (!cp_parser_error_occurred (parser)
6130 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6131 (parser->lexer)))
6133 cp_parser_parse_definitely (parser);
6134 expr = cp_parser_cast_expression (parser,
6135 /*address_p=*/false,
6136 /*cast_p=*/true, pidk);
6138 /* Warn about old-style casts, if so requested. */
6139 if (warn_old_style_cast
6140 && !in_system_header
6141 && !VOID_TYPE_P (type)
6142 && current_lang_name != lang_name_c)
6143 warning (OPT_Wold_style_cast, "use of old-style cast");
6145 /* Only type conversions to integral or enumeration types
6146 can be used in constant-expressions. */
6147 if (!cast_valid_in_integral_constant_expression_p (type)
6148 && (cp_parser_non_integral_constant_expression
6149 (parser,
6150 "a cast to a type other than an integral or "
6151 "enumeration type")))
6152 return error_mark_node;
6154 /* Perform the cast. */
6155 expr = build_c_cast (input_location, type, expr);
6156 return expr;
6158 else
6159 cp_parser_abort_tentative_parse (parser);
6162 /* If we get here, then it's not a cast, so it must be a
6163 unary-expression. */
6164 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6167 /* Parse a binary expression of the general form:
6169 pm-expression:
6170 cast-expression
6171 pm-expression .* cast-expression
6172 pm-expression ->* cast-expression
6174 multiplicative-expression:
6175 pm-expression
6176 multiplicative-expression * pm-expression
6177 multiplicative-expression / pm-expression
6178 multiplicative-expression % pm-expression
6180 additive-expression:
6181 multiplicative-expression
6182 additive-expression + multiplicative-expression
6183 additive-expression - multiplicative-expression
6185 shift-expression:
6186 additive-expression
6187 shift-expression << additive-expression
6188 shift-expression >> additive-expression
6190 relational-expression:
6191 shift-expression
6192 relational-expression < shift-expression
6193 relational-expression > shift-expression
6194 relational-expression <= shift-expression
6195 relational-expression >= shift-expression
6197 GNU Extension:
6199 relational-expression:
6200 relational-expression <? shift-expression
6201 relational-expression >? shift-expression
6203 equality-expression:
6204 relational-expression
6205 equality-expression == relational-expression
6206 equality-expression != relational-expression
6208 and-expression:
6209 equality-expression
6210 and-expression & equality-expression
6212 exclusive-or-expression:
6213 and-expression
6214 exclusive-or-expression ^ and-expression
6216 inclusive-or-expression:
6217 exclusive-or-expression
6218 inclusive-or-expression | exclusive-or-expression
6220 logical-and-expression:
6221 inclusive-or-expression
6222 logical-and-expression && inclusive-or-expression
6224 logical-or-expression:
6225 logical-and-expression
6226 logical-or-expression || logical-and-expression
6228 All these are implemented with a single function like:
6230 binary-expression:
6231 simple-cast-expression
6232 binary-expression <token> binary-expression
6234 CAST_P is true if this expression is the target of a cast.
6236 The binops_by_token map is used to get the tree codes for each <token> type.
6237 binary-expressions are associated according to a precedence table. */
6239 #define TOKEN_PRECEDENCE(token) \
6240 (((token->type == CPP_GREATER \
6241 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6242 && !parser->greater_than_is_operator_p) \
6243 ? PREC_NOT_OPERATOR \
6244 : binops_by_token[token->type].prec)
6246 static tree
6247 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6248 bool no_toplevel_fold_p,
6249 enum cp_parser_prec prec,
6250 cp_id_kind * pidk)
6252 cp_parser_expression_stack stack;
6253 cp_parser_expression_stack_entry *sp = &stack[0];
6254 tree lhs, rhs;
6255 cp_token *token;
6256 enum tree_code tree_type, lhs_type, rhs_type;
6257 enum cp_parser_prec new_prec, lookahead_prec;
6258 bool overloaded_p;
6260 /* Parse the first expression. */
6261 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6262 lhs_type = ERROR_MARK;
6264 for (;;)
6266 /* Get an operator token. */
6267 token = cp_lexer_peek_token (parser->lexer);
6269 if (warn_cxx0x_compat
6270 && token->type == CPP_RSHIFT
6271 && !parser->greater_than_is_operator_p)
6273 if (warning_at (token->location, OPT_Wc__0x_compat,
6274 "%<>>%> operator will be treated as"
6275 " two right angle brackets in C++0x"))
6276 inform (token->location,
6277 "suggest parentheses around %<>>%> expression");
6280 new_prec = TOKEN_PRECEDENCE (token);
6282 /* Popping an entry off the stack means we completed a subexpression:
6283 - either we found a token which is not an operator (`>' where it is not
6284 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6285 will happen repeatedly;
6286 - or, we found an operator which has lower priority. This is the case
6287 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6288 parsing `3 * 4'. */
6289 if (new_prec <= prec)
6291 if (sp == stack)
6292 break;
6293 else
6294 goto pop;
6297 get_rhs:
6298 tree_type = binops_by_token[token->type].tree_type;
6300 /* We used the operator token. */
6301 cp_lexer_consume_token (parser->lexer);
6303 /* For "false && x" or "true || x", x will never be executed;
6304 disable warnings while evaluating it. */
6305 if (tree_type == TRUTH_ANDIF_EXPR)
6306 c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6307 else if (tree_type == TRUTH_ORIF_EXPR)
6308 c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6310 /* Extract another operand. It may be the RHS of this expression
6311 or the LHS of a new, higher priority expression. */
6312 rhs = cp_parser_simple_cast_expression (parser);
6313 rhs_type = ERROR_MARK;
6315 /* Get another operator token. Look up its precedence to avoid
6316 building a useless (immediately popped) stack entry for common
6317 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6318 token = cp_lexer_peek_token (parser->lexer);
6319 lookahead_prec = TOKEN_PRECEDENCE (token);
6320 if (lookahead_prec > new_prec)
6322 /* ... and prepare to parse the RHS of the new, higher priority
6323 expression. Since precedence levels on the stack are
6324 monotonically increasing, we do not have to care about
6325 stack overflows. */
6326 sp->prec = prec;
6327 sp->tree_type = tree_type;
6328 sp->lhs = lhs;
6329 sp->lhs_type = lhs_type;
6330 sp++;
6331 lhs = rhs;
6332 lhs_type = rhs_type;
6333 prec = new_prec;
6334 new_prec = lookahead_prec;
6335 goto get_rhs;
6337 pop:
6338 lookahead_prec = new_prec;
6339 /* If the stack is not empty, we have parsed into LHS the right side
6340 (`4' in the example above) of an expression we had suspended.
6341 We can use the information on the stack to recover the LHS (`3')
6342 from the stack together with the tree code (`MULT_EXPR'), and
6343 the precedence of the higher level subexpression
6344 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6345 which will be used to actually build the additive expression. */
6346 --sp;
6347 prec = sp->prec;
6348 tree_type = sp->tree_type;
6349 rhs = lhs;
6350 rhs_type = lhs_type;
6351 lhs = sp->lhs;
6352 lhs_type = sp->lhs_type;
6355 /* Undo the disabling of warnings done above. */
6356 if (tree_type == TRUTH_ANDIF_EXPR)
6357 c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6358 else if (tree_type == TRUTH_ORIF_EXPR)
6359 c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6361 overloaded_p = false;
6362 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6363 ERROR_MARK for everything that is not a binary expression.
6364 This makes warn_about_parentheses miss some warnings that
6365 involve unary operators. For unary expressions we should
6366 pass the correct tree_code unless the unary expression was
6367 surrounded by parentheses.
6369 if (no_toplevel_fold_p
6370 && lookahead_prec <= prec
6371 && sp == stack
6372 && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6373 lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6374 else
6375 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6376 &overloaded_p, tf_warning_or_error);
6377 lhs_type = tree_type;
6379 /* If the binary operator required the use of an overloaded operator,
6380 then this expression cannot be an integral constant-expression.
6381 An overloaded operator can be used even if both operands are
6382 otherwise permissible in an integral constant-expression if at
6383 least one of the operands is of enumeration type. */
6385 if (overloaded_p
6386 && (cp_parser_non_integral_constant_expression
6387 (parser, "calls to overloaded operators")))
6388 return error_mark_node;
6391 return lhs;
6395 /* Parse the `? expression : assignment-expression' part of a
6396 conditional-expression. The LOGICAL_OR_EXPR is the
6397 logical-or-expression that started the conditional-expression.
6398 Returns a representation of the entire conditional-expression.
6400 This routine is used by cp_parser_assignment_expression.
6402 ? expression : assignment-expression
6404 GNU Extensions:
6406 ? : assignment-expression */
6408 static tree
6409 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6411 tree expr;
6412 tree assignment_expr;
6414 /* Consume the `?' token. */
6415 cp_lexer_consume_token (parser->lexer);
6416 if (cp_parser_allow_gnu_extensions_p (parser)
6417 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6419 /* Implicit true clause. */
6420 expr = NULL_TREE;
6421 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6423 else
6425 /* Parse the expression. */
6426 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6427 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6428 c_inhibit_evaluation_warnings +=
6429 ((logical_or_expr == truthvalue_true_node)
6430 - (logical_or_expr == truthvalue_false_node));
6433 /* The next token should be a `:'. */
6434 cp_parser_require (parser, CPP_COLON, "%<:%>");
6435 /* Parse the assignment-expression. */
6436 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6437 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6439 /* Build the conditional-expression. */
6440 return build_x_conditional_expr (logical_or_expr,
6441 expr,
6442 assignment_expr,
6443 tf_warning_or_error);
6446 /* Parse an assignment-expression.
6448 assignment-expression:
6449 conditional-expression
6450 logical-or-expression assignment-operator assignment_expression
6451 throw-expression
6453 CAST_P is true if this expression is the target of a cast.
6455 Returns a representation for the expression. */
6457 static tree
6458 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6459 cp_id_kind * pidk)
6461 tree expr;
6463 /* If the next token is the `throw' keyword, then we're looking at
6464 a throw-expression. */
6465 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6466 expr = cp_parser_throw_expression (parser);
6467 /* Otherwise, it must be that we are looking at a
6468 logical-or-expression. */
6469 else
6471 /* Parse the binary expressions (logical-or-expression). */
6472 expr = cp_parser_binary_expression (parser, cast_p, false,
6473 PREC_NOT_OPERATOR, pidk);
6474 /* If the next token is a `?' then we're actually looking at a
6475 conditional-expression. */
6476 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6477 return cp_parser_question_colon_clause (parser, expr);
6478 else
6480 enum tree_code assignment_operator;
6482 /* If it's an assignment-operator, we're using the second
6483 production. */
6484 assignment_operator
6485 = cp_parser_assignment_operator_opt (parser);
6486 if (assignment_operator != ERROR_MARK)
6488 bool non_constant_p;
6490 /* Parse the right-hand side of the assignment. */
6491 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6493 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6494 maybe_warn_cpp0x ("extended initializer lists");
6496 /* An assignment may not appear in a
6497 constant-expression. */
6498 if (cp_parser_non_integral_constant_expression (parser,
6499 "an assignment"))
6500 return error_mark_node;
6501 /* Build the assignment expression. */
6502 expr = build_x_modify_expr (expr,
6503 assignment_operator,
6504 rhs,
6505 tf_warning_or_error);
6510 return expr;
6513 /* Parse an (optional) assignment-operator.
6515 assignment-operator: one of
6516 = *= /= %= += -= >>= <<= &= ^= |=
6518 GNU Extension:
6520 assignment-operator: one of
6521 <?= >?=
6523 If the next token is an assignment operator, the corresponding tree
6524 code is returned, and the token is consumed. For example, for
6525 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6526 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6527 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6528 operator, ERROR_MARK is returned. */
6530 static enum tree_code
6531 cp_parser_assignment_operator_opt (cp_parser* parser)
6533 enum tree_code op;
6534 cp_token *token;
6536 /* Peek at the next token. */
6537 token = cp_lexer_peek_token (parser->lexer);
6539 switch (token->type)
6541 case CPP_EQ:
6542 op = NOP_EXPR;
6543 break;
6545 case CPP_MULT_EQ:
6546 op = MULT_EXPR;
6547 break;
6549 case CPP_DIV_EQ:
6550 op = TRUNC_DIV_EXPR;
6551 break;
6553 case CPP_MOD_EQ:
6554 op = TRUNC_MOD_EXPR;
6555 break;
6557 case CPP_PLUS_EQ:
6558 op = PLUS_EXPR;
6559 break;
6561 case CPP_MINUS_EQ:
6562 op = MINUS_EXPR;
6563 break;
6565 case CPP_RSHIFT_EQ:
6566 op = RSHIFT_EXPR;
6567 break;
6569 case CPP_LSHIFT_EQ:
6570 op = LSHIFT_EXPR;
6571 break;
6573 case CPP_AND_EQ:
6574 op = BIT_AND_EXPR;
6575 break;
6577 case CPP_XOR_EQ:
6578 op = BIT_XOR_EXPR;
6579 break;
6581 case CPP_OR_EQ:
6582 op = BIT_IOR_EXPR;
6583 break;
6585 default:
6586 /* Nothing else is an assignment operator. */
6587 op = ERROR_MARK;
6590 /* If it was an assignment operator, consume it. */
6591 if (op != ERROR_MARK)
6592 cp_lexer_consume_token (parser->lexer);
6594 return op;
6597 /* Parse an expression.
6599 expression:
6600 assignment-expression
6601 expression , assignment-expression
6603 CAST_P is true if this expression is the target of a cast.
6605 Returns a representation of the expression. */
6607 static tree
6608 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6610 tree expression = NULL_TREE;
6612 while (true)
6614 tree assignment_expression;
6616 /* Parse the next assignment-expression. */
6617 assignment_expression
6618 = cp_parser_assignment_expression (parser, cast_p, pidk);
6619 /* If this is the first assignment-expression, we can just
6620 save it away. */
6621 if (!expression)
6622 expression = assignment_expression;
6623 else
6624 expression = build_x_compound_expr (expression,
6625 assignment_expression,
6626 tf_warning_or_error);
6627 /* If the next token is not a comma, then we are done with the
6628 expression. */
6629 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6630 break;
6631 /* Consume the `,'. */
6632 cp_lexer_consume_token (parser->lexer);
6633 /* A comma operator cannot appear in a constant-expression. */
6634 if (cp_parser_non_integral_constant_expression (parser,
6635 "a comma operator"))
6636 expression = error_mark_node;
6639 return expression;
6642 /* Parse a constant-expression.
6644 constant-expression:
6645 conditional-expression
6647 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6648 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6649 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6650 is false, NON_CONSTANT_P should be NULL. */
6652 static tree
6653 cp_parser_constant_expression (cp_parser* parser,
6654 bool allow_non_constant_p,
6655 bool *non_constant_p)
6657 bool saved_integral_constant_expression_p;
6658 bool saved_allow_non_integral_constant_expression_p;
6659 bool saved_non_integral_constant_expression_p;
6660 tree expression;
6662 /* It might seem that we could simply parse the
6663 conditional-expression, and then check to see if it were
6664 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6665 one that the compiler can figure out is constant, possibly after
6666 doing some simplifications or optimizations. The standard has a
6667 precise definition of constant-expression, and we must honor
6668 that, even though it is somewhat more restrictive.
6670 For example:
6672 int i[(2, 3)];
6674 is not a legal declaration, because `(2, 3)' is not a
6675 constant-expression. The `,' operator is forbidden in a
6676 constant-expression. However, GCC's constant-folding machinery
6677 will fold this operation to an INTEGER_CST for `3'. */
6679 /* Save the old settings. */
6680 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6681 saved_allow_non_integral_constant_expression_p
6682 = parser->allow_non_integral_constant_expression_p;
6683 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6684 /* We are now parsing a constant-expression. */
6685 parser->integral_constant_expression_p = true;
6686 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6687 parser->non_integral_constant_expression_p = false;
6688 /* Although the grammar says "conditional-expression", we parse an
6689 "assignment-expression", which also permits "throw-expression"
6690 and the use of assignment operators. In the case that
6691 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6692 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6693 actually essential that we look for an assignment-expression.
6694 For example, cp_parser_initializer_clauses uses this function to
6695 determine whether a particular assignment-expression is in fact
6696 constant. */
6697 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6698 /* Restore the old settings. */
6699 parser->integral_constant_expression_p
6700 = saved_integral_constant_expression_p;
6701 parser->allow_non_integral_constant_expression_p
6702 = saved_allow_non_integral_constant_expression_p;
6703 if (allow_non_constant_p)
6704 *non_constant_p = parser->non_integral_constant_expression_p;
6705 else if (parser->non_integral_constant_expression_p)
6706 expression = error_mark_node;
6707 parser->non_integral_constant_expression_p
6708 = saved_non_integral_constant_expression_p;
6710 return expression;
6713 /* Parse __builtin_offsetof.
6715 offsetof-expression:
6716 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6718 offsetof-member-designator:
6719 id-expression
6720 | offsetof-member-designator "." id-expression
6721 | offsetof-member-designator "[" expression "]"
6722 | offsetof-member-designator "->" id-expression */
6724 static tree
6725 cp_parser_builtin_offsetof (cp_parser *parser)
6727 int save_ice_p, save_non_ice_p;
6728 tree type, expr;
6729 cp_id_kind dummy;
6730 cp_token *token;
6732 /* We're about to accept non-integral-constant things, but will
6733 definitely yield an integral constant expression. Save and
6734 restore these values around our local parsing. */
6735 save_ice_p = parser->integral_constant_expression_p;
6736 save_non_ice_p = parser->non_integral_constant_expression_p;
6738 /* Consume the "__builtin_offsetof" token. */
6739 cp_lexer_consume_token (parser->lexer);
6740 /* Consume the opening `('. */
6741 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6742 /* Parse the type-id. */
6743 type = cp_parser_type_id (parser);
6744 /* Look for the `,'. */
6745 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6746 token = cp_lexer_peek_token (parser->lexer);
6748 /* Build the (type *)null that begins the traditional offsetof macro. */
6749 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6750 tf_warning_or_error);
6752 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6753 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6754 true, &dummy, token->location);
6755 while (true)
6757 token = cp_lexer_peek_token (parser->lexer);
6758 switch (token->type)
6760 case CPP_OPEN_SQUARE:
6761 /* offsetof-member-designator "[" expression "]" */
6762 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6763 break;
6765 case CPP_DEREF:
6766 /* offsetof-member-designator "->" identifier */
6767 expr = grok_array_decl (expr, integer_zero_node);
6768 /* FALLTHRU */
6770 case CPP_DOT:
6771 /* offsetof-member-designator "." identifier */
6772 cp_lexer_consume_token (parser->lexer);
6773 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
6774 expr, true, &dummy,
6775 token->location);
6776 break;
6778 case CPP_CLOSE_PAREN:
6779 /* Consume the ")" token. */
6780 cp_lexer_consume_token (parser->lexer);
6781 goto success;
6783 default:
6784 /* Error. We know the following require will fail, but
6785 that gives the proper error message. */
6786 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6787 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6788 expr = error_mark_node;
6789 goto failure;
6793 success:
6794 /* If we're processing a template, we can't finish the semantics yet.
6795 Otherwise we can fold the entire expression now. */
6796 if (processing_template_decl)
6797 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6798 else
6799 expr = finish_offsetof (expr);
6801 failure:
6802 parser->integral_constant_expression_p = save_ice_p;
6803 parser->non_integral_constant_expression_p = save_non_ice_p;
6805 return expr;
6808 /* Parse a trait expression. */
6810 static tree
6811 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6813 cp_trait_kind kind;
6814 tree type1, type2 = NULL_TREE;
6815 bool binary = false;
6816 cp_decl_specifier_seq decl_specs;
6818 switch (keyword)
6820 case RID_HAS_NOTHROW_ASSIGN:
6821 kind = CPTK_HAS_NOTHROW_ASSIGN;
6822 break;
6823 case RID_HAS_NOTHROW_CONSTRUCTOR:
6824 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6825 break;
6826 case RID_HAS_NOTHROW_COPY:
6827 kind = CPTK_HAS_NOTHROW_COPY;
6828 break;
6829 case RID_HAS_TRIVIAL_ASSIGN:
6830 kind = CPTK_HAS_TRIVIAL_ASSIGN;
6831 break;
6832 case RID_HAS_TRIVIAL_CONSTRUCTOR:
6833 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6834 break;
6835 case RID_HAS_TRIVIAL_COPY:
6836 kind = CPTK_HAS_TRIVIAL_COPY;
6837 break;
6838 case RID_HAS_TRIVIAL_DESTRUCTOR:
6839 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6840 break;
6841 case RID_HAS_VIRTUAL_DESTRUCTOR:
6842 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6843 break;
6844 case RID_IS_ABSTRACT:
6845 kind = CPTK_IS_ABSTRACT;
6846 break;
6847 case RID_IS_BASE_OF:
6848 kind = CPTK_IS_BASE_OF;
6849 binary = true;
6850 break;
6851 case RID_IS_CLASS:
6852 kind = CPTK_IS_CLASS;
6853 break;
6854 case RID_IS_CONVERTIBLE_TO:
6855 kind = CPTK_IS_CONVERTIBLE_TO;
6856 binary = true;
6857 break;
6858 case RID_IS_EMPTY:
6859 kind = CPTK_IS_EMPTY;
6860 break;
6861 case RID_IS_ENUM:
6862 kind = CPTK_IS_ENUM;
6863 break;
6864 case RID_IS_POD:
6865 kind = CPTK_IS_POD;
6866 break;
6867 case RID_IS_POLYMORPHIC:
6868 kind = CPTK_IS_POLYMORPHIC;
6869 break;
6870 case RID_IS_STD_LAYOUT:
6871 kind = CPTK_IS_STD_LAYOUT;
6872 break;
6873 case RID_IS_TRIVIAL:
6874 kind = CPTK_IS_TRIVIAL;
6875 break;
6876 case RID_IS_UNION:
6877 kind = CPTK_IS_UNION;
6878 break;
6879 default:
6880 gcc_unreachable ();
6883 /* Consume the token. */
6884 cp_lexer_consume_token (parser->lexer);
6886 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6888 type1 = cp_parser_type_id (parser);
6890 if (type1 == error_mark_node)
6891 return error_mark_node;
6893 /* Build a trivial decl-specifier-seq. */
6894 clear_decl_specs (&decl_specs);
6895 decl_specs.type = type1;
6897 /* Call grokdeclarator to figure out what type this is. */
6898 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6899 /*initialized=*/0, /*attrlist=*/NULL);
6901 if (binary)
6903 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6905 type2 = cp_parser_type_id (parser);
6907 if (type2 == error_mark_node)
6908 return error_mark_node;
6910 /* Build a trivial decl-specifier-seq. */
6911 clear_decl_specs (&decl_specs);
6912 decl_specs.type = type2;
6914 /* Call grokdeclarator to figure out what type this is. */
6915 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6916 /*initialized=*/0, /*attrlist=*/NULL);
6919 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6921 /* Complete the trait expression, which may mean either processing
6922 the trait expr now or saving it for template instantiation. */
6923 return finish_trait_expr (kind, type1, type2);
6926 /* Statements [gram.stmt.stmt] */
6928 /* Parse a statement.
6930 statement:
6931 labeled-statement
6932 expression-statement
6933 compound-statement
6934 selection-statement
6935 iteration-statement
6936 jump-statement
6937 declaration-statement
6938 try-block
6940 IN_COMPOUND is true when the statement is nested inside a
6941 cp_parser_compound_statement; this matters for certain pragmas.
6943 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6944 is a (possibly labeled) if statement which is not enclosed in braces
6945 and has an else clause. This is used to implement -Wparentheses. */
6947 static void
6948 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6949 bool in_compound, bool *if_p)
6951 tree statement;
6952 cp_token *token;
6953 location_t statement_location;
6955 restart:
6956 if (if_p != NULL)
6957 *if_p = false;
6958 /* There is no statement yet. */
6959 statement = NULL_TREE;
6960 /* Peek at the next token. */
6961 token = cp_lexer_peek_token (parser->lexer);
6962 /* Remember the location of the first token in the statement. */
6963 statement_location = token->location;
6964 /* If this is a keyword, then that will often determine what kind of
6965 statement we have. */
6966 if (token->type == CPP_KEYWORD)
6968 enum rid keyword = token->keyword;
6970 switch (keyword)
6972 case RID_CASE:
6973 case RID_DEFAULT:
6974 /* Looks like a labeled-statement with a case label.
6975 Parse the label, and then use tail recursion to parse
6976 the statement. */
6977 cp_parser_label_for_labeled_statement (parser);
6978 goto restart;
6980 case RID_IF:
6981 case RID_SWITCH:
6982 statement = cp_parser_selection_statement (parser, if_p);
6983 break;
6985 case RID_WHILE:
6986 case RID_DO:
6987 case RID_FOR:
6988 statement = cp_parser_iteration_statement (parser);
6989 break;
6991 case RID_BREAK:
6992 case RID_CONTINUE:
6993 case RID_RETURN:
6994 case RID_GOTO:
6995 statement = cp_parser_jump_statement (parser);
6996 break;
6998 /* Objective-C++ exception-handling constructs. */
6999 case RID_AT_TRY:
7000 case RID_AT_CATCH:
7001 case RID_AT_FINALLY:
7002 case RID_AT_SYNCHRONIZED:
7003 case RID_AT_THROW:
7004 statement = cp_parser_objc_statement (parser);
7005 break;
7007 case RID_TRY:
7008 statement = cp_parser_try_block (parser);
7009 break;
7011 case RID_NAMESPACE:
7012 /* This must be a namespace alias definition. */
7013 cp_parser_declaration_statement (parser);
7014 return;
7016 default:
7017 /* It might be a keyword like `int' that can start a
7018 declaration-statement. */
7019 break;
7022 else if (token->type == CPP_NAME)
7024 /* If the next token is a `:', then we are looking at a
7025 labeled-statement. */
7026 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7027 if (token->type == CPP_COLON)
7029 /* Looks like a labeled-statement with an ordinary label.
7030 Parse the label, and then use tail recursion to parse
7031 the statement. */
7032 cp_parser_label_for_labeled_statement (parser);
7033 goto restart;
7036 /* Anything that starts with a `{' must be a compound-statement. */
7037 else if (token->type == CPP_OPEN_BRACE)
7038 statement = cp_parser_compound_statement (parser, NULL, false);
7039 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
7040 a statement all its own. */
7041 else if (token->type == CPP_PRAGMA)
7043 /* Only certain OpenMP pragmas are attached to statements, and thus
7044 are considered statements themselves. All others are not. In
7045 the context of a compound, accept the pragma as a "statement" and
7046 return so that we can check for a close brace. Otherwise we
7047 require a real statement and must go back and read one. */
7048 if (in_compound)
7049 cp_parser_pragma (parser, pragma_compound);
7050 else if (!cp_parser_pragma (parser, pragma_stmt))
7051 goto restart;
7052 return;
7054 else if (token->type == CPP_EOF)
7056 cp_parser_error (parser, "expected statement");
7057 return;
7060 /* Everything else must be a declaration-statement or an
7061 expression-statement. Try for the declaration-statement
7062 first, unless we are looking at a `;', in which case we know that
7063 we have an expression-statement. */
7064 if (!statement)
7066 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7068 cp_parser_parse_tentatively (parser);
7069 /* Try to parse the declaration-statement. */
7070 cp_parser_declaration_statement (parser);
7071 /* If that worked, we're done. */
7072 if (cp_parser_parse_definitely (parser))
7073 return;
7075 /* Look for an expression-statement instead. */
7076 statement = cp_parser_expression_statement (parser, in_statement_expr);
7079 /* Set the line number for the statement. */
7080 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7081 SET_EXPR_LOCATION (statement, statement_location);
7084 /* Parse the label for a labeled-statement, i.e.
7086 identifier :
7087 case constant-expression :
7088 default :
7090 GNU Extension:
7091 case constant-expression ... constant-expression : statement
7093 When a label is parsed without errors, the label is added to the
7094 parse tree by the finish_* functions, so this function doesn't
7095 have to return the label. */
7097 static void
7098 cp_parser_label_for_labeled_statement (cp_parser* parser)
7100 cp_token *token;
7101 tree label = NULL_TREE;
7103 /* The next token should be an identifier. */
7104 token = cp_lexer_peek_token (parser->lexer);
7105 if (token->type != CPP_NAME
7106 && token->type != CPP_KEYWORD)
7108 cp_parser_error (parser, "expected labeled-statement");
7109 return;
7112 switch (token->keyword)
7114 case RID_CASE:
7116 tree expr, expr_hi;
7117 cp_token *ellipsis;
7119 /* Consume the `case' token. */
7120 cp_lexer_consume_token (parser->lexer);
7121 /* Parse the constant-expression. */
7122 expr = cp_parser_constant_expression (parser,
7123 /*allow_non_constant_p=*/false,
7124 NULL);
7126 ellipsis = cp_lexer_peek_token (parser->lexer);
7127 if (ellipsis->type == CPP_ELLIPSIS)
7129 /* Consume the `...' token. */
7130 cp_lexer_consume_token (parser->lexer);
7131 expr_hi =
7132 cp_parser_constant_expression (parser,
7133 /*allow_non_constant_p=*/false,
7134 NULL);
7135 /* We don't need to emit warnings here, as the common code
7136 will do this for us. */
7138 else
7139 expr_hi = NULL_TREE;
7141 if (parser->in_switch_statement_p)
7142 finish_case_label (token->location, expr, expr_hi);
7143 else
7144 error_at (token->location,
7145 "case label %qE not within a switch statement",
7146 expr);
7148 break;
7150 case RID_DEFAULT:
7151 /* Consume the `default' token. */
7152 cp_lexer_consume_token (parser->lexer);
7154 if (parser->in_switch_statement_p)
7155 finish_case_label (token->location, NULL_TREE, NULL_TREE);
7156 else
7157 error_at (token->location, "case label not within a switch statement");
7158 break;
7160 default:
7161 /* Anything else must be an ordinary label. */
7162 label = finish_label_stmt (cp_parser_identifier (parser));
7163 break;
7166 /* Require the `:' token. */
7167 cp_parser_require (parser, CPP_COLON, "%<:%>");
7169 /* An ordinary label may optionally be followed by attributes.
7170 However, this is only permitted if the attributes are then
7171 followed by a semicolon. This is because, for backward
7172 compatibility, when parsing
7173 lab: __attribute__ ((unused)) int i;
7174 we want the attribute to attach to "i", not "lab". */
7175 if (label != NULL_TREE
7176 && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
7178 tree attrs;
7180 cp_parser_parse_tentatively (parser);
7181 attrs = cp_parser_attributes_opt (parser);
7182 if (attrs == NULL_TREE
7183 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7184 cp_parser_abort_tentative_parse (parser);
7185 else if (!cp_parser_parse_definitely (parser))
7187 else
7188 cplus_decl_attributes (&label, attrs, 0);
7192 /* Parse an expression-statement.
7194 expression-statement:
7195 expression [opt] ;
7197 Returns the new EXPR_STMT -- or NULL_TREE if the expression
7198 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
7199 indicates whether this expression-statement is part of an
7200 expression statement. */
7202 static tree
7203 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
7205 tree statement = NULL_TREE;
7207 /* If the next token is a ';', then there is no expression
7208 statement. */
7209 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7210 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7212 /* Consume the final `;'. */
7213 cp_parser_consume_semicolon_at_end_of_statement (parser);
7215 if (in_statement_expr
7216 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
7217 /* This is the final expression statement of a statement
7218 expression. */
7219 statement = finish_stmt_expr_expr (statement, in_statement_expr);
7220 else if (statement)
7221 statement = finish_expr_stmt (statement);
7222 else
7223 finish_stmt ();
7225 return statement;
7228 /* Parse a compound-statement.
7230 compound-statement:
7231 { statement-seq [opt] }
7233 GNU extension:
7235 compound-statement:
7236 { label-declaration-seq [opt] statement-seq [opt] }
7238 label-declaration-seq:
7239 label-declaration
7240 label-declaration-seq label-declaration
7242 Returns a tree representing the statement. */
7244 static tree
7245 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7246 bool in_try)
7248 tree compound_stmt;
7250 /* Consume the `{'. */
7251 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7252 return error_mark_node;
7253 /* Begin the compound-statement. */
7254 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7255 /* If the next keyword is `__label__' we have a label declaration. */
7256 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7257 cp_parser_label_declaration (parser);
7258 /* Parse an (optional) statement-seq. */
7259 cp_parser_statement_seq_opt (parser, in_statement_expr);
7260 /* Finish the compound-statement. */
7261 finish_compound_stmt (compound_stmt);
7262 /* Consume the `}'. */
7263 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7265 return compound_stmt;
7268 /* Parse an (optional) statement-seq.
7270 statement-seq:
7271 statement
7272 statement-seq [opt] statement */
7274 static void
7275 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7277 /* Scan statements until there aren't any more. */
7278 while (true)
7280 cp_token *token = cp_lexer_peek_token (parser->lexer);
7282 /* If we're looking at a `}', then we've run out of statements. */
7283 if (token->type == CPP_CLOSE_BRACE
7284 || token->type == CPP_EOF
7285 || token->type == CPP_PRAGMA_EOL)
7286 break;
7288 /* If we are in a compound statement and find 'else' then
7289 something went wrong. */
7290 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7292 if (parser->in_statement & IN_IF_STMT)
7293 break;
7294 else
7296 token = cp_lexer_consume_token (parser->lexer);
7297 error_at (token->location, "%<else%> without a previous %<if%>");
7301 /* Parse the statement. */
7302 cp_parser_statement (parser, in_statement_expr, true, NULL);
7306 /* Parse a selection-statement.
7308 selection-statement:
7309 if ( condition ) statement
7310 if ( condition ) statement else statement
7311 switch ( condition ) statement
7313 Returns the new IF_STMT or SWITCH_STMT.
7315 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7316 is a (possibly labeled) if statement which is not enclosed in
7317 braces and has an else clause. This is used to implement
7318 -Wparentheses. */
7320 static tree
7321 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7323 cp_token *token;
7324 enum rid keyword;
7326 if (if_p != NULL)
7327 *if_p = false;
7329 /* Peek at the next token. */
7330 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7332 /* See what kind of keyword it is. */
7333 keyword = token->keyword;
7334 switch (keyword)
7336 case RID_IF:
7337 case RID_SWITCH:
7339 tree statement;
7340 tree condition;
7342 /* Look for the `('. */
7343 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7345 cp_parser_skip_to_end_of_statement (parser);
7346 return error_mark_node;
7349 /* Begin the selection-statement. */
7350 if (keyword == RID_IF)
7351 statement = begin_if_stmt ();
7352 else
7353 statement = begin_switch_stmt ();
7355 /* Parse the condition. */
7356 condition = cp_parser_condition (parser);
7357 /* Look for the `)'. */
7358 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7359 cp_parser_skip_to_closing_parenthesis (parser, true, false,
7360 /*consume_paren=*/true);
7362 if (keyword == RID_IF)
7364 bool nested_if;
7365 unsigned char in_statement;
7367 /* Add the condition. */
7368 finish_if_stmt_cond (condition, statement);
7370 /* Parse the then-clause. */
7371 in_statement = parser->in_statement;
7372 parser->in_statement |= IN_IF_STMT;
7373 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7375 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7376 add_stmt (build_empty_stmt (loc));
7377 cp_lexer_consume_token (parser->lexer);
7378 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
7379 warning_at (loc, OPT_Wempty_body, "suggest braces around "
7380 "empty body in an %<if%> statement");
7381 nested_if = false;
7383 else
7384 cp_parser_implicitly_scoped_statement (parser, &nested_if);
7385 parser->in_statement = in_statement;
7387 finish_then_clause (statement);
7389 /* If the next token is `else', parse the else-clause. */
7390 if (cp_lexer_next_token_is_keyword (parser->lexer,
7391 RID_ELSE))
7393 /* Consume the `else' keyword. */
7394 cp_lexer_consume_token (parser->lexer);
7395 begin_else_clause (statement);
7396 /* Parse the else-clause. */
7397 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7399 location_t loc;
7400 loc = cp_lexer_peek_token (parser->lexer)->location;
7401 warning_at (loc,
7402 OPT_Wempty_body, "suggest braces around "
7403 "empty body in an %<else%> statement");
7404 add_stmt (build_empty_stmt (loc));
7405 cp_lexer_consume_token (parser->lexer);
7407 else
7408 cp_parser_implicitly_scoped_statement (parser, NULL);
7410 finish_else_clause (statement);
7412 /* If we are currently parsing a then-clause, then
7413 IF_P will not be NULL. We set it to true to
7414 indicate that this if statement has an else clause.
7415 This may trigger the Wparentheses warning below
7416 when we get back up to the parent if statement. */
7417 if (if_p != NULL)
7418 *if_p = true;
7420 else
7422 /* This if statement does not have an else clause. If
7423 NESTED_IF is true, then the then-clause is an if
7424 statement which does have an else clause. We warn
7425 about the potential ambiguity. */
7426 if (nested_if)
7427 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
7428 "suggest explicit braces to avoid ambiguous"
7429 " %<else%>");
7432 /* Now we're all done with the if-statement. */
7433 finish_if_stmt (statement);
7435 else
7437 bool in_switch_statement_p;
7438 unsigned char in_statement;
7440 /* Add the condition. */
7441 finish_switch_cond (condition, statement);
7443 /* Parse the body of the switch-statement. */
7444 in_switch_statement_p = parser->in_switch_statement_p;
7445 in_statement = parser->in_statement;
7446 parser->in_switch_statement_p = true;
7447 parser->in_statement |= IN_SWITCH_STMT;
7448 cp_parser_implicitly_scoped_statement (parser, NULL);
7449 parser->in_switch_statement_p = in_switch_statement_p;
7450 parser->in_statement = in_statement;
7452 /* Now we're all done with the switch-statement. */
7453 finish_switch_stmt (statement);
7456 return statement;
7458 break;
7460 default:
7461 cp_parser_error (parser, "expected selection-statement");
7462 return error_mark_node;
7466 /* Parse a condition.
7468 condition:
7469 expression
7470 type-specifier-seq declarator = initializer-clause
7471 type-specifier-seq declarator braced-init-list
7473 GNU Extension:
7475 condition:
7476 type-specifier-seq declarator asm-specification [opt]
7477 attributes [opt] = assignment-expression
7479 Returns the expression that should be tested. */
7481 static tree
7482 cp_parser_condition (cp_parser* parser)
7484 cp_decl_specifier_seq type_specifiers;
7485 const char *saved_message;
7487 /* Try the declaration first. */
7488 cp_parser_parse_tentatively (parser);
7489 /* New types are not allowed in the type-specifier-seq for a
7490 condition. */
7491 saved_message = parser->type_definition_forbidden_message;
7492 parser->type_definition_forbidden_message
7493 = "types may not be defined in conditions";
7494 /* Parse the type-specifier-seq. */
7495 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
7496 &type_specifiers);
7497 /* Restore the saved message. */
7498 parser->type_definition_forbidden_message = saved_message;
7499 /* If all is well, we might be looking at a declaration. */
7500 if (!cp_parser_error_occurred (parser))
7502 tree decl;
7503 tree asm_specification;
7504 tree attributes;
7505 cp_declarator *declarator;
7506 tree initializer = NULL_TREE;
7508 /* Parse the declarator. */
7509 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7510 /*ctor_dtor_or_conv_p=*/NULL,
7511 /*parenthesized_p=*/NULL,
7512 /*member_p=*/false);
7513 /* Parse the attributes. */
7514 attributes = cp_parser_attributes_opt (parser);
7515 /* Parse the asm-specification. */
7516 asm_specification = cp_parser_asm_specification_opt (parser);
7517 /* If the next token is not an `=' or '{', then we might still be
7518 looking at an expression. For example:
7520 if (A(a).x)
7522 looks like a decl-specifier-seq and a declarator -- but then
7523 there is no `=', so this is an expression. */
7524 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7525 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7526 cp_parser_simulate_error (parser);
7528 /* If we did see an `=' or '{', then we are looking at a declaration
7529 for sure. */
7530 if (cp_parser_parse_definitely (parser))
7532 tree pushed_scope;
7533 bool non_constant_p;
7534 bool flags = LOOKUP_ONLYCONVERTING;
7536 /* Create the declaration. */
7537 decl = start_decl (declarator, &type_specifiers,
7538 /*initialized_p=*/true,
7539 attributes, /*prefix_attributes=*/NULL_TREE,
7540 &pushed_scope);
7542 /* Parse the initializer. */
7543 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7545 initializer = cp_parser_braced_list (parser, &non_constant_p);
7546 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
7547 flags = 0;
7549 else
7551 /* Consume the `='. */
7552 cp_parser_require (parser, CPP_EQ, "%<=%>");
7553 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
7555 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
7556 maybe_warn_cpp0x ("extended initializer lists");
7558 if (!non_constant_p)
7559 initializer = fold_non_dependent_expr (initializer);
7561 /* Process the initializer. */
7562 cp_finish_decl (decl,
7563 initializer, !non_constant_p,
7564 asm_specification,
7565 flags);
7567 if (pushed_scope)
7568 pop_scope (pushed_scope);
7570 return convert_from_reference (decl);
7573 /* If we didn't even get past the declarator successfully, we are
7574 definitely not looking at a declaration. */
7575 else
7576 cp_parser_abort_tentative_parse (parser);
7578 /* Otherwise, we are looking at an expression. */
7579 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
7582 /* Parse an iteration-statement.
7584 iteration-statement:
7585 while ( condition ) statement
7586 do statement while ( expression ) ;
7587 for ( for-init-statement condition [opt] ; expression [opt] )
7588 statement
7590 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
7592 static tree
7593 cp_parser_iteration_statement (cp_parser* parser)
7595 cp_token *token;
7596 enum rid keyword;
7597 tree statement;
7598 unsigned char in_statement;
7600 /* Peek at the next token. */
7601 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
7602 if (!token)
7603 return error_mark_node;
7605 /* Remember whether or not we are already within an iteration
7606 statement. */
7607 in_statement = parser->in_statement;
7609 /* See what kind of keyword it is. */
7610 keyword = token->keyword;
7611 switch (keyword)
7613 case RID_WHILE:
7615 tree condition;
7617 /* Begin the while-statement. */
7618 statement = begin_while_stmt ();
7619 /* Look for the `('. */
7620 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7621 /* Parse the condition. */
7622 condition = cp_parser_condition (parser);
7623 finish_while_stmt_cond (condition, statement);
7624 /* Look for the `)'. */
7625 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7626 /* Parse the dependent statement. */
7627 parser->in_statement = IN_ITERATION_STMT;
7628 cp_parser_already_scoped_statement (parser);
7629 parser->in_statement = in_statement;
7630 /* We're done with the while-statement. */
7631 finish_while_stmt (statement);
7633 break;
7635 case RID_DO:
7637 tree expression;
7639 /* Begin the do-statement. */
7640 statement = begin_do_stmt ();
7641 /* Parse the body of the do-statement. */
7642 parser->in_statement = IN_ITERATION_STMT;
7643 cp_parser_implicitly_scoped_statement (parser, NULL);
7644 parser->in_statement = in_statement;
7645 finish_do_body (statement);
7646 /* Look for the `while' keyword. */
7647 cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
7648 /* Look for the `('. */
7649 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7650 /* Parse the expression. */
7651 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7652 /* We're done with the do-statement. */
7653 finish_do_stmt (expression, statement);
7654 /* Look for the `)'. */
7655 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7656 /* Look for the `;'. */
7657 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7659 break;
7661 case RID_FOR:
7663 tree condition = NULL_TREE;
7664 tree expression = NULL_TREE;
7666 /* Begin the for-statement. */
7667 statement = begin_for_stmt ();
7668 /* Look for the `('. */
7669 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7670 /* Parse the initialization. */
7671 cp_parser_for_init_statement (parser);
7672 finish_for_init_stmt (statement);
7674 /* If there's a condition, process it. */
7675 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7676 condition = cp_parser_condition (parser);
7677 finish_for_cond (condition, statement);
7678 /* Look for the `;'. */
7679 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7681 /* If there's an expression, process it. */
7682 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7683 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7684 finish_for_expr (expression, statement);
7685 /* Look for the `)'. */
7686 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7688 /* Parse the body of the for-statement. */
7689 parser->in_statement = IN_ITERATION_STMT;
7690 cp_parser_already_scoped_statement (parser);
7691 parser->in_statement = in_statement;
7693 /* We're done with the for-statement. */
7694 finish_for_stmt (statement);
7696 break;
7698 default:
7699 cp_parser_error (parser, "expected iteration-statement");
7700 statement = error_mark_node;
7701 break;
7704 return statement;
7707 /* Parse a for-init-statement.
7709 for-init-statement:
7710 expression-statement
7711 simple-declaration */
7713 static void
7714 cp_parser_for_init_statement (cp_parser* parser)
7716 /* If the next token is a `;', then we have an empty
7717 expression-statement. Grammatically, this is also a
7718 simple-declaration, but an invalid one, because it does not
7719 declare anything. Therefore, if we did not handle this case
7720 specially, we would issue an error message about an invalid
7721 declaration. */
7722 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7724 /* We're going to speculatively look for a declaration, falling back
7725 to an expression, if necessary. */
7726 cp_parser_parse_tentatively (parser);
7727 /* Parse the declaration. */
7728 cp_parser_simple_declaration (parser,
7729 /*function_definition_allowed_p=*/false);
7730 /* If the tentative parse failed, then we shall need to look for an
7731 expression-statement. */
7732 if (cp_parser_parse_definitely (parser))
7733 return;
7736 cp_parser_expression_statement (parser, false);
7739 /* Parse a jump-statement.
7741 jump-statement:
7742 break ;
7743 continue ;
7744 return expression [opt] ;
7745 return braced-init-list ;
7746 goto identifier ;
7748 GNU extension:
7750 jump-statement:
7751 goto * expression ;
7753 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
7755 static tree
7756 cp_parser_jump_statement (cp_parser* parser)
7758 tree statement = error_mark_node;
7759 cp_token *token;
7760 enum rid keyword;
7761 unsigned char in_statement;
7763 /* Peek at the next token. */
7764 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7765 if (!token)
7766 return error_mark_node;
7768 /* See what kind of keyword it is. */
7769 keyword = token->keyword;
7770 switch (keyword)
7772 case RID_BREAK:
7773 in_statement = parser->in_statement & ~IN_IF_STMT;
7774 switch (in_statement)
7776 case 0:
7777 error_at (token->location, "break statement not within loop or switch");
7778 break;
7779 default:
7780 gcc_assert ((in_statement & IN_SWITCH_STMT)
7781 || in_statement == IN_ITERATION_STMT);
7782 statement = finish_break_stmt ();
7783 break;
7784 case IN_OMP_BLOCK:
7785 error_at (token->location, "invalid exit from OpenMP structured block");
7786 break;
7787 case IN_OMP_FOR:
7788 error_at (token->location, "break statement used with OpenMP for loop");
7789 break;
7791 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7792 break;
7794 case RID_CONTINUE:
7795 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7797 case 0:
7798 error_at (token->location, "continue statement not within a loop");
7799 break;
7800 case IN_ITERATION_STMT:
7801 case IN_OMP_FOR:
7802 statement = finish_continue_stmt ();
7803 break;
7804 case IN_OMP_BLOCK:
7805 error_at (token->location, "invalid exit from OpenMP structured block");
7806 break;
7807 default:
7808 gcc_unreachable ();
7810 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7811 break;
7813 case RID_RETURN:
7815 tree expr;
7816 bool expr_non_constant_p;
7818 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7820 maybe_warn_cpp0x ("extended initializer lists");
7821 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7823 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7824 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7825 else
7826 /* If the next token is a `;', then there is no
7827 expression. */
7828 expr = NULL_TREE;
7829 /* Build the return-statement. */
7830 statement = finish_return_stmt (expr);
7831 /* Look for the final `;'. */
7832 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7834 break;
7836 case RID_GOTO:
7837 /* Create the goto-statement. */
7838 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7840 /* Issue a warning about this use of a GNU extension. */
7841 pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
7842 /* Consume the '*' token. */
7843 cp_lexer_consume_token (parser->lexer);
7844 /* Parse the dependent expression. */
7845 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
7847 else
7848 finish_goto_stmt (cp_parser_identifier (parser));
7849 /* Look for the final `;'. */
7850 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7851 break;
7853 default:
7854 cp_parser_error (parser, "expected jump-statement");
7855 break;
7858 return statement;
7861 /* Parse a declaration-statement.
7863 declaration-statement:
7864 block-declaration */
7866 static void
7867 cp_parser_declaration_statement (cp_parser* parser)
7869 void *p;
7871 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7872 p = obstack_alloc (&declarator_obstack, 0);
7874 /* Parse the block-declaration. */
7875 cp_parser_block_declaration (parser, /*statement_p=*/true);
7877 /* Free any declarators allocated. */
7878 obstack_free (&declarator_obstack, p);
7880 /* Finish off the statement. */
7881 finish_stmt ();
7884 /* Some dependent statements (like `if (cond) statement'), are
7885 implicitly in their own scope. In other words, if the statement is
7886 a single statement (as opposed to a compound-statement), it is
7887 none-the-less treated as if it were enclosed in braces. Any
7888 declarations appearing in the dependent statement are out of scope
7889 after control passes that point. This function parses a statement,
7890 but ensures that is in its own scope, even if it is not a
7891 compound-statement.
7893 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7894 is a (possibly labeled) if statement which is not enclosed in
7895 braces and has an else clause. This is used to implement
7896 -Wparentheses.
7898 Returns the new statement. */
7900 static tree
7901 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7903 tree statement;
7905 if (if_p != NULL)
7906 *if_p = false;
7908 /* Mark if () ; with a special NOP_EXPR. */
7909 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7911 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7912 cp_lexer_consume_token (parser->lexer);
7913 statement = add_stmt (build_empty_stmt (loc));
7915 /* if a compound is opened, we simply parse the statement directly. */
7916 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7917 statement = cp_parser_compound_statement (parser, NULL, false);
7918 /* If the token is not a `{', then we must take special action. */
7919 else
7921 /* Create a compound-statement. */
7922 statement = begin_compound_stmt (0);
7923 /* Parse the dependent-statement. */
7924 cp_parser_statement (parser, NULL_TREE, false, if_p);
7925 /* Finish the dummy compound-statement. */
7926 finish_compound_stmt (statement);
7929 /* Return the statement. */
7930 return statement;
7933 /* For some dependent statements (like `while (cond) statement'), we
7934 have already created a scope. Therefore, even if the dependent
7935 statement is a compound-statement, we do not want to create another
7936 scope. */
7938 static void
7939 cp_parser_already_scoped_statement (cp_parser* parser)
7941 /* If the token is a `{', then we must take special action. */
7942 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7943 cp_parser_statement (parser, NULL_TREE, false, NULL);
7944 else
7946 /* Avoid calling cp_parser_compound_statement, so that we
7947 don't create a new scope. Do everything else by hand. */
7948 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7949 /* If the next keyword is `__label__' we have a label declaration. */
7950 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7951 cp_parser_label_declaration (parser);
7952 /* Parse an (optional) statement-seq. */
7953 cp_parser_statement_seq_opt (parser, NULL_TREE);
7954 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7958 /* Declarations [gram.dcl.dcl] */
7960 /* Parse an optional declaration-sequence.
7962 declaration-seq:
7963 declaration
7964 declaration-seq declaration */
7966 static void
7967 cp_parser_declaration_seq_opt (cp_parser* parser)
7969 while (true)
7971 cp_token *token;
7973 token = cp_lexer_peek_token (parser->lexer);
7975 if (token->type == CPP_CLOSE_BRACE
7976 || token->type == CPP_EOF
7977 || token->type == CPP_PRAGMA_EOL)
7978 break;
7980 if (token->type == CPP_SEMICOLON)
7982 /* A declaration consisting of a single semicolon is
7983 invalid. Allow it unless we're being pedantic. */
7984 cp_lexer_consume_token (parser->lexer);
7985 if (!in_system_header)
7986 pedwarn (input_location, OPT_pedantic, "extra %<;%>");
7987 continue;
7990 /* If we're entering or exiting a region that's implicitly
7991 extern "C", modify the lang context appropriately. */
7992 if (!parser->implicit_extern_c && token->implicit_extern_c)
7994 push_lang_context (lang_name_c);
7995 parser->implicit_extern_c = true;
7997 else if (parser->implicit_extern_c && !token->implicit_extern_c)
7999 pop_lang_context ();
8000 parser->implicit_extern_c = false;
8003 if (token->type == CPP_PRAGMA)
8005 /* A top-level declaration can consist solely of a #pragma.
8006 A nested declaration cannot, so this is done here and not
8007 in cp_parser_declaration. (A #pragma at block scope is
8008 handled in cp_parser_statement.) */
8009 cp_parser_pragma (parser, pragma_external);
8010 continue;
8013 /* Parse the declaration itself. */
8014 cp_parser_declaration (parser);
8018 /* Parse a declaration.
8020 declaration:
8021 block-declaration
8022 function-definition
8023 template-declaration
8024 explicit-instantiation
8025 explicit-specialization
8026 linkage-specification
8027 namespace-definition
8029 GNU extension:
8031 declaration:
8032 __extension__ declaration */
8034 static void
8035 cp_parser_declaration (cp_parser* parser)
8037 cp_token token1;
8038 cp_token token2;
8039 int saved_pedantic;
8040 void *p;
8042 /* Check for the `__extension__' keyword. */
8043 if (cp_parser_extension_opt (parser, &saved_pedantic))
8045 /* Parse the qualified declaration. */
8046 cp_parser_declaration (parser);
8047 /* Restore the PEDANTIC flag. */
8048 pedantic = saved_pedantic;
8050 return;
8053 /* Try to figure out what kind of declaration is present. */
8054 token1 = *cp_lexer_peek_token (parser->lexer);
8056 if (token1.type != CPP_EOF)
8057 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
8058 else
8060 token2.type = CPP_EOF;
8061 token2.keyword = RID_MAX;
8064 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
8065 p = obstack_alloc (&declarator_obstack, 0);
8067 /* If the next token is `extern' and the following token is a string
8068 literal, then we have a linkage specification. */
8069 if (token1.keyword == RID_EXTERN
8070 && cp_parser_is_string_literal (&token2))
8071 cp_parser_linkage_specification (parser);
8072 /* If the next token is `template', then we have either a template
8073 declaration, an explicit instantiation, or an explicit
8074 specialization. */
8075 else if (token1.keyword == RID_TEMPLATE)
8077 /* `template <>' indicates a template specialization. */
8078 if (token2.type == CPP_LESS
8079 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
8080 cp_parser_explicit_specialization (parser);
8081 /* `template <' indicates a template declaration. */
8082 else if (token2.type == CPP_LESS)
8083 cp_parser_template_declaration (parser, /*member_p=*/false);
8084 /* Anything else must be an explicit instantiation. */
8085 else
8086 cp_parser_explicit_instantiation (parser);
8088 /* If the next token is `export', then we have a template
8089 declaration. */
8090 else if (token1.keyword == RID_EXPORT)
8091 cp_parser_template_declaration (parser, /*member_p=*/false);
8092 /* If the next token is `extern', 'static' or 'inline' and the one
8093 after that is `template', we have a GNU extended explicit
8094 instantiation directive. */
8095 else if (cp_parser_allow_gnu_extensions_p (parser)
8096 && (token1.keyword == RID_EXTERN
8097 || token1.keyword == RID_STATIC
8098 || token1.keyword == RID_INLINE)
8099 && token2.keyword == RID_TEMPLATE)
8100 cp_parser_explicit_instantiation (parser);
8101 /* If the next token is `namespace', check for a named or unnamed
8102 namespace definition. */
8103 else if (token1.keyword == RID_NAMESPACE
8104 && (/* A named namespace definition. */
8105 (token2.type == CPP_NAME
8106 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
8107 != CPP_EQ))
8108 /* An unnamed namespace definition. */
8109 || token2.type == CPP_OPEN_BRACE
8110 || token2.keyword == RID_ATTRIBUTE))
8111 cp_parser_namespace_definition (parser);
8112 /* An inline (associated) namespace definition. */
8113 else if (token1.keyword == RID_INLINE
8114 && token2.keyword == RID_NAMESPACE)
8115 cp_parser_namespace_definition (parser);
8116 /* Objective-C++ declaration/definition. */
8117 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
8118 cp_parser_objc_declaration (parser);
8119 /* We must have either a block declaration or a function
8120 definition. */
8121 else
8122 /* Try to parse a block-declaration, or a function-definition. */
8123 cp_parser_block_declaration (parser, /*statement_p=*/false);
8125 /* Free any declarators allocated. */
8126 obstack_free (&declarator_obstack, p);
8129 /* Parse a block-declaration.
8131 block-declaration:
8132 simple-declaration
8133 asm-definition
8134 namespace-alias-definition
8135 using-declaration
8136 using-directive
8138 GNU Extension:
8140 block-declaration:
8141 __extension__ block-declaration
8143 C++0x Extension:
8145 block-declaration:
8146 static_assert-declaration
8148 If STATEMENT_P is TRUE, then this block-declaration is occurring as
8149 part of a declaration-statement. */
8151 static void
8152 cp_parser_block_declaration (cp_parser *parser,
8153 bool statement_p)
8155 cp_token *token1;
8156 int saved_pedantic;
8158 /* Check for the `__extension__' keyword. */
8159 if (cp_parser_extension_opt (parser, &saved_pedantic))
8161 /* Parse the qualified declaration. */
8162 cp_parser_block_declaration (parser, statement_p);
8163 /* Restore the PEDANTIC flag. */
8164 pedantic = saved_pedantic;
8166 return;
8169 /* Peek at the next token to figure out which kind of declaration is
8170 present. */
8171 token1 = cp_lexer_peek_token (parser->lexer);
8173 /* If the next keyword is `asm', we have an asm-definition. */
8174 if (token1->keyword == RID_ASM)
8176 if (statement_p)
8177 cp_parser_commit_to_tentative_parse (parser);
8178 cp_parser_asm_definition (parser);
8180 /* If the next keyword is `namespace', we have a
8181 namespace-alias-definition. */
8182 else if (token1->keyword == RID_NAMESPACE)
8183 cp_parser_namespace_alias_definition (parser);
8184 /* If the next keyword is `using', we have either a
8185 using-declaration or a using-directive. */
8186 else if (token1->keyword == RID_USING)
8188 cp_token *token2;
8190 if (statement_p)
8191 cp_parser_commit_to_tentative_parse (parser);
8192 /* If the token after `using' is `namespace', then we have a
8193 using-directive. */
8194 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8195 if (token2->keyword == RID_NAMESPACE)
8196 cp_parser_using_directive (parser);
8197 /* Otherwise, it's a using-declaration. */
8198 else
8199 cp_parser_using_declaration (parser,
8200 /*access_declaration_p=*/false);
8202 /* If the next keyword is `__label__' we have a misplaced label
8203 declaration. */
8204 else if (token1->keyword == RID_LABEL)
8206 cp_lexer_consume_token (parser->lexer);
8207 error_at (token1->location, "%<__label__%> not at the beginning of a block");
8208 cp_parser_skip_to_end_of_statement (parser);
8209 /* If the next token is now a `;', consume it. */
8210 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8211 cp_lexer_consume_token (parser->lexer);
8213 /* If the next token is `static_assert' we have a static assertion. */
8214 else if (token1->keyword == RID_STATIC_ASSERT)
8215 cp_parser_static_assert (parser, /*member_p=*/false);
8216 /* Anything else must be a simple-declaration. */
8217 else
8218 cp_parser_simple_declaration (parser, !statement_p);
8221 /* Parse a simple-declaration.
8223 simple-declaration:
8224 decl-specifier-seq [opt] init-declarator-list [opt] ;
8226 init-declarator-list:
8227 init-declarator
8228 init-declarator-list , init-declarator
8230 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8231 function-definition as a simple-declaration. */
8233 static void
8234 cp_parser_simple_declaration (cp_parser* parser,
8235 bool function_definition_allowed_p)
8237 cp_decl_specifier_seq decl_specifiers;
8238 int declares_class_or_enum;
8239 bool saw_declarator;
8241 /* Defer access checks until we know what is being declared; the
8242 checks for names appearing in the decl-specifier-seq should be
8243 done as if we were in the scope of the thing being declared. */
8244 push_deferring_access_checks (dk_deferred);
8246 /* Parse the decl-specifier-seq. We have to keep track of whether
8247 or not the decl-specifier-seq declares a named class or
8248 enumeration type, since that is the only case in which the
8249 init-declarator-list is allowed to be empty.
8251 [dcl.dcl]
8253 In a simple-declaration, the optional init-declarator-list can be
8254 omitted only when declaring a class or enumeration, that is when
8255 the decl-specifier-seq contains either a class-specifier, an
8256 elaborated-type-specifier, or an enum-specifier. */
8257 cp_parser_decl_specifier_seq (parser,
8258 CP_PARSER_FLAGS_OPTIONAL,
8259 &decl_specifiers,
8260 &declares_class_or_enum);
8261 /* We no longer need to defer access checks. */
8262 stop_deferring_access_checks ();
8264 /* In a block scope, a valid declaration must always have a
8265 decl-specifier-seq. By not trying to parse declarators, we can
8266 resolve the declaration/expression ambiguity more quickly. */
8267 if (!function_definition_allowed_p
8268 && !decl_specifiers.any_specifiers_p)
8270 cp_parser_error (parser, "expected declaration");
8271 goto done;
8274 /* If the next two tokens are both identifiers, the code is
8275 erroneous. The usual cause of this situation is code like:
8277 T t;
8279 where "T" should name a type -- but does not. */
8280 if (!decl_specifiers.type
8281 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8283 /* If parsing tentatively, we should commit; we really are
8284 looking at a declaration. */
8285 cp_parser_commit_to_tentative_parse (parser);
8286 /* Give up. */
8287 goto done;
8290 /* If we have seen at least one decl-specifier, and the next token
8291 is not a parenthesis, then we must be looking at a declaration.
8292 (After "int (" we might be looking at a functional cast.) */
8293 if (decl_specifiers.any_specifiers_p
8294 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8295 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
8296 && !cp_parser_error_occurred (parser))
8297 cp_parser_commit_to_tentative_parse (parser);
8299 /* Keep going until we hit the `;' at the end of the simple
8300 declaration. */
8301 saw_declarator = false;
8302 while (cp_lexer_next_token_is_not (parser->lexer,
8303 CPP_SEMICOLON))
8305 cp_token *token;
8306 bool function_definition_p;
8307 tree decl;
8309 if (saw_declarator)
8311 /* If we are processing next declarator, coma is expected */
8312 token = cp_lexer_peek_token (parser->lexer);
8313 gcc_assert (token->type == CPP_COMMA);
8314 cp_lexer_consume_token (parser->lexer);
8316 else
8317 saw_declarator = true;
8319 /* Parse the init-declarator. */
8320 decl = cp_parser_init_declarator (parser, &decl_specifiers,
8321 /*checks=*/NULL,
8322 function_definition_allowed_p,
8323 /*member_p=*/false,
8324 declares_class_or_enum,
8325 &function_definition_p);
8326 /* If an error occurred while parsing tentatively, exit quickly.
8327 (That usually happens when in the body of a function; each
8328 statement is treated as a declaration-statement until proven
8329 otherwise.) */
8330 if (cp_parser_error_occurred (parser))
8331 goto done;
8332 /* Handle function definitions specially. */
8333 if (function_definition_p)
8335 /* If the next token is a `,', then we are probably
8336 processing something like:
8338 void f() {}, *p;
8340 which is erroneous. */
8341 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8343 cp_token *token = cp_lexer_peek_token (parser->lexer);
8344 error_at (token->location,
8345 "mixing"
8346 " declarations and function-definitions is forbidden");
8348 /* Otherwise, we're done with the list of declarators. */
8349 else
8351 pop_deferring_access_checks ();
8352 return;
8355 /* The next token should be either a `,' or a `;'. */
8356 token = cp_lexer_peek_token (parser->lexer);
8357 /* If it's a `,', there are more declarators to come. */
8358 if (token->type == CPP_COMMA)
8359 /* will be consumed next time around */;
8360 /* If it's a `;', we are done. */
8361 else if (token->type == CPP_SEMICOLON)
8362 break;
8363 /* Anything else is an error. */
8364 else
8366 /* If we have already issued an error message we don't need
8367 to issue another one. */
8368 if (decl != error_mark_node
8369 || cp_parser_uncommitted_to_tentative_parse_p (parser))
8370 cp_parser_error (parser, "expected %<,%> or %<;%>");
8371 /* Skip tokens until we reach the end of the statement. */
8372 cp_parser_skip_to_end_of_statement (parser);
8373 /* If the next token is now a `;', consume it. */
8374 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8375 cp_lexer_consume_token (parser->lexer);
8376 goto done;
8378 /* After the first time around, a function-definition is not
8379 allowed -- even if it was OK at first. For example:
8381 int i, f() {}
8383 is not valid. */
8384 function_definition_allowed_p = false;
8387 /* Issue an error message if no declarators are present, and the
8388 decl-specifier-seq does not itself declare a class or
8389 enumeration. */
8390 if (!saw_declarator)
8392 if (cp_parser_declares_only_class_p (parser))
8393 shadow_tag (&decl_specifiers);
8394 /* Perform any deferred access checks. */
8395 perform_deferred_access_checks ();
8398 /* Consume the `;'. */
8399 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8401 done:
8402 pop_deferring_access_checks ();
8405 /* Parse a decl-specifier-seq.
8407 decl-specifier-seq:
8408 decl-specifier-seq [opt] decl-specifier
8410 decl-specifier:
8411 storage-class-specifier
8412 type-specifier
8413 function-specifier
8414 friend
8415 typedef
8417 GNU Extension:
8419 decl-specifier:
8420 attributes
8422 Set *DECL_SPECS to a representation of the decl-specifier-seq.
8424 The parser flags FLAGS is used to control type-specifier parsing.
8426 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8427 flags:
8429 1: one of the decl-specifiers is an elaborated-type-specifier
8430 (i.e., a type declaration)
8431 2: one of the decl-specifiers is an enum-specifier or a
8432 class-specifier (i.e., a type definition)
8436 static void
8437 cp_parser_decl_specifier_seq (cp_parser* parser,
8438 cp_parser_flags flags,
8439 cp_decl_specifier_seq *decl_specs,
8440 int* declares_class_or_enum)
8442 bool constructor_possible_p = !parser->in_declarator_p;
8443 cp_token *start_token = NULL;
8445 /* Clear DECL_SPECS. */
8446 clear_decl_specs (decl_specs);
8448 /* Assume no class or enumeration type is declared. */
8449 *declares_class_or_enum = 0;
8451 /* Keep reading specifiers until there are no more to read. */
8452 while (true)
8454 bool constructor_p;
8455 bool found_decl_spec;
8456 cp_token *token;
8458 /* Peek at the next token. */
8459 token = cp_lexer_peek_token (parser->lexer);
8461 /* Save the first token of the decl spec list for error
8462 reporting. */
8463 if (!start_token)
8464 start_token = token;
8465 /* Handle attributes. */
8466 if (token->keyword == RID_ATTRIBUTE)
8468 /* Parse the attributes. */
8469 decl_specs->attributes
8470 = chainon (decl_specs->attributes,
8471 cp_parser_attributes_opt (parser));
8472 continue;
8474 /* Assume we will find a decl-specifier keyword. */
8475 found_decl_spec = true;
8476 /* If the next token is an appropriate keyword, we can simply
8477 add it to the list. */
8478 switch (token->keyword)
8480 /* decl-specifier:
8481 friend */
8482 case RID_FRIEND:
8483 if (!at_class_scope_p ())
8485 error_at (token->location, "%<friend%> used outside of class");
8486 cp_lexer_purge_token (parser->lexer);
8488 else
8490 ++decl_specs->specs[(int) ds_friend];
8491 /* Consume the token. */
8492 cp_lexer_consume_token (parser->lexer);
8494 break;
8496 /* function-specifier:
8497 inline
8498 virtual
8499 explicit */
8500 case RID_INLINE:
8501 case RID_VIRTUAL:
8502 case RID_EXPLICIT:
8503 cp_parser_function_specifier_opt (parser, decl_specs);
8504 break;
8506 /* decl-specifier:
8507 typedef */
8508 case RID_TYPEDEF:
8509 ++decl_specs->specs[(int) ds_typedef];
8510 /* Consume the token. */
8511 cp_lexer_consume_token (parser->lexer);
8512 /* A constructor declarator cannot appear in a typedef. */
8513 constructor_possible_p = false;
8514 /* The "typedef" keyword can only occur in a declaration; we
8515 may as well commit at this point. */
8516 cp_parser_commit_to_tentative_parse (parser);
8518 if (decl_specs->storage_class != sc_none)
8519 decl_specs->conflicting_specifiers_p = true;
8520 break;
8522 /* storage-class-specifier:
8523 auto
8524 register
8525 static
8526 extern
8527 mutable
8529 GNU Extension:
8530 thread */
8531 case RID_AUTO:
8532 if (cxx_dialect == cxx98)
8534 /* Consume the token. */
8535 cp_lexer_consume_token (parser->lexer);
8537 /* Complain about `auto' as a storage specifier, if
8538 we're complaining about C++0x compatibility. */
8539 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
8540 " will change meaning in C++0x; please remove it");
8542 /* Set the storage class anyway. */
8543 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
8544 token->location);
8546 else
8547 /* C++0x auto type-specifier. */
8548 found_decl_spec = false;
8549 break;
8551 case RID_REGISTER:
8552 case RID_STATIC:
8553 case RID_EXTERN:
8554 case RID_MUTABLE:
8555 /* Consume the token. */
8556 cp_lexer_consume_token (parser->lexer);
8557 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
8558 token->location);
8559 break;
8560 case RID_THREAD:
8561 /* Consume the token. */
8562 cp_lexer_consume_token (parser->lexer);
8563 ++decl_specs->specs[(int) ds_thread];
8564 break;
8566 default:
8567 /* We did not yet find a decl-specifier yet. */
8568 found_decl_spec = false;
8569 break;
8572 /* Constructors are a special case. The `S' in `S()' is not a
8573 decl-specifier; it is the beginning of the declarator. */
8574 constructor_p
8575 = (!found_decl_spec
8576 && constructor_possible_p
8577 && (cp_parser_constructor_declarator_p
8578 (parser, decl_specs->specs[(int) ds_friend] != 0)));
8580 /* If we don't have a DECL_SPEC yet, then we must be looking at
8581 a type-specifier. */
8582 if (!found_decl_spec && !constructor_p)
8584 int decl_spec_declares_class_or_enum;
8585 bool is_cv_qualifier;
8586 tree type_spec;
8588 type_spec
8589 = cp_parser_type_specifier (parser, flags,
8590 decl_specs,
8591 /*is_declaration=*/true,
8592 &decl_spec_declares_class_or_enum,
8593 &is_cv_qualifier);
8594 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8596 /* If this type-specifier referenced a user-defined type
8597 (a typedef, class-name, etc.), then we can't allow any
8598 more such type-specifiers henceforth.
8600 [dcl.spec]
8602 The longest sequence of decl-specifiers that could
8603 possibly be a type name is taken as the
8604 decl-specifier-seq of a declaration. The sequence shall
8605 be self-consistent as described below.
8607 [dcl.type]
8609 As a general rule, at most one type-specifier is allowed
8610 in the complete decl-specifier-seq of a declaration. The
8611 only exceptions are the following:
8613 -- const or volatile can be combined with any other
8614 type-specifier.
8616 -- signed or unsigned can be combined with char, long,
8617 short, or int.
8619 -- ..
8621 Example:
8623 typedef char* Pc;
8624 void g (const int Pc);
8626 Here, Pc is *not* part of the decl-specifier seq; it's
8627 the declarator. Therefore, once we see a type-specifier
8628 (other than a cv-qualifier), we forbid any additional
8629 user-defined types. We *do* still allow things like `int
8630 int' to be considered a decl-specifier-seq, and issue the
8631 error message later. */
8632 if (type_spec && !is_cv_qualifier)
8633 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
8634 /* A constructor declarator cannot follow a type-specifier. */
8635 if (type_spec)
8637 constructor_possible_p = false;
8638 found_decl_spec = true;
8642 /* If we still do not have a DECL_SPEC, then there are no more
8643 decl-specifiers. */
8644 if (!found_decl_spec)
8645 break;
8647 decl_specs->any_specifiers_p = true;
8648 /* After we see one decl-specifier, further decl-specifiers are
8649 always optional. */
8650 flags |= CP_PARSER_FLAGS_OPTIONAL;
8653 cp_parser_check_decl_spec (decl_specs, start_token->location);
8655 /* Don't allow a friend specifier with a class definition. */
8656 if (decl_specs->specs[(int) ds_friend] != 0
8657 && (*declares_class_or_enum & 2))
8658 error_at (start_token->location,
8659 "class definition may not be declared a friend");
8662 /* Parse an (optional) storage-class-specifier.
8664 storage-class-specifier:
8665 auto
8666 register
8667 static
8668 extern
8669 mutable
8671 GNU Extension:
8673 storage-class-specifier:
8674 thread
8676 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
8678 static tree
8679 cp_parser_storage_class_specifier_opt (cp_parser* parser)
8681 switch (cp_lexer_peek_token (parser->lexer)->keyword)
8683 case RID_AUTO:
8684 if (cxx_dialect != cxx98)
8685 return NULL_TREE;
8686 /* Fall through for C++98. */
8688 case RID_REGISTER:
8689 case RID_STATIC:
8690 case RID_EXTERN:
8691 case RID_MUTABLE:
8692 case RID_THREAD:
8693 /* Consume the token. */
8694 return cp_lexer_consume_token (parser->lexer)->u.value;
8696 default:
8697 return NULL_TREE;
8701 /* Parse an (optional) function-specifier.
8703 function-specifier:
8704 inline
8705 virtual
8706 explicit
8708 Returns an IDENTIFIER_NODE corresponding to the keyword used.
8709 Updates DECL_SPECS, if it is non-NULL. */
8711 static tree
8712 cp_parser_function_specifier_opt (cp_parser* parser,
8713 cp_decl_specifier_seq *decl_specs)
8715 cp_token *token = cp_lexer_peek_token (parser->lexer);
8716 switch (token->keyword)
8718 case RID_INLINE:
8719 if (decl_specs)
8720 ++decl_specs->specs[(int) ds_inline];
8721 break;
8723 case RID_VIRTUAL:
8724 /* 14.5.2.3 [temp.mem]
8726 A member function template shall not be virtual. */
8727 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8728 error_at (token->location, "templates may not be %<virtual%>");
8729 else if (decl_specs)
8730 ++decl_specs->specs[(int) ds_virtual];
8731 break;
8733 case RID_EXPLICIT:
8734 if (decl_specs)
8735 ++decl_specs->specs[(int) ds_explicit];
8736 break;
8738 default:
8739 return NULL_TREE;
8742 /* Consume the token. */
8743 return cp_lexer_consume_token (parser->lexer)->u.value;
8746 /* Parse a linkage-specification.
8748 linkage-specification:
8749 extern string-literal { declaration-seq [opt] }
8750 extern string-literal declaration */
8752 static void
8753 cp_parser_linkage_specification (cp_parser* parser)
8755 tree linkage;
8757 /* Look for the `extern' keyword. */
8758 cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
8760 /* Look for the string-literal. */
8761 linkage = cp_parser_string_literal (parser, false, false);
8763 /* Transform the literal into an identifier. If the literal is a
8764 wide-character string, or contains embedded NULs, then we can't
8765 handle it as the user wants. */
8766 if (strlen (TREE_STRING_POINTER (linkage))
8767 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8769 cp_parser_error (parser, "invalid linkage-specification");
8770 /* Assume C++ linkage. */
8771 linkage = lang_name_cplusplus;
8773 else
8774 linkage = get_identifier (TREE_STRING_POINTER (linkage));
8776 /* We're now using the new linkage. */
8777 push_lang_context (linkage);
8779 /* If the next token is a `{', then we're using the first
8780 production. */
8781 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8783 /* Consume the `{' token. */
8784 cp_lexer_consume_token (parser->lexer);
8785 /* Parse the declarations. */
8786 cp_parser_declaration_seq_opt (parser);
8787 /* Look for the closing `}'. */
8788 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8790 /* Otherwise, there's just one declaration. */
8791 else
8793 bool saved_in_unbraced_linkage_specification_p;
8795 saved_in_unbraced_linkage_specification_p
8796 = parser->in_unbraced_linkage_specification_p;
8797 parser->in_unbraced_linkage_specification_p = true;
8798 cp_parser_declaration (parser);
8799 parser->in_unbraced_linkage_specification_p
8800 = saved_in_unbraced_linkage_specification_p;
8803 /* We're done with the linkage-specification. */
8804 pop_lang_context ();
8807 /* Parse a static_assert-declaration.
8809 static_assert-declaration:
8810 static_assert ( constant-expression , string-literal ) ;
8812 If MEMBER_P, this static_assert is a class member. */
8814 static void
8815 cp_parser_static_assert(cp_parser *parser, bool member_p)
8817 tree condition;
8818 tree message;
8819 cp_token *token;
8820 location_t saved_loc;
8822 /* Peek at the `static_assert' token so we can keep track of exactly
8823 where the static assertion started. */
8824 token = cp_lexer_peek_token (parser->lexer);
8825 saved_loc = token->location;
8827 /* Look for the `static_assert' keyword. */
8828 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
8829 "%<static_assert%>"))
8830 return;
8832 /* We know we are in a static assertion; commit to any tentative
8833 parse. */
8834 if (cp_parser_parsing_tentatively (parser))
8835 cp_parser_commit_to_tentative_parse (parser);
8837 /* Parse the `(' starting the static assertion condition. */
8838 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8840 /* Parse the constant-expression. */
8841 condition =
8842 cp_parser_constant_expression (parser,
8843 /*allow_non_constant_p=*/false,
8844 /*non_constant_p=*/NULL);
8846 /* Parse the separating `,'. */
8847 cp_parser_require (parser, CPP_COMMA, "%<,%>");
8849 /* Parse the string-literal message. */
8850 message = cp_parser_string_literal (parser,
8851 /*translate=*/false,
8852 /*wide_ok=*/true);
8854 /* A `)' completes the static assertion. */
8855 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8856 cp_parser_skip_to_closing_parenthesis (parser,
8857 /*recovering=*/true,
8858 /*or_comma=*/false,
8859 /*consume_paren=*/true);
8861 /* A semicolon terminates the declaration. */
8862 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8864 /* Complete the static assertion, which may mean either processing
8865 the static assert now or saving it for template instantiation. */
8866 finish_static_assert (condition, message, saved_loc, member_p);
8869 /* Parse a `decltype' type. Returns the type.
8871 simple-type-specifier:
8872 decltype ( expression ) */
8874 static tree
8875 cp_parser_decltype (cp_parser *parser)
8877 tree expr;
8878 bool id_expression_or_member_access_p = false;
8879 const char *saved_message;
8880 bool saved_integral_constant_expression_p;
8881 bool saved_non_integral_constant_expression_p;
8882 cp_token *id_expr_start_token;
8884 /* Look for the `decltype' token. */
8885 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
8886 return error_mark_node;
8888 /* Types cannot be defined in a `decltype' expression. Save away the
8889 old message. */
8890 saved_message = parser->type_definition_forbidden_message;
8892 /* And create the new one. */
8893 parser->type_definition_forbidden_message
8894 = "types may not be defined in %<decltype%> expressions";
8896 /* The restrictions on constant-expressions do not apply inside
8897 decltype expressions. */
8898 saved_integral_constant_expression_p
8899 = parser->integral_constant_expression_p;
8900 saved_non_integral_constant_expression_p
8901 = parser->non_integral_constant_expression_p;
8902 parser->integral_constant_expression_p = false;
8904 /* Do not actually evaluate the expression. */
8905 ++cp_unevaluated_operand;
8907 /* Do not warn about problems with the expression. */
8908 ++c_inhibit_evaluation_warnings;
8910 /* Parse the opening `('. */
8911 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
8912 return error_mark_node;
8914 /* First, try parsing an id-expression. */
8915 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
8916 cp_parser_parse_tentatively (parser);
8917 expr = cp_parser_id_expression (parser,
8918 /*template_keyword_p=*/false,
8919 /*check_dependency_p=*/true,
8920 /*template_p=*/NULL,
8921 /*declarator_p=*/false,
8922 /*optional_p=*/false);
8924 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
8926 bool non_integral_constant_expression_p = false;
8927 tree id_expression = expr;
8928 cp_id_kind idk;
8929 const char *error_msg;
8931 if (TREE_CODE (expr) == IDENTIFIER_NODE)
8932 /* Lookup the name we got back from the id-expression. */
8933 expr = cp_parser_lookup_name (parser, expr,
8934 none_type,
8935 /*is_template=*/false,
8936 /*is_namespace=*/false,
8937 /*check_dependency=*/true,
8938 /*ambiguous_decls=*/NULL,
8939 id_expr_start_token->location);
8941 if (expr
8942 && expr != error_mark_node
8943 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
8944 && TREE_CODE (expr) != TYPE_DECL
8945 && (TREE_CODE (expr) != BIT_NOT_EXPR
8946 || !TYPE_P (TREE_OPERAND (expr, 0)))
8947 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8949 /* Complete lookup of the id-expression. */
8950 expr = (finish_id_expression
8951 (id_expression, expr, parser->scope, &idk,
8952 /*integral_constant_expression_p=*/false,
8953 /*allow_non_integral_constant_expression_p=*/true,
8954 &non_integral_constant_expression_p,
8955 /*template_p=*/false,
8956 /*done=*/true,
8957 /*address_p=*/false,
8958 /*template_arg_p=*/false,
8959 &error_msg,
8960 id_expr_start_token->location));
8962 if (expr == error_mark_node)
8963 /* We found an id-expression, but it was something that we
8964 should not have found. This is an error, not something
8965 we can recover from, so note that we found an
8966 id-expression and we'll recover as gracefully as
8967 possible. */
8968 id_expression_or_member_access_p = true;
8971 if (expr
8972 && expr != error_mark_node
8973 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8974 /* We have an id-expression. */
8975 id_expression_or_member_access_p = true;
8978 if (!id_expression_or_member_access_p)
8980 /* Abort the id-expression parse. */
8981 cp_parser_abort_tentative_parse (parser);
8983 /* Parsing tentatively, again. */
8984 cp_parser_parse_tentatively (parser);
8986 /* Parse a class member access. */
8987 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
8988 /*cast_p=*/false,
8989 /*member_access_only_p=*/true, NULL);
8991 if (expr
8992 && expr != error_mark_node
8993 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8994 /* We have an id-expression. */
8995 id_expression_or_member_access_p = true;
8998 if (id_expression_or_member_access_p)
8999 /* We have parsed the complete id-expression or member access. */
9000 cp_parser_parse_definitely (parser);
9001 else
9003 /* Abort our attempt to parse an id-expression or member access
9004 expression. */
9005 cp_parser_abort_tentative_parse (parser);
9007 /* Parse a full expression. */
9008 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9011 /* Go back to evaluating expressions. */
9012 --cp_unevaluated_operand;
9013 --c_inhibit_evaluation_warnings;
9015 /* Restore the old message and the integral constant expression
9016 flags. */
9017 parser->type_definition_forbidden_message = saved_message;
9018 parser->integral_constant_expression_p
9019 = saved_integral_constant_expression_p;
9020 parser->non_integral_constant_expression_p
9021 = saved_non_integral_constant_expression_p;
9023 if (expr == error_mark_node)
9025 /* Skip everything up to the closing `)'. */
9026 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9027 /*consume_paren=*/true);
9028 return error_mark_node;
9031 /* Parse to the closing `)'. */
9032 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
9034 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9035 /*consume_paren=*/true);
9036 return error_mark_node;
9039 return finish_decltype_type (expr, id_expression_or_member_access_p);
9042 /* Special member functions [gram.special] */
9044 /* Parse a conversion-function-id.
9046 conversion-function-id:
9047 operator conversion-type-id
9049 Returns an IDENTIFIER_NODE representing the operator. */
9051 static tree
9052 cp_parser_conversion_function_id (cp_parser* parser)
9054 tree type;
9055 tree saved_scope;
9056 tree saved_qualifying_scope;
9057 tree saved_object_scope;
9058 tree pushed_scope = NULL_TREE;
9060 /* Look for the `operator' token. */
9061 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9062 return error_mark_node;
9063 /* When we parse the conversion-type-id, the current scope will be
9064 reset. However, we need that information in able to look up the
9065 conversion function later, so we save it here. */
9066 saved_scope = parser->scope;
9067 saved_qualifying_scope = parser->qualifying_scope;
9068 saved_object_scope = parser->object_scope;
9069 /* We must enter the scope of the class so that the names of
9070 entities declared within the class are available in the
9071 conversion-type-id. For example, consider:
9073 struct S {
9074 typedef int I;
9075 operator I();
9078 S::operator I() { ... }
9080 In order to see that `I' is a type-name in the definition, we
9081 must be in the scope of `S'. */
9082 if (saved_scope)
9083 pushed_scope = push_scope (saved_scope);
9084 /* Parse the conversion-type-id. */
9085 type = cp_parser_conversion_type_id (parser);
9086 /* Leave the scope of the class, if any. */
9087 if (pushed_scope)
9088 pop_scope (pushed_scope);
9089 /* Restore the saved scope. */
9090 parser->scope = saved_scope;
9091 parser->qualifying_scope = saved_qualifying_scope;
9092 parser->object_scope = saved_object_scope;
9093 /* If the TYPE is invalid, indicate failure. */
9094 if (type == error_mark_node)
9095 return error_mark_node;
9096 return mangle_conv_op_name_for_type (type);
9099 /* Parse a conversion-type-id:
9101 conversion-type-id:
9102 type-specifier-seq conversion-declarator [opt]
9104 Returns the TYPE specified. */
9106 static tree
9107 cp_parser_conversion_type_id (cp_parser* parser)
9109 tree attributes;
9110 cp_decl_specifier_seq type_specifiers;
9111 cp_declarator *declarator;
9112 tree type_specified;
9114 /* Parse the attributes. */
9115 attributes = cp_parser_attributes_opt (parser);
9116 /* Parse the type-specifiers. */
9117 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
9118 &type_specifiers);
9119 /* If that didn't work, stop. */
9120 if (type_specifiers.type == error_mark_node)
9121 return error_mark_node;
9122 /* Parse the conversion-declarator. */
9123 declarator = cp_parser_conversion_declarator_opt (parser);
9125 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
9126 /*initialized=*/0, &attributes);
9127 if (attributes)
9128 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
9130 /* Don't give this error when parsing tentatively. This happens to
9131 work because we always parse this definitively once. */
9132 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
9133 && type_uses_auto (type_specified))
9135 error ("invalid use of %<auto%> in conversion operator");
9136 return error_mark_node;
9139 return type_specified;
9142 /* Parse an (optional) conversion-declarator.
9144 conversion-declarator:
9145 ptr-operator conversion-declarator [opt]
9149 static cp_declarator *
9150 cp_parser_conversion_declarator_opt (cp_parser* parser)
9152 enum tree_code code;
9153 tree class_type;
9154 cp_cv_quals cv_quals;
9156 /* We don't know if there's a ptr-operator next, or not. */
9157 cp_parser_parse_tentatively (parser);
9158 /* Try the ptr-operator. */
9159 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
9160 /* If it worked, look for more conversion-declarators. */
9161 if (cp_parser_parse_definitely (parser))
9163 cp_declarator *declarator;
9165 /* Parse another optional declarator. */
9166 declarator = cp_parser_conversion_declarator_opt (parser);
9168 return cp_parser_make_indirect_declarator
9169 (code, class_type, cv_quals, declarator);
9172 return NULL;
9175 /* Parse an (optional) ctor-initializer.
9177 ctor-initializer:
9178 : mem-initializer-list
9180 Returns TRUE iff the ctor-initializer was actually present. */
9182 static bool
9183 cp_parser_ctor_initializer_opt (cp_parser* parser)
9185 /* If the next token is not a `:', then there is no
9186 ctor-initializer. */
9187 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
9189 /* Do default initialization of any bases and members. */
9190 if (DECL_CONSTRUCTOR_P (current_function_decl))
9191 finish_mem_initializers (NULL_TREE);
9193 return false;
9196 /* Consume the `:' token. */
9197 cp_lexer_consume_token (parser->lexer);
9198 /* And the mem-initializer-list. */
9199 cp_parser_mem_initializer_list (parser);
9201 return true;
9204 /* Parse a mem-initializer-list.
9206 mem-initializer-list:
9207 mem-initializer ... [opt]
9208 mem-initializer ... [opt] , mem-initializer-list */
9210 static void
9211 cp_parser_mem_initializer_list (cp_parser* parser)
9213 tree mem_initializer_list = NULL_TREE;
9214 cp_token *token = cp_lexer_peek_token (parser->lexer);
9216 /* Let the semantic analysis code know that we are starting the
9217 mem-initializer-list. */
9218 if (!DECL_CONSTRUCTOR_P (current_function_decl))
9219 error_at (token->location,
9220 "only constructors take base initializers");
9222 /* Loop through the list. */
9223 while (true)
9225 tree mem_initializer;
9227 token = cp_lexer_peek_token (parser->lexer);
9228 /* Parse the mem-initializer. */
9229 mem_initializer = cp_parser_mem_initializer (parser);
9230 /* If the next token is a `...', we're expanding member initializers. */
9231 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9233 /* Consume the `...'. */
9234 cp_lexer_consume_token (parser->lexer);
9236 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9237 can be expanded but members cannot. */
9238 if (mem_initializer != error_mark_node
9239 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9241 error_at (token->location,
9242 "cannot expand initializer for member %<%D%>",
9243 TREE_PURPOSE (mem_initializer));
9244 mem_initializer = error_mark_node;
9247 /* Construct the pack expansion type. */
9248 if (mem_initializer != error_mark_node)
9249 mem_initializer = make_pack_expansion (mem_initializer);
9251 /* Add it to the list, unless it was erroneous. */
9252 if (mem_initializer != error_mark_node)
9254 TREE_CHAIN (mem_initializer) = mem_initializer_list;
9255 mem_initializer_list = mem_initializer;
9257 /* If the next token is not a `,', we're done. */
9258 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9259 break;
9260 /* Consume the `,' token. */
9261 cp_lexer_consume_token (parser->lexer);
9264 /* Perform semantic analysis. */
9265 if (DECL_CONSTRUCTOR_P (current_function_decl))
9266 finish_mem_initializers (mem_initializer_list);
9269 /* Parse a mem-initializer.
9271 mem-initializer:
9272 mem-initializer-id ( expression-list [opt] )
9273 mem-initializer-id braced-init-list
9275 GNU extension:
9277 mem-initializer:
9278 ( expression-list [opt] )
9280 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
9281 class) or FIELD_DECL (for a non-static data member) to initialize;
9282 the TREE_VALUE is the expression-list. An empty initialization
9283 list is represented by void_list_node. */
9285 static tree
9286 cp_parser_mem_initializer (cp_parser* parser)
9288 tree mem_initializer_id;
9289 tree expression_list;
9290 tree member;
9291 cp_token *token = cp_lexer_peek_token (parser->lexer);
9293 /* Find out what is being initialized. */
9294 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9296 permerror (token->location,
9297 "anachronistic old-style base class initializer");
9298 mem_initializer_id = NULL_TREE;
9300 else
9302 mem_initializer_id = cp_parser_mem_initializer_id (parser);
9303 if (mem_initializer_id == error_mark_node)
9304 return mem_initializer_id;
9306 member = expand_member_init (mem_initializer_id);
9307 if (member && !DECL_P (member))
9308 in_base_initializer = 1;
9310 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9312 bool expr_non_constant_p;
9313 maybe_warn_cpp0x ("extended initializer lists");
9314 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9315 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9316 expression_list = build_tree_list (NULL_TREE, expression_list);
9318 else
9320 VEC(tree,gc)* vec;
9321 vec = cp_parser_parenthesized_expression_list (parser, false,
9322 /*cast_p=*/false,
9323 /*allow_expansion_p=*/true,
9324 /*non_constant_p=*/NULL);
9325 if (vec == NULL)
9326 return error_mark_node;
9327 expression_list = build_tree_list_vec (vec);
9328 release_tree_vector (vec);
9331 if (expression_list == error_mark_node)
9332 return error_mark_node;
9333 if (!expression_list)
9334 expression_list = void_type_node;
9336 in_base_initializer = 0;
9338 return member ? build_tree_list (member, expression_list) : error_mark_node;
9341 /* Parse a mem-initializer-id.
9343 mem-initializer-id:
9344 :: [opt] nested-name-specifier [opt] class-name
9345 identifier
9347 Returns a TYPE indicating the class to be initializer for the first
9348 production. Returns an IDENTIFIER_NODE indicating the data member
9349 to be initialized for the second production. */
9351 static tree
9352 cp_parser_mem_initializer_id (cp_parser* parser)
9354 bool global_scope_p;
9355 bool nested_name_specifier_p;
9356 bool template_p = false;
9357 tree id;
9359 cp_token *token = cp_lexer_peek_token (parser->lexer);
9361 /* `typename' is not allowed in this context ([temp.res]). */
9362 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
9364 error_at (token->location,
9365 "keyword %<typename%> not allowed in this context (a qualified "
9366 "member initializer is implicitly a type)");
9367 cp_lexer_consume_token (parser->lexer);
9369 /* Look for the optional `::' operator. */
9370 global_scope_p
9371 = (cp_parser_global_scope_opt (parser,
9372 /*current_scope_valid_p=*/false)
9373 != NULL_TREE);
9374 /* Look for the optional nested-name-specifier. The simplest way to
9375 implement:
9377 [temp.res]
9379 The keyword `typename' is not permitted in a base-specifier or
9380 mem-initializer; in these contexts a qualified name that
9381 depends on a template-parameter is implicitly assumed to be a
9382 type name.
9384 is to assume that we have seen the `typename' keyword at this
9385 point. */
9386 nested_name_specifier_p
9387 = (cp_parser_nested_name_specifier_opt (parser,
9388 /*typename_keyword_p=*/true,
9389 /*check_dependency_p=*/true,
9390 /*type_p=*/true,
9391 /*is_declaration=*/true)
9392 != NULL_TREE);
9393 if (nested_name_specifier_p)
9394 template_p = cp_parser_optional_template_keyword (parser);
9395 /* If there is a `::' operator or a nested-name-specifier, then we
9396 are definitely looking for a class-name. */
9397 if (global_scope_p || nested_name_specifier_p)
9398 return cp_parser_class_name (parser,
9399 /*typename_keyword_p=*/true,
9400 /*template_keyword_p=*/template_p,
9401 none_type,
9402 /*check_dependency_p=*/true,
9403 /*class_head_p=*/false,
9404 /*is_declaration=*/true);
9405 /* Otherwise, we could also be looking for an ordinary identifier. */
9406 cp_parser_parse_tentatively (parser);
9407 /* Try a class-name. */
9408 id = cp_parser_class_name (parser,
9409 /*typename_keyword_p=*/true,
9410 /*template_keyword_p=*/false,
9411 none_type,
9412 /*check_dependency_p=*/true,
9413 /*class_head_p=*/false,
9414 /*is_declaration=*/true);
9415 /* If we found one, we're done. */
9416 if (cp_parser_parse_definitely (parser))
9417 return id;
9418 /* Otherwise, look for an ordinary identifier. */
9419 return cp_parser_identifier (parser);
9422 /* Overloading [gram.over] */
9424 /* Parse an operator-function-id.
9426 operator-function-id:
9427 operator operator
9429 Returns an IDENTIFIER_NODE for the operator which is a
9430 human-readable spelling of the identifier, e.g., `operator +'. */
9432 static tree
9433 cp_parser_operator_function_id (cp_parser* parser)
9435 /* Look for the `operator' keyword. */
9436 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9437 return error_mark_node;
9438 /* And then the name of the operator itself. */
9439 return cp_parser_operator (parser);
9442 /* Parse an operator.
9444 operator:
9445 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
9446 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
9447 || ++ -- , ->* -> () []
9449 GNU Extensions:
9451 operator:
9452 <? >? <?= >?=
9454 Returns an IDENTIFIER_NODE for the operator which is a
9455 human-readable spelling of the identifier, e.g., `operator +'. */
9457 static tree
9458 cp_parser_operator (cp_parser* parser)
9460 tree id = NULL_TREE;
9461 cp_token *token;
9463 /* Peek at the next token. */
9464 token = cp_lexer_peek_token (parser->lexer);
9465 /* Figure out which operator we have. */
9466 switch (token->type)
9468 case CPP_KEYWORD:
9470 enum tree_code op;
9472 /* The keyword should be either `new' or `delete'. */
9473 if (token->keyword == RID_NEW)
9474 op = NEW_EXPR;
9475 else if (token->keyword == RID_DELETE)
9476 op = DELETE_EXPR;
9477 else
9478 break;
9480 /* Consume the `new' or `delete' token. */
9481 cp_lexer_consume_token (parser->lexer);
9483 /* Peek at the next token. */
9484 token = cp_lexer_peek_token (parser->lexer);
9485 /* If it's a `[' token then this is the array variant of the
9486 operator. */
9487 if (token->type == CPP_OPEN_SQUARE)
9489 /* Consume the `[' token. */
9490 cp_lexer_consume_token (parser->lexer);
9491 /* Look for the `]' token. */
9492 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9493 id = ansi_opname (op == NEW_EXPR
9494 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
9496 /* Otherwise, we have the non-array variant. */
9497 else
9498 id = ansi_opname (op);
9500 return id;
9503 case CPP_PLUS:
9504 id = ansi_opname (PLUS_EXPR);
9505 break;
9507 case CPP_MINUS:
9508 id = ansi_opname (MINUS_EXPR);
9509 break;
9511 case CPP_MULT:
9512 id = ansi_opname (MULT_EXPR);
9513 break;
9515 case CPP_DIV:
9516 id = ansi_opname (TRUNC_DIV_EXPR);
9517 break;
9519 case CPP_MOD:
9520 id = ansi_opname (TRUNC_MOD_EXPR);
9521 break;
9523 case CPP_XOR:
9524 id = ansi_opname (BIT_XOR_EXPR);
9525 break;
9527 case CPP_AND:
9528 id = ansi_opname (BIT_AND_EXPR);
9529 break;
9531 case CPP_OR:
9532 id = ansi_opname (BIT_IOR_EXPR);
9533 break;
9535 case CPP_COMPL:
9536 id = ansi_opname (BIT_NOT_EXPR);
9537 break;
9539 case CPP_NOT:
9540 id = ansi_opname (TRUTH_NOT_EXPR);
9541 break;
9543 case CPP_EQ:
9544 id = ansi_assopname (NOP_EXPR);
9545 break;
9547 case CPP_LESS:
9548 id = ansi_opname (LT_EXPR);
9549 break;
9551 case CPP_GREATER:
9552 id = ansi_opname (GT_EXPR);
9553 break;
9555 case CPP_PLUS_EQ:
9556 id = ansi_assopname (PLUS_EXPR);
9557 break;
9559 case CPP_MINUS_EQ:
9560 id = ansi_assopname (MINUS_EXPR);
9561 break;
9563 case CPP_MULT_EQ:
9564 id = ansi_assopname (MULT_EXPR);
9565 break;
9567 case CPP_DIV_EQ:
9568 id = ansi_assopname (TRUNC_DIV_EXPR);
9569 break;
9571 case CPP_MOD_EQ:
9572 id = ansi_assopname (TRUNC_MOD_EXPR);
9573 break;
9575 case CPP_XOR_EQ:
9576 id = ansi_assopname (BIT_XOR_EXPR);
9577 break;
9579 case CPP_AND_EQ:
9580 id = ansi_assopname (BIT_AND_EXPR);
9581 break;
9583 case CPP_OR_EQ:
9584 id = ansi_assopname (BIT_IOR_EXPR);
9585 break;
9587 case CPP_LSHIFT:
9588 id = ansi_opname (LSHIFT_EXPR);
9589 break;
9591 case CPP_RSHIFT:
9592 id = ansi_opname (RSHIFT_EXPR);
9593 break;
9595 case CPP_LSHIFT_EQ:
9596 id = ansi_assopname (LSHIFT_EXPR);
9597 break;
9599 case CPP_RSHIFT_EQ:
9600 id = ansi_assopname (RSHIFT_EXPR);
9601 break;
9603 case CPP_EQ_EQ:
9604 id = ansi_opname (EQ_EXPR);
9605 break;
9607 case CPP_NOT_EQ:
9608 id = ansi_opname (NE_EXPR);
9609 break;
9611 case CPP_LESS_EQ:
9612 id = ansi_opname (LE_EXPR);
9613 break;
9615 case CPP_GREATER_EQ:
9616 id = ansi_opname (GE_EXPR);
9617 break;
9619 case CPP_AND_AND:
9620 id = ansi_opname (TRUTH_ANDIF_EXPR);
9621 break;
9623 case CPP_OR_OR:
9624 id = ansi_opname (TRUTH_ORIF_EXPR);
9625 break;
9627 case CPP_PLUS_PLUS:
9628 id = ansi_opname (POSTINCREMENT_EXPR);
9629 break;
9631 case CPP_MINUS_MINUS:
9632 id = ansi_opname (PREDECREMENT_EXPR);
9633 break;
9635 case CPP_COMMA:
9636 id = ansi_opname (COMPOUND_EXPR);
9637 break;
9639 case CPP_DEREF_STAR:
9640 id = ansi_opname (MEMBER_REF);
9641 break;
9643 case CPP_DEREF:
9644 id = ansi_opname (COMPONENT_REF);
9645 break;
9647 case CPP_OPEN_PAREN:
9648 /* Consume the `('. */
9649 cp_lexer_consume_token (parser->lexer);
9650 /* Look for the matching `)'. */
9651 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
9652 return ansi_opname (CALL_EXPR);
9654 case CPP_OPEN_SQUARE:
9655 /* Consume the `['. */
9656 cp_lexer_consume_token (parser->lexer);
9657 /* Look for the matching `]'. */
9658 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9659 return ansi_opname (ARRAY_REF);
9661 default:
9662 /* Anything else is an error. */
9663 break;
9666 /* If we have selected an identifier, we need to consume the
9667 operator token. */
9668 if (id)
9669 cp_lexer_consume_token (parser->lexer);
9670 /* Otherwise, no valid operator name was present. */
9671 else
9673 cp_parser_error (parser, "expected operator");
9674 id = error_mark_node;
9677 return id;
9680 /* Parse a template-declaration.
9682 template-declaration:
9683 export [opt] template < template-parameter-list > declaration
9685 If MEMBER_P is TRUE, this template-declaration occurs within a
9686 class-specifier.
9688 The grammar rule given by the standard isn't correct. What
9689 is really meant is:
9691 template-declaration:
9692 export [opt] template-parameter-list-seq
9693 decl-specifier-seq [opt] init-declarator [opt] ;
9694 export [opt] template-parameter-list-seq
9695 function-definition
9697 template-parameter-list-seq:
9698 template-parameter-list-seq [opt]
9699 template < template-parameter-list > */
9701 static void
9702 cp_parser_template_declaration (cp_parser* parser, bool member_p)
9704 /* Check for `export'. */
9705 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
9707 /* Consume the `export' token. */
9708 cp_lexer_consume_token (parser->lexer);
9709 /* Warn that we do not support `export'. */
9710 warning (0, "keyword %<export%> not implemented, and will be ignored");
9713 cp_parser_template_declaration_after_export (parser, member_p);
9716 /* Parse a template-parameter-list.
9718 template-parameter-list:
9719 template-parameter
9720 template-parameter-list , template-parameter
9722 Returns a TREE_LIST. Each node represents a template parameter.
9723 The nodes are connected via their TREE_CHAINs. */
9725 static tree
9726 cp_parser_template_parameter_list (cp_parser* parser)
9728 tree parameter_list = NULL_TREE;
9730 begin_template_parm_list ();
9731 while (true)
9733 tree parameter;
9734 bool is_non_type;
9735 bool is_parameter_pack;
9736 location_t parm_loc;
9738 /* Parse the template-parameter. */
9739 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
9740 parameter = cp_parser_template_parameter (parser,
9741 &is_non_type,
9742 &is_parameter_pack);
9743 /* Add it to the list. */
9744 if (parameter != error_mark_node)
9745 parameter_list = process_template_parm (parameter_list,
9746 parm_loc,
9747 parameter,
9748 is_non_type,
9749 is_parameter_pack);
9750 else
9752 tree err_parm = build_tree_list (parameter, parameter);
9753 TREE_VALUE (err_parm) = error_mark_node;
9754 parameter_list = chainon (parameter_list, err_parm);
9757 /* If the next token is not a `,', we're done. */
9758 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9759 break;
9760 /* Otherwise, consume the `,' token. */
9761 cp_lexer_consume_token (parser->lexer);
9764 return end_template_parm_list (parameter_list);
9767 /* Parse a template-parameter.
9769 template-parameter:
9770 type-parameter
9771 parameter-declaration
9773 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
9774 the parameter. The TREE_PURPOSE is the default value, if any.
9775 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
9776 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
9777 set to true iff this parameter is a parameter pack. */
9779 static tree
9780 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
9781 bool *is_parameter_pack)
9783 cp_token *token;
9784 cp_parameter_declarator *parameter_declarator;
9785 cp_declarator *id_declarator;
9786 tree parm;
9788 /* Assume it is a type parameter or a template parameter. */
9789 *is_non_type = false;
9790 /* Assume it not a parameter pack. */
9791 *is_parameter_pack = false;
9792 /* Peek at the next token. */
9793 token = cp_lexer_peek_token (parser->lexer);
9794 /* If it is `class' or `template', we have a type-parameter. */
9795 if (token->keyword == RID_TEMPLATE)
9796 return cp_parser_type_parameter (parser, is_parameter_pack);
9797 /* If it is `class' or `typename' we do not know yet whether it is a
9798 type parameter or a non-type parameter. Consider:
9800 template <typename T, typename T::X X> ...
9804 template <class C, class D*> ...
9806 Here, the first parameter is a type parameter, and the second is
9807 a non-type parameter. We can tell by looking at the token after
9808 the identifier -- if it is a `,', `=', or `>' then we have a type
9809 parameter. */
9810 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9812 /* Peek at the token after `class' or `typename'. */
9813 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9814 /* If it's an ellipsis, we have a template type parameter
9815 pack. */
9816 if (token->type == CPP_ELLIPSIS)
9817 return cp_parser_type_parameter (parser, is_parameter_pack);
9818 /* If it's an identifier, skip it. */
9819 if (token->type == CPP_NAME)
9820 token = cp_lexer_peek_nth_token (parser->lexer, 3);
9821 /* Now, see if the token looks like the end of a template
9822 parameter. */
9823 if (token->type == CPP_COMMA
9824 || token->type == CPP_EQ
9825 || token->type == CPP_GREATER)
9826 return cp_parser_type_parameter (parser, is_parameter_pack);
9829 /* Otherwise, it is a non-type parameter.
9831 [temp.param]
9833 When parsing a default template-argument for a non-type
9834 template-parameter, the first non-nested `>' is taken as the end
9835 of the template parameter-list rather than a greater-than
9836 operator. */
9837 *is_non_type = true;
9838 parameter_declarator
9839 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9840 /*parenthesized_p=*/NULL);
9842 /* If the parameter declaration is marked as a parameter pack, set
9843 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9844 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9845 grokdeclarator. */
9846 if (parameter_declarator
9847 && parameter_declarator->declarator
9848 && parameter_declarator->declarator->parameter_pack_p)
9850 *is_parameter_pack = true;
9851 parameter_declarator->declarator->parameter_pack_p = false;
9854 /* If the next token is an ellipsis, and we don't already have it
9855 marked as a parameter pack, then we have a parameter pack (that
9856 has no declarator). */
9857 if (!*is_parameter_pack
9858 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
9859 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
9861 /* Consume the `...'. */
9862 cp_lexer_consume_token (parser->lexer);
9863 maybe_warn_variadic_templates ();
9865 *is_parameter_pack = true;
9867 /* We might end up with a pack expansion as the type of the non-type
9868 template parameter, in which case this is a non-type template
9869 parameter pack. */
9870 else if (parameter_declarator
9871 && parameter_declarator->decl_specifiers.type
9872 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
9874 *is_parameter_pack = true;
9875 parameter_declarator->decl_specifiers.type =
9876 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
9879 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9881 /* Parameter packs cannot have default arguments. However, a
9882 user may try to do so, so we'll parse them and give an
9883 appropriate diagnostic here. */
9885 /* Consume the `='. */
9886 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
9887 cp_lexer_consume_token (parser->lexer);
9889 /* Find the name of the parameter pack. */
9890 id_declarator = parameter_declarator->declarator;
9891 while (id_declarator && id_declarator->kind != cdk_id)
9892 id_declarator = id_declarator->declarator;
9894 if (id_declarator && id_declarator->kind == cdk_id)
9895 error_at (start_token->location,
9896 "template parameter pack %qD cannot have a default argument",
9897 id_declarator->u.id.unqualified_name);
9898 else
9899 error_at (start_token->location,
9900 "template parameter pack cannot have a default argument");
9902 /* Parse the default argument, but throw away the result. */
9903 cp_parser_default_argument (parser, /*template_parm_p=*/true);
9906 parm = grokdeclarator (parameter_declarator->declarator,
9907 &parameter_declarator->decl_specifiers,
9908 PARM, /*initialized=*/0,
9909 /*attrlist=*/NULL);
9910 if (parm == error_mark_node)
9911 return error_mark_node;
9913 return build_tree_list (parameter_declarator->default_argument, parm);
9916 /* Parse a type-parameter.
9918 type-parameter:
9919 class identifier [opt]
9920 class identifier [opt] = type-id
9921 typename identifier [opt]
9922 typename identifier [opt] = type-id
9923 template < template-parameter-list > class identifier [opt]
9924 template < template-parameter-list > class identifier [opt]
9925 = id-expression
9927 GNU Extension (variadic templates):
9929 type-parameter:
9930 class ... identifier [opt]
9931 typename ... identifier [opt]
9933 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
9934 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
9935 the declaration of the parameter.
9937 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9939 static tree
9940 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
9942 cp_token *token;
9943 tree parameter;
9945 /* Look for a keyword to tell us what kind of parameter this is. */
9946 token = cp_parser_require (parser, CPP_KEYWORD,
9947 "%<class%>, %<typename%>, or %<template%>");
9948 if (!token)
9949 return error_mark_node;
9951 switch (token->keyword)
9953 case RID_CLASS:
9954 case RID_TYPENAME:
9956 tree identifier;
9957 tree default_argument;
9959 /* If the next token is an ellipsis, we have a template
9960 argument pack. */
9961 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9963 /* Consume the `...' token. */
9964 cp_lexer_consume_token (parser->lexer);
9965 maybe_warn_variadic_templates ();
9967 *is_parameter_pack = true;
9970 /* If the next token is an identifier, then it names the
9971 parameter. */
9972 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9973 identifier = cp_parser_identifier (parser);
9974 else
9975 identifier = NULL_TREE;
9977 /* Create the parameter. */
9978 parameter = finish_template_type_parm (class_type_node, identifier);
9980 /* If the next token is an `=', we have a default argument. */
9981 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9983 /* Consume the `=' token. */
9984 cp_lexer_consume_token (parser->lexer);
9985 /* Parse the default-argument. */
9986 push_deferring_access_checks (dk_no_deferred);
9987 default_argument = cp_parser_type_id (parser);
9989 /* Template parameter packs cannot have default
9990 arguments. */
9991 if (*is_parameter_pack)
9993 if (identifier)
9994 error_at (token->location,
9995 "template parameter pack %qD cannot have a "
9996 "default argument", identifier);
9997 else
9998 error_at (token->location,
9999 "template parameter packs cannot have "
10000 "default arguments");
10001 default_argument = NULL_TREE;
10003 pop_deferring_access_checks ();
10005 else
10006 default_argument = NULL_TREE;
10008 /* Create the combined representation of the parameter and the
10009 default argument. */
10010 parameter = build_tree_list (default_argument, parameter);
10012 break;
10014 case RID_TEMPLATE:
10016 tree parameter_list;
10017 tree identifier;
10018 tree default_argument;
10020 /* Look for the `<'. */
10021 cp_parser_require (parser, CPP_LESS, "%<<%>");
10022 /* Parse the template-parameter-list. */
10023 parameter_list = cp_parser_template_parameter_list (parser);
10024 /* Look for the `>'. */
10025 cp_parser_require (parser, CPP_GREATER, "%<>%>");
10026 /* Look for the `class' keyword. */
10027 cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
10028 /* If the next token is an ellipsis, we have a template
10029 argument pack. */
10030 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10032 /* Consume the `...' token. */
10033 cp_lexer_consume_token (parser->lexer);
10034 maybe_warn_variadic_templates ();
10036 *is_parameter_pack = true;
10038 /* If the next token is an `=', then there is a
10039 default-argument. If the next token is a `>', we are at
10040 the end of the parameter-list. If the next token is a `,',
10041 then we are at the end of this parameter. */
10042 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10043 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
10044 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10046 identifier = cp_parser_identifier (parser);
10047 /* Treat invalid names as if the parameter were nameless. */
10048 if (identifier == error_mark_node)
10049 identifier = NULL_TREE;
10051 else
10052 identifier = NULL_TREE;
10054 /* Create the template parameter. */
10055 parameter = finish_template_template_parm (class_type_node,
10056 identifier);
10058 /* If the next token is an `=', then there is a
10059 default-argument. */
10060 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10062 bool is_template;
10064 /* Consume the `='. */
10065 cp_lexer_consume_token (parser->lexer);
10066 /* Parse the id-expression. */
10067 push_deferring_access_checks (dk_no_deferred);
10068 /* save token before parsing the id-expression, for error
10069 reporting */
10070 token = cp_lexer_peek_token (parser->lexer);
10071 default_argument
10072 = cp_parser_id_expression (parser,
10073 /*template_keyword_p=*/false,
10074 /*check_dependency_p=*/true,
10075 /*template_p=*/&is_template,
10076 /*declarator_p=*/false,
10077 /*optional_p=*/false);
10078 if (TREE_CODE (default_argument) == TYPE_DECL)
10079 /* If the id-expression was a template-id that refers to
10080 a template-class, we already have the declaration here,
10081 so no further lookup is needed. */
10083 else
10084 /* Look up the name. */
10085 default_argument
10086 = cp_parser_lookup_name (parser, default_argument,
10087 none_type,
10088 /*is_template=*/is_template,
10089 /*is_namespace=*/false,
10090 /*check_dependency=*/true,
10091 /*ambiguous_decls=*/NULL,
10092 token->location);
10093 /* See if the default argument is valid. */
10094 default_argument
10095 = check_template_template_default_arg (default_argument);
10097 /* Template parameter packs cannot have default
10098 arguments. */
10099 if (*is_parameter_pack)
10101 if (identifier)
10102 error_at (token->location,
10103 "template parameter pack %qD cannot "
10104 "have a default argument",
10105 identifier);
10106 else
10107 error_at (token->location, "template parameter packs cannot "
10108 "have default arguments");
10109 default_argument = NULL_TREE;
10111 pop_deferring_access_checks ();
10113 else
10114 default_argument = NULL_TREE;
10116 /* Create the combined representation of the parameter and the
10117 default argument. */
10118 parameter = build_tree_list (default_argument, parameter);
10120 break;
10122 default:
10123 gcc_unreachable ();
10124 break;
10127 return parameter;
10130 /* Parse a template-id.
10132 template-id:
10133 template-name < template-argument-list [opt] >
10135 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
10136 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
10137 returned. Otherwise, if the template-name names a function, or set
10138 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
10139 names a class, returns a TYPE_DECL for the specialization.
10141 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
10142 uninstantiated templates. */
10144 static tree
10145 cp_parser_template_id (cp_parser *parser,
10146 bool template_keyword_p,
10147 bool check_dependency_p,
10148 bool is_declaration)
10150 int i;
10151 tree templ;
10152 tree arguments;
10153 tree template_id;
10154 cp_token_position start_of_id = 0;
10155 deferred_access_check *chk;
10156 VEC (deferred_access_check,gc) *access_check;
10157 cp_token *next_token = NULL, *next_token_2 = NULL, *token = NULL;
10158 bool is_identifier;
10160 /* If the next token corresponds to a template-id, there is no need
10161 to reparse it. */
10162 next_token = cp_lexer_peek_token (parser->lexer);
10163 if (next_token->type == CPP_TEMPLATE_ID)
10165 struct tree_check *check_value;
10167 /* Get the stored value. */
10168 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
10169 /* Perform any access checks that were deferred. */
10170 access_check = check_value->checks;
10171 if (access_check)
10173 for (i = 0 ;
10174 VEC_iterate (deferred_access_check, access_check, i, chk) ;
10175 ++i)
10177 perform_or_defer_access_check (chk->binfo,
10178 chk->decl,
10179 chk->diag_decl);
10182 /* Return the stored value. */
10183 return check_value->value;
10186 /* Avoid performing name lookup if there is no possibility of
10187 finding a template-id. */
10188 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
10189 || (next_token->type == CPP_NAME
10190 && !cp_parser_nth_token_starts_template_argument_list_p
10191 (parser, 2)))
10193 cp_parser_error (parser, "expected template-id");
10194 return error_mark_node;
10197 /* Remember where the template-id starts. */
10198 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
10199 start_of_id = cp_lexer_token_position (parser->lexer, false);
10201 push_deferring_access_checks (dk_deferred);
10203 /* Parse the template-name. */
10204 is_identifier = false;
10205 token = cp_lexer_peek_token (parser->lexer);
10206 templ = cp_parser_template_name (parser, template_keyword_p,
10207 check_dependency_p,
10208 is_declaration,
10209 &is_identifier);
10210 if (templ == error_mark_node || is_identifier)
10212 pop_deferring_access_checks ();
10213 return templ;
10216 /* If we find the sequence `[:' after a template-name, it's probably
10217 a digraph-typo for `< ::'. Substitute the tokens and check if we can
10218 parse correctly the argument list. */
10219 next_token = cp_lexer_peek_token (parser->lexer);
10220 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10221 if (next_token->type == CPP_OPEN_SQUARE
10222 && next_token->flags & DIGRAPH
10223 && next_token_2->type == CPP_COLON
10224 && !(next_token_2->flags & PREV_WHITE))
10226 cp_parser_parse_tentatively (parser);
10227 /* Change `:' into `::'. */
10228 next_token_2->type = CPP_SCOPE;
10229 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
10230 CPP_LESS. */
10231 cp_lexer_consume_token (parser->lexer);
10233 /* Parse the arguments. */
10234 arguments = cp_parser_enclosed_template_argument_list (parser);
10235 if (!cp_parser_parse_definitely (parser))
10237 /* If we couldn't parse an argument list, then we revert our changes
10238 and return simply an error. Maybe this is not a template-id
10239 after all. */
10240 next_token_2->type = CPP_COLON;
10241 cp_parser_error (parser, "expected %<<%>");
10242 pop_deferring_access_checks ();
10243 return error_mark_node;
10245 /* Otherwise, emit an error about the invalid digraph, but continue
10246 parsing because we got our argument list. */
10247 if (permerror (next_token->location,
10248 "%<<::%> cannot begin a template-argument list"))
10250 static bool hint = false;
10251 inform (next_token->location,
10252 "%<<:%> is an alternate spelling for %<[%>."
10253 " Insert whitespace between %<<%> and %<::%>");
10254 if (!hint && !flag_permissive)
10256 inform (next_token->location, "(if you use %<-fpermissive%>"
10257 " G++ will accept your code)");
10258 hint = true;
10262 else
10264 /* Look for the `<' that starts the template-argument-list. */
10265 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10267 pop_deferring_access_checks ();
10268 return error_mark_node;
10270 /* Parse the arguments. */
10271 arguments = cp_parser_enclosed_template_argument_list (parser);
10274 /* Build a representation of the specialization. */
10275 if (TREE_CODE (templ) == IDENTIFIER_NODE)
10276 template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10277 else if (DECL_CLASS_TEMPLATE_P (templ)
10278 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10280 bool entering_scope;
10281 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10282 template (rather than some instantiation thereof) only if
10283 is not nested within some other construct. For example, in
10284 "template <typename T> void f(T) { A<T>::", A<T> is just an
10285 instantiation of A. */
10286 entering_scope = (template_parm_scope_p ()
10287 && cp_lexer_next_token_is (parser->lexer,
10288 CPP_SCOPE));
10289 template_id
10290 = finish_template_type (templ, arguments, entering_scope);
10292 else
10294 /* If it's not a class-template or a template-template, it should be
10295 a function-template. */
10296 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10297 || TREE_CODE (templ) == OVERLOAD
10298 || BASELINK_P (templ)));
10300 template_id = lookup_template_function (templ, arguments);
10303 /* If parsing tentatively, replace the sequence of tokens that makes
10304 up the template-id with a CPP_TEMPLATE_ID token. That way,
10305 should we re-parse the token stream, we will not have to repeat
10306 the effort required to do the parse, nor will we issue duplicate
10307 error messages about problems during instantiation of the
10308 template. */
10309 if (start_of_id)
10311 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10313 /* Reset the contents of the START_OF_ID token. */
10314 token->type = CPP_TEMPLATE_ID;
10315 /* Retrieve any deferred checks. Do not pop this access checks yet
10316 so the memory will not be reclaimed during token replacing below. */
10317 token->u.tree_check_value = GGC_CNEW (struct tree_check);
10318 token->u.tree_check_value->value = template_id;
10319 token->u.tree_check_value->checks = get_deferred_access_checks ();
10320 token->keyword = RID_MAX;
10322 /* Purge all subsequent tokens. */
10323 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10325 /* ??? Can we actually assume that, if template_id ==
10326 error_mark_node, we will have issued a diagnostic to the
10327 user, as opposed to simply marking the tentative parse as
10328 failed? */
10329 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10330 error_at (token->location, "parse error in template argument list");
10333 pop_deferring_access_checks ();
10334 return template_id;
10337 /* Parse a template-name.
10339 template-name:
10340 identifier
10342 The standard should actually say:
10344 template-name:
10345 identifier
10346 operator-function-id
10348 A defect report has been filed about this issue.
10350 A conversion-function-id cannot be a template name because they cannot
10351 be part of a template-id. In fact, looking at this code:
10353 a.operator K<int>()
10355 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10356 It is impossible to call a templated conversion-function-id with an
10357 explicit argument list, since the only allowed template parameter is
10358 the type to which it is converting.
10360 If TEMPLATE_KEYWORD_P is true, then we have just seen the
10361 `template' keyword, in a construction like:
10363 T::template f<3>()
10365 In that case `f' is taken to be a template-name, even though there
10366 is no way of knowing for sure.
10368 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
10369 name refers to a set of overloaded functions, at least one of which
10370 is a template, or an IDENTIFIER_NODE with the name of the template,
10371 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
10372 names are looked up inside uninstantiated templates. */
10374 static tree
10375 cp_parser_template_name (cp_parser* parser,
10376 bool template_keyword_p,
10377 bool check_dependency_p,
10378 bool is_declaration,
10379 bool *is_identifier)
10381 tree identifier;
10382 tree decl;
10383 tree fns;
10384 cp_token *token = cp_lexer_peek_token (parser->lexer);
10386 /* If the next token is `operator', then we have either an
10387 operator-function-id or a conversion-function-id. */
10388 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
10390 /* We don't know whether we're looking at an
10391 operator-function-id or a conversion-function-id. */
10392 cp_parser_parse_tentatively (parser);
10393 /* Try an operator-function-id. */
10394 identifier = cp_parser_operator_function_id (parser);
10395 /* If that didn't work, try a conversion-function-id. */
10396 if (!cp_parser_parse_definitely (parser))
10398 cp_parser_error (parser, "expected template-name");
10399 return error_mark_node;
10402 /* Look for the identifier. */
10403 else
10404 identifier = cp_parser_identifier (parser);
10406 /* If we didn't find an identifier, we don't have a template-id. */
10407 if (identifier == error_mark_node)
10408 return error_mark_node;
10410 /* If the name immediately followed the `template' keyword, then it
10411 is a template-name. However, if the next token is not `<', then
10412 we do not treat it as a template-name, since it is not being used
10413 as part of a template-id. This enables us to handle constructs
10414 like:
10416 template <typename T> struct S { S(); };
10417 template <typename T> S<T>::S();
10419 correctly. We would treat `S' as a template -- if it were `S<T>'
10420 -- but we do not if there is no `<'. */
10422 if (processing_template_decl
10423 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
10425 /* In a declaration, in a dependent context, we pretend that the
10426 "template" keyword was present in order to improve error
10427 recovery. For example, given:
10429 template <typename T> void f(T::X<int>);
10431 we want to treat "X<int>" as a template-id. */
10432 if (is_declaration
10433 && !template_keyword_p
10434 && parser->scope && TYPE_P (parser->scope)
10435 && check_dependency_p
10436 && dependent_scope_p (parser->scope)
10437 /* Do not do this for dtors (or ctors), since they never
10438 need the template keyword before their name. */
10439 && !constructor_name_p (identifier, parser->scope))
10441 cp_token_position start = 0;
10443 /* Explain what went wrong. */
10444 error_at (token->location, "non-template %qD used as template",
10445 identifier);
10446 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
10447 parser->scope, identifier);
10448 /* If parsing tentatively, find the location of the "<" token. */
10449 if (cp_parser_simulate_error (parser))
10450 start = cp_lexer_token_position (parser->lexer, true);
10451 /* Parse the template arguments so that we can issue error
10452 messages about them. */
10453 cp_lexer_consume_token (parser->lexer);
10454 cp_parser_enclosed_template_argument_list (parser);
10455 /* Skip tokens until we find a good place from which to
10456 continue parsing. */
10457 cp_parser_skip_to_closing_parenthesis (parser,
10458 /*recovering=*/true,
10459 /*or_comma=*/true,
10460 /*consume_paren=*/false);
10461 /* If parsing tentatively, permanently remove the
10462 template argument list. That will prevent duplicate
10463 error messages from being issued about the missing
10464 "template" keyword. */
10465 if (start)
10466 cp_lexer_purge_tokens_after (parser->lexer, start);
10467 if (is_identifier)
10468 *is_identifier = true;
10469 return identifier;
10472 /* If the "template" keyword is present, then there is generally
10473 no point in doing name-lookup, so we just return IDENTIFIER.
10474 But, if the qualifying scope is non-dependent then we can
10475 (and must) do name-lookup normally. */
10476 if (template_keyword_p
10477 && (!parser->scope
10478 || (TYPE_P (parser->scope)
10479 && dependent_type_p (parser->scope))))
10480 return identifier;
10483 /* Look up the name. */
10484 decl = cp_parser_lookup_name (parser, identifier,
10485 none_type,
10486 /*is_template=*/false,
10487 /*is_namespace=*/false,
10488 check_dependency_p,
10489 /*ambiguous_decls=*/NULL,
10490 token->location);
10491 decl = maybe_get_template_decl_from_type_decl (decl);
10493 /* If DECL is a template, then the name was a template-name. */
10494 if (TREE_CODE (decl) == TEMPLATE_DECL)
10496 else
10498 tree fn = NULL_TREE;
10500 /* The standard does not explicitly indicate whether a name that
10501 names a set of overloaded declarations, some of which are
10502 templates, is a template-name. However, such a name should
10503 be a template-name; otherwise, there is no way to form a
10504 template-id for the overloaded templates. */
10505 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
10506 if (TREE_CODE (fns) == OVERLOAD)
10507 for (fn = fns; fn; fn = OVL_NEXT (fn))
10508 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
10509 break;
10511 if (!fn)
10513 /* The name does not name a template. */
10514 cp_parser_error (parser, "expected template-name");
10515 return error_mark_node;
10519 /* If DECL is dependent, and refers to a function, then just return
10520 its name; we will look it up again during template instantiation. */
10521 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
10523 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
10524 if (TYPE_P (scope) && dependent_type_p (scope))
10525 return identifier;
10528 return decl;
10531 /* Parse a template-argument-list.
10533 template-argument-list:
10534 template-argument ... [opt]
10535 template-argument-list , template-argument ... [opt]
10537 Returns a TREE_VEC containing the arguments. */
10539 static tree
10540 cp_parser_template_argument_list (cp_parser* parser)
10542 tree fixed_args[10];
10543 unsigned n_args = 0;
10544 unsigned alloced = 10;
10545 tree *arg_ary = fixed_args;
10546 tree vec;
10547 bool saved_in_template_argument_list_p;
10548 bool saved_ice_p;
10549 bool saved_non_ice_p;
10551 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
10552 parser->in_template_argument_list_p = true;
10553 /* Even if the template-id appears in an integral
10554 constant-expression, the contents of the argument list do
10555 not. */
10556 saved_ice_p = parser->integral_constant_expression_p;
10557 parser->integral_constant_expression_p = false;
10558 saved_non_ice_p = parser->non_integral_constant_expression_p;
10559 parser->non_integral_constant_expression_p = false;
10560 /* Parse the arguments. */
10563 tree argument;
10565 if (n_args)
10566 /* Consume the comma. */
10567 cp_lexer_consume_token (parser->lexer);
10569 /* Parse the template-argument. */
10570 argument = cp_parser_template_argument (parser);
10572 /* If the next token is an ellipsis, we're expanding a template
10573 argument pack. */
10574 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10576 if (argument == error_mark_node)
10578 cp_token *token = cp_lexer_peek_token (parser->lexer);
10579 error_at (token->location,
10580 "expected parameter pack before %<...%>");
10582 /* Consume the `...' token. */
10583 cp_lexer_consume_token (parser->lexer);
10585 /* Make the argument into a TYPE_PACK_EXPANSION or
10586 EXPR_PACK_EXPANSION. */
10587 argument = make_pack_expansion (argument);
10590 if (n_args == alloced)
10592 alloced *= 2;
10594 if (arg_ary == fixed_args)
10596 arg_ary = XNEWVEC (tree, alloced);
10597 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
10599 else
10600 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
10602 arg_ary[n_args++] = argument;
10604 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
10606 vec = make_tree_vec (n_args);
10608 while (n_args--)
10609 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
10611 if (arg_ary != fixed_args)
10612 free (arg_ary);
10613 parser->non_integral_constant_expression_p = saved_non_ice_p;
10614 parser->integral_constant_expression_p = saved_ice_p;
10615 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
10616 return vec;
10619 /* Parse a template-argument.
10621 template-argument:
10622 assignment-expression
10623 type-id
10624 id-expression
10626 The representation is that of an assignment-expression, type-id, or
10627 id-expression -- except that the qualified id-expression is
10628 evaluated, so that the value returned is either a DECL or an
10629 OVERLOAD.
10631 Although the standard says "assignment-expression", it forbids
10632 throw-expressions or assignments in the template argument.
10633 Therefore, we use "conditional-expression" instead. */
10635 static tree
10636 cp_parser_template_argument (cp_parser* parser)
10638 tree argument;
10639 bool template_p;
10640 bool address_p;
10641 bool maybe_type_id = false;
10642 cp_token *token = NULL, *argument_start_token = NULL;
10643 cp_id_kind idk;
10645 /* There's really no way to know what we're looking at, so we just
10646 try each alternative in order.
10648 [temp.arg]
10650 In a template-argument, an ambiguity between a type-id and an
10651 expression is resolved to a type-id, regardless of the form of
10652 the corresponding template-parameter.
10654 Therefore, we try a type-id first. */
10655 cp_parser_parse_tentatively (parser);
10656 argument = cp_parser_template_type_arg (parser);
10657 /* If there was no error parsing the type-id but the next token is a
10658 '>>', our behavior depends on which dialect of C++ we're
10659 parsing. In C++98, we probably found a typo for '> >'. But there
10660 are type-id which are also valid expressions. For instance:
10662 struct X { int operator >> (int); };
10663 template <int V> struct Foo {};
10664 Foo<X () >> 5> r;
10666 Here 'X()' is a valid type-id of a function type, but the user just
10667 wanted to write the expression "X() >> 5". Thus, we remember that we
10668 found a valid type-id, but we still try to parse the argument as an
10669 expression to see what happens.
10671 In C++0x, the '>>' will be considered two separate '>'
10672 tokens. */
10673 if (!cp_parser_error_occurred (parser)
10674 && cxx_dialect == cxx98
10675 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
10677 maybe_type_id = true;
10678 cp_parser_abort_tentative_parse (parser);
10680 else
10682 /* If the next token isn't a `,' or a `>', then this argument wasn't
10683 really finished. This means that the argument is not a valid
10684 type-id. */
10685 if (!cp_parser_next_token_ends_template_argument_p (parser))
10686 cp_parser_error (parser, "expected template-argument");
10687 /* If that worked, we're done. */
10688 if (cp_parser_parse_definitely (parser))
10689 return argument;
10691 /* We're still not sure what the argument will be. */
10692 cp_parser_parse_tentatively (parser);
10693 /* Try a template. */
10694 argument_start_token = cp_lexer_peek_token (parser->lexer);
10695 argument = cp_parser_id_expression (parser,
10696 /*template_keyword_p=*/false,
10697 /*check_dependency_p=*/true,
10698 &template_p,
10699 /*declarator_p=*/false,
10700 /*optional_p=*/false);
10701 /* If the next token isn't a `,' or a `>', then this argument wasn't
10702 really finished. */
10703 if (!cp_parser_next_token_ends_template_argument_p (parser))
10704 cp_parser_error (parser, "expected template-argument");
10705 if (!cp_parser_error_occurred (parser))
10707 /* Figure out what is being referred to. If the id-expression
10708 was for a class template specialization, then we will have a
10709 TYPE_DECL at this point. There is no need to do name lookup
10710 at this point in that case. */
10711 if (TREE_CODE (argument) != TYPE_DECL)
10712 argument = cp_parser_lookup_name (parser, argument,
10713 none_type,
10714 /*is_template=*/template_p,
10715 /*is_namespace=*/false,
10716 /*check_dependency=*/true,
10717 /*ambiguous_decls=*/NULL,
10718 argument_start_token->location);
10719 if (TREE_CODE (argument) != TEMPLATE_DECL
10720 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
10721 cp_parser_error (parser, "expected template-name");
10723 if (cp_parser_parse_definitely (parser))
10724 return argument;
10725 /* It must be a non-type argument. There permitted cases are given
10726 in [temp.arg.nontype]:
10728 -- an integral constant-expression of integral or enumeration
10729 type; or
10731 -- the name of a non-type template-parameter; or
10733 -- the name of an object or function with external linkage...
10735 -- the address of an object or function with external linkage...
10737 -- a pointer to member... */
10738 /* Look for a non-type template parameter. */
10739 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10741 cp_parser_parse_tentatively (parser);
10742 argument = cp_parser_primary_expression (parser,
10743 /*address_p=*/false,
10744 /*cast_p=*/false,
10745 /*template_arg_p=*/true,
10746 &idk);
10747 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
10748 || !cp_parser_next_token_ends_template_argument_p (parser))
10749 cp_parser_simulate_error (parser);
10750 if (cp_parser_parse_definitely (parser))
10751 return argument;
10754 /* If the next token is "&", the argument must be the address of an
10755 object or function with external linkage. */
10756 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
10757 if (address_p)
10758 cp_lexer_consume_token (parser->lexer);
10759 /* See if we might have an id-expression. */
10760 token = cp_lexer_peek_token (parser->lexer);
10761 if (token->type == CPP_NAME
10762 || token->keyword == RID_OPERATOR
10763 || token->type == CPP_SCOPE
10764 || token->type == CPP_TEMPLATE_ID
10765 || token->type == CPP_NESTED_NAME_SPECIFIER)
10767 cp_parser_parse_tentatively (parser);
10768 argument = cp_parser_primary_expression (parser,
10769 address_p,
10770 /*cast_p=*/false,
10771 /*template_arg_p=*/true,
10772 &idk);
10773 if (cp_parser_error_occurred (parser)
10774 || !cp_parser_next_token_ends_template_argument_p (parser))
10775 cp_parser_abort_tentative_parse (parser);
10776 else
10778 if (TREE_CODE (argument) == INDIRECT_REF)
10780 gcc_assert (REFERENCE_REF_P (argument));
10781 argument = TREE_OPERAND (argument, 0);
10784 if (TREE_CODE (argument) == VAR_DECL)
10786 /* A variable without external linkage might still be a
10787 valid constant-expression, so no error is issued here
10788 if the external-linkage check fails. */
10789 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
10790 cp_parser_simulate_error (parser);
10792 else if (is_overloaded_fn (argument))
10793 /* All overloaded functions are allowed; if the external
10794 linkage test does not pass, an error will be issued
10795 later. */
10797 else if (address_p
10798 && (TREE_CODE (argument) == OFFSET_REF
10799 || TREE_CODE (argument) == SCOPE_REF))
10800 /* A pointer-to-member. */
10802 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
10804 else
10805 cp_parser_simulate_error (parser);
10807 if (cp_parser_parse_definitely (parser))
10809 if (address_p)
10810 argument = build_x_unary_op (ADDR_EXPR, argument,
10811 tf_warning_or_error);
10812 return argument;
10816 /* If the argument started with "&", there are no other valid
10817 alternatives at this point. */
10818 if (address_p)
10820 cp_parser_error (parser, "invalid non-type template argument");
10821 return error_mark_node;
10824 /* If the argument wasn't successfully parsed as a type-id followed
10825 by '>>', the argument can only be a constant expression now.
10826 Otherwise, we try parsing the constant-expression tentatively,
10827 because the argument could really be a type-id. */
10828 if (maybe_type_id)
10829 cp_parser_parse_tentatively (parser);
10830 argument = cp_parser_constant_expression (parser,
10831 /*allow_non_constant_p=*/false,
10832 /*non_constant_p=*/NULL);
10833 argument = fold_non_dependent_expr (argument);
10834 if (!maybe_type_id)
10835 return argument;
10836 if (!cp_parser_next_token_ends_template_argument_p (parser))
10837 cp_parser_error (parser, "expected template-argument");
10838 if (cp_parser_parse_definitely (parser))
10839 return argument;
10840 /* We did our best to parse the argument as a non type-id, but that
10841 was the only alternative that matched (albeit with a '>' after
10842 it). We can assume it's just a typo from the user, and a
10843 diagnostic will then be issued. */
10844 return cp_parser_template_type_arg (parser);
10847 /* Parse an explicit-instantiation.
10849 explicit-instantiation:
10850 template declaration
10852 Although the standard says `declaration', what it really means is:
10854 explicit-instantiation:
10855 template decl-specifier-seq [opt] declarator [opt] ;
10857 Things like `template int S<int>::i = 5, int S<double>::j;' are not
10858 supposed to be allowed. A defect report has been filed about this
10859 issue.
10861 GNU Extension:
10863 explicit-instantiation:
10864 storage-class-specifier template
10865 decl-specifier-seq [opt] declarator [opt] ;
10866 function-specifier template
10867 decl-specifier-seq [opt] declarator [opt] ; */
10869 static void
10870 cp_parser_explicit_instantiation (cp_parser* parser)
10872 int declares_class_or_enum;
10873 cp_decl_specifier_seq decl_specifiers;
10874 tree extension_specifier = NULL_TREE;
10875 cp_token *token;
10877 /* Look for an (optional) storage-class-specifier or
10878 function-specifier. */
10879 if (cp_parser_allow_gnu_extensions_p (parser))
10881 extension_specifier
10882 = cp_parser_storage_class_specifier_opt (parser);
10883 if (!extension_specifier)
10884 extension_specifier
10885 = cp_parser_function_specifier_opt (parser,
10886 /*decl_specs=*/NULL);
10889 /* Look for the `template' keyword. */
10890 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10891 /* Let the front end know that we are processing an explicit
10892 instantiation. */
10893 begin_explicit_instantiation ();
10894 /* [temp.explicit] says that we are supposed to ignore access
10895 control while processing explicit instantiation directives. */
10896 push_deferring_access_checks (dk_no_check);
10897 /* Parse a decl-specifier-seq. */
10898 token = cp_lexer_peek_token (parser->lexer);
10899 cp_parser_decl_specifier_seq (parser,
10900 CP_PARSER_FLAGS_OPTIONAL,
10901 &decl_specifiers,
10902 &declares_class_or_enum);
10903 /* If there was exactly one decl-specifier, and it declared a class,
10904 and there's no declarator, then we have an explicit type
10905 instantiation. */
10906 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
10908 tree type;
10910 type = check_tag_decl (&decl_specifiers);
10911 /* Turn access control back on for names used during
10912 template instantiation. */
10913 pop_deferring_access_checks ();
10914 if (type)
10915 do_type_instantiation (type, extension_specifier,
10916 /*complain=*/tf_error);
10918 else
10920 cp_declarator *declarator;
10921 tree decl;
10923 /* Parse the declarator. */
10924 declarator
10925 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10926 /*ctor_dtor_or_conv_p=*/NULL,
10927 /*parenthesized_p=*/NULL,
10928 /*member_p=*/false);
10929 if (declares_class_or_enum & 2)
10930 cp_parser_check_for_definition_in_return_type (declarator,
10931 decl_specifiers.type,
10932 decl_specifiers.type_location);
10933 if (declarator != cp_error_declarator)
10935 decl = grokdeclarator (declarator, &decl_specifiers,
10936 NORMAL, 0, &decl_specifiers.attributes);
10937 /* Turn access control back on for names used during
10938 template instantiation. */
10939 pop_deferring_access_checks ();
10940 /* Do the explicit instantiation. */
10941 do_decl_instantiation (decl, extension_specifier);
10943 else
10945 pop_deferring_access_checks ();
10946 /* Skip the body of the explicit instantiation. */
10947 cp_parser_skip_to_end_of_statement (parser);
10950 /* We're done with the instantiation. */
10951 end_explicit_instantiation ();
10953 cp_parser_consume_semicolon_at_end_of_statement (parser);
10956 /* Parse an explicit-specialization.
10958 explicit-specialization:
10959 template < > declaration
10961 Although the standard says `declaration', what it really means is:
10963 explicit-specialization:
10964 template <> decl-specifier [opt] init-declarator [opt] ;
10965 template <> function-definition
10966 template <> explicit-specialization
10967 template <> template-declaration */
10969 static void
10970 cp_parser_explicit_specialization (cp_parser* parser)
10972 bool need_lang_pop;
10973 cp_token *token = cp_lexer_peek_token (parser->lexer);
10975 /* Look for the `template' keyword. */
10976 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10977 /* Look for the `<'. */
10978 cp_parser_require (parser, CPP_LESS, "%<<%>");
10979 /* Look for the `>'. */
10980 cp_parser_require (parser, CPP_GREATER, "%<>%>");
10981 /* We have processed another parameter list. */
10982 ++parser->num_template_parameter_lists;
10983 /* [temp]
10985 A template ... explicit specialization ... shall not have C
10986 linkage. */
10987 if (current_lang_name == lang_name_c)
10989 error_at (token->location, "template specialization with C linkage");
10990 /* Give it C++ linkage to avoid confusing other parts of the
10991 front end. */
10992 push_lang_context (lang_name_cplusplus);
10993 need_lang_pop = true;
10995 else
10996 need_lang_pop = false;
10997 /* Let the front end know that we are beginning a specialization. */
10998 if (!begin_specialization ())
11000 end_specialization ();
11001 return;
11004 /* If the next keyword is `template', we need to figure out whether
11005 or not we're looking a template-declaration. */
11006 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
11008 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
11009 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
11010 cp_parser_template_declaration_after_export (parser,
11011 /*member_p=*/false);
11012 else
11013 cp_parser_explicit_specialization (parser);
11015 else
11016 /* Parse the dependent declaration. */
11017 cp_parser_single_declaration (parser,
11018 /*checks=*/NULL,
11019 /*member_p=*/false,
11020 /*explicit_specialization_p=*/true,
11021 /*friend_p=*/NULL);
11022 /* We're done with the specialization. */
11023 end_specialization ();
11024 /* For the erroneous case of a template with C linkage, we pushed an
11025 implicit C++ linkage scope; exit that scope now. */
11026 if (need_lang_pop)
11027 pop_lang_context ();
11028 /* We're done with this parameter list. */
11029 --parser->num_template_parameter_lists;
11032 /* Parse a type-specifier.
11034 type-specifier:
11035 simple-type-specifier
11036 class-specifier
11037 enum-specifier
11038 elaborated-type-specifier
11039 cv-qualifier
11041 GNU Extension:
11043 type-specifier:
11044 __complex__
11046 Returns a representation of the type-specifier. For a
11047 class-specifier, enum-specifier, or elaborated-type-specifier, a
11048 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
11050 The parser flags FLAGS is used to control type-specifier parsing.
11052 If IS_DECLARATION is TRUE, then this type-specifier is appearing
11053 in a decl-specifier-seq.
11055 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
11056 class-specifier, enum-specifier, or elaborated-type-specifier, then
11057 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
11058 if a type is declared; 2 if it is defined. Otherwise, it is set to
11059 zero.
11061 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
11062 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
11063 is set to FALSE. */
11065 static tree
11066 cp_parser_type_specifier (cp_parser* parser,
11067 cp_parser_flags flags,
11068 cp_decl_specifier_seq *decl_specs,
11069 bool is_declaration,
11070 int* declares_class_or_enum,
11071 bool* is_cv_qualifier)
11073 tree type_spec = NULL_TREE;
11074 cp_token *token;
11075 enum rid keyword;
11076 cp_decl_spec ds = ds_last;
11078 /* Assume this type-specifier does not declare a new type. */
11079 if (declares_class_or_enum)
11080 *declares_class_or_enum = 0;
11081 /* And that it does not specify a cv-qualifier. */
11082 if (is_cv_qualifier)
11083 *is_cv_qualifier = false;
11084 /* Peek at the next token. */
11085 token = cp_lexer_peek_token (parser->lexer);
11087 /* If we're looking at a keyword, we can use that to guide the
11088 production we choose. */
11089 keyword = token->keyword;
11090 switch (keyword)
11092 case RID_ENUM:
11093 /* Look for the enum-specifier. */
11094 type_spec = cp_parser_enum_specifier (parser);
11095 /* If that worked, we're done. */
11096 if (type_spec)
11098 if (declares_class_or_enum)
11099 *declares_class_or_enum = 2;
11100 if (decl_specs)
11101 cp_parser_set_decl_spec_type (decl_specs,
11102 type_spec,
11103 token->location,
11104 /*user_defined_p=*/true);
11105 return type_spec;
11107 else
11108 goto elaborated_type_specifier;
11110 /* Any of these indicate either a class-specifier, or an
11111 elaborated-type-specifier. */
11112 case RID_CLASS:
11113 case RID_STRUCT:
11114 case RID_UNION:
11115 /* Parse tentatively so that we can back up if we don't find a
11116 class-specifier. */
11117 cp_parser_parse_tentatively (parser);
11118 /* Look for the class-specifier. */
11119 type_spec = cp_parser_class_specifier (parser);
11120 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
11121 /* If that worked, we're done. */
11122 if (cp_parser_parse_definitely (parser))
11124 if (declares_class_or_enum)
11125 *declares_class_or_enum = 2;
11126 if (decl_specs)
11127 cp_parser_set_decl_spec_type (decl_specs,
11128 type_spec,
11129 token->location,
11130 /*user_defined_p=*/true);
11131 return type_spec;
11134 /* Fall through. */
11135 elaborated_type_specifier:
11136 /* We're declaring (not defining) a class or enum. */
11137 if (declares_class_or_enum)
11138 *declares_class_or_enum = 1;
11140 /* Fall through. */
11141 case RID_TYPENAME:
11142 /* Look for an elaborated-type-specifier. */
11143 type_spec
11144 = (cp_parser_elaborated_type_specifier
11145 (parser,
11146 decl_specs && decl_specs->specs[(int) ds_friend],
11147 is_declaration));
11148 if (decl_specs)
11149 cp_parser_set_decl_spec_type (decl_specs,
11150 type_spec,
11151 token->location,
11152 /*user_defined_p=*/true);
11153 return type_spec;
11155 case RID_CONST:
11156 ds = ds_const;
11157 if (is_cv_qualifier)
11158 *is_cv_qualifier = true;
11159 break;
11161 case RID_VOLATILE:
11162 ds = ds_volatile;
11163 if (is_cv_qualifier)
11164 *is_cv_qualifier = true;
11165 break;
11167 case RID_RESTRICT:
11168 ds = ds_restrict;
11169 if (is_cv_qualifier)
11170 *is_cv_qualifier = true;
11171 break;
11173 case RID_COMPLEX:
11174 /* The `__complex__' keyword is a GNU extension. */
11175 ds = ds_complex;
11176 break;
11178 default:
11179 break;
11182 /* Handle simple keywords. */
11183 if (ds != ds_last)
11185 if (decl_specs)
11187 ++decl_specs->specs[(int)ds];
11188 decl_specs->any_specifiers_p = true;
11190 return cp_lexer_consume_token (parser->lexer)->u.value;
11193 /* If we do not already have a type-specifier, assume we are looking
11194 at a simple-type-specifier. */
11195 type_spec = cp_parser_simple_type_specifier (parser,
11196 decl_specs,
11197 flags);
11199 /* If we didn't find a type-specifier, and a type-specifier was not
11200 optional in this context, issue an error message. */
11201 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11203 cp_parser_error (parser, "expected type specifier");
11204 return error_mark_node;
11207 return type_spec;
11210 /* Parse a simple-type-specifier.
11212 simple-type-specifier:
11213 :: [opt] nested-name-specifier [opt] type-name
11214 :: [opt] nested-name-specifier template template-id
11215 char
11216 wchar_t
11217 bool
11218 short
11220 long
11221 signed
11222 unsigned
11223 float
11224 double
11225 void
11227 C++0x Extension:
11229 simple-type-specifier:
11230 auto
11231 decltype ( expression )
11232 char16_t
11233 char32_t
11235 GNU Extension:
11237 simple-type-specifier:
11238 __typeof__ unary-expression
11239 __typeof__ ( type-id )
11241 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
11242 appropriately updated. */
11244 static tree
11245 cp_parser_simple_type_specifier (cp_parser* parser,
11246 cp_decl_specifier_seq *decl_specs,
11247 cp_parser_flags flags)
11249 tree type = NULL_TREE;
11250 cp_token *token;
11252 /* Peek at the next token. */
11253 token = cp_lexer_peek_token (parser->lexer);
11255 /* If we're looking at a keyword, things are easy. */
11256 switch (token->keyword)
11258 case RID_CHAR:
11259 if (decl_specs)
11260 decl_specs->explicit_char_p = true;
11261 type = char_type_node;
11262 break;
11263 case RID_CHAR16:
11264 type = char16_type_node;
11265 break;
11266 case RID_CHAR32:
11267 type = char32_type_node;
11268 break;
11269 case RID_WCHAR:
11270 type = wchar_type_node;
11271 break;
11272 case RID_BOOL:
11273 type = boolean_type_node;
11274 break;
11275 case RID_SHORT:
11276 if (decl_specs)
11277 ++decl_specs->specs[(int) ds_short];
11278 type = short_integer_type_node;
11279 break;
11280 case RID_INT:
11281 if (decl_specs)
11282 decl_specs->explicit_int_p = true;
11283 type = integer_type_node;
11284 break;
11285 case RID_LONG:
11286 if (decl_specs)
11287 ++decl_specs->specs[(int) ds_long];
11288 type = long_integer_type_node;
11289 break;
11290 case RID_SIGNED:
11291 if (decl_specs)
11292 ++decl_specs->specs[(int) ds_signed];
11293 type = integer_type_node;
11294 break;
11295 case RID_UNSIGNED:
11296 if (decl_specs)
11297 ++decl_specs->specs[(int) ds_unsigned];
11298 type = unsigned_type_node;
11299 break;
11300 case RID_FLOAT:
11301 type = float_type_node;
11302 break;
11303 case RID_DOUBLE:
11304 type = double_type_node;
11305 break;
11306 case RID_VOID:
11307 type = void_type_node;
11308 break;
11310 case RID_AUTO:
11311 maybe_warn_cpp0x ("C++0x auto");
11312 type = make_auto ();
11313 break;
11315 case RID_DECLTYPE:
11316 /* Parse the `decltype' type. */
11317 type = cp_parser_decltype (parser);
11319 if (decl_specs)
11320 cp_parser_set_decl_spec_type (decl_specs, type,
11321 token->location,
11322 /*user_defined_p=*/true);
11324 return type;
11326 case RID_TYPEOF:
11327 /* Consume the `typeof' token. */
11328 cp_lexer_consume_token (parser->lexer);
11329 /* Parse the operand to `typeof'. */
11330 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11331 /* If it is not already a TYPE, take its type. */
11332 if (!TYPE_P (type))
11333 type = finish_typeof (type);
11335 if (decl_specs)
11336 cp_parser_set_decl_spec_type (decl_specs, type,
11337 token->location,
11338 /*user_defined_p=*/true);
11340 return type;
11342 default:
11343 break;
11346 /* If the type-specifier was for a built-in type, we're done. */
11347 if (type)
11349 tree id;
11351 /* Record the type. */
11352 if (decl_specs
11353 && (token->keyword != RID_SIGNED
11354 && token->keyword != RID_UNSIGNED
11355 && token->keyword != RID_SHORT
11356 && token->keyword != RID_LONG))
11357 cp_parser_set_decl_spec_type (decl_specs,
11358 type,
11359 token->location,
11360 /*user_defined=*/false);
11361 if (decl_specs)
11362 decl_specs->any_specifiers_p = true;
11364 /* Consume the token. */
11365 id = cp_lexer_consume_token (parser->lexer)->u.value;
11367 /* There is no valid C++ program where a non-template type is
11368 followed by a "<". That usually indicates that the user thought
11369 that the type was a template. */
11370 cp_parser_check_for_invalid_template_id (parser, type, token->location);
11372 return TYPE_NAME (type);
11375 /* The type-specifier must be a user-defined type. */
11376 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
11378 bool qualified_p;
11379 bool global_p;
11381 /* Don't gobble tokens or issue error messages if this is an
11382 optional type-specifier. */
11383 if (flags & CP_PARSER_FLAGS_OPTIONAL)
11384 cp_parser_parse_tentatively (parser);
11386 /* Look for the optional `::' operator. */
11387 global_p
11388 = (cp_parser_global_scope_opt (parser,
11389 /*current_scope_valid_p=*/false)
11390 != NULL_TREE);
11391 /* Look for the nested-name specifier. */
11392 qualified_p
11393 = (cp_parser_nested_name_specifier_opt (parser,
11394 /*typename_keyword_p=*/false,
11395 /*check_dependency_p=*/true,
11396 /*type_p=*/false,
11397 /*is_declaration=*/false)
11398 != NULL_TREE);
11399 token = cp_lexer_peek_token (parser->lexer);
11400 /* If we have seen a nested-name-specifier, and the next token
11401 is `template', then we are using the template-id production. */
11402 if (parser->scope
11403 && cp_parser_optional_template_keyword (parser))
11405 /* Look for the template-id. */
11406 type = cp_parser_template_id (parser,
11407 /*template_keyword_p=*/true,
11408 /*check_dependency_p=*/true,
11409 /*is_declaration=*/false);
11410 /* If the template-id did not name a type, we are out of
11411 luck. */
11412 if (TREE_CODE (type) != TYPE_DECL)
11414 cp_parser_error (parser, "expected template-id for type");
11415 type = NULL_TREE;
11418 /* Otherwise, look for a type-name. */
11419 else
11420 type = cp_parser_type_name (parser);
11421 /* Keep track of all name-lookups performed in class scopes. */
11422 if (type
11423 && !global_p
11424 && !qualified_p
11425 && TREE_CODE (type) == TYPE_DECL
11426 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
11427 maybe_note_name_used_in_class (DECL_NAME (type), type);
11428 /* If it didn't work out, we don't have a TYPE. */
11429 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
11430 && !cp_parser_parse_definitely (parser))
11431 type = NULL_TREE;
11432 if (type && decl_specs)
11433 cp_parser_set_decl_spec_type (decl_specs, type,
11434 token->location,
11435 /*user_defined=*/true);
11438 /* If we didn't get a type-name, issue an error message. */
11439 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11441 cp_parser_error (parser, "expected type-name");
11442 return error_mark_node;
11445 /* There is no valid C++ program where a non-template type is
11446 followed by a "<". That usually indicates that the user thought
11447 that the type was a template. */
11448 if (type && type != error_mark_node)
11450 /* As a last-ditch effort, see if TYPE is an Objective-C type.
11451 If it is, then the '<'...'>' enclose protocol names rather than
11452 template arguments, and so everything is fine. */
11453 if (c_dialect_objc ()
11454 && (objc_is_id (type) || objc_is_class_name (type)))
11456 tree protos = cp_parser_objc_protocol_refs_opt (parser);
11457 tree qual_type = objc_get_protocol_qualified_type (type, protos);
11459 /* Clobber the "unqualified" type previously entered into
11460 DECL_SPECS with the new, improved protocol-qualified version. */
11461 if (decl_specs)
11462 decl_specs->type = qual_type;
11464 return qual_type;
11467 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
11468 token->location);
11471 return type;
11474 /* Parse a type-name.
11476 type-name:
11477 class-name
11478 enum-name
11479 typedef-name
11481 enum-name:
11482 identifier
11484 typedef-name:
11485 identifier
11487 Returns a TYPE_DECL for the type. */
11489 static tree
11490 cp_parser_type_name (cp_parser* parser)
11492 tree type_decl;
11494 /* We can't know yet whether it is a class-name or not. */
11495 cp_parser_parse_tentatively (parser);
11496 /* Try a class-name. */
11497 type_decl = cp_parser_class_name (parser,
11498 /*typename_keyword_p=*/false,
11499 /*template_keyword_p=*/false,
11500 none_type,
11501 /*check_dependency_p=*/true,
11502 /*class_head_p=*/false,
11503 /*is_declaration=*/false);
11504 /* If it's not a class-name, keep looking. */
11505 if (!cp_parser_parse_definitely (parser))
11507 /* It must be a typedef-name or an enum-name. */
11508 return cp_parser_nonclass_name (parser);
11511 return type_decl;
11514 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
11516 enum-name:
11517 identifier
11519 typedef-name:
11520 identifier
11522 Returns a TYPE_DECL for the type. */
11524 static tree
11525 cp_parser_nonclass_name (cp_parser* parser)
11527 tree type_decl;
11528 tree identifier;
11530 cp_token *token = cp_lexer_peek_token (parser->lexer);
11531 identifier = cp_parser_identifier (parser);
11532 if (identifier == error_mark_node)
11533 return error_mark_node;
11535 /* Look up the type-name. */
11536 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
11538 if (TREE_CODE (type_decl) != TYPE_DECL
11539 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
11541 /* See if this is an Objective-C type. */
11542 tree protos = cp_parser_objc_protocol_refs_opt (parser);
11543 tree type = objc_get_protocol_qualified_type (identifier, protos);
11544 if (type)
11545 type_decl = TYPE_NAME (type);
11548 /* Issue an error if we did not find a type-name. */
11549 if (TREE_CODE (type_decl) != TYPE_DECL)
11551 if (!cp_parser_simulate_error (parser))
11552 cp_parser_name_lookup_error (parser, identifier, type_decl,
11553 "is not a type", token->location);
11554 return error_mark_node;
11556 /* Remember that the name was used in the definition of the
11557 current class so that we can check later to see if the
11558 meaning would have been different after the class was
11559 entirely defined. */
11560 else if (type_decl != error_mark_node
11561 && !parser->scope)
11562 maybe_note_name_used_in_class (identifier, type_decl);
11564 return type_decl;
11567 /* Parse an elaborated-type-specifier. Note that the grammar given
11568 here incorporates the resolution to DR68.
11570 elaborated-type-specifier:
11571 class-key :: [opt] nested-name-specifier [opt] identifier
11572 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
11573 enum-key :: [opt] nested-name-specifier [opt] identifier
11574 typename :: [opt] nested-name-specifier identifier
11575 typename :: [opt] nested-name-specifier template [opt]
11576 template-id
11578 GNU extension:
11580 elaborated-type-specifier:
11581 class-key attributes :: [opt] nested-name-specifier [opt] identifier
11582 class-key attributes :: [opt] nested-name-specifier [opt]
11583 template [opt] template-id
11584 enum attributes :: [opt] nested-name-specifier [opt] identifier
11586 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
11587 declared `friend'. If IS_DECLARATION is TRUE, then this
11588 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
11589 something is being declared.
11591 Returns the TYPE specified. */
11593 static tree
11594 cp_parser_elaborated_type_specifier (cp_parser* parser,
11595 bool is_friend,
11596 bool is_declaration)
11598 enum tag_types tag_type;
11599 tree identifier;
11600 tree type = NULL_TREE;
11601 tree attributes = NULL_TREE;
11602 tree globalscope;
11603 cp_token *token = NULL;
11605 /* See if we're looking at the `enum' keyword. */
11606 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
11608 /* Consume the `enum' token. */
11609 cp_lexer_consume_token (parser->lexer);
11610 /* Remember that it's an enumeration type. */
11611 tag_type = enum_type;
11612 /* Parse the optional `struct' or `class' key (for C++0x scoped
11613 enums). */
11614 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11615 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11617 if (cxx_dialect == cxx98)
11618 maybe_warn_cpp0x ("scoped enums");
11620 /* Consume the `struct' or `class'. */
11621 cp_lexer_consume_token (parser->lexer);
11623 /* Parse the attributes. */
11624 attributes = cp_parser_attributes_opt (parser);
11626 /* Or, it might be `typename'. */
11627 else if (cp_lexer_next_token_is_keyword (parser->lexer,
11628 RID_TYPENAME))
11630 /* Consume the `typename' token. */
11631 cp_lexer_consume_token (parser->lexer);
11632 /* Remember that it's a `typename' type. */
11633 tag_type = typename_type;
11635 /* Otherwise it must be a class-key. */
11636 else
11638 tag_type = cp_parser_class_key (parser);
11639 if (tag_type == none_type)
11640 return error_mark_node;
11641 /* Parse the attributes. */
11642 attributes = cp_parser_attributes_opt (parser);
11645 /* Look for the `::' operator. */
11646 globalscope = cp_parser_global_scope_opt (parser,
11647 /*current_scope_valid_p=*/false);
11648 /* Look for the nested-name-specifier. */
11649 if (tag_type == typename_type && !globalscope)
11651 if (!cp_parser_nested_name_specifier (parser,
11652 /*typename_keyword_p=*/true,
11653 /*check_dependency_p=*/true,
11654 /*type_p=*/true,
11655 is_declaration))
11656 return error_mark_node;
11658 else
11659 /* Even though `typename' is not present, the proposed resolution
11660 to Core Issue 180 says that in `class A<T>::B', `B' should be
11661 considered a type-name, even if `A<T>' is dependent. */
11662 cp_parser_nested_name_specifier_opt (parser,
11663 /*typename_keyword_p=*/true,
11664 /*check_dependency_p=*/true,
11665 /*type_p=*/true,
11666 is_declaration);
11667 /* For everything but enumeration types, consider a template-id.
11668 For an enumeration type, consider only a plain identifier. */
11669 if (tag_type != enum_type)
11671 bool template_p = false;
11672 tree decl;
11674 /* Allow the `template' keyword. */
11675 template_p = cp_parser_optional_template_keyword (parser);
11676 /* If we didn't see `template', we don't know if there's a
11677 template-id or not. */
11678 if (!template_p)
11679 cp_parser_parse_tentatively (parser);
11680 /* Parse the template-id. */
11681 token = cp_lexer_peek_token (parser->lexer);
11682 decl = cp_parser_template_id (parser, template_p,
11683 /*check_dependency_p=*/true,
11684 is_declaration);
11685 /* If we didn't find a template-id, look for an ordinary
11686 identifier. */
11687 if (!template_p && !cp_parser_parse_definitely (parser))
11689 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
11690 in effect, then we must assume that, upon instantiation, the
11691 template will correspond to a class. */
11692 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11693 && tag_type == typename_type)
11694 type = make_typename_type (parser->scope, decl,
11695 typename_type,
11696 /*complain=*/tf_error);
11697 /* If the `typename' keyword is in effect and DECL is not a type
11698 decl. Then type is non existant. */
11699 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
11700 type = NULL_TREE;
11701 else
11702 type = TREE_TYPE (decl);
11705 if (!type)
11707 token = cp_lexer_peek_token (parser->lexer);
11708 identifier = cp_parser_identifier (parser);
11710 if (identifier == error_mark_node)
11712 parser->scope = NULL_TREE;
11713 return error_mark_node;
11716 /* For a `typename', we needn't call xref_tag. */
11717 if (tag_type == typename_type
11718 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
11719 return cp_parser_make_typename_type (parser, parser->scope,
11720 identifier,
11721 token->location);
11722 /* Look up a qualified name in the usual way. */
11723 if (parser->scope)
11725 tree decl;
11726 tree ambiguous_decls;
11728 decl = cp_parser_lookup_name (parser, identifier,
11729 tag_type,
11730 /*is_template=*/false,
11731 /*is_namespace=*/false,
11732 /*check_dependency=*/true,
11733 &ambiguous_decls,
11734 token->location);
11736 /* If the lookup was ambiguous, an error will already have been
11737 issued. */
11738 if (ambiguous_decls)
11739 return error_mark_node;
11741 /* If we are parsing friend declaration, DECL may be a
11742 TEMPLATE_DECL tree node here. However, we need to check
11743 whether this TEMPLATE_DECL results in valid code. Consider
11744 the following example:
11746 namespace N {
11747 template <class T> class C {};
11749 class X {
11750 template <class T> friend class N::C; // #1, valid code
11752 template <class T> class Y {
11753 friend class N::C; // #2, invalid code
11756 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
11757 name lookup of `N::C'. We see that friend declaration must
11758 be template for the code to be valid. Note that
11759 processing_template_decl does not work here since it is
11760 always 1 for the above two cases. */
11762 decl = (cp_parser_maybe_treat_template_as_class
11763 (decl, /*tag_name_p=*/is_friend
11764 && parser->num_template_parameter_lists));
11766 if (TREE_CODE (decl) != TYPE_DECL)
11768 cp_parser_diagnose_invalid_type_name (parser,
11769 parser->scope,
11770 identifier,
11771 token->location);
11772 return error_mark_node;
11775 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
11777 bool allow_template = (parser->num_template_parameter_lists
11778 || DECL_SELF_REFERENCE_P (decl));
11779 type = check_elaborated_type_specifier (tag_type, decl,
11780 allow_template);
11782 if (type == error_mark_node)
11783 return error_mark_node;
11786 /* Forward declarations of nested types, such as
11788 class C1::C2;
11789 class C1::C2::C3;
11791 are invalid unless all components preceding the final '::'
11792 are complete. If all enclosing types are complete, these
11793 declarations become merely pointless.
11795 Invalid forward declarations of nested types are errors
11796 caught elsewhere in parsing. Those that are pointless arrive
11797 here. */
11799 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
11800 && !is_friend && !processing_explicit_instantiation)
11801 warning (0, "declaration %qD does not declare anything", decl);
11803 type = TREE_TYPE (decl);
11805 else
11807 /* An elaborated-type-specifier sometimes introduces a new type and
11808 sometimes names an existing type. Normally, the rule is that it
11809 introduces a new type only if there is not an existing type of
11810 the same name already in scope. For example, given:
11812 struct S {};
11813 void f() { struct S s; }
11815 the `struct S' in the body of `f' is the same `struct S' as in
11816 the global scope; the existing definition is used. However, if
11817 there were no global declaration, this would introduce a new
11818 local class named `S'.
11820 An exception to this rule applies to the following code:
11822 namespace N { struct S; }
11824 Here, the elaborated-type-specifier names a new type
11825 unconditionally; even if there is already an `S' in the
11826 containing scope this declaration names a new type.
11827 This exception only applies if the elaborated-type-specifier
11828 forms the complete declaration:
11830 [class.name]
11832 A declaration consisting solely of `class-key identifier ;' is
11833 either a redeclaration of the name in the current scope or a
11834 forward declaration of the identifier as a class name. It
11835 introduces the name into the current scope.
11837 We are in this situation precisely when the next token is a `;'.
11839 An exception to the exception is that a `friend' declaration does
11840 *not* name a new type; i.e., given:
11842 struct S { friend struct T; };
11844 `T' is not a new type in the scope of `S'.
11846 Also, `new struct S' or `sizeof (struct S)' never results in the
11847 definition of a new type; a new type can only be declared in a
11848 declaration context. */
11850 tag_scope ts;
11851 bool template_p;
11853 if (is_friend)
11854 /* Friends have special name lookup rules. */
11855 ts = ts_within_enclosing_non_class;
11856 else if (is_declaration
11857 && cp_lexer_next_token_is (parser->lexer,
11858 CPP_SEMICOLON))
11859 /* This is a `class-key identifier ;' */
11860 ts = ts_current;
11861 else
11862 ts = ts_global;
11864 template_p =
11865 (parser->num_template_parameter_lists
11866 && (cp_parser_next_token_starts_class_definition_p (parser)
11867 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
11868 /* An unqualified name was used to reference this type, so
11869 there were no qualifying templates. */
11870 if (!cp_parser_check_template_parameters (parser,
11871 /*num_templates=*/0,
11872 token->location,
11873 /*declarator=*/NULL))
11874 return error_mark_node;
11875 type = xref_tag (tag_type, identifier, ts, template_p);
11879 if (type == error_mark_node)
11880 return error_mark_node;
11882 /* Allow attributes on forward declarations of classes. */
11883 if (attributes)
11885 if (TREE_CODE (type) == TYPENAME_TYPE)
11886 warning (OPT_Wattributes,
11887 "attributes ignored on uninstantiated type");
11888 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
11889 && ! processing_explicit_instantiation)
11890 warning (OPT_Wattributes,
11891 "attributes ignored on template instantiation");
11892 else if (is_declaration && cp_parser_declares_only_class_p (parser))
11893 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
11894 else
11895 warning (OPT_Wattributes,
11896 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11899 if (tag_type != enum_type)
11900 cp_parser_check_class_key (tag_type, type);
11902 /* A "<" cannot follow an elaborated type specifier. If that
11903 happens, the user was probably trying to form a template-id. */
11904 cp_parser_check_for_invalid_template_id (parser, type, token->location);
11906 return type;
11909 /* Parse an enum-specifier.
11911 enum-specifier:
11912 enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
11914 enum-key:
11915 enum
11916 enum class [C++0x]
11917 enum struct [C++0x]
11919 enum-base: [C++0x]
11920 : type-specifier-seq
11922 GNU Extensions:
11923 enum-key attributes[opt] identifier [opt] enum-base [opt]
11924 { enumerator-list [opt] }attributes[opt]
11926 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11927 if the token stream isn't an enum-specifier after all. */
11929 static tree
11930 cp_parser_enum_specifier (cp_parser* parser)
11932 tree identifier;
11933 tree type;
11934 tree attributes;
11935 bool scoped_enum_p = false;
11936 bool has_underlying_type = false;
11937 tree underlying_type = NULL_TREE;
11939 /* Parse tentatively so that we can back up if we don't find a
11940 enum-specifier. */
11941 cp_parser_parse_tentatively (parser);
11943 /* Caller guarantees that the current token is 'enum', an identifier
11944 possibly follows, and the token after that is an opening brace.
11945 If we don't have an identifier, fabricate an anonymous name for
11946 the enumeration being defined. */
11947 cp_lexer_consume_token (parser->lexer);
11949 /* Parse the "class" or "struct", which indicates a scoped
11950 enumeration type in C++0x. */
11951 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11952 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11954 if (cxx_dialect == cxx98)
11955 maybe_warn_cpp0x ("scoped enums");
11957 /* Consume the `struct' or `class' token. */
11958 cp_lexer_consume_token (parser->lexer);
11960 scoped_enum_p = true;
11963 attributes = cp_parser_attributes_opt (parser);
11965 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11966 identifier = cp_parser_identifier (parser);
11967 else
11968 identifier = make_anon_name ();
11970 /* Check for the `:' that denotes a specified underlying type in C++0x. */
11971 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11973 cp_decl_specifier_seq type_specifiers;
11975 /* At this point this is surely not elaborated type specifier. */
11976 if (!cp_parser_parse_definitely (parser))
11977 return NULL_TREE;
11979 if (cxx_dialect == cxx98)
11980 maybe_warn_cpp0x ("scoped enums");
11982 /* Consume the `:'. */
11983 cp_lexer_consume_token (parser->lexer);
11985 has_underlying_type = true;
11987 /* Parse the type-specifier-seq. */
11988 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11989 &type_specifiers);
11991 /* If that didn't work, stop. */
11992 if (type_specifiers.type != error_mark_node)
11994 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
11995 /*initialized=*/0, NULL);
11996 if (underlying_type == error_mark_node)
11997 underlying_type = NULL_TREE;
12001 /* Look for the `{' but don't consume it yet. */
12002 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12004 cp_parser_error (parser, "expected %<{%>");
12005 if (has_underlying_type)
12006 return NULL_TREE;
12009 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
12010 return NULL_TREE;
12012 /* Issue an error message if type-definitions are forbidden here. */
12013 if (!cp_parser_check_type_definition (parser))
12014 type = error_mark_node;
12015 else
12016 /* Create the new type. We do this before consuming the opening
12017 brace so the enum will be recorded as being on the line of its
12018 tag (or the 'enum' keyword, if there is no tag). */
12019 type = start_enum (identifier, underlying_type, scoped_enum_p);
12021 /* Consume the opening brace. */
12022 cp_lexer_consume_token (parser->lexer);
12024 if (type == error_mark_node)
12026 cp_parser_skip_to_end_of_block_or_statement (parser);
12027 return error_mark_node;
12030 /* If the next token is not '}', then there are some enumerators. */
12031 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12032 cp_parser_enumerator_list (parser, type);
12034 /* Consume the final '}'. */
12035 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12037 /* Look for trailing attributes to apply to this enumeration, and
12038 apply them if appropriate. */
12039 if (cp_parser_allow_gnu_extensions_p (parser))
12041 tree trailing_attr = cp_parser_attributes_opt (parser);
12042 trailing_attr = chainon (trailing_attr, attributes);
12043 cplus_decl_attributes (&type,
12044 trailing_attr,
12045 (int) ATTR_FLAG_TYPE_IN_PLACE);
12048 /* Finish up the enumeration. */
12049 finish_enum (type);
12051 return type;
12054 /* Parse an enumerator-list. The enumerators all have the indicated
12055 TYPE.
12057 enumerator-list:
12058 enumerator-definition
12059 enumerator-list , enumerator-definition */
12061 static void
12062 cp_parser_enumerator_list (cp_parser* parser, tree type)
12064 while (true)
12066 /* Parse an enumerator-definition. */
12067 cp_parser_enumerator_definition (parser, type);
12069 /* If the next token is not a ',', we've reached the end of
12070 the list. */
12071 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12072 break;
12073 /* Otherwise, consume the `,' and keep going. */
12074 cp_lexer_consume_token (parser->lexer);
12075 /* If the next token is a `}', there is a trailing comma. */
12076 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
12078 if (!in_system_header)
12079 pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
12080 break;
12085 /* Parse an enumerator-definition. The enumerator has the indicated
12086 TYPE.
12088 enumerator-definition:
12089 enumerator
12090 enumerator = constant-expression
12092 enumerator:
12093 identifier */
12095 static void
12096 cp_parser_enumerator_definition (cp_parser* parser, tree type)
12098 tree identifier;
12099 tree value;
12101 /* Look for the identifier. */
12102 identifier = cp_parser_identifier (parser);
12103 if (identifier == error_mark_node)
12104 return;
12106 /* If the next token is an '=', then there is an explicit value. */
12107 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12109 /* Consume the `=' token. */
12110 cp_lexer_consume_token (parser->lexer);
12111 /* Parse the value. */
12112 value = cp_parser_constant_expression (parser,
12113 /*allow_non_constant_p=*/false,
12114 NULL);
12116 else
12117 value = NULL_TREE;
12119 /* If we are processing a template, make sure the initializer of the
12120 enumerator doesn't contain any bare template parameter pack. */
12121 if (check_for_bare_parameter_packs (value))
12122 value = error_mark_node;
12124 /* Create the enumerator. */
12125 build_enumerator (identifier, value, type);
12128 /* Parse a namespace-name.
12130 namespace-name:
12131 original-namespace-name
12132 namespace-alias
12134 Returns the NAMESPACE_DECL for the namespace. */
12136 static tree
12137 cp_parser_namespace_name (cp_parser* parser)
12139 tree identifier;
12140 tree namespace_decl;
12142 cp_token *token = cp_lexer_peek_token (parser->lexer);
12144 /* Get the name of the namespace. */
12145 identifier = cp_parser_identifier (parser);
12146 if (identifier == error_mark_node)
12147 return error_mark_node;
12149 /* Look up the identifier in the currently active scope. Look only
12150 for namespaces, due to:
12152 [basic.lookup.udir]
12154 When looking up a namespace-name in a using-directive or alias
12155 definition, only namespace names are considered.
12157 And:
12159 [basic.lookup.qual]
12161 During the lookup of a name preceding the :: scope resolution
12162 operator, object, function, and enumerator names are ignored.
12164 (Note that cp_parser_qualifying_entity only calls this
12165 function if the token after the name is the scope resolution
12166 operator.) */
12167 namespace_decl = cp_parser_lookup_name (parser, identifier,
12168 none_type,
12169 /*is_template=*/false,
12170 /*is_namespace=*/true,
12171 /*check_dependency=*/true,
12172 /*ambiguous_decls=*/NULL,
12173 token->location);
12174 /* If it's not a namespace, issue an error. */
12175 if (namespace_decl == error_mark_node
12176 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
12178 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12179 error_at (token->location, "%qD is not a namespace-name", identifier);
12180 cp_parser_error (parser, "expected namespace-name");
12181 namespace_decl = error_mark_node;
12184 return namespace_decl;
12187 /* Parse a namespace-definition.
12189 namespace-definition:
12190 named-namespace-definition
12191 unnamed-namespace-definition
12193 named-namespace-definition:
12194 original-namespace-definition
12195 extension-namespace-definition
12197 original-namespace-definition:
12198 namespace identifier { namespace-body }
12200 extension-namespace-definition:
12201 namespace original-namespace-name { namespace-body }
12203 unnamed-namespace-definition:
12204 namespace { namespace-body } */
12206 static void
12207 cp_parser_namespace_definition (cp_parser* parser)
12209 tree identifier, attribs;
12210 bool has_visibility;
12211 bool is_inline;
12213 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
12215 is_inline = true;
12216 cp_lexer_consume_token (parser->lexer);
12218 else
12219 is_inline = false;
12221 /* Look for the `namespace' keyword. */
12222 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12224 /* Get the name of the namespace. We do not attempt to distinguish
12225 between an original-namespace-definition and an
12226 extension-namespace-definition at this point. The semantic
12227 analysis routines are responsible for that. */
12228 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12229 identifier = cp_parser_identifier (parser);
12230 else
12231 identifier = NULL_TREE;
12233 /* Parse any specified attributes. */
12234 attribs = cp_parser_attributes_opt (parser);
12236 /* Look for the `{' to start the namespace. */
12237 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
12238 /* Start the namespace. */
12239 push_namespace (identifier);
12241 /* "inline namespace" is equivalent to a stub namespace definition
12242 followed by a strong using directive. */
12243 if (is_inline)
12245 tree name_space = current_namespace;
12246 /* Set up namespace association. */
12247 DECL_NAMESPACE_ASSOCIATIONS (name_space)
12248 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
12249 DECL_NAMESPACE_ASSOCIATIONS (name_space));
12250 /* Import the contents of the inline namespace. */
12251 pop_namespace ();
12252 do_using_directive (name_space);
12253 push_namespace (identifier);
12256 has_visibility = handle_namespace_attrs (current_namespace, attribs);
12258 /* Parse the body of the namespace. */
12259 cp_parser_namespace_body (parser);
12261 #ifdef HANDLE_PRAGMA_VISIBILITY
12262 if (has_visibility)
12263 pop_visibility ();
12264 #endif
12266 /* Finish the namespace. */
12267 pop_namespace ();
12268 /* Look for the final `}'. */
12269 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12272 /* Parse a namespace-body.
12274 namespace-body:
12275 declaration-seq [opt] */
12277 static void
12278 cp_parser_namespace_body (cp_parser* parser)
12280 cp_parser_declaration_seq_opt (parser);
12283 /* Parse a namespace-alias-definition.
12285 namespace-alias-definition:
12286 namespace identifier = qualified-namespace-specifier ; */
12288 static void
12289 cp_parser_namespace_alias_definition (cp_parser* parser)
12291 tree identifier;
12292 tree namespace_specifier;
12294 cp_token *token = cp_lexer_peek_token (parser->lexer);
12296 /* Look for the `namespace' keyword. */
12297 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12298 /* Look for the identifier. */
12299 identifier = cp_parser_identifier (parser);
12300 if (identifier == error_mark_node)
12301 return;
12302 /* Look for the `=' token. */
12303 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12304 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12306 error_at (token->location, "%<namespace%> definition is not allowed here");
12307 /* Skip the definition. */
12308 cp_lexer_consume_token (parser->lexer);
12309 if (cp_parser_skip_to_closing_brace (parser))
12310 cp_lexer_consume_token (parser->lexer);
12311 return;
12313 cp_parser_require (parser, CPP_EQ, "%<=%>");
12314 /* Look for the qualified-namespace-specifier. */
12315 namespace_specifier
12316 = cp_parser_qualified_namespace_specifier (parser);
12317 /* Look for the `;' token. */
12318 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12320 /* Register the alias in the symbol table. */
12321 do_namespace_alias (identifier, namespace_specifier);
12324 /* Parse a qualified-namespace-specifier.
12326 qualified-namespace-specifier:
12327 :: [opt] nested-name-specifier [opt] namespace-name
12329 Returns a NAMESPACE_DECL corresponding to the specified
12330 namespace. */
12332 static tree
12333 cp_parser_qualified_namespace_specifier (cp_parser* parser)
12335 /* Look for the optional `::'. */
12336 cp_parser_global_scope_opt (parser,
12337 /*current_scope_valid_p=*/false);
12339 /* Look for the optional nested-name-specifier. */
12340 cp_parser_nested_name_specifier_opt (parser,
12341 /*typename_keyword_p=*/false,
12342 /*check_dependency_p=*/true,
12343 /*type_p=*/false,
12344 /*is_declaration=*/true);
12346 return cp_parser_namespace_name (parser);
12349 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
12350 access declaration.
12352 using-declaration:
12353 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
12354 using :: unqualified-id ;
12356 access-declaration:
12357 qualified-id ;
12361 static bool
12362 cp_parser_using_declaration (cp_parser* parser,
12363 bool access_declaration_p)
12365 cp_token *token;
12366 bool typename_p = false;
12367 bool global_scope_p;
12368 tree decl;
12369 tree identifier;
12370 tree qscope;
12372 if (access_declaration_p)
12373 cp_parser_parse_tentatively (parser);
12374 else
12376 /* Look for the `using' keyword. */
12377 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12379 /* Peek at the next token. */
12380 token = cp_lexer_peek_token (parser->lexer);
12381 /* See if it's `typename'. */
12382 if (token->keyword == RID_TYPENAME)
12384 /* Remember that we've seen it. */
12385 typename_p = true;
12386 /* Consume the `typename' token. */
12387 cp_lexer_consume_token (parser->lexer);
12391 /* Look for the optional global scope qualification. */
12392 global_scope_p
12393 = (cp_parser_global_scope_opt (parser,
12394 /*current_scope_valid_p=*/false)
12395 != NULL_TREE);
12397 /* If we saw `typename', or didn't see `::', then there must be a
12398 nested-name-specifier present. */
12399 if (typename_p || !global_scope_p)
12400 qscope = cp_parser_nested_name_specifier (parser, typename_p,
12401 /*check_dependency_p=*/true,
12402 /*type_p=*/false,
12403 /*is_declaration=*/true);
12404 /* Otherwise, we could be in either of the two productions. In that
12405 case, treat the nested-name-specifier as optional. */
12406 else
12407 qscope = cp_parser_nested_name_specifier_opt (parser,
12408 /*typename_keyword_p=*/false,
12409 /*check_dependency_p=*/true,
12410 /*type_p=*/false,
12411 /*is_declaration=*/true);
12412 if (!qscope)
12413 qscope = global_namespace;
12415 if (access_declaration_p && cp_parser_error_occurred (parser))
12416 /* Something has already gone wrong; there's no need to parse
12417 further. Since an error has occurred, the return value of
12418 cp_parser_parse_definitely will be false, as required. */
12419 return cp_parser_parse_definitely (parser);
12421 token = cp_lexer_peek_token (parser->lexer);
12422 /* Parse the unqualified-id. */
12423 identifier = cp_parser_unqualified_id (parser,
12424 /*template_keyword_p=*/false,
12425 /*check_dependency_p=*/true,
12426 /*declarator_p=*/true,
12427 /*optional_p=*/false);
12429 if (access_declaration_p)
12431 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12432 cp_parser_simulate_error (parser);
12433 if (!cp_parser_parse_definitely (parser))
12434 return false;
12437 /* The function we call to handle a using-declaration is different
12438 depending on what scope we are in. */
12439 if (qscope == error_mark_node || identifier == error_mark_node)
12441 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
12442 && TREE_CODE (identifier) != BIT_NOT_EXPR)
12443 /* [namespace.udecl]
12445 A using declaration shall not name a template-id. */
12446 error_at (token->location,
12447 "a template-id may not appear in a using-declaration");
12448 else
12450 if (at_class_scope_p ())
12452 /* Create the USING_DECL. */
12453 decl = do_class_using_decl (parser->scope, identifier);
12455 if (check_for_bare_parameter_packs (decl))
12456 return false;
12457 else
12458 /* Add it to the list of members in this class. */
12459 finish_member_declaration (decl);
12461 else
12463 decl = cp_parser_lookup_name_simple (parser,
12464 identifier,
12465 token->location);
12466 if (decl == error_mark_node)
12467 cp_parser_name_lookup_error (parser, identifier,
12468 decl, NULL,
12469 token->location);
12470 else if (check_for_bare_parameter_packs (decl))
12471 return false;
12472 else if (!at_namespace_scope_p ())
12473 do_local_using_decl (decl, qscope, identifier);
12474 else
12475 do_toplevel_using_decl (decl, qscope, identifier);
12479 /* Look for the final `;'. */
12480 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12482 return true;
12485 /* Parse a using-directive.
12487 using-directive:
12488 using namespace :: [opt] nested-name-specifier [opt]
12489 namespace-name ; */
12491 static void
12492 cp_parser_using_directive (cp_parser* parser)
12494 tree namespace_decl;
12495 tree attribs;
12497 /* Look for the `using' keyword. */
12498 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12499 /* And the `namespace' keyword. */
12500 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12501 /* Look for the optional `::' operator. */
12502 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12503 /* And the optional nested-name-specifier. */
12504 cp_parser_nested_name_specifier_opt (parser,
12505 /*typename_keyword_p=*/false,
12506 /*check_dependency_p=*/true,
12507 /*type_p=*/false,
12508 /*is_declaration=*/true);
12509 /* Get the namespace being used. */
12510 namespace_decl = cp_parser_namespace_name (parser);
12511 /* And any specified attributes. */
12512 attribs = cp_parser_attributes_opt (parser);
12513 /* Update the symbol table. */
12514 parse_using_directive (namespace_decl, attribs);
12515 /* Look for the final `;'. */
12516 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12519 /* Parse an asm-definition.
12521 asm-definition:
12522 asm ( string-literal ) ;
12524 GNU Extension:
12526 asm-definition:
12527 asm volatile [opt] ( string-literal ) ;
12528 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
12529 asm volatile [opt] ( string-literal : asm-operand-list [opt]
12530 : asm-operand-list [opt] ) ;
12531 asm volatile [opt] ( string-literal : asm-operand-list [opt]
12532 : asm-operand-list [opt]
12533 : asm-operand-list [opt] ) ; */
12535 static void
12536 cp_parser_asm_definition (cp_parser* parser)
12538 tree string;
12539 tree outputs = NULL_TREE;
12540 tree inputs = NULL_TREE;
12541 tree clobbers = NULL_TREE;
12542 tree asm_stmt;
12543 bool volatile_p = false;
12544 bool extended_p = false;
12545 bool invalid_inputs_p = false;
12546 bool invalid_outputs_p = false;
12548 /* Look for the `asm' keyword. */
12549 cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
12550 /* See if the next token is `volatile'. */
12551 if (cp_parser_allow_gnu_extensions_p (parser)
12552 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
12554 /* Remember that we saw the `volatile' keyword. */
12555 volatile_p = true;
12556 /* Consume the token. */
12557 cp_lexer_consume_token (parser->lexer);
12559 /* Look for the opening `('. */
12560 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
12561 return;
12562 /* Look for the string. */
12563 string = cp_parser_string_literal (parser, false, false);
12564 if (string == error_mark_node)
12566 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12567 /*consume_paren=*/true);
12568 return;
12571 /* If we're allowing GNU extensions, check for the extended assembly
12572 syntax. Unfortunately, the `:' tokens need not be separated by
12573 a space in C, and so, for compatibility, we tolerate that here
12574 too. Doing that means that we have to treat the `::' operator as
12575 two `:' tokens. */
12576 if (cp_parser_allow_gnu_extensions_p (parser)
12577 && parser->in_function_body
12578 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
12579 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
12581 bool inputs_p = false;
12582 bool clobbers_p = false;
12584 /* The extended syntax was used. */
12585 extended_p = true;
12587 /* Look for outputs. */
12588 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12590 /* Consume the `:'. */
12591 cp_lexer_consume_token (parser->lexer);
12592 /* Parse the output-operands. */
12593 if (cp_lexer_next_token_is_not (parser->lexer,
12594 CPP_COLON)
12595 && cp_lexer_next_token_is_not (parser->lexer,
12596 CPP_SCOPE)
12597 && cp_lexer_next_token_is_not (parser->lexer,
12598 CPP_CLOSE_PAREN))
12599 outputs = cp_parser_asm_operand_list (parser);
12601 if (outputs == error_mark_node)
12602 invalid_outputs_p = true;
12604 /* If the next token is `::', there are no outputs, and the
12605 next token is the beginning of the inputs. */
12606 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12607 /* The inputs are coming next. */
12608 inputs_p = true;
12610 /* Look for inputs. */
12611 if (inputs_p
12612 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12614 /* Consume the `:' or `::'. */
12615 cp_lexer_consume_token (parser->lexer);
12616 /* Parse the output-operands. */
12617 if (cp_lexer_next_token_is_not (parser->lexer,
12618 CPP_COLON)
12619 && cp_lexer_next_token_is_not (parser->lexer,
12620 CPP_CLOSE_PAREN))
12621 inputs = cp_parser_asm_operand_list (parser);
12623 if (inputs == error_mark_node)
12624 invalid_inputs_p = true;
12626 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12627 /* The clobbers are coming next. */
12628 clobbers_p = true;
12630 /* Look for clobbers. */
12631 if (clobbers_p
12632 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12634 /* Consume the `:' or `::'. */
12635 cp_lexer_consume_token (parser->lexer);
12636 /* Parse the clobbers. */
12637 if (cp_lexer_next_token_is_not (parser->lexer,
12638 CPP_CLOSE_PAREN))
12639 clobbers = cp_parser_asm_clobber_list (parser);
12642 /* Look for the closing `)'. */
12643 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
12644 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12645 /*consume_paren=*/true);
12646 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12648 if (!invalid_inputs_p && !invalid_outputs_p)
12650 /* Create the ASM_EXPR. */
12651 if (parser->in_function_body)
12653 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
12654 inputs, clobbers);
12655 /* If the extended syntax was not used, mark the ASM_EXPR. */
12656 if (!extended_p)
12658 tree temp = asm_stmt;
12659 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
12660 temp = TREE_OPERAND (temp, 0);
12662 ASM_INPUT_P (temp) = 1;
12665 else
12666 cgraph_add_asm_node (string);
12670 /* Declarators [gram.dcl.decl] */
12672 /* Parse an init-declarator.
12674 init-declarator:
12675 declarator initializer [opt]
12677 GNU Extension:
12679 init-declarator:
12680 declarator asm-specification [opt] attributes [opt] initializer [opt]
12682 function-definition:
12683 decl-specifier-seq [opt] declarator ctor-initializer [opt]
12684 function-body
12685 decl-specifier-seq [opt] declarator function-try-block
12687 GNU Extension:
12689 function-definition:
12690 __extension__ function-definition
12692 The DECL_SPECIFIERS apply to this declarator. Returns a
12693 representation of the entity declared. If MEMBER_P is TRUE, then
12694 this declarator appears in a class scope. The new DECL created by
12695 this declarator is returned.
12697 The CHECKS are access checks that should be performed once we know
12698 what entity is being declared (and, therefore, what classes have
12699 befriended it).
12701 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
12702 for a function-definition here as well. If the declarator is a
12703 declarator for a function-definition, *FUNCTION_DEFINITION_P will
12704 be TRUE upon return. By that point, the function-definition will
12705 have been completely parsed.
12707 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
12708 is FALSE. */
12710 static tree
12711 cp_parser_init_declarator (cp_parser* parser,
12712 cp_decl_specifier_seq *decl_specifiers,
12713 VEC (deferred_access_check,gc)* checks,
12714 bool function_definition_allowed_p,
12715 bool member_p,
12716 int declares_class_or_enum,
12717 bool* function_definition_p)
12719 cp_token *token = NULL, *asm_spec_start_token = NULL,
12720 *attributes_start_token = NULL;
12721 cp_declarator *declarator;
12722 tree prefix_attributes;
12723 tree attributes;
12724 tree asm_specification;
12725 tree initializer;
12726 tree decl = NULL_TREE;
12727 tree scope;
12728 int is_initialized;
12729 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
12730 initialized with "= ..", CPP_OPEN_PAREN if initialized with
12731 "(...)". */
12732 enum cpp_ttype initialization_kind;
12733 bool is_direct_init = false;
12734 bool is_non_constant_init;
12735 int ctor_dtor_or_conv_p;
12736 bool friend_p;
12737 tree pushed_scope = NULL;
12739 /* Gather the attributes that were provided with the
12740 decl-specifiers. */
12741 prefix_attributes = decl_specifiers->attributes;
12743 /* Assume that this is not the declarator for a function
12744 definition. */
12745 if (function_definition_p)
12746 *function_definition_p = false;
12748 /* Defer access checks while parsing the declarator; we cannot know
12749 what names are accessible until we know what is being
12750 declared. */
12751 resume_deferring_access_checks ();
12753 /* Parse the declarator. */
12754 token = cp_lexer_peek_token (parser->lexer);
12755 declarator
12756 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12757 &ctor_dtor_or_conv_p,
12758 /*parenthesized_p=*/NULL,
12759 /*member_p=*/false);
12760 /* Gather up the deferred checks. */
12761 stop_deferring_access_checks ();
12763 /* If the DECLARATOR was erroneous, there's no need to go
12764 further. */
12765 if (declarator == cp_error_declarator)
12766 return error_mark_node;
12768 /* Check that the number of template-parameter-lists is OK. */
12769 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
12770 token->location))
12771 return error_mark_node;
12773 if (declares_class_or_enum & 2)
12774 cp_parser_check_for_definition_in_return_type (declarator,
12775 decl_specifiers->type,
12776 decl_specifiers->type_location);
12778 /* Figure out what scope the entity declared by the DECLARATOR is
12779 located in. `grokdeclarator' sometimes changes the scope, so
12780 we compute it now. */
12781 scope = get_scope_of_declarator (declarator);
12783 /* If we're allowing GNU extensions, look for an asm-specification
12784 and attributes. */
12785 if (cp_parser_allow_gnu_extensions_p (parser))
12787 /* Look for an asm-specification. */
12788 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
12789 asm_specification = cp_parser_asm_specification_opt (parser);
12790 /* And attributes. */
12791 attributes_start_token = cp_lexer_peek_token (parser->lexer);
12792 attributes = cp_parser_attributes_opt (parser);
12794 else
12796 asm_specification = NULL_TREE;
12797 attributes = NULL_TREE;
12800 /* Peek at the next token. */
12801 token = cp_lexer_peek_token (parser->lexer);
12802 /* Check to see if the token indicates the start of a
12803 function-definition. */
12804 if (function_declarator_p (declarator)
12805 && cp_parser_token_starts_function_definition_p (token))
12807 if (!function_definition_allowed_p)
12809 /* If a function-definition should not appear here, issue an
12810 error message. */
12811 cp_parser_error (parser,
12812 "a function-definition is not allowed here");
12813 return error_mark_node;
12815 else
12817 location_t func_brace_location
12818 = cp_lexer_peek_token (parser->lexer)->location;
12820 /* Neither attributes nor an asm-specification are allowed
12821 on a function-definition. */
12822 if (asm_specification)
12823 error_at (asm_spec_start_token->location,
12824 "an asm-specification is not allowed "
12825 "on a function-definition");
12826 if (attributes)
12827 error_at (attributes_start_token->location,
12828 "attributes are not allowed on a function-definition");
12829 /* This is a function-definition. */
12830 *function_definition_p = true;
12832 /* Parse the function definition. */
12833 if (member_p)
12834 decl = cp_parser_save_member_function_body (parser,
12835 decl_specifiers,
12836 declarator,
12837 prefix_attributes);
12838 else
12839 decl
12840 = (cp_parser_function_definition_from_specifiers_and_declarator
12841 (parser, decl_specifiers, prefix_attributes, declarator));
12843 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
12845 /* This is where the prologue starts... */
12846 DECL_STRUCT_FUNCTION (decl)->function_start_locus
12847 = func_brace_location;
12850 return decl;
12854 /* [dcl.dcl]
12856 Only in function declarations for constructors, destructors, and
12857 type conversions can the decl-specifier-seq be omitted.
12859 We explicitly postpone this check past the point where we handle
12860 function-definitions because we tolerate function-definitions
12861 that are missing their return types in some modes. */
12862 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
12864 cp_parser_error (parser,
12865 "expected constructor, destructor, or type conversion");
12866 return error_mark_node;
12869 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
12870 if (token->type == CPP_EQ
12871 || token->type == CPP_OPEN_PAREN
12872 || token->type == CPP_OPEN_BRACE)
12874 is_initialized = SD_INITIALIZED;
12875 initialization_kind = token->type;
12877 if (token->type == CPP_EQ
12878 && function_declarator_p (declarator))
12880 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12881 if (t2->keyword == RID_DEFAULT)
12882 is_initialized = SD_DEFAULTED;
12883 else if (t2->keyword == RID_DELETE)
12884 is_initialized = SD_DELETED;
12887 else
12889 /* If the init-declarator isn't initialized and isn't followed by a
12890 `,' or `;', it's not a valid init-declarator. */
12891 if (token->type != CPP_COMMA
12892 && token->type != CPP_SEMICOLON)
12894 cp_parser_error (parser, "expected initializer");
12895 return error_mark_node;
12897 is_initialized = SD_UNINITIALIZED;
12898 initialization_kind = CPP_EOF;
12901 /* Because start_decl has side-effects, we should only call it if we
12902 know we're going ahead. By this point, we know that we cannot
12903 possibly be looking at any other construct. */
12904 cp_parser_commit_to_tentative_parse (parser);
12906 /* If the decl specifiers were bad, issue an error now that we're
12907 sure this was intended to be a declarator. Then continue
12908 declaring the variable(s), as int, to try to cut down on further
12909 errors. */
12910 if (decl_specifiers->any_specifiers_p
12911 && decl_specifiers->type == error_mark_node)
12913 cp_parser_error (parser, "invalid type in declaration");
12914 decl_specifiers->type = integer_type_node;
12917 /* Check to see whether or not this declaration is a friend. */
12918 friend_p = cp_parser_friend_p (decl_specifiers);
12920 /* Enter the newly declared entry in the symbol table. If we're
12921 processing a declaration in a class-specifier, we wait until
12922 after processing the initializer. */
12923 if (!member_p)
12925 if (parser->in_unbraced_linkage_specification_p)
12926 decl_specifiers->storage_class = sc_extern;
12927 decl = start_decl (declarator, decl_specifiers,
12928 is_initialized, attributes, prefix_attributes,
12929 &pushed_scope);
12931 else if (scope)
12932 /* Enter the SCOPE. That way unqualified names appearing in the
12933 initializer will be looked up in SCOPE. */
12934 pushed_scope = push_scope (scope);
12936 /* Perform deferred access control checks, now that we know in which
12937 SCOPE the declared entity resides. */
12938 if (!member_p && decl)
12940 tree saved_current_function_decl = NULL_TREE;
12942 /* If the entity being declared is a function, pretend that we
12943 are in its scope. If it is a `friend', it may have access to
12944 things that would not otherwise be accessible. */
12945 if (TREE_CODE (decl) == FUNCTION_DECL)
12947 saved_current_function_decl = current_function_decl;
12948 current_function_decl = decl;
12951 /* Perform access checks for template parameters. */
12952 cp_parser_perform_template_parameter_access_checks (checks);
12954 /* Perform the access control checks for the declarator and the
12955 decl-specifiers. */
12956 perform_deferred_access_checks ();
12958 /* Restore the saved value. */
12959 if (TREE_CODE (decl) == FUNCTION_DECL)
12960 current_function_decl = saved_current_function_decl;
12963 /* Parse the initializer. */
12964 initializer = NULL_TREE;
12965 is_direct_init = false;
12966 is_non_constant_init = true;
12967 if (is_initialized)
12969 if (function_declarator_p (declarator))
12971 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
12972 if (initialization_kind == CPP_EQ)
12973 initializer = cp_parser_pure_specifier (parser);
12974 else
12976 /* If the declaration was erroneous, we don't really
12977 know what the user intended, so just silently
12978 consume the initializer. */
12979 if (decl != error_mark_node)
12980 error_at (initializer_start_token->location,
12981 "initializer provided for function");
12982 cp_parser_skip_to_closing_parenthesis (parser,
12983 /*recovering=*/true,
12984 /*or_comma=*/false,
12985 /*consume_paren=*/true);
12988 else
12989 initializer = cp_parser_initializer (parser,
12990 &is_direct_init,
12991 &is_non_constant_init);
12994 /* The old parser allows attributes to appear after a parenthesized
12995 initializer. Mark Mitchell proposed removing this functionality
12996 on the GCC mailing lists on 2002-08-13. This parser accepts the
12997 attributes -- but ignores them. */
12998 if (cp_parser_allow_gnu_extensions_p (parser)
12999 && initialization_kind == CPP_OPEN_PAREN)
13000 if (cp_parser_attributes_opt (parser))
13001 warning (OPT_Wattributes,
13002 "attributes after parenthesized initializer ignored");
13004 /* For an in-class declaration, use `grokfield' to create the
13005 declaration. */
13006 if (member_p)
13008 if (pushed_scope)
13010 pop_scope (pushed_scope);
13011 pushed_scope = false;
13013 decl = grokfield (declarator, decl_specifiers,
13014 initializer, !is_non_constant_init,
13015 /*asmspec=*/NULL_TREE,
13016 prefix_attributes);
13017 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
13018 cp_parser_save_default_args (parser, decl);
13021 /* Finish processing the declaration. But, skip friend
13022 declarations. */
13023 if (!friend_p && decl && decl != error_mark_node)
13025 cp_finish_decl (decl,
13026 initializer, !is_non_constant_init,
13027 asm_specification,
13028 /* If the initializer is in parentheses, then this is
13029 a direct-initialization, which means that an
13030 `explicit' constructor is OK. Otherwise, an
13031 `explicit' constructor cannot be used. */
13032 ((is_direct_init || !is_initialized)
13033 ? 0 : LOOKUP_ONLYCONVERTING));
13035 else if ((cxx_dialect != cxx98) && friend_p
13036 && decl && TREE_CODE (decl) == FUNCTION_DECL)
13037 /* Core issue #226 (C++0x only): A default template-argument
13038 shall not be specified in a friend class template
13039 declaration. */
13040 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
13041 /*is_partial=*/0, /*is_friend_decl=*/1);
13043 if (!friend_p && pushed_scope)
13044 pop_scope (pushed_scope);
13046 return decl;
13049 /* Parse a declarator.
13051 declarator:
13052 direct-declarator
13053 ptr-operator declarator
13055 abstract-declarator:
13056 ptr-operator abstract-declarator [opt]
13057 direct-abstract-declarator
13059 GNU Extensions:
13061 declarator:
13062 attributes [opt] direct-declarator
13063 attributes [opt] ptr-operator declarator
13065 abstract-declarator:
13066 attributes [opt] ptr-operator abstract-declarator [opt]
13067 attributes [opt] direct-abstract-declarator
13069 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
13070 detect constructor, destructor or conversion operators. It is set
13071 to -1 if the declarator is a name, and +1 if it is a
13072 function. Otherwise it is set to zero. Usually you just want to
13073 test for >0, but internally the negative value is used.
13075 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
13076 a decl-specifier-seq unless it declares a constructor, destructor,
13077 or conversion. It might seem that we could check this condition in
13078 semantic analysis, rather than parsing, but that makes it difficult
13079 to handle something like `f()'. We want to notice that there are
13080 no decl-specifiers, and therefore realize that this is an
13081 expression, not a declaration.)
13083 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
13084 the declarator is a direct-declarator of the form "(...)".
13086 MEMBER_P is true iff this declarator is a member-declarator. */
13088 static cp_declarator *
13089 cp_parser_declarator (cp_parser* parser,
13090 cp_parser_declarator_kind dcl_kind,
13091 int* ctor_dtor_or_conv_p,
13092 bool* parenthesized_p,
13093 bool member_p)
13095 cp_token *token;
13096 cp_declarator *declarator;
13097 enum tree_code code;
13098 cp_cv_quals cv_quals;
13099 tree class_type;
13100 tree attributes = NULL_TREE;
13102 /* Assume this is not a constructor, destructor, or type-conversion
13103 operator. */
13104 if (ctor_dtor_or_conv_p)
13105 *ctor_dtor_or_conv_p = 0;
13107 if (cp_parser_allow_gnu_extensions_p (parser))
13108 attributes = cp_parser_attributes_opt (parser);
13110 /* Peek at the next token. */
13111 token = cp_lexer_peek_token (parser->lexer);
13113 /* Check for the ptr-operator production. */
13114 cp_parser_parse_tentatively (parser);
13115 /* Parse the ptr-operator. */
13116 code = cp_parser_ptr_operator (parser,
13117 &class_type,
13118 &cv_quals);
13119 /* If that worked, then we have a ptr-operator. */
13120 if (cp_parser_parse_definitely (parser))
13122 /* If a ptr-operator was found, then this declarator was not
13123 parenthesized. */
13124 if (parenthesized_p)
13125 *parenthesized_p = true;
13126 /* The dependent declarator is optional if we are parsing an
13127 abstract-declarator. */
13128 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13129 cp_parser_parse_tentatively (parser);
13131 /* Parse the dependent declarator. */
13132 declarator = cp_parser_declarator (parser, dcl_kind,
13133 /*ctor_dtor_or_conv_p=*/NULL,
13134 /*parenthesized_p=*/NULL,
13135 /*member_p=*/false);
13137 /* If we are parsing an abstract-declarator, we must handle the
13138 case where the dependent declarator is absent. */
13139 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
13140 && !cp_parser_parse_definitely (parser))
13141 declarator = NULL;
13143 declarator = cp_parser_make_indirect_declarator
13144 (code, class_type, cv_quals, declarator);
13146 /* Everything else is a direct-declarator. */
13147 else
13149 if (parenthesized_p)
13150 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
13151 CPP_OPEN_PAREN);
13152 declarator = cp_parser_direct_declarator (parser, dcl_kind,
13153 ctor_dtor_or_conv_p,
13154 member_p);
13157 if (attributes && declarator && declarator != cp_error_declarator)
13158 declarator->attributes = attributes;
13160 return declarator;
13163 /* Parse a direct-declarator or direct-abstract-declarator.
13165 direct-declarator:
13166 declarator-id
13167 direct-declarator ( parameter-declaration-clause )
13168 cv-qualifier-seq [opt]
13169 exception-specification [opt]
13170 direct-declarator [ constant-expression [opt] ]
13171 ( declarator )
13173 direct-abstract-declarator:
13174 direct-abstract-declarator [opt]
13175 ( parameter-declaration-clause )
13176 cv-qualifier-seq [opt]
13177 exception-specification [opt]
13178 direct-abstract-declarator [opt] [ constant-expression [opt] ]
13179 ( abstract-declarator )
13181 Returns a representation of the declarator. DCL_KIND is
13182 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
13183 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
13184 we are parsing a direct-declarator. It is
13185 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
13186 of ambiguity we prefer an abstract declarator, as per
13187 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
13188 cp_parser_declarator. */
13190 static cp_declarator *
13191 cp_parser_direct_declarator (cp_parser* parser,
13192 cp_parser_declarator_kind dcl_kind,
13193 int* ctor_dtor_or_conv_p,
13194 bool member_p)
13196 cp_token *token;
13197 cp_declarator *declarator = NULL;
13198 tree scope = NULL_TREE;
13199 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13200 bool saved_in_declarator_p = parser->in_declarator_p;
13201 bool first = true;
13202 tree pushed_scope = NULL_TREE;
13204 while (true)
13206 /* Peek at the next token. */
13207 token = cp_lexer_peek_token (parser->lexer);
13208 if (token->type == CPP_OPEN_PAREN)
13210 /* This is either a parameter-declaration-clause, or a
13211 parenthesized declarator. When we know we are parsing a
13212 named declarator, it must be a parenthesized declarator
13213 if FIRST is true. For instance, `(int)' is a
13214 parameter-declaration-clause, with an omitted
13215 direct-abstract-declarator. But `((*))', is a
13216 parenthesized abstract declarator. Finally, when T is a
13217 template parameter `(T)' is a
13218 parameter-declaration-clause, and not a parenthesized
13219 named declarator.
13221 We first try and parse a parameter-declaration-clause,
13222 and then try a nested declarator (if FIRST is true).
13224 It is not an error for it not to be a
13225 parameter-declaration-clause, even when FIRST is
13226 false. Consider,
13228 int i (int);
13229 int i (3);
13231 The first is the declaration of a function while the
13232 second is the definition of a variable, including its
13233 initializer.
13235 Having seen only the parenthesis, we cannot know which of
13236 these two alternatives should be selected. Even more
13237 complex are examples like:
13239 int i (int (a));
13240 int i (int (3));
13242 The former is a function-declaration; the latter is a
13243 variable initialization.
13245 Thus again, we try a parameter-declaration-clause, and if
13246 that fails, we back out and return. */
13248 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13250 tree params;
13251 unsigned saved_num_template_parameter_lists;
13252 bool is_declarator = false;
13253 tree t;
13255 /* In a member-declarator, the only valid interpretation
13256 of a parenthesis is the start of a
13257 parameter-declaration-clause. (It is invalid to
13258 initialize a static data member with a parenthesized
13259 initializer; only the "=" form of initialization is
13260 permitted.) */
13261 if (!member_p)
13262 cp_parser_parse_tentatively (parser);
13264 /* Consume the `('. */
13265 cp_lexer_consume_token (parser->lexer);
13266 if (first)
13268 /* If this is going to be an abstract declarator, we're
13269 in a declarator and we can't have default args. */
13270 parser->default_arg_ok_p = false;
13271 parser->in_declarator_p = true;
13274 /* Inside the function parameter list, surrounding
13275 template-parameter-lists do not apply. */
13276 saved_num_template_parameter_lists
13277 = parser->num_template_parameter_lists;
13278 parser->num_template_parameter_lists = 0;
13280 begin_scope (sk_function_parms, NULL_TREE);
13282 /* Parse the parameter-declaration-clause. */
13283 params = cp_parser_parameter_declaration_clause (parser);
13285 parser->num_template_parameter_lists
13286 = saved_num_template_parameter_lists;
13288 /* If all went well, parse the cv-qualifier-seq and the
13289 exception-specification. */
13290 if (member_p || cp_parser_parse_definitely (parser))
13292 cp_cv_quals cv_quals;
13293 tree exception_specification;
13294 tree late_return;
13296 is_declarator = true;
13298 if (ctor_dtor_or_conv_p)
13299 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
13300 first = false;
13301 /* Consume the `)'. */
13302 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
13304 /* Parse the cv-qualifier-seq. */
13305 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13306 /* And the exception-specification. */
13307 exception_specification
13308 = cp_parser_exception_specification_opt (parser);
13310 late_return
13311 = cp_parser_late_return_type_opt (parser);
13313 /* Create the function-declarator. */
13314 declarator = make_call_declarator (declarator,
13315 params,
13316 cv_quals,
13317 exception_specification,
13318 late_return);
13319 /* Any subsequent parameter lists are to do with
13320 return type, so are not those of the declared
13321 function. */
13322 parser->default_arg_ok_p = false;
13325 /* Remove the function parms from scope. */
13326 for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
13327 pop_binding (DECL_NAME (t), t);
13328 leave_scope();
13330 if (is_declarator)
13331 /* Repeat the main loop. */
13332 continue;
13335 /* If this is the first, we can try a parenthesized
13336 declarator. */
13337 if (first)
13339 bool saved_in_type_id_in_expr_p;
13341 parser->default_arg_ok_p = saved_default_arg_ok_p;
13342 parser->in_declarator_p = saved_in_declarator_p;
13344 /* Consume the `('. */
13345 cp_lexer_consume_token (parser->lexer);
13346 /* Parse the nested declarator. */
13347 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
13348 parser->in_type_id_in_expr_p = true;
13349 declarator
13350 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
13351 /*parenthesized_p=*/NULL,
13352 member_p);
13353 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
13354 first = false;
13355 /* Expect a `)'. */
13356 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
13357 declarator = cp_error_declarator;
13358 if (declarator == cp_error_declarator)
13359 break;
13361 goto handle_declarator;
13363 /* Otherwise, we must be done. */
13364 else
13365 break;
13367 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13368 && token->type == CPP_OPEN_SQUARE)
13370 /* Parse an array-declarator. */
13371 tree bounds;
13373 if (ctor_dtor_or_conv_p)
13374 *ctor_dtor_or_conv_p = 0;
13376 first = false;
13377 parser->default_arg_ok_p = false;
13378 parser->in_declarator_p = true;
13379 /* Consume the `['. */
13380 cp_lexer_consume_token (parser->lexer);
13381 /* Peek at the next token. */
13382 token = cp_lexer_peek_token (parser->lexer);
13383 /* If the next token is `]', then there is no
13384 constant-expression. */
13385 if (token->type != CPP_CLOSE_SQUARE)
13387 bool non_constant_p;
13389 bounds
13390 = cp_parser_constant_expression (parser,
13391 /*allow_non_constant=*/true,
13392 &non_constant_p);
13393 if (!non_constant_p)
13394 bounds = fold_non_dependent_expr (bounds);
13395 /* Normally, the array bound must be an integral constant
13396 expression. However, as an extension, we allow VLAs
13397 in function scopes. */
13398 else if (!parser->in_function_body)
13400 error_at (token->location,
13401 "array bound is not an integer constant");
13402 bounds = error_mark_node;
13404 else if (processing_template_decl && !error_operand_p (bounds))
13406 /* Remember this wasn't a constant-expression. */
13407 bounds = build_nop (TREE_TYPE (bounds), bounds);
13408 TREE_SIDE_EFFECTS (bounds) = 1;
13411 else
13412 bounds = NULL_TREE;
13413 /* Look for the closing `]'. */
13414 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
13416 declarator = cp_error_declarator;
13417 break;
13420 declarator = make_array_declarator (declarator, bounds);
13422 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
13425 tree qualifying_scope;
13426 tree unqualified_name;
13427 special_function_kind sfk;
13428 bool abstract_ok;
13429 bool pack_expansion_p = false;
13430 cp_token *declarator_id_start_token;
13432 /* Parse a declarator-id */
13433 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
13434 if (abstract_ok)
13436 cp_parser_parse_tentatively (parser);
13438 /* If we see an ellipsis, we should be looking at a
13439 parameter pack. */
13440 if (token->type == CPP_ELLIPSIS)
13442 /* Consume the `...' */
13443 cp_lexer_consume_token (parser->lexer);
13445 pack_expansion_p = true;
13449 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
13450 unqualified_name
13451 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
13452 qualifying_scope = parser->scope;
13453 if (abstract_ok)
13455 bool okay = false;
13457 if (!unqualified_name && pack_expansion_p)
13459 /* Check whether an error occurred. */
13460 okay = !cp_parser_error_occurred (parser);
13462 /* We already consumed the ellipsis to mark a
13463 parameter pack, but we have no way to report it,
13464 so abort the tentative parse. We will be exiting
13465 immediately anyway. */
13466 cp_parser_abort_tentative_parse (parser);
13468 else
13469 okay = cp_parser_parse_definitely (parser);
13471 if (!okay)
13472 unqualified_name = error_mark_node;
13473 else if (unqualified_name
13474 && (qualifying_scope
13475 || (TREE_CODE (unqualified_name)
13476 != IDENTIFIER_NODE)))
13478 cp_parser_error (parser, "expected unqualified-id");
13479 unqualified_name = error_mark_node;
13483 if (!unqualified_name)
13484 return NULL;
13485 if (unqualified_name == error_mark_node)
13487 declarator = cp_error_declarator;
13488 pack_expansion_p = false;
13489 declarator->parameter_pack_p = false;
13490 break;
13493 if (qualifying_scope && at_namespace_scope_p ()
13494 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
13496 /* In the declaration of a member of a template class
13497 outside of the class itself, the SCOPE will sometimes
13498 be a TYPENAME_TYPE. For example, given:
13500 template <typename T>
13501 int S<T>::R::i = 3;
13503 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
13504 this context, we must resolve S<T>::R to an ordinary
13505 type, rather than a typename type.
13507 The reason we normally avoid resolving TYPENAME_TYPEs
13508 is that a specialization of `S' might render
13509 `S<T>::R' not a type. However, if `S' is
13510 specialized, then this `i' will not be used, so there
13511 is no harm in resolving the types here. */
13512 tree type;
13514 /* Resolve the TYPENAME_TYPE. */
13515 type = resolve_typename_type (qualifying_scope,
13516 /*only_current_p=*/false);
13517 /* If that failed, the declarator is invalid. */
13518 if (TREE_CODE (type) == TYPENAME_TYPE)
13519 error_at (declarator_id_start_token->location,
13520 "%<%T::%E%> is not a type",
13521 TYPE_CONTEXT (qualifying_scope),
13522 TYPE_IDENTIFIER (qualifying_scope));
13523 qualifying_scope = type;
13526 sfk = sfk_none;
13528 if (unqualified_name)
13530 tree class_type;
13532 if (qualifying_scope
13533 && CLASS_TYPE_P (qualifying_scope))
13534 class_type = qualifying_scope;
13535 else
13536 class_type = current_class_type;
13538 if (TREE_CODE (unqualified_name) == TYPE_DECL)
13540 tree name_type = TREE_TYPE (unqualified_name);
13541 if (class_type && same_type_p (name_type, class_type))
13543 if (qualifying_scope
13544 && CLASSTYPE_USE_TEMPLATE (name_type))
13546 error_at (declarator_id_start_token->location,
13547 "invalid use of constructor as a template");
13548 inform (declarator_id_start_token->location,
13549 "use %<%T::%D%> instead of %<%T::%D%> to "
13550 "name the constructor in a qualified name",
13551 class_type,
13552 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
13553 class_type, name_type);
13554 declarator = cp_error_declarator;
13555 break;
13557 else
13558 unqualified_name = constructor_name (class_type);
13560 else
13562 /* We do not attempt to print the declarator
13563 here because we do not have enough
13564 information about its original syntactic
13565 form. */
13566 cp_parser_error (parser, "invalid declarator");
13567 declarator = cp_error_declarator;
13568 break;
13572 if (class_type)
13574 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
13575 sfk = sfk_destructor;
13576 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
13577 sfk = sfk_conversion;
13578 else if (/* There's no way to declare a constructor
13579 for an anonymous type, even if the type
13580 got a name for linkage purposes. */
13581 !TYPE_WAS_ANONYMOUS (class_type)
13582 && constructor_name_p (unqualified_name,
13583 class_type))
13585 unqualified_name = constructor_name (class_type);
13586 sfk = sfk_constructor;
13589 if (ctor_dtor_or_conv_p && sfk != sfk_none)
13590 *ctor_dtor_or_conv_p = -1;
13593 declarator = make_id_declarator (qualifying_scope,
13594 unqualified_name,
13595 sfk);
13596 declarator->id_loc = token->location;
13597 declarator->parameter_pack_p = pack_expansion_p;
13599 if (pack_expansion_p)
13600 maybe_warn_variadic_templates ();
13603 handle_declarator:;
13604 scope = get_scope_of_declarator (declarator);
13605 if (scope)
13606 /* Any names that appear after the declarator-id for a
13607 member are looked up in the containing scope. */
13608 pushed_scope = push_scope (scope);
13609 parser->in_declarator_p = true;
13610 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
13611 || (declarator && declarator->kind == cdk_id))
13612 /* Default args are only allowed on function
13613 declarations. */
13614 parser->default_arg_ok_p = saved_default_arg_ok_p;
13615 else
13616 parser->default_arg_ok_p = false;
13618 first = false;
13620 /* We're done. */
13621 else
13622 break;
13625 /* For an abstract declarator, we might wind up with nothing at this
13626 point. That's an error; the declarator is not optional. */
13627 if (!declarator)
13628 cp_parser_error (parser, "expected declarator");
13630 /* If we entered a scope, we must exit it now. */
13631 if (pushed_scope)
13632 pop_scope (pushed_scope);
13634 parser->default_arg_ok_p = saved_default_arg_ok_p;
13635 parser->in_declarator_p = saved_in_declarator_p;
13637 return declarator;
13640 /* Parse a ptr-operator.
13642 ptr-operator:
13643 * cv-qualifier-seq [opt]
13645 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
13647 GNU Extension:
13649 ptr-operator:
13650 & cv-qualifier-seq [opt]
13652 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
13653 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
13654 an rvalue reference. In the case of a pointer-to-member, *TYPE is
13655 filled in with the TYPE containing the member. *CV_QUALS is
13656 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
13657 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
13658 Note that the tree codes returned by this function have nothing
13659 to do with the types of trees that will be eventually be created
13660 to represent the pointer or reference type being parsed. They are
13661 just constants with suggestive names. */
13662 static enum tree_code
13663 cp_parser_ptr_operator (cp_parser* parser,
13664 tree* type,
13665 cp_cv_quals *cv_quals)
13667 enum tree_code code = ERROR_MARK;
13668 cp_token *token;
13670 /* Assume that it's not a pointer-to-member. */
13671 *type = NULL_TREE;
13672 /* And that there are no cv-qualifiers. */
13673 *cv_quals = TYPE_UNQUALIFIED;
13675 /* Peek at the next token. */
13676 token = cp_lexer_peek_token (parser->lexer);
13678 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
13679 if (token->type == CPP_MULT)
13680 code = INDIRECT_REF;
13681 else if (token->type == CPP_AND)
13682 code = ADDR_EXPR;
13683 else if ((cxx_dialect != cxx98) &&
13684 token->type == CPP_AND_AND) /* C++0x only */
13685 code = NON_LVALUE_EXPR;
13687 if (code != ERROR_MARK)
13689 /* Consume the `*', `&' or `&&'. */
13690 cp_lexer_consume_token (parser->lexer);
13692 /* A `*' can be followed by a cv-qualifier-seq, and so can a
13693 `&', if we are allowing GNU extensions. (The only qualifier
13694 that can legally appear after `&' is `restrict', but that is
13695 enforced during semantic analysis. */
13696 if (code == INDIRECT_REF
13697 || cp_parser_allow_gnu_extensions_p (parser))
13698 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13700 else
13702 /* Try the pointer-to-member case. */
13703 cp_parser_parse_tentatively (parser);
13704 /* Look for the optional `::' operator. */
13705 cp_parser_global_scope_opt (parser,
13706 /*current_scope_valid_p=*/false);
13707 /* Look for the nested-name specifier. */
13708 token = cp_lexer_peek_token (parser->lexer);
13709 cp_parser_nested_name_specifier (parser,
13710 /*typename_keyword_p=*/false,
13711 /*check_dependency_p=*/true,
13712 /*type_p=*/false,
13713 /*is_declaration=*/false);
13714 /* If we found it, and the next token is a `*', then we are
13715 indeed looking at a pointer-to-member operator. */
13716 if (!cp_parser_error_occurred (parser)
13717 && cp_parser_require (parser, CPP_MULT, "%<*%>"))
13719 /* Indicate that the `*' operator was used. */
13720 code = INDIRECT_REF;
13722 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
13723 error_at (token->location, "%qD is a namespace", parser->scope);
13724 else
13726 /* The type of which the member is a member is given by the
13727 current SCOPE. */
13728 *type = parser->scope;
13729 /* The next name will not be qualified. */
13730 parser->scope = NULL_TREE;
13731 parser->qualifying_scope = NULL_TREE;
13732 parser->object_scope = NULL_TREE;
13733 /* Look for the optional cv-qualifier-seq. */
13734 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13737 /* If that didn't work we don't have a ptr-operator. */
13738 if (!cp_parser_parse_definitely (parser))
13739 cp_parser_error (parser, "expected ptr-operator");
13742 return code;
13745 /* Parse an (optional) cv-qualifier-seq.
13747 cv-qualifier-seq:
13748 cv-qualifier cv-qualifier-seq [opt]
13750 cv-qualifier:
13751 const
13752 volatile
13754 GNU Extension:
13756 cv-qualifier:
13757 __restrict__
13759 Returns a bitmask representing the cv-qualifiers. */
13761 static cp_cv_quals
13762 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
13764 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
13766 while (true)
13768 cp_token *token;
13769 cp_cv_quals cv_qualifier;
13771 /* Peek at the next token. */
13772 token = cp_lexer_peek_token (parser->lexer);
13773 /* See if it's a cv-qualifier. */
13774 switch (token->keyword)
13776 case RID_CONST:
13777 cv_qualifier = TYPE_QUAL_CONST;
13778 break;
13780 case RID_VOLATILE:
13781 cv_qualifier = TYPE_QUAL_VOLATILE;
13782 break;
13784 case RID_RESTRICT:
13785 cv_qualifier = TYPE_QUAL_RESTRICT;
13786 break;
13788 default:
13789 cv_qualifier = TYPE_UNQUALIFIED;
13790 break;
13793 if (!cv_qualifier)
13794 break;
13796 if (cv_quals & cv_qualifier)
13798 error_at (token->location, "duplicate cv-qualifier");
13799 cp_lexer_purge_token (parser->lexer);
13801 else
13803 cp_lexer_consume_token (parser->lexer);
13804 cv_quals |= cv_qualifier;
13808 return cv_quals;
13811 /* Parse a late-specified return type, if any. This is not a separate
13812 non-terminal, but part of a function declarator, which looks like
13814 -> type-id
13816 Returns the type indicated by the type-id. */
13818 static tree
13819 cp_parser_late_return_type_opt (cp_parser* parser)
13821 cp_token *token;
13823 /* Peek at the next token. */
13824 token = cp_lexer_peek_token (parser->lexer);
13825 /* A late-specified return type is indicated by an initial '->'. */
13826 if (token->type != CPP_DEREF)
13827 return NULL_TREE;
13829 /* Consume the ->. */
13830 cp_lexer_consume_token (parser->lexer);
13832 return cp_parser_type_id (parser);
13835 /* Parse a declarator-id.
13837 declarator-id:
13838 id-expression
13839 :: [opt] nested-name-specifier [opt] type-name
13841 In the `id-expression' case, the value returned is as for
13842 cp_parser_id_expression if the id-expression was an unqualified-id.
13843 If the id-expression was a qualified-id, then a SCOPE_REF is
13844 returned. The first operand is the scope (either a NAMESPACE_DECL
13845 or TREE_TYPE), but the second is still just a representation of an
13846 unqualified-id. */
13848 static tree
13849 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
13851 tree id;
13852 /* The expression must be an id-expression. Assume that qualified
13853 names are the names of types so that:
13855 template <class T>
13856 int S<T>::R::i = 3;
13858 will work; we must treat `S<T>::R' as the name of a type.
13859 Similarly, assume that qualified names are templates, where
13860 required, so that:
13862 template <class T>
13863 int S<T>::R<T>::i = 3;
13865 will work, too. */
13866 id = cp_parser_id_expression (parser,
13867 /*template_keyword_p=*/false,
13868 /*check_dependency_p=*/false,
13869 /*template_p=*/NULL,
13870 /*declarator_p=*/true,
13871 optional_p);
13872 if (id && BASELINK_P (id))
13873 id = BASELINK_FUNCTIONS (id);
13874 return id;
13877 /* Parse a type-id.
13879 type-id:
13880 type-specifier-seq abstract-declarator [opt]
13882 Returns the TYPE specified. */
13884 static tree
13885 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg)
13887 cp_decl_specifier_seq type_specifier_seq;
13888 cp_declarator *abstract_declarator;
13890 /* Parse the type-specifier-seq. */
13891 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
13892 &type_specifier_seq);
13893 if (type_specifier_seq.type == error_mark_node)
13894 return error_mark_node;
13896 /* There might or might not be an abstract declarator. */
13897 cp_parser_parse_tentatively (parser);
13898 /* Look for the declarator. */
13899 abstract_declarator
13900 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
13901 /*parenthesized_p=*/NULL,
13902 /*member_p=*/false);
13903 /* Check to see if there really was a declarator. */
13904 if (!cp_parser_parse_definitely (parser))
13905 abstract_declarator = NULL;
13907 if (type_specifier_seq.type
13908 && type_uses_auto (type_specifier_seq.type))
13910 /* A type-id with type 'auto' is only ok if the abstract declarator
13911 is a function declarator with a late-specified return type. */
13912 if (abstract_declarator
13913 && abstract_declarator->kind == cdk_function
13914 && abstract_declarator->u.function.late_return_type)
13915 /* OK */;
13916 else
13918 error ("invalid use of %<auto%>");
13919 return error_mark_node;
13923 return groktypename (&type_specifier_seq, abstract_declarator,
13924 is_template_arg);
13927 static tree cp_parser_type_id (cp_parser *parser)
13929 return cp_parser_type_id_1 (parser, false);
13932 static tree cp_parser_template_type_arg (cp_parser *parser)
13934 return cp_parser_type_id_1 (parser, true);
13937 /* Parse a type-specifier-seq.
13939 type-specifier-seq:
13940 type-specifier type-specifier-seq [opt]
13942 GNU extension:
13944 type-specifier-seq:
13945 attributes type-specifier-seq [opt]
13947 If IS_CONDITION is true, we are at the start of a "condition",
13948 e.g., we've just seen "if (".
13950 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
13952 static void
13953 cp_parser_type_specifier_seq (cp_parser* parser,
13954 bool is_condition,
13955 cp_decl_specifier_seq *type_specifier_seq)
13957 bool seen_type_specifier = false;
13958 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
13959 cp_token *start_token = NULL;
13961 /* Clear the TYPE_SPECIFIER_SEQ. */
13962 clear_decl_specs (type_specifier_seq);
13964 /* Parse the type-specifiers and attributes. */
13965 while (true)
13967 tree type_specifier;
13968 bool is_cv_qualifier;
13970 /* Check for attributes first. */
13971 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
13973 type_specifier_seq->attributes =
13974 chainon (type_specifier_seq->attributes,
13975 cp_parser_attributes_opt (parser));
13976 continue;
13979 /* record the token of the beginning of the type specifier seq,
13980 for error reporting purposes*/
13981 if (!start_token)
13982 start_token = cp_lexer_peek_token (parser->lexer);
13984 /* Look for the type-specifier. */
13985 type_specifier = cp_parser_type_specifier (parser,
13986 flags,
13987 type_specifier_seq,
13988 /*is_declaration=*/false,
13989 NULL,
13990 &is_cv_qualifier);
13991 if (!type_specifier)
13993 /* If the first type-specifier could not be found, this is not a
13994 type-specifier-seq at all. */
13995 if (!seen_type_specifier)
13997 cp_parser_error (parser, "expected type-specifier");
13998 type_specifier_seq->type = error_mark_node;
13999 return;
14001 /* If subsequent type-specifiers could not be found, the
14002 type-specifier-seq is complete. */
14003 break;
14006 seen_type_specifier = true;
14007 /* The standard says that a condition can be:
14009 type-specifier-seq declarator = assignment-expression
14011 However, given:
14013 struct S {};
14014 if (int S = ...)
14016 we should treat the "S" as a declarator, not as a
14017 type-specifier. The standard doesn't say that explicitly for
14018 type-specifier-seq, but it does say that for
14019 decl-specifier-seq in an ordinary declaration. Perhaps it
14020 would be clearer just to allow a decl-specifier-seq here, and
14021 then add a semantic restriction that if any decl-specifiers
14022 that are not type-specifiers appear, the program is invalid. */
14023 if (is_condition && !is_cv_qualifier)
14024 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
14027 cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
14030 /* Parse a parameter-declaration-clause.
14032 parameter-declaration-clause:
14033 parameter-declaration-list [opt] ... [opt]
14034 parameter-declaration-list , ...
14036 Returns a representation for the parameter declarations. A return
14037 value of NULL indicates a parameter-declaration-clause consisting
14038 only of an ellipsis. */
14040 static tree
14041 cp_parser_parameter_declaration_clause (cp_parser* parser)
14043 tree parameters;
14044 cp_token *token;
14045 bool ellipsis_p;
14046 bool is_error;
14048 /* Peek at the next token. */
14049 token = cp_lexer_peek_token (parser->lexer);
14050 /* Check for trivial parameter-declaration-clauses. */
14051 if (token->type == CPP_ELLIPSIS)
14053 /* Consume the `...' token. */
14054 cp_lexer_consume_token (parser->lexer);
14055 return NULL_TREE;
14057 else if (token->type == CPP_CLOSE_PAREN)
14058 /* There are no parameters. */
14060 #ifndef NO_IMPLICIT_EXTERN_C
14061 if (in_system_header && current_class_type == NULL
14062 && current_lang_name == lang_name_c)
14063 return NULL_TREE;
14064 else
14065 #endif
14066 return void_list_node;
14068 /* Check for `(void)', too, which is a special case. */
14069 else if (token->keyword == RID_VOID
14070 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
14071 == CPP_CLOSE_PAREN))
14073 /* Consume the `void' token. */
14074 cp_lexer_consume_token (parser->lexer);
14075 /* There are no parameters. */
14076 return void_list_node;
14079 /* Parse the parameter-declaration-list. */
14080 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
14081 /* If a parse error occurred while parsing the
14082 parameter-declaration-list, then the entire
14083 parameter-declaration-clause is erroneous. */
14084 if (is_error)
14085 return NULL;
14087 /* Peek at the next token. */
14088 token = cp_lexer_peek_token (parser->lexer);
14089 /* If it's a `,', the clause should terminate with an ellipsis. */
14090 if (token->type == CPP_COMMA)
14092 /* Consume the `,'. */
14093 cp_lexer_consume_token (parser->lexer);
14094 /* Expect an ellipsis. */
14095 ellipsis_p
14096 = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
14098 /* It might also be `...' if the optional trailing `,' was
14099 omitted. */
14100 else if (token->type == CPP_ELLIPSIS)
14102 /* Consume the `...' token. */
14103 cp_lexer_consume_token (parser->lexer);
14104 /* And remember that we saw it. */
14105 ellipsis_p = true;
14107 else
14108 ellipsis_p = false;
14110 /* Finish the parameter list. */
14111 if (!ellipsis_p)
14112 parameters = chainon (parameters, void_list_node);
14114 return parameters;
14117 /* Parse a parameter-declaration-list.
14119 parameter-declaration-list:
14120 parameter-declaration
14121 parameter-declaration-list , parameter-declaration
14123 Returns a representation of the parameter-declaration-list, as for
14124 cp_parser_parameter_declaration_clause. However, the
14125 `void_list_node' is never appended to the list. Upon return,
14126 *IS_ERROR will be true iff an error occurred. */
14128 static tree
14129 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
14131 tree parameters = NULL_TREE;
14132 tree *tail = &parameters;
14133 bool saved_in_unbraced_linkage_specification_p;
14134 int index = 0;
14136 /* Assume all will go well. */
14137 *is_error = false;
14138 /* The special considerations that apply to a function within an
14139 unbraced linkage specifications do not apply to the parameters
14140 to the function. */
14141 saved_in_unbraced_linkage_specification_p
14142 = parser->in_unbraced_linkage_specification_p;
14143 parser->in_unbraced_linkage_specification_p = false;
14145 /* Look for more parameters. */
14146 while (true)
14148 cp_parameter_declarator *parameter;
14149 tree decl = error_mark_node;
14150 bool parenthesized_p;
14151 /* Parse the parameter. */
14152 parameter
14153 = cp_parser_parameter_declaration (parser,
14154 /*template_parm_p=*/false,
14155 &parenthesized_p);
14157 /* We don't know yet if the enclosing context is deprecated, so wait
14158 and warn in grokparms if appropriate. */
14159 deprecated_state = DEPRECATED_SUPPRESS;
14161 if (parameter)
14162 decl = grokdeclarator (parameter->declarator,
14163 &parameter->decl_specifiers,
14164 PARM,
14165 parameter->default_argument != NULL_TREE,
14166 &parameter->decl_specifiers.attributes);
14168 deprecated_state = DEPRECATED_NORMAL;
14170 /* If a parse error occurred parsing the parameter declaration,
14171 then the entire parameter-declaration-list is erroneous. */
14172 if (decl == error_mark_node)
14174 *is_error = true;
14175 parameters = error_mark_node;
14176 break;
14179 if (parameter->decl_specifiers.attributes)
14180 cplus_decl_attributes (&decl,
14181 parameter->decl_specifiers.attributes,
14183 if (DECL_NAME (decl))
14184 decl = pushdecl (decl);
14186 if (decl != error_mark_node)
14188 retrofit_lang_decl (decl);
14189 DECL_PARM_INDEX (decl) = ++index;
14192 /* Add the new parameter to the list. */
14193 *tail = build_tree_list (parameter->default_argument, decl);
14194 tail = &TREE_CHAIN (*tail);
14196 /* Peek at the next token. */
14197 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
14198 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
14199 /* These are for Objective-C++ */
14200 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14201 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14202 /* The parameter-declaration-list is complete. */
14203 break;
14204 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14206 cp_token *token;
14208 /* Peek at the next token. */
14209 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14210 /* If it's an ellipsis, then the list is complete. */
14211 if (token->type == CPP_ELLIPSIS)
14212 break;
14213 /* Otherwise, there must be more parameters. Consume the
14214 `,'. */
14215 cp_lexer_consume_token (parser->lexer);
14216 /* When parsing something like:
14218 int i(float f, double d)
14220 we can tell after seeing the declaration for "f" that we
14221 are not looking at an initialization of a variable "i",
14222 but rather at the declaration of a function "i".
14224 Due to the fact that the parsing of template arguments
14225 (as specified to a template-id) requires backtracking we
14226 cannot use this technique when inside a template argument
14227 list. */
14228 if (!parser->in_template_argument_list_p
14229 && !parser->in_type_id_in_expr_p
14230 && cp_parser_uncommitted_to_tentative_parse_p (parser)
14231 /* However, a parameter-declaration of the form
14232 "foat(f)" (which is a valid declaration of a
14233 parameter "f") can also be interpreted as an
14234 expression (the conversion of "f" to "float"). */
14235 && !parenthesized_p)
14236 cp_parser_commit_to_tentative_parse (parser);
14238 else
14240 cp_parser_error (parser, "expected %<,%> or %<...%>");
14241 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14242 cp_parser_skip_to_closing_parenthesis (parser,
14243 /*recovering=*/true,
14244 /*or_comma=*/false,
14245 /*consume_paren=*/false);
14246 break;
14250 parser->in_unbraced_linkage_specification_p
14251 = saved_in_unbraced_linkage_specification_p;
14253 return parameters;
14256 /* Parse a parameter declaration.
14258 parameter-declaration:
14259 decl-specifier-seq ... [opt] declarator
14260 decl-specifier-seq declarator = assignment-expression
14261 decl-specifier-seq ... [opt] abstract-declarator [opt]
14262 decl-specifier-seq abstract-declarator [opt] = assignment-expression
14264 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
14265 declares a template parameter. (In that case, a non-nested `>'
14266 token encountered during the parsing of the assignment-expression
14267 is not interpreted as a greater-than operator.)
14269 Returns a representation of the parameter, or NULL if an error
14270 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
14271 true iff the declarator is of the form "(p)". */
14273 static cp_parameter_declarator *
14274 cp_parser_parameter_declaration (cp_parser *parser,
14275 bool template_parm_p,
14276 bool *parenthesized_p)
14278 int declares_class_or_enum;
14279 bool greater_than_is_operator_p;
14280 cp_decl_specifier_seq decl_specifiers;
14281 cp_declarator *declarator;
14282 tree default_argument;
14283 cp_token *token = NULL, *declarator_token_start = NULL;
14284 const char *saved_message;
14286 /* In a template parameter, `>' is not an operator.
14288 [temp.param]
14290 When parsing a default template-argument for a non-type
14291 template-parameter, the first non-nested `>' is taken as the end
14292 of the template parameter-list rather than a greater-than
14293 operator. */
14294 greater_than_is_operator_p = !template_parm_p;
14296 /* Type definitions may not appear in parameter types. */
14297 saved_message = parser->type_definition_forbidden_message;
14298 parser->type_definition_forbidden_message
14299 = "types may not be defined in parameter types";
14301 /* Parse the declaration-specifiers. */
14302 cp_parser_decl_specifier_seq (parser,
14303 CP_PARSER_FLAGS_NONE,
14304 &decl_specifiers,
14305 &declares_class_or_enum);
14306 /* If an error occurred, there's no reason to attempt to parse the
14307 rest of the declaration. */
14308 if (cp_parser_error_occurred (parser))
14310 parser->type_definition_forbidden_message = saved_message;
14311 return NULL;
14314 /* Peek at the next token. */
14315 token = cp_lexer_peek_token (parser->lexer);
14317 /* If the next token is a `)', `,', `=', `>', or `...', then there
14318 is no declarator. However, when variadic templates are enabled,
14319 there may be a declarator following `...'. */
14320 if (token->type == CPP_CLOSE_PAREN
14321 || token->type == CPP_COMMA
14322 || token->type == CPP_EQ
14323 || token->type == CPP_GREATER)
14325 declarator = NULL;
14326 if (parenthesized_p)
14327 *parenthesized_p = false;
14329 /* Otherwise, there should be a declarator. */
14330 else
14332 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14333 parser->default_arg_ok_p = false;
14335 /* After seeing a decl-specifier-seq, if the next token is not a
14336 "(", there is no possibility that the code is a valid
14337 expression. Therefore, if parsing tentatively, we commit at
14338 this point. */
14339 if (!parser->in_template_argument_list_p
14340 /* In an expression context, having seen:
14342 (int((char ...
14344 we cannot be sure whether we are looking at a
14345 function-type (taking a "char" as a parameter) or a cast
14346 of some object of type "char" to "int". */
14347 && !parser->in_type_id_in_expr_p
14348 && cp_parser_uncommitted_to_tentative_parse_p (parser)
14349 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
14350 cp_parser_commit_to_tentative_parse (parser);
14351 /* Parse the declarator. */
14352 declarator_token_start = token;
14353 declarator = cp_parser_declarator (parser,
14354 CP_PARSER_DECLARATOR_EITHER,
14355 /*ctor_dtor_or_conv_p=*/NULL,
14356 parenthesized_p,
14357 /*member_p=*/false);
14358 parser->default_arg_ok_p = saved_default_arg_ok_p;
14359 /* After the declarator, allow more attributes. */
14360 decl_specifiers.attributes
14361 = chainon (decl_specifiers.attributes,
14362 cp_parser_attributes_opt (parser));
14365 /* If the next token is an ellipsis, and we have not seen a
14366 declarator name, and the type of the declarator contains parameter
14367 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
14368 a parameter pack expansion expression. Otherwise, leave the
14369 ellipsis for a C-style variadic function. */
14370 token = cp_lexer_peek_token (parser->lexer);
14371 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14373 tree type = decl_specifiers.type;
14375 if (type && DECL_P (type))
14376 type = TREE_TYPE (type);
14378 if (type
14379 && TREE_CODE (type) != TYPE_PACK_EXPANSION
14380 && declarator_can_be_parameter_pack (declarator)
14381 && (!declarator || !declarator->parameter_pack_p)
14382 && uses_parameter_packs (type))
14384 /* Consume the `...'. */
14385 cp_lexer_consume_token (parser->lexer);
14386 maybe_warn_variadic_templates ();
14388 /* Build a pack expansion type */
14389 if (declarator)
14390 declarator->parameter_pack_p = true;
14391 else
14392 decl_specifiers.type = make_pack_expansion (type);
14396 /* The restriction on defining new types applies only to the type
14397 of the parameter, not to the default argument. */
14398 parser->type_definition_forbidden_message = saved_message;
14400 /* If the next token is `=', then process a default argument. */
14401 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14403 /* Consume the `='. */
14404 cp_lexer_consume_token (parser->lexer);
14406 /* If we are defining a class, then the tokens that make up the
14407 default argument must be saved and processed later. */
14408 if (!template_parm_p && at_class_scope_p ()
14409 && TYPE_BEING_DEFINED (current_class_type))
14411 unsigned depth = 0;
14412 int maybe_template_id = 0;
14413 cp_token *first_token;
14414 cp_token *token;
14416 /* Add tokens until we have processed the entire default
14417 argument. We add the range [first_token, token). */
14418 first_token = cp_lexer_peek_token (parser->lexer);
14419 while (true)
14421 bool done = false;
14423 /* Peek at the next token. */
14424 token = cp_lexer_peek_token (parser->lexer);
14425 /* What we do depends on what token we have. */
14426 switch (token->type)
14428 /* In valid code, a default argument must be
14429 immediately followed by a `,' `)', or `...'. */
14430 case CPP_COMMA:
14431 if (depth == 0 && maybe_template_id)
14433 /* If we've seen a '<', we might be in a
14434 template-argument-list. Until Core issue 325 is
14435 resolved, we don't know how this situation ought
14436 to be handled, so try to DTRT. We check whether
14437 what comes after the comma is a valid parameter
14438 declaration list. If it is, then the comma ends
14439 the default argument; otherwise the default
14440 argument continues. */
14441 bool error = false;
14443 /* Set ITALP so cp_parser_parameter_declaration_list
14444 doesn't decide to commit to this parse. */
14445 bool saved_italp = parser->in_template_argument_list_p;
14446 parser->in_template_argument_list_p = true;
14448 cp_parser_parse_tentatively (parser);
14449 cp_lexer_consume_token (parser->lexer);
14450 cp_parser_parameter_declaration_list (parser, &error);
14451 if (!cp_parser_error_occurred (parser) && !error)
14452 done = true;
14453 cp_parser_abort_tentative_parse (parser);
14455 parser->in_template_argument_list_p = saved_italp;
14456 break;
14458 case CPP_CLOSE_PAREN:
14459 case CPP_ELLIPSIS:
14460 /* If we run into a non-nested `;', `}', or `]',
14461 then the code is invalid -- but the default
14462 argument is certainly over. */
14463 case CPP_SEMICOLON:
14464 case CPP_CLOSE_BRACE:
14465 case CPP_CLOSE_SQUARE:
14466 if (depth == 0)
14467 done = true;
14468 /* Update DEPTH, if necessary. */
14469 else if (token->type == CPP_CLOSE_PAREN
14470 || token->type == CPP_CLOSE_BRACE
14471 || token->type == CPP_CLOSE_SQUARE)
14472 --depth;
14473 break;
14475 case CPP_OPEN_PAREN:
14476 case CPP_OPEN_SQUARE:
14477 case CPP_OPEN_BRACE:
14478 ++depth;
14479 break;
14481 case CPP_LESS:
14482 if (depth == 0)
14483 /* This might be the comparison operator, or it might
14484 start a template argument list. */
14485 ++maybe_template_id;
14486 break;
14488 case CPP_RSHIFT:
14489 if (cxx_dialect == cxx98)
14490 break;
14491 /* Fall through for C++0x, which treats the `>>'
14492 operator like two `>' tokens in certain
14493 cases. */
14495 case CPP_GREATER:
14496 if (depth == 0)
14498 /* This might be an operator, or it might close a
14499 template argument list. But if a previous '<'
14500 started a template argument list, this will have
14501 closed it, so we can't be in one anymore. */
14502 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
14503 if (maybe_template_id < 0)
14504 maybe_template_id = 0;
14506 break;
14508 /* If we run out of tokens, issue an error message. */
14509 case CPP_EOF:
14510 case CPP_PRAGMA_EOL:
14511 error_at (token->location, "file ends in default argument");
14512 done = true;
14513 break;
14515 case CPP_NAME:
14516 case CPP_SCOPE:
14517 /* In these cases, we should look for template-ids.
14518 For example, if the default argument is
14519 `X<int, double>()', we need to do name lookup to
14520 figure out whether or not `X' is a template; if
14521 so, the `,' does not end the default argument.
14523 That is not yet done. */
14524 break;
14526 default:
14527 break;
14530 /* If we've reached the end, stop. */
14531 if (done)
14532 break;
14534 /* Add the token to the token block. */
14535 token = cp_lexer_consume_token (parser->lexer);
14538 /* Create a DEFAULT_ARG to represent the unparsed default
14539 argument. */
14540 default_argument = make_node (DEFAULT_ARG);
14541 DEFARG_TOKENS (default_argument)
14542 = cp_token_cache_new (first_token, token);
14543 DEFARG_INSTANTIATIONS (default_argument) = NULL;
14545 /* Outside of a class definition, we can just parse the
14546 assignment-expression. */
14547 else
14549 token = cp_lexer_peek_token (parser->lexer);
14550 default_argument
14551 = cp_parser_default_argument (parser, template_parm_p);
14554 if (!parser->default_arg_ok_p)
14556 if (flag_permissive)
14557 warning (0, "deprecated use of default argument for parameter of non-function");
14558 else
14560 error_at (token->location,
14561 "default arguments are only "
14562 "permitted for function parameters");
14563 default_argument = NULL_TREE;
14566 else if ((declarator && declarator->parameter_pack_p)
14567 || (decl_specifiers.type
14568 && PACK_EXPANSION_P (decl_specifiers.type)))
14570 /* Find the name of the parameter pack. */
14571 cp_declarator *id_declarator = declarator;
14572 while (id_declarator && id_declarator->kind != cdk_id)
14573 id_declarator = id_declarator->declarator;
14575 if (id_declarator && id_declarator->kind == cdk_id)
14576 error_at (declarator_token_start->location,
14577 template_parm_p
14578 ? "template parameter pack %qD"
14579 " cannot have a default argument"
14580 : "parameter pack %qD cannot have a default argument",
14581 id_declarator->u.id.unqualified_name);
14582 else
14583 error_at (declarator_token_start->location,
14584 template_parm_p
14585 ? "template parameter pack cannot have a default argument"
14586 : "parameter pack cannot have a default argument");
14588 default_argument = NULL_TREE;
14591 else
14592 default_argument = NULL_TREE;
14594 return make_parameter_declarator (&decl_specifiers,
14595 declarator,
14596 default_argument);
14599 /* Parse a default argument and return it.
14601 TEMPLATE_PARM_P is true if this is a default argument for a
14602 non-type template parameter. */
14603 static tree
14604 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
14606 tree default_argument = NULL_TREE;
14607 bool saved_greater_than_is_operator_p;
14608 bool saved_local_variables_forbidden_p;
14610 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
14611 set correctly. */
14612 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
14613 parser->greater_than_is_operator_p = !template_parm_p;
14614 /* Local variable names (and the `this' keyword) may not
14615 appear in a default argument. */
14616 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14617 parser->local_variables_forbidden_p = true;
14618 /* Parse the assignment-expression. */
14619 if (template_parm_p)
14620 push_deferring_access_checks (dk_no_deferred);
14621 default_argument
14622 = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
14623 if (template_parm_p)
14624 pop_deferring_access_checks ();
14625 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
14626 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14628 return default_argument;
14631 /* Parse a function-body.
14633 function-body:
14634 compound_statement */
14636 static void
14637 cp_parser_function_body (cp_parser *parser)
14639 cp_parser_compound_statement (parser, NULL, false);
14642 /* Parse a ctor-initializer-opt followed by a function-body. Return
14643 true if a ctor-initializer was present. */
14645 static bool
14646 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
14648 tree body;
14649 bool ctor_initializer_p;
14651 /* Begin the function body. */
14652 body = begin_function_body ();
14653 /* Parse the optional ctor-initializer. */
14654 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
14655 /* Parse the function-body. */
14656 cp_parser_function_body (parser);
14657 /* Finish the function body. */
14658 finish_function_body (body);
14660 return ctor_initializer_p;
14663 /* Parse an initializer.
14665 initializer:
14666 = initializer-clause
14667 ( expression-list )
14669 Returns an expression representing the initializer. If no
14670 initializer is present, NULL_TREE is returned.
14672 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
14673 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
14674 set to TRUE if there is no initializer present. If there is an
14675 initializer, and it is not a constant-expression, *NON_CONSTANT_P
14676 is set to true; otherwise it is set to false. */
14678 static tree
14679 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
14680 bool* non_constant_p)
14682 cp_token *token;
14683 tree init;
14685 /* Peek at the next token. */
14686 token = cp_lexer_peek_token (parser->lexer);
14688 /* Let our caller know whether or not this initializer was
14689 parenthesized. */
14690 *is_direct_init = (token->type != CPP_EQ);
14691 /* Assume that the initializer is constant. */
14692 *non_constant_p = false;
14694 if (token->type == CPP_EQ)
14696 /* Consume the `='. */
14697 cp_lexer_consume_token (parser->lexer);
14698 /* Parse the initializer-clause. */
14699 init = cp_parser_initializer_clause (parser, non_constant_p);
14701 else if (token->type == CPP_OPEN_PAREN)
14703 VEC(tree,gc) *vec;
14704 vec = cp_parser_parenthesized_expression_list (parser, false,
14705 /*cast_p=*/false,
14706 /*allow_expansion_p=*/true,
14707 non_constant_p);
14708 if (vec == NULL)
14709 return error_mark_node;
14710 init = build_tree_list_vec (vec);
14711 release_tree_vector (vec);
14713 else if (token->type == CPP_OPEN_BRACE)
14715 maybe_warn_cpp0x ("extended initializer lists");
14716 init = cp_parser_braced_list (parser, non_constant_p);
14717 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
14719 else
14721 /* Anything else is an error. */
14722 cp_parser_error (parser, "expected initializer");
14723 init = error_mark_node;
14726 return init;
14729 /* Parse an initializer-clause.
14731 initializer-clause:
14732 assignment-expression
14733 braced-init-list
14735 Returns an expression representing the initializer.
14737 If the `assignment-expression' production is used the value
14738 returned is simply a representation for the expression.
14740 Otherwise, calls cp_parser_braced_list. */
14742 static tree
14743 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
14745 tree initializer;
14747 /* Assume the expression is constant. */
14748 *non_constant_p = false;
14750 /* If it is not a `{', then we are looking at an
14751 assignment-expression. */
14752 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14754 initializer
14755 = cp_parser_constant_expression (parser,
14756 /*allow_non_constant_p=*/true,
14757 non_constant_p);
14758 if (!*non_constant_p)
14759 initializer = fold_non_dependent_expr (initializer);
14761 else
14762 initializer = cp_parser_braced_list (parser, non_constant_p);
14764 return initializer;
14767 /* Parse a brace-enclosed initializer list.
14769 braced-init-list:
14770 { initializer-list , [opt] }
14773 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
14774 the elements of the initializer-list (or NULL, if the last
14775 production is used). The TREE_TYPE for the CONSTRUCTOR will be
14776 NULL_TREE. There is no way to detect whether or not the optional
14777 trailing `,' was provided. NON_CONSTANT_P is as for
14778 cp_parser_initializer. */
14780 static tree
14781 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
14783 tree initializer;
14785 /* Consume the `{' token. */
14786 cp_lexer_consume_token (parser->lexer);
14787 /* Create a CONSTRUCTOR to represent the braced-initializer. */
14788 initializer = make_node (CONSTRUCTOR);
14789 /* If it's not a `}', then there is a non-trivial initializer. */
14790 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14792 /* Parse the initializer list. */
14793 CONSTRUCTOR_ELTS (initializer)
14794 = cp_parser_initializer_list (parser, non_constant_p);
14795 /* A trailing `,' token is allowed. */
14796 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14797 cp_lexer_consume_token (parser->lexer);
14799 /* Now, there should be a trailing `}'. */
14800 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14801 TREE_TYPE (initializer) = init_list_type_node;
14802 return initializer;
14805 /* Parse an initializer-list.
14807 initializer-list:
14808 initializer-clause ... [opt]
14809 initializer-list , initializer-clause ... [opt]
14811 GNU Extension:
14813 initializer-list:
14814 identifier : initializer-clause
14815 initializer-list, identifier : initializer-clause
14817 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
14818 for the initializer. If the INDEX of the elt is non-NULL, it is the
14819 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
14820 as for cp_parser_initializer. */
14822 static VEC(constructor_elt,gc) *
14823 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
14825 VEC(constructor_elt,gc) *v = NULL;
14827 /* Assume all of the expressions are constant. */
14828 *non_constant_p = false;
14830 /* Parse the rest of the list. */
14831 while (true)
14833 cp_token *token;
14834 tree identifier;
14835 tree initializer;
14836 bool clause_non_constant_p;
14838 /* If the next token is an identifier and the following one is a
14839 colon, we are looking at the GNU designated-initializer
14840 syntax. */
14841 if (cp_parser_allow_gnu_extensions_p (parser)
14842 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
14843 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
14845 /* Warn the user that they are using an extension. */
14846 pedwarn (input_location, OPT_pedantic,
14847 "ISO C++ does not allow designated initializers");
14848 /* Consume the identifier. */
14849 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
14850 /* Consume the `:'. */
14851 cp_lexer_consume_token (parser->lexer);
14853 else
14854 identifier = NULL_TREE;
14856 /* Parse the initializer. */
14857 initializer = cp_parser_initializer_clause (parser,
14858 &clause_non_constant_p);
14859 /* If any clause is non-constant, so is the entire initializer. */
14860 if (clause_non_constant_p)
14861 *non_constant_p = true;
14863 /* If we have an ellipsis, this is an initializer pack
14864 expansion. */
14865 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14867 /* Consume the `...'. */
14868 cp_lexer_consume_token (parser->lexer);
14870 /* Turn the initializer into an initializer expansion. */
14871 initializer = make_pack_expansion (initializer);
14874 /* Add it to the vector. */
14875 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
14877 /* If the next token is not a comma, we have reached the end of
14878 the list. */
14879 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14880 break;
14882 /* Peek at the next token. */
14883 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14884 /* If the next token is a `}', then we're still done. An
14885 initializer-clause can have a trailing `,' after the
14886 initializer-list and before the closing `}'. */
14887 if (token->type == CPP_CLOSE_BRACE)
14888 break;
14890 /* Consume the `,' token. */
14891 cp_lexer_consume_token (parser->lexer);
14894 return v;
14897 /* Classes [gram.class] */
14899 /* Parse a class-name.
14901 class-name:
14902 identifier
14903 template-id
14905 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
14906 to indicate that names looked up in dependent types should be
14907 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
14908 keyword has been used to indicate that the name that appears next
14909 is a template. TAG_TYPE indicates the explicit tag given before
14910 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
14911 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
14912 is the class being defined in a class-head.
14914 Returns the TYPE_DECL representing the class. */
14916 static tree
14917 cp_parser_class_name (cp_parser *parser,
14918 bool typename_keyword_p,
14919 bool template_keyword_p,
14920 enum tag_types tag_type,
14921 bool check_dependency_p,
14922 bool class_head_p,
14923 bool is_declaration)
14925 tree decl;
14926 tree scope;
14927 bool typename_p;
14928 cp_token *token;
14929 tree identifier = NULL_TREE;
14931 /* All class-names start with an identifier. */
14932 token = cp_lexer_peek_token (parser->lexer);
14933 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
14935 cp_parser_error (parser, "expected class-name");
14936 return error_mark_node;
14939 /* PARSER->SCOPE can be cleared when parsing the template-arguments
14940 to a template-id, so we save it here. */
14941 scope = parser->scope;
14942 if (scope == error_mark_node)
14943 return error_mark_node;
14945 /* Any name names a type if we're following the `typename' keyword
14946 in a qualified name where the enclosing scope is type-dependent. */
14947 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
14948 && dependent_type_p (scope));
14949 /* Handle the common case (an identifier, but not a template-id)
14950 efficiently. */
14951 if (token->type == CPP_NAME
14952 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
14954 cp_token *identifier_token;
14955 bool ambiguous_p;
14957 /* Look for the identifier. */
14958 identifier_token = cp_lexer_peek_token (parser->lexer);
14959 ambiguous_p = identifier_token->ambiguous_p;
14960 identifier = cp_parser_identifier (parser);
14961 /* If the next token isn't an identifier, we are certainly not
14962 looking at a class-name. */
14963 if (identifier == error_mark_node)
14964 decl = error_mark_node;
14965 /* If we know this is a type-name, there's no need to look it
14966 up. */
14967 else if (typename_p)
14968 decl = identifier;
14969 else
14971 tree ambiguous_decls;
14972 /* If we already know that this lookup is ambiguous, then
14973 we've already issued an error message; there's no reason
14974 to check again. */
14975 if (ambiguous_p)
14977 cp_parser_simulate_error (parser);
14978 return error_mark_node;
14980 /* If the next token is a `::', then the name must be a type
14981 name.
14983 [basic.lookup.qual]
14985 During the lookup for a name preceding the :: scope
14986 resolution operator, object, function, and enumerator
14987 names are ignored. */
14988 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14989 tag_type = typename_type;
14990 /* Look up the name. */
14991 decl = cp_parser_lookup_name (parser, identifier,
14992 tag_type,
14993 /*is_template=*/false,
14994 /*is_namespace=*/false,
14995 check_dependency_p,
14996 &ambiguous_decls,
14997 identifier_token->location);
14998 if (ambiguous_decls)
15000 error_at (identifier_token->location,
15001 "reference to %qD is ambiguous", identifier);
15002 print_candidates (ambiguous_decls);
15003 if (cp_parser_parsing_tentatively (parser))
15005 identifier_token->ambiguous_p = true;
15006 cp_parser_simulate_error (parser);
15008 return error_mark_node;
15012 else
15014 /* Try a template-id. */
15015 decl = cp_parser_template_id (parser, template_keyword_p,
15016 check_dependency_p,
15017 is_declaration);
15018 if (decl == error_mark_node)
15019 return error_mark_node;
15022 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
15024 /* If this is a typename, create a TYPENAME_TYPE. */
15025 if (typename_p && decl != error_mark_node)
15027 decl = make_typename_type (scope, decl, typename_type,
15028 /*complain=*/tf_error);
15029 if (decl != error_mark_node)
15030 decl = TYPE_NAME (decl);
15033 /* Check to see that it is really the name of a class. */
15034 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15035 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
15036 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15037 /* Situations like this:
15039 template <typename T> struct A {
15040 typename T::template X<int>::I i;
15043 are problematic. Is `T::template X<int>' a class-name? The
15044 standard does not seem to be definitive, but there is no other
15045 valid interpretation of the following `::'. Therefore, those
15046 names are considered class-names. */
15048 decl = make_typename_type (scope, decl, tag_type, tf_error);
15049 if (decl != error_mark_node)
15050 decl = TYPE_NAME (decl);
15052 else if (TREE_CODE (decl) != TYPE_DECL
15053 || TREE_TYPE (decl) == error_mark_node
15054 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
15055 decl = error_mark_node;
15057 if (decl == error_mark_node)
15058 cp_parser_error (parser, "expected class-name");
15059 else if (identifier && !parser->scope)
15060 maybe_note_name_used_in_class (identifier, decl);
15062 return decl;
15065 /* Parse a class-specifier.
15067 class-specifier:
15068 class-head { member-specification [opt] }
15070 Returns the TREE_TYPE representing the class. */
15072 static tree
15073 cp_parser_class_specifier (cp_parser* parser)
15075 tree type;
15076 tree attributes = NULL_TREE;
15077 bool nested_name_specifier_p;
15078 unsigned saved_num_template_parameter_lists;
15079 bool saved_in_function_body;
15080 bool saved_in_unbraced_linkage_specification_p;
15081 tree old_scope = NULL_TREE;
15082 tree scope = NULL_TREE;
15083 tree bases;
15085 push_deferring_access_checks (dk_no_deferred);
15087 /* Parse the class-head. */
15088 type = cp_parser_class_head (parser,
15089 &nested_name_specifier_p,
15090 &attributes,
15091 &bases);
15092 /* If the class-head was a semantic disaster, skip the entire body
15093 of the class. */
15094 if (!type)
15096 cp_parser_skip_to_end_of_block_or_statement (parser);
15097 pop_deferring_access_checks ();
15098 return error_mark_node;
15101 /* Look for the `{'. */
15102 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
15104 pop_deferring_access_checks ();
15105 return error_mark_node;
15108 /* Process the base classes. If they're invalid, skip the
15109 entire class body. */
15110 if (!xref_basetypes (type, bases))
15112 /* Consuming the closing brace yields better error messages
15113 later on. */
15114 if (cp_parser_skip_to_closing_brace (parser))
15115 cp_lexer_consume_token (parser->lexer);
15116 pop_deferring_access_checks ();
15117 return error_mark_node;
15120 /* Issue an error message if type-definitions are forbidden here. */
15121 cp_parser_check_type_definition (parser);
15122 /* Remember that we are defining one more class. */
15123 ++parser->num_classes_being_defined;
15124 /* Inside the class, surrounding template-parameter-lists do not
15125 apply. */
15126 saved_num_template_parameter_lists
15127 = parser->num_template_parameter_lists;
15128 parser->num_template_parameter_lists = 0;
15129 /* We are not in a function body. */
15130 saved_in_function_body = parser->in_function_body;
15131 parser->in_function_body = false;
15132 /* We are not immediately inside an extern "lang" block. */
15133 saved_in_unbraced_linkage_specification_p
15134 = parser->in_unbraced_linkage_specification_p;
15135 parser->in_unbraced_linkage_specification_p = false;
15137 /* Start the class. */
15138 if (nested_name_specifier_p)
15140 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
15141 old_scope = push_inner_scope (scope);
15143 type = begin_class_definition (type, attributes);
15145 if (type == error_mark_node)
15146 /* If the type is erroneous, skip the entire body of the class. */
15147 cp_parser_skip_to_closing_brace (parser);
15148 else
15149 /* Parse the member-specification. */
15150 cp_parser_member_specification_opt (parser);
15152 /* Look for the trailing `}'. */
15153 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15154 /* Look for trailing attributes to apply to this class. */
15155 if (cp_parser_allow_gnu_extensions_p (parser))
15156 attributes = cp_parser_attributes_opt (parser);
15157 if (type != error_mark_node)
15158 type = finish_struct (type, attributes);
15159 if (nested_name_specifier_p)
15160 pop_inner_scope (old_scope, scope);
15161 /* If this class is not itself within the scope of another class,
15162 then we need to parse the bodies of all of the queued function
15163 definitions. Note that the queued functions defined in a class
15164 are not always processed immediately following the
15165 class-specifier for that class. Consider:
15167 struct A {
15168 struct B { void f() { sizeof (A); } };
15171 If `f' were processed before the processing of `A' were
15172 completed, there would be no way to compute the size of `A'.
15173 Note that the nesting we are interested in here is lexical --
15174 not the semantic nesting given by TYPE_CONTEXT. In particular,
15175 for:
15177 struct A { struct B; };
15178 struct A::B { void f() { } };
15180 there is no need to delay the parsing of `A::B::f'. */
15181 if (--parser->num_classes_being_defined == 0)
15183 tree queue_entry;
15184 tree fn;
15185 tree class_type = NULL_TREE;
15186 tree pushed_scope = NULL_TREE;
15188 /* In a first pass, parse default arguments to the functions.
15189 Then, in a second pass, parse the bodies of the functions.
15190 This two-phased approach handles cases like:
15192 struct S {
15193 void f() { g(); }
15194 void g(int i = 3);
15198 for (TREE_PURPOSE (parser->unparsed_functions_queues)
15199 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
15200 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
15201 TREE_PURPOSE (parser->unparsed_functions_queues)
15202 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
15204 fn = TREE_VALUE (queue_entry);
15205 /* If there are default arguments that have not yet been processed,
15206 take care of them now. */
15207 if (class_type != TREE_PURPOSE (queue_entry))
15209 if (pushed_scope)
15210 pop_scope (pushed_scope);
15211 class_type = TREE_PURPOSE (queue_entry);
15212 pushed_scope = push_scope (class_type);
15214 /* Make sure that any template parameters are in scope. */
15215 maybe_begin_member_template_processing (fn);
15216 /* Parse the default argument expressions. */
15217 cp_parser_late_parsing_default_args (parser, fn);
15218 /* Remove any template parameters from the symbol table. */
15219 maybe_end_member_template_processing ();
15221 if (pushed_scope)
15222 pop_scope (pushed_scope);
15223 /* Now parse the body of the functions. */
15224 for (TREE_VALUE (parser->unparsed_functions_queues)
15225 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
15226 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
15227 TREE_VALUE (parser->unparsed_functions_queues)
15228 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
15230 /* Figure out which function we need to process. */
15231 fn = TREE_VALUE (queue_entry);
15232 /* Parse the function. */
15233 cp_parser_late_parsing_for_member (parser, fn);
15237 /* Put back any saved access checks. */
15238 pop_deferring_access_checks ();
15240 /* Restore saved state. */
15241 parser->in_function_body = saved_in_function_body;
15242 parser->num_template_parameter_lists
15243 = saved_num_template_parameter_lists;
15244 parser->in_unbraced_linkage_specification_p
15245 = saved_in_unbraced_linkage_specification_p;
15247 return type;
15250 /* Parse a class-head.
15252 class-head:
15253 class-key identifier [opt] base-clause [opt]
15254 class-key nested-name-specifier identifier base-clause [opt]
15255 class-key nested-name-specifier [opt] template-id
15256 base-clause [opt]
15258 GNU Extensions:
15259 class-key attributes identifier [opt] base-clause [opt]
15260 class-key attributes nested-name-specifier identifier base-clause [opt]
15261 class-key attributes nested-name-specifier [opt] template-id
15262 base-clause [opt]
15264 Upon return BASES is initialized to the list of base classes (or
15265 NULL, if there are none) in the same form returned by
15266 cp_parser_base_clause.
15268 Returns the TYPE of the indicated class. Sets
15269 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
15270 involving a nested-name-specifier was used, and FALSE otherwise.
15272 Returns error_mark_node if this is not a class-head.
15274 Returns NULL_TREE if the class-head is syntactically valid, but
15275 semantically invalid in a way that means we should skip the entire
15276 body of the class. */
15278 static tree
15279 cp_parser_class_head (cp_parser* parser,
15280 bool* nested_name_specifier_p,
15281 tree *attributes_p,
15282 tree *bases)
15284 tree nested_name_specifier;
15285 enum tag_types class_key;
15286 tree id = NULL_TREE;
15287 tree type = NULL_TREE;
15288 tree attributes;
15289 bool template_id_p = false;
15290 bool qualified_p = false;
15291 bool invalid_nested_name_p = false;
15292 bool invalid_explicit_specialization_p = false;
15293 tree pushed_scope = NULL_TREE;
15294 unsigned num_templates;
15295 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
15296 /* Assume no nested-name-specifier will be present. */
15297 *nested_name_specifier_p = false;
15298 /* Assume no template parameter lists will be used in defining the
15299 type. */
15300 num_templates = 0;
15302 *bases = NULL_TREE;
15304 /* Look for the class-key. */
15305 class_key = cp_parser_class_key (parser);
15306 if (class_key == none_type)
15307 return error_mark_node;
15309 /* Parse the attributes. */
15310 attributes = cp_parser_attributes_opt (parser);
15312 /* If the next token is `::', that is invalid -- but sometimes
15313 people do try to write:
15315 struct ::S {};
15317 Handle this gracefully by accepting the extra qualifier, and then
15318 issuing an error about it later if this really is a
15319 class-head. If it turns out just to be an elaborated type
15320 specifier, remain silent. */
15321 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
15322 qualified_p = true;
15324 push_deferring_access_checks (dk_no_check);
15326 /* Determine the name of the class. Begin by looking for an
15327 optional nested-name-specifier. */
15328 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
15329 nested_name_specifier
15330 = cp_parser_nested_name_specifier_opt (parser,
15331 /*typename_keyword_p=*/false,
15332 /*check_dependency_p=*/false,
15333 /*type_p=*/false,
15334 /*is_declaration=*/false);
15335 /* If there was a nested-name-specifier, then there *must* be an
15336 identifier. */
15337 if (nested_name_specifier)
15339 type_start_token = cp_lexer_peek_token (parser->lexer);
15340 /* Although the grammar says `identifier', it really means
15341 `class-name' or `template-name'. You are only allowed to
15342 define a class that has already been declared with this
15343 syntax.
15345 The proposed resolution for Core Issue 180 says that wherever
15346 you see `class T::X' you should treat `X' as a type-name.
15348 It is OK to define an inaccessible class; for example:
15350 class A { class B; };
15351 class A::B {};
15353 We do not know if we will see a class-name, or a
15354 template-name. We look for a class-name first, in case the
15355 class-name is a template-id; if we looked for the
15356 template-name first we would stop after the template-name. */
15357 cp_parser_parse_tentatively (parser);
15358 type = cp_parser_class_name (parser,
15359 /*typename_keyword_p=*/false,
15360 /*template_keyword_p=*/false,
15361 class_type,
15362 /*check_dependency_p=*/false,
15363 /*class_head_p=*/true,
15364 /*is_declaration=*/false);
15365 /* If that didn't work, ignore the nested-name-specifier. */
15366 if (!cp_parser_parse_definitely (parser))
15368 invalid_nested_name_p = true;
15369 type_start_token = cp_lexer_peek_token (parser->lexer);
15370 id = cp_parser_identifier (parser);
15371 if (id == error_mark_node)
15372 id = NULL_TREE;
15374 /* If we could not find a corresponding TYPE, treat this
15375 declaration like an unqualified declaration. */
15376 if (type == error_mark_node)
15377 nested_name_specifier = NULL_TREE;
15378 /* Otherwise, count the number of templates used in TYPE and its
15379 containing scopes. */
15380 else
15382 tree scope;
15384 for (scope = TREE_TYPE (type);
15385 scope && TREE_CODE (scope) != NAMESPACE_DECL;
15386 scope = (TYPE_P (scope)
15387 ? TYPE_CONTEXT (scope)
15388 : DECL_CONTEXT (scope)))
15389 if (TYPE_P (scope)
15390 && CLASS_TYPE_P (scope)
15391 && CLASSTYPE_TEMPLATE_INFO (scope)
15392 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
15393 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
15394 ++num_templates;
15397 /* Otherwise, the identifier is optional. */
15398 else
15400 /* We don't know whether what comes next is a template-id,
15401 an identifier, or nothing at all. */
15402 cp_parser_parse_tentatively (parser);
15403 /* Check for a template-id. */
15404 type_start_token = cp_lexer_peek_token (parser->lexer);
15405 id = cp_parser_template_id (parser,
15406 /*template_keyword_p=*/false,
15407 /*check_dependency_p=*/true,
15408 /*is_declaration=*/true);
15409 /* If that didn't work, it could still be an identifier. */
15410 if (!cp_parser_parse_definitely (parser))
15412 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15414 type_start_token = cp_lexer_peek_token (parser->lexer);
15415 id = cp_parser_identifier (parser);
15417 else
15418 id = NULL_TREE;
15420 else
15422 template_id_p = true;
15423 ++num_templates;
15427 pop_deferring_access_checks ();
15429 if (id)
15430 cp_parser_check_for_invalid_template_id (parser, id,
15431 type_start_token->location);
15433 /* If it's not a `:' or a `{' then we can't really be looking at a
15434 class-head, since a class-head only appears as part of a
15435 class-specifier. We have to detect this situation before calling
15436 xref_tag, since that has irreversible side-effects. */
15437 if (!cp_parser_next_token_starts_class_definition_p (parser))
15439 cp_parser_error (parser, "expected %<{%> or %<:%>");
15440 return error_mark_node;
15443 /* At this point, we're going ahead with the class-specifier, even
15444 if some other problem occurs. */
15445 cp_parser_commit_to_tentative_parse (parser);
15446 /* Issue the error about the overly-qualified name now. */
15447 if (qualified_p)
15449 cp_parser_error (parser,
15450 "global qualification of class name is invalid");
15451 return error_mark_node;
15453 else if (invalid_nested_name_p)
15455 cp_parser_error (parser,
15456 "qualified name does not name a class");
15457 return error_mark_node;
15459 else if (nested_name_specifier)
15461 tree scope;
15463 /* Reject typedef-names in class heads. */
15464 if (!DECL_IMPLICIT_TYPEDEF_P (type))
15466 error_at (type_start_token->location,
15467 "invalid class name in declaration of %qD",
15468 type);
15469 type = NULL_TREE;
15470 goto done;
15473 /* Figure out in what scope the declaration is being placed. */
15474 scope = current_scope ();
15475 /* If that scope does not contain the scope in which the
15476 class was originally declared, the program is invalid. */
15477 if (scope && !is_ancestor (scope, nested_name_specifier))
15479 if (at_namespace_scope_p ())
15480 error_at (type_start_token->location,
15481 "declaration of %qD in namespace %qD which does not "
15482 "enclose %qD",
15483 type, scope, nested_name_specifier);
15484 else
15485 error_at (type_start_token->location,
15486 "declaration of %qD in %qD which does not enclose %qD",
15487 type, scope, nested_name_specifier);
15488 type = NULL_TREE;
15489 goto done;
15491 /* [dcl.meaning]
15493 A declarator-id shall not be qualified except for the
15494 definition of a ... nested class outside of its class
15495 ... [or] the definition or explicit instantiation of a
15496 class member of a namespace outside of its namespace. */
15497 if (scope == nested_name_specifier)
15499 permerror (nested_name_specifier_token_start->location,
15500 "extra qualification not allowed");
15501 nested_name_specifier = NULL_TREE;
15502 num_templates = 0;
15505 /* An explicit-specialization must be preceded by "template <>". If
15506 it is not, try to recover gracefully. */
15507 if (at_namespace_scope_p ()
15508 && parser->num_template_parameter_lists == 0
15509 && template_id_p)
15511 error_at (type_start_token->location,
15512 "an explicit specialization must be preceded by %<template <>%>");
15513 invalid_explicit_specialization_p = true;
15514 /* Take the same action that would have been taken by
15515 cp_parser_explicit_specialization. */
15516 ++parser->num_template_parameter_lists;
15517 begin_specialization ();
15519 /* There must be no "return" statements between this point and the
15520 end of this function; set "type "to the correct return value and
15521 use "goto done;" to return. */
15522 /* Make sure that the right number of template parameters were
15523 present. */
15524 if (!cp_parser_check_template_parameters (parser, num_templates,
15525 type_start_token->location,
15526 /*declarator=*/NULL))
15528 /* If something went wrong, there is no point in even trying to
15529 process the class-definition. */
15530 type = NULL_TREE;
15531 goto done;
15534 /* Look up the type. */
15535 if (template_id_p)
15537 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
15538 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
15539 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
15541 error_at (type_start_token->location,
15542 "function template %qD redeclared as a class template", id);
15543 type = error_mark_node;
15545 else
15547 type = TREE_TYPE (id);
15548 type = maybe_process_partial_specialization (type);
15550 if (nested_name_specifier)
15551 pushed_scope = push_scope (nested_name_specifier);
15553 else if (nested_name_specifier)
15555 tree class_type;
15557 /* Given:
15559 template <typename T> struct S { struct T };
15560 template <typename T> struct S<T>::T { };
15562 we will get a TYPENAME_TYPE when processing the definition of
15563 `S::T'. We need to resolve it to the actual type before we
15564 try to define it. */
15565 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
15567 class_type = resolve_typename_type (TREE_TYPE (type),
15568 /*only_current_p=*/false);
15569 if (TREE_CODE (class_type) != TYPENAME_TYPE)
15570 type = TYPE_NAME (class_type);
15571 else
15573 cp_parser_error (parser, "could not resolve typename type");
15574 type = error_mark_node;
15578 if (maybe_process_partial_specialization (TREE_TYPE (type))
15579 == error_mark_node)
15581 type = NULL_TREE;
15582 goto done;
15585 class_type = current_class_type;
15586 /* Enter the scope indicated by the nested-name-specifier. */
15587 pushed_scope = push_scope (nested_name_specifier);
15588 /* Get the canonical version of this type. */
15589 type = TYPE_MAIN_DECL (TREE_TYPE (type));
15590 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
15591 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
15593 type = push_template_decl (type);
15594 if (type == error_mark_node)
15596 type = NULL_TREE;
15597 goto done;
15601 type = TREE_TYPE (type);
15602 *nested_name_specifier_p = true;
15604 else /* The name is not a nested name. */
15606 /* If the class was unnamed, create a dummy name. */
15607 if (!id)
15608 id = make_anon_name ();
15609 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
15610 parser->num_template_parameter_lists);
15613 /* Indicate whether this class was declared as a `class' or as a
15614 `struct'. */
15615 if (TREE_CODE (type) == RECORD_TYPE)
15616 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
15617 cp_parser_check_class_key (class_key, type);
15619 /* If this type was already complete, and we see another definition,
15620 that's an error. */
15621 if (type != error_mark_node && COMPLETE_TYPE_P (type))
15623 error_at (type_start_token->location, "redefinition of %q#T",
15624 type);
15625 error_at (type_start_token->location, "previous definition of %q+#T",
15626 type);
15627 type = NULL_TREE;
15628 goto done;
15630 else if (type == error_mark_node)
15631 type = NULL_TREE;
15633 /* We will have entered the scope containing the class; the names of
15634 base classes should be looked up in that context. For example:
15636 struct A { struct B {}; struct C; };
15637 struct A::C : B {};
15639 is valid. */
15641 /* Get the list of base-classes, if there is one. */
15642 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15643 *bases = cp_parser_base_clause (parser);
15645 done:
15646 /* Leave the scope given by the nested-name-specifier. We will
15647 enter the class scope itself while processing the members. */
15648 if (pushed_scope)
15649 pop_scope (pushed_scope);
15651 if (invalid_explicit_specialization_p)
15653 end_specialization ();
15654 --parser->num_template_parameter_lists;
15656 *attributes_p = attributes;
15657 return type;
15660 /* Parse a class-key.
15662 class-key:
15663 class
15664 struct
15665 union
15667 Returns the kind of class-key specified, or none_type to indicate
15668 error. */
15670 static enum tag_types
15671 cp_parser_class_key (cp_parser* parser)
15673 cp_token *token;
15674 enum tag_types tag_type;
15676 /* Look for the class-key. */
15677 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
15678 if (!token)
15679 return none_type;
15681 /* Check to see if the TOKEN is a class-key. */
15682 tag_type = cp_parser_token_is_class_key (token);
15683 if (!tag_type)
15684 cp_parser_error (parser, "expected class-key");
15685 return tag_type;
15688 /* Parse an (optional) member-specification.
15690 member-specification:
15691 member-declaration member-specification [opt]
15692 access-specifier : member-specification [opt] */
15694 static void
15695 cp_parser_member_specification_opt (cp_parser* parser)
15697 while (true)
15699 cp_token *token;
15700 enum rid keyword;
15702 /* Peek at the next token. */
15703 token = cp_lexer_peek_token (parser->lexer);
15704 /* If it's a `}', or EOF then we've seen all the members. */
15705 if (token->type == CPP_CLOSE_BRACE
15706 || token->type == CPP_EOF
15707 || token->type == CPP_PRAGMA_EOL)
15708 break;
15710 /* See if this token is a keyword. */
15711 keyword = token->keyword;
15712 switch (keyword)
15714 case RID_PUBLIC:
15715 case RID_PROTECTED:
15716 case RID_PRIVATE:
15717 /* Consume the access-specifier. */
15718 cp_lexer_consume_token (parser->lexer);
15719 /* Remember which access-specifier is active. */
15720 current_access_specifier = token->u.value;
15721 /* Look for the `:'. */
15722 cp_parser_require (parser, CPP_COLON, "%<:%>");
15723 break;
15725 default:
15726 /* Accept #pragmas at class scope. */
15727 if (token->type == CPP_PRAGMA)
15729 cp_parser_pragma (parser, pragma_external);
15730 break;
15733 /* Otherwise, the next construction must be a
15734 member-declaration. */
15735 cp_parser_member_declaration (parser);
15740 /* Parse a member-declaration.
15742 member-declaration:
15743 decl-specifier-seq [opt] member-declarator-list [opt] ;
15744 function-definition ; [opt]
15745 :: [opt] nested-name-specifier template [opt] unqualified-id ;
15746 using-declaration
15747 template-declaration
15749 member-declarator-list:
15750 member-declarator
15751 member-declarator-list , member-declarator
15753 member-declarator:
15754 declarator pure-specifier [opt]
15755 declarator constant-initializer [opt]
15756 identifier [opt] : constant-expression
15758 GNU Extensions:
15760 member-declaration:
15761 __extension__ member-declaration
15763 member-declarator:
15764 declarator attributes [opt] pure-specifier [opt]
15765 declarator attributes [opt] constant-initializer [opt]
15766 identifier [opt] attributes [opt] : constant-expression
15768 C++0x Extensions:
15770 member-declaration:
15771 static_assert-declaration */
15773 static void
15774 cp_parser_member_declaration (cp_parser* parser)
15776 cp_decl_specifier_seq decl_specifiers;
15777 tree prefix_attributes;
15778 tree decl;
15779 int declares_class_or_enum;
15780 bool friend_p;
15781 cp_token *token = NULL;
15782 cp_token *decl_spec_token_start = NULL;
15783 cp_token *initializer_token_start = NULL;
15784 int saved_pedantic;
15786 /* Check for the `__extension__' keyword. */
15787 if (cp_parser_extension_opt (parser, &saved_pedantic))
15789 /* Recurse. */
15790 cp_parser_member_declaration (parser);
15791 /* Restore the old value of the PEDANTIC flag. */
15792 pedantic = saved_pedantic;
15794 return;
15797 /* Check for a template-declaration. */
15798 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15800 /* An explicit specialization here is an error condition, and we
15801 expect the specialization handler to detect and report this. */
15802 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15803 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
15804 cp_parser_explicit_specialization (parser);
15805 else
15806 cp_parser_template_declaration (parser, /*member_p=*/true);
15808 return;
15811 /* Check for a using-declaration. */
15812 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
15814 /* Parse the using-declaration. */
15815 cp_parser_using_declaration (parser,
15816 /*access_declaration_p=*/false);
15817 return;
15820 /* Check for @defs. */
15821 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
15823 tree ivar, member;
15824 tree ivar_chains = cp_parser_objc_defs_expression (parser);
15825 ivar = ivar_chains;
15826 while (ivar)
15828 member = ivar;
15829 ivar = TREE_CHAIN (member);
15830 TREE_CHAIN (member) = NULL_TREE;
15831 finish_member_declaration (member);
15833 return;
15836 /* If the next token is `static_assert' we have a static assertion. */
15837 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
15839 cp_parser_static_assert (parser, /*member_p=*/true);
15840 return;
15843 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
15844 return;
15846 /* Parse the decl-specifier-seq. */
15847 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
15848 cp_parser_decl_specifier_seq (parser,
15849 CP_PARSER_FLAGS_OPTIONAL,
15850 &decl_specifiers,
15851 &declares_class_or_enum);
15852 prefix_attributes = decl_specifiers.attributes;
15853 decl_specifiers.attributes = NULL_TREE;
15854 /* Check for an invalid type-name. */
15855 if (!decl_specifiers.type
15856 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
15857 return;
15858 /* If there is no declarator, then the decl-specifier-seq should
15859 specify a type. */
15860 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15862 /* If there was no decl-specifier-seq, and the next token is a
15863 `;', then we have something like:
15865 struct S { ; };
15867 [class.mem]
15869 Each member-declaration shall declare at least one member
15870 name of the class. */
15871 if (!decl_specifiers.any_specifiers_p)
15873 cp_token *token = cp_lexer_peek_token (parser->lexer);
15874 if (!in_system_header_at (token->location))
15875 pedwarn (token->location, OPT_pedantic, "extra %<;%>");
15877 else
15879 tree type;
15881 /* See if this declaration is a friend. */
15882 friend_p = cp_parser_friend_p (&decl_specifiers);
15883 /* If there were decl-specifiers, check to see if there was
15884 a class-declaration. */
15885 type = check_tag_decl (&decl_specifiers);
15886 /* Nested classes have already been added to the class, but
15887 a `friend' needs to be explicitly registered. */
15888 if (friend_p)
15890 /* If the `friend' keyword was present, the friend must
15891 be introduced with a class-key. */
15892 if (!declares_class_or_enum)
15893 error_at (decl_spec_token_start->location,
15894 "a class-key must be used when declaring a friend");
15895 /* In this case:
15897 template <typename T> struct A {
15898 friend struct A<T>::B;
15901 A<T>::B will be represented by a TYPENAME_TYPE, and
15902 therefore not recognized by check_tag_decl. */
15903 if (!type
15904 && decl_specifiers.type
15905 && TYPE_P (decl_specifiers.type))
15906 type = decl_specifiers.type;
15907 if (!type || !TYPE_P (type))
15908 error_at (decl_spec_token_start->location,
15909 "friend declaration does not name a class or "
15910 "function");
15911 else
15912 make_friend_class (current_class_type, type,
15913 /*complain=*/true);
15915 /* If there is no TYPE, an error message will already have
15916 been issued. */
15917 else if (!type || type == error_mark_node)
15919 /* An anonymous aggregate has to be handled specially; such
15920 a declaration really declares a data member (with a
15921 particular type), as opposed to a nested class. */
15922 else if (ANON_AGGR_TYPE_P (type))
15924 /* Remove constructors and such from TYPE, now that we
15925 know it is an anonymous aggregate. */
15926 fixup_anonymous_aggr (type);
15927 /* And make the corresponding data member. */
15928 decl = build_decl (decl_spec_token_start->location,
15929 FIELD_DECL, NULL_TREE, type);
15930 /* Add it to the class. */
15931 finish_member_declaration (decl);
15933 else
15934 cp_parser_check_access_in_redeclaration
15935 (TYPE_NAME (type),
15936 decl_spec_token_start->location);
15939 else
15941 /* See if these declarations will be friends. */
15942 friend_p = cp_parser_friend_p (&decl_specifiers);
15944 /* Keep going until we hit the `;' at the end of the
15945 declaration. */
15946 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15948 tree attributes = NULL_TREE;
15949 tree first_attribute;
15951 /* Peek at the next token. */
15952 token = cp_lexer_peek_token (parser->lexer);
15954 /* Check for a bitfield declaration. */
15955 if (token->type == CPP_COLON
15956 || (token->type == CPP_NAME
15957 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
15958 == CPP_COLON))
15960 tree identifier;
15961 tree width;
15963 /* Get the name of the bitfield. Note that we cannot just
15964 check TOKEN here because it may have been invalidated by
15965 the call to cp_lexer_peek_nth_token above. */
15966 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
15967 identifier = cp_parser_identifier (parser);
15968 else
15969 identifier = NULL_TREE;
15971 /* Consume the `:' token. */
15972 cp_lexer_consume_token (parser->lexer);
15973 /* Get the width of the bitfield. */
15974 width
15975 = cp_parser_constant_expression (parser,
15976 /*allow_non_constant=*/false,
15977 NULL);
15979 /* Look for attributes that apply to the bitfield. */
15980 attributes = cp_parser_attributes_opt (parser);
15981 /* Remember which attributes are prefix attributes and
15982 which are not. */
15983 first_attribute = attributes;
15984 /* Combine the attributes. */
15985 attributes = chainon (prefix_attributes, attributes);
15987 /* Create the bitfield declaration. */
15988 decl = grokbitfield (identifier
15989 ? make_id_declarator (NULL_TREE,
15990 identifier,
15991 sfk_none)
15992 : NULL,
15993 &decl_specifiers,
15994 width,
15995 attributes);
15997 else
15999 cp_declarator *declarator;
16000 tree initializer;
16001 tree asm_specification;
16002 int ctor_dtor_or_conv_p;
16004 /* Parse the declarator. */
16005 declarator
16006 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16007 &ctor_dtor_or_conv_p,
16008 /*parenthesized_p=*/NULL,
16009 /*member_p=*/true);
16011 /* If something went wrong parsing the declarator, make sure
16012 that we at least consume some tokens. */
16013 if (declarator == cp_error_declarator)
16015 /* Skip to the end of the statement. */
16016 cp_parser_skip_to_end_of_statement (parser);
16017 /* If the next token is not a semicolon, that is
16018 probably because we just skipped over the body of
16019 a function. So, we consume a semicolon if
16020 present, but do not issue an error message if it
16021 is not present. */
16022 if (cp_lexer_next_token_is (parser->lexer,
16023 CPP_SEMICOLON))
16024 cp_lexer_consume_token (parser->lexer);
16025 return;
16028 if (declares_class_or_enum & 2)
16029 cp_parser_check_for_definition_in_return_type
16030 (declarator, decl_specifiers.type,
16031 decl_specifiers.type_location);
16033 /* Look for an asm-specification. */
16034 asm_specification = cp_parser_asm_specification_opt (parser);
16035 /* Look for attributes that apply to the declaration. */
16036 attributes = cp_parser_attributes_opt (parser);
16037 /* Remember which attributes are prefix attributes and
16038 which are not. */
16039 first_attribute = attributes;
16040 /* Combine the attributes. */
16041 attributes = chainon (prefix_attributes, attributes);
16043 /* If it's an `=', then we have a constant-initializer or a
16044 pure-specifier. It is not correct to parse the
16045 initializer before registering the member declaration
16046 since the member declaration should be in scope while
16047 its initializer is processed. However, the rest of the
16048 front end does not yet provide an interface that allows
16049 us to handle this correctly. */
16050 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16052 /* In [class.mem]:
16054 A pure-specifier shall be used only in the declaration of
16055 a virtual function.
16057 A member-declarator can contain a constant-initializer
16058 only if it declares a static member of integral or
16059 enumeration type.
16061 Therefore, if the DECLARATOR is for a function, we look
16062 for a pure-specifier; otherwise, we look for a
16063 constant-initializer. When we call `grokfield', it will
16064 perform more stringent semantics checks. */
16065 initializer_token_start = cp_lexer_peek_token (parser->lexer);
16066 if (function_declarator_p (declarator))
16067 initializer = cp_parser_pure_specifier (parser);
16068 else
16069 /* Parse the initializer. */
16070 initializer = cp_parser_constant_initializer (parser);
16072 /* Otherwise, there is no initializer. */
16073 else
16074 initializer = NULL_TREE;
16076 /* See if we are probably looking at a function
16077 definition. We are certainly not looking at a
16078 member-declarator. Calling `grokfield' has
16079 side-effects, so we must not do it unless we are sure
16080 that we are looking at a member-declarator. */
16081 if (cp_parser_token_starts_function_definition_p
16082 (cp_lexer_peek_token (parser->lexer)))
16084 /* The grammar does not allow a pure-specifier to be
16085 used when a member function is defined. (It is
16086 possible that this fact is an oversight in the
16087 standard, since a pure function may be defined
16088 outside of the class-specifier. */
16089 if (initializer)
16090 error_at (initializer_token_start->location,
16091 "pure-specifier on function-definition");
16092 decl = cp_parser_save_member_function_body (parser,
16093 &decl_specifiers,
16094 declarator,
16095 attributes);
16096 /* If the member was not a friend, declare it here. */
16097 if (!friend_p)
16098 finish_member_declaration (decl);
16099 /* Peek at the next token. */
16100 token = cp_lexer_peek_token (parser->lexer);
16101 /* If the next token is a semicolon, consume it. */
16102 if (token->type == CPP_SEMICOLON)
16103 cp_lexer_consume_token (parser->lexer);
16104 return;
16106 else
16107 if (declarator->kind == cdk_function)
16108 declarator->id_loc = token->location;
16109 /* Create the declaration. */
16110 decl = grokfield (declarator, &decl_specifiers,
16111 initializer, /*init_const_expr_p=*/true,
16112 asm_specification,
16113 attributes);
16116 /* Reset PREFIX_ATTRIBUTES. */
16117 while (attributes && TREE_CHAIN (attributes) != first_attribute)
16118 attributes = TREE_CHAIN (attributes);
16119 if (attributes)
16120 TREE_CHAIN (attributes) = NULL_TREE;
16122 /* If there is any qualification still in effect, clear it
16123 now; we will be starting fresh with the next declarator. */
16124 parser->scope = NULL_TREE;
16125 parser->qualifying_scope = NULL_TREE;
16126 parser->object_scope = NULL_TREE;
16127 /* If it's a `,', then there are more declarators. */
16128 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16129 cp_lexer_consume_token (parser->lexer);
16130 /* If the next token isn't a `;', then we have a parse error. */
16131 else if (cp_lexer_next_token_is_not (parser->lexer,
16132 CPP_SEMICOLON))
16134 cp_parser_error (parser, "expected %<;%>");
16135 /* Skip tokens until we find a `;'. */
16136 cp_parser_skip_to_end_of_statement (parser);
16138 break;
16141 if (decl)
16143 /* Add DECL to the list of members. */
16144 if (!friend_p)
16145 finish_member_declaration (decl);
16147 if (TREE_CODE (decl) == FUNCTION_DECL)
16148 cp_parser_save_default_args (parser, decl);
16153 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16156 /* Parse a pure-specifier.
16158 pure-specifier:
16161 Returns INTEGER_ZERO_NODE if a pure specifier is found.
16162 Otherwise, ERROR_MARK_NODE is returned. */
16164 static tree
16165 cp_parser_pure_specifier (cp_parser* parser)
16167 cp_token *token;
16169 /* Look for the `=' token. */
16170 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16171 return error_mark_node;
16172 /* Look for the `0' token. */
16173 token = cp_lexer_peek_token (parser->lexer);
16175 if (token->type == CPP_EOF
16176 || token->type == CPP_PRAGMA_EOL)
16177 return error_mark_node;
16179 cp_lexer_consume_token (parser->lexer);
16181 /* Accept = default or = delete in c++0x mode. */
16182 if (token->keyword == RID_DEFAULT
16183 || token->keyword == RID_DELETE)
16185 maybe_warn_cpp0x ("defaulted and deleted functions");
16186 return token->u.value;
16189 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
16190 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
16192 cp_parser_error (parser,
16193 "invalid pure specifier (only %<= 0%> is allowed)");
16194 cp_parser_skip_to_end_of_statement (parser);
16195 return error_mark_node;
16197 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
16199 error_at (token->location, "templates may not be %<virtual%>");
16200 return error_mark_node;
16203 return integer_zero_node;
16206 /* Parse a constant-initializer.
16208 constant-initializer:
16209 = constant-expression
16211 Returns a representation of the constant-expression. */
16213 static tree
16214 cp_parser_constant_initializer (cp_parser* parser)
16216 /* Look for the `=' token. */
16217 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16218 return error_mark_node;
16220 /* It is invalid to write:
16222 struct S { static const int i = { 7 }; };
16225 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16227 cp_parser_error (parser,
16228 "a brace-enclosed initializer is not allowed here");
16229 /* Consume the opening brace. */
16230 cp_lexer_consume_token (parser->lexer);
16231 /* Skip the initializer. */
16232 cp_parser_skip_to_closing_brace (parser);
16233 /* Look for the trailing `}'. */
16234 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
16236 return error_mark_node;
16239 return cp_parser_constant_expression (parser,
16240 /*allow_non_constant=*/false,
16241 NULL);
16244 /* Derived classes [gram.class.derived] */
16246 /* Parse a base-clause.
16248 base-clause:
16249 : base-specifier-list
16251 base-specifier-list:
16252 base-specifier ... [opt]
16253 base-specifier-list , base-specifier ... [opt]
16255 Returns a TREE_LIST representing the base-classes, in the order in
16256 which they were declared. The representation of each node is as
16257 described by cp_parser_base_specifier.
16259 In the case that no bases are specified, this function will return
16260 NULL_TREE, not ERROR_MARK_NODE. */
16262 static tree
16263 cp_parser_base_clause (cp_parser* parser)
16265 tree bases = NULL_TREE;
16267 /* Look for the `:' that begins the list. */
16268 cp_parser_require (parser, CPP_COLON, "%<:%>");
16270 /* Scan the base-specifier-list. */
16271 while (true)
16273 cp_token *token;
16274 tree base;
16275 bool pack_expansion_p = false;
16277 /* Look for the base-specifier. */
16278 base = cp_parser_base_specifier (parser);
16279 /* Look for the (optional) ellipsis. */
16280 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16282 /* Consume the `...'. */
16283 cp_lexer_consume_token (parser->lexer);
16285 pack_expansion_p = true;
16288 /* Add BASE to the front of the list. */
16289 if (base != error_mark_node)
16291 if (pack_expansion_p)
16292 /* Make this a pack expansion type. */
16293 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
16296 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
16298 TREE_CHAIN (base) = bases;
16299 bases = base;
16302 /* Peek at the next token. */
16303 token = cp_lexer_peek_token (parser->lexer);
16304 /* If it's not a comma, then the list is complete. */
16305 if (token->type != CPP_COMMA)
16306 break;
16307 /* Consume the `,'. */
16308 cp_lexer_consume_token (parser->lexer);
16311 /* PARSER->SCOPE may still be non-NULL at this point, if the last
16312 base class had a qualified name. However, the next name that
16313 appears is certainly not qualified. */
16314 parser->scope = NULL_TREE;
16315 parser->qualifying_scope = NULL_TREE;
16316 parser->object_scope = NULL_TREE;
16318 return nreverse (bases);
16321 /* Parse a base-specifier.
16323 base-specifier:
16324 :: [opt] nested-name-specifier [opt] class-name
16325 virtual access-specifier [opt] :: [opt] nested-name-specifier
16326 [opt] class-name
16327 access-specifier virtual [opt] :: [opt] nested-name-specifier
16328 [opt] class-name
16330 Returns a TREE_LIST. The TREE_PURPOSE will be one of
16331 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
16332 indicate the specifiers provided. The TREE_VALUE will be a TYPE
16333 (or the ERROR_MARK_NODE) indicating the type that was specified. */
16335 static tree
16336 cp_parser_base_specifier (cp_parser* parser)
16338 cp_token *token;
16339 bool done = false;
16340 bool virtual_p = false;
16341 bool duplicate_virtual_error_issued_p = false;
16342 bool duplicate_access_error_issued_p = false;
16343 bool class_scope_p, template_p;
16344 tree access = access_default_node;
16345 tree type;
16347 /* Process the optional `virtual' and `access-specifier'. */
16348 while (!done)
16350 /* Peek at the next token. */
16351 token = cp_lexer_peek_token (parser->lexer);
16352 /* Process `virtual'. */
16353 switch (token->keyword)
16355 case RID_VIRTUAL:
16356 /* If `virtual' appears more than once, issue an error. */
16357 if (virtual_p && !duplicate_virtual_error_issued_p)
16359 cp_parser_error (parser,
16360 "%<virtual%> specified more than once in base-specified");
16361 duplicate_virtual_error_issued_p = true;
16364 virtual_p = true;
16366 /* Consume the `virtual' token. */
16367 cp_lexer_consume_token (parser->lexer);
16369 break;
16371 case RID_PUBLIC:
16372 case RID_PROTECTED:
16373 case RID_PRIVATE:
16374 /* If more than one access specifier appears, issue an
16375 error. */
16376 if (access != access_default_node
16377 && !duplicate_access_error_issued_p)
16379 cp_parser_error (parser,
16380 "more than one access specifier in base-specified");
16381 duplicate_access_error_issued_p = true;
16384 access = ridpointers[(int) token->keyword];
16386 /* Consume the access-specifier. */
16387 cp_lexer_consume_token (parser->lexer);
16389 break;
16391 default:
16392 done = true;
16393 break;
16396 /* It is not uncommon to see programs mechanically, erroneously, use
16397 the 'typename' keyword to denote (dependent) qualified types
16398 as base classes. */
16399 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
16401 token = cp_lexer_peek_token (parser->lexer);
16402 if (!processing_template_decl)
16403 error_at (token->location,
16404 "keyword %<typename%> not allowed outside of templates");
16405 else
16406 error_at (token->location,
16407 "keyword %<typename%> not allowed in this context "
16408 "(the base class is implicitly a type)");
16409 cp_lexer_consume_token (parser->lexer);
16412 /* Look for the optional `::' operator. */
16413 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16414 /* Look for the nested-name-specifier. The simplest way to
16415 implement:
16417 [temp.res]
16419 The keyword `typename' is not permitted in a base-specifier or
16420 mem-initializer; in these contexts a qualified name that
16421 depends on a template-parameter is implicitly assumed to be a
16422 type name.
16424 is to pretend that we have seen the `typename' keyword at this
16425 point. */
16426 cp_parser_nested_name_specifier_opt (parser,
16427 /*typename_keyword_p=*/true,
16428 /*check_dependency_p=*/true,
16429 typename_type,
16430 /*is_declaration=*/true);
16431 /* If the base class is given by a qualified name, assume that names
16432 we see are type names or templates, as appropriate. */
16433 class_scope_p = (parser->scope && TYPE_P (parser->scope));
16434 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
16436 /* Finally, look for the class-name. */
16437 type = cp_parser_class_name (parser,
16438 class_scope_p,
16439 template_p,
16440 typename_type,
16441 /*check_dependency_p=*/true,
16442 /*class_head_p=*/false,
16443 /*is_declaration=*/true);
16445 if (type == error_mark_node)
16446 return error_mark_node;
16448 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
16451 /* Exception handling [gram.exception] */
16453 /* Parse an (optional) exception-specification.
16455 exception-specification:
16456 throw ( type-id-list [opt] )
16458 Returns a TREE_LIST representing the exception-specification. The
16459 TREE_VALUE of each node is a type. */
16461 static tree
16462 cp_parser_exception_specification_opt (cp_parser* parser)
16464 cp_token *token;
16465 tree type_id_list;
16467 /* Peek at the next token. */
16468 token = cp_lexer_peek_token (parser->lexer);
16469 /* If it's not `throw', then there's no exception-specification. */
16470 if (!cp_parser_is_keyword (token, RID_THROW))
16471 return NULL_TREE;
16473 /* Consume the `throw'. */
16474 cp_lexer_consume_token (parser->lexer);
16476 /* Look for the `('. */
16477 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16479 /* Peek at the next token. */
16480 token = cp_lexer_peek_token (parser->lexer);
16481 /* If it's not a `)', then there is a type-id-list. */
16482 if (token->type != CPP_CLOSE_PAREN)
16484 const char *saved_message;
16486 /* Types may not be defined in an exception-specification. */
16487 saved_message = parser->type_definition_forbidden_message;
16488 parser->type_definition_forbidden_message
16489 = "types may not be defined in an exception-specification";
16490 /* Parse the type-id-list. */
16491 type_id_list = cp_parser_type_id_list (parser);
16492 /* Restore the saved message. */
16493 parser->type_definition_forbidden_message = saved_message;
16495 else
16496 type_id_list = empty_except_spec;
16498 /* Look for the `)'. */
16499 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16501 return type_id_list;
16504 /* Parse an (optional) type-id-list.
16506 type-id-list:
16507 type-id ... [opt]
16508 type-id-list , type-id ... [opt]
16510 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
16511 in the order that the types were presented. */
16513 static tree
16514 cp_parser_type_id_list (cp_parser* parser)
16516 tree types = NULL_TREE;
16518 while (true)
16520 cp_token *token;
16521 tree type;
16523 /* Get the next type-id. */
16524 type = cp_parser_type_id (parser);
16525 /* Parse the optional ellipsis. */
16526 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16528 /* Consume the `...'. */
16529 cp_lexer_consume_token (parser->lexer);
16531 /* Turn the type into a pack expansion expression. */
16532 type = make_pack_expansion (type);
16534 /* Add it to the list. */
16535 types = add_exception_specifier (types, type, /*complain=*/1);
16536 /* Peek at the next token. */
16537 token = cp_lexer_peek_token (parser->lexer);
16538 /* If it is not a `,', we are done. */
16539 if (token->type != CPP_COMMA)
16540 break;
16541 /* Consume the `,'. */
16542 cp_lexer_consume_token (parser->lexer);
16545 return nreverse (types);
16548 /* Parse a try-block.
16550 try-block:
16551 try compound-statement handler-seq */
16553 static tree
16554 cp_parser_try_block (cp_parser* parser)
16556 tree try_block;
16558 cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
16559 try_block = begin_try_block ();
16560 cp_parser_compound_statement (parser, NULL, true);
16561 finish_try_block (try_block);
16562 cp_parser_handler_seq (parser);
16563 finish_handler_sequence (try_block);
16565 return try_block;
16568 /* Parse a function-try-block.
16570 function-try-block:
16571 try ctor-initializer [opt] function-body handler-seq */
16573 static bool
16574 cp_parser_function_try_block (cp_parser* parser)
16576 tree compound_stmt;
16577 tree try_block;
16578 bool ctor_initializer_p;
16580 /* Look for the `try' keyword. */
16581 if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
16582 return false;
16583 /* Let the rest of the front end know where we are. */
16584 try_block = begin_function_try_block (&compound_stmt);
16585 /* Parse the function-body. */
16586 ctor_initializer_p
16587 = cp_parser_ctor_initializer_opt_and_function_body (parser);
16588 /* We're done with the `try' part. */
16589 finish_function_try_block (try_block);
16590 /* Parse the handlers. */
16591 cp_parser_handler_seq (parser);
16592 /* We're done with the handlers. */
16593 finish_function_handler_sequence (try_block, compound_stmt);
16595 return ctor_initializer_p;
16598 /* Parse a handler-seq.
16600 handler-seq:
16601 handler handler-seq [opt] */
16603 static void
16604 cp_parser_handler_seq (cp_parser* parser)
16606 while (true)
16608 cp_token *token;
16610 /* Parse the handler. */
16611 cp_parser_handler (parser);
16612 /* Peek at the next token. */
16613 token = cp_lexer_peek_token (parser->lexer);
16614 /* If it's not `catch' then there are no more handlers. */
16615 if (!cp_parser_is_keyword (token, RID_CATCH))
16616 break;
16620 /* Parse a handler.
16622 handler:
16623 catch ( exception-declaration ) compound-statement */
16625 static void
16626 cp_parser_handler (cp_parser* parser)
16628 tree handler;
16629 tree declaration;
16631 cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
16632 handler = begin_handler ();
16633 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16634 declaration = cp_parser_exception_declaration (parser);
16635 finish_handler_parms (declaration, handler);
16636 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16637 cp_parser_compound_statement (parser, NULL, false);
16638 finish_handler (handler);
16641 /* Parse an exception-declaration.
16643 exception-declaration:
16644 type-specifier-seq declarator
16645 type-specifier-seq abstract-declarator
16646 type-specifier-seq
16649 Returns a VAR_DECL for the declaration, or NULL_TREE if the
16650 ellipsis variant is used. */
16652 static tree
16653 cp_parser_exception_declaration (cp_parser* parser)
16655 cp_decl_specifier_seq type_specifiers;
16656 cp_declarator *declarator;
16657 const char *saved_message;
16659 /* If it's an ellipsis, it's easy to handle. */
16660 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16662 /* Consume the `...' token. */
16663 cp_lexer_consume_token (parser->lexer);
16664 return NULL_TREE;
16667 /* Types may not be defined in exception-declarations. */
16668 saved_message = parser->type_definition_forbidden_message;
16669 parser->type_definition_forbidden_message
16670 = "types may not be defined in exception-declarations";
16672 /* Parse the type-specifier-seq. */
16673 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
16674 &type_specifiers);
16675 /* If it's a `)', then there is no declarator. */
16676 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
16677 declarator = NULL;
16678 else
16679 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
16680 /*ctor_dtor_or_conv_p=*/NULL,
16681 /*parenthesized_p=*/NULL,
16682 /*member_p=*/false);
16684 /* Restore the saved message. */
16685 parser->type_definition_forbidden_message = saved_message;
16687 if (!type_specifiers.any_specifiers_p)
16688 return error_mark_node;
16690 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
16693 /* Parse a throw-expression.
16695 throw-expression:
16696 throw assignment-expression [opt]
16698 Returns a THROW_EXPR representing the throw-expression. */
16700 static tree
16701 cp_parser_throw_expression (cp_parser* parser)
16703 tree expression;
16704 cp_token* token;
16706 cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
16707 token = cp_lexer_peek_token (parser->lexer);
16708 /* Figure out whether or not there is an assignment-expression
16709 following the "throw" keyword. */
16710 if (token->type == CPP_COMMA
16711 || token->type == CPP_SEMICOLON
16712 || token->type == CPP_CLOSE_PAREN
16713 || token->type == CPP_CLOSE_SQUARE
16714 || token->type == CPP_CLOSE_BRACE
16715 || token->type == CPP_COLON)
16716 expression = NULL_TREE;
16717 else
16718 expression = cp_parser_assignment_expression (parser,
16719 /*cast_p=*/false, NULL);
16721 return build_throw (expression);
16724 /* GNU Extensions */
16726 /* Parse an (optional) asm-specification.
16728 asm-specification:
16729 asm ( string-literal )
16731 If the asm-specification is present, returns a STRING_CST
16732 corresponding to the string-literal. Otherwise, returns
16733 NULL_TREE. */
16735 static tree
16736 cp_parser_asm_specification_opt (cp_parser* parser)
16738 cp_token *token;
16739 tree asm_specification;
16741 /* Peek at the next token. */
16742 token = cp_lexer_peek_token (parser->lexer);
16743 /* If the next token isn't the `asm' keyword, then there's no
16744 asm-specification. */
16745 if (!cp_parser_is_keyword (token, RID_ASM))
16746 return NULL_TREE;
16748 /* Consume the `asm' token. */
16749 cp_lexer_consume_token (parser->lexer);
16750 /* Look for the `('. */
16751 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16753 /* Look for the string-literal. */
16754 asm_specification = cp_parser_string_literal (parser, false, false);
16756 /* Look for the `)'. */
16757 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16759 return asm_specification;
16762 /* Parse an asm-operand-list.
16764 asm-operand-list:
16765 asm-operand
16766 asm-operand-list , asm-operand
16768 asm-operand:
16769 string-literal ( expression )
16770 [ string-literal ] string-literal ( expression )
16772 Returns a TREE_LIST representing the operands. The TREE_VALUE of
16773 each node is the expression. The TREE_PURPOSE is itself a
16774 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
16775 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
16776 is a STRING_CST for the string literal before the parenthesis. Returns
16777 ERROR_MARK_NODE if any of the operands are invalid. */
16779 static tree
16780 cp_parser_asm_operand_list (cp_parser* parser)
16782 tree asm_operands = NULL_TREE;
16783 bool invalid_operands = false;
16785 while (true)
16787 tree string_literal;
16788 tree expression;
16789 tree name;
16791 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
16793 /* Consume the `[' token. */
16794 cp_lexer_consume_token (parser->lexer);
16795 /* Read the operand name. */
16796 name = cp_parser_identifier (parser);
16797 if (name != error_mark_node)
16798 name = build_string (IDENTIFIER_LENGTH (name),
16799 IDENTIFIER_POINTER (name));
16800 /* Look for the closing `]'. */
16801 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
16803 else
16804 name = NULL_TREE;
16805 /* Look for the string-literal. */
16806 string_literal = cp_parser_string_literal (parser, false, false);
16808 /* Look for the `('. */
16809 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16810 /* Parse the expression. */
16811 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
16812 /* Look for the `)'. */
16813 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16815 if (name == error_mark_node
16816 || string_literal == error_mark_node
16817 || expression == error_mark_node)
16818 invalid_operands = true;
16820 /* Add this operand to the list. */
16821 asm_operands = tree_cons (build_tree_list (name, string_literal),
16822 expression,
16823 asm_operands);
16824 /* If the next token is not a `,', there are no more
16825 operands. */
16826 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16827 break;
16828 /* Consume the `,'. */
16829 cp_lexer_consume_token (parser->lexer);
16832 return invalid_operands ? error_mark_node : nreverse (asm_operands);
16835 /* Parse an asm-clobber-list.
16837 asm-clobber-list:
16838 string-literal
16839 asm-clobber-list , string-literal
16841 Returns a TREE_LIST, indicating the clobbers in the order that they
16842 appeared. The TREE_VALUE of each node is a STRING_CST. */
16844 static tree
16845 cp_parser_asm_clobber_list (cp_parser* parser)
16847 tree clobbers = NULL_TREE;
16849 while (true)
16851 tree string_literal;
16853 /* Look for the string literal. */
16854 string_literal = cp_parser_string_literal (parser, false, false);
16855 /* Add it to the list. */
16856 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
16857 /* If the next token is not a `,', then the list is
16858 complete. */
16859 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16860 break;
16861 /* Consume the `,' token. */
16862 cp_lexer_consume_token (parser->lexer);
16865 return clobbers;
16868 /* Parse an (optional) series of attributes.
16870 attributes:
16871 attributes attribute
16873 attribute:
16874 __attribute__ (( attribute-list [opt] ))
16876 The return value is as for cp_parser_attribute_list. */
16878 static tree
16879 cp_parser_attributes_opt (cp_parser* parser)
16881 tree attributes = NULL_TREE;
16883 while (true)
16885 cp_token *token;
16886 tree attribute_list;
16888 /* Peek at the next token. */
16889 token = cp_lexer_peek_token (parser->lexer);
16890 /* If it's not `__attribute__', then we're done. */
16891 if (token->keyword != RID_ATTRIBUTE)
16892 break;
16894 /* Consume the `__attribute__' keyword. */
16895 cp_lexer_consume_token (parser->lexer);
16896 /* Look for the two `(' tokens. */
16897 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16898 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16900 /* Peek at the next token. */
16901 token = cp_lexer_peek_token (parser->lexer);
16902 if (token->type != CPP_CLOSE_PAREN)
16903 /* Parse the attribute-list. */
16904 attribute_list = cp_parser_attribute_list (parser);
16905 else
16906 /* If the next token is a `)', then there is no attribute
16907 list. */
16908 attribute_list = NULL;
16910 /* Look for the two `)' tokens. */
16911 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16912 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16914 /* Add these new attributes to the list. */
16915 attributes = chainon (attributes, attribute_list);
16918 return attributes;
16921 /* Parse an attribute-list.
16923 attribute-list:
16924 attribute
16925 attribute-list , attribute
16927 attribute:
16928 identifier
16929 identifier ( identifier )
16930 identifier ( identifier , expression-list )
16931 identifier ( expression-list )
16933 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
16934 to an attribute. The TREE_PURPOSE of each node is the identifier
16935 indicating which attribute is in use. The TREE_VALUE represents
16936 the arguments, if any. */
16938 static tree
16939 cp_parser_attribute_list (cp_parser* parser)
16941 tree attribute_list = NULL_TREE;
16942 bool save_translate_strings_p = parser->translate_strings_p;
16944 parser->translate_strings_p = false;
16945 while (true)
16947 cp_token *token;
16948 tree identifier;
16949 tree attribute;
16951 /* Look for the identifier. We also allow keywords here; for
16952 example `__attribute__ ((const))' is legal. */
16953 token = cp_lexer_peek_token (parser->lexer);
16954 if (token->type == CPP_NAME
16955 || token->type == CPP_KEYWORD)
16957 tree arguments = NULL_TREE;
16959 /* Consume the token. */
16960 token = cp_lexer_consume_token (parser->lexer);
16962 /* Save away the identifier that indicates which attribute
16963 this is. */
16964 identifier = (token->type == CPP_KEYWORD)
16965 /* For keywords, use the canonical spelling, not the
16966 parsed identifier. */
16967 ? ridpointers[(int) token->keyword]
16968 : token->u.value;
16970 attribute = build_tree_list (identifier, NULL_TREE);
16972 /* Peek at the next token. */
16973 token = cp_lexer_peek_token (parser->lexer);
16974 /* If it's an `(', then parse the attribute arguments. */
16975 if (token->type == CPP_OPEN_PAREN)
16977 VEC(tree,gc) *vec;
16978 vec = cp_parser_parenthesized_expression_list
16979 (parser, true, /*cast_p=*/false,
16980 /*allow_expansion_p=*/false,
16981 /*non_constant_p=*/NULL);
16982 if (vec == NULL)
16983 arguments = error_mark_node;
16984 else
16986 arguments = build_tree_list_vec (vec);
16987 release_tree_vector (vec);
16989 /* Save the arguments away. */
16990 TREE_VALUE (attribute) = arguments;
16993 if (arguments != error_mark_node)
16995 /* Add this attribute to the list. */
16996 TREE_CHAIN (attribute) = attribute_list;
16997 attribute_list = attribute;
17000 token = cp_lexer_peek_token (parser->lexer);
17002 /* Now, look for more attributes. If the next token isn't a
17003 `,', we're done. */
17004 if (token->type != CPP_COMMA)
17005 break;
17007 /* Consume the comma and keep going. */
17008 cp_lexer_consume_token (parser->lexer);
17010 parser->translate_strings_p = save_translate_strings_p;
17012 /* We built up the list in reverse order. */
17013 return nreverse (attribute_list);
17016 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
17017 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
17018 current value of the PEDANTIC flag, regardless of whether or not
17019 the `__extension__' keyword is present. The caller is responsible
17020 for restoring the value of the PEDANTIC flag. */
17022 static bool
17023 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
17025 /* Save the old value of the PEDANTIC flag. */
17026 *saved_pedantic = pedantic;
17028 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
17030 /* Consume the `__extension__' token. */
17031 cp_lexer_consume_token (parser->lexer);
17032 /* We're not being pedantic while the `__extension__' keyword is
17033 in effect. */
17034 pedantic = 0;
17036 return true;
17039 return false;
17042 /* Parse a label declaration.
17044 label-declaration:
17045 __label__ label-declarator-seq ;
17047 label-declarator-seq:
17048 identifier , label-declarator-seq
17049 identifier */
17051 static void
17052 cp_parser_label_declaration (cp_parser* parser)
17054 /* Look for the `__label__' keyword. */
17055 cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
17057 while (true)
17059 tree identifier;
17061 /* Look for an identifier. */
17062 identifier = cp_parser_identifier (parser);
17063 /* If we failed, stop. */
17064 if (identifier == error_mark_node)
17065 break;
17066 /* Declare it as a label. */
17067 finish_label_decl (identifier);
17068 /* If the next token is a `;', stop. */
17069 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17070 break;
17071 /* Look for the `,' separating the label declarations. */
17072 cp_parser_require (parser, CPP_COMMA, "%<,%>");
17075 /* Look for the final `;'. */
17076 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
17079 /* Support Functions */
17081 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
17082 NAME should have one of the representations used for an
17083 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
17084 is returned. If PARSER->SCOPE is a dependent type, then a
17085 SCOPE_REF is returned.
17087 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
17088 returned; the name was already resolved when the TEMPLATE_ID_EXPR
17089 was formed. Abstractly, such entities should not be passed to this
17090 function, because they do not need to be looked up, but it is
17091 simpler to check for this special case here, rather than at the
17092 call-sites.
17094 In cases not explicitly covered above, this function returns a
17095 DECL, OVERLOAD, or baselink representing the result of the lookup.
17096 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
17097 is returned.
17099 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
17100 (e.g., "struct") that was used. In that case bindings that do not
17101 refer to types are ignored.
17103 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
17104 ignored.
17106 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
17107 are ignored.
17109 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
17110 types.
17112 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
17113 TREE_LIST of candidates if name-lookup results in an ambiguity, and
17114 NULL_TREE otherwise. */
17116 static tree
17117 cp_parser_lookup_name (cp_parser *parser, tree name,
17118 enum tag_types tag_type,
17119 bool is_template,
17120 bool is_namespace,
17121 bool check_dependency,
17122 tree *ambiguous_decls,
17123 location_t name_location)
17125 int flags = 0;
17126 tree decl;
17127 tree object_type = parser->context->object_type;
17129 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17130 flags |= LOOKUP_COMPLAIN;
17132 /* Assume that the lookup will be unambiguous. */
17133 if (ambiguous_decls)
17134 *ambiguous_decls = NULL_TREE;
17136 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
17137 no longer valid. Note that if we are parsing tentatively, and
17138 the parse fails, OBJECT_TYPE will be automatically restored. */
17139 parser->context->object_type = NULL_TREE;
17141 if (name == error_mark_node)
17142 return error_mark_node;
17144 /* A template-id has already been resolved; there is no lookup to
17145 do. */
17146 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
17147 return name;
17148 if (BASELINK_P (name))
17150 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
17151 == TEMPLATE_ID_EXPR);
17152 return name;
17155 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
17156 it should already have been checked to make sure that the name
17157 used matches the type being destroyed. */
17158 if (TREE_CODE (name) == BIT_NOT_EXPR)
17160 tree type;
17162 /* Figure out to which type this destructor applies. */
17163 if (parser->scope)
17164 type = parser->scope;
17165 else if (object_type)
17166 type = object_type;
17167 else
17168 type = current_class_type;
17169 /* If that's not a class type, there is no destructor. */
17170 if (!type || !CLASS_TYPE_P (type))
17171 return error_mark_node;
17172 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
17173 lazily_declare_fn (sfk_destructor, type);
17174 if (!CLASSTYPE_DESTRUCTORS (type))
17175 return error_mark_node;
17176 /* If it was a class type, return the destructor. */
17177 return CLASSTYPE_DESTRUCTORS (type);
17180 /* By this point, the NAME should be an ordinary identifier. If
17181 the id-expression was a qualified name, the qualifying scope is
17182 stored in PARSER->SCOPE at this point. */
17183 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
17185 /* Perform the lookup. */
17186 if (parser->scope)
17188 bool dependent_p;
17190 if (parser->scope == error_mark_node)
17191 return error_mark_node;
17193 /* If the SCOPE is dependent, the lookup must be deferred until
17194 the template is instantiated -- unless we are explicitly
17195 looking up names in uninstantiated templates. Even then, we
17196 cannot look up the name if the scope is not a class type; it
17197 might, for example, be a template type parameter. */
17198 dependent_p = (TYPE_P (parser->scope)
17199 && dependent_scope_p (parser->scope));
17200 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
17201 && dependent_p)
17202 /* Defer lookup. */
17203 decl = error_mark_node;
17204 else
17206 tree pushed_scope = NULL_TREE;
17208 /* If PARSER->SCOPE is a dependent type, then it must be a
17209 class type, and we must not be checking dependencies;
17210 otherwise, we would have processed this lookup above. So
17211 that PARSER->SCOPE is not considered a dependent base by
17212 lookup_member, we must enter the scope here. */
17213 if (dependent_p)
17214 pushed_scope = push_scope (parser->scope);
17215 /* If the PARSER->SCOPE is a template specialization, it
17216 may be instantiated during name lookup. In that case,
17217 errors may be issued. Even if we rollback the current
17218 tentative parse, those errors are valid. */
17219 decl = lookup_qualified_name (parser->scope, name,
17220 tag_type != none_type,
17221 /*complain=*/true);
17223 /* If we have a single function from a using decl, pull it out. */
17224 if (TREE_CODE (decl) == OVERLOAD
17225 && !really_overloaded_fn (decl))
17226 decl = OVL_FUNCTION (decl);
17228 if (pushed_scope)
17229 pop_scope (pushed_scope);
17232 /* If the scope is a dependent type and either we deferred lookup or
17233 we did lookup but didn't find the name, rememeber the name. */
17234 if (decl == error_mark_node && TYPE_P (parser->scope)
17235 && dependent_type_p (parser->scope))
17237 if (tag_type)
17239 tree type;
17241 /* The resolution to Core Issue 180 says that `struct
17242 A::B' should be considered a type-name, even if `A'
17243 is dependent. */
17244 type = make_typename_type (parser->scope, name, tag_type,
17245 /*complain=*/tf_error);
17246 decl = TYPE_NAME (type);
17248 else if (is_template
17249 && (cp_parser_next_token_ends_template_argument_p (parser)
17250 || cp_lexer_next_token_is (parser->lexer,
17251 CPP_CLOSE_PAREN)))
17252 decl = make_unbound_class_template (parser->scope,
17253 name, NULL_TREE,
17254 /*complain=*/tf_error);
17255 else
17256 decl = build_qualified_name (/*type=*/NULL_TREE,
17257 parser->scope, name,
17258 is_template);
17260 parser->qualifying_scope = parser->scope;
17261 parser->object_scope = NULL_TREE;
17263 else if (object_type)
17265 tree object_decl = NULL_TREE;
17266 /* Look up the name in the scope of the OBJECT_TYPE, unless the
17267 OBJECT_TYPE is not a class. */
17268 if (CLASS_TYPE_P (object_type))
17269 /* If the OBJECT_TYPE is a template specialization, it may
17270 be instantiated during name lookup. In that case, errors
17271 may be issued. Even if we rollback the current tentative
17272 parse, those errors are valid. */
17273 object_decl = lookup_member (object_type,
17274 name,
17275 /*protect=*/0,
17276 tag_type != none_type);
17277 /* Look it up in the enclosing context, too. */
17278 decl = lookup_name_real (name, tag_type != none_type,
17279 /*nonclass=*/0,
17280 /*block_p=*/true, is_namespace, flags);
17281 parser->object_scope = object_type;
17282 parser->qualifying_scope = NULL_TREE;
17283 if (object_decl)
17284 decl = object_decl;
17286 else
17288 decl = lookup_name_real (name, tag_type != none_type,
17289 /*nonclass=*/0,
17290 /*block_p=*/true, is_namespace, flags);
17291 parser->qualifying_scope = NULL_TREE;
17292 parser->object_scope = NULL_TREE;
17295 /* If the lookup failed, let our caller know. */
17296 if (!decl || decl == error_mark_node)
17297 return error_mark_node;
17299 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
17300 if (TREE_CODE (decl) == TREE_LIST)
17302 if (ambiguous_decls)
17303 *ambiguous_decls = decl;
17304 /* The error message we have to print is too complicated for
17305 cp_parser_error, so we incorporate its actions directly. */
17306 if (!cp_parser_simulate_error (parser))
17308 error_at (name_location, "reference to %qD is ambiguous",
17309 name);
17310 print_candidates (decl);
17312 return error_mark_node;
17315 gcc_assert (DECL_P (decl)
17316 || TREE_CODE (decl) == OVERLOAD
17317 || TREE_CODE (decl) == SCOPE_REF
17318 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
17319 || BASELINK_P (decl));
17321 /* If we have resolved the name of a member declaration, check to
17322 see if the declaration is accessible. When the name resolves to
17323 set of overloaded functions, accessibility is checked when
17324 overload resolution is done.
17326 During an explicit instantiation, access is not checked at all,
17327 as per [temp.explicit]. */
17328 if (DECL_P (decl))
17329 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
17331 return decl;
17334 /* Like cp_parser_lookup_name, but for use in the typical case where
17335 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
17336 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
17338 static tree
17339 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
17341 return cp_parser_lookup_name (parser, name,
17342 none_type,
17343 /*is_template=*/false,
17344 /*is_namespace=*/false,
17345 /*check_dependency=*/true,
17346 /*ambiguous_decls=*/NULL,
17347 location);
17350 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
17351 the current context, return the TYPE_DECL. If TAG_NAME_P is
17352 true, the DECL indicates the class being defined in a class-head,
17353 or declared in an elaborated-type-specifier.
17355 Otherwise, return DECL. */
17357 static tree
17358 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
17360 /* If the TEMPLATE_DECL is being declared as part of a class-head,
17361 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
17363 struct A {
17364 template <typename T> struct B;
17367 template <typename T> struct A::B {};
17369 Similarly, in an elaborated-type-specifier:
17371 namespace N { struct X{}; }
17373 struct A {
17374 template <typename T> friend struct N::X;
17377 However, if the DECL refers to a class type, and we are in
17378 the scope of the class, then the name lookup automatically
17379 finds the TYPE_DECL created by build_self_reference rather
17380 than a TEMPLATE_DECL. For example, in:
17382 template <class T> struct S {
17383 S s;
17386 there is no need to handle such case. */
17388 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
17389 return DECL_TEMPLATE_RESULT (decl);
17391 return decl;
17394 /* If too many, or too few, template-parameter lists apply to the
17395 declarator, issue an error message. Returns TRUE if all went well,
17396 and FALSE otherwise. */
17398 static bool
17399 cp_parser_check_declarator_template_parameters (cp_parser* parser,
17400 cp_declarator *declarator,
17401 location_t declarator_location)
17403 unsigned num_templates;
17405 /* We haven't seen any classes that involve template parameters yet. */
17406 num_templates = 0;
17408 switch (declarator->kind)
17410 case cdk_id:
17411 if (declarator->u.id.qualifying_scope)
17413 tree scope;
17414 tree member;
17416 scope = declarator->u.id.qualifying_scope;
17417 member = declarator->u.id.unqualified_name;
17419 while (scope && CLASS_TYPE_P (scope))
17421 /* You're supposed to have one `template <...>'
17422 for every template class, but you don't need one
17423 for a full specialization. For example:
17425 template <class T> struct S{};
17426 template <> struct S<int> { void f(); };
17427 void S<int>::f () {}
17429 is correct; there shouldn't be a `template <>' for
17430 the definition of `S<int>::f'. */
17431 if (!CLASSTYPE_TEMPLATE_INFO (scope))
17432 /* If SCOPE does not have template information of any
17433 kind, then it is not a template, nor is it nested
17434 within a template. */
17435 break;
17436 if (explicit_class_specialization_p (scope))
17437 break;
17438 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
17439 ++num_templates;
17441 scope = TYPE_CONTEXT (scope);
17444 else if (TREE_CODE (declarator->u.id.unqualified_name)
17445 == TEMPLATE_ID_EXPR)
17446 /* If the DECLARATOR has the form `X<y>' then it uses one
17447 additional level of template parameters. */
17448 ++num_templates;
17450 return cp_parser_check_template_parameters
17451 (parser, num_templates, declarator_location, declarator);
17454 case cdk_function:
17455 case cdk_array:
17456 case cdk_pointer:
17457 case cdk_reference:
17458 case cdk_ptrmem:
17459 return (cp_parser_check_declarator_template_parameters
17460 (parser, declarator->declarator, declarator_location));
17462 case cdk_error:
17463 return true;
17465 default:
17466 gcc_unreachable ();
17468 return false;
17471 /* NUM_TEMPLATES were used in the current declaration. If that is
17472 invalid, return FALSE and issue an error messages. Otherwise,
17473 return TRUE. If DECLARATOR is non-NULL, then we are checking a
17474 declarator and we can print more accurate diagnostics. */
17476 static bool
17477 cp_parser_check_template_parameters (cp_parser* parser,
17478 unsigned num_templates,
17479 location_t location,
17480 cp_declarator *declarator)
17482 /* If there are the same number of template classes and parameter
17483 lists, that's OK. */
17484 if (parser->num_template_parameter_lists == num_templates)
17485 return true;
17486 /* If there are more, but only one more, then we are referring to a
17487 member template. That's OK too. */
17488 if (parser->num_template_parameter_lists == num_templates + 1)
17489 return true;
17490 /* If there are more template classes than parameter lists, we have
17491 something like:
17493 template <class T> void S<T>::R<T>::f (); */
17494 if (parser->num_template_parameter_lists < num_templates)
17496 if (declarator)
17497 error_at (location, "specializing member %<%T::%E%> "
17498 "requires %<template<>%> syntax",
17499 declarator->u.id.qualifying_scope,
17500 declarator->u.id.unqualified_name);
17501 else
17502 error_at (location, "too few template-parameter-lists");
17503 return false;
17505 /* Otherwise, there are too many template parameter lists. We have
17506 something like:
17508 template <class T> template <class U> void S::f(); */
17509 error_at (location, "too many template-parameter-lists");
17510 return false;
17513 /* Parse an optional `::' token indicating that the following name is
17514 from the global namespace. If so, PARSER->SCOPE is set to the
17515 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
17516 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
17517 Returns the new value of PARSER->SCOPE, if the `::' token is
17518 present, and NULL_TREE otherwise. */
17520 static tree
17521 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
17523 cp_token *token;
17525 /* Peek at the next token. */
17526 token = cp_lexer_peek_token (parser->lexer);
17527 /* If we're looking at a `::' token then we're starting from the
17528 global namespace, not our current location. */
17529 if (token->type == CPP_SCOPE)
17531 /* Consume the `::' token. */
17532 cp_lexer_consume_token (parser->lexer);
17533 /* Set the SCOPE so that we know where to start the lookup. */
17534 parser->scope = global_namespace;
17535 parser->qualifying_scope = global_namespace;
17536 parser->object_scope = NULL_TREE;
17538 return parser->scope;
17540 else if (!current_scope_valid_p)
17542 parser->scope = NULL_TREE;
17543 parser->qualifying_scope = NULL_TREE;
17544 parser->object_scope = NULL_TREE;
17547 return NULL_TREE;
17550 /* Returns TRUE if the upcoming token sequence is the start of a
17551 constructor declarator. If FRIEND_P is true, the declarator is
17552 preceded by the `friend' specifier. */
17554 static bool
17555 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
17557 bool constructor_p;
17558 tree type_decl = NULL_TREE;
17559 bool nested_name_p;
17560 cp_token *next_token;
17562 /* The common case is that this is not a constructor declarator, so
17563 try to avoid doing lots of work if at all possible. It's not
17564 valid declare a constructor at function scope. */
17565 if (parser->in_function_body)
17566 return false;
17567 /* And only certain tokens can begin a constructor declarator. */
17568 next_token = cp_lexer_peek_token (parser->lexer);
17569 if (next_token->type != CPP_NAME
17570 && next_token->type != CPP_SCOPE
17571 && next_token->type != CPP_NESTED_NAME_SPECIFIER
17572 && next_token->type != CPP_TEMPLATE_ID)
17573 return false;
17575 /* Parse tentatively; we are going to roll back all of the tokens
17576 consumed here. */
17577 cp_parser_parse_tentatively (parser);
17578 /* Assume that we are looking at a constructor declarator. */
17579 constructor_p = true;
17581 /* Look for the optional `::' operator. */
17582 cp_parser_global_scope_opt (parser,
17583 /*current_scope_valid_p=*/false);
17584 /* Look for the nested-name-specifier. */
17585 nested_name_p
17586 = (cp_parser_nested_name_specifier_opt (parser,
17587 /*typename_keyword_p=*/false,
17588 /*check_dependency_p=*/false,
17589 /*type_p=*/false,
17590 /*is_declaration=*/false)
17591 != NULL_TREE);
17592 /* Outside of a class-specifier, there must be a
17593 nested-name-specifier. */
17594 if (!nested_name_p &&
17595 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
17596 || friend_p))
17597 constructor_p = false;
17598 /* If we still think that this might be a constructor-declarator,
17599 look for a class-name. */
17600 if (constructor_p)
17602 /* If we have:
17604 template <typename T> struct S { S(); };
17605 template <typename T> S<T>::S ();
17607 we must recognize that the nested `S' names a class.
17608 Similarly, for:
17610 template <typename T> S<T>::S<T> ();
17612 we must recognize that the nested `S' names a template. */
17613 type_decl = cp_parser_class_name (parser,
17614 /*typename_keyword_p=*/false,
17615 /*template_keyword_p=*/false,
17616 none_type,
17617 /*check_dependency_p=*/false,
17618 /*class_head_p=*/false,
17619 /*is_declaration=*/false);
17620 /* If there was no class-name, then this is not a constructor. */
17621 constructor_p = !cp_parser_error_occurred (parser);
17624 /* If we're still considering a constructor, we have to see a `(',
17625 to begin the parameter-declaration-clause, followed by either a
17626 `)', an `...', or a decl-specifier. We need to check for a
17627 type-specifier to avoid being fooled into thinking that:
17629 S::S (f) (int);
17631 is a constructor. (It is actually a function named `f' that
17632 takes one parameter (of type `int') and returns a value of type
17633 `S::S'. */
17634 if (constructor_p
17635 && cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
17637 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17638 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
17639 /* A parameter declaration begins with a decl-specifier,
17640 which is either the "attribute" keyword, a storage class
17641 specifier, or (usually) a type-specifier. */
17642 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
17644 tree type;
17645 tree pushed_scope = NULL_TREE;
17646 unsigned saved_num_template_parameter_lists;
17648 /* Names appearing in the type-specifier should be looked up
17649 in the scope of the class. */
17650 if (current_class_type)
17651 type = NULL_TREE;
17652 else
17654 type = TREE_TYPE (type_decl);
17655 if (TREE_CODE (type) == TYPENAME_TYPE)
17657 type = resolve_typename_type (type,
17658 /*only_current_p=*/false);
17659 if (TREE_CODE (type) == TYPENAME_TYPE)
17661 cp_parser_abort_tentative_parse (parser);
17662 return false;
17665 pushed_scope = push_scope (type);
17668 /* Inside the constructor parameter list, surrounding
17669 template-parameter-lists do not apply. */
17670 saved_num_template_parameter_lists
17671 = parser->num_template_parameter_lists;
17672 parser->num_template_parameter_lists = 0;
17674 /* Look for the type-specifier. */
17675 cp_parser_type_specifier (parser,
17676 CP_PARSER_FLAGS_NONE,
17677 /*decl_specs=*/NULL,
17678 /*is_declarator=*/true,
17679 /*declares_class_or_enum=*/NULL,
17680 /*is_cv_qualifier=*/NULL);
17682 parser->num_template_parameter_lists
17683 = saved_num_template_parameter_lists;
17685 /* Leave the scope of the class. */
17686 if (pushed_scope)
17687 pop_scope (pushed_scope);
17689 constructor_p = !cp_parser_error_occurred (parser);
17692 else
17693 constructor_p = false;
17694 /* We did not really want to consume any tokens. */
17695 cp_parser_abort_tentative_parse (parser);
17697 return constructor_p;
17700 /* Parse the definition of the function given by the DECL_SPECIFIERS,
17701 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
17702 they must be performed once we are in the scope of the function.
17704 Returns the function defined. */
17706 static tree
17707 cp_parser_function_definition_from_specifiers_and_declarator
17708 (cp_parser* parser,
17709 cp_decl_specifier_seq *decl_specifiers,
17710 tree attributes,
17711 const cp_declarator *declarator)
17713 tree fn;
17714 bool success_p;
17716 /* Begin the function-definition. */
17717 success_p = start_function (decl_specifiers, declarator, attributes);
17719 /* The things we're about to see are not directly qualified by any
17720 template headers we've seen thus far. */
17721 reset_specialization ();
17723 /* If there were names looked up in the decl-specifier-seq that we
17724 did not check, check them now. We must wait until we are in the
17725 scope of the function to perform the checks, since the function
17726 might be a friend. */
17727 perform_deferred_access_checks ();
17729 if (!success_p)
17731 /* Skip the entire function. */
17732 cp_parser_skip_to_end_of_block_or_statement (parser);
17733 fn = error_mark_node;
17735 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
17737 /* Seen already, skip it. An error message has already been output. */
17738 cp_parser_skip_to_end_of_block_or_statement (parser);
17739 fn = current_function_decl;
17740 current_function_decl = NULL_TREE;
17741 /* If this is a function from a class, pop the nested class. */
17742 if (current_class_name)
17743 pop_nested_class ();
17745 else
17746 fn = cp_parser_function_definition_after_declarator (parser,
17747 /*inline_p=*/false);
17749 return fn;
17752 /* Parse the part of a function-definition that follows the
17753 declarator. INLINE_P is TRUE iff this function is an inline
17754 function defined with a class-specifier.
17756 Returns the function defined. */
17758 static tree
17759 cp_parser_function_definition_after_declarator (cp_parser* parser,
17760 bool inline_p)
17762 tree fn;
17763 bool ctor_initializer_p = false;
17764 bool saved_in_unbraced_linkage_specification_p;
17765 bool saved_in_function_body;
17766 unsigned saved_num_template_parameter_lists;
17767 cp_token *token;
17769 saved_in_function_body = parser->in_function_body;
17770 parser->in_function_body = true;
17771 /* If the next token is `return', then the code may be trying to
17772 make use of the "named return value" extension that G++ used to
17773 support. */
17774 token = cp_lexer_peek_token (parser->lexer);
17775 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
17777 /* Consume the `return' keyword. */
17778 cp_lexer_consume_token (parser->lexer);
17779 /* Look for the identifier that indicates what value is to be
17780 returned. */
17781 cp_parser_identifier (parser);
17782 /* Issue an error message. */
17783 error_at (token->location,
17784 "named return values are no longer supported");
17785 /* Skip tokens until we reach the start of the function body. */
17786 while (true)
17788 cp_token *token = cp_lexer_peek_token (parser->lexer);
17789 if (token->type == CPP_OPEN_BRACE
17790 || token->type == CPP_EOF
17791 || token->type == CPP_PRAGMA_EOL)
17792 break;
17793 cp_lexer_consume_token (parser->lexer);
17796 /* The `extern' in `extern "C" void f () { ... }' does not apply to
17797 anything declared inside `f'. */
17798 saved_in_unbraced_linkage_specification_p
17799 = parser->in_unbraced_linkage_specification_p;
17800 parser->in_unbraced_linkage_specification_p = false;
17801 /* Inside the function, surrounding template-parameter-lists do not
17802 apply. */
17803 saved_num_template_parameter_lists
17804 = parser->num_template_parameter_lists;
17805 parser->num_template_parameter_lists = 0;
17806 /* If the next token is `try', then we are looking at a
17807 function-try-block. */
17808 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
17809 ctor_initializer_p = cp_parser_function_try_block (parser);
17810 /* A function-try-block includes the function-body, so we only do
17811 this next part if we're not processing a function-try-block. */
17812 else
17813 ctor_initializer_p
17814 = cp_parser_ctor_initializer_opt_and_function_body (parser);
17816 /* Finish the function. */
17817 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
17818 (inline_p ? 2 : 0));
17819 /* Generate code for it, if necessary. */
17820 expand_or_defer_fn (fn);
17821 /* Restore the saved values. */
17822 parser->in_unbraced_linkage_specification_p
17823 = saved_in_unbraced_linkage_specification_p;
17824 parser->num_template_parameter_lists
17825 = saved_num_template_parameter_lists;
17826 parser->in_function_body = saved_in_function_body;
17828 return fn;
17831 /* Parse a template-declaration, assuming that the `export' (and
17832 `extern') keywords, if present, has already been scanned. MEMBER_P
17833 is as for cp_parser_template_declaration. */
17835 static void
17836 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
17838 tree decl = NULL_TREE;
17839 VEC (deferred_access_check,gc) *checks;
17840 tree parameter_list;
17841 bool friend_p = false;
17842 bool need_lang_pop;
17843 cp_token *token;
17845 /* Look for the `template' keyword. */
17846 token = cp_lexer_peek_token (parser->lexer);
17847 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
17848 return;
17850 /* And the `<'. */
17851 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
17852 return;
17853 if (at_class_scope_p () && current_function_decl)
17855 /* 14.5.2.2 [temp.mem]
17857 A local class shall not have member templates. */
17858 error_at (token->location,
17859 "invalid declaration of member template in local class");
17860 cp_parser_skip_to_end_of_block_or_statement (parser);
17861 return;
17863 /* [temp]
17865 A template ... shall not have C linkage. */
17866 if (current_lang_name == lang_name_c)
17868 error_at (token->location, "template with C linkage");
17869 /* Give it C++ linkage to avoid confusing other parts of the
17870 front end. */
17871 push_lang_context (lang_name_cplusplus);
17872 need_lang_pop = true;
17874 else
17875 need_lang_pop = false;
17877 /* We cannot perform access checks on the template parameter
17878 declarations until we know what is being declared, just as we
17879 cannot check the decl-specifier list. */
17880 push_deferring_access_checks (dk_deferred);
17882 /* If the next token is `>', then we have an invalid
17883 specialization. Rather than complain about an invalid template
17884 parameter, issue an error message here. */
17885 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
17887 cp_parser_error (parser, "invalid explicit specialization");
17888 begin_specialization ();
17889 parameter_list = NULL_TREE;
17891 else
17892 /* Parse the template parameters. */
17893 parameter_list = cp_parser_template_parameter_list (parser);
17895 /* Get the deferred access checks from the parameter list. These
17896 will be checked once we know what is being declared, as for a
17897 member template the checks must be performed in the scope of the
17898 class containing the member. */
17899 checks = get_deferred_access_checks ();
17901 /* Look for the `>'. */
17902 cp_parser_skip_to_end_of_template_parameter_list (parser);
17903 /* We just processed one more parameter list. */
17904 ++parser->num_template_parameter_lists;
17905 /* If the next token is `template', there are more template
17906 parameters. */
17907 if (cp_lexer_next_token_is_keyword (parser->lexer,
17908 RID_TEMPLATE))
17909 cp_parser_template_declaration_after_export (parser, member_p);
17910 else
17912 /* There are no access checks when parsing a template, as we do not
17913 know if a specialization will be a friend. */
17914 push_deferring_access_checks (dk_no_check);
17915 token = cp_lexer_peek_token (parser->lexer);
17916 decl = cp_parser_single_declaration (parser,
17917 checks,
17918 member_p,
17919 /*explicit_specialization_p=*/false,
17920 &friend_p);
17921 pop_deferring_access_checks ();
17923 /* If this is a member template declaration, let the front
17924 end know. */
17925 if (member_p && !friend_p && decl)
17927 if (TREE_CODE (decl) == TYPE_DECL)
17928 cp_parser_check_access_in_redeclaration (decl, token->location);
17930 decl = finish_member_template_decl (decl);
17932 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
17933 make_friend_class (current_class_type, TREE_TYPE (decl),
17934 /*complain=*/true);
17936 /* We are done with the current parameter list. */
17937 --parser->num_template_parameter_lists;
17939 pop_deferring_access_checks ();
17941 /* Finish up. */
17942 finish_template_decl (parameter_list);
17944 /* Register member declarations. */
17945 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
17946 finish_member_declaration (decl);
17947 /* For the erroneous case of a template with C linkage, we pushed an
17948 implicit C++ linkage scope; exit that scope now. */
17949 if (need_lang_pop)
17950 pop_lang_context ();
17951 /* If DECL is a function template, we must return to parse it later.
17952 (Even though there is no definition, there might be default
17953 arguments that need handling.) */
17954 if (member_p && decl
17955 && (TREE_CODE (decl) == FUNCTION_DECL
17956 || DECL_FUNCTION_TEMPLATE_P (decl)))
17957 TREE_VALUE (parser->unparsed_functions_queues)
17958 = tree_cons (NULL_TREE, decl,
17959 TREE_VALUE (parser->unparsed_functions_queues));
17962 /* Perform the deferred access checks from a template-parameter-list.
17963 CHECKS is a TREE_LIST of access checks, as returned by
17964 get_deferred_access_checks. */
17966 static void
17967 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
17969 ++processing_template_parmlist;
17970 perform_access_checks (checks);
17971 --processing_template_parmlist;
17974 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
17975 `function-definition' sequence. MEMBER_P is true, this declaration
17976 appears in a class scope.
17978 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
17979 *FRIEND_P is set to TRUE iff the declaration is a friend. */
17981 static tree
17982 cp_parser_single_declaration (cp_parser* parser,
17983 VEC (deferred_access_check,gc)* checks,
17984 bool member_p,
17985 bool explicit_specialization_p,
17986 bool* friend_p)
17988 int declares_class_or_enum;
17989 tree decl = NULL_TREE;
17990 cp_decl_specifier_seq decl_specifiers;
17991 bool function_definition_p = false;
17992 cp_token *decl_spec_token_start;
17994 /* This function is only used when processing a template
17995 declaration. */
17996 gcc_assert (innermost_scope_kind () == sk_template_parms
17997 || innermost_scope_kind () == sk_template_spec);
17999 /* Defer access checks until we know what is being declared. */
18000 push_deferring_access_checks (dk_deferred);
18002 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
18003 alternative. */
18004 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
18005 cp_parser_decl_specifier_seq (parser,
18006 CP_PARSER_FLAGS_OPTIONAL,
18007 &decl_specifiers,
18008 &declares_class_or_enum);
18009 if (friend_p)
18010 *friend_p = cp_parser_friend_p (&decl_specifiers);
18012 /* There are no template typedefs. */
18013 if (decl_specifiers.specs[(int) ds_typedef])
18015 error_at (decl_spec_token_start->location,
18016 "template declaration of %<typedef%>");
18017 decl = error_mark_node;
18020 /* Gather up the access checks that occurred the
18021 decl-specifier-seq. */
18022 stop_deferring_access_checks ();
18024 /* Check for the declaration of a template class. */
18025 if (declares_class_or_enum)
18027 if (cp_parser_declares_only_class_p (parser))
18029 decl = shadow_tag (&decl_specifiers);
18031 /* In this case:
18033 struct C {
18034 friend template <typename T> struct A<T>::B;
18037 A<T>::B will be represented by a TYPENAME_TYPE, and
18038 therefore not recognized by shadow_tag. */
18039 if (friend_p && *friend_p
18040 && !decl
18041 && decl_specifiers.type
18042 && TYPE_P (decl_specifiers.type))
18043 decl = decl_specifiers.type;
18045 if (decl && decl != error_mark_node)
18046 decl = TYPE_NAME (decl);
18047 else
18048 decl = error_mark_node;
18050 /* Perform access checks for template parameters. */
18051 cp_parser_perform_template_parameter_access_checks (checks);
18054 /* If it's not a template class, try for a template function. If
18055 the next token is a `;', then this declaration does not declare
18056 anything. But, if there were errors in the decl-specifiers, then
18057 the error might well have come from an attempted class-specifier.
18058 In that case, there's no need to warn about a missing declarator. */
18059 if (!decl
18060 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
18061 || decl_specifiers.type != error_mark_node))
18063 decl = cp_parser_init_declarator (parser,
18064 &decl_specifiers,
18065 checks,
18066 /*function_definition_allowed_p=*/true,
18067 member_p,
18068 declares_class_or_enum,
18069 &function_definition_p);
18071 /* 7.1.1-1 [dcl.stc]
18073 A storage-class-specifier shall not be specified in an explicit
18074 specialization... */
18075 if (decl
18076 && explicit_specialization_p
18077 && decl_specifiers.storage_class != sc_none)
18079 error_at (decl_spec_token_start->location,
18080 "explicit template specialization cannot have a storage class");
18081 decl = error_mark_node;
18085 pop_deferring_access_checks ();
18087 /* Clear any current qualification; whatever comes next is the start
18088 of something new. */
18089 parser->scope = NULL_TREE;
18090 parser->qualifying_scope = NULL_TREE;
18091 parser->object_scope = NULL_TREE;
18092 /* Look for a trailing `;' after the declaration. */
18093 if (!function_definition_p
18094 && (decl == error_mark_node
18095 || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
18096 cp_parser_skip_to_end_of_block_or_statement (parser);
18098 return decl;
18101 /* Parse a cast-expression that is not the operand of a unary "&". */
18103 static tree
18104 cp_parser_simple_cast_expression (cp_parser *parser)
18106 return cp_parser_cast_expression (parser, /*address_p=*/false,
18107 /*cast_p=*/false, NULL);
18110 /* Parse a functional cast to TYPE. Returns an expression
18111 representing the cast. */
18113 static tree
18114 cp_parser_functional_cast (cp_parser* parser, tree type)
18116 VEC(tree,gc) *vec;
18117 tree expression_list;
18118 tree cast;
18119 bool nonconst_p;
18121 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18123 maybe_warn_cpp0x ("extended initializer lists");
18124 expression_list = cp_parser_braced_list (parser, &nonconst_p);
18125 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
18126 if (TREE_CODE (type) == TYPE_DECL)
18127 type = TREE_TYPE (type);
18128 return finish_compound_literal (type, expression_list);
18132 vec = cp_parser_parenthesized_expression_list (parser, false,
18133 /*cast_p=*/true,
18134 /*allow_expansion_p=*/true,
18135 /*non_constant_p=*/NULL);
18136 if (vec == NULL)
18137 expression_list = error_mark_node;
18138 else
18140 expression_list = build_tree_list_vec (vec);
18141 release_tree_vector (vec);
18144 cast = build_functional_cast (type, expression_list,
18145 tf_warning_or_error);
18146 /* [expr.const]/1: In an integral constant expression "only type
18147 conversions to integral or enumeration type can be used". */
18148 if (TREE_CODE (type) == TYPE_DECL)
18149 type = TREE_TYPE (type);
18150 if (cast != error_mark_node
18151 && !cast_valid_in_integral_constant_expression_p (type)
18152 && (cp_parser_non_integral_constant_expression
18153 (parser, "a call to a constructor")))
18154 return error_mark_node;
18155 return cast;
18158 /* Save the tokens that make up the body of a member function defined
18159 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
18160 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
18161 specifiers applied to the declaration. Returns the FUNCTION_DECL
18162 for the member function. */
18164 static tree
18165 cp_parser_save_member_function_body (cp_parser* parser,
18166 cp_decl_specifier_seq *decl_specifiers,
18167 cp_declarator *declarator,
18168 tree attributes)
18170 cp_token *first;
18171 cp_token *last;
18172 tree fn;
18174 /* Create the function-declaration. */
18175 fn = start_method (decl_specifiers, declarator, attributes);
18176 /* If something went badly wrong, bail out now. */
18177 if (fn == error_mark_node)
18179 /* If there's a function-body, skip it. */
18180 if (cp_parser_token_starts_function_definition_p
18181 (cp_lexer_peek_token (parser->lexer)))
18182 cp_parser_skip_to_end_of_block_or_statement (parser);
18183 return error_mark_node;
18186 /* Remember it, if there default args to post process. */
18187 cp_parser_save_default_args (parser, fn);
18189 /* Save away the tokens that make up the body of the
18190 function. */
18191 first = parser->lexer->next_token;
18192 /* We can have braced-init-list mem-initializers before the fn body. */
18193 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18195 cp_lexer_consume_token (parser->lexer);
18196 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18197 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
18199 /* cache_group will stop after an un-nested { } pair, too. */
18200 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
18201 break;
18203 /* variadic mem-inits have ... after the ')'. */
18204 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18205 cp_lexer_consume_token (parser->lexer);
18208 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18209 /* Handle function try blocks. */
18210 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
18211 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18212 last = parser->lexer->next_token;
18214 /* Save away the inline definition; we will process it when the
18215 class is complete. */
18216 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
18217 DECL_PENDING_INLINE_P (fn) = 1;
18219 /* We need to know that this was defined in the class, so that
18220 friend templates are handled correctly. */
18221 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
18223 /* We're done with the inline definition. */
18224 finish_method (fn);
18226 /* Add FN to the queue of functions to be parsed later. */
18227 TREE_VALUE (parser->unparsed_functions_queues)
18228 = tree_cons (NULL_TREE, fn,
18229 TREE_VALUE (parser->unparsed_functions_queues));
18231 return fn;
18234 /* Parse a template-argument-list, as well as the trailing ">" (but
18235 not the opening ">"). See cp_parser_template_argument_list for the
18236 return value. */
18238 static tree
18239 cp_parser_enclosed_template_argument_list (cp_parser* parser)
18241 tree arguments;
18242 tree saved_scope;
18243 tree saved_qualifying_scope;
18244 tree saved_object_scope;
18245 bool saved_greater_than_is_operator_p;
18246 int saved_unevaluated_operand;
18247 int saved_inhibit_evaluation_warnings;
18249 /* [temp.names]
18251 When parsing a template-id, the first non-nested `>' is taken as
18252 the end of the template-argument-list rather than a greater-than
18253 operator. */
18254 saved_greater_than_is_operator_p
18255 = parser->greater_than_is_operator_p;
18256 parser->greater_than_is_operator_p = false;
18257 /* Parsing the argument list may modify SCOPE, so we save it
18258 here. */
18259 saved_scope = parser->scope;
18260 saved_qualifying_scope = parser->qualifying_scope;
18261 saved_object_scope = parser->object_scope;
18262 /* We need to evaluate the template arguments, even though this
18263 template-id may be nested within a "sizeof". */
18264 saved_unevaluated_operand = cp_unevaluated_operand;
18265 cp_unevaluated_operand = 0;
18266 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
18267 c_inhibit_evaluation_warnings = 0;
18268 /* Parse the template-argument-list itself. */
18269 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
18270 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18271 arguments = NULL_TREE;
18272 else
18273 arguments = cp_parser_template_argument_list (parser);
18274 /* Look for the `>' that ends the template-argument-list. If we find
18275 a '>>' instead, it's probably just a typo. */
18276 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18278 if (cxx_dialect != cxx98)
18280 /* In C++0x, a `>>' in a template argument list or cast
18281 expression is considered to be two separate `>'
18282 tokens. So, change the current token to a `>', but don't
18283 consume it: it will be consumed later when the outer
18284 template argument list (or cast expression) is parsed.
18285 Note that this replacement of `>' for `>>' is necessary
18286 even if we are parsing tentatively: in the tentative
18287 case, after calling
18288 cp_parser_enclosed_template_argument_list we will always
18289 throw away all of the template arguments and the first
18290 closing `>', either because the template argument list
18291 was erroneous or because we are replacing those tokens
18292 with a CPP_TEMPLATE_ID token. The second `>' (which will
18293 not have been thrown away) is needed either to close an
18294 outer template argument list or to complete a new-style
18295 cast. */
18296 cp_token *token = cp_lexer_peek_token (parser->lexer);
18297 token->type = CPP_GREATER;
18299 else if (!saved_greater_than_is_operator_p)
18301 /* If we're in a nested template argument list, the '>>' has
18302 to be a typo for '> >'. We emit the error message, but we
18303 continue parsing and we push a '>' as next token, so that
18304 the argument list will be parsed correctly. Note that the
18305 global source location is still on the token before the
18306 '>>', so we need to say explicitly where we want it. */
18307 cp_token *token = cp_lexer_peek_token (parser->lexer);
18308 error_at (token->location, "%<>>%> should be %<> >%> "
18309 "within a nested template argument list");
18311 token->type = CPP_GREATER;
18313 else
18315 /* If this is not a nested template argument list, the '>>'
18316 is a typo for '>'. Emit an error message and continue.
18317 Same deal about the token location, but here we can get it
18318 right by consuming the '>>' before issuing the diagnostic. */
18319 cp_token *token = cp_lexer_consume_token (parser->lexer);
18320 error_at (token->location,
18321 "spurious %<>>%>, use %<>%> to terminate "
18322 "a template argument list");
18325 else
18326 cp_parser_skip_to_end_of_template_parameter_list (parser);
18327 /* The `>' token might be a greater-than operator again now. */
18328 parser->greater_than_is_operator_p
18329 = saved_greater_than_is_operator_p;
18330 /* Restore the SAVED_SCOPE. */
18331 parser->scope = saved_scope;
18332 parser->qualifying_scope = saved_qualifying_scope;
18333 parser->object_scope = saved_object_scope;
18334 cp_unevaluated_operand = saved_unevaluated_operand;
18335 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
18337 return arguments;
18340 /* MEMBER_FUNCTION is a member function, or a friend. If default
18341 arguments, or the body of the function have not yet been parsed,
18342 parse them now. */
18344 static void
18345 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
18347 /* If this member is a template, get the underlying
18348 FUNCTION_DECL. */
18349 if (DECL_FUNCTION_TEMPLATE_P (member_function))
18350 member_function = DECL_TEMPLATE_RESULT (member_function);
18352 /* There should not be any class definitions in progress at this
18353 point; the bodies of members are only parsed outside of all class
18354 definitions. */
18355 gcc_assert (parser->num_classes_being_defined == 0);
18356 /* While we're parsing the member functions we might encounter more
18357 classes. We want to handle them right away, but we don't want
18358 them getting mixed up with functions that are currently in the
18359 queue. */
18360 parser->unparsed_functions_queues
18361 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18363 /* Make sure that any template parameters are in scope. */
18364 maybe_begin_member_template_processing (member_function);
18366 /* If the body of the function has not yet been parsed, parse it
18367 now. */
18368 if (DECL_PENDING_INLINE_P (member_function))
18370 tree function_scope;
18371 cp_token_cache *tokens;
18373 /* The function is no longer pending; we are processing it. */
18374 tokens = DECL_PENDING_INLINE_INFO (member_function);
18375 DECL_PENDING_INLINE_INFO (member_function) = NULL;
18376 DECL_PENDING_INLINE_P (member_function) = 0;
18378 /* If this is a local class, enter the scope of the containing
18379 function. */
18380 function_scope = current_function_decl;
18381 if (function_scope)
18382 push_function_context ();
18384 /* Push the body of the function onto the lexer stack. */
18385 cp_parser_push_lexer_for_tokens (parser, tokens);
18387 /* Let the front end know that we going to be defining this
18388 function. */
18389 start_preparsed_function (member_function, NULL_TREE,
18390 SF_PRE_PARSED | SF_INCLASS_INLINE);
18392 /* Don't do access checking if it is a templated function. */
18393 if (processing_template_decl)
18394 push_deferring_access_checks (dk_no_check);
18396 /* Now, parse the body of the function. */
18397 cp_parser_function_definition_after_declarator (parser,
18398 /*inline_p=*/true);
18400 if (processing_template_decl)
18401 pop_deferring_access_checks ();
18403 /* Leave the scope of the containing function. */
18404 if (function_scope)
18405 pop_function_context ();
18406 cp_parser_pop_lexer (parser);
18409 /* Remove any template parameters from the symbol table. */
18410 maybe_end_member_template_processing ();
18412 /* Restore the queue. */
18413 parser->unparsed_functions_queues
18414 = TREE_CHAIN (parser->unparsed_functions_queues);
18417 /* If DECL contains any default args, remember it on the unparsed
18418 functions queue. */
18420 static void
18421 cp_parser_save_default_args (cp_parser* parser, tree decl)
18423 tree probe;
18425 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
18426 probe;
18427 probe = TREE_CHAIN (probe))
18428 if (TREE_PURPOSE (probe))
18430 TREE_PURPOSE (parser->unparsed_functions_queues)
18431 = tree_cons (current_class_type, decl,
18432 TREE_PURPOSE (parser->unparsed_functions_queues));
18433 break;
18437 /* FN is a FUNCTION_DECL which may contains a parameter with an
18438 unparsed DEFAULT_ARG. Parse the default args now. This function
18439 assumes that the current scope is the scope in which the default
18440 argument should be processed. */
18442 static void
18443 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
18445 bool saved_local_variables_forbidden_p;
18446 tree parm;
18448 /* While we're parsing the default args, we might (due to the
18449 statement expression extension) encounter more classes. We want
18450 to handle them right away, but we don't want them getting mixed
18451 up with default args that are currently in the queue. */
18452 parser->unparsed_functions_queues
18453 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18455 /* Local variable names (and the `this' keyword) may not appear
18456 in a default argument. */
18457 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
18458 parser->local_variables_forbidden_p = true;
18460 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
18461 parm;
18462 parm = TREE_CHAIN (parm))
18464 cp_token_cache *tokens;
18465 tree default_arg = TREE_PURPOSE (parm);
18466 tree parsed_arg;
18467 VEC(tree,gc) *insts;
18468 tree copy;
18469 unsigned ix;
18471 if (!default_arg)
18472 continue;
18474 if (TREE_CODE (default_arg) != DEFAULT_ARG)
18475 /* This can happen for a friend declaration for a function
18476 already declared with default arguments. */
18477 continue;
18479 /* Push the saved tokens for the default argument onto the parser's
18480 lexer stack. */
18481 tokens = DEFARG_TOKENS (default_arg);
18482 cp_parser_push_lexer_for_tokens (parser, tokens);
18484 /* Parse the assignment-expression. */
18485 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
18486 if (parsed_arg == error_mark_node)
18488 cp_parser_pop_lexer (parser);
18489 continue;
18492 if (!processing_template_decl)
18493 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
18495 TREE_PURPOSE (parm) = parsed_arg;
18497 /* Update any instantiations we've already created. */
18498 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
18499 VEC_iterate (tree, insts, ix, copy); ix++)
18500 TREE_PURPOSE (copy) = parsed_arg;
18502 /* If the token stream has not been completely used up, then
18503 there was extra junk after the end of the default
18504 argument. */
18505 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
18506 cp_parser_error (parser, "expected %<,%>");
18508 /* Revert to the main lexer. */
18509 cp_parser_pop_lexer (parser);
18512 /* Make sure no default arg is missing. */
18513 check_default_args (fn);
18515 /* Restore the state of local_variables_forbidden_p. */
18516 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
18518 /* Restore the queue. */
18519 parser->unparsed_functions_queues
18520 = TREE_CHAIN (parser->unparsed_functions_queues);
18523 /* Parse the operand of `sizeof' (or a similar operator). Returns
18524 either a TYPE or an expression, depending on the form of the
18525 input. The KEYWORD indicates which kind of expression we have
18526 encountered. */
18528 static tree
18529 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
18531 tree expr = NULL_TREE;
18532 const char *saved_message;
18533 char *tmp;
18534 bool saved_integral_constant_expression_p;
18535 bool saved_non_integral_constant_expression_p;
18536 bool pack_expansion_p = false;
18538 /* Types cannot be defined in a `sizeof' expression. Save away the
18539 old message. */
18540 saved_message = parser->type_definition_forbidden_message;
18541 /* And create the new one. */
18542 tmp = concat ("types may not be defined in %<",
18543 IDENTIFIER_POINTER (ridpointers[keyword]),
18544 "%> expressions", NULL);
18545 parser->type_definition_forbidden_message = tmp;
18547 /* The restrictions on constant-expressions do not apply inside
18548 sizeof expressions. */
18549 saved_integral_constant_expression_p
18550 = parser->integral_constant_expression_p;
18551 saved_non_integral_constant_expression_p
18552 = parser->non_integral_constant_expression_p;
18553 parser->integral_constant_expression_p = false;
18555 /* If it's a `...', then we are computing the length of a parameter
18556 pack. */
18557 if (keyword == RID_SIZEOF
18558 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18560 /* Consume the `...'. */
18561 cp_lexer_consume_token (parser->lexer);
18562 maybe_warn_variadic_templates ();
18564 /* Note that this is an expansion. */
18565 pack_expansion_p = true;
18568 /* Do not actually evaluate the expression. */
18569 ++cp_unevaluated_operand;
18570 ++c_inhibit_evaluation_warnings;
18571 /* If it's a `(', then we might be looking at the type-id
18572 construction. */
18573 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18575 tree type;
18576 bool saved_in_type_id_in_expr_p;
18578 /* We can't be sure yet whether we're looking at a type-id or an
18579 expression. */
18580 cp_parser_parse_tentatively (parser);
18581 /* Consume the `('. */
18582 cp_lexer_consume_token (parser->lexer);
18583 /* Parse the type-id. */
18584 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
18585 parser->in_type_id_in_expr_p = true;
18586 type = cp_parser_type_id (parser);
18587 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
18588 /* Now, look for the trailing `)'. */
18589 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18590 /* If all went well, then we're done. */
18591 if (cp_parser_parse_definitely (parser))
18593 cp_decl_specifier_seq decl_specs;
18595 /* Build a trivial decl-specifier-seq. */
18596 clear_decl_specs (&decl_specs);
18597 decl_specs.type = type;
18599 /* Call grokdeclarator to figure out what type this is. */
18600 expr = grokdeclarator (NULL,
18601 &decl_specs,
18602 TYPENAME,
18603 /*initialized=*/0,
18604 /*attrlist=*/NULL);
18608 /* If the type-id production did not work out, then we must be
18609 looking at the unary-expression production. */
18610 if (!expr)
18611 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
18612 /*cast_p=*/false, NULL);
18614 if (pack_expansion_p)
18615 /* Build a pack expansion. */
18616 expr = make_pack_expansion (expr);
18618 /* Go back to evaluating expressions. */
18619 --cp_unevaluated_operand;
18620 --c_inhibit_evaluation_warnings;
18622 /* Free the message we created. */
18623 free (tmp);
18624 /* And restore the old one. */
18625 parser->type_definition_forbidden_message = saved_message;
18626 parser->integral_constant_expression_p
18627 = saved_integral_constant_expression_p;
18628 parser->non_integral_constant_expression_p
18629 = saved_non_integral_constant_expression_p;
18631 return expr;
18634 /* If the current declaration has no declarator, return true. */
18636 static bool
18637 cp_parser_declares_only_class_p (cp_parser *parser)
18639 /* If the next token is a `;' or a `,' then there is no
18640 declarator. */
18641 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18642 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
18645 /* Update the DECL_SPECS to reflect the storage class indicated by
18646 KEYWORD. */
18648 static void
18649 cp_parser_set_storage_class (cp_parser *parser,
18650 cp_decl_specifier_seq *decl_specs,
18651 enum rid keyword,
18652 location_t location)
18654 cp_storage_class storage_class;
18656 if (parser->in_unbraced_linkage_specification_p)
18658 error_at (location, "invalid use of %qD in linkage specification",
18659 ridpointers[keyword]);
18660 return;
18662 else if (decl_specs->storage_class != sc_none)
18664 decl_specs->conflicting_specifiers_p = true;
18665 return;
18668 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
18669 && decl_specs->specs[(int) ds_thread])
18671 error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
18672 decl_specs->specs[(int) ds_thread] = 0;
18675 switch (keyword)
18677 case RID_AUTO:
18678 storage_class = sc_auto;
18679 break;
18680 case RID_REGISTER:
18681 storage_class = sc_register;
18682 break;
18683 case RID_STATIC:
18684 storage_class = sc_static;
18685 break;
18686 case RID_EXTERN:
18687 storage_class = sc_extern;
18688 break;
18689 case RID_MUTABLE:
18690 storage_class = sc_mutable;
18691 break;
18692 default:
18693 gcc_unreachable ();
18695 decl_specs->storage_class = storage_class;
18697 /* A storage class specifier cannot be applied alongside a typedef
18698 specifier. If there is a typedef specifier present then set
18699 conflicting_specifiers_p which will trigger an error later
18700 on in grokdeclarator. */
18701 if (decl_specs->specs[(int)ds_typedef])
18702 decl_specs->conflicting_specifiers_p = true;
18705 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
18706 is true, the type is a user-defined type; otherwise it is a
18707 built-in type specified by a keyword. */
18709 static void
18710 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
18711 tree type_spec,
18712 location_t location,
18713 bool user_defined_p)
18715 decl_specs->any_specifiers_p = true;
18717 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
18718 (with, for example, in "typedef int wchar_t;") we remember that
18719 this is what happened. In system headers, we ignore these
18720 declarations so that G++ can work with system headers that are not
18721 C++-safe. */
18722 if (decl_specs->specs[(int) ds_typedef]
18723 && !user_defined_p
18724 && (type_spec == boolean_type_node
18725 || type_spec == char16_type_node
18726 || type_spec == char32_type_node
18727 || type_spec == wchar_type_node)
18728 && (decl_specs->type
18729 || decl_specs->specs[(int) ds_long]
18730 || decl_specs->specs[(int) ds_short]
18731 || decl_specs->specs[(int) ds_unsigned]
18732 || decl_specs->specs[(int) ds_signed]))
18734 decl_specs->redefined_builtin_type = type_spec;
18735 if (!decl_specs->type)
18737 decl_specs->type = type_spec;
18738 decl_specs->user_defined_type_p = false;
18739 decl_specs->type_location = location;
18742 else if (decl_specs->type)
18743 decl_specs->multiple_types_p = true;
18744 else
18746 decl_specs->type = type_spec;
18747 decl_specs->user_defined_type_p = user_defined_p;
18748 decl_specs->redefined_builtin_type = NULL_TREE;
18749 decl_specs->type_location = location;
18753 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
18754 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
18756 static bool
18757 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
18759 return decl_specifiers->specs[(int) ds_friend] != 0;
18762 /* If the next token is of the indicated TYPE, consume it. Otherwise,
18763 issue an error message indicating that TOKEN_DESC was expected.
18765 Returns the token consumed, if the token had the appropriate type.
18766 Otherwise, returns NULL. */
18768 static cp_token *
18769 cp_parser_require (cp_parser* parser,
18770 enum cpp_ttype type,
18771 const char* token_desc)
18773 if (cp_lexer_next_token_is (parser->lexer, type))
18774 return cp_lexer_consume_token (parser->lexer);
18775 else
18777 /* Output the MESSAGE -- unless we're parsing tentatively. */
18778 if (!cp_parser_simulate_error (parser))
18780 char *message = concat ("expected ", token_desc, NULL);
18781 cp_parser_error (parser, message);
18782 free (message);
18784 return NULL;
18788 /* An error message is produced if the next token is not '>'.
18789 All further tokens are skipped until the desired token is
18790 found or '{', '}', ';' or an unbalanced ')' or ']'. */
18792 static void
18793 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
18795 /* Current level of '< ... >'. */
18796 unsigned level = 0;
18797 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
18798 unsigned nesting_depth = 0;
18800 /* Are we ready, yet? If not, issue error message. */
18801 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
18802 return;
18804 /* Skip tokens until the desired token is found. */
18805 while (true)
18807 /* Peek at the next token. */
18808 switch (cp_lexer_peek_token (parser->lexer)->type)
18810 case CPP_LESS:
18811 if (!nesting_depth)
18812 ++level;
18813 break;
18815 case CPP_RSHIFT:
18816 if (cxx_dialect == cxx98)
18817 /* C++0x views the `>>' operator as two `>' tokens, but
18818 C++98 does not. */
18819 break;
18820 else if (!nesting_depth && level-- == 0)
18822 /* We've hit a `>>' where the first `>' closes the
18823 template argument list, and the second `>' is
18824 spurious. Just consume the `>>' and stop; we've
18825 already produced at least one error. */
18826 cp_lexer_consume_token (parser->lexer);
18827 return;
18829 /* Fall through for C++0x, so we handle the second `>' in
18830 the `>>'. */
18832 case CPP_GREATER:
18833 if (!nesting_depth && level-- == 0)
18835 /* We've reached the token we want, consume it and stop. */
18836 cp_lexer_consume_token (parser->lexer);
18837 return;
18839 break;
18841 case CPP_OPEN_PAREN:
18842 case CPP_OPEN_SQUARE:
18843 ++nesting_depth;
18844 break;
18846 case CPP_CLOSE_PAREN:
18847 case CPP_CLOSE_SQUARE:
18848 if (nesting_depth-- == 0)
18849 return;
18850 break;
18852 case CPP_EOF:
18853 case CPP_PRAGMA_EOL:
18854 case CPP_SEMICOLON:
18855 case CPP_OPEN_BRACE:
18856 case CPP_CLOSE_BRACE:
18857 /* The '>' was probably forgotten, don't look further. */
18858 return;
18860 default:
18861 break;
18864 /* Consume this token. */
18865 cp_lexer_consume_token (parser->lexer);
18869 /* If the next token is the indicated keyword, consume it. Otherwise,
18870 issue an error message indicating that TOKEN_DESC was expected.
18872 Returns the token consumed, if the token had the appropriate type.
18873 Otherwise, returns NULL. */
18875 static cp_token *
18876 cp_parser_require_keyword (cp_parser* parser,
18877 enum rid keyword,
18878 const char* token_desc)
18880 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
18882 if (token && token->keyword != keyword)
18884 dyn_string_t error_msg;
18886 /* Format the error message. */
18887 error_msg = dyn_string_new (0);
18888 dyn_string_append_cstr (error_msg, "expected ");
18889 dyn_string_append_cstr (error_msg, token_desc);
18890 cp_parser_error (parser, error_msg->s);
18891 dyn_string_delete (error_msg);
18892 return NULL;
18895 return token;
18898 /* Returns TRUE iff TOKEN is a token that can begin the body of a
18899 function-definition. */
18901 static bool
18902 cp_parser_token_starts_function_definition_p (cp_token* token)
18904 return (/* An ordinary function-body begins with an `{'. */
18905 token->type == CPP_OPEN_BRACE
18906 /* A ctor-initializer begins with a `:'. */
18907 || token->type == CPP_COLON
18908 /* A function-try-block begins with `try'. */
18909 || token->keyword == RID_TRY
18910 /* The named return value extension begins with `return'. */
18911 || token->keyword == RID_RETURN);
18914 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
18915 definition. */
18917 static bool
18918 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
18920 cp_token *token;
18922 token = cp_lexer_peek_token (parser->lexer);
18923 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
18926 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
18927 C++0x) ending a template-argument. */
18929 static bool
18930 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
18932 cp_token *token;
18934 token = cp_lexer_peek_token (parser->lexer);
18935 return (token->type == CPP_COMMA
18936 || token->type == CPP_GREATER
18937 || token->type == CPP_ELLIPSIS
18938 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
18941 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
18942 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
18944 static bool
18945 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
18946 size_t n)
18948 cp_token *token;
18950 token = cp_lexer_peek_nth_token (parser->lexer, n);
18951 if (token->type == CPP_LESS)
18952 return true;
18953 /* Check for the sequence `<::' in the original code. It would be lexed as
18954 `[:', where `[' is a digraph, and there is no whitespace before
18955 `:'. */
18956 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
18958 cp_token *token2;
18959 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
18960 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
18961 return true;
18963 return false;
18966 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
18967 or none_type otherwise. */
18969 static enum tag_types
18970 cp_parser_token_is_class_key (cp_token* token)
18972 switch (token->keyword)
18974 case RID_CLASS:
18975 return class_type;
18976 case RID_STRUCT:
18977 return record_type;
18978 case RID_UNION:
18979 return union_type;
18981 default:
18982 return none_type;
18986 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
18988 static void
18989 cp_parser_check_class_key (enum tag_types class_key, tree type)
18991 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
18992 permerror (input_location, "%qs tag used in naming %q#T",
18993 class_key == union_type ? "union"
18994 : class_key == record_type ? "struct" : "class",
18995 type);
18998 /* Issue an error message if DECL is redeclared with different
18999 access than its original declaration [class.access.spec/3].
19000 This applies to nested classes and nested class templates.
19001 [class.mem/1]. */
19003 static void
19004 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
19006 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
19007 return;
19009 if ((TREE_PRIVATE (decl)
19010 != (current_access_specifier == access_private_node))
19011 || (TREE_PROTECTED (decl)
19012 != (current_access_specifier == access_protected_node)))
19013 error_at (location, "%qD redeclared with different access", decl);
19016 /* Look for the `template' keyword, as a syntactic disambiguator.
19017 Return TRUE iff it is present, in which case it will be
19018 consumed. */
19020 static bool
19021 cp_parser_optional_template_keyword (cp_parser *parser)
19023 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
19025 /* The `template' keyword can only be used within templates;
19026 outside templates the parser can always figure out what is a
19027 template and what is not. */
19028 if (!processing_template_decl)
19030 cp_token *token = cp_lexer_peek_token (parser->lexer);
19031 error_at (token->location,
19032 "%<template%> (as a disambiguator) is only allowed "
19033 "within templates");
19034 /* If this part of the token stream is rescanned, the same
19035 error message would be generated. So, we purge the token
19036 from the stream. */
19037 cp_lexer_purge_token (parser->lexer);
19038 return false;
19040 else
19042 /* Consume the `template' keyword. */
19043 cp_lexer_consume_token (parser->lexer);
19044 return true;
19048 return false;
19051 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
19052 set PARSER->SCOPE, and perform other related actions. */
19054 static void
19055 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
19057 int i;
19058 struct tree_check *check_value;
19059 deferred_access_check *chk;
19060 VEC (deferred_access_check,gc) *checks;
19062 /* Get the stored value. */
19063 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
19064 /* Perform any access checks that were deferred. */
19065 checks = check_value->checks;
19066 if (checks)
19068 for (i = 0 ;
19069 VEC_iterate (deferred_access_check, checks, i, chk) ;
19070 ++i)
19072 perform_or_defer_access_check (chk->binfo,
19073 chk->decl,
19074 chk->diag_decl);
19077 /* Set the scope from the stored value. */
19078 parser->scope = check_value->value;
19079 parser->qualifying_scope = check_value->qualifying_scope;
19080 parser->object_scope = NULL_TREE;
19083 /* Consume tokens up through a non-nested END token. Returns TRUE if we
19084 encounter the end of a block before what we were looking for. */
19086 static bool
19087 cp_parser_cache_group (cp_parser *parser,
19088 enum cpp_ttype end,
19089 unsigned depth)
19091 while (true)
19093 cp_token *token = cp_lexer_peek_token (parser->lexer);
19095 /* Abort a parenthesized expression if we encounter a semicolon. */
19096 if ((end == CPP_CLOSE_PAREN || depth == 0)
19097 && token->type == CPP_SEMICOLON)
19098 return true;
19099 /* If we've reached the end of the file, stop. */
19100 if (token->type == CPP_EOF
19101 || (end != CPP_PRAGMA_EOL
19102 && token->type == CPP_PRAGMA_EOL))
19103 return true;
19104 if (token->type == CPP_CLOSE_BRACE && depth == 0)
19105 /* We've hit the end of an enclosing block, so there's been some
19106 kind of syntax error. */
19107 return true;
19109 /* Consume the token. */
19110 cp_lexer_consume_token (parser->lexer);
19111 /* See if it starts a new group. */
19112 if (token->type == CPP_OPEN_BRACE)
19114 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
19115 /* In theory this should probably check end == '}', but
19116 cp_parser_save_member_function_body needs it to exit
19117 after either '}' or ')' when called with ')'. */
19118 if (depth == 0)
19119 return false;
19121 else if (token->type == CPP_OPEN_PAREN)
19123 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
19124 if (depth == 0 && end == CPP_CLOSE_PAREN)
19125 return false;
19127 else if (token->type == CPP_PRAGMA)
19128 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
19129 else if (token->type == end)
19130 return false;
19134 /* Begin parsing tentatively. We always save tokens while parsing
19135 tentatively so that if the tentative parsing fails we can restore the
19136 tokens. */
19138 static void
19139 cp_parser_parse_tentatively (cp_parser* parser)
19141 /* Enter a new parsing context. */
19142 parser->context = cp_parser_context_new (parser->context);
19143 /* Begin saving tokens. */
19144 cp_lexer_save_tokens (parser->lexer);
19145 /* In order to avoid repetitive access control error messages,
19146 access checks are queued up until we are no longer parsing
19147 tentatively. */
19148 push_deferring_access_checks (dk_deferred);
19151 /* Commit to the currently active tentative parse. */
19153 static void
19154 cp_parser_commit_to_tentative_parse (cp_parser* parser)
19156 cp_parser_context *context;
19157 cp_lexer *lexer;
19159 /* Mark all of the levels as committed. */
19160 lexer = parser->lexer;
19161 for (context = parser->context; context->next; context = context->next)
19163 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
19164 break;
19165 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
19166 while (!cp_lexer_saving_tokens (lexer))
19167 lexer = lexer->next;
19168 cp_lexer_commit_tokens (lexer);
19172 /* Abort the currently active tentative parse. All consumed tokens
19173 will be rolled back, and no diagnostics will be issued. */
19175 static void
19176 cp_parser_abort_tentative_parse (cp_parser* parser)
19178 cp_parser_simulate_error (parser);
19179 /* Now, pretend that we want to see if the construct was
19180 successfully parsed. */
19181 cp_parser_parse_definitely (parser);
19184 /* Stop parsing tentatively. If a parse error has occurred, restore the
19185 token stream. Otherwise, commit to the tokens we have consumed.
19186 Returns true if no error occurred; false otherwise. */
19188 static bool
19189 cp_parser_parse_definitely (cp_parser* parser)
19191 bool error_occurred;
19192 cp_parser_context *context;
19194 /* Remember whether or not an error occurred, since we are about to
19195 destroy that information. */
19196 error_occurred = cp_parser_error_occurred (parser);
19197 /* Remove the topmost context from the stack. */
19198 context = parser->context;
19199 parser->context = context->next;
19200 /* If no parse errors occurred, commit to the tentative parse. */
19201 if (!error_occurred)
19203 /* Commit to the tokens read tentatively, unless that was
19204 already done. */
19205 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
19206 cp_lexer_commit_tokens (parser->lexer);
19208 pop_to_parent_deferring_access_checks ();
19210 /* Otherwise, if errors occurred, roll back our state so that things
19211 are just as they were before we began the tentative parse. */
19212 else
19214 cp_lexer_rollback_tokens (parser->lexer);
19215 pop_deferring_access_checks ();
19217 /* Add the context to the front of the free list. */
19218 context->next = cp_parser_context_free_list;
19219 cp_parser_context_free_list = context;
19221 return !error_occurred;
19224 /* Returns true if we are parsing tentatively and are not committed to
19225 this tentative parse. */
19227 static bool
19228 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
19230 return (cp_parser_parsing_tentatively (parser)
19231 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
19234 /* Returns nonzero iff an error has occurred during the most recent
19235 tentative parse. */
19237 static bool
19238 cp_parser_error_occurred (cp_parser* parser)
19240 return (cp_parser_parsing_tentatively (parser)
19241 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
19244 /* Returns nonzero if GNU extensions are allowed. */
19246 static bool
19247 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
19249 return parser->allow_gnu_extensions_p;
19252 /* Objective-C++ Productions */
19255 /* Parse an Objective-C expression, which feeds into a primary-expression
19256 above.
19258 objc-expression:
19259 objc-message-expression
19260 objc-string-literal
19261 objc-encode-expression
19262 objc-protocol-expression
19263 objc-selector-expression
19265 Returns a tree representation of the expression. */
19267 static tree
19268 cp_parser_objc_expression (cp_parser* parser)
19270 /* Try to figure out what kind of declaration is present. */
19271 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19273 switch (kwd->type)
19275 case CPP_OPEN_SQUARE:
19276 return cp_parser_objc_message_expression (parser);
19278 case CPP_OBJC_STRING:
19279 kwd = cp_lexer_consume_token (parser->lexer);
19280 return objc_build_string_object (kwd->u.value);
19282 case CPP_KEYWORD:
19283 switch (kwd->keyword)
19285 case RID_AT_ENCODE:
19286 return cp_parser_objc_encode_expression (parser);
19288 case RID_AT_PROTOCOL:
19289 return cp_parser_objc_protocol_expression (parser);
19291 case RID_AT_SELECTOR:
19292 return cp_parser_objc_selector_expression (parser);
19294 default:
19295 break;
19297 default:
19298 error_at (kwd->location,
19299 "misplaced %<@%D%> Objective-C++ construct",
19300 kwd->u.value);
19301 cp_parser_skip_to_end_of_block_or_statement (parser);
19304 return error_mark_node;
19307 /* Parse an Objective-C message expression.
19309 objc-message-expression:
19310 [ objc-message-receiver objc-message-args ]
19312 Returns a representation of an Objective-C message. */
19314 static tree
19315 cp_parser_objc_message_expression (cp_parser* parser)
19317 tree receiver, messageargs;
19319 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
19320 receiver = cp_parser_objc_message_receiver (parser);
19321 messageargs = cp_parser_objc_message_args (parser);
19322 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
19324 return objc_build_message_expr (build_tree_list (receiver, messageargs));
19327 /* Parse an objc-message-receiver.
19329 objc-message-receiver:
19330 expression
19331 simple-type-specifier
19333 Returns a representation of the type or expression. */
19335 static tree
19336 cp_parser_objc_message_receiver (cp_parser* parser)
19338 tree rcv;
19340 /* An Objective-C message receiver may be either (1) a type
19341 or (2) an expression. */
19342 cp_parser_parse_tentatively (parser);
19343 rcv = cp_parser_expression (parser, false, NULL);
19345 if (cp_parser_parse_definitely (parser))
19346 return rcv;
19348 rcv = cp_parser_simple_type_specifier (parser,
19349 /*decl_specs=*/NULL,
19350 CP_PARSER_FLAGS_NONE);
19352 return objc_get_class_reference (rcv);
19355 /* Parse the arguments and selectors comprising an Objective-C message.
19357 objc-message-args:
19358 objc-selector
19359 objc-selector-args
19360 objc-selector-args , objc-comma-args
19362 objc-selector-args:
19363 objc-selector [opt] : assignment-expression
19364 objc-selector-args objc-selector [opt] : assignment-expression
19366 objc-comma-args:
19367 assignment-expression
19368 objc-comma-args , assignment-expression
19370 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
19371 selector arguments and TREE_VALUE containing a list of comma
19372 arguments. */
19374 static tree
19375 cp_parser_objc_message_args (cp_parser* parser)
19377 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
19378 bool maybe_unary_selector_p = true;
19379 cp_token *token = cp_lexer_peek_token (parser->lexer);
19381 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19383 tree selector = NULL_TREE, arg;
19385 if (token->type != CPP_COLON)
19386 selector = cp_parser_objc_selector (parser);
19388 /* Detect if we have a unary selector. */
19389 if (maybe_unary_selector_p
19390 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19391 return build_tree_list (selector, NULL_TREE);
19393 maybe_unary_selector_p = false;
19394 cp_parser_require (parser, CPP_COLON, "%<:%>");
19395 arg = cp_parser_assignment_expression (parser, false, NULL);
19397 sel_args
19398 = chainon (sel_args,
19399 build_tree_list (selector, arg));
19401 token = cp_lexer_peek_token (parser->lexer);
19404 /* Handle non-selector arguments, if any. */
19405 while (token->type == CPP_COMMA)
19407 tree arg;
19409 cp_lexer_consume_token (parser->lexer);
19410 arg = cp_parser_assignment_expression (parser, false, NULL);
19412 addl_args
19413 = chainon (addl_args,
19414 build_tree_list (NULL_TREE, arg));
19416 token = cp_lexer_peek_token (parser->lexer);
19419 return build_tree_list (sel_args, addl_args);
19422 /* Parse an Objective-C encode expression.
19424 objc-encode-expression:
19425 @encode objc-typename
19427 Returns an encoded representation of the type argument. */
19429 static tree
19430 cp_parser_objc_encode_expression (cp_parser* parser)
19432 tree type;
19433 cp_token *token;
19435 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
19436 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19437 token = cp_lexer_peek_token (parser->lexer);
19438 type = complete_type (cp_parser_type_id (parser));
19439 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19441 if (!type)
19443 error_at (token->location,
19444 "%<@encode%> must specify a type as an argument");
19445 return error_mark_node;
19448 return objc_build_encode_expr (type);
19451 /* Parse an Objective-C @defs expression. */
19453 static tree
19454 cp_parser_objc_defs_expression (cp_parser *parser)
19456 tree name;
19458 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
19459 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19460 name = cp_parser_identifier (parser);
19461 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19463 return objc_get_class_ivars (name);
19466 /* Parse an Objective-C protocol expression.
19468 objc-protocol-expression:
19469 @protocol ( identifier )
19471 Returns a representation of the protocol expression. */
19473 static tree
19474 cp_parser_objc_protocol_expression (cp_parser* parser)
19476 tree proto;
19478 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
19479 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19480 proto = cp_parser_identifier (parser);
19481 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19483 return objc_build_protocol_expr (proto);
19486 /* Parse an Objective-C selector expression.
19488 objc-selector-expression:
19489 @selector ( objc-method-signature )
19491 objc-method-signature:
19492 objc-selector
19493 objc-selector-seq
19495 objc-selector-seq:
19496 objc-selector :
19497 objc-selector-seq objc-selector :
19499 Returns a representation of the method selector. */
19501 static tree
19502 cp_parser_objc_selector_expression (cp_parser* parser)
19504 tree sel_seq = NULL_TREE;
19505 bool maybe_unary_selector_p = true;
19506 cp_token *token;
19507 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
19509 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
19510 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19511 token = cp_lexer_peek_token (parser->lexer);
19513 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
19514 || token->type == CPP_SCOPE)
19516 tree selector = NULL_TREE;
19518 if (token->type != CPP_COLON
19519 || token->type == CPP_SCOPE)
19520 selector = cp_parser_objc_selector (parser);
19522 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
19523 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
19525 /* Detect if we have a unary selector. */
19526 if (maybe_unary_selector_p)
19528 sel_seq = selector;
19529 goto finish_selector;
19531 else
19533 cp_parser_error (parser, "expected %<:%>");
19536 maybe_unary_selector_p = false;
19537 token = cp_lexer_consume_token (parser->lexer);
19539 if (token->type == CPP_SCOPE)
19541 sel_seq
19542 = chainon (sel_seq,
19543 build_tree_list (selector, NULL_TREE));
19544 sel_seq
19545 = chainon (sel_seq,
19546 build_tree_list (NULL_TREE, NULL_TREE));
19548 else
19549 sel_seq
19550 = chainon (sel_seq,
19551 build_tree_list (selector, NULL_TREE));
19553 token = cp_lexer_peek_token (parser->lexer);
19556 finish_selector:
19557 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19559 return objc_build_selector_expr (loc, sel_seq);
19562 /* Parse a list of identifiers.
19564 objc-identifier-list:
19565 identifier
19566 objc-identifier-list , identifier
19568 Returns a TREE_LIST of identifier nodes. */
19570 static tree
19571 cp_parser_objc_identifier_list (cp_parser* parser)
19573 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
19574 cp_token *sep = cp_lexer_peek_token (parser->lexer);
19576 while (sep->type == CPP_COMMA)
19578 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19579 list = chainon (list,
19580 build_tree_list (NULL_TREE,
19581 cp_parser_identifier (parser)));
19582 sep = cp_lexer_peek_token (parser->lexer);
19585 return list;
19588 /* Parse an Objective-C alias declaration.
19590 objc-alias-declaration:
19591 @compatibility_alias identifier identifier ;
19593 This function registers the alias mapping with the Objective-C front end.
19594 It returns nothing. */
19596 static void
19597 cp_parser_objc_alias_declaration (cp_parser* parser)
19599 tree alias, orig;
19601 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
19602 alias = cp_parser_identifier (parser);
19603 orig = cp_parser_identifier (parser);
19604 objc_declare_alias (alias, orig);
19605 cp_parser_consume_semicolon_at_end_of_statement (parser);
19608 /* Parse an Objective-C class forward-declaration.
19610 objc-class-declaration:
19611 @class objc-identifier-list ;
19613 The function registers the forward declarations with the Objective-C
19614 front end. It returns nothing. */
19616 static void
19617 cp_parser_objc_class_declaration (cp_parser* parser)
19619 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
19620 objc_declare_class (cp_parser_objc_identifier_list (parser));
19621 cp_parser_consume_semicolon_at_end_of_statement (parser);
19624 /* Parse a list of Objective-C protocol references.
19626 objc-protocol-refs-opt:
19627 objc-protocol-refs [opt]
19629 objc-protocol-refs:
19630 < objc-identifier-list >
19632 Returns a TREE_LIST of identifiers, if any. */
19634 static tree
19635 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
19637 tree protorefs = NULL_TREE;
19639 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
19641 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
19642 protorefs = cp_parser_objc_identifier_list (parser);
19643 cp_parser_require (parser, CPP_GREATER, "%<>%>");
19646 return protorefs;
19649 /* Parse a Objective-C visibility specification. */
19651 static void
19652 cp_parser_objc_visibility_spec (cp_parser* parser)
19654 cp_token *vis = cp_lexer_peek_token (parser->lexer);
19656 switch (vis->keyword)
19658 case RID_AT_PRIVATE:
19659 objc_set_visibility (2);
19660 break;
19661 case RID_AT_PROTECTED:
19662 objc_set_visibility (0);
19663 break;
19664 case RID_AT_PUBLIC:
19665 objc_set_visibility (1);
19666 break;
19667 default:
19668 return;
19671 /* Eat '@private'/'@protected'/'@public'. */
19672 cp_lexer_consume_token (parser->lexer);
19675 /* Parse an Objective-C method type. */
19677 static void
19678 cp_parser_objc_method_type (cp_parser* parser)
19680 objc_set_method_type
19681 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
19682 ? PLUS_EXPR
19683 : MINUS_EXPR);
19686 /* Parse an Objective-C protocol qualifier. */
19688 static tree
19689 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
19691 tree quals = NULL_TREE, node;
19692 cp_token *token = cp_lexer_peek_token (parser->lexer);
19694 node = token->u.value;
19696 while (node && TREE_CODE (node) == IDENTIFIER_NODE
19697 && (node == ridpointers [(int) RID_IN]
19698 || node == ridpointers [(int) RID_OUT]
19699 || node == ridpointers [(int) RID_INOUT]
19700 || node == ridpointers [(int) RID_BYCOPY]
19701 || node == ridpointers [(int) RID_BYREF]
19702 || node == ridpointers [(int) RID_ONEWAY]))
19704 quals = tree_cons (NULL_TREE, node, quals);
19705 cp_lexer_consume_token (parser->lexer);
19706 token = cp_lexer_peek_token (parser->lexer);
19707 node = token->u.value;
19710 return quals;
19713 /* Parse an Objective-C typename. */
19715 static tree
19716 cp_parser_objc_typename (cp_parser* parser)
19718 tree type_name = NULL_TREE;
19720 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19722 tree proto_quals, cp_type = NULL_TREE;
19724 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
19725 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
19727 /* An ObjC type name may consist of just protocol qualifiers, in which
19728 case the type shall default to 'id'. */
19729 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
19730 cp_type = cp_parser_type_id (parser);
19732 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19733 type_name = build_tree_list (proto_quals, cp_type);
19736 return type_name;
19739 /* Check to see if TYPE refers to an Objective-C selector name. */
19741 static bool
19742 cp_parser_objc_selector_p (enum cpp_ttype type)
19744 return (type == CPP_NAME || type == CPP_KEYWORD
19745 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
19746 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
19747 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
19748 || type == CPP_XOR || type == CPP_XOR_EQ);
19751 /* Parse an Objective-C selector. */
19753 static tree
19754 cp_parser_objc_selector (cp_parser* parser)
19756 cp_token *token = cp_lexer_consume_token (parser->lexer);
19758 if (!cp_parser_objc_selector_p (token->type))
19760 error_at (token->location, "invalid Objective-C++ selector name");
19761 return error_mark_node;
19764 /* C++ operator names are allowed to appear in ObjC selectors. */
19765 switch (token->type)
19767 case CPP_AND_AND: return get_identifier ("and");
19768 case CPP_AND_EQ: return get_identifier ("and_eq");
19769 case CPP_AND: return get_identifier ("bitand");
19770 case CPP_OR: return get_identifier ("bitor");
19771 case CPP_COMPL: return get_identifier ("compl");
19772 case CPP_NOT: return get_identifier ("not");
19773 case CPP_NOT_EQ: return get_identifier ("not_eq");
19774 case CPP_OR_OR: return get_identifier ("or");
19775 case CPP_OR_EQ: return get_identifier ("or_eq");
19776 case CPP_XOR: return get_identifier ("xor");
19777 case CPP_XOR_EQ: return get_identifier ("xor_eq");
19778 default: return token->u.value;
19782 /* Parse an Objective-C params list. */
19784 static tree
19785 cp_parser_objc_method_keyword_params (cp_parser* parser)
19787 tree params = NULL_TREE;
19788 bool maybe_unary_selector_p = true;
19789 cp_token *token = cp_lexer_peek_token (parser->lexer);
19791 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19793 tree selector = NULL_TREE, type_name, identifier;
19795 if (token->type != CPP_COLON)
19796 selector = cp_parser_objc_selector (parser);
19798 /* Detect if we have a unary selector. */
19799 if (maybe_unary_selector_p
19800 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19801 return selector;
19803 maybe_unary_selector_p = false;
19804 cp_parser_require (parser, CPP_COLON, "%<:%>");
19805 type_name = cp_parser_objc_typename (parser);
19806 identifier = cp_parser_identifier (parser);
19808 params
19809 = chainon (params,
19810 objc_build_keyword_decl (selector,
19811 type_name,
19812 identifier));
19814 token = cp_lexer_peek_token (parser->lexer);
19817 return params;
19820 /* Parse the non-keyword Objective-C params. */
19822 static tree
19823 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
19825 tree params = make_node (TREE_LIST);
19826 cp_token *token = cp_lexer_peek_token (parser->lexer);
19827 *ellipsisp = false; /* Initially, assume no ellipsis. */
19829 while (token->type == CPP_COMMA)
19831 cp_parameter_declarator *parmdecl;
19832 tree parm;
19834 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19835 token = cp_lexer_peek_token (parser->lexer);
19837 if (token->type == CPP_ELLIPSIS)
19839 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
19840 *ellipsisp = true;
19841 break;
19844 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19845 parm = grokdeclarator (parmdecl->declarator,
19846 &parmdecl->decl_specifiers,
19847 PARM, /*initialized=*/0,
19848 /*attrlist=*/NULL);
19850 chainon (params, build_tree_list (NULL_TREE, parm));
19851 token = cp_lexer_peek_token (parser->lexer);
19854 return params;
19857 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
19859 static void
19860 cp_parser_objc_interstitial_code (cp_parser* parser)
19862 cp_token *token = cp_lexer_peek_token (parser->lexer);
19864 /* If the next token is `extern' and the following token is a string
19865 literal, then we have a linkage specification. */
19866 if (token->keyword == RID_EXTERN
19867 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
19868 cp_parser_linkage_specification (parser);
19869 /* Handle #pragma, if any. */
19870 else if (token->type == CPP_PRAGMA)
19871 cp_parser_pragma (parser, pragma_external);
19872 /* Allow stray semicolons. */
19873 else if (token->type == CPP_SEMICOLON)
19874 cp_lexer_consume_token (parser->lexer);
19875 /* Finally, try to parse a block-declaration, or a function-definition. */
19876 else
19877 cp_parser_block_declaration (parser, /*statement_p=*/false);
19880 /* Parse a method signature. */
19882 static tree
19883 cp_parser_objc_method_signature (cp_parser* parser)
19885 tree rettype, kwdparms, optparms;
19886 bool ellipsis = false;
19888 cp_parser_objc_method_type (parser);
19889 rettype = cp_parser_objc_typename (parser);
19890 kwdparms = cp_parser_objc_method_keyword_params (parser);
19891 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
19893 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
19896 /* Pars an Objective-C method prototype list. */
19898 static void
19899 cp_parser_objc_method_prototype_list (cp_parser* parser)
19901 cp_token *token = cp_lexer_peek_token (parser->lexer);
19903 while (token->keyword != RID_AT_END)
19905 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19907 objc_add_method_declaration
19908 (cp_parser_objc_method_signature (parser));
19909 cp_parser_consume_semicolon_at_end_of_statement (parser);
19911 else
19912 /* Allow for interspersed non-ObjC++ code. */
19913 cp_parser_objc_interstitial_code (parser);
19915 token = cp_lexer_peek_token (parser->lexer);
19918 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
19919 objc_finish_interface ();
19922 /* Parse an Objective-C method definition list. */
19924 static void
19925 cp_parser_objc_method_definition_list (cp_parser* parser)
19927 cp_token *token = cp_lexer_peek_token (parser->lexer);
19929 while (token->keyword != RID_AT_END)
19931 tree meth;
19933 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19935 push_deferring_access_checks (dk_deferred);
19936 objc_start_method_definition
19937 (cp_parser_objc_method_signature (parser));
19939 /* For historical reasons, we accept an optional semicolon. */
19940 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19941 cp_lexer_consume_token (parser->lexer);
19943 perform_deferred_access_checks ();
19944 stop_deferring_access_checks ();
19945 meth = cp_parser_function_definition_after_declarator (parser,
19946 false);
19947 pop_deferring_access_checks ();
19948 objc_finish_method_definition (meth);
19950 else
19951 /* Allow for interspersed non-ObjC++ code. */
19952 cp_parser_objc_interstitial_code (parser);
19954 token = cp_lexer_peek_token (parser->lexer);
19957 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
19958 objc_finish_implementation ();
19961 /* Parse Objective-C ivars. */
19963 static void
19964 cp_parser_objc_class_ivars (cp_parser* parser)
19966 cp_token *token = cp_lexer_peek_token (parser->lexer);
19968 if (token->type != CPP_OPEN_BRACE)
19969 return; /* No ivars specified. */
19971 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
19972 token = cp_lexer_peek_token (parser->lexer);
19974 while (token->type != CPP_CLOSE_BRACE)
19976 cp_decl_specifier_seq declspecs;
19977 int decl_class_or_enum_p;
19978 tree prefix_attributes;
19980 cp_parser_objc_visibility_spec (parser);
19982 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
19983 break;
19985 cp_parser_decl_specifier_seq (parser,
19986 CP_PARSER_FLAGS_OPTIONAL,
19987 &declspecs,
19988 &decl_class_or_enum_p);
19989 prefix_attributes = declspecs.attributes;
19990 declspecs.attributes = NULL_TREE;
19992 /* Keep going until we hit the `;' at the end of the
19993 declaration. */
19994 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19996 tree width = NULL_TREE, attributes, first_attribute, decl;
19997 cp_declarator *declarator = NULL;
19998 int ctor_dtor_or_conv_p;
20000 /* Check for a (possibly unnamed) bitfield declaration. */
20001 token = cp_lexer_peek_token (parser->lexer);
20002 if (token->type == CPP_COLON)
20003 goto eat_colon;
20005 if (token->type == CPP_NAME
20006 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
20007 == CPP_COLON))
20009 /* Get the name of the bitfield. */
20010 declarator = make_id_declarator (NULL_TREE,
20011 cp_parser_identifier (parser),
20012 sfk_none);
20014 eat_colon:
20015 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
20016 /* Get the width of the bitfield. */
20017 width
20018 = cp_parser_constant_expression (parser,
20019 /*allow_non_constant=*/false,
20020 NULL);
20022 else
20024 /* Parse the declarator. */
20025 declarator
20026 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20027 &ctor_dtor_or_conv_p,
20028 /*parenthesized_p=*/NULL,
20029 /*member_p=*/false);
20032 /* Look for attributes that apply to the ivar. */
20033 attributes = cp_parser_attributes_opt (parser);
20034 /* Remember which attributes are prefix attributes and
20035 which are not. */
20036 first_attribute = attributes;
20037 /* Combine the attributes. */
20038 attributes = chainon (prefix_attributes, attributes);
20040 if (width)
20041 /* Create the bitfield declaration. */
20042 decl = grokbitfield (declarator, &declspecs,
20043 width,
20044 attributes);
20045 else
20046 decl = grokfield (declarator, &declspecs,
20047 NULL_TREE, /*init_const_expr_p=*/false,
20048 NULL_TREE, attributes);
20050 /* Add the instance variable. */
20051 objc_add_instance_variable (decl);
20053 /* Reset PREFIX_ATTRIBUTES. */
20054 while (attributes && TREE_CHAIN (attributes) != first_attribute)
20055 attributes = TREE_CHAIN (attributes);
20056 if (attributes)
20057 TREE_CHAIN (attributes) = NULL_TREE;
20059 token = cp_lexer_peek_token (parser->lexer);
20061 if (token->type == CPP_COMMA)
20063 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
20064 continue;
20066 break;
20069 cp_parser_consume_semicolon_at_end_of_statement (parser);
20070 token = cp_lexer_peek_token (parser->lexer);
20073 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
20074 /* For historical reasons, we accept an optional semicolon. */
20075 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20076 cp_lexer_consume_token (parser->lexer);
20079 /* Parse an Objective-C protocol declaration. */
20081 static void
20082 cp_parser_objc_protocol_declaration (cp_parser* parser)
20084 tree proto, protorefs;
20085 cp_token *tok;
20087 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
20088 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
20090 tok = cp_lexer_peek_token (parser->lexer);
20091 error_at (tok->location, "identifier expected after %<@protocol%>");
20092 goto finish;
20095 /* See if we have a forward declaration or a definition. */
20096 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
20098 /* Try a forward declaration first. */
20099 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
20101 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
20102 finish:
20103 cp_parser_consume_semicolon_at_end_of_statement (parser);
20106 /* Ok, we got a full-fledged definition (or at least should). */
20107 else
20109 proto = cp_parser_identifier (parser);
20110 protorefs = cp_parser_objc_protocol_refs_opt (parser);
20111 objc_start_protocol (proto, protorefs);
20112 cp_parser_objc_method_prototype_list (parser);
20116 /* Parse an Objective-C superclass or category. */
20118 static void
20119 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
20120 tree *categ)
20122 cp_token *next = cp_lexer_peek_token (parser->lexer);
20124 *super = *categ = NULL_TREE;
20125 if (next->type == CPP_COLON)
20127 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
20128 *super = cp_parser_identifier (parser);
20130 else if (next->type == CPP_OPEN_PAREN)
20132 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
20133 *categ = cp_parser_identifier (parser);
20134 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20138 /* Parse an Objective-C class interface. */
20140 static void
20141 cp_parser_objc_class_interface (cp_parser* parser)
20143 tree name, super, categ, protos;
20145 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
20146 name = cp_parser_identifier (parser);
20147 cp_parser_objc_superclass_or_category (parser, &super, &categ);
20148 protos = cp_parser_objc_protocol_refs_opt (parser);
20150 /* We have either a class or a category on our hands. */
20151 if (categ)
20152 objc_start_category_interface (name, categ, protos);
20153 else
20155 objc_start_class_interface (name, super, protos);
20156 /* Handle instance variable declarations, if any. */
20157 cp_parser_objc_class_ivars (parser);
20158 objc_continue_interface ();
20161 cp_parser_objc_method_prototype_list (parser);
20164 /* Parse an Objective-C class implementation. */
20166 static void
20167 cp_parser_objc_class_implementation (cp_parser* parser)
20169 tree name, super, categ;
20171 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
20172 name = cp_parser_identifier (parser);
20173 cp_parser_objc_superclass_or_category (parser, &super, &categ);
20175 /* We have either a class or a category on our hands. */
20176 if (categ)
20177 objc_start_category_implementation (name, categ);
20178 else
20180 objc_start_class_implementation (name, super);
20181 /* Handle instance variable declarations, if any. */
20182 cp_parser_objc_class_ivars (parser);
20183 objc_continue_implementation ();
20186 cp_parser_objc_method_definition_list (parser);
20189 /* Consume the @end token and finish off the implementation. */
20191 static void
20192 cp_parser_objc_end_implementation (cp_parser* parser)
20194 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
20195 objc_finish_implementation ();
20198 /* Parse an Objective-C declaration. */
20200 static void
20201 cp_parser_objc_declaration (cp_parser* parser)
20203 /* Try to figure out what kind of declaration is present. */
20204 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20206 switch (kwd->keyword)
20208 case RID_AT_ALIAS:
20209 cp_parser_objc_alias_declaration (parser);
20210 break;
20211 case RID_AT_CLASS:
20212 cp_parser_objc_class_declaration (parser);
20213 break;
20214 case RID_AT_PROTOCOL:
20215 cp_parser_objc_protocol_declaration (parser);
20216 break;
20217 case RID_AT_INTERFACE:
20218 cp_parser_objc_class_interface (parser);
20219 break;
20220 case RID_AT_IMPLEMENTATION:
20221 cp_parser_objc_class_implementation (parser);
20222 break;
20223 case RID_AT_END:
20224 cp_parser_objc_end_implementation (parser);
20225 break;
20226 default:
20227 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
20228 kwd->u.value);
20229 cp_parser_skip_to_end_of_block_or_statement (parser);
20233 /* Parse an Objective-C try-catch-finally statement.
20235 objc-try-catch-finally-stmt:
20236 @try compound-statement objc-catch-clause-seq [opt]
20237 objc-finally-clause [opt]
20239 objc-catch-clause-seq:
20240 objc-catch-clause objc-catch-clause-seq [opt]
20242 objc-catch-clause:
20243 @catch ( exception-declaration ) compound-statement
20245 objc-finally-clause
20246 @finally compound-statement
20248 Returns NULL_TREE. */
20250 static tree
20251 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
20252 location_t location;
20253 tree stmt;
20255 cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
20256 location = cp_lexer_peek_token (parser->lexer)->location;
20257 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
20258 node, lest it get absorbed into the surrounding block. */
20259 stmt = push_stmt_list ();
20260 cp_parser_compound_statement (parser, NULL, false);
20261 objc_begin_try_stmt (location, pop_stmt_list (stmt));
20263 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
20265 cp_parameter_declarator *parmdecl;
20266 tree parm;
20268 cp_lexer_consume_token (parser->lexer);
20269 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20270 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20271 parm = grokdeclarator (parmdecl->declarator,
20272 &parmdecl->decl_specifiers,
20273 PARM, /*initialized=*/0,
20274 /*attrlist=*/NULL);
20275 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20276 objc_begin_catch_clause (parm);
20277 cp_parser_compound_statement (parser, NULL, false);
20278 objc_finish_catch_clause ();
20281 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
20283 cp_lexer_consume_token (parser->lexer);
20284 location = cp_lexer_peek_token (parser->lexer)->location;
20285 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
20286 node, lest it get absorbed into the surrounding block. */
20287 stmt = push_stmt_list ();
20288 cp_parser_compound_statement (parser, NULL, false);
20289 objc_build_finally_clause (location, pop_stmt_list (stmt));
20292 return objc_finish_try_stmt ();
20295 /* Parse an Objective-C synchronized statement.
20297 objc-synchronized-stmt:
20298 @synchronized ( expression ) compound-statement
20300 Returns NULL_TREE. */
20302 static tree
20303 cp_parser_objc_synchronized_statement (cp_parser *parser) {
20304 location_t location;
20305 tree lock, stmt;
20307 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
20309 location = cp_lexer_peek_token (parser->lexer)->location;
20310 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20311 lock = cp_parser_expression (parser, false, NULL);
20312 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20314 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
20315 node, lest it get absorbed into the surrounding block. */
20316 stmt = push_stmt_list ();
20317 cp_parser_compound_statement (parser, NULL, false);
20319 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
20322 /* Parse an Objective-C throw statement.
20324 objc-throw-stmt:
20325 @throw assignment-expression [opt] ;
20327 Returns a constructed '@throw' statement. */
20329 static tree
20330 cp_parser_objc_throw_statement (cp_parser *parser) {
20331 tree expr = NULL_TREE;
20332 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
20334 cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
20336 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20337 expr = cp_parser_assignment_expression (parser, false, NULL);
20339 cp_parser_consume_semicolon_at_end_of_statement (parser);
20341 return objc_build_throw_stmt (loc, expr);
20344 /* Parse an Objective-C statement. */
20346 static tree
20347 cp_parser_objc_statement (cp_parser * parser) {
20348 /* Try to figure out what kind of declaration is present. */
20349 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20351 switch (kwd->keyword)
20353 case RID_AT_TRY:
20354 return cp_parser_objc_try_catch_finally_statement (parser);
20355 case RID_AT_SYNCHRONIZED:
20356 return cp_parser_objc_synchronized_statement (parser);
20357 case RID_AT_THROW:
20358 return cp_parser_objc_throw_statement (parser);
20359 default:
20360 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
20361 kwd->u.value);
20362 cp_parser_skip_to_end_of_block_or_statement (parser);
20365 return error_mark_node;
20368 /* OpenMP 2.5 parsing routines. */
20370 /* Returns name of the next clause.
20371 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
20372 the token is not consumed. Otherwise appropriate pragma_omp_clause is
20373 returned and the token is consumed. */
20375 static pragma_omp_clause
20376 cp_parser_omp_clause_name (cp_parser *parser)
20378 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
20380 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
20381 result = PRAGMA_OMP_CLAUSE_IF;
20382 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
20383 result = PRAGMA_OMP_CLAUSE_DEFAULT;
20384 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
20385 result = PRAGMA_OMP_CLAUSE_PRIVATE;
20386 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20388 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20389 const char *p = IDENTIFIER_POINTER (id);
20391 switch (p[0])
20393 case 'c':
20394 if (!strcmp ("collapse", p))
20395 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
20396 else if (!strcmp ("copyin", p))
20397 result = PRAGMA_OMP_CLAUSE_COPYIN;
20398 else if (!strcmp ("copyprivate", p))
20399 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
20400 break;
20401 case 'f':
20402 if (!strcmp ("firstprivate", p))
20403 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
20404 break;
20405 case 'l':
20406 if (!strcmp ("lastprivate", p))
20407 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
20408 break;
20409 case 'n':
20410 if (!strcmp ("nowait", p))
20411 result = PRAGMA_OMP_CLAUSE_NOWAIT;
20412 else if (!strcmp ("num_threads", p))
20413 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
20414 break;
20415 case 'o':
20416 if (!strcmp ("ordered", p))
20417 result = PRAGMA_OMP_CLAUSE_ORDERED;
20418 break;
20419 case 'r':
20420 if (!strcmp ("reduction", p))
20421 result = PRAGMA_OMP_CLAUSE_REDUCTION;
20422 break;
20423 case 's':
20424 if (!strcmp ("schedule", p))
20425 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
20426 else if (!strcmp ("shared", p))
20427 result = PRAGMA_OMP_CLAUSE_SHARED;
20428 break;
20429 case 'u':
20430 if (!strcmp ("untied", p))
20431 result = PRAGMA_OMP_CLAUSE_UNTIED;
20432 break;
20436 if (result != PRAGMA_OMP_CLAUSE_NONE)
20437 cp_lexer_consume_token (parser->lexer);
20439 return result;
20442 /* Validate that a clause of the given type does not already exist. */
20444 static void
20445 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
20446 const char *name, location_t location)
20448 tree c;
20450 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
20451 if (OMP_CLAUSE_CODE (c) == code)
20453 error_at (location, "too many %qs clauses", name);
20454 break;
20458 /* OpenMP 2.5:
20459 variable-list:
20460 identifier
20461 variable-list , identifier
20463 In addition, we match a closing parenthesis. An opening parenthesis
20464 will have been consumed by the caller.
20466 If KIND is nonzero, create the appropriate node and install the decl
20467 in OMP_CLAUSE_DECL and add the node to the head of the list.
20469 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
20470 return the list created. */
20472 static tree
20473 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
20474 tree list)
20476 cp_token *token;
20477 while (1)
20479 tree name, decl;
20481 token = cp_lexer_peek_token (parser->lexer);
20482 name = cp_parser_id_expression (parser, /*template_p=*/false,
20483 /*check_dependency_p=*/true,
20484 /*template_p=*/NULL,
20485 /*declarator_p=*/false,
20486 /*optional_p=*/false);
20487 if (name == error_mark_node)
20488 goto skip_comma;
20490 decl = cp_parser_lookup_name_simple (parser, name, token->location);
20491 if (decl == error_mark_node)
20492 cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
20493 else if (kind != 0)
20495 tree u = build_omp_clause (token->location, kind);
20496 OMP_CLAUSE_DECL (u) = decl;
20497 OMP_CLAUSE_CHAIN (u) = list;
20498 list = u;
20500 else
20501 list = tree_cons (decl, NULL_TREE, list);
20503 get_comma:
20504 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20505 break;
20506 cp_lexer_consume_token (parser->lexer);
20509 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20511 int ending;
20513 /* Try to resync to an unnested comma. Copied from
20514 cp_parser_parenthesized_expression_list. */
20515 skip_comma:
20516 ending = cp_parser_skip_to_closing_parenthesis (parser,
20517 /*recovering=*/true,
20518 /*or_comma=*/true,
20519 /*consume_paren=*/true);
20520 if (ending < 0)
20521 goto get_comma;
20524 return list;
20527 /* Similarly, but expect leading and trailing parenthesis. This is a very
20528 common case for omp clauses. */
20530 static tree
20531 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
20533 if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20534 return cp_parser_omp_var_list_no_open (parser, kind, list);
20535 return list;
20538 /* OpenMP 3.0:
20539 collapse ( constant-expression ) */
20541 static tree
20542 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
20544 tree c, num;
20545 location_t loc;
20546 HOST_WIDE_INT n;
20548 loc = cp_lexer_peek_token (parser->lexer)->location;
20549 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20550 return list;
20552 num = cp_parser_constant_expression (parser, false, NULL);
20554 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20555 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20556 /*or_comma=*/false,
20557 /*consume_paren=*/true);
20559 if (num == error_mark_node)
20560 return list;
20561 num = fold_non_dependent_expr (num);
20562 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
20563 || !host_integerp (num, 0)
20564 || (n = tree_low_cst (num, 0)) <= 0
20565 || (int) n != n)
20567 error_at (loc, "collapse argument needs positive constant integer expression");
20568 return list;
20571 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
20572 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
20573 OMP_CLAUSE_CHAIN (c) = list;
20574 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
20576 return c;
20579 /* OpenMP 2.5:
20580 default ( shared | none ) */
20582 static tree
20583 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
20585 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
20586 tree c;
20588 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20589 return list;
20590 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20592 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20593 const char *p = IDENTIFIER_POINTER (id);
20595 switch (p[0])
20597 case 'n':
20598 if (strcmp ("none", p) != 0)
20599 goto invalid_kind;
20600 kind = OMP_CLAUSE_DEFAULT_NONE;
20601 break;
20603 case 's':
20604 if (strcmp ("shared", p) != 0)
20605 goto invalid_kind;
20606 kind = OMP_CLAUSE_DEFAULT_SHARED;
20607 break;
20609 default:
20610 goto invalid_kind;
20613 cp_lexer_consume_token (parser->lexer);
20615 else
20617 invalid_kind:
20618 cp_parser_error (parser, "expected %<none%> or %<shared%>");
20621 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20622 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20623 /*or_comma=*/false,
20624 /*consume_paren=*/true);
20626 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
20627 return list;
20629 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
20630 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
20631 OMP_CLAUSE_CHAIN (c) = list;
20632 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
20634 return c;
20637 /* OpenMP 2.5:
20638 if ( expression ) */
20640 static tree
20641 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
20643 tree t, c;
20645 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20646 return list;
20648 t = cp_parser_condition (parser);
20650 if (t == error_mark_node
20651 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20652 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20653 /*or_comma=*/false,
20654 /*consume_paren=*/true);
20656 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
20658 c = build_omp_clause (location, OMP_CLAUSE_IF);
20659 OMP_CLAUSE_IF_EXPR (c) = t;
20660 OMP_CLAUSE_CHAIN (c) = list;
20662 return c;
20665 /* OpenMP 2.5:
20666 nowait */
20668 static tree
20669 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
20670 tree list, location_t location)
20672 tree c;
20674 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
20676 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
20677 OMP_CLAUSE_CHAIN (c) = list;
20678 return c;
20681 /* OpenMP 2.5:
20682 num_threads ( expression ) */
20684 static tree
20685 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
20686 location_t location)
20688 tree t, c;
20690 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20691 return list;
20693 t = cp_parser_expression (parser, false, NULL);
20695 if (t == error_mark_node
20696 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20697 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20698 /*or_comma=*/false,
20699 /*consume_paren=*/true);
20701 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
20702 "num_threads", location);
20704 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
20705 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
20706 OMP_CLAUSE_CHAIN (c) = list;
20708 return c;
20711 /* OpenMP 2.5:
20712 ordered */
20714 static tree
20715 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
20716 tree list, location_t location)
20718 tree c;
20720 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
20721 "ordered", location);
20723 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
20724 OMP_CLAUSE_CHAIN (c) = list;
20725 return c;
20728 /* OpenMP 2.5:
20729 reduction ( reduction-operator : variable-list )
20731 reduction-operator:
20732 One of: + * - & ^ | && || */
20734 static tree
20735 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
20737 enum tree_code code;
20738 tree nlist, c;
20740 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20741 return list;
20743 switch (cp_lexer_peek_token (parser->lexer)->type)
20745 case CPP_PLUS:
20746 code = PLUS_EXPR;
20747 break;
20748 case CPP_MULT:
20749 code = MULT_EXPR;
20750 break;
20751 case CPP_MINUS:
20752 code = MINUS_EXPR;
20753 break;
20754 case CPP_AND:
20755 code = BIT_AND_EXPR;
20756 break;
20757 case CPP_XOR:
20758 code = BIT_XOR_EXPR;
20759 break;
20760 case CPP_OR:
20761 code = BIT_IOR_EXPR;
20762 break;
20763 case CPP_AND_AND:
20764 code = TRUTH_ANDIF_EXPR;
20765 break;
20766 case CPP_OR_OR:
20767 code = TRUTH_ORIF_EXPR;
20768 break;
20769 default:
20770 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
20771 "%<|%>, %<&&%>, or %<||%>");
20772 resync_fail:
20773 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20774 /*or_comma=*/false,
20775 /*consume_paren=*/true);
20776 return list;
20778 cp_lexer_consume_token (parser->lexer);
20780 if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
20781 goto resync_fail;
20783 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
20784 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
20785 OMP_CLAUSE_REDUCTION_CODE (c) = code;
20787 return nlist;
20790 /* OpenMP 2.5:
20791 schedule ( schedule-kind )
20792 schedule ( schedule-kind , expression )
20794 schedule-kind:
20795 static | dynamic | guided | runtime | auto */
20797 static tree
20798 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
20800 tree c, t;
20802 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20803 return list;
20805 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
20807 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20809 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20810 const char *p = IDENTIFIER_POINTER (id);
20812 switch (p[0])
20814 case 'd':
20815 if (strcmp ("dynamic", p) != 0)
20816 goto invalid_kind;
20817 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
20818 break;
20820 case 'g':
20821 if (strcmp ("guided", p) != 0)
20822 goto invalid_kind;
20823 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
20824 break;
20826 case 'r':
20827 if (strcmp ("runtime", p) != 0)
20828 goto invalid_kind;
20829 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
20830 break;
20832 default:
20833 goto invalid_kind;
20836 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
20837 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
20838 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
20839 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
20840 else
20841 goto invalid_kind;
20842 cp_lexer_consume_token (parser->lexer);
20844 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20846 cp_token *token;
20847 cp_lexer_consume_token (parser->lexer);
20849 token = cp_lexer_peek_token (parser->lexer);
20850 t = cp_parser_assignment_expression (parser, false, NULL);
20852 if (t == error_mark_node)
20853 goto resync_fail;
20854 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
20855 error_at (token->location, "schedule %<runtime%> does not take "
20856 "a %<chunk_size%> parameter");
20857 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
20858 error_at (token->location, "schedule %<auto%> does not take "
20859 "a %<chunk_size%> parameter");
20860 else
20861 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
20863 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20864 goto resync_fail;
20866 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
20867 goto resync_fail;
20869 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
20870 OMP_CLAUSE_CHAIN (c) = list;
20871 return c;
20873 invalid_kind:
20874 cp_parser_error (parser, "invalid schedule kind");
20875 resync_fail:
20876 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20877 /*or_comma=*/false,
20878 /*consume_paren=*/true);
20879 return list;
20882 /* OpenMP 3.0:
20883 untied */
20885 static tree
20886 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
20887 tree list, location_t location)
20889 tree c;
20891 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
20893 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
20894 OMP_CLAUSE_CHAIN (c) = list;
20895 return c;
20898 /* Parse all OpenMP clauses. The set clauses allowed by the directive
20899 is a bitmask in MASK. Return the list of clauses found; the result
20900 of clause default goes in *pdefault. */
20902 static tree
20903 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
20904 const char *where, cp_token *pragma_tok)
20906 tree clauses = NULL;
20907 bool first = true;
20908 cp_token *token = NULL;
20910 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
20912 pragma_omp_clause c_kind;
20913 const char *c_name;
20914 tree prev = clauses;
20916 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20917 cp_lexer_consume_token (parser->lexer);
20919 token = cp_lexer_peek_token (parser->lexer);
20920 c_kind = cp_parser_omp_clause_name (parser);
20921 first = false;
20923 switch (c_kind)
20925 case PRAGMA_OMP_CLAUSE_COLLAPSE:
20926 clauses = cp_parser_omp_clause_collapse (parser, clauses,
20927 token->location);
20928 c_name = "collapse";
20929 break;
20930 case PRAGMA_OMP_CLAUSE_COPYIN:
20931 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
20932 c_name = "copyin";
20933 break;
20934 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
20935 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
20936 clauses);
20937 c_name = "copyprivate";
20938 break;
20939 case PRAGMA_OMP_CLAUSE_DEFAULT:
20940 clauses = cp_parser_omp_clause_default (parser, clauses,
20941 token->location);
20942 c_name = "default";
20943 break;
20944 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
20945 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
20946 clauses);
20947 c_name = "firstprivate";
20948 break;
20949 case PRAGMA_OMP_CLAUSE_IF:
20950 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
20951 c_name = "if";
20952 break;
20953 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
20954 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
20955 clauses);
20956 c_name = "lastprivate";
20957 break;
20958 case PRAGMA_OMP_CLAUSE_NOWAIT:
20959 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
20960 c_name = "nowait";
20961 break;
20962 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
20963 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
20964 token->location);
20965 c_name = "num_threads";
20966 break;
20967 case PRAGMA_OMP_CLAUSE_ORDERED:
20968 clauses = cp_parser_omp_clause_ordered (parser, clauses,
20969 token->location);
20970 c_name = "ordered";
20971 break;
20972 case PRAGMA_OMP_CLAUSE_PRIVATE:
20973 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
20974 clauses);
20975 c_name = "private";
20976 break;
20977 case PRAGMA_OMP_CLAUSE_REDUCTION:
20978 clauses = cp_parser_omp_clause_reduction (parser, clauses);
20979 c_name = "reduction";
20980 break;
20981 case PRAGMA_OMP_CLAUSE_SCHEDULE:
20982 clauses = cp_parser_omp_clause_schedule (parser, clauses,
20983 token->location);
20984 c_name = "schedule";
20985 break;
20986 case PRAGMA_OMP_CLAUSE_SHARED:
20987 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
20988 clauses);
20989 c_name = "shared";
20990 break;
20991 case PRAGMA_OMP_CLAUSE_UNTIED:
20992 clauses = cp_parser_omp_clause_untied (parser, clauses,
20993 token->location);
20994 c_name = "nowait";
20995 break;
20996 default:
20997 cp_parser_error (parser, "expected %<#pragma omp%> clause");
20998 goto saw_error;
21001 if (((mask >> c_kind) & 1) == 0)
21003 /* Remove the invalid clause(s) from the list to avoid
21004 confusing the rest of the compiler. */
21005 clauses = prev;
21006 error_at (token->location, "%qs is not valid for %qs", c_name, where);
21009 saw_error:
21010 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
21011 return finish_omp_clauses (clauses);
21014 /* OpenMP 2.5:
21015 structured-block:
21016 statement
21018 In practice, we're also interested in adding the statement to an
21019 outer node. So it is convenient if we work around the fact that
21020 cp_parser_statement calls add_stmt. */
21022 static unsigned
21023 cp_parser_begin_omp_structured_block (cp_parser *parser)
21025 unsigned save = parser->in_statement;
21027 /* Only move the values to IN_OMP_BLOCK if they weren't false.
21028 This preserves the "not within loop or switch" style error messages
21029 for nonsense cases like
21030 void foo() {
21031 #pragma omp single
21032 break;
21035 if (parser->in_statement)
21036 parser->in_statement = IN_OMP_BLOCK;
21038 return save;
21041 static void
21042 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
21044 parser->in_statement = save;
21047 static tree
21048 cp_parser_omp_structured_block (cp_parser *parser)
21050 tree stmt = begin_omp_structured_block ();
21051 unsigned int save = cp_parser_begin_omp_structured_block (parser);
21053 cp_parser_statement (parser, NULL_TREE, false, NULL);
21055 cp_parser_end_omp_structured_block (parser, save);
21056 return finish_omp_structured_block (stmt);
21059 /* OpenMP 2.5:
21060 # pragma omp atomic new-line
21061 expression-stmt
21063 expression-stmt:
21064 x binop= expr | x++ | ++x | x-- | --x
21065 binop:
21066 +, *, -, /, &, ^, |, <<, >>
21068 where x is an lvalue expression with scalar type. */
21070 static void
21071 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
21073 tree lhs, rhs;
21074 enum tree_code code;
21076 cp_parser_require_pragma_eol (parser, pragma_tok);
21078 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
21079 /*cast_p=*/false, NULL);
21080 switch (TREE_CODE (lhs))
21082 case ERROR_MARK:
21083 goto saw_error;
21085 case PREINCREMENT_EXPR:
21086 case POSTINCREMENT_EXPR:
21087 lhs = TREE_OPERAND (lhs, 0);
21088 code = PLUS_EXPR;
21089 rhs = integer_one_node;
21090 break;
21092 case PREDECREMENT_EXPR:
21093 case POSTDECREMENT_EXPR:
21094 lhs = TREE_OPERAND (lhs, 0);
21095 code = MINUS_EXPR;
21096 rhs = integer_one_node;
21097 break;
21099 default:
21100 switch (cp_lexer_peek_token (parser->lexer)->type)
21102 case CPP_MULT_EQ:
21103 code = MULT_EXPR;
21104 break;
21105 case CPP_DIV_EQ:
21106 code = TRUNC_DIV_EXPR;
21107 break;
21108 case CPP_PLUS_EQ:
21109 code = PLUS_EXPR;
21110 break;
21111 case CPP_MINUS_EQ:
21112 code = MINUS_EXPR;
21113 break;
21114 case CPP_LSHIFT_EQ:
21115 code = LSHIFT_EXPR;
21116 break;
21117 case CPP_RSHIFT_EQ:
21118 code = RSHIFT_EXPR;
21119 break;
21120 case CPP_AND_EQ:
21121 code = BIT_AND_EXPR;
21122 break;
21123 case CPP_OR_EQ:
21124 code = BIT_IOR_EXPR;
21125 break;
21126 case CPP_XOR_EQ:
21127 code = BIT_XOR_EXPR;
21128 break;
21129 default:
21130 cp_parser_error (parser,
21131 "invalid operator for %<#pragma omp atomic%>");
21132 goto saw_error;
21134 cp_lexer_consume_token (parser->lexer);
21136 rhs = cp_parser_expression (parser, false, NULL);
21137 if (rhs == error_mark_node)
21138 goto saw_error;
21139 break;
21141 finish_omp_atomic (code, lhs, rhs);
21142 cp_parser_consume_semicolon_at_end_of_statement (parser);
21143 return;
21145 saw_error:
21146 cp_parser_skip_to_end_of_block_or_statement (parser);
21150 /* OpenMP 2.5:
21151 # pragma omp barrier new-line */
21153 static void
21154 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
21156 cp_parser_require_pragma_eol (parser, pragma_tok);
21157 finish_omp_barrier ();
21160 /* OpenMP 2.5:
21161 # pragma omp critical [(name)] new-line
21162 structured-block */
21164 static tree
21165 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
21167 tree stmt, name = NULL;
21169 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21171 cp_lexer_consume_token (parser->lexer);
21173 name = cp_parser_identifier (parser);
21175 if (name == error_mark_node
21176 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21177 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21178 /*or_comma=*/false,
21179 /*consume_paren=*/true);
21180 if (name == error_mark_node)
21181 name = NULL;
21183 cp_parser_require_pragma_eol (parser, pragma_tok);
21185 stmt = cp_parser_omp_structured_block (parser);
21186 return c_finish_omp_critical (input_location, stmt, name);
21189 /* OpenMP 2.5:
21190 # pragma omp flush flush-vars[opt] new-line
21192 flush-vars:
21193 ( variable-list ) */
21195 static void
21196 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
21198 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21199 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
21200 cp_parser_require_pragma_eol (parser, pragma_tok);
21202 finish_omp_flush ();
21205 /* Helper function, to parse omp for increment expression. */
21207 static tree
21208 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
21210 tree cond = cp_parser_binary_expression (parser, false, true,
21211 PREC_NOT_OPERATOR, NULL);
21212 bool overloaded_p;
21214 if (cond == error_mark_node
21215 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21217 cp_parser_skip_to_end_of_statement (parser);
21218 return error_mark_node;
21221 switch (TREE_CODE (cond))
21223 case GT_EXPR:
21224 case GE_EXPR:
21225 case LT_EXPR:
21226 case LE_EXPR:
21227 break;
21228 default:
21229 return error_mark_node;
21232 /* If decl is an iterator, preserve LHS and RHS of the relational
21233 expr until finish_omp_for. */
21234 if (decl
21235 && (type_dependent_expression_p (decl)
21236 || CLASS_TYPE_P (TREE_TYPE (decl))))
21237 return cond;
21239 return build_x_binary_op (TREE_CODE (cond),
21240 TREE_OPERAND (cond, 0), ERROR_MARK,
21241 TREE_OPERAND (cond, 1), ERROR_MARK,
21242 &overloaded_p, tf_warning_or_error);
21245 /* Helper function, to parse omp for increment expression. */
21247 static tree
21248 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
21250 cp_token *token = cp_lexer_peek_token (parser->lexer);
21251 enum tree_code op;
21252 tree lhs, rhs;
21253 cp_id_kind idk;
21254 bool decl_first;
21256 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21258 op = (token->type == CPP_PLUS_PLUS
21259 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
21260 cp_lexer_consume_token (parser->lexer);
21261 lhs = cp_parser_cast_expression (parser, false, false, NULL);
21262 if (lhs != decl)
21263 return error_mark_node;
21264 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21267 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
21268 if (lhs != decl)
21269 return error_mark_node;
21271 token = cp_lexer_peek_token (parser->lexer);
21272 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21274 op = (token->type == CPP_PLUS_PLUS
21275 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
21276 cp_lexer_consume_token (parser->lexer);
21277 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21280 op = cp_parser_assignment_operator_opt (parser);
21281 if (op == ERROR_MARK)
21282 return error_mark_node;
21284 if (op != NOP_EXPR)
21286 rhs = cp_parser_assignment_expression (parser, false, NULL);
21287 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
21288 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21291 lhs = cp_parser_binary_expression (parser, false, false,
21292 PREC_ADDITIVE_EXPRESSION, NULL);
21293 token = cp_lexer_peek_token (parser->lexer);
21294 decl_first = lhs == decl;
21295 if (decl_first)
21296 lhs = NULL_TREE;
21297 if (token->type != CPP_PLUS
21298 && token->type != CPP_MINUS)
21299 return error_mark_node;
21303 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
21304 cp_lexer_consume_token (parser->lexer);
21305 rhs = cp_parser_binary_expression (parser, false, false,
21306 PREC_ADDITIVE_EXPRESSION, NULL);
21307 token = cp_lexer_peek_token (parser->lexer);
21308 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
21310 if (lhs == NULL_TREE)
21312 if (op == PLUS_EXPR)
21313 lhs = rhs;
21314 else
21315 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
21317 else
21318 lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
21319 NULL, tf_warning_or_error);
21322 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
21324 if (!decl_first)
21326 if (rhs != decl || op == MINUS_EXPR)
21327 return error_mark_node;
21328 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
21330 else
21331 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
21333 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21336 /* Parse the restricted form of the for statement allowed by OpenMP. */
21338 static tree
21339 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
21341 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
21342 tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
21343 tree this_pre_body, cl;
21344 location_t loc_first;
21345 bool collapse_err = false;
21346 int i, collapse = 1, nbraces = 0;
21348 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
21349 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
21350 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
21352 gcc_assert (collapse >= 1);
21354 declv = make_tree_vec (collapse);
21355 initv = make_tree_vec (collapse);
21356 condv = make_tree_vec (collapse);
21357 incrv = make_tree_vec (collapse);
21359 loc_first = cp_lexer_peek_token (parser->lexer)->location;
21361 for (i = 0; i < collapse; i++)
21363 int bracecount = 0;
21364 bool add_private_clause = false;
21365 location_t loc;
21367 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21369 cp_parser_error (parser, "for statement expected");
21370 return NULL;
21372 loc = cp_lexer_consume_token (parser->lexer)->location;
21374 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21375 return NULL;
21377 init = decl = real_decl = NULL;
21378 this_pre_body = push_stmt_list ();
21379 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21381 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
21383 init-expr:
21384 var = lb
21385 integer-type var = lb
21386 random-access-iterator-type var = lb
21387 pointer-type var = lb
21389 cp_decl_specifier_seq type_specifiers;
21391 /* First, try to parse as an initialized declaration. See
21392 cp_parser_condition, from whence the bulk of this is copied. */
21394 cp_parser_parse_tentatively (parser);
21395 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
21396 &type_specifiers);
21397 if (cp_parser_parse_definitely (parser))
21399 /* If parsing a type specifier seq succeeded, then this
21400 MUST be a initialized declaration. */
21401 tree asm_specification, attributes;
21402 cp_declarator *declarator;
21404 declarator = cp_parser_declarator (parser,
21405 CP_PARSER_DECLARATOR_NAMED,
21406 /*ctor_dtor_or_conv_p=*/NULL,
21407 /*parenthesized_p=*/NULL,
21408 /*member_p=*/false);
21409 attributes = cp_parser_attributes_opt (parser);
21410 asm_specification = cp_parser_asm_specification_opt (parser);
21412 if (declarator == cp_error_declarator)
21413 cp_parser_skip_to_end_of_statement (parser);
21415 else
21417 tree pushed_scope, auto_node;
21419 decl = start_decl (declarator, &type_specifiers,
21420 SD_INITIALIZED, attributes,
21421 /*prefix_attributes=*/NULL_TREE,
21422 &pushed_scope);
21424 auto_node = type_uses_auto (TREE_TYPE (decl));
21425 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
21427 if (cp_lexer_next_token_is (parser->lexer,
21428 CPP_OPEN_PAREN))
21429 error ("parenthesized initialization is not allowed in "
21430 "OpenMP %<for%> loop");
21431 else
21432 /* Trigger an error. */
21433 cp_parser_require (parser, CPP_EQ, "%<=%>");
21435 init = error_mark_node;
21436 cp_parser_skip_to_end_of_statement (parser);
21438 else if (CLASS_TYPE_P (TREE_TYPE (decl))
21439 || type_dependent_expression_p (decl)
21440 || auto_node)
21442 bool is_direct_init, is_non_constant_init;
21444 init = cp_parser_initializer (parser,
21445 &is_direct_init,
21446 &is_non_constant_init);
21448 if (auto_node && describable_type (init))
21450 TREE_TYPE (decl)
21451 = do_auto_deduction (TREE_TYPE (decl), init,
21452 auto_node);
21454 if (!CLASS_TYPE_P (TREE_TYPE (decl))
21455 && !type_dependent_expression_p (decl))
21456 goto non_class;
21459 cp_finish_decl (decl, init, !is_non_constant_init,
21460 asm_specification,
21461 LOOKUP_ONLYCONVERTING);
21462 if (CLASS_TYPE_P (TREE_TYPE (decl)))
21464 for_block
21465 = tree_cons (NULL, this_pre_body, for_block);
21466 init = NULL_TREE;
21468 else
21469 init = pop_stmt_list (this_pre_body);
21470 this_pre_body = NULL_TREE;
21472 else
21474 /* Consume '='. */
21475 cp_lexer_consume_token (parser->lexer);
21476 init = cp_parser_assignment_expression (parser, false, NULL);
21478 non_class:
21479 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
21480 init = error_mark_node;
21481 else
21482 cp_finish_decl (decl, NULL_TREE,
21483 /*init_const_expr_p=*/false,
21484 asm_specification,
21485 LOOKUP_ONLYCONVERTING);
21488 if (pushed_scope)
21489 pop_scope (pushed_scope);
21492 else
21494 cp_id_kind idk;
21495 /* If parsing a type specifier sequence failed, then
21496 this MUST be a simple expression. */
21497 cp_parser_parse_tentatively (parser);
21498 decl = cp_parser_primary_expression (parser, false, false,
21499 false, &idk);
21500 if (!cp_parser_error_occurred (parser)
21501 && decl
21502 && DECL_P (decl)
21503 && CLASS_TYPE_P (TREE_TYPE (decl)))
21505 tree rhs;
21507 cp_parser_parse_definitely (parser);
21508 cp_parser_require (parser, CPP_EQ, "%<=%>");
21509 rhs = cp_parser_assignment_expression (parser, false, NULL);
21510 finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
21511 rhs,
21512 tf_warning_or_error));
21513 add_private_clause = true;
21515 else
21517 decl = NULL;
21518 cp_parser_abort_tentative_parse (parser);
21519 init = cp_parser_expression (parser, false, NULL);
21520 if (init)
21522 if (TREE_CODE (init) == MODIFY_EXPR
21523 || TREE_CODE (init) == MODOP_EXPR)
21524 real_decl = TREE_OPERAND (init, 0);
21529 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21530 if (this_pre_body)
21532 this_pre_body = pop_stmt_list (this_pre_body);
21533 if (pre_body)
21535 tree t = pre_body;
21536 pre_body = push_stmt_list ();
21537 add_stmt (t);
21538 add_stmt (this_pre_body);
21539 pre_body = pop_stmt_list (pre_body);
21541 else
21542 pre_body = this_pre_body;
21545 if (decl)
21546 real_decl = decl;
21547 if (par_clauses != NULL && real_decl != NULL_TREE)
21549 tree *c;
21550 for (c = par_clauses; *c ; )
21551 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
21552 && OMP_CLAUSE_DECL (*c) == real_decl)
21554 error_at (loc, "iteration variable %qD"
21555 " should not be firstprivate", real_decl);
21556 *c = OMP_CLAUSE_CHAIN (*c);
21558 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
21559 && OMP_CLAUSE_DECL (*c) == real_decl)
21561 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
21562 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
21563 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
21564 OMP_CLAUSE_DECL (l) = real_decl;
21565 OMP_CLAUSE_CHAIN (l) = clauses;
21566 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
21567 clauses = l;
21568 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
21569 CP_OMP_CLAUSE_INFO (*c) = NULL;
21570 add_private_clause = false;
21572 else
21574 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
21575 && OMP_CLAUSE_DECL (*c) == real_decl)
21576 add_private_clause = false;
21577 c = &OMP_CLAUSE_CHAIN (*c);
21581 if (add_private_clause)
21583 tree c;
21584 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21586 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
21587 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
21588 && OMP_CLAUSE_DECL (c) == decl)
21589 break;
21590 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
21591 && OMP_CLAUSE_DECL (c) == decl)
21592 error_at (loc, "iteration variable %qD "
21593 "should not be firstprivate",
21594 decl);
21595 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
21596 && OMP_CLAUSE_DECL (c) == decl)
21597 error_at (loc, "iteration variable %qD should not be reduction",
21598 decl);
21600 if (c == NULL)
21602 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
21603 OMP_CLAUSE_DECL (c) = decl;
21604 c = finish_omp_clauses (c);
21605 if (c)
21607 OMP_CLAUSE_CHAIN (c) = clauses;
21608 clauses = c;
21613 cond = NULL;
21614 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21615 cond = cp_parser_omp_for_cond (parser, decl);
21616 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21618 incr = NULL;
21619 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21621 /* If decl is an iterator, preserve the operator on decl
21622 until finish_omp_for. */
21623 if (decl
21624 && (type_dependent_expression_p (decl)
21625 || CLASS_TYPE_P (TREE_TYPE (decl))))
21626 incr = cp_parser_omp_for_incr (parser, decl);
21627 else
21628 incr = cp_parser_expression (parser, false, NULL);
21631 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21632 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21633 /*or_comma=*/false,
21634 /*consume_paren=*/true);
21636 TREE_VEC_ELT (declv, i) = decl;
21637 TREE_VEC_ELT (initv, i) = init;
21638 TREE_VEC_ELT (condv, i) = cond;
21639 TREE_VEC_ELT (incrv, i) = incr;
21641 if (i == collapse - 1)
21642 break;
21644 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
21645 in between the collapsed for loops to be still considered perfectly
21646 nested. Hopefully the final version clarifies this.
21647 For now handle (multiple) {'s and empty statements. */
21648 cp_parser_parse_tentatively (parser);
21651 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21652 break;
21653 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21655 cp_lexer_consume_token (parser->lexer);
21656 bracecount++;
21658 else if (bracecount
21659 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21660 cp_lexer_consume_token (parser->lexer);
21661 else
21663 loc = cp_lexer_peek_token (parser->lexer)->location;
21664 error_at (loc, "not enough collapsed for loops");
21665 collapse_err = true;
21666 cp_parser_abort_tentative_parse (parser);
21667 declv = NULL_TREE;
21668 break;
21671 while (1);
21673 if (declv)
21675 cp_parser_parse_definitely (parser);
21676 nbraces += bracecount;
21680 /* Note that we saved the original contents of this flag when we entered
21681 the structured block, and so we don't need to re-save it here. */
21682 parser->in_statement = IN_OMP_FOR;
21684 /* Note that the grammar doesn't call for a structured block here,
21685 though the loop as a whole is a structured block. */
21686 body = push_stmt_list ();
21687 cp_parser_statement (parser, NULL_TREE, false, NULL);
21688 body = pop_stmt_list (body);
21690 if (declv == NULL_TREE)
21691 ret = NULL_TREE;
21692 else
21693 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
21694 pre_body, clauses);
21696 while (nbraces)
21698 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21700 cp_lexer_consume_token (parser->lexer);
21701 nbraces--;
21703 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21704 cp_lexer_consume_token (parser->lexer);
21705 else
21707 if (!collapse_err)
21709 error_at (cp_lexer_peek_token (parser->lexer)->location,
21710 "collapsed loops not perfectly nested");
21712 collapse_err = true;
21713 cp_parser_statement_seq_opt (parser, NULL);
21714 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21718 while (for_block)
21720 add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
21721 for_block = TREE_CHAIN (for_block);
21724 return ret;
21727 /* OpenMP 2.5:
21728 #pragma omp for for-clause[optseq] new-line
21729 for-loop */
21731 #define OMP_FOR_CLAUSE_MASK \
21732 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21733 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21734 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
21735 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21736 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
21737 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
21738 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
21739 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
21741 static tree
21742 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
21744 tree clauses, sb, ret;
21745 unsigned int save;
21747 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
21748 "#pragma omp for", pragma_tok);
21750 sb = begin_omp_structured_block ();
21751 save = cp_parser_begin_omp_structured_block (parser);
21753 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
21755 cp_parser_end_omp_structured_block (parser, save);
21756 add_stmt (finish_omp_structured_block (sb));
21758 return ret;
21761 /* OpenMP 2.5:
21762 # pragma omp master new-line
21763 structured-block */
21765 static tree
21766 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
21768 cp_parser_require_pragma_eol (parser, pragma_tok);
21769 return c_finish_omp_master (input_location,
21770 cp_parser_omp_structured_block (parser));
21773 /* OpenMP 2.5:
21774 # pragma omp ordered new-line
21775 structured-block */
21777 static tree
21778 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
21780 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21781 cp_parser_require_pragma_eol (parser, pragma_tok);
21782 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
21785 /* OpenMP 2.5:
21787 section-scope:
21788 { section-sequence }
21790 section-sequence:
21791 section-directive[opt] structured-block
21792 section-sequence section-directive structured-block */
21794 static tree
21795 cp_parser_omp_sections_scope (cp_parser *parser)
21797 tree stmt, substmt;
21798 bool error_suppress = false;
21799 cp_token *tok;
21801 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
21802 return NULL_TREE;
21804 stmt = push_stmt_list ();
21806 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
21808 unsigned save;
21810 substmt = begin_omp_structured_block ();
21811 save = cp_parser_begin_omp_structured_block (parser);
21813 while (1)
21815 cp_parser_statement (parser, NULL_TREE, false, NULL);
21817 tok = cp_lexer_peek_token (parser->lexer);
21818 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21819 break;
21820 if (tok->type == CPP_CLOSE_BRACE)
21821 break;
21822 if (tok->type == CPP_EOF)
21823 break;
21826 cp_parser_end_omp_structured_block (parser, save);
21827 substmt = finish_omp_structured_block (substmt);
21828 substmt = build1 (OMP_SECTION, void_type_node, substmt);
21829 add_stmt (substmt);
21832 while (1)
21834 tok = cp_lexer_peek_token (parser->lexer);
21835 if (tok->type == CPP_CLOSE_BRACE)
21836 break;
21837 if (tok->type == CPP_EOF)
21838 break;
21840 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21842 cp_lexer_consume_token (parser->lexer);
21843 cp_parser_require_pragma_eol (parser, tok);
21844 error_suppress = false;
21846 else if (!error_suppress)
21848 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
21849 error_suppress = true;
21852 substmt = cp_parser_omp_structured_block (parser);
21853 substmt = build1 (OMP_SECTION, void_type_node, substmt);
21854 add_stmt (substmt);
21856 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21858 substmt = pop_stmt_list (stmt);
21860 stmt = make_node (OMP_SECTIONS);
21861 TREE_TYPE (stmt) = void_type_node;
21862 OMP_SECTIONS_BODY (stmt) = substmt;
21864 add_stmt (stmt);
21865 return stmt;
21868 /* OpenMP 2.5:
21869 # pragma omp sections sections-clause[optseq] newline
21870 sections-scope */
21872 #define OMP_SECTIONS_CLAUSE_MASK \
21873 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21874 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21875 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
21876 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21877 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21879 static tree
21880 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
21882 tree clauses, ret;
21884 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
21885 "#pragma omp sections", pragma_tok);
21887 ret = cp_parser_omp_sections_scope (parser);
21888 if (ret)
21889 OMP_SECTIONS_CLAUSES (ret) = clauses;
21891 return ret;
21894 /* OpenMP 2.5:
21895 # pragma parallel parallel-clause new-line
21896 # pragma parallel for parallel-for-clause new-line
21897 # pragma parallel sections parallel-sections-clause new-line */
21899 #define OMP_PARALLEL_CLAUSE_MASK \
21900 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
21901 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21902 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21903 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
21904 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
21905 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
21906 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21907 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
21909 static tree
21910 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
21912 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
21913 const char *p_name = "#pragma omp parallel";
21914 tree stmt, clauses, par_clause, ws_clause, block;
21915 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
21916 unsigned int save;
21917 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21919 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21921 cp_lexer_consume_token (parser->lexer);
21922 p_kind = PRAGMA_OMP_PARALLEL_FOR;
21923 p_name = "#pragma omp parallel for";
21924 mask |= OMP_FOR_CLAUSE_MASK;
21925 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21927 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21929 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21930 const char *p = IDENTIFIER_POINTER (id);
21931 if (strcmp (p, "sections") == 0)
21933 cp_lexer_consume_token (parser->lexer);
21934 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
21935 p_name = "#pragma omp parallel sections";
21936 mask |= OMP_SECTIONS_CLAUSE_MASK;
21937 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21941 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
21942 block = begin_omp_parallel ();
21943 save = cp_parser_begin_omp_structured_block (parser);
21945 switch (p_kind)
21947 case PRAGMA_OMP_PARALLEL:
21948 cp_parser_statement (parser, NULL_TREE, false, NULL);
21949 par_clause = clauses;
21950 break;
21952 case PRAGMA_OMP_PARALLEL_FOR:
21953 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
21954 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
21955 break;
21957 case PRAGMA_OMP_PARALLEL_SECTIONS:
21958 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
21959 stmt = cp_parser_omp_sections_scope (parser);
21960 if (stmt)
21961 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
21962 break;
21964 default:
21965 gcc_unreachable ();
21968 cp_parser_end_omp_structured_block (parser, save);
21969 stmt = finish_omp_parallel (par_clause, block);
21970 if (p_kind != PRAGMA_OMP_PARALLEL)
21971 OMP_PARALLEL_COMBINED (stmt) = 1;
21972 return stmt;
21975 /* OpenMP 2.5:
21976 # pragma omp single single-clause[optseq] new-line
21977 structured-block */
21979 #define OMP_SINGLE_CLAUSE_MASK \
21980 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21981 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21982 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
21983 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21985 static tree
21986 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
21988 tree stmt = make_node (OMP_SINGLE);
21989 TREE_TYPE (stmt) = void_type_node;
21991 OMP_SINGLE_CLAUSES (stmt)
21992 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
21993 "#pragma omp single", pragma_tok);
21994 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
21996 return add_stmt (stmt);
21999 /* OpenMP 3.0:
22000 # pragma omp task task-clause[optseq] new-line
22001 structured-block */
22003 #define OMP_TASK_CLAUSE_MASK \
22004 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
22005 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
22006 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
22007 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22008 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22009 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
22011 static tree
22012 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
22014 tree clauses, block;
22015 unsigned int save;
22017 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
22018 "#pragma omp task", pragma_tok);
22019 block = begin_omp_task ();
22020 save = cp_parser_begin_omp_structured_block (parser);
22021 cp_parser_statement (parser, NULL_TREE, false, NULL);
22022 cp_parser_end_omp_structured_block (parser, save);
22023 return finish_omp_task (clauses, block);
22026 /* OpenMP 3.0:
22027 # pragma omp taskwait new-line */
22029 static void
22030 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
22032 cp_parser_require_pragma_eol (parser, pragma_tok);
22033 finish_omp_taskwait ();
22036 /* OpenMP 2.5:
22037 # pragma omp threadprivate (variable-list) */
22039 static void
22040 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
22042 tree vars;
22044 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
22045 cp_parser_require_pragma_eol (parser, pragma_tok);
22047 finish_omp_threadprivate (vars);
22050 /* Main entry point to OpenMP statement pragmas. */
22052 static void
22053 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
22055 tree stmt;
22057 switch (pragma_tok->pragma_kind)
22059 case PRAGMA_OMP_ATOMIC:
22060 cp_parser_omp_atomic (parser, pragma_tok);
22061 return;
22062 case PRAGMA_OMP_CRITICAL:
22063 stmt = cp_parser_omp_critical (parser, pragma_tok);
22064 break;
22065 case PRAGMA_OMP_FOR:
22066 stmt = cp_parser_omp_for (parser, pragma_tok);
22067 break;
22068 case PRAGMA_OMP_MASTER:
22069 stmt = cp_parser_omp_master (parser, pragma_tok);
22070 break;
22071 case PRAGMA_OMP_ORDERED:
22072 stmt = cp_parser_omp_ordered (parser, pragma_tok);
22073 break;
22074 case PRAGMA_OMP_PARALLEL:
22075 stmt = cp_parser_omp_parallel (parser, pragma_tok);
22076 break;
22077 case PRAGMA_OMP_SECTIONS:
22078 stmt = cp_parser_omp_sections (parser, pragma_tok);
22079 break;
22080 case PRAGMA_OMP_SINGLE:
22081 stmt = cp_parser_omp_single (parser, pragma_tok);
22082 break;
22083 case PRAGMA_OMP_TASK:
22084 stmt = cp_parser_omp_task (parser, pragma_tok);
22085 break;
22086 default:
22087 gcc_unreachable ();
22090 if (stmt)
22091 SET_EXPR_LOCATION (stmt, pragma_tok->location);
22094 /* The parser. */
22096 static GTY (()) cp_parser *the_parser;
22099 /* Special handling for the first token or line in the file. The first
22100 thing in the file might be #pragma GCC pch_preprocess, which loads a
22101 PCH file, which is a GC collection point. So we need to handle this
22102 first pragma without benefit of an existing lexer structure.
22104 Always returns one token to the caller in *FIRST_TOKEN. This is
22105 either the true first token of the file, or the first token after
22106 the initial pragma. */
22108 static void
22109 cp_parser_initial_pragma (cp_token *first_token)
22111 tree name = NULL;
22113 cp_lexer_get_preprocessor_token (NULL, first_token);
22114 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
22115 return;
22117 cp_lexer_get_preprocessor_token (NULL, first_token);
22118 if (first_token->type == CPP_STRING)
22120 name = first_token->u.value;
22122 cp_lexer_get_preprocessor_token (NULL, first_token);
22123 if (first_token->type != CPP_PRAGMA_EOL)
22124 error_at (first_token->location,
22125 "junk at end of %<#pragma GCC pch_preprocess%>");
22127 else
22128 error_at (first_token->location, "expected string literal");
22130 /* Skip to the end of the pragma. */
22131 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
22132 cp_lexer_get_preprocessor_token (NULL, first_token);
22134 /* Now actually load the PCH file. */
22135 if (name)
22136 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
22138 /* Read one more token to return to our caller. We have to do this
22139 after reading the PCH file in, since its pointers have to be
22140 live. */
22141 cp_lexer_get_preprocessor_token (NULL, first_token);
22144 /* Normal parsing of a pragma token. Here we can (and must) use the
22145 regular lexer. */
22147 static bool
22148 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
22150 cp_token *pragma_tok;
22151 unsigned int id;
22153 pragma_tok = cp_lexer_consume_token (parser->lexer);
22154 gcc_assert (pragma_tok->type == CPP_PRAGMA);
22155 parser->lexer->in_pragma = true;
22157 id = pragma_tok->pragma_kind;
22158 switch (id)
22160 case PRAGMA_GCC_PCH_PREPROCESS:
22161 error_at (pragma_tok->location,
22162 "%<#pragma GCC pch_preprocess%> must be first");
22163 break;
22165 case PRAGMA_OMP_BARRIER:
22166 switch (context)
22168 case pragma_compound:
22169 cp_parser_omp_barrier (parser, pragma_tok);
22170 return false;
22171 case pragma_stmt:
22172 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
22173 "used in compound statements");
22174 break;
22175 default:
22176 goto bad_stmt;
22178 break;
22180 case PRAGMA_OMP_FLUSH:
22181 switch (context)
22183 case pragma_compound:
22184 cp_parser_omp_flush (parser, pragma_tok);
22185 return false;
22186 case pragma_stmt:
22187 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
22188 "used in compound statements");
22189 break;
22190 default:
22191 goto bad_stmt;
22193 break;
22195 case PRAGMA_OMP_TASKWAIT:
22196 switch (context)
22198 case pragma_compound:
22199 cp_parser_omp_taskwait (parser, pragma_tok);
22200 return false;
22201 case pragma_stmt:
22202 error_at (pragma_tok->location,
22203 "%<#pragma omp taskwait%> may only be "
22204 "used in compound statements");
22205 break;
22206 default:
22207 goto bad_stmt;
22209 break;
22211 case PRAGMA_OMP_THREADPRIVATE:
22212 cp_parser_omp_threadprivate (parser, pragma_tok);
22213 return false;
22215 case PRAGMA_OMP_ATOMIC:
22216 case PRAGMA_OMP_CRITICAL:
22217 case PRAGMA_OMP_FOR:
22218 case PRAGMA_OMP_MASTER:
22219 case PRAGMA_OMP_ORDERED:
22220 case PRAGMA_OMP_PARALLEL:
22221 case PRAGMA_OMP_SECTIONS:
22222 case PRAGMA_OMP_SINGLE:
22223 case PRAGMA_OMP_TASK:
22224 if (context == pragma_external)
22225 goto bad_stmt;
22226 cp_parser_omp_construct (parser, pragma_tok);
22227 return true;
22229 case PRAGMA_OMP_SECTION:
22230 error_at (pragma_tok->location,
22231 "%<#pragma omp section%> may only be used in "
22232 "%<#pragma omp sections%> construct");
22233 break;
22235 default:
22236 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
22237 c_invoke_pragma_handler (id);
22238 break;
22240 bad_stmt:
22241 cp_parser_error (parser, "expected declaration specifiers");
22242 break;
22245 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
22246 return false;
22249 /* The interface the pragma parsers have to the lexer. */
22251 enum cpp_ttype
22252 pragma_lex (tree *value)
22254 cp_token *tok;
22255 enum cpp_ttype ret;
22257 tok = cp_lexer_peek_token (the_parser->lexer);
22259 ret = tok->type;
22260 *value = tok->u.value;
22262 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
22263 ret = CPP_EOF;
22264 else if (ret == CPP_STRING)
22265 *value = cp_parser_string_literal (the_parser, false, false);
22266 else
22268 cp_lexer_consume_token (the_parser->lexer);
22269 if (ret == CPP_KEYWORD)
22270 ret = CPP_NAME;
22273 return ret;
22277 /* External interface. */
22279 /* Parse one entire translation unit. */
22281 void
22282 c_parse_file (void)
22284 bool error_occurred;
22285 static bool already_called = false;
22287 if (already_called)
22289 sorry ("inter-module optimizations not implemented for C++");
22290 return;
22292 already_called = true;
22294 the_parser = cp_parser_new ();
22295 push_deferring_access_checks (flag_access_control
22296 ? dk_no_deferred : dk_no_check);
22297 error_occurred = cp_parser_translation_unit (the_parser);
22298 the_parser = NULL;
22301 #include "gt-cp-parser.h"