Make lambda conversion op and op() non-static.
[official-gcc/constexpr.git] / gcc / cp / parser.c
blobcbbfd94938d1caa28beeacd87047e93405a4a3c0
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 "intl.h"
32 #include "c-pragma.h"
33 #include "decl.h"
34 #include "flags.h"
35 #include "diagnostic.h"
36 #include "toplev.h"
37 #include "output.h"
38 #include "target.h"
39 #include "cgraph.h"
40 #include "c-common.h"
41 #include "plugin.h"
44 /* The lexer. */
46 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
47 and c-lex.c) and the C++ parser. */
49 /* A token's value and its associated deferred access checks and
50 qualifying scope. */
52 struct GTY(()) tree_check {
53 /* The value associated with the token. */
54 tree value;
55 /* The checks that have been associated with value. */
56 VEC (deferred_access_check, gc)* checks;
57 /* The token's qualifying scope (used when it is a
58 CPP_NESTED_NAME_SPECIFIER). */
59 tree qualifying_scope;
62 /* A C++ token. */
64 typedef struct GTY (()) cp_token {
65 /* The kind of token. */
66 ENUM_BITFIELD (cpp_ttype) type : 8;
67 /* If this token is a keyword, this value indicates which keyword.
68 Otherwise, this value is RID_MAX. */
69 ENUM_BITFIELD (rid) keyword : 8;
70 /* Token flags. */
71 unsigned char flags;
72 /* Identifier for the pragma. */
73 ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
74 /* True if this token is from a context where it is implicitly extern "C" */
75 BOOL_BITFIELD implicit_extern_c : 1;
76 /* True for a CPP_NAME token that is not a keyword (i.e., for which
77 KEYWORD is RID_MAX) iff this name was looked up and found to be
78 ambiguous. An error has already been reported. */
79 BOOL_BITFIELD ambiguous_p : 1;
80 /* The location at which this token was found. */
81 location_t location;
82 /* The value associated with this token, if any. */
83 union cp_token_value {
84 /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID. */
85 struct tree_check* GTY((tag ("1"))) tree_check_value;
86 /* Use for all other tokens. */
87 tree GTY((tag ("0"))) value;
88 } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
89 } cp_token;
91 /* We use a stack of token pointer for saving token sets. */
92 typedef struct cp_token *cp_token_position;
93 DEF_VEC_P (cp_token_position);
94 DEF_VEC_ALLOC_P (cp_token_position,heap);
96 static cp_token eof_token =
98 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, 0, 0, { NULL }
101 /* The cp_lexer structure represents the C++ lexer. It is responsible
102 for managing the token stream from the preprocessor and supplying
103 it to the parser. Tokens are never added to the cp_lexer after
104 it is created. */
106 typedef struct GTY (()) cp_lexer {
107 /* The memory allocated for the buffer. NULL if this lexer does not
108 own the token buffer. */
109 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
110 /* If the lexer owns the buffer, this is the number of tokens in the
111 buffer. */
112 size_t buffer_length;
114 /* A pointer just past the last available token. The tokens
115 in this lexer are [buffer, last_token). */
116 cp_token_position GTY ((skip)) last_token;
118 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
119 no more available tokens. */
120 cp_token_position GTY ((skip)) next_token;
122 /* A stack indicating positions at which cp_lexer_save_tokens was
123 called. The top entry is the most recent position at which we
124 began saving tokens. If the stack is non-empty, we are saving
125 tokens. */
126 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
128 /* The next lexer in a linked list of lexers. */
129 struct cp_lexer *next;
131 /* True if we should output debugging information. */
132 bool debugging_p;
134 /* True if we're in the context of parsing a pragma, and should not
135 increment past the end-of-line marker. */
136 bool in_pragma;
137 } cp_lexer;
139 /* cp_token_cache is a range of tokens. There is no need to represent
140 allocate heap memory for it, since tokens are never removed from the
141 lexer's array. There is also no need for the GC to walk through
142 a cp_token_cache, since everything in here is referenced through
143 a lexer. */
145 typedef struct GTY(()) cp_token_cache {
146 /* The beginning of the token range. */
147 cp_token * GTY((skip)) first;
149 /* Points immediately after the last token in the range. */
150 cp_token * GTY ((skip)) last;
151 } cp_token_cache;
153 /* Prototypes. */
155 static cp_lexer *cp_lexer_new_main
156 (void);
157 static cp_lexer *cp_lexer_new_from_tokens
158 (cp_token_cache *tokens);
159 static void cp_lexer_destroy
160 (cp_lexer *);
161 static int cp_lexer_saving_tokens
162 (const cp_lexer *);
163 static cp_token_position cp_lexer_token_position
164 (cp_lexer *, bool);
165 static cp_token *cp_lexer_token_at
166 (cp_lexer *, cp_token_position);
167 static void cp_lexer_get_preprocessor_token
168 (cp_lexer *, cp_token *);
169 static inline cp_token *cp_lexer_peek_token
170 (cp_lexer *);
171 static cp_token *cp_lexer_peek_nth_token
172 (cp_lexer *, size_t);
173 static inline bool cp_lexer_next_token_is
174 (cp_lexer *, enum cpp_ttype);
175 static bool cp_lexer_next_token_is_not
176 (cp_lexer *, enum cpp_ttype);
177 static bool cp_lexer_next_token_is_keyword
178 (cp_lexer *, enum rid);
179 static cp_token *cp_lexer_consume_token
180 (cp_lexer *);
181 static void cp_lexer_purge_token
182 (cp_lexer *);
183 static void cp_lexer_purge_tokens_after
184 (cp_lexer *, cp_token_position);
185 static void cp_lexer_save_tokens
186 (cp_lexer *);
187 static void cp_lexer_commit_tokens
188 (cp_lexer *);
189 static void cp_lexer_rollback_tokens
190 (cp_lexer *);
191 #ifdef ENABLE_CHECKING
192 static void cp_lexer_print_token
193 (FILE *, cp_token *);
194 static inline bool cp_lexer_debugging_p
195 (cp_lexer *);
196 static void cp_lexer_start_debugging
197 (cp_lexer *) ATTRIBUTE_UNUSED;
198 static void cp_lexer_stop_debugging
199 (cp_lexer *) ATTRIBUTE_UNUSED;
200 #else
201 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
202 about passing NULL to functions that require non-NULL arguments
203 (fputs, fprintf). It will never be used, so all we need is a value
204 of the right type that's guaranteed not to be NULL. */
205 #define cp_lexer_debug_stream stdout
206 #define cp_lexer_print_token(str, tok) (void) 0
207 #define cp_lexer_debugging_p(lexer) 0
208 #endif /* ENABLE_CHECKING */
210 static cp_token_cache *cp_token_cache_new
211 (cp_token *, cp_token *);
213 static void cp_parser_initial_pragma
214 (cp_token *);
216 /* Manifest constants. */
217 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
218 #define CP_SAVED_TOKEN_STACK 5
220 /* A token type for keywords, as opposed to ordinary identifiers. */
221 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
223 /* A token type for template-ids. If a template-id is processed while
224 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
225 the value of the CPP_TEMPLATE_ID is whatever was returned by
226 cp_parser_template_id. */
227 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
229 /* A token type for nested-name-specifiers. If a
230 nested-name-specifier is processed while parsing tentatively, it is
231 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
232 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
233 cp_parser_nested_name_specifier_opt. */
234 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
236 /* A token type for tokens that are not tokens at all; these are used
237 to represent slots in the array where there used to be a token
238 that has now been deleted. */
239 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
241 /* The number of token types, including C++-specific ones. */
242 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
244 /* Variables. */
246 #ifdef ENABLE_CHECKING
247 /* The stream to which debugging output should be written. */
248 static FILE *cp_lexer_debug_stream;
249 #endif /* ENABLE_CHECKING */
251 /* Nonzero if we are parsing an unevaluated operand: an operand to
252 sizeof, typeof, or alignof. */
253 int cp_unevaluated_operand;
255 /* Create a new main C++ lexer, the lexer that gets tokens from the
256 preprocessor. */
258 static cp_lexer *
259 cp_lexer_new_main (void)
261 cp_token first_token;
262 cp_lexer *lexer;
263 cp_token *pos;
264 size_t alloc;
265 size_t space;
266 cp_token *buffer;
268 /* It's possible that parsing the first pragma will load a PCH file,
269 which is a GC collection point. So we have to do that before
270 allocating any memory. */
271 cp_parser_initial_pragma (&first_token);
273 c_common_no_more_pch ();
275 /* Allocate the memory. */
276 lexer = GGC_CNEW (cp_lexer);
278 #ifdef ENABLE_CHECKING
279 /* Initially we are not debugging. */
280 lexer->debugging_p = false;
281 #endif /* ENABLE_CHECKING */
282 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
283 CP_SAVED_TOKEN_STACK);
285 /* Create the buffer. */
286 alloc = CP_LEXER_BUFFER_SIZE;
287 buffer = GGC_NEWVEC (cp_token, alloc);
289 /* Put the first token in the buffer. */
290 space = alloc;
291 pos = buffer;
292 *pos = first_token;
294 /* Get the remaining tokens from the preprocessor. */
295 while (pos->type != CPP_EOF)
297 pos++;
298 if (!--space)
300 space = alloc;
301 alloc *= 2;
302 buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
303 pos = buffer + space;
305 cp_lexer_get_preprocessor_token (lexer, pos);
307 lexer->buffer = buffer;
308 lexer->buffer_length = alloc - space;
309 lexer->last_token = pos;
310 lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
312 /* Subsequent preprocessor diagnostics should use compiler
313 diagnostic functions to get the compiler source location. */
314 done_lexing = true;
316 gcc_assert (lexer->next_token->type != CPP_PURGED);
317 return lexer;
320 /* Create a new lexer whose token stream is primed with the tokens in
321 CACHE. When these tokens are exhausted, no new tokens will be read. */
323 static cp_lexer *
324 cp_lexer_new_from_tokens (cp_token_cache *cache)
326 cp_token *first = cache->first;
327 cp_token *last = cache->last;
328 cp_lexer *lexer = GGC_CNEW (cp_lexer);
330 /* We do not own the buffer. */
331 lexer->buffer = NULL;
332 lexer->buffer_length = 0;
333 lexer->next_token = first == last ? &eof_token : first;
334 lexer->last_token = last;
336 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
337 CP_SAVED_TOKEN_STACK);
339 #ifdef ENABLE_CHECKING
340 /* Initially we are not debugging. */
341 lexer->debugging_p = false;
342 #endif
344 gcc_assert (lexer->next_token->type != CPP_PURGED);
345 return lexer;
348 /* Frees all resources associated with LEXER. */
350 static void
351 cp_lexer_destroy (cp_lexer *lexer)
353 if (lexer->buffer)
354 ggc_free (lexer->buffer);
355 VEC_free (cp_token_position, heap, lexer->saved_tokens);
356 ggc_free (lexer);
359 /* Returns nonzero if debugging information should be output. */
361 #ifdef ENABLE_CHECKING
363 static inline bool
364 cp_lexer_debugging_p (cp_lexer *lexer)
366 return lexer->debugging_p;
369 #endif /* ENABLE_CHECKING */
371 static inline cp_token_position
372 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
374 gcc_assert (!previous_p || lexer->next_token != &eof_token);
376 return lexer->next_token - previous_p;
379 static inline cp_token *
380 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
382 return pos;
385 /* nonzero if we are presently saving tokens. */
387 static inline int
388 cp_lexer_saving_tokens (const cp_lexer* lexer)
390 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
393 /* Store the next token from the preprocessor in *TOKEN. Return true
394 if we reach EOF. If LEXER is NULL, assume we are handling an
395 initial #pragma pch_preprocess, and thus want the lexer to return
396 processed strings. */
398 static void
399 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
401 static int is_extern_c = 0;
403 /* Get a new token from the preprocessor. */
404 token->type
405 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
406 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
407 token->keyword = RID_MAX;
408 token->pragma_kind = PRAGMA_NONE;
410 /* On some systems, some header files are surrounded by an
411 implicit extern "C" block. Set a flag in the token if it
412 comes from such a header. */
413 is_extern_c += pending_lang_change;
414 pending_lang_change = 0;
415 token->implicit_extern_c = is_extern_c > 0;
417 /* Check to see if this token is a keyword. */
418 if (token->type == CPP_NAME)
420 if (C_IS_RESERVED_WORD (token->u.value))
422 /* Mark this token as a keyword. */
423 token->type = CPP_KEYWORD;
424 /* Record which keyword. */
425 token->keyword = C_RID_CODE (token->u.value);
427 else
429 if (warn_cxx0x_compat
430 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
431 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
433 /* Warn about the C++0x keyword (but still treat it as
434 an identifier). */
435 warning (OPT_Wc__0x_compat,
436 "identifier %qE will become a keyword in C++0x",
437 token->u.value);
439 /* Clear out the C_RID_CODE so we don't warn about this
440 particular identifier-turned-keyword again. */
441 C_SET_RID_CODE (token->u.value, RID_MAX);
444 token->ambiguous_p = false;
445 token->keyword = RID_MAX;
448 /* Handle Objective-C++ keywords. */
449 else if (token->type == CPP_AT_NAME)
451 token->type = CPP_KEYWORD;
452 switch (C_RID_CODE (token->u.value))
454 /* Map 'class' to '@class', 'private' to '@private', etc. */
455 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
456 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
457 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
458 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
459 case RID_THROW: token->keyword = RID_AT_THROW; break;
460 case RID_TRY: token->keyword = RID_AT_TRY; break;
461 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
462 default: token->keyword = C_RID_CODE (token->u.value);
465 else if (token->type == CPP_PRAGMA)
467 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
468 token->pragma_kind = ((enum pragma_kind)
469 TREE_INT_CST_LOW (token->u.value));
470 token->u.value = NULL_TREE;
474 /* Update the globals input_location and the input file stack from TOKEN. */
475 static inline void
476 cp_lexer_set_source_position_from_token (cp_token *token)
478 if (token->type != CPP_EOF)
480 input_location = token->location;
484 /* Return a pointer to the next token in the token stream, but do not
485 consume it. */
487 static inline cp_token *
488 cp_lexer_peek_token (cp_lexer *lexer)
490 if (cp_lexer_debugging_p (lexer))
492 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
493 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
494 putc ('\n', cp_lexer_debug_stream);
496 return lexer->next_token;
499 /* Return true if the next token has the indicated TYPE. */
501 static inline bool
502 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
504 return cp_lexer_peek_token (lexer)->type == type;
507 /* Return true if the next token does not have the indicated TYPE. */
509 static inline bool
510 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
512 return !cp_lexer_next_token_is (lexer, type);
515 /* Return true if the next token is the indicated KEYWORD. */
517 static inline bool
518 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
520 return cp_lexer_peek_token (lexer)->keyword == keyword;
523 /* Return true if the next token is not the indicated KEYWORD. */
525 static inline bool
526 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
528 return cp_lexer_peek_token (lexer)->keyword != keyword;
531 /* Return true if the next token is a keyword for a decl-specifier. */
533 static bool
534 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
536 cp_token *token;
538 token = cp_lexer_peek_token (lexer);
539 switch (token->keyword)
541 /* auto specifier: storage-class-specifier in C++,
542 simple-type-specifier in C++0x. */
543 case RID_AUTO:
544 /* Storage classes. */
545 case RID_REGISTER:
546 case RID_STATIC:
547 case RID_EXTERN:
548 case RID_MUTABLE:
549 case RID_THREAD:
550 /* Elaborated type specifiers. */
551 case RID_ENUM:
552 case RID_CLASS:
553 case RID_STRUCT:
554 case RID_UNION:
555 case RID_TYPENAME:
556 /* Simple type specifiers. */
557 case RID_CHAR:
558 case RID_CHAR16:
559 case RID_CHAR32:
560 case RID_WCHAR:
561 case RID_BOOL:
562 case RID_SHORT:
563 case RID_INT:
564 case RID_LONG:
565 case RID_SIGNED:
566 case RID_UNSIGNED:
567 case RID_FLOAT:
568 case RID_DOUBLE:
569 case RID_VOID:
570 /* GNU extensions. */
571 case RID_ATTRIBUTE:
572 case RID_TYPEOF:
573 /* C++0x extensions. */
574 case RID_DECLTYPE:
575 return true;
577 default:
578 return false;
582 /* Return a pointer to the Nth token in the token stream. If N is 1,
583 then this is precisely equivalent to cp_lexer_peek_token (except
584 that it is not inline). One would like to disallow that case, but
585 there is one case (cp_parser_nth_token_starts_template_id) where
586 the caller passes a variable for N and it might be 1. */
588 static cp_token *
589 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
591 cp_token *token;
593 /* N is 1-based, not zero-based. */
594 gcc_assert (n > 0);
596 if (cp_lexer_debugging_p (lexer))
597 fprintf (cp_lexer_debug_stream,
598 "cp_lexer: peeking ahead %ld at token: ", (long)n);
600 --n;
601 token = lexer->next_token;
602 gcc_assert (!n || token != &eof_token);
603 while (n != 0)
605 ++token;
606 if (token == lexer->last_token)
608 token = &eof_token;
609 break;
612 if (token->type != CPP_PURGED)
613 --n;
616 if (cp_lexer_debugging_p (lexer))
618 cp_lexer_print_token (cp_lexer_debug_stream, token);
619 putc ('\n', cp_lexer_debug_stream);
622 return token;
625 /* Return the next token, and advance the lexer's next_token pointer
626 to point to the next non-purged token. */
628 static cp_token *
629 cp_lexer_consume_token (cp_lexer* lexer)
631 cp_token *token = lexer->next_token;
633 gcc_assert (token != &eof_token);
634 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
638 lexer->next_token++;
639 if (lexer->next_token == lexer->last_token)
641 lexer->next_token = &eof_token;
642 break;
646 while (lexer->next_token->type == CPP_PURGED);
648 cp_lexer_set_source_position_from_token (token);
650 /* Provide debugging output. */
651 if (cp_lexer_debugging_p (lexer))
653 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
654 cp_lexer_print_token (cp_lexer_debug_stream, token);
655 putc ('\n', cp_lexer_debug_stream);
658 return token;
661 /* Permanently remove the next token from the token stream, and
662 advance the next_token pointer to refer to the next non-purged
663 token. */
665 static void
666 cp_lexer_purge_token (cp_lexer *lexer)
668 cp_token *tok = lexer->next_token;
670 gcc_assert (tok != &eof_token);
671 tok->type = CPP_PURGED;
672 tok->location = UNKNOWN_LOCATION;
673 tok->u.value = NULL_TREE;
674 tok->keyword = RID_MAX;
678 tok++;
679 if (tok == lexer->last_token)
681 tok = &eof_token;
682 break;
685 while (tok->type == CPP_PURGED);
686 lexer->next_token = tok;
689 /* Permanently remove all tokens after TOK, up to, but not
690 including, the token that will be returned next by
691 cp_lexer_peek_token. */
693 static void
694 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
696 cp_token *peek = lexer->next_token;
698 if (peek == &eof_token)
699 peek = lexer->last_token;
701 gcc_assert (tok < peek);
703 for ( tok += 1; tok != peek; tok += 1)
705 tok->type = CPP_PURGED;
706 tok->location = UNKNOWN_LOCATION;
707 tok->u.value = NULL_TREE;
708 tok->keyword = RID_MAX;
712 /* Begin saving tokens. All tokens consumed after this point will be
713 preserved. */
715 static void
716 cp_lexer_save_tokens (cp_lexer* lexer)
718 /* Provide debugging output. */
719 if (cp_lexer_debugging_p (lexer))
720 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
722 VEC_safe_push (cp_token_position, heap,
723 lexer->saved_tokens, lexer->next_token);
726 /* Commit to the portion of the token stream most recently saved. */
728 static void
729 cp_lexer_commit_tokens (cp_lexer* lexer)
731 /* Provide debugging output. */
732 if (cp_lexer_debugging_p (lexer))
733 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
735 VEC_pop (cp_token_position, lexer->saved_tokens);
738 /* Return all tokens saved since the last call to cp_lexer_save_tokens
739 to the token stream. Stop saving tokens. */
741 static void
742 cp_lexer_rollback_tokens (cp_lexer* lexer)
744 /* Provide debugging output. */
745 if (cp_lexer_debugging_p (lexer))
746 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
748 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
751 /* Print a representation of the TOKEN on the STREAM. */
753 #ifdef ENABLE_CHECKING
755 static void
756 cp_lexer_print_token (FILE * stream, cp_token *token)
758 /* We don't use cpp_type2name here because the parser defines
759 a few tokens of its own. */
760 static const char *const token_names[] = {
761 /* cpplib-defined token types */
762 #define OP(e, s) #e,
763 #define TK(e, s) #e,
764 TTYPE_TABLE
765 #undef OP
766 #undef TK
767 /* C++ parser token types - see "Manifest constants", above. */
768 "KEYWORD",
769 "TEMPLATE_ID",
770 "NESTED_NAME_SPECIFIER",
771 "PURGED"
774 /* If we have a name for the token, print it out. Otherwise, we
775 simply give the numeric code. */
776 gcc_assert (token->type < ARRAY_SIZE(token_names));
777 fputs (token_names[token->type], stream);
779 /* For some tokens, print the associated data. */
780 switch (token->type)
782 case CPP_KEYWORD:
783 /* Some keywords have a value that is not an IDENTIFIER_NODE.
784 For example, `struct' is mapped to an INTEGER_CST. */
785 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
786 break;
787 /* else fall through */
788 case CPP_NAME:
789 fputs (IDENTIFIER_POINTER (token->u.value), stream);
790 break;
792 case CPP_STRING:
793 case CPP_STRING16:
794 case CPP_STRING32:
795 case CPP_WSTRING:
796 case CPP_UTF8STRING:
797 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
798 break;
800 default:
801 break;
805 /* Start emitting debugging information. */
807 static void
808 cp_lexer_start_debugging (cp_lexer* lexer)
810 lexer->debugging_p = true;
813 /* Stop emitting debugging information. */
815 static void
816 cp_lexer_stop_debugging (cp_lexer* lexer)
818 lexer->debugging_p = false;
821 #endif /* ENABLE_CHECKING */
823 /* Create a new cp_token_cache, representing a range of tokens. */
825 static cp_token_cache *
826 cp_token_cache_new (cp_token *first, cp_token *last)
828 cp_token_cache *cache = GGC_NEW (cp_token_cache);
829 cache->first = first;
830 cache->last = last;
831 return cache;
835 /* Decl-specifiers. */
837 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
839 static void
840 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
842 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
845 /* Declarators. */
847 /* Nothing other than the parser should be creating declarators;
848 declarators are a semi-syntactic representation of C++ entities.
849 Other parts of the front end that need to create entities (like
850 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
852 static cp_declarator *make_call_declarator
853 (cp_declarator *, tree, cp_cv_quals, tree, tree);
854 static cp_declarator *make_array_declarator
855 (cp_declarator *, tree);
856 static cp_declarator *make_pointer_declarator
857 (cp_cv_quals, cp_declarator *);
858 static cp_declarator *make_reference_declarator
859 (cp_cv_quals, cp_declarator *, bool);
860 static cp_parameter_declarator *make_parameter_declarator
861 (cp_decl_specifier_seq *, cp_declarator *, tree);
862 static cp_declarator *make_ptrmem_declarator
863 (cp_cv_quals, tree, cp_declarator *);
865 /* An erroneous declarator. */
866 static cp_declarator *cp_error_declarator;
868 /* The obstack on which declarators and related data structures are
869 allocated. */
870 static struct obstack declarator_obstack;
872 /* Alloc BYTES from the declarator memory pool. */
874 static inline void *
875 alloc_declarator (size_t bytes)
877 return obstack_alloc (&declarator_obstack, bytes);
880 /* Allocate a declarator of the indicated KIND. Clear fields that are
881 common to all declarators. */
883 static cp_declarator *
884 make_declarator (cp_declarator_kind kind)
886 cp_declarator *declarator;
888 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
889 declarator->kind = kind;
890 declarator->attributes = NULL_TREE;
891 declarator->declarator = NULL;
892 declarator->parameter_pack_p = false;
894 return declarator;
897 /* Make a declarator for a generalized identifier. If
898 QUALIFYING_SCOPE is non-NULL, the identifier is
899 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
900 UNQUALIFIED_NAME. SFK indicates the kind of special function this
901 is, if any. */
903 static cp_declarator *
904 make_id_declarator (tree qualifying_scope, tree unqualified_name,
905 special_function_kind sfk)
907 cp_declarator *declarator;
909 /* It is valid to write:
911 class C { void f(); };
912 typedef C D;
913 void D::f();
915 The standard is not clear about whether `typedef const C D' is
916 legal; as of 2002-09-15 the committee is considering that
917 question. EDG 3.0 allows that syntax. Therefore, we do as
918 well. */
919 if (qualifying_scope && TYPE_P (qualifying_scope))
920 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
922 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
923 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
924 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
926 declarator = make_declarator (cdk_id);
927 declarator->u.id.qualifying_scope = qualifying_scope;
928 declarator->u.id.unqualified_name = unqualified_name;
929 declarator->u.id.sfk = sfk;
931 return declarator;
934 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
935 of modifiers such as const or volatile to apply to the pointer
936 type, represented as identifiers. */
938 cp_declarator *
939 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
941 cp_declarator *declarator;
943 declarator = make_declarator (cdk_pointer);
944 declarator->declarator = target;
945 declarator->u.pointer.qualifiers = cv_qualifiers;
946 declarator->u.pointer.class_type = NULL_TREE;
947 if (target)
949 declarator->parameter_pack_p = target->parameter_pack_p;
950 target->parameter_pack_p = false;
952 else
953 declarator->parameter_pack_p = false;
955 return declarator;
958 /* Like make_pointer_declarator -- but for references. */
960 cp_declarator *
961 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
962 bool rvalue_ref)
964 cp_declarator *declarator;
966 declarator = make_declarator (cdk_reference);
967 declarator->declarator = target;
968 declarator->u.reference.qualifiers = cv_qualifiers;
969 declarator->u.reference.rvalue_ref = rvalue_ref;
970 if (target)
972 declarator->parameter_pack_p = target->parameter_pack_p;
973 target->parameter_pack_p = false;
975 else
976 declarator->parameter_pack_p = false;
978 return declarator;
981 /* Like make_pointer_declarator -- but for a pointer to a non-static
982 member of CLASS_TYPE. */
984 cp_declarator *
985 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
986 cp_declarator *pointee)
988 cp_declarator *declarator;
990 declarator = make_declarator (cdk_ptrmem);
991 declarator->declarator = pointee;
992 declarator->u.pointer.qualifiers = cv_qualifiers;
993 declarator->u.pointer.class_type = class_type;
995 if (pointee)
997 declarator->parameter_pack_p = pointee->parameter_pack_p;
998 pointee->parameter_pack_p = false;
1000 else
1001 declarator->parameter_pack_p = false;
1003 return declarator;
1006 /* Make a declarator for the function given by TARGET, with the
1007 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1008 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1009 indicates what exceptions can be thrown. */
1011 cp_declarator *
1012 make_call_declarator (cp_declarator *target,
1013 tree parms,
1014 cp_cv_quals cv_qualifiers,
1015 tree exception_specification,
1016 tree late_return_type)
1018 cp_declarator *declarator;
1020 declarator = make_declarator (cdk_function);
1021 declarator->declarator = target;
1022 declarator->u.function.parameters = parms;
1023 declarator->u.function.qualifiers = cv_qualifiers;
1024 declarator->u.function.exception_specification = exception_specification;
1025 declarator->u.function.late_return_type = late_return_type;
1026 if (target)
1028 declarator->parameter_pack_p = target->parameter_pack_p;
1029 target->parameter_pack_p = false;
1031 else
1032 declarator->parameter_pack_p = false;
1034 return declarator;
1037 /* Make a declarator for an array of BOUNDS elements, each of which is
1038 defined by ELEMENT. */
1040 cp_declarator *
1041 make_array_declarator (cp_declarator *element, tree bounds)
1043 cp_declarator *declarator;
1045 declarator = make_declarator (cdk_array);
1046 declarator->declarator = element;
1047 declarator->u.array.bounds = bounds;
1048 if (element)
1050 declarator->parameter_pack_p = element->parameter_pack_p;
1051 element->parameter_pack_p = false;
1053 else
1054 declarator->parameter_pack_p = false;
1056 return declarator;
1059 /* Determine whether the declarator we've seen so far can be a
1060 parameter pack, when followed by an ellipsis. */
1061 static bool
1062 declarator_can_be_parameter_pack (cp_declarator *declarator)
1064 /* Search for a declarator name, or any other declarator that goes
1065 after the point where the ellipsis could appear in a parameter
1066 pack. If we find any of these, then this declarator can not be
1067 made into a parameter pack. */
1068 bool found = false;
1069 while (declarator && !found)
1071 switch ((int)declarator->kind)
1073 case cdk_id:
1074 case cdk_array:
1075 found = true;
1076 break;
1078 case cdk_error:
1079 return true;
1081 default:
1082 declarator = declarator->declarator;
1083 break;
1087 return !found;
1090 cp_parameter_declarator *no_parameters;
1092 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1093 DECLARATOR and DEFAULT_ARGUMENT. */
1095 cp_parameter_declarator *
1096 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1097 cp_declarator *declarator,
1098 tree default_argument)
1100 cp_parameter_declarator *parameter;
1102 parameter = ((cp_parameter_declarator *)
1103 alloc_declarator (sizeof (cp_parameter_declarator)));
1104 parameter->next = NULL;
1105 if (decl_specifiers)
1106 parameter->decl_specifiers = *decl_specifiers;
1107 else
1108 clear_decl_specs (&parameter->decl_specifiers);
1109 parameter->declarator = declarator;
1110 parameter->default_argument = default_argument;
1111 parameter->ellipsis_p = false;
1113 return parameter;
1116 /* Returns true iff DECLARATOR is a declaration for a function. */
1118 static bool
1119 function_declarator_p (const cp_declarator *declarator)
1121 while (declarator)
1123 if (declarator->kind == cdk_function
1124 && declarator->declarator->kind == cdk_id)
1125 return true;
1126 if (declarator->kind == cdk_id
1127 || declarator->kind == cdk_error)
1128 return false;
1129 declarator = declarator->declarator;
1131 return false;
1134 /* The parser. */
1136 /* Overview
1137 --------
1139 A cp_parser parses the token stream as specified by the C++
1140 grammar. Its job is purely parsing, not semantic analysis. For
1141 example, the parser breaks the token stream into declarators,
1142 expressions, statements, and other similar syntactic constructs.
1143 It does not check that the types of the expressions on either side
1144 of an assignment-statement are compatible, or that a function is
1145 not declared with a parameter of type `void'.
1147 The parser invokes routines elsewhere in the compiler to perform
1148 semantic analysis and to build up the abstract syntax tree for the
1149 code processed.
1151 The parser (and the template instantiation code, which is, in a
1152 way, a close relative of parsing) are the only parts of the
1153 compiler that should be calling push_scope and pop_scope, or
1154 related functions. The parser (and template instantiation code)
1155 keeps track of what scope is presently active; everything else
1156 should simply honor that. (The code that generates static
1157 initializers may also need to set the scope, in order to check
1158 access control correctly when emitting the initializers.)
1160 Methodology
1161 -----------
1163 The parser is of the standard recursive-descent variety. Upcoming
1164 tokens in the token stream are examined in order to determine which
1165 production to use when parsing a non-terminal. Some C++ constructs
1166 require arbitrary look ahead to disambiguate. For example, it is
1167 impossible, in the general case, to tell whether a statement is an
1168 expression or declaration without scanning the entire statement.
1169 Therefore, the parser is capable of "parsing tentatively." When the
1170 parser is not sure what construct comes next, it enters this mode.
1171 Then, while we attempt to parse the construct, the parser queues up
1172 error messages, rather than issuing them immediately, and saves the
1173 tokens it consumes. If the construct is parsed successfully, the
1174 parser "commits", i.e., it issues any queued error messages and
1175 the tokens that were being preserved are permanently discarded.
1176 If, however, the construct is not parsed successfully, the parser
1177 rolls back its state completely so that it can resume parsing using
1178 a different alternative.
1180 Future Improvements
1181 -------------------
1183 The performance of the parser could probably be improved substantially.
1184 We could often eliminate the need to parse tentatively by looking ahead
1185 a little bit. In some places, this approach might not entirely eliminate
1186 the need to parse tentatively, but it might still speed up the average
1187 case. */
1189 /* Flags that are passed to some parsing functions. These values can
1190 be bitwise-ored together. */
1192 enum
1194 /* No flags. */
1195 CP_PARSER_FLAGS_NONE = 0x0,
1196 /* The construct is optional. If it is not present, then no error
1197 should be issued. */
1198 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1199 /* When parsing a type-specifier, treat user-defined type-names
1200 as non-type identifiers. */
1201 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1202 /* When parsing a type-specifier, do not try to parse a class-specifier
1203 or enum-specifier. */
1204 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4
1207 /* This type is used for parameters and variables which hold
1208 combinations of the above flags. */
1209 typedef int cp_parser_flags;
1211 /* The different kinds of declarators we want to parse. */
1213 typedef enum cp_parser_declarator_kind
1215 /* We want an abstract declarator. */
1216 CP_PARSER_DECLARATOR_ABSTRACT,
1217 /* We want a named declarator. */
1218 CP_PARSER_DECLARATOR_NAMED,
1219 /* We don't mind, but the name must be an unqualified-id. */
1220 CP_PARSER_DECLARATOR_EITHER
1221 } cp_parser_declarator_kind;
1223 /* The precedence values used to parse binary expressions. The minimum value
1224 of PREC must be 1, because zero is reserved to quickly discriminate
1225 binary operators from other tokens. */
1227 enum cp_parser_prec
1229 PREC_NOT_OPERATOR,
1230 PREC_LOGICAL_OR_EXPRESSION,
1231 PREC_LOGICAL_AND_EXPRESSION,
1232 PREC_INCLUSIVE_OR_EXPRESSION,
1233 PREC_EXCLUSIVE_OR_EXPRESSION,
1234 PREC_AND_EXPRESSION,
1235 PREC_EQUALITY_EXPRESSION,
1236 PREC_RELATIONAL_EXPRESSION,
1237 PREC_SHIFT_EXPRESSION,
1238 PREC_ADDITIVE_EXPRESSION,
1239 PREC_MULTIPLICATIVE_EXPRESSION,
1240 PREC_PM_EXPRESSION,
1241 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1244 /* A mapping from a token type to a corresponding tree node type, with a
1245 precedence value. */
1247 typedef struct cp_parser_binary_operations_map_node
1249 /* The token type. */
1250 enum cpp_ttype token_type;
1251 /* The corresponding tree code. */
1252 enum tree_code tree_type;
1253 /* The precedence of this operator. */
1254 enum cp_parser_prec prec;
1255 } cp_parser_binary_operations_map_node;
1257 /* The status of a tentative parse. */
1259 typedef enum cp_parser_status_kind
1261 /* No errors have occurred. */
1262 CP_PARSER_STATUS_KIND_NO_ERROR,
1263 /* An error has occurred. */
1264 CP_PARSER_STATUS_KIND_ERROR,
1265 /* We are committed to this tentative parse, whether or not an error
1266 has occurred. */
1267 CP_PARSER_STATUS_KIND_COMMITTED
1268 } cp_parser_status_kind;
1270 typedef struct cp_parser_expression_stack_entry
1272 /* Left hand side of the binary operation we are currently
1273 parsing. */
1274 tree lhs;
1275 /* Original tree code for left hand side, if it was a binary
1276 expression itself (used for -Wparentheses). */
1277 enum tree_code lhs_type;
1278 /* Tree code for the binary operation we are parsing. */
1279 enum tree_code tree_type;
1280 /* Precedence of the binary operation we are parsing. */
1281 enum cp_parser_prec prec;
1282 } cp_parser_expression_stack_entry;
1284 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1285 entries because precedence levels on the stack are monotonically
1286 increasing. */
1287 typedef struct cp_parser_expression_stack_entry
1288 cp_parser_expression_stack[NUM_PREC_VALUES];
1290 /* Context that is saved and restored when parsing tentatively. */
1291 typedef struct GTY (()) cp_parser_context {
1292 /* If this is a tentative parsing context, the status of the
1293 tentative parse. */
1294 enum cp_parser_status_kind status;
1295 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1296 that are looked up in this context must be looked up both in the
1297 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1298 the context of the containing expression. */
1299 tree object_type;
1301 /* The next parsing context in the stack. */
1302 struct cp_parser_context *next;
1303 } cp_parser_context;
1305 /* Prototypes. */
1307 /* Constructors and destructors. */
1309 static cp_parser_context *cp_parser_context_new
1310 (cp_parser_context *);
1312 /* Class variables. */
1314 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1316 /* The operator-precedence table used by cp_parser_binary_expression.
1317 Transformed into an associative array (binops_by_token) by
1318 cp_parser_new. */
1320 static const cp_parser_binary_operations_map_node binops[] = {
1321 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1322 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1324 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1325 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1326 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1328 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1329 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1331 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1332 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1334 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1335 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1336 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1337 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1339 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1340 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1342 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1344 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1346 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1348 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1350 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1353 /* The same as binops, but initialized by cp_parser_new so that
1354 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1355 for speed. */
1356 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1358 /* Constructors and destructors. */
1360 /* Construct a new context. The context below this one on the stack
1361 is given by NEXT. */
1363 static cp_parser_context *
1364 cp_parser_context_new (cp_parser_context* next)
1366 cp_parser_context *context;
1368 /* Allocate the storage. */
1369 if (cp_parser_context_free_list != NULL)
1371 /* Pull the first entry from the free list. */
1372 context = cp_parser_context_free_list;
1373 cp_parser_context_free_list = context->next;
1374 memset (context, 0, sizeof (*context));
1376 else
1377 context = GGC_CNEW (cp_parser_context);
1379 /* No errors have occurred yet in this context. */
1380 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1381 /* If this is not the bottommost context, copy information that we
1382 need from the previous context. */
1383 if (next)
1385 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1386 expression, then we are parsing one in this context, too. */
1387 context->object_type = next->object_type;
1388 /* Thread the stack. */
1389 context->next = next;
1392 return context;
1395 /* The cp_parser structure represents the C++ parser. */
1397 typedef struct GTY(()) cp_parser {
1398 /* The lexer from which we are obtaining tokens. */
1399 cp_lexer *lexer;
1401 /* The scope in which names should be looked up. If NULL_TREE, then
1402 we look up names in the scope that is currently open in the
1403 source program. If non-NULL, this is either a TYPE or
1404 NAMESPACE_DECL for the scope in which we should look. It can
1405 also be ERROR_MARK, when we've parsed a bogus scope.
1407 This value is not cleared automatically after a name is looked
1408 up, so we must be careful to clear it before starting a new look
1409 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1410 will look up `Z' in the scope of `X', rather than the current
1411 scope.) Unfortunately, it is difficult to tell when name lookup
1412 is complete, because we sometimes peek at a token, look it up,
1413 and then decide not to consume it. */
1414 tree scope;
1416 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1417 last lookup took place. OBJECT_SCOPE is used if an expression
1418 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1419 respectively. QUALIFYING_SCOPE is used for an expression of the
1420 form "X::Y"; it refers to X. */
1421 tree object_scope;
1422 tree qualifying_scope;
1424 /* A stack of parsing contexts. All but the bottom entry on the
1425 stack will be tentative contexts.
1427 We parse tentatively in order to determine which construct is in
1428 use in some situations. For example, in order to determine
1429 whether a statement is an expression-statement or a
1430 declaration-statement we parse it tentatively as a
1431 declaration-statement. If that fails, we then reparse the same
1432 token stream as an expression-statement. */
1433 cp_parser_context *context;
1435 /* True if we are parsing GNU C++. If this flag is not set, then
1436 GNU extensions are not recognized. */
1437 bool allow_gnu_extensions_p;
1439 /* TRUE if the `>' token should be interpreted as the greater-than
1440 operator. FALSE if it is the end of a template-id or
1441 template-parameter-list. In C++0x mode, this flag also applies to
1442 `>>' tokens, which are viewed as two consecutive `>' tokens when
1443 this flag is FALSE. */
1444 bool greater_than_is_operator_p;
1446 /* TRUE if default arguments are allowed within a parameter list
1447 that starts at this point. FALSE if only a gnu extension makes
1448 them permissible. */
1449 bool default_arg_ok_p;
1451 /* TRUE if we are parsing an integral constant-expression. See
1452 [expr.const] for a precise definition. */
1453 bool integral_constant_expression_p;
1455 /* TRUE if we are parsing an integral constant-expression -- but a
1456 non-constant expression should be permitted as well. This flag
1457 is used when parsing an array bound so that GNU variable-length
1458 arrays are tolerated. */
1459 bool allow_non_integral_constant_expression_p;
1461 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1462 been seen that makes the expression non-constant. */
1463 bool non_integral_constant_expression_p;
1465 /* TRUE if local variable names and `this' are forbidden in the
1466 current context. */
1467 bool local_variables_forbidden_p;
1469 /* TRUE if the declaration we are parsing is part of a
1470 linkage-specification of the form `extern string-literal
1471 declaration'. */
1472 bool in_unbraced_linkage_specification_p;
1474 /* TRUE if we are presently parsing a declarator, after the
1475 direct-declarator. */
1476 bool in_declarator_p;
1478 /* TRUE if we are presently parsing a template-argument-list. */
1479 bool in_template_argument_list_p;
1481 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1482 to IN_OMP_BLOCK if parsing OpenMP structured block and
1483 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1484 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1485 iteration-statement, OpenMP block or loop within that switch. */
1486 #define IN_SWITCH_STMT 1
1487 #define IN_ITERATION_STMT 2
1488 #define IN_OMP_BLOCK 4
1489 #define IN_OMP_FOR 8
1490 #define IN_IF_STMT 16
1491 unsigned char in_statement;
1493 /* TRUE if we are presently parsing the body of a switch statement.
1494 Note that this doesn't quite overlap with in_statement above.
1495 The difference relates to giving the right sets of error messages:
1496 "case not in switch" vs "break statement used with OpenMP...". */
1497 bool in_switch_statement_p;
1499 /* TRUE if we are parsing a type-id in an expression context. In
1500 such a situation, both "type (expr)" and "type (type)" are valid
1501 alternatives. */
1502 bool in_type_id_in_expr_p;
1504 /* TRUE if we are currently in a header file where declarations are
1505 implicitly extern "C". */
1506 bool implicit_extern_c;
1508 /* TRUE if strings in expressions should be translated to the execution
1509 character set. */
1510 bool translate_strings_p;
1512 /* TRUE if we are presently parsing the body of a function, but not
1513 a local class. */
1514 bool in_function_body;
1516 /* If non-NULL, then we are parsing a construct where new type
1517 definitions are not permitted. The string stored here will be
1518 issued as an error message if a type is defined. */
1519 const char *type_definition_forbidden_message;
1521 /* A list of lists. The outer list is a stack, used for member
1522 functions of local classes. At each level there are two sub-list,
1523 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1524 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1525 TREE_VALUE's. The functions are chained in reverse declaration
1526 order.
1528 The TREE_PURPOSE sublist contains those functions with default
1529 arguments that need post processing, and the TREE_VALUE sublist
1530 contains those functions with definitions that need post
1531 processing.
1533 These lists can only be processed once the outermost class being
1534 defined is complete. */
1535 tree unparsed_functions_queues;
1537 /* The number of classes whose definitions are currently in
1538 progress. */
1539 unsigned num_classes_being_defined;
1541 /* The number of template parameter lists that apply directly to the
1542 current declaration. */
1543 unsigned num_template_parameter_lists;
1544 } cp_parser;
1546 /* Prototypes. */
1548 /* Constructors and destructors. */
1550 static cp_parser *cp_parser_new
1551 (void);
1553 /* Routines to parse various constructs.
1555 Those that return `tree' will return the error_mark_node (rather
1556 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1557 Sometimes, they will return an ordinary node if error-recovery was
1558 attempted, even though a parse error occurred. So, to check
1559 whether or not a parse error occurred, you should always use
1560 cp_parser_error_occurred. If the construct is optional (indicated
1561 either by an `_opt' in the name of the function that does the
1562 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1563 the construct is not present. */
1565 /* Lexical conventions [gram.lex] */
1567 static tree cp_parser_identifier
1568 (cp_parser *);
1569 static tree cp_parser_string_literal
1570 (cp_parser *, bool, bool);
1572 /* Basic concepts [gram.basic] */
1574 static bool cp_parser_translation_unit
1575 (cp_parser *);
1577 /* Expressions [gram.expr] */
1579 static tree cp_parser_primary_expression
1580 (cp_parser *, bool, bool, bool, cp_id_kind *);
1581 static tree cp_parser_id_expression
1582 (cp_parser *, bool, bool, bool *, bool, bool);
1583 static tree cp_parser_unqualified_id
1584 (cp_parser *, bool, bool, bool, bool);
1585 static tree cp_parser_nested_name_specifier_opt
1586 (cp_parser *, bool, bool, bool, bool);
1587 static tree cp_parser_nested_name_specifier
1588 (cp_parser *, bool, bool, bool, bool);
1589 static tree cp_parser_qualifying_entity
1590 (cp_parser *, bool, bool, bool, bool, bool);
1591 static tree cp_parser_postfix_expression
1592 (cp_parser *, bool, bool, bool, cp_id_kind *);
1593 static tree cp_parser_postfix_open_square_expression
1594 (cp_parser *, tree, bool);
1595 static tree cp_parser_postfix_dot_deref_expression
1596 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1597 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1598 (cp_parser *, bool, bool, bool, bool *);
1599 static void cp_parser_pseudo_destructor_name
1600 (cp_parser *, tree *, tree *);
1601 static tree cp_parser_unary_expression
1602 (cp_parser *, bool, bool, cp_id_kind *);
1603 static enum tree_code cp_parser_unary_operator
1604 (cp_token *);
1605 static tree cp_parser_new_expression
1606 (cp_parser *);
1607 static VEC(tree,gc) *cp_parser_new_placement
1608 (cp_parser *);
1609 static tree cp_parser_new_type_id
1610 (cp_parser *, tree *);
1611 static cp_declarator *cp_parser_new_declarator_opt
1612 (cp_parser *);
1613 static cp_declarator *cp_parser_direct_new_declarator
1614 (cp_parser *);
1615 static VEC(tree,gc) *cp_parser_new_initializer
1616 (cp_parser *);
1617 static tree cp_parser_delete_expression
1618 (cp_parser *);
1619 static tree cp_parser_cast_expression
1620 (cp_parser *, bool, bool, cp_id_kind *);
1621 static tree cp_parser_binary_expression
1622 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1623 static tree cp_parser_question_colon_clause
1624 (cp_parser *, tree);
1625 static tree cp_parser_assignment_expression
1626 (cp_parser *, bool, cp_id_kind *);
1627 static enum tree_code cp_parser_assignment_operator_opt
1628 (cp_parser *);
1629 static tree cp_parser_expression
1630 (cp_parser *, bool, cp_id_kind *);
1631 static tree cp_parser_constant_expression
1632 (cp_parser *, bool, bool *);
1633 static tree cp_parser_builtin_offsetof
1634 (cp_parser *);
1635 static tree cp_parser_lambda_expression
1636 (cp_parser *);
1637 static void cp_parser_lambda_introducer
1638 (cp_parser *, tree);
1639 static void cp_parser_lambda_declarator_opt
1640 (cp_parser *, tree);
1641 static void cp_parser_lambda_body
1642 (cp_parser *, tree);
1644 /* Statements [gram.stmt.stmt] */
1646 static void cp_parser_statement
1647 (cp_parser *, tree, bool, bool *);
1648 static void cp_parser_label_for_labeled_statement
1649 (cp_parser *);
1650 static tree cp_parser_expression_statement
1651 (cp_parser *, tree);
1652 static tree cp_parser_compound_statement
1653 (cp_parser *, tree, bool);
1654 static void cp_parser_statement_seq_opt
1655 (cp_parser *, tree);
1656 static tree cp_parser_selection_statement
1657 (cp_parser *, bool *);
1658 static tree cp_parser_condition
1659 (cp_parser *);
1660 static tree cp_parser_iteration_statement
1661 (cp_parser *);
1662 static void cp_parser_for_init_statement
1663 (cp_parser *);
1664 static tree cp_parser_jump_statement
1665 (cp_parser *);
1666 static void cp_parser_declaration_statement
1667 (cp_parser *);
1669 static tree cp_parser_implicitly_scoped_statement
1670 (cp_parser *, bool *);
1671 static void cp_parser_already_scoped_statement
1672 (cp_parser *);
1674 /* Declarations [gram.dcl.dcl] */
1676 static void cp_parser_declaration_seq_opt
1677 (cp_parser *);
1678 static void cp_parser_declaration
1679 (cp_parser *);
1680 static void cp_parser_block_declaration
1681 (cp_parser *, bool);
1682 static void cp_parser_simple_declaration
1683 (cp_parser *, bool);
1684 static void cp_parser_decl_specifier_seq
1685 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1686 static tree cp_parser_storage_class_specifier_opt
1687 (cp_parser *);
1688 static tree cp_parser_function_specifier_opt
1689 (cp_parser *, cp_decl_specifier_seq *);
1690 static tree cp_parser_type_specifier
1691 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1692 int *, bool *);
1693 static tree cp_parser_simple_type_specifier
1694 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1695 static tree cp_parser_type_name
1696 (cp_parser *);
1697 static tree cp_parser_nonclass_name
1698 (cp_parser* parser);
1699 static tree cp_parser_elaborated_type_specifier
1700 (cp_parser *, bool, bool);
1701 static tree cp_parser_enum_specifier
1702 (cp_parser *);
1703 static void cp_parser_enumerator_list
1704 (cp_parser *, tree);
1705 static void cp_parser_enumerator_definition
1706 (cp_parser *, tree);
1707 static tree cp_parser_namespace_name
1708 (cp_parser *);
1709 static void cp_parser_namespace_definition
1710 (cp_parser *);
1711 static void cp_parser_namespace_body
1712 (cp_parser *);
1713 static tree cp_parser_qualified_namespace_specifier
1714 (cp_parser *);
1715 static void cp_parser_namespace_alias_definition
1716 (cp_parser *);
1717 static bool cp_parser_using_declaration
1718 (cp_parser *, bool);
1719 static void cp_parser_using_directive
1720 (cp_parser *);
1721 static void cp_parser_asm_definition
1722 (cp_parser *);
1723 static void cp_parser_linkage_specification
1724 (cp_parser *);
1725 static void cp_parser_static_assert
1726 (cp_parser *, bool);
1727 static tree cp_parser_decltype
1728 (cp_parser *);
1730 /* Declarators [gram.dcl.decl] */
1732 static tree cp_parser_init_declarator
1733 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1734 static cp_declarator *cp_parser_declarator
1735 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1736 static cp_declarator *cp_parser_direct_declarator
1737 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1738 static enum tree_code cp_parser_ptr_operator
1739 (cp_parser *, tree *, cp_cv_quals *);
1740 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1741 (cp_parser *);
1742 static tree cp_parser_late_return_type_opt
1743 (cp_parser *);
1744 static tree cp_parser_declarator_id
1745 (cp_parser *, bool);
1746 static tree cp_parser_type_id
1747 (cp_parser *);
1748 static tree cp_parser_template_type_arg
1749 (cp_parser *);
1750 static tree cp_parser_trailing_type_id (cp_parser *);
1751 static tree cp_parser_type_id_1
1752 (cp_parser *, bool, bool);
1753 static void cp_parser_type_specifier_seq
1754 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1755 static tree cp_parser_parameter_declaration_clause
1756 (cp_parser *);
1757 static tree cp_parser_parameter_declaration_list
1758 (cp_parser *, bool *);
1759 static cp_parameter_declarator *cp_parser_parameter_declaration
1760 (cp_parser *, bool, bool *);
1761 static tree cp_parser_default_argument
1762 (cp_parser *, bool);
1763 static void cp_parser_function_body
1764 (cp_parser *);
1765 static tree cp_parser_initializer
1766 (cp_parser *, bool *, bool *);
1767 static tree cp_parser_initializer_clause
1768 (cp_parser *, bool *);
1769 static tree cp_parser_braced_list
1770 (cp_parser*, bool*);
1771 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1772 (cp_parser *, bool *);
1774 static bool cp_parser_ctor_initializer_opt_and_function_body
1775 (cp_parser *);
1777 /* Classes [gram.class] */
1779 static tree cp_parser_class_name
1780 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1781 static tree cp_parser_class_specifier
1782 (cp_parser *);
1783 static tree cp_parser_class_head
1784 (cp_parser *, bool *, tree *, tree *);
1785 static enum tag_types cp_parser_class_key
1786 (cp_parser *);
1787 static void cp_parser_member_specification_opt
1788 (cp_parser *);
1789 static void cp_parser_member_declaration
1790 (cp_parser *);
1791 static tree cp_parser_pure_specifier
1792 (cp_parser *);
1793 static tree cp_parser_constant_initializer
1794 (cp_parser *);
1796 /* Derived classes [gram.class.derived] */
1798 static tree cp_parser_base_clause
1799 (cp_parser *);
1800 static tree cp_parser_base_specifier
1801 (cp_parser *);
1803 /* Special member functions [gram.special] */
1805 static tree cp_parser_conversion_function_id
1806 (cp_parser *);
1807 static tree cp_parser_conversion_type_id
1808 (cp_parser *);
1809 static cp_declarator *cp_parser_conversion_declarator_opt
1810 (cp_parser *);
1811 static bool cp_parser_ctor_initializer_opt
1812 (cp_parser *);
1813 static void cp_parser_mem_initializer_list
1814 (cp_parser *);
1815 static tree cp_parser_mem_initializer
1816 (cp_parser *);
1817 static tree cp_parser_mem_initializer_id
1818 (cp_parser *);
1820 /* Overloading [gram.over] */
1822 static tree cp_parser_operator_function_id
1823 (cp_parser *);
1824 static tree cp_parser_operator
1825 (cp_parser *);
1827 /* Templates [gram.temp] */
1829 static void cp_parser_template_declaration
1830 (cp_parser *, bool);
1831 static tree cp_parser_template_parameter_list
1832 (cp_parser *);
1833 static tree cp_parser_template_parameter
1834 (cp_parser *, bool *, bool *);
1835 static tree cp_parser_type_parameter
1836 (cp_parser *, bool *);
1837 static tree cp_parser_template_id
1838 (cp_parser *, bool, bool, bool);
1839 static tree cp_parser_template_name
1840 (cp_parser *, bool, bool, bool, bool *);
1841 static tree cp_parser_template_argument_list
1842 (cp_parser *);
1843 static tree cp_parser_template_argument
1844 (cp_parser *);
1845 static void cp_parser_explicit_instantiation
1846 (cp_parser *);
1847 static void cp_parser_explicit_specialization
1848 (cp_parser *);
1850 /* Exception handling [gram.exception] */
1852 static tree cp_parser_try_block
1853 (cp_parser *);
1854 static bool cp_parser_function_try_block
1855 (cp_parser *);
1856 static void cp_parser_handler_seq
1857 (cp_parser *);
1858 static void cp_parser_handler
1859 (cp_parser *);
1860 static tree cp_parser_exception_declaration
1861 (cp_parser *);
1862 static tree cp_parser_throw_expression
1863 (cp_parser *);
1864 static tree cp_parser_exception_specification_opt
1865 (cp_parser *);
1866 static tree cp_parser_type_id_list
1867 (cp_parser *);
1869 /* GNU Extensions */
1871 static tree cp_parser_asm_specification_opt
1872 (cp_parser *);
1873 static tree cp_parser_asm_operand_list
1874 (cp_parser *);
1875 static tree cp_parser_asm_clobber_list
1876 (cp_parser *);
1877 static tree cp_parser_asm_label_list
1878 (cp_parser *);
1879 static tree cp_parser_attributes_opt
1880 (cp_parser *);
1881 static tree cp_parser_attribute_list
1882 (cp_parser *);
1883 static bool cp_parser_extension_opt
1884 (cp_parser *, int *);
1885 static void cp_parser_label_declaration
1886 (cp_parser *);
1888 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1889 static bool cp_parser_pragma
1890 (cp_parser *, enum pragma_context);
1892 /* Objective-C++ Productions */
1894 static tree cp_parser_objc_message_receiver
1895 (cp_parser *);
1896 static tree cp_parser_objc_message_args
1897 (cp_parser *);
1898 static tree cp_parser_objc_message_expression
1899 (cp_parser *);
1900 static tree cp_parser_objc_encode_expression
1901 (cp_parser *);
1902 static tree cp_parser_objc_defs_expression
1903 (cp_parser *);
1904 static tree cp_parser_objc_protocol_expression
1905 (cp_parser *);
1906 static tree cp_parser_objc_selector_expression
1907 (cp_parser *);
1908 static tree cp_parser_objc_expression
1909 (cp_parser *);
1910 static bool cp_parser_objc_selector_p
1911 (enum cpp_ttype);
1912 static tree cp_parser_objc_selector
1913 (cp_parser *);
1914 static tree cp_parser_objc_protocol_refs_opt
1915 (cp_parser *);
1916 static void cp_parser_objc_declaration
1917 (cp_parser *);
1918 static tree cp_parser_objc_statement
1919 (cp_parser *);
1921 /* Utility Routines */
1923 static tree cp_parser_lookup_name
1924 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1925 static tree cp_parser_lookup_name_simple
1926 (cp_parser *, tree, location_t);
1927 static tree cp_parser_maybe_treat_template_as_class
1928 (tree, bool);
1929 static bool cp_parser_check_declarator_template_parameters
1930 (cp_parser *, cp_declarator *, location_t);
1931 static bool cp_parser_check_template_parameters
1932 (cp_parser *, unsigned, location_t, cp_declarator *);
1933 static tree cp_parser_simple_cast_expression
1934 (cp_parser *);
1935 static tree cp_parser_global_scope_opt
1936 (cp_parser *, bool);
1937 static bool cp_parser_constructor_declarator_p
1938 (cp_parser *, bool);
1939 static tree cp_parser_function_definition_from_specifiers_and_declarator
1940 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1941 static tree cp_parser_function_definition_after_declarator
1942 (cp_parser *, bool);
1943 static void cp_parser_template_declaration_after_export
1944 (cp_parser *, bool);
1945 static void cp_parser_perform_template_parameter_access_checks
1946 (VEC (deferred_access_check,gc)*);
1947 static tree cp_parser_single_declaration
1948 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1949 static tree cp_parser_functional_cast
1950 (cp_parser *, tree);
1951 static tree cp_parser_save_member_function_body
1952 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1953 static tree cp_parser_enclosed_template_argument_list
1954 (cp_parser *);
1955 static void cp_parser_save_default_args
1956 (cp_parser *, tree);
1957 static void cp_parser_late_parsing_for_member
1958 (cp_parser *, tree);
1959 static void cp_parser_late_parsing_default_args
1960 (cp_parser *, tree);
1961 static tree cp_parser_sizeof_operand
1962 (cp_parser *, enum rid);
1963 static tree cp_parser_trait_expr
1964 (cp_parser *, enum rid);
1965 static bool cp_parser_declares_only_class_p
1966 (cp_parser *);
1967 static void cp_parser_set_storage_class
1968 (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1969 static void cp_parser_set_decl_spec_type
1970 (cp_decl_specifier_seq *, tree, location_t, bool);
1971 static bool cp_parser_friend_p
1972 (const cp_decl_specifier_seq *);
1973 static cp_token *cp_parser_require
1974 (cp_parser *, enum cpp_ttype, const char *);
1975 static cp_token *cp_parser_require_keyword
1976 (cp_parser *, enum rid, const char *);
1977 static bool cp_parser_token_starts_function_definition_p
1978 (cp_token *);
1979 static bool cp_parser_next_token_starts_class_definition_p
1980 (cp_parser *);
1981 static bool cp_parser_next_token_ends_template_argument_p
1982 (cp_parser *);
1983 static bool cp_parser_nth_token_starts_template_argument_list_p
1984 (cp_parser *, size_t);
1985 static enum tag_types cp_parser_token_is_class_key
1986 (cp_token *);
1987 static void cp_parser_check_class_key
1988 (enum tag_types, tree type);
1989 static void cp_parser_check_access_in_redeclaration
1990 (tree type, location_t location);
1991 static bool cp_parser_optional_template_keyword
1992 (cp_parser *);
1993 static void cp_parser_pre_parsed_nested_name_specifier
1994 (cp_parser *);
1995 static bool cp_parser_cache_group
1996 (cp_parser *, enum cpp_ttype, unsigned);
1997 static void cp_parser_parse_tentatively
1998 (cp_parser *);
1999 static void cp_parser_commit_to_tentative_parse
2000 (cp_parser *);
2001 static void cp_parser_abort_tentative_parse
2002 (cp_parser *);
2003 static bool cp_parser_parse_definitely
2004 (cp_parser *);
2005 static inline bool cp_parser_parsing_tentatively
2006 (cp_parser *);
2007 static bool cp_parser_uncommitted_to_tentative_parse_p
2008 (cp_parser *);
2009 static void cp_parser_error
2010 (cp_parser *, const char *);
2011 static void cp_parser_name_lookup_error
2012 (cp_parser *, tree, tree, const char *, location_t);
2013 static bool cp_parser_simulate_error
2014 (cp_parser *);
2015 static bool cp_parser_check_type_definition
2016 (cp_parser *);
2017 static void cp_parser_check_for_definition_in_return_type
2018 (cp_declarator *, tree, location_t type_location);
2019 static void cp_parser_check_for_invalid_template_id
2020 (cp_parser *, tree, location_t location);
2021 static bool cp_parser_non_integral_constant_expression
2022 (cp_parser *, const char *);
2023 static void cp_parser_diagnose_invalid_type_name
2024 (cp_parser *, tree, tree, location_t);
2025 static bool cp_parser_parse_and_diagnose_invalid_type_name
2026 (cp_parser *);
2027 static int cp_parser_skip_to_closing_parenthesis
2028 (cp_parser *, bool, bool, bool);
2029 static void cp_parser_skip_to_end_of_statement
2030 (cp_parser *);
2031 static void cp_parser_consume_semicolon_at_end_of_statement
2032 (cp_parser *);
2033 static void cp_parser_skip_to_end_of_block_or_statement
2034 (cp_parser *);
2035 static bool cp_parser_skip_to_closing_brace
2036 (cp_parser *);
2037 static void cp_parser_skip_to_end_of_template_parameter_list
2038 (cp_parser *);
2039 static void cp_parser_skip_to_pragma_eol
2040 (cp_parser*, cp_token *);
2041 static bool cp_parser_error_occurred
2042 (cp_parser *);
2043 static bool cp_parser_allow_gnu_extensions_p
2044 (cp_parser *);
2045 static bool cp_parser_is_string_literal
2046 (cp_token *);
2047 static bool cp_parser_is_keyword
2048 (cp_token *, enum rid);
2049 static tree cp_parser_make_typename_type
2050 (cp_parser *, tree, tree, location_t location);
2051 static cp_declarator * cp_parser_make_indirect_declarator
2052 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2054 /* Returns nonzero if we are parsing tentatively. */
2056 static inline bool
2057 cp_parser_parsing_tentatively (cp_parser* parser)
2059 return parser->context->next != NULL;
2062 /* Returns nonzero if TOKEN is a string literal. */
2064 static bool
2065 cp_parser_is_string_literal (cp_token* token)
2067 return (token->type == CPP_STRING ||
2068 token->type == CPP_STRING16 ||
2069 token->type == CPP_STRING32 ||
2070 token->type == CPP_WSTRING ||
2071 token->type == CPP_UTF8STRING);
2074 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2076 static bool
2077 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2079 return token->keyword == keyword;
2082 /* If not parsing tentatively, issue a diagnostic of the form
2083 FILE:LINE: MESSAGE before TOKEN
2084 where TOKEN is the next token in the input stream. MESSAGE
2085 (specified by the caller) is usually of the form "expected
2086 OTHER-TOKEN". */
2088 static void
2089 cp_parser_error (cp_parser* parser, const char* message)
2091 if (!cp_parser_simulate_error (parser))
2093 cp_token *token = cp_lexer_peek_token (parser->lexer);
2094 /* This diagnostic makes more sense if it is tagged to the line
2095 of the token we just peeked at. */
2096 cp_lexer_set_source_position_from_token (token);
2098 if (token->type == CPP_PRAGMA)
2100 error_at (token->location,
2101 "%<#pragma%> is not allowed here");
2102 cp_parser_skip_to_pragma_eol (parser, token);
2103 return;
2106 c_parse_error (message,
2107 /* Because c_parser_error does not understand
2108 CPP_KEYWORD, keywords are treated like
2109 identifiers. */
2110 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2111 token->u.value, token->flags);
2115 /* Issue an error about name-lookup failing. NAME is the
2116 IDENTIFIER_NODE DECL is the result of
2117 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2118 the thing that we hoped to find. */
2120 static void
2121 cp_parser_name_lookup_error (cp_parser* parser,
2122 tree name,
2123 tree decl,
2124 const char* desired,
2125 location_t location)
2127 /* If name lookup completely failed, tell the user that NAME was not
2128 declared. */
2129 if (decl == error_mark_node)
2131 if (parser->scope && parser->scope != global_namespace)
2132 error_at (location, "%<%E::%E%> has not been declared",
2133 parser->scope, name);
2134 else if (parser->scope == global_namespace)
2135 error_at (location, "%<::%E%> has not been declared", name);
2136 else if (parser->object_scope
2137 && !CLASS_TYPE_P (parser->object_scope))
2138 error_at (location, "request for member %qE in non-class type %qT",
2139 name, parser->object_scope);
2140 else if (parser->object_scope)
2141 error_at (location, "%<%T::%E%> has not been declared",
2142 parser->object_scope, name);
2143 else
2144 error_at (location, "%qE has not been declared", name);
2146 else if (parser->scope && parser->scope != global_namespace)
2147 error_at (location, "%<%E::%E%> %s", parser->scope, name, desired);
2148 else if (parser->scope == global_namespace)
2149 error_at (location, "%<::%E%> %s", name, desired);
2150 else
2151 error_at (location, "%qE %s", name, desired);
2154 /* If we are parsing tentatively, remember that an error has occurred
2155 during this tentative parse. Returns true if the error was
2156 simulated; false if a message should be issued by the caller. */
2158 static bool
2159 cp_parser_simulate_error (cp_parser* parser)
2161 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2163 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2164 return true;
2166 return false;
2169 /* Check for repeated decl-specifiers. */
2171 static void
2172 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2173 location_t location)
2175 int ds;
2177 for (ds = ds_first; ds != ds_last; ++ds)
2179 unsigned count = decl_specs->specs[ds];
2180 if (count < 2)
2181 continue;
2182 /* The "long" specifier is a special case because of "long long". */
2183 if (ds == ds_long)
2185 if (count > 2)
2186 error_at (location, "%<long long long%> is too long for GCC");
2187 else
2188 pedwarn_cxx98 (location, OPT_Wlong_long,
2189 "ISO C++ 1998 does not support %<long long%>");
2191 else if (count > 1)
2193 static const char *const decl_spec_names[] = {
2194 "signed",
2195 "unsigned",
2196 "short",
2197 "long",
2198 "const",
2199 "volatile",
2200 "restrict",
2201 "inline",
2202 "virtual",
2203 "explicit",
2204 "friend",
2205 "typedef",
2206 "constexpr",
2207 "__complex",
2208 "__thread"
2210 error_at (location, "duplicate %qs", decl_spec_names[ds]);
2215 /* This function is called when a type is defined. If type
2216 definitions are forbidden at this point, an error message is
2217 issued. */
2219 static bool
2220 cp_parser_check_type_definition (cp_parser* parser)
2222 /* If types are forbidden here, issue a message. */
2223 if (parser->type_definition_forbidden_message)
2225 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2226 in the message need to be interpreted. */
2227 error (parser->type_definition_forbidden_message);
2228 return false;
2230 return true;
2233 /* This function is called when the DECLARATOR is processed. The TYPE
2234 was a type defined in the decl-specifiers. If it is invalid to
2235 define a type in the decl-specifiers for DECLARATOR, an error is
2236 issued. TYPE_LOCATION is the location of TYPE and is used
2237 for error reporting. */
2239 static void
2240 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2241 tree type, location_t type_location)
2243 /* [dcl.fct] forbids type definitions in return types.
2244 Unfortunately, it's not easy to know whether or not we are
2245 processing a return type until after the fact. */
2246 while (declarator
2247 && (declarator->kind == cdk_pointer
2248 || declarator->kind == cdk_reference
2249 || declarator->kind == cdk_ptrmem))
2250 declarator = declarator->declarator;
2251 if (declarator
2252 && declarator->kind == cdk_function)
2254 error_at (type_location,
2255 "new types may not be defined in a return type");
2256 inform (type_location,
2257 "(perhaps a semicolon is missing after the definition of %qT)",
2258 type);
2262 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2263 "<" in any valid C++ program. If the next token is indeed "<",
2264 issue a message warning the user about what appears to be an
2265 invalid attempt to form a template-id. LOCATION is the location
2266 of the type-specifier (TYPE) */
2268 static void
2269 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2270 tree type, location_t location)
2272 cp_token_position start = 0;
2274 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2276 if (TYPE_P (type))
2277 error_at (location, "%qT is not a template", type);
2278 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2279 error_at (location, "%qE is not a template", type);
2280 else
2281 error_at (location, "invalid template-id");
2282 /* Remember the location of the invalid "<". */
2283 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2284 start = cp_lexer_token_position (parser->lexer, true);
2285 /* Consume the "<". */
2286 cp_lexer_consume_token (parser->lexer);
2287 /* Parse the template arguments. */
2288 cp_parser_enclosed_template_argument_list (parser);
2289 /* Permanently remove the invalid template arguments so that
2290 this error message is not issued again. */
2291 if (start)
2292 cp_lexer_purge_tokens_after (parser->lexer, start);
2296 /* If parsing an integral constant-expression, issue an error message
2297 about the fact that THING appeared and return true. Otherwise,
2298 return false. In either case, set
2299 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2301 static bool
2302 cp_parser_non_integral_constant_expression (cp_parser *parser,
2303 const char *thing)
2305 parser->non_integral_constant_expression_p = true;
2306 if (parser->integral_constant_expression_p)
2308 if (!parser->allow_non_integral_constant_expression_p)
2310 /* Don't use `%s' to print THING, because quotations (`%<', `%>')
2311 in the message need to be interpreted. */
2312 char *message = concat (thing,
2313 " cannot appear in a constant-expression",
2314 NULL);
2315 error (message);
2316 free (message);
2317 return true;
2320 return false;
2323 /* Emit a diagnostic for an invalid type name. SCOPE is the
2324 qualifying scope (or NULL, if none) for ID. This function commits
2325 to the current active tentative parse, if any. (Otherwise, the
2326 problematic construct might be encountered again later, resulting
2327 in duplicate error messages.) LOCATION is the location of ID. */
2329 static void
2330 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2331 tree scope, tree id,
2332 location_t location)
2334 tree decl, old_scope;
2335 /* Try to lookup the identifier. */
2336 old_scope = parser->scope;
2337 parser->scope = scope;
2338 decl = cp_parser_lookup_name_simple (parser, id, location);
2339 parser->scope = old_scope;
2340 /* If the lookup found a template-name, it means that the user forgot
2341 to specify an argument list. Emit a useful error message. */
2342 if (TREE_CODE (decl) == TEMPLATE_DECL)
2343 error_at (location,
2344 "invalid use of template-name %qE without an argument list",
2345 decl);
2346 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2347 error_at (location, "invalid use of destructor %qD as a type", id);
2348 else if (TREE_CODE (decl) == TYPE_DECL)
2349 /* Something like 'unsigned A a;' */
2350 error_at (location, "invalid combination of multiple type-specifiers");
2351 else if (!parser->scope)
2353 /* Issue an error message. */
2354 error_at (location, "%qE does not name a type", id);
2355 /* If we're in a template class, it's possible that the user was
2356 referring to a type from a base class. For example:
2358 template <typename T> struct A { typedef T X; };
2359 template <typename T> struct B : public A<T> { X x; };
2361 The user should have said "typename A<T>::X". */
2362 if (processing_template_decl && current_class_type
2363 && TYPE_BINFO (current_class_type))
2365 tree b;
2367 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2369 b = TREE_CHAIN (b))
2371 tree base_type = BINFO_TYPE (b);
2372 if (CLASS_TYPE_P (base_type)
2373 && dependent_type_p (base_type))
2375 tree field;
2376 /* Go from a particular instantiation of the
2377 template (which will have an empty TYPE_FIELDs),
2378 to the main version. */
2379 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2380 for (field = TYPE_FIELDS (base_type);
2381 field;
2382 field = TREE_CHAIN (field))
2383 if (TREE_CODE (field) == TYPE_DECL
2384 && DECL_NAME (field) == id)
2386 inform (location,
2387 "(perhaps %<typename %T::%E%> was intended)",
2388 BINFO_TYPE (b), id);
2389 break;
2391 if (field)
2392 break;
2397 /* Here we diagnose qualified-ids where the scope is actually correct,
2398 but the identifier does not resolve to a valid type name. */
2399 else if (parser->scope != error_mark_node)
2401 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2402 error_at (location, "%qE in namespace %qE does not name a type",
2403 id, parser->scope);
2404 else if (CLASS_TYPE_P (parser->scope)
2405 && constructor_name_p (id, parser->scope))
2407 /* A<T>::A<T>() */
2408 error_at (location, "%<%T::%E%> names the constructor, not"
2409 " the type", parser->scope, id);
2410 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2411 error_at (location, "and %qT has no template constructors",
2412 parser->scope);
2414 else if (TYPE_P (parser->scope)
2415 && dependent_scope_p (parser->scope))
2416 error_at (location, "need %<typename%> before %<%T::%E%> because "
2417 "%qT is a dependent scope",
2418 parser->scope, id, parser->scope);
2419 else if (TYPE_P (parser->scope))
2420 error_at (location, "%qE in class %qT does not name a type",
2421 id, parser->scope);
2422 else
2423 gcc_unreachable ();
2425 cp_parser_commit_to_tentative_parse (parser);
2428 /* Check for a common situation where a type-name should be present,
2429 but is not, and issue a sensible error message. Returns true if an
2430 invalid type-name was detected.
2432 The situation handled by this function are variable declarations of the
2433 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2434 Usually, `ID' should name a type, but if we got here it means that it
2435 does not. We try to emit the best possible error message depending on
2436 how exactly the id-expression looks like. */
2438 static bool
2439 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2441 tree id;
2442 cp_token *token = cp_lexer_peek_token (parser->lexer);
2444 /* Avoid duplicate error about ambiguous lookup. */
2445 if (token->type == CPP_NESTED_NAME_SPECIFIER)
2447 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2448 if (next->type == CPP_NAME && next->ambiguous_p)
2449 goto out;
2452 cp_parser_parse_tentatively (parser);
2453 id = cp_parser_id_expression (parser,
2454 /*template_keyword_p=*/false,
2455 /*check_dependency_p=*/true,
2456 /*template_p=*/NULL,
2457 /*declarator_p=*/true,
2458 /*optional_p=*/false);
2459 /* If the next token is a (, this is a function with no explicit return
2460 type, i.e. constructor, destructor or conversion op. */
2461 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2462 || TREE_CODE (id) == TYPE_DECL)
2464 cp_parser_abort_tentative_parse (parser);
2465 return false;
2467 if (!cp_parser_parse_definitely (parser))
2468 return false;
2470 /* Emit a diagnostic for the invalid type. */
2471 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2472 id, token->location);
2473 out:
2474 /* If we aren't in the middle of a declarator (i.e. in a
2475 parameter-declaration-clause), skip to the end of the declaration;
2476 there's no point in trying to process it. */
2477 if (!parser->in_declarator_p)
2478 cp_parser_skip_to_end_of_block_or_statement (parser);
2479 return true;
2482 /* Consume tokens up to, and including, the next non-nested closing `)'.
2483 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2484 are doing error recovery. Returns -1 if OR_COMMA is true and we
2485 found an unnested comma. */
2487 static int
2488 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2489 bool recovering,
2490 bool or_comma,
2491 bool consume_paren)
2493 unsigned paren_depth = 0;
2494 unsigned brace_depth = 0;
2495 unsigned square_depth = 0;
2497 if (recovering && !or_comma
2498 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2499 return 0;
2501 while (true)
2503 cp_token * token = cp_lexer_peek_token (parser->lexer);
2505 switch (token->type)
2507 case CPP_EOF:
2508 case CPP_PRAGMA_EOL:
2509 /* If we've run out of tokens, then there is no closing `)'. */
2510 return 0;
2512 /* This is good for lambda expression capture-lists. */
2513 case CPP_OPEN_SQUARE:
2514 ++square_depth;
2515 break;
2516 case CPP_CLOSE_SQUARE:
2517 if (!square_depth--)
2518 return 0;
2519 break;
2521 case CPP_SEMICOLON:
2522 /* This matches the processing in skip_to_end_of_statement. */
2523 if (!brace_depth)
2524 return 0;
2525 break;
2527 case CPP_OPEN_BRACE:
2528 ++brace_depth;
2529 break;
2530 case CPP_CLOSE_BRACE:
2531 if (!brace_depth--)
2532 return 0;
2533 break;
2535 case CPP_COMMA:
2536 if (recovering && or_comma && !brace_depth && !paren_depth
2537 && !square_depth)
2538 return -1;
2539 break;
2541 case CPP_OPEN_PAREN:
2542 if (!brace_depth)
2543 ++paren_depth;
2544 break;
2546 case CPP_CLOSE_PAREN:
2547 if (!brace_depth && !paren_depth--)
2549 if (consume_paren)
2550 cp_lexer_consume_token (parser->lexer);
2551 return 1;
2553 break;
2555 default:
2556 break;
2559 /* Consume the token. */
2560 cp_lexer_consume_token (parser->lexer);
2564 /* Consume tokens until we reach the end of the current statement.
2565 Normally, that will be just before consuming a `;'. However, if a
2566 non-nested `}' comes first, then we stop before consuming that. */
2568 static void
2569 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2571 unsigned nesting_depth = 0;
2573 while (true)
2575 cp_token *token = cp_lexer_peek_token (parser->lexer);
2577 switch (token->type)
2579 case CPP_EOF:
2580 case CPP_PRAGMA_EOL:
2581 /* If we've run out of tokens, stop. */
2582 return;
2584 case CPP_SEMICOLON:
2585 /* If the next token is a `;', we have reached the end of the
2586 statement. */
2587 if (!nesting_depth)
2588 return;
2589 break;
2591 case CPP_CLOSE_BRACE:
2592 /* If this is a non-nested '}', stop before consuming it.
2593 That way, when confronted with something like:
2595 { 3 + }
2597 we stop before consuming the closing '}', even though we
2598 have not yet reached a `;'. */
2599 if (nesting_depth == 0)
2600 return;
2602 /* If it is the closing '}' for a block that we have
2603 scanned, stop -- but only after consuming the token.
2604 That way given:
2606 void f g () { ... }
2607 typedef int I;
2609 we will stop after the body of the erroneously declared
2610 function, but before consuming the following `typedef'
2611 declaration. */
2612 if (--nesting_depth == 0)
2614 cp_lexer_consume_token (parser->lexer);
2615 return;
2618 case CPP_OPEN_BRACE:
2619 ++nesting_depth;
2620 break;
2622 default:
2623 break;
2626 /* Consume the token. */
2627 cp_lexer_consume_token (parser->lexer);
2631 /* This function is called at the end of a statement or declaration.
2632 If the next token is a semicolon, it is consumed; otherwise, error
2633 recovery is attempted. */
2635 static void
2636 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2638 /* Look for the trailing `;'. */
2639 if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2641 /* If there is additional (erroneous) input, skip to the end of
2642 the statement. */
2643 cp_parser_skip_to_end_of_statement (parser);
2644 /* If the next token is now a `;', consume it. */
2645 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2646 cp_lexer_consume_token (parser->lexer);
2650 /* Skip tokens until we have consumed an entire block, or until we
2651 have consumed a non-nested `;'. */
2653 static void
2654 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2656 int nesting_depth = 0;
2658 while (nesting_depth >= 0)
2660 cp_token *token = cp_lexer_peek_token (parser->lexer);
2662 switch (token->type)
2664 case CPP_EOF:
2665 case CPP_PRAGMA_EOL:
2666 /* If we've run out of tokens, stop. */
2667 return;
2669 case CPP_SEMICOLON:
2670 /* Stop if this is an unnested ';'. */
2671 if (!nesting_depth)
2672 nesting_depth = -1;
2673 break;
2675 case CPP_CLOSE_BRACE:
2676 /* Stop if this is an unnested '}', or closes the outermost
2677 nesting level. */
2678 nesting_depth--;
2679 if (nesting_depth < 0)
2680 return;
2681 if (!nesting_depth)
2682 nesting_depth = -1;
2683 break;
2685 case CPP_OPEN_BRACE:
2686 /* Nest. */
2687 nesting_depth++;
2688 break;
2690 default:
2691 break;
2694 /* Consume the token. */
2695 cp_lexer_consume_token (parser->lexer);
2699 /* Skip tokens until a non-nested closing curly brace is the next
2700 token, or there are no more tokens. Return true in the first case,
2701 false otherwise. */
2703 static bool
2704 cp_parser_skip_to_closing_brace (cp_parser *parser)
2706 unsigned nesting_depth = 0;
2708 while (true)
2710 cp_token *token = cp_lexer_peek_token (parser->lexer);
2712 switch (token->type)
2714 case CPP_EOF:
2715 case CPP_PRAGMA_EOL:
2716 /* If we've run out of tokens, stop. */
2717 return false;
2719 case CPP_CLOSE_BRACE:
2720 /* If the next token is a non-nested `}', then we have reached
2721 the end of the current block. */
2722 if (nesting_depth-- == 0)
2723 return true;
2724 break;
2726 case CPP_OPEN_BRACE:
2727 /* If it the next token is a `{', then we are entering a new
2728 block. Consume the entire block. */
2729 ++nesting_depth;
2730 break;
2732 default:
2733 break;
2736 /* Consume the token. */
2737 cp_lexer_consume_token (parser->lexer);
2741 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2742 parameter is the PRAGMA token, allowing us to purge the entire pragma
2743 sequence. */
2745 static void
2746 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2748 cp_token *token;
2750 parser->lexer->in_pragma = false;
2753 token = cp_lexer_consume_token (parser->lexer);
2754 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2756 /* Ensure that the pragma is not parsed again. */
2757 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2760 /* Require pragma end of line, resyncing with it as necessary. The
2761 arguments are as for cp_parser_skip_to_pragma_eol. */
2763 static void
2764 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2766 parser->lexer->in_pragma = false;
2767 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2768 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2771 /* This is a simple wrapper around make_typename_type. When the id is
2772 an unresolved identifier node, we can provide a superior diagnostic
2773 using cp_parser_diagnose_invalid_type_name. */
2775 static tree
2776 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2777 tree id, location_t id_location)
2779 tree result;
2780 if (TREE_CODE (id) == IDENTIFIER_NODE)
2782 result = make_typename_type (scope, id, typename_type,
2783 /*complain=*/tf_none);
2784 if (result == error_mark_node)
2785 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2786 return result;
2788 return make_typename_type (scope, id, typename_type, tf_error);
2791 /* This is a wrapper around the
2792 make_{pointer,ptrmem,reference}_declarator functions that decides
2793 which one to call based on the CODE and CLASS_TYPE arguments. The
2794 CODE argument should be one of the values returned by
2795 cp_parser_ptr_operator. */
2796 static cp_declarator *
2797 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2798 cp_cv_quals cv_qualifiers,
2799 cp_declarator *target)
2801 if (code == ERROR_MARK)
2802 return cp_error_declarator;
2804 if (code == INDIRECT_REF)
2805 if (class_type == NULL_TREE)
2806 return make_pointer_declarator (cv_qualifiers, target);
2807 else
2808 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2809 else if (code == ADDR_EXPR && class_type == NULL_TREE)
2810 return make_reference_declarator (cv_qualifiers, target, false);
2811 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2812 return make_reference_declarator (cv_qualifiers, target, true);
2813 gcc_unreachable ();
2816 /* Create a new C++ parser. */
2818 static cp_parser *
2819 cp_parser_new (void)
2821 cp_parser *parser;
2822 cp_lexer *lexer;
2823 unsigned i;
2825 /* cp_lexer_new_main is called before calling ggc_alloc because
2826 cp_lexer_new_main might load a PCH file. */
2827 lexer = cp_lexer_new_main ();
2829 /* Initialize the binops_by_token so that we can get the tree
2830 directly from the token. */
2831 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2832 binops_by_token[binops[i].token_type] = binops[i];
2834 parser = GGC_CNEW (cp_parser);
2835 parser->lexer = lexer;
2836 parser->context = cp_parser_context_new (NULL);
2838 /* For now, we always accept GNU extensions. */
2839 parser->allow_gnu_extensions_p = 1;
2841 /* The `>' token is a greater-than operator, not the end of a
2842 template-id. */
2843 parser->greater_than_is_operator_p = true;
2845 parser->default_arg_ok_p = true;
2847 /* We are not parsing a constant-expression. */
2848 parser->integral_constant_expression_p = false;
2849 parser->allow_non_integral_constant_expression_p = false;
2850 parser->non_integral_constant_expression_p = false;
2852 /* Local variable names are not forbidden. */
2853 parser->local_variables_forbidden_p = false;
2855 /* We are not processing an `extern "C"' declaration. */
2856 parser->in_unbraced_linkage_specification_p = false;
2858 /* We are not processing a declarator. */
2859 parser->in_declarator_p = false;
2861 /* We are not processing a template-argument-list. */
2862 parser->in_template_argument_list_p = false;
2864 /* We are not in an iteration statement. */
2865 parser->in_statement = 0;
2867 /* We are not in a switch statement. */
2868 parser->in_switch_statement_p = false;
2870 /* We are not parsing a type-id inside an expression. */
2871 parser->in_type_id_in_expr_p = false;
2873 /* Declarations aren't implicitly extern "C". */
2874 parser->implicit_extern_c = false;
2876 /* String literals should be translated to the execution character set. */
2877 parser->translate_strings_p = true;
2879 /* We are not parsing a function body. */
2880 parser->in_function_body = false;
2882 /* The unparsed function queue is empty. */
2883 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2885 /* There are no classes being defined. */
2886 parser->num_classes_being_defined = 0;
2888 /* No template parameters apply. */
2889 parser->num_template_parameter_lists = 0;
2891 return parser;
2894 /* Create a cp_lexer structure which will emit the tokens in CACHE
2895 and push it onto the parser's lexer stack. This is used for delayed
2896 parsing of in-class method bodies and default arguments, and should
2897 not be confused with tentative parsing. */
2898 static void
2899 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2901 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2902 lexer->next = parser->lexer;
2903 parser->lexer = lexer;
2905 /* Move the current source position to that of the first token in the
2906 new lexer. */
2907 cp_lexer_set_source_position_from_token (lexer->next_token);
2910 /* Pop the top lexer off the parser stack. This is never used for the
2911 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2912 static void
2913 cp_parser_pop_lexer (cp_parser *parser)
2915 cp_lexer *lexer = parser->lexer;
2916 parser->lexer = lexer->next;
2917 cp_lexer_destroy (lexer);
2919 /* Put the current source position back where it was before this
2920 lexer was pushed. */
2921 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2924 /* Lexical conventions [gram.lex] */
2926 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2927 identifier. */
2929 static tree
2930 cp_parser_identifier (cp_parser* parser)
2932 cp_token *token;
2934 /* Look for the identifier. */
2935 token = cp_parser_require (parser, CPP_NAME, "identifier");
2936 /* Return the value. */
2937 return token ? token->u.value : error_mark_node;
2940 /* Parse a sequence of adjacent string constants. Returns a
2941 TREE_STRING representing the combined, nul-terminated string
2942 constant. If TRANSLATE is true, translate the string to the
2943 execution character set. If WIDE_OK is true, a wide string is
2944 invalid here.
2946 C++98 [lex.string] says that if a narrow string literal token is
2947 adjacent to a wide string literal token, the behavior is undefined.
2948 However, C99 6.4.5p4 says that this results in a wide string literal.
2949 We follow C99 here, for consistency with the C front end.
2951 This code is largely lifted from lex_string() in c-lex.c.
2953 FUTURE: ObjC++ will need to handle @-strings here. */
2954 static tree
2955 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2957 tree value;
2958 size_t count;
2959 struct obstack str_ob;
2960 cpp_string str, istr, *strs;
2961 cp_token *tok;
2962 enum cpp_ttype type;
2964 tok = cp_lexer_peek_token (parser->lexer);
2965 if (!cp_parser_is_string_literal (tok))
2967 cp_parser_error (parser, "expected string-literal");
2968 return error_mark_node;
2971 type = tok->type;
2973 /* Try to avoid the overhead of creating and destroying an obstack
2974 for the common case of just one string. */
2975 if (!cp_parser_is_string_literal
2976 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2978 cp_lexer_consume_token (parser->lexer);
2980 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2981 str.len = TREE_STRING_LENGTH (tok->u.value);
2982 count = 1;
2984 strs = &str;
2986 else
2988 gcc_obstack_init (&str_ob);
2989 count = 0;
2993 cp_lexer_consume_token (parser->lexer);
2994 count++;
2995 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2996 str.len = TREE_STRING_LENGTH (tok->u.value);
2998 if (type != tok->type)
3000 if (type == CPP_STRING)
3001 type = tok->type;
3002 else if (tok->type != CPP_STRING)
3003 error_at (tok->location,
3004 "unsupported non-standard concatenation "
3005 "of string literals");
3008 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3010 tok = cp_lexer_peek_token (parser->lexer);
3012 while (cp_parser_is_string_literal (tok));
3014 strs = (cpp_string *) obstack_finish (&str_ob);
3017 if (type != CPP_STRING && !wide_ok)
3019 cp_parser_error (parser, "a wide string is invalid in this context");
3020 type = CPP_STRING;
3023 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3024 (parse_in, strs, count, &istr, type))
3026 value = build_string (istr.len, (const char *)istr.text);
3027 free (CONST_CAST (unsigned char *, istr.text));
3029 switch (type)
3031 default:
3032 case CPP_STRING:
3033 case CPP_UTF8STRING:
3034 TREE_TYPE (value) = char_array_type_node;
3035 break;
3036 case CPP_STRING16:
3037 TREE_TYPE (value) = char16_array_type_node;
3038 break;
3039 case CPP_STRING32:
3040 TREE_TYPE (value) = char32_array_type_node;
3041 break;
3042 case CPP_WSTRING:
3043 TREE_TYPE (value) = wchar_array_type_node;
3044 break;
3047 value = fix_string_type (value);
3049 else
3050 /* cpp_interpret_string has issued an error. */
3051 value = error_mark_node;
3053 if (count > 1)
3054 obstack_free (&str_ob, 0);
3056 return value;
3060 /* Basic concepts [gram.basic] */
3062 /* Parse a translation-unit.
3064 translation-unit:
3065 declaration-seq [opt]
3067 Returns TRUE if all went well. */
3069 static bool
3070 cp_parser_translation_unit (cp_parser* parser)
3072 /* The address of the first non-permanent object on the declarator
3073 obstack. */
3074 static void *declarator_obstack_base;
3076 bool success;
3078 /* Create the declarator obstack, if necessary. */
3079 if (!cp_error_declarator)
3081 gcc_obstack_init (&declarator_obstack);
3082 /* Create the error declarator. */
3083 cp_error_declarator = make_declarator (cdk_error);
3084 /* Create the empty parameter list. */
3085 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3086 /* Remember where the base of the declarator obstack lies. */
3087 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3090 cp_parser_declaration_seq_opt (parser);
3092 /* If there are no tokens left then all went well. */
3093 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3095 /* Get rid of the token array; we don't need it any more. */
3096 cp_lexer_destroy (parser->lexer);
3097 parser->lexer = NULL;
3099 /* This file might have been a context that's implicitly extern
3100 "C". If so, pop the lang context. (Only relevant for PCH.) */
3101 if (parser->implicit_extern_c)
3103 pop_lang_context ();
3104 parser->implicit_extern_c = false;
3107 /* Finish up. */
3108 finish_translation_unit ();
3110 success = true;
3112 else
3114 cp_parser_error (parser, "expected declaration");
3115 success = false;
3118 /* Make sure the declarator obstack was fully cleaned up. */
3119 gcc_assert (obstack_next_free (&declarator_obstack)
3120 == declarator_obstack_base);
3122 /* All went well. */
3123 return success;
3126 /* Expressions [gram.expr] */
3128 /* Parse a primary-expression.
3130 primary-expression:
3131 literal
3132 this
3133 ( expression )
3134 id-expression
3136 GNU Extensions:
3138 primary-expression:
3139 ( compound-statement )
3140 __builtin_va_arg ( assignment-expression , type-id )
3141 __builtin_offsetof ( type-id , offsetof-expression )
3143 C++ Extensions:
3144 __has_nothrow_assign ( type-id )
3145 __has_nothrow_constructor ( type-id )
3146 __has_nothrow_copy ( type-id )
3147 __has_trivial_assign ( type-id )
3148 __has_trivial_constructor ( type-id )
3149 __has_trivial_copy ( type-id )
3150 __has_trivial_destructor ( type-id )
3151 __has_virtual_destructor ( type-id )
3152 __is_abstract ( type-id )
3153 __is_base_of ( type-id , type-id )
3154 __is_class ( type-id )
3155 __is_convertible_to ( type-id , type-id )
3156 __is_empty ( type-id )
3157 __is_enum ( type-id )
3158 __is_pod ( type-id )
3159 __is_polymorphic ( type-id )
3160 __is_union ( type-id )
3162 Objective-C++ Extension:
3164 primary-expression:
3165 objc-expression
3167 literal:
3168 __null
3170 ADDRESS_P is true iff this expression was immediately preceded by
3171 "&" and therefore might denote a pointer-to-member. CAST_P is true
3172 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3173 true iff this expression is a template argument.
3175 Returns a representation of the expression. Upon return, *IDK
3176 indicates what kind of id-expression (if any) was present. */
3178 static tree
3179 cp_parser_primary_expression (cp_parser *parser,
3180 bool address_p,
3181 bool cast_p,
3182 bool template_arg_p,
3183 cp_id_kind *idk)
3185 cp_token *token = NULL;
3187 /* Assume the primary expression is not an id-expression. */
3188 *idk = CP_ID_KIND_NONE;
3190 /* Peek at the next token. */
3191 token = cp_lexer_peek_token (parser->lexer);
3192 switch (token->type)
3194 /* literal:
3195 integer-literal
3196 character-literal
3197 floating-literal
3198 string-literal
3199 boolean-literal */
3200 case CPP_CHAR:
3201 case CPP_CHAR16:
3202 case CPP_CHAR32:
3203 case CPP_WCHAR:
3204 case CPP_NUMBER:
3205 token = cp_lexer_consume_token (parser->lexer);
3206 if (TREE_CODE (token->u.value) == FIXED_CST)
3208 error_at (token->location,
3209 "fixed-point types not supported in C++");
3210 return error_mark_node;
3212 /* Floating-point literals are only allowed in an integral
3213 constant expression if they are cast to an integral or
3214 enumeration type. */
3215 if (TREE_CODE (token->u.value) == REAL_CST
3216 && parser->integral_constant_expression_p
3217 && pedantic)
3219 /* CAST_P will be set even in invalid code like "int(2.7 +
3220 ...)". Therefore, we have to check that the next token
3221 is sure to end the cast. */
3222 if (cast_p)
3224 cp_token *next_token;
3226 next_token = cp_lexer_peek_token (parser->lexer);
3227 if (/* The comma at the end of an
3228 enumerator-definition. */
3229 next_token->type != CPP_COMMA
3230 /* The curly brace at the end of an enum-specifier. */
3231 && next_token->type != CPP_CLOSE_BRACE
3232 /* The end of a statement. */
3233 && next_token->type != CPP_SEMICOLON
3234 /* The end of the cast-expression. */
3235 && next_token->type != CPP_CLOSE_PAREN
3236 /* The end of an array bound. */
3237 && next_token->type != CPP_CLOSE_SQUARE
3238 /* The closing ">" in a template-argument-list. */
3239 && (next_token->type != CPP_GREATER
3240 || parser->greater_than_is_operator_p)
3241 /* C++0x only: A ">>" treated like two ">" tokens,
3242 in a template-argument-list. */
3243 && (next_token->type != CPP_RSHIFT
3244 || (cxx_dialect == cxx98)
3245 || parser->greater_than_is_operator_p))
3246 cast_p = false;
3249 /* If we are within a cast, then the constraint that the
3250 cast is to an integral or enumeration type will be
3251 checked at that point. If we are not within a cast, then
3252 this code is invalid. */
3253 if (!cast_p)
3254 cp_parser_non_integral_constant_expression
3255 (parser, "floating-point literal");
3257 return token->u.value;
3259 case CPP_STRING:
3260 case CPP_STRING16:
3261 case CPP_STRING32:
3262 case CPP_WSTRING:
3263 case CPP_UTF8STRING:
3264 /* ??? Should wide strings be allowed when parser->translate_strings_p
3265 is false (i.e. in attributes)? If not, we can kill the third
3266 argument to cp_parser_string_literal. */
3267 return cp_parser_string_literal (parser,
3268 parser->translate_strings_p,
3269 true);
3271 case CPP_OPEN_PAREN:
3273 tree expr;
3274 bool saved_greater_than_is_operator_p;
3276 /* Consume the `('. */
3277 cp_lexer_consume_token (parser->lexer);
3278 /* Within a parenthesized expression, a `>' token is always
3279 the greater-than operator. */
3280 saved_greater_than_is_operator_p
3281 = parser->greater_than_is_operator_p;
3282 parser->greater_than_is_operator_p = true;
3283 /* If we see `( { ' then we are looking at the beginning of
3284 a GNU statement-expression. */
3285 if (cp_parser_allow_gnu_extensions_p (parser)
3286 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3288 /* Statement-expressions are not allowed by the standard. */
3289 pedwarn (token->location, OPT_pedantic,
3290 "ISO C++ forbids braced-groups within expressions");
3292 /* And they're not allowed outside of a function-body; you
3293 cannot, for example, write:
3295 int i = ({ int j = 3; j + 1; });
3297 at class or namespace scope. */
3298 if (!parser->in_function_body
3299 || parser->in_template_argument_list_p)
3301 error_at (token->location,
3302 "statement-expressions are not allowed outside "
3303 "functions nor in template-argument lists");
3304 cp_parser_skip_to_end_of_block_or_statement (parser);
3305 expr = error_mark_node;
3307 else
3309 /* Start the statement-expression. */
3310 expr = begin_stmt_expr ();
3311 /* Parse the compound-statement. */
3312 cp_parser_compound_statement (parser, expr, false);
3313 /* Finish up. */
3314 expr = finish_stmt_expr (expr, false);
3317 else
3319 /* Parse the parenthesized expression. */
3320 expr = cp_parser_expression (parser, cast_p, idk);
3321 /* Let the front end know that this expression was
3322 enclosed in parentheses. This matters in case, for
3323 example, the expression is of the form `A::B', since
3324 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3325 not. */
3326 finish_parenthesized_expr (expr);
3328 /* The `>' token might be the end of a template-id or
3329 template-parameter-list now. */
3330 parser->greater_than_is_operator_p
3331 = saved_greater_than_is_operator_p;
3332 /* Consume the `)'. */
3333 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3334 cp_parser_skip_to_end_of_statement (parser);
3336 return expr;
3339 case CPP_OPEN_SQUARE:
3340 if (c_dialect_objc ())
3341 /* We have an Objective-C++ message. */
3342 return cp_parser_objc_expression (parser);
3343 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
3344 return cp_parser_lambda_expression (parser);
3346 case CPP_OBJC_STRING:
3347 if (c_dialect_objc ())
3348 /* We have an Objective-C++ string literal. */
3349 return cp_parser_objc_expression (parser);
3350 cp_parser_error (parser, "expected primary-expression");
3351 return error_mark_node;
3353 case CPP_KEYWORD:
3354 switch (token->keyword)
3356 /* These two are the boolean literals. */
3357 case RID_TRUE:
3358 cp_lexer_consume_token (parser->lexer);
3359 return boolean_true_node;
3360 case RID_FALSE:
3361 cp_lexer_consume_token (parser->lexer);
3362 return boolean_false_node;
3364 /* The `__null' literal. */
3365 case RID_NULL:
3366 cp_lexer_consume_token (parser->lexer);
3367 return null_node;
3369 /* Recognize the `this' keyword. */
3370 case RID_THIS:
3371 cp_lexer_consume_token (parser->lexer);
3372 if (parser->local_variables_forbidden_p)
3374 error_at (token->location,
3375 "%<this%> may not be used in this context");
3376 return error_mark_node;
3378 /* Pointers cannot appear in constant-expressions. */
3379 if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3380 return error_mark_node;
3381 return finish_this_expr ();
3383 /* The `operator' keyword can be the beginning of an
3384 id-expression. */
3385 case RID_OPERATOR:
3386 goto id_expression;
3388 case RID_FUNCTION_NAME:
3389 case RID_PRETTY_FUNCTION_NAME:
3390 case RID_C99_FUNCTION_NAME:
3392 const char *name;
3394 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3395 __func__ are the names of variables -- but they are
3396 treated specially. Therefore, they are handled here,
3397 rather than relying on the generic id-expression logic
3398 below. Grammatically, these names are id-expressions.
3400 Consume the token. */
3401 token = cp_lexer_consume_token (parser->lexer);
3403 switch (token->keyword)
3405 case RID_FUNCTION_NAME:
3406 name = "%<__FUNCTION__%>";
3407 break;
3408 case RID_PRETTY_FUNCTION_NAME:
3409 name = "%<__PRETTY_FUNCTION__%>";
3410 break;
3411 case RID_C99_FUNCTION_NAME:
3412 name = "%<__func__%>";
3413 break;
3414 default:
3415 gcc_unreachable ();
3418 if (cp_parser_non_integral_constant_expression (parser, name))
3419 return error_mark_node;
3421 /* Look up the name. */
3422 return finish_fname (token->u.value);
3425 case RID_VA_ARG:
3427 tree expression;
3428 tree type;
3430 /* The `__builtin_va_arg' construct is used to handle
3431 `va_arg'. Consume the `__builtin_va_arg' token. */
3432 cp_lexer_consume_token (parser->lexer);
3433 /* Look for the opening `('. */
3434 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3435 /* Now, parse the assignment-expression. */
3436 expression = cp_parser_assignment_expression (parser,
3437 /*cast_p=*/false, NULL);
3438 /* Look for the `,'. */
3439 cp_parser_require (parser, CPP_COMMA, "%<,%>");
3440 /* Parse the type-id. */
3441 type = cp_parser_type_id (parser);
3442 /* Look for the closing `)'. */
3443 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3444 /* Using `va_arg' in a constant-expression is not
3445 allowed. */
3446 if (cp_parser_non_integral_constant_expression (parser,
3447 "%<va_arg%>"))
3448 return error_mark_node;
3449 return build_x_va_arg (expression, type);
3452 case RID_OFFSETOF:
3453 return cp_parser_builtin_offsetof (parser);
3455 case RID_HAS_NOTHROW_ASSIGN:
3456 case RID_HAS_NOTHROW_CONSTRUCTOR:
3457 case RID_HAS_NOTHROW_COPY:
3458 case RID_HAS_TRIVIAL_ASSIGN:
3459 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3460 case RID_HAS_TRIVIAL_COPY:
3461 case RID_HAS_TRIVIAL_DESTRUCTOR:
3462 case RID_HAS_VIRTUAL_DESTRUCTOR:
3463 case RID_IS_ABSTRACT:
3464 case RID_IS_BASE_OF:
3465 case RID_IS_CLASS:
3466 case RID_IS_CONVERTIBLE_TO:
3467 case RID_IS_EMPTY:
3468 case RID_IS_ENUM:
3469 case RID_IS_POD:
3470 case RID_IS_POLYMORPHIC:
3471 case RID_IS_STD_LAYOUT:
3472 case RID_IS_TRIVIAL:
3473 case RID_IS_UNION:
3474 return cp_parser_trait_expr (parser, token->keyword);
3476 /* Objective-C++ expressions. */
3477 case RID_AT_ENCODE:
3478 case RID_AT_PROTOCOL:
3479 case RID_AT_SELECTOR:
3480 return cp_parser_objc_expression (parser);
3482 default:
3483 cp_parser_error (parser, "expected primary-expression");
3484 return error_mark_node;
3487 /* An id-expression can start with either an identifier, a
3488 `::' as the beginning of a qualified-id, or the "operator"
3489 keyword. */
3490 case CPP_NAME:
3491 case CPP_SCOPE:
3492 case CPP_TEMPLATE_ID:
3493 case CPP_NESTED_NAME_SPECIFIER:
3495 tree id_expression;
3496 tree decl;
3497 const char *error_msg;
3498 bool template_p;
3499 bool done;
3500 cp_token *id_expr_token;
3502 id_expression:
3503 /* Parse the id-expression. */
3504 id_expression
3505 = cp_parser_id_expression (parser,
3506 /*template_keyword_p=*/false,
3507 /*check_dependency_p=*/true,
3508 &template_p,
3509 /*declarator_p=*/false,
3510 /*optional_p=*/false);
3511 if (id_expression == error_mark_node)
3512 return error_mark_node;
3513 id_expr_token = token;
3514 token = cp_lexer_peek_token (parser->lexer);
3515 done = (token->type != CPP_OPEN_SQUARE
3516 && token->type != CPP_OPEN_PAREN
3517 && token->type != CPP_DOT
3518 && token->type != CPP_DEREF
3519 && token->type != CPP_PLUS_PLUS
3520 && token->type != CPP_MINUS_MINUS);
3521 /* If we have a template-id, then no further lookup is
3522 required. If the template-id was for a template-class, we
3523 will sometimes have a TYPE_DECL at this point. */
3524 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3525 || TREE_CODE (id_expression) == TYPE_DECL)
3526 decl = id_expression;
3527 /* Look up the name. */
3528 else
3530 tree ambiguous_decls;
3532 /* If we already know that this lookup is ambiguous, then
3533 we've already issued an error message; there's no reason
3534 to check again. */
3535 if (id_expr_token->type == CPP_NAME
3536 && id_expr_token->ambiguous_p)
3538 cp_parser_simulate_error (parser);
3539 return error_mark_node;
3542 decl = cp_parser_lookup_name (parser, id_expression,
3543 none_type,
3544 template_p,
3545 /*is_namespace=*/false,
3546 /*check_dependency=*/true,
3547 &ambiguous_decls,
3548 id_expr_token->location);
3549 /* If the lookup was ambiguous, an error will already have
3550 been issued. */
3551 if (ambiguous_decls)
3552 return error_mark_node;
3554 /* In Objective-C++, an instance variable (ivar) may be preferred
3555 to whatever cp_parser_lookup_name() found. */
3556 decl = objc_lookup_ivar (decl, id_expression);
3558 /* If name lookup gives us a SCOPE_REF, then the
3559 qualifying scope was dependent. */
3560 if (TREE_CODE (decl) == SCOPE_REF)
3562 /* At this point, we do not know if DECL is a valid
3563 integral constant expression. We assume that it is
3564 in fact such an expression, so that code like:
3566 template <int N> struct A {
3567 int a[B<N>::i];
3570 is accepted. At template-instantiation time, we
3571 will check that B<N>::i is actually a constant. */
3572 return decl;
3574 /* Check to see if DECL is a local variable in a context
3575 where that is forbidden. */
3576 if (parser->local_variables_forbidden_p
3577 && local_variable_p (decl))
3579 /* It might be that we only found DECL because we are
3580 trying to be generous with pre-ISO scoping rules.
3581 For example, consider:
3583 int i;
3584 void g() {
3585 for (int i = 0; i < 10; ++i) {}
3586 extern void f(int j = i);
3589 Here, name look up will originally find the out
3590 of scope `i'. We need to issue a warning message,
3591 but then use the global `i'. */
3592 decl = check_for_out_of_scope_variable (decl);
3593 if (local_variable_p (decl))
3595 error_at (id_expr_token->location,
3596 "local variable %qD may not appear in this context",
3597 decl);
3598 return error_mark_node;
3603 decl = (finish_id_expression
3604 (id_expression, decl, parser->scope,
3605 idk,
3606 parser->integral_constant_expression_p,
3607 parser->allow_non_integral_constant_expression_p,
3608 &parser->non_integral_constant_expression_p,
3609 template_p, done, address_p,
3610 template_arg_p,
3611 &error_msg,
3612 id_expr_token->location));
3613 if (error_msg)
3614 cp_parser_error (parser, error_msg);
3615 return decl;
3618 /* Anything else is an error. */
3619 default:
3620 cp_parser_error (parser, "expected primary-expression");
3621 return error_mark_node;
3625 /* Parse an id-expression.
3627 id-expression:
3628 unqualified-id
3629 qualified-id
3631 qualified-id:
3632 :: [opt] nested-name-specifier template [opt] unqualified-id
3633 :: identifier
3634 :: operator-function-id
3635 :: template-id
3637 Return a representation of the unqualified portion of the
3638 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3639 a `::' or nested-name-specifier.
3641 Often, if the id-expression was a qualified-id, the caller will
3642 want to make a SCOPE_REF to represent the qualified-id. This
3643 function does not do this in order to avoid wastefully creating
3644 SCOPE_REFs when they are not required.
3646 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3647 `template' keyword.
3649 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3650 uninstantiated templates.
3652 If *TEMPLATE_P is non-NULL, it is set to true iff the
3653 `template' keyword is used to explicitly indicate that the entity
3654 named is a template.
3656 If DECLARATOR_P is true, the id-expression is appearing as part of
3657 a declarator, rather than as part of an expression. */
3659 static tree
3660 cp_parser_id_expression (cp_parser *parser,
3661 bool template_keyword_p,
3662 bool check_dependency_p,
3663 bool *template_p,
3664 bool declarator_p,
3665 bool optional_p)
3667 bool global_scope_p;
3668 bool nested_name_specifier_p;
3670 /* Assume the `template' keyword was not used. */
3671 if (template_p)
3672 *template_p = template_keyword_p;
3674 /* Look for the optional `::' operator. */
3675 global_scope_p
3676 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3677 != NULL_TREE);
3678 /* Look for the optional nested-name-specifier. */
3679 nested_name_specifier_p
3680 = (cp_parser_nested_name_specifier_opt (parser,
3681 /*typename_keyword_p=*/false,
3682 check_dependency_p,
3683 /*type_p=*/false,
3684 declarator_p)
3685 != NULL_TREE);
3686 /* If there is a nested-name-specifier, then we are looking at
3687 the first qualified-id production. */
3688 if (nested_name_specifier_p)
3690 tree saved_scope;
3691 tree saved_object_scope;
3692 tree saved_qualifying_scope;
3693 tree unqualified_id;
3694 bool is_template;
3696 /* See if the next token is the `template' keyword. */
3697 if (!template_p)
3698 template_p = &is_template;
3699 *template_p = cp_parser_optional_template_keyword (parser);
3700 /* Name lookup we do during the processing of the
3701 unqualified-id might obliterate SCOPE. */
3702 saved_scope = parser->scope;
3703 saved_object_scope = parser->object_scope;
3704 saved_qualifying_scope = parser->qualifying_scope;
3705 /* Process the final unqualified-id. */
3706 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3707 check_dependency_p,
3708 declarator_p,
3709 /*optional_p=*/false);
3710 /* Restore the SAVED_SCOPE for our caller. */
3711 parser->scope = saved_scope;
3712 parser->object_scope = saved_object_scope;
3713 parser->qualifying_scope = saved_qualifying_scope;
3715 return unqualified_id;
3717 /* Otherwise, if we are in global scope, then we are looking at one
3718 of the other qualified-id productions. */
3719 else if (global_scope_p)
3721 cp_token *token;
3722 tree id;
3724 /* Peek at the next token. */
3725 token = cp_lexer_peek_token (parser->lexer);
3727 /* If it's an identifier, and the next token is not a "<", then
3728 we can avoid the template-id case. This is an optimization
3729 for this common case. */
3730 if (token->type == CPP_NAME
3731 && !cp_parser_nth_token_starts_template_argument_list_p
3732 (parser, 2))
3733 return cp_parser_identifier (parser);
3735 cp_parser_parse_tentatively (parser);
3736 /* Try a template-id. */
3737 id = cp_parser_template_id (parser,
3738 /*template_keyword_p=*/false,
3739 /*check_dependency_p=*/true,
3740 declarator_p);
3741 /* If that worked, we're done. */
3742 if (cp_parser_parse_definitely (parser))
3743 return id;
3745 /* Peek at the next token. (Changes in the token buffer may
3746 have invalidated the pointer obtained above.) */
3747 token = cp_lexer_peek_token (parser->lexer);
3749 switch (token->type)
3751 case CPP_NAME:
3752 return cp_parser_identifier (parser);
3754 case CPP_KEYWORD:
3755 if (token->keyword == RID_OPERATOR)
3756 return cp_parser_operator_function_id (parser);
3757 /* Fall through. */
3759 default:
3760 cp_parser_error (parser, "expected id-expression");
3761 return error_mark_node;
3764 else
3765 return cp_parser_unqualified_id (parser, template_keyword_p,
3766 /*check_dependency_p=*/true,
3767 declarator_p,
3768 optional_p);
3771 /* Parse an unqualified-id.
3773 unqualified-id:
3774 identifier
3775 operator-function-id
3776 conversion-function-id
3777 ~ class-name
3778 template-id
3780 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3781 keyword, in a construct like `A::template ...'.
3783 Returns a representation of unqualified-id. For the `identifier'
3784 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3785 production a BIT_NOT_EXPR is returned; the operand of the
3786 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3787 other productions, see the documentation accompanying the
3788 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3789 names are looked up in uninstantiated templates. If DECLARATOR_P
3790 is true, the unqualified-id is appearing as part of a declarator,
3791 rather than as part of an expression. */
3793 static tree
3794 cp_parser_unqualified_id (cp_parser* parser,
3795 bool template_keyword_p,
3796 bool check_dependency_p,
3797 bool declarator_p,
3798 bool optional_p)
3800 cp_token *token;
3802 /* Peek at the next token. */
3803 token = cp_lexer_peek_token (parser->lexer);
3805 switch (token->type)
3807 case CPP_NAME:
3809 tree id;
3811 /* We don't know yet whether or not this will be a
3812 template-id. */
3813 cp_parser_parse_tentatively (parser);
3814 /* Try a template-id. */
3815 id = cp_parser_template_id (parser, template_keyword_p,
3816 check_dependency_p,
3817 declarator_p);
3818 /* If it worked, we're done. */
3819 if (cp_parser_parse_definitely (parser))
3820 return id;
3821 /* Otherwise, it's an ordinary identifier. */
3822 return cp_parser_identifier (parser);
3825 case CPP_TEMPLATE_ID:
3826 return cp_parser_template_id (parser, template_keyword_p,
3827 check_dependency_p,
3828 declarator_p);
3830 case CPP_COMPL:
3832 tree type_decl;
3833 tree qualifying_scope;
3834 tree object_scope;
3835 tree scope;
3836 bool done;
3838 /* Consume the `~' token. */
3839 cp_lexer_consume_token (parser->lexer);
3840 /* Parse the class-name. The standard, as written, seems to
3841 say that:
3843 template <typename T> struct S { ~S (); };
3844 template <typename T> S<T>::~S() {}
3846 is invalid, since `~' must be followed by a class-name, but
3847 `S<T>' is dependent, and so not known to be a class.
3848 That's not right; we need to look in uninstantiated
3849 templates. A further complication arises from:
3851 template <typename T> void f(T t) {
3852 t.T::~T();
3855 Here, it is not possible to look up `T' in the scope of `T'
3856 itself. We must look in both the current scope, and the
3857 scope of the containing complete expression.
3859 Yet another issue is:
3861 struct S {
3862 int S;
3863 ~S();
3866 S::~S() {}
3868 The standard does not seem to say that the `S' in `~S'
3869 should refer to the type `S' and not the data member
3870 `S::S'. */
3872 /* DR 244 says that we look up the name after the "~" in the
3873 same scope as we looked up the qualifying name. That idea
3874 isn't fully worked out; it's more complicated than that. */
3875 scope = parser->scope;
3876 object_scope = parser->object_scope;
3877 qualifying_scope = parser->qualifying_scope;
3879 /* Check for invalid scopes. */
3880 if (scope == error_mark_node)
3882 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3883 cp_lexer_consume_token (parser->lexer);
3884 return error_mark_node;
3886 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3888 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3889 error_at (token->location,
3890 "scope %qT before %<~%> is not a class-name",
3891 scope);
3892 cp_parser_simulate_error (parser);
3893 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3894 cp_lexer_consume_token (parser->lexer);
3895 return error_mark_node;
3897 gcc_assert (!scope || TYPE_P (scope));
3899 /* If the name is of the form "X::~X" it's OK. */
3900 token = cp_lexer_peek_token (parser->lexer);
3901 if (scope
3902 && token->type == CPP_NAME
3903 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3904 != CPP_LESS)
3905 && constructor_name_p (token->u.value, scope))
3907 cp_lexer_consume_token (parser->lexer);
3908 return build_nt (BIT_NOT_EXPR, scope);
3911 /* If there was an explicit qualification (S::~T), first look
3912 in the scope given by the qualification (i.e., S).
3914 Note: in the calls to cp_parser_class_name below we pass
3915 typename_type so that lookup finds the injected-class-name
3916 rather than the constructor. */
3917 done = false;
3918 type_decl = NULL_TREE;
3919 if (scope)
3921 cp_parser_parse_tentatively (parser);
3922 type_decl = cp_parser_class_name (parser,
3923 /*typename_keyword_p=*/false,
3924 /*template_keyword_p=*/false,
3925 typename_type,
3926 /*check_dependency=*/false,
3927 /*class_head_p=*/false,
3928 declarator_p);
3929 if (cp_parser_parse_definitely (parser))
3930 done = true;
3932 /* In "N::S::~S", look in "N" as well. */
3933 if (!done && scope && qualifying_scope)
3935 cp_parser_parse_tentatively (parser);
3936 parser->scope = qualifying_scope;
3937 parser->object_scope = NULL_TREE;
3938 parser->qualifying_scope = NULL_TREE;
3939 type_decl
3940 = cp_parser_class_name (parser,
3941 /*typename_keyword_p=*/false,
3942 /*template_keyword_p=*/false,
3943 typename_type,
3944 /*check_dependency=*/false,
3945 /*class_head_p=*/false,
3946 declarator_p);
3947 if (cp_parser_parse_definitely (parser))
3948 done = true;
3950 /* In "p->S::~T", look in the scope given by "*p" as well. */
3951 else if (!done && object_scope)
3953 cp_parser_parse_tentatively (parser);
3954 parser->scope = object_scope;
3955 parser->object_scope = NULL_TREE;
3956 parser->qualifying_scope = NULL_TREE;
3957 type_decl
3958 = cp_parser_class_name (parser,
3959 /*typename_keyword_p=*/false,
3960 /*template_keyword_p=*/false,
3961 typename_type,
3962 /*check_dependency=*/false,
3963 /*class_head_p=*/false,
3964 declarator_p);
3965 if (cp_parser_parse_definitely (parser))
3966 done = true;
3968 /* Look in the surrounding context. */
3969 if (!done)
3971 parser->scope = NULL_TREE;
3972 parser->object_scope = NULL_TREE;
3973 parser->qualifying_scope = NULL_TREE;
3974 if (processing_template_decl)
3975 cp_parser_parse_tentatively (parser);
3976 type_decl
3977 = cp_parser_class_name (parser,
3978 /*typename_keyword_p=*/false,
3979 /*template_keyword_p=*/false,
3980 typename_type,
3981 /*check_dependency=*/false,
3982 /*class_head_p=*/false,
3983 declarator_p);
3984 if (processing_template_decl
3985 && ! cp_parser_parse_definitely (parser))
3987 /* We couldn't find a type with this name, so just accept
3988 it and check for a match at instantiation time. */
3989 type_decl = cp_parser_identifier (parser);
3990 if (type_decl != error_mark_node)
3991 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
3992 return type_decl;
3995 /* If an error occurred, assume that the name of the
3996 destructor is the same as the name of the qualifying
3997 class. That allows us to keep parsing after running
3998 into ill-formed destructor names. */
3999 if (type_decl == error_mark_node && scope)
4000 return build_nt (BIT_NOT_EXPR, scope);
4001 else if (type_decl == error_mark_node)
4002 return error_mark_node;
4004 /* Check that destructor name and scope match. */
4005 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4007 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4008 error_at (token->location,
4009 "declaration of %<~%T%> as member of %qT",
4010 type_decl, scope);
4011 cp_parser_simulate_error (parser);
4012 return error_mark_node;
4015 /* [class.dtor]
4017 A typedef-name that names a class shall not be used as the
4018 identifier in the declarator for a destructor declaration. */
4019 if (declarator_p
4020 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4021 && !DECL_SELF_REFERENCE_P (type_decl)
4022 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4023 error_at (token->location,
4024 "typedef-name %qD used as destructor declarator",
4025 type_decl);
4027 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4030 case CPP_KEYWORD:
4031 if (token->keyword == RID_OPERATOR)
4033 tree id;
4035 /* This could be a template-id, so we try that first. */
4036 cp_parser_parse_tentatively (parser);
4037 /* Try a template-id. */
4038 id = cp_parser_template_id (parser, template_keyword_p,
4039 /*check_dependency_p=*/true,
4040 declarator_p);
4041 /* If that worked, we're done. */
4042 if (cp_parser_parse_definitely (parser))
4043 return id;
4044 /* We still don't know whether we're looking at an
4045 operator-function-id or a conversion-function-id. */
4046 cp_parser_parse_tentatively (parser);
4047 /* Try an operator-function-id. */
4048 id = cp_parser_operator_function_id (parser);
4049 /* If that didn't work, try a conversion-function-id. */
4050 if (!cp_parser_parse_definitely (parser))
4051 id = cp_parser_conversion_function_id (parser);
4053 return id;
4055 /* Fall through. */
4057 default:
4058 if (optional_p)
4059 return NULL_TREE;
4060 cp_parser_error (parser, "expected unqualified-id");
4061 return error_mark_node;
4065 /* Parse an (optional) nested-name-specifier.
4067 nested-name-specifier: [C++98]
4068 class-or-namespace-name :: nested-name-specifier [opt]
4069 class-or-namespace-name :: template nested-name-specifier [opt]
4071 nested-name-specifier: [C++0x]
4072 type-name ::
4073 namespace-name ::
4074 nested-name-specifier identifier ::
4075 nested-name-specifier template [opt] simple-template-id ::
4077 PARSER->SCOPE should be set appropriately before this function is
4078 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4079 effect. TYPE_P is TRUE if we non-type bindings should be ignored
4080 in name lookups.
4082 Sets PARSER->SCOPE to the class (TYPE) or namespace
4083 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4084 it unchanged if there is no nested-name-specifier. Returns the new
4085 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4087 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4088 part of a declaration and/or decl-specifier. */
4090 static tree
4091 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4092 bool typename_keyword_p,
4093 bool check_dependency_p,
4094 bool type_p,
4095 bool is_declaration)
4097 bool success = false;
4098 cp_token_position start = 0;
4099 cp_token *token;
4101 /* Remember where the nested-name-specifier starts. */
4102 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4104 start = cp_lexer_token_position (parser->lexer, false);
4105 push_deferring_access_checks (dk_deferred);
4108 while (true)
4110 tree new_scope;
4111 tree old_scope;
4112 tree saved_qualifying_scope;
4113 bool template_keyword_p;
4115 /* Spot cases that cannot be the beginning of a
4116 nested-name-specifier. */
4117 token = cp_lexer_peek_token (parser->lexer);
4119 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4120 the already parsed nested-name-specifier. */
4121 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4123 /* Grab the nested-name-specifier and continue the loop. */
4124 cp_parser_pre_parsed_nested_name_specifier (parser);
4125 /* If we originally encountered this nested-name-specifier
4126 with IS_DECLARATION set to false, we will not have
4127 resolved TYPENAME_TYPEs, so we must do so here. */
4128 if (is_declaration
4129 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4131 new_scope = resolve_typename_type (parser->scope,
4132 /*only_current_p=*/false);
4133 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4134 parser->scope = new_scope;
4136 success = true;
4137 continue;
4140 /* Spot cases that cannot be the beginning of a
4141 nested-name-specifier. On the second and subsequent times
4142 through the loop, we look for the `template' keyword. */
4143 if (success && token->keyword == RID_TEMPLATE)
4145 /* A template-id can start a nested-name-specifier. */
4146 else if (token->type == CPP_TEMPLATE_ID)
4148 else
4150 /* If the next token is not an identifier, then it is
4151 definitely not a type-name or namespace-name. */
4152 if (token->type != CPP_NAME)
4153 break;
4154 /* If the following token is neither a `<' (to begin a
4155 template-id), nor a `::', then we are not looking at a
4156 nested-name-specifier. */
4157 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4158 if (token->type != CPP_SCOPE
4159 && !cp_parser_nth_token_starts_template_argument_list_p
4160 (parser, 2))
4161 break;
4164 /* The nested-name-specifier is optional, so we parse
4165 tentatively. */
4166 cp_parser_parse_tentatively (parser);
4168 /* Look for the optional `template' keyword, if this isn't the
4169 first time through the loop. */
4170 if (success)
4171 template_keyword_p = cp_parser_optional_template_keyword (parser);
4172 else
4173 template_keyword_p = false;
4175 /* Save the old scope since the name lookup we are about to do
4176 might destroy it. */
4177 old_scope = parser->scope;
4178 saved_qualifying_scope = parser->qualifying_scope;
4179 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4180 look up names in "X<T>::I" in order to determine that "Y" is
4181 a template. So, if we have a typename at this point, we make
4182 an effort to look through it. */
4183 if (is_declaration
4184 && !typename_keyword_p
4185 && parser->scope
4186 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4187 parser->scope = resolve_typename_type (parser->scope,
4188 /*only_current_p=*/false);
4189 /* Parse the qualifying entity. */
4190 new_scope
4191 = cp_parser_qualifying_entity (parser,
4192 typename_keyword_p,
4193 template_keyword_p,
4194 check_dependency_p,
4195 type_p,
4196 is_declaration);
4197 /* Look for the `::' token. */
4198 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4200 /* If we found what we wanted, we keep going; otherwise, we're
4201 done. */
4202 if (!cp_parser_parse_definitely (parser))
4204 bool error_p = false;
4206 /* Restore the OLD_SCOPE since it was valid before the
4207 failed attempt at finding the last
4208 class-or-namespace-name. */
4209 parser->scope = old_scope;
4210 parser->qualifying_scope = saved_qualifying_scope;
4211 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4212 break;
4213 /* If the next token is an identifier, and the one after
4214 that is a `::', then any valid interpretation would have
4215 found a class-or-namespace-name. */
4216 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4217 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4218 == CPP_SCOPE)
4219 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4220 != CPP_COMPL))
4222 token = cp_lexer_consume_token (parser->lexer);
4223 if (!error_p)
4225 if (!token->ambiguous_p)
4227 tree decl;
4228 tree ambiguous_decls;
4230 decl = cp_parser_lookup_name (parser, token->u.value,
4231 none_type,
4232 /*is_template=*/false,
4233 /*is_namespace=*/false,
4234 /*check_dependency=*/true,
4235 &ambiguous_decls,
4236 token->location);
4237 if (TREE_CODE (decl) == TEMPLATE_DECL)
4238 error_at (token->location,
4239 "%qD used without template parameters",
4240 decl);
4241 else if (ambiguous_decls)
4243 error_at (token->location,
4244 "reference to %qD is ambiguous",
4245 token->u.value);
4246 print_candidates (ambiguous_decls);
4247 decl = error_mark_node;
4249 else
4251 const char* msg = "is not a class or namespace";
4252 if (cxx_dialect != cxx98)
4253 msg = "is not a class, namespace, or enumeration";
4254 cp_parser_name_lookup_error
4255 (parser, token->u.value, decl, msg,
4256 token->location);
4259 parser->scope = error_mark_node;
4260 error_p = true;
4261 /* Treat this as a successful nested-name-specifier
4262 due to:
4264 [basic.lookup.qual]
4266 If the name found is not a class-name (clause
4267 _class_) or namespace-name (_namespace.def_), the
4268 program is ill-formed. */
4269 success = true;
4271 cp_lexer_consume_token (parser->lexer);
4273 break;
4275 /* We've found one valid nested-name-specifier. */
4276 success = true;
4277 /* Name lookup always gives us a DECL. */
4278 if (TREE_CODE (new_scope) == TYPE_DECL)
4279 new_scope = TREE_TYPE (new_scope);
4280 /* Uses of "template" must be followed by actual templates. */
4281 if (template_keyword_p
4282 && !(CLASS_TYPE_P (new_scope)
4283 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4284 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4285 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4286 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4287 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4288 == TEMPLATE_ID_EXPR)))
4289 permerror (input_location, TYPE_P (new_scope)
4290 ? "%qT is not a template"
4291 : "%qD is not a template",
4292 new_scope);
4293 /* If it is a class scope, try to complete it; we are about to
4294 be looking up names inside the class. */
4295 if (TYPE_P (new_scope)
4296 /* Since checking types for dependency can be expensive,
4297 avoid doing it if the type is already complete. */
4298 && !COMPLETE_TYPE_P (new_scope)
4299 /* Do not try to complete dependent types. */
4300 && !dependent_type_p (new_scope))
4302 new_scope = complete_type (new_scope);
4303 /* If it is a typedef to current class, use the current
4304 class instead, as the typedef won't have any names inside
4305 it yet. */
4306 if (!COMPLETE_TYPE_P (new_scope)
4307 && currently_open_class (new_scope))
4308 new_scope = TYPE_MAIN_VARIANT (new_scope);
4310 /* Make sure we look in the right scope the next time through
4311 the loop. */
4312 parser->scope = new_scope;
4315 /* If parsing tentatively, replace the sequence of tokens that makes
4316 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4317 token. That way, should we re-parse the token stream, we will
4318 not have to repeat the effort required to do the parse, nor will
4319 we issue duplicate error messages. */
4320 if (success && start)
4322 cp_token *token;
4324 token = cp_lexer_token_at (parser->lexer, start);
4325 /* Reset the contents of the START token. */
4326 token->type = CPP_NESTED_NAME_SPECIFIER;
4327 /* Retrieve any deferred checks. Do not pop this access checks yet
4328 so the memory will not be reclaimed during token replacing below. */
4329 token->u.tree_check_value = GGC_CNEW (struct tree_check);
4330 token->u.tree_check_value->value = parser->scope;
4331 token->u.tree_check_value->checks = get_deferred_access_checks ();
4332 token->u.tree_check_value->qualifying_scope =
4333 parser->qualifying_scope;
4334 token->keyword = RID_MAX;
4336 /* Purge all subsequent tokens. */
4337 cp_lexer_purge_tokens_after (parser->lexer, start);
4340 if (start)
4341 pop_to_parent_deferring_access_checks ();
4343 return success ? parser->scope : NULL_TREE;
4346 /* Parse a nested-name-specifier. See
4347 cp_parser_nested_name_specifier_opt for details. This function
4348 behaves identically, except that it will an issue an error if no
4349 nested-name-specifier is present. */
4351 static tree
4352 cp_parser_nested_name_specifier (cp_parser *parser,
4353 bool typename_keyword_p,
4354 bool check_dependency_p,
4355 bool type_p,
4356 bool is_declaration)
4358 tree scope;
4360 /* Look for the nested-name-specifier. */
4361 scope = cp_parser_nested_name_specifier_opt (parser,
4362 typename_keyword_p,
4363 check_dependency_p,
4364 type_p,
4365 is_declaration);
4366 /* If it was not present, issue an error message. */
4367 if (!scope)
4369 cp_parser_error (parser, "expected nested-name-specifier");
4370 parser->scope = NULL_TREE;
4373 return scope;
4376 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4377 this is either a class-name or a namespace-name (which corresponds
4378 to the class-or-namespace-name production in the grammar). For
4379 C++0x, it can also be a type-name that refers to an enumeration
4380 type.
4382 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4383 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4384 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4385 TYPE_P is TRUE iff the next name should be taken as a class-name,
4386 even the same name is declared to be another entity in the same
4387 scope.
4389 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4390 specified by the class-or-namespace-name. If neither is found the
4391 ERROR_MARK_NODE is returned. */
4393 static tree
4394 cp_parser_qualifying_entity (cp_parser *parser,
4395 bool typename_keyword_p,
4396 bool template_keyword_p,
4397 bool check_dependency_p,
4398 bool type_p,
4399 bool is_declaration)
4401 tree saved_scope;
4402 tree saved_qualifying_scope;
4403 tree saved_object_scope;
4404 tree scope;
4405 bool only_class_p;
4406 bool successful_parse_p;
4408 /* Before we try to parse the class-name, we must save away the
4409 current PARSER->SCOPE since cp_parser_class_name will destroy
4410 it. */
4411 saved_scope = parser->scope;
4412 saved_qualifying_scope = parser->qualifying_scope;
4413 saved_object_scope = parser->object_scope;
4414 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4415 there is no need to look for a namespace-name. */
4416 only_class_p = template_keyword_p
4417 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4418 if (!only_class_p)
4419 cp_parser_parse_tentatively (parser);
4420 scope = cp_parser_class_name (parser,
4421 typename_keyword_p,
4422 template_keyword_p,
4423 type_p ? class_type : none_type,
4424 check_dependency_p,
4425 /*class_head_p=*/false,
4426 is_declaration);
4427 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4428 /* If that didn't work and we're in C++0x mode, try for a type-name. */
4429 if (!only_class_p
4430 && cxx_dialect != cxx98
4431 && !successful_parse_p)
4433 /* Restore the saved scope. */
4434 parser->scope = saved_scope;
4435 parser->qualifying_scope = saved_qualifying_scope;
4436 parser->object_scope = saved_object_scope;
4438 /* Parse tentatively. */
4439 cp_parser_parse_tentatively (parser);
4441 /* Parse a typedef-name or enum-name. */
4442 scope = cp_parser_nonclass_name (parser);
4443 successful_parse_p = cp_parser_parse_definitely (parser);
4445 /* If that didn't work, try for a namespace-name. */
4446 if (!only_class_p && !successful_parse_p)
4448 /* Restore the saved scope. */
4449 parser->scope = saved_scope;
4450 parser->qualifying_scope = saved_qualifying_scope;
4451 parser->object_scope = saved_object_scope;
4452 /* If we are not looking at an identifier followed by the scope
4453 resolution operator, then this is not part of a
4454 nested-name-specifier. (Note that this function is only used
4455 to parse the components of a nested-name-specifier.) */
4456 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4457 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4458 return error_mark_node;
4459 scope = cp_parser_namespace_name (parser);
4462 return scope;
4465 /* Parse a postfix-expression.
4467 postfix-expression:
4468 primary-expression
4469 postfix-expression [ expression ]
4470 postfix-expression ( expression-list [opt] )
4471 simple-type-specifier ( expression-list [opt] )
4472 typename :: [opt] nested-name-specifier identifier
4473 ( expression-list [opt] )
4474 typename :: [opt] nested-name-specifier template [opt] template-id
4475 ( expression-list [opt] )
4476 postfix-expression . template [opt] id-expression
4477 postfix-expression -> template [opt] id-expression
4478 postfix-expression . pseudo-destructor-name
4479 postfix-expression -> pseudo-destructor-name
4480 postfix-expression ++
4481 postfix-expression --
4482 dynamic_cast < type-id > ( expression )
4483 static_cast < type-id > ( expression )
4484 reinterpret_cast < type-id > ( expression )
4485 const_cast < type-id > ( expression )
4486 typeid ( expression )
4487 typeid ( type-id )
4489 GNU Extension:
4491 postfix-expression:
4492 ( type-id ) { initializer-list , [opt] }
4494 This extension is a GNU version of the C99 compound-literal
4495 construct. (The C99 grammar uses `type-name' instead of `type-id',
4496 but they are essentially the same concept.)
4498 If ADDRESS_P is true, the postfix expression is the operand of the
4499 `&' operator. CAST_P is true if this expression is the target of a
4500 cast.
4502 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4503 class member access expressions [expr.ref].
4505 Returns a representation of the expression. */
4507 static tree
4508 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4509 bool member_access_only_p,
4510 cp_id_kind * pidk_return)
4512 cp_token *token;
4513 enum rid keyword;
4514 cp_id_kind idk = CP_ID_KIND_NONE;
4515 tree postfix_expression = NULL_TREE;
4516 bool is_member_access = false;
4518 /* Peek at the next token. */
4519 token = cp_lexer_peek_token (parser->lexer);
4520 /* Some of the productions are determined by keywords. */
4521 keyword = token->keyword;
4522 switch (keyword)
4524 case RID_DYNCAST:
4525 case RID_STATCAST:
4526 case RID_REINTCAST:
4527 case RID_CONSTCAST:
4529 tree type;
4530 tree expression;
4531 const char *saved_message;
4533 /* All of these can be handled in the same way from the point
4534 of view of parsing. Begin by consuming the token
4535 identifying the cast. */
4536 cp_lexer_consume_token (parser->lexer);
4538 /* New types cannot be defined in the cast. */
4539 saved_message = parser->type_definition_forbidden_message;
4540 parser->type_definition_forbidden_message
4541 = G_("types may not be defined in casts");
4543 /* Look for the opening `<'. */
4544 cp_parser_require (parser, CPP_LESS, "%<<%>");
4545 /* Parse the type to which we are casting. */
4546 type = cp_parser_type_id (parser);
4547 /* Look for the closing `>'. */
4548 cp_parser_require (parser, CPP_GREATER, "%<>%>");
4549 /* Restore the old message. */
4550 parser->type_definition_forbidden_message = saved_message;
4552 /* And the expression which is being cast. */
4553 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4554 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4555 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4557 /* Only type conversions to integral or enumeration types
4558 can be used in constant-expressions. */
4559 if (!cast_valid_in_integral_constant_expression_p (type)
4560 && (cp_parser_non_integral_constant_expression
4561 (parser,
4562 "a cast to a type other than an integral or "
4563 "enumeration type")))
4564 return error_mark_node;
4566 switch (keyword)
4568 case RID_DYNCAST:
4569 postfix_expression
4570 = build_dynamic_cast (type, expression, tf_warning_or_error);
4571 break;
4572 case RID_STATCAST:
4573 postfix_expression
4574 = build_static_cast (type, expression, tf_warning_or_error);
4575 break;
4576 case RID_REINTCAST:
4577 postfix_expression
4578 = build_reinterpret_cast (type, expression,
4579 tf_warning_or_error);
4580 break;
4581 case RID_CONSTCAST:
4582 postfix_expression
4583 = build_const_cast (type, expression, tf_warning_or_error);
4584 break;
4585 default:
4586 gcc_unreachable ();
4589 break;
4591 case RID_TYPEID:
4593 tree type;
4594 const char *saved_message;
4595 bool saved_in_type_id_in_expr_p;
4597 /* Consume the `typeid' token. */
4598 cp_lexer_consume_token (parser->lexer);
4599 /* Look for the `(' token. */
4600 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4601 /* Types cannot be defined in a `typeid' expression. */
4602 saved_message = parser->type_definition_forbidden_message;
4603 parser->type_definition_forbidden_message
4604 = G_("types may not be defined in a %<typeid%> expression");
4605 /* We can't be sure yet whether we're looking at a type-id or an
4606 expression. */
4607 cp_parser_parse_tentatively (parser);
4608 /* Try a type-id first. */
4609 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4610 parser->in_type_id_in_expr_p = true;
4611 type = cp_parser_type_id (parser);
4612 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4613 /* Look for the `)' token. Otherwise, we can't be sure that
4614 we're not looking at an expression: consider `typeid (int
4615 (3))', for example. */
4616 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4617 /* If all went well, simply lookup the type-id. */
4618 if (cp_parser_parse_definitely (parser))
4619 postfix_expression = get_typeid (type);
4620 /* Otherwise, fall back to the expression variant. */
4621 else
4623 tree expression;
4625 /* Look for an expression. */
4626 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4627 /* Compute its typeid. */
4628 postfix_expression = build_typeid (expression);
4629 /* Look for the `)' token. */
4630 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4632 /* Restore the saved message. */
4633 parser->type_definition_forbidden_message = saved_message;
4634 /* `typeid' may not appear in an integral constant expression. */
4635 if (cp_parser_non_integral_constant_expression(parser,
4636 "%<typeid%> operator"))
4637 return error_mark_node;
4639 break;
4641 case RID_TYPENAME:
4643 tree type;
4644 /* The syntax permitted here is the same permitted for an
4645 elaborated-type-specifier. */
4646 type = cp_parser_elaborated_type_specifier (parser,
4647 /*is_friend=*/false,
4648 /*is_declaration=*/false);
4649 postfix_expression = cp_parser_functional_cast (parser, type);
4651 break;
4653 default:
4655 tree type;
4657 /* If the next thing is a simple-type-specifier, we may be
4658 looking at a functional cast. We could also be looking at
4659 an id-expression. So, we try the functional cast, and if
4660 that doesn't work we fall back to the primary-expression. */
4661 cp_parser_parse_tentatively (parser);
4662 /* Look for the simple-type-specifier. */
4663 type = cp_parser_simple_type_specifier (parser,
4664 /*decl_specs=*/NULL,
4665 CP_PARSER_FLAGS_NONE);
4666 /* Parse the cast itself. */
4667 if (!cp_parser_error_occurred (parser))
4668 postfix_expression
4669 = cp_parser_functional_cast (parser, type);
4670 /* If that worked, we're done. */
4671 if (cp_parser_parse_definitely (parser))
4672 break;
4674 /* If the functional-cast didn't work out, try a
4675 compound-literal. */
4676 if (cp_parser_allow_gnu_extensions_p (parser)
4677 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4679 VEC(constructor_elt,gc) *initializer_list = NULL;
4680 bool saved_in_type_id_in_expr_p;
4682 cp_parser_parse_tentatively (parser);
4683 /* Consume the `('. */
4684 cp_lexer_consume_token (parser->lexer);
4685 /* Parse the type. */
4686 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4687 parser->in_type_id_in_expr_p = true;
4688 type = cp_parser_type_id (parser);
4689 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4690 /* Look for the `)'. */
4691 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4692 /* Look for the `{'. */
4693 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4694 /* If things aren't going well, there's no need to
4695 keep going. */
4696 if (!cp_parser_error_occurred (parser))
4698 bool non_constant_p;
4699 /* Parse the initializer-list. */
4700 initializer_list
4701 = cp_parser_initializer_list (parser, &non_constant_p);
4702 /* Allow a trailing `,'. */
4703 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4704 cp_lexer_consume_token (parser->lexer);
4705 /* Look for the final `}'. */
4706 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4708 /* If that worked, we're definitely looking at a
4709 compound-literal expression. */
4710 if (cp_parser_parse_definitely (parser))
4712 /* Warn the user that a compound literal is not
4713 allowed in standard C++. */
4714 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4715 /* For simplicity, we disallow compound literals in
4716 constant-expressions. We could
4717 allow compound literals of integer type, whose
4718 initializer was a constant, in constant
4719 expressions. Permitting that usage, as a further
4720 extension, would not change the meaning of any
4721 currently accepted programs. (Of course, as
4722 compound literals are not part of ISO C++, the
4723 standard has nothing to say.) */
4724 if (cp_parser_non_integral_constant_expression
4725 (parser, "non-constant compound literals"))
4727 postfix_expression = error_mark_node;
4728 break;
4730 /* Form the representation of the compound-literal. */
4731 postfix_expression
4732 = (finish_compound_literal
4733 (type, build_constructor (init_list_type_node,
4734 initializer_list)));
4735 break;
4739 /* It must be a primary-expression. */
4740 postfix_expression
4741 = cp_parser_primary_expression (parser, address_p, cast_p,
4742 /*template_arg_p=*/false,
4743 &idk);
4745 break;
4748 /* Keep looping until the postfix-expression is complete. */
4749 while (true)
4751 if (idk == CP_ID_KIND_UNQUALIFIED
4752 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4753 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4754 /* It is not a Koenig lookup function call. */
4755 postfix_expression
4756 = unqualified_name_lookup_error (postfix_expression);
4758 /* Peek at the next token. */
4759 token = cp_lexer_peek_token (parser->lexer);
4761 switch (token->type)
4763 case CPP_OPEN_SQUARE:
4764 postfix_expression
4765 = cp_parser_postfix_open_square_expression (parser,
4766 postfix_expression,
4767 false);
4768 idk = CP_ID_KIND_NONE;
4769 is_member_access = false;
4770 break;
4772 case CPP_OPEN_PAREN:
4773 /* postfix-expression ( expression-list [opt] ) */
4775 bool koenig_p;
4776 bool is_builtin_constant_p;
4777 bool saved_integral_constant_expression_p = false;
4778 bool saved_non_integral_constant_expression_p = false;
4779 VEC(tree,gc) *args;
4781 is_member_access = false;
4783 is_builtin_constant_p
4784 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4785 if (is_builtin_constant_p)
4787 /* The whole point of __builtin_constant_p is to allow
4788 non-constant expressions to appear as arguments. */
4789 saved_integral_constant_expression_p
4790 = parser->integral_constant_expression_p;
4791 saved_non_integral_constant_expression_p
4792 = parser->non_integral_constant_expression_p;
4793 parser->integral_constant_expression_p = false;
4795 args = (cp_parser_parenthesized_expression_list
4796 (parser, /*is_attribute_list=*/false,
4797 /*cast_p=*/false, /*allow_expansion_p=*/true,
4798 /*non_constant_p=*/NULL));
4799 if (is_builtin_constant_p)
4801 parser->integral_constant_expression_p
4802 = saved_integral_constant_expression_p;
4803 parser->non_integral_constant_expression_p
4804 = saved_non_integral_constant_expression_p;
4807 if (args == NULL)
4809 postfix_expression = error_mark_node;
4810 break;
4813 /* Function calls are not permitted in
4814 constant-expressions. */
4815 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4816 && cp_parser_non_integral_constant_expression (parser,
4817 "a function call"))
4819 postfix_expression = error_mark_node;
4820 release_tree_vector (args);
4821 break;
4824 koenig_p = false;
4825 if (idk == CP_ID_KIND_UNQUALIFIED
4826 || idk == CP_ID_KIND_TEMPLATE_ID)
4828 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4830 if (!VEC_empty (tree, args))
4832 koenig_p = true;
4833 if (!any_type_dependent_arguments_p (args))
4834 postfix_expression
4835 = perform_koenig_lookup (postfix_expression, args);
4837 else
4838 postfix_expression
4839 = unqualified_fn_lookup_error (postfix_expression);
4841 /* We do not perform argument-dependent lookup if
4842 normal lookup finds a non-function, in accordance
4843 with the expected resolution of DR 218. */
4844 else if (!VEC_empty (tree, args)
4845 && is_overloaded_fn (postfix_expression))
4847 tree fn = get_first_fn (postfix_expression);
4848 fn = STRIP_TEMPLATE (fn);
4850 /* Do not do argument dependent lookup if regular
4851 lookup finds a member function or a block-scope
4852 function declaration. [basic.lookup.argdep]/3 */
4853 if (!DECL_FUNCTION_MEMBER_P (fn)
4854 && !DECL_LOCAL_FUNCTION_P (fn))
4856 koenig_p = true;
4857 if (!any_type_dependent_arguments_p (args))
4858 postfix_expression
4859 = perform_koenig_lookup (postfix_expression, args);
4864 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4866 tree instance = TREE_OPERAND (postfix_expression, 0);
4867 tree fn = TREE_OPERAND (postfix_expression, 1);
4869 if (processing_template_decl
4870 && (type_dependent_expression_p (instance)
4871 || (!BASELINK_P (fn)
4872 && TREE_CODE (fn) != FIELD_DECL)
4873 || type_dependent_expression_p (fn)
4874 || any_type_dependent_arguments_p (args)))
4876 postfix_expression
4877 = build_nt_call_vec (postfix_expression, args);
4878 release_tree_vector (args);
4879 break;
4882 if (BASELINK_P (fn))
4884 postfix_expression
4885 = (build_new_method_call
4886 (instance, fn, &args, NULL_TREE,
4887 (idk == CP_ID_KIND_QUALIFIED
4888 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4889 /*fn_p=*/NULL,
4890 tf_warning_or_error));
4892 else
4893 postfix_expression
4894 = finish_call_expr (postfix_expression, &args,
4895 /*disallow_virtual=*/false,
4896 /*koenig_p=*/false,
4897 tf_warning_or_error);
4899 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4900 || TREE_CODE (postfix_expression) == MEMBER_REF
4901 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4902 postfix_expression = (build_offset_ref_call_from_tree
4903 (postfix_expression, &args));
4904 else if (idk == CP_ID_KIND_QUALIFIED)
4905 /* A call to a static class member, or a namespace-scope
4906 function. */
4907 postfix_expression
4908 = finish_call_expr (postfix_expression, &args,
4909 /*disallow_virtual=*/true,
4910 koenig_p,
4911 tf_warning_or_error);
4912 else
4913 /* All other function calls. */
4914 postfix_expression
4915 = finish_call_expr (postfix_expression, &args,
4916 /*disallow_virtual=*/false,
4917 koenig_p,
4918 tf_warning_or_error);
4920 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4921 idk = CP_ID_KIND_NONE;
4923 release_tree_vector (args);
4925 break;
4927 case CPP_DOT:
4928 case CPP_DEREF:
4929 /* postfix-expression . template [opt] id-expression
4930 postfix-expression . pseudo-destructor-name
4931 postfix-expression -> template [opt] id-expression
4932 postfix-expression -> pseudo-destructor-name */
4934 /* Consume the `.' or `->' operator. */
4935 cp_lexer_consume_token (parser->lexer);
4937 postfix_expression
4938 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4939 postfix_expression,
4940 false, &idk,
4941 token->location);
4943 is_member_access = true;
4944 break;
4946 case CPP_PLUS_PLUS:
4947 /* postfix-expression ++ */
4948 /* Consume the `++' token. */
4949 cp_lexer_consume_token (parser->lexer);
4950 /* Generate a representation for the complete expression. */
4951 postfix_expression
4952 = finish_increment_expr (postfix_expression,
4953 POSTINCREMENT_EXPR);
4954 /* Increments may not appear in constant-expressions. */
4955 if (cp_parser_non_integral_constant_expression (parser,
4956 "an increment"))
4957 postfix_expression = error_mark_node;
4958 idk = CP_ID_KIND_NONE;
4959 is_member_access = false;
4960 break;
4962 case CPP_MINUS_MINUS:
4963 /* postfix-expression -- */
4964 /* Consume the `--' token. */
4965 cp_lexer_consume_token (parser->lexer);
4966 /* Generate a representation for the complete expression. */
4967 postfix_expression
4968 = finish_increment_expr (postfix_expression,
4969 POSTDECREMENT_EXPR);
4970 /* Decrements may not appear in constant-expressions. */
4971 if (cp_parser_non_integral_constant_expression (parser,
4972 "a decrement"))
4973 postfix_expression = error_mark_node;
4974 idk = CP_ID_KIND_NONE;
4975 is_member_access = false;
4976 break;
4978 default:
4979 if (pidk_return != NULL)
4980 * pidk_return = idk;
4981 if (member_access_only_p)
4982 return is_member_access? postfix_expression : error_mark_node;
4983 else
4984 return postfix_expression;
4988 /* We should never get here. */
4989 gcc_unreachable ();
4990 return error_mark_node;
4993 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4994 by cp_parser_builtin_offsetof. We're looking for
4996 postfix-expression [ expression ]
4998 FOR_OFFSETOF is set if we're being called in that context, which
4999 changes how we deal with integer constant expressions. */
5001 static tree
5002 cp_parser_postfix_open_square_expression (cp_parser *parser,
5003 tree postfix_expression,
5004 bool for_offsetof)
5006 tree index;
5008 /* Consume the `[' token. */
5009 cp_lexer_consume_token (parser->lexer);
5011 /* Parse the index expression. */
5012 /* ??? For offsetof, there is a question of what to allow here. If
5013 offsetof is not being used in an integral constant expression context,
5014 then we *could* get the right answer by computing the value at runtime.
5015 If we are in an integral constant expression context, then we might
5016 could accept any constant expression; hard to say without analysis.
5017 Rather than open the barn door too wide right away, allow only integer
5018 constant expressions here. */
5019 if (for_offsetof)
5020 index = cp_parser_constant_expression (parser, false, NULL);
5021 else
5022 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5024 /* Look for the closing `]'. */
5025 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5027 /* Build the ARRAY_REF. */
5028 postfix_expression = grok_array_decl (postfix_expression, index);
5030 /* When not doing offsetof, array references are not permitted in
5031 constant-expressions. */
5032 if (!for_offsetof
5033 && (cp_parser_non_integral_constant_expression
5034 (parser, "an array reference")))
5035 postfix_expression = error_mark_node;
5037 return postfix_expression;
5040 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5041 by cp_parser_builtin_offsetof. We're looking for
5043 postfix-expression . template [opt] id-expression
5044 postfix-expression . pseudo-destructor-name
5045 postfix-expression -> template [opt] id-expression
5046 postfix-expression -> pseudo-destructor-name
5048 FOR_OFFSETOF is set if we're being called in that context. That sorta
5049 limits what of the above we'll actually accept, but nevermind.
5050 TOKEN_TYPE is the "." or "->" token, which will already have been
5051 removed from the stream. */
5053 static tree
5054 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5055 enum cpp_ttype token_type,
5056 tree postfix_expression,
5057 bool for_offsetof, cp_id_kind *idk,
5058 location_t location)
5060 tree name;
5061 bool dependent_p;
5062 bool pseudo_destructor_p;
5063 tree scope = NULL_TREE;
5065 /* If this is a `->' operator, dereference the pointer. */
5066 if (token_type == CPP_DEREF)
5067 postfix_expression = build_x_arrow (postfix_expression);
5068 /* Check to see whether or not the expression is type-dependent. */
5069 dependent_p = type_dependent_expression_p (postfix_expression);
5070 /* The identifier following the `->' or `.' is not qualified. */
5071 parser->scope = NULL_TREE;
5072 parser->qualifying_scope = NULL_TREE;
5073 parser->object_scope = NULL_TREE;
5074 *idk = CP_ID_KIND_NONE;
5076 /* Enter the scope corresponding to the type of the object
5077 given by the POSTFIX_EXPRESSION. */
5078 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5080 scope = TREE_TYPE (postfix_expression);
5081 /* According to the standard, no expression should ever have
5082 reference type. Unfortunately, we do not currently match
5083 the standard in this respect in that our internal representation
5084 of an expression may have reference type even when the standard
5085 says it does not. Therefore, we have to manually obtain the
5086 underlying type here. */
5087 scope = non_reference (scope);
5088 /* The type of the POSTFIX_EXPRESSION must be complete. */
5089 if (scope == unknown_type_node)
5091 error_at (location, "%qE does not have class type",
5092 postfix_expression);
5093 scope = NULL_TREE;
5095 else
5096 scope = complete_type_or_else (scope, NULL_TREE);
5097 /* Let the name lookup machinery know that we are processing a
5098 class member access expression. */
5099 parser->context->object_type = scope;
5100 /* If something went wrong, we want to be able to discern that case,
5101 as opposed to the case where there was no SCOPE due to the type
5102 of expression being dependent. */
5103 if (!scope)
5104 scope = error_mark_node;
5105 /* If the SCOPE was erroneous, make the various semantic analysis
5106 functions exit quickly -- and without issuing additional error
5107 messages. */
5108 if (scope == error_mark_node)
5109 postfix_expression = error_mark_node;
5112 /* Assume this expression is not a pseudo-destructor access. */
5113 pseudo_destructor_p = false;
5115 /* If the SCOPE is a scalar type, then, if this is a valid program,
5116 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5117 is type dependent, it can be pseudo-destructor-name or something else.
5118 Try to parse it as pseudo-destructor-name first. */
5119 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5121 tree s;
5122 tree type;
5124 cp_parser_parse_tentatively (parser);
5125 /* Parse the pseudo-destructor-name. */
5126 s = NULL_TREE;
5127 cp_parser_pseudo_destructor_name (parser, &s, &type);
5128 if (dependent_p
5129 && (cp_parser_error_occurred (parser)
5130 || TREE_CODE (type) != TYPE_DECL
5131 || !SCALAR_TYPE_P (TREE_TYPE (type))))
5132 cp_parser_abort_tentative_parse (parser);
5133 else if (cp_parser_parse_definitely (parser))
5135 pseudo_destructor_p = true;
5136 postfix_expression
5137 = finish_pseudo_destructor_expr (postfix_expression,
5138 s, TREE_TYPE (type));
5142 if (!pseudo_destructor_p)
5144 /* If the SCOPE is not a scalar type, we are looking at an
5145 ordinary class member access expression, rather than a
5146 pseudo-destructor-name. */
5147 bool template_p;
5148 cp_token *token = cp_lexer_peek_token (parser->lexer);
5149 /* Parse the id-expression. */
5150 name = (cp_parser_id_expression
5151 (parser,
5152 cp_parser_optional_template_keyword (parser),
5153 /*check_dependency_p=*/true,
5154 &template_p,
5155 /*declarator_p=*/false,
5156 /*optional_p=*/false));
5157 /* In general, build a SCOPE_REF if the member name is qualified.
5158 However, if the name was not dependent and has already been
5159 resolved; there is no need to build the SCOPE_REF. For example;
5161 struct X { void f(); };
5162 template <typename T> void f(T* t) { t->X::f(); }
5164 Even though "t" is dependent, "X::f" is not and has been resolved
5165 to a BASELINK; there is no need to include scope information. */
5167 /* But we do need to remember that there was an explicit scope for
5168 virtual function calls. */
5169 if (parser->scope)
5170 *idk = CP_ID_KIND_QUALIFIED;
5172 /* If the name is a template-id that names a type, we will get a
5173 TYPE_DECL here. That is invalid code. */
5174 if (TREE_CODE (name) == TYPE_DECL)
5176 error_at (token->location, "invalid use of %qD", name);
5177 postfix_expression = error_mark_node;
5179 else
5181 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5183 name = build_qualified_name (/*type=*/NULL_TREE,
5184 parser->scope,
5185 name,
5186 template_p);
5187 parser->scope = NULL_TREE;
5188 parser->qualifying_scope = NULL_TREE;
5189 parser->object_scope = NULL_TREE;
5191 if (scope && name && BASELINK_P (name))
5192 adjust_result_of_qualified_name_lookup
5193 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5194 postfix_expression
5195 = finish_class_member_access_expr (postfix_expression, name,
5196 template_p,
5197 tf_warning_or_error);
5201 /* We no longer need to look up names in the scope of the object on
5202 the left-hand side of the `.' or `->' operator. */
5203 parser->context->object_type = NULL_TREE;
5205 /* Outside of offsetof, these operators may not appear in
5206 constant-expressions. */
5207 if (!for_offsetof
5208 && (cp_parser_non_integral_constant_expression
5209 (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5210 postfix_expression = error_mark_node;
5212 return postfix_expression;
5215 /* Parse a parenthesized expression-list.
5217 expression-list:
5218 assignment-expression
5219 expression-list, assignment-expression
5221 attribute-list:
5222 expression-list
5223 identifier
5224 identifier, expression-list
5226 CAST_P is true if this expression is the target of a cast.
5228 ALLOW_EXPANSION_P is true if this expression allows expansion of an
5229 argument pack.
5231 Returns a vector of trees. Each element is a representation of an
5232 assignment-expression. NULL is returned if the ( and or ) are
5233 missing. An empty, but allocated, vector is returned on no
5234 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is true
5235 if this is really an attribute list being parsed. If
5236 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5237 not all of the expressions in the list were constant. */
5239 static VEC(tree,gc) *
5240 cp_parser_parenthesized_expression_list (cp_parser* parser,
5241 bool is_attribute_list,
5242 bool cast_p,
5243 bool allow_expansion_p,
5244 bool *non_constant_p)
5246 VEC(tree,gc) *expression_list;
5247 bool fold_expr_p = is_attribute_list;
5248 tree identifier = NULL_TREE;
5249 bool saved_greater_than_is_operator_p;
5251 /* Assume all the expressions will be constant. */
5252 if (non_constant_p)
5253 *non_constant_p = false;
5255 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5256 return NULL;
5258 expression_list = make_tree_vector ();
5260 /* Within a parenthesized expression, a `>' token is always
5261 the greater-than operator. */
5262 saved_greater_than_is_operator_p
5263 = parser->greater_than_is_operator_p;
5264 parser->greater_than_is_operator_p = true;
5266 /* Consume expressions until there are no more. */
5267 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5268 while (true)
5270 tree expr;
5272 /* At the beginning of attribute lists, check to see if the
5273 next token is an identifier. */
5274 if (is_attribute_list
5275 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5277 cp_token *token;
5279 /* Consume the identifier. */
5280 token = cp_lexer_consume_token (parser->lexer);
5281 /* Save the identifier. */
5282 identifier = token->u.value;
5284 else
5286 bool expr_non_constant_p;
5288 /* Parse the next assignment-expression. */
5289 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5291 /* A braced-init-list. */
5292 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5293 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5294 if (non_constant_p && expr_non_constant_p)
5295 *non_constant_p = true;
5297 else if (non_constant_p)
5299 expr = (cp_parser_constant_expression
5300 (parser, /*allow_non_constant_p=*/true,
5301 &expr_non_constant_p));
5302 if (expr_non_constant_p)
5303 *non_constant_p = true;
5305 else
5306 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5308 if (fold_expr_p)
5309 expr = fold_non_dependent_expr (expr);
5311 /* If we have an ellipsis, then this is an expression
5312 expansion. */
5313 if (allow_expansion_p
5314 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5316 /* Consume the `...'. */
5317 cp_lexer_consume_token (parser->lexer);
5319 /* Build the argument pack. */
5320 expr = make_pack_expansion (expr);
5323 /* Add it to the list. We add error_mark_node
5324 expressions to the list, so that we can still tell if
5325 the correct form for a parenthesized expression-list
5326 is found. That gives better errors. */
5327 VEC_safe_push (tree, gc, expression_list, expr);
5329 if (expr == error_mark_node)
5330 goto skip_comma;
5333 /* After the first item, attribute lists look the same as
5334 expression lists. */
5335 is_attribute_list = false;
5337 get_comma:;
5338 /* If the next token isn't a `,', then we are done. */
5339 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5340 break;
5342 /* Otherwise, consume the `,' and keep going. */
5343 cp_lexer_consume_token (parser->lexer);
5346 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5348 int ending;
5350 skip_comma:;
5351 /* We try and resync to an unnested comma, as that will give the
5352 user better diagnostics. */
5353 ending = cp_parser_skip_to_closing_parenthesis (parser,
5354 /*recovering=*/true,
5355 /*or_comma=*/true,
5356 /*consume_paren=*/true);
5357 if (ending < 0)
5358 goto get_comma;
5359 if (!ending)
5361 parser->greater_than_is_operator_p
5362 = saved_greater_than_is_operator_p;
5363 return NULL;
5367 parser->greater_than_is_operator_p
5368 = saved_greater_than_is_operator_p;
5370 if (identifier)
5371 VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5373 return expression_list;
5376 /* Parse a pseudo-destructor-name.
5378 pseudo-destructor-name:
5379 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5380 :: [opt] nested-name-specifier template template-id :: ~ type-name
5381 :: [opt] nested-name-specifier [opt] ~ type-name
5383 If either of the first two productions is used, sets *SCOPE to the
5384 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5385 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5386 or ERROR_MARK_NODE if the parse fails. */
5388 static void
5389 cp_parser_pseudo_destructor_name (cp_parser* parser,
5390 tree* scope,
5391 tree* type)
5393 bool nested_name_specifier_p;
5395 /* Assume that things will not work out. */
5396 *type = error_mark_node;
5398 /* Look for the optional `::' operator. */
5399 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5400 /* Look for the optional nested-name-specifier. */
5401 nested_name_specifier_p
5402 = (cp_parser_nested_name_specifier_opt (parser,
5403 /*typename_keyword_p=*/false,
5404 /*check_dependency_p=*/true,
5405 /*type_p=*/false,
5406 /*is_declaration=*/false)
5407 != NULL_TREE);
5408 /* Now, if we saw a nested-name-specifier, we might be doing the
5409 second production. */
5410 if (nested_name_specifier_p
5411 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5413 /* Consume the `template' keyword. */
5414 cp_lexer_consume_token (parser->lexer);
5415 /* Parse the template-id. */
5416 cp_parser_template_id (parser,
5417 /*template_keyword_p=*/true,
5418 /*check_dependency_p=*/false,
5419 /*is_declaration=*/true);
5420 /* Look for the `::' token. */
5421 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5423 /* If the next token is not a `~', then there might be some
5424 additional qualification. */
5425 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5427 /* At this point, we're looking for "type-name :: ~". The type-name
5428 must not be a class-name, since this is a pseudo-destructor. So,
5429 it must be either an enum-name, or a typedef-name -- both of which
5430 are just identifiers. So, we peek ahead to check that the "::"
5431 and "~" tokens are present; if they are not, then we can avoid
5432 calling type_name. */
5433 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5434 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5435 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5437 cp_parser_error (parser, "non-scalar type");
5438 return;
5441 /* Look for the type-name. */
5442 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5443 if (*scope == error_mark_node)
5444 return;
5446 /* Look for the `::' token. */
5447 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5449 else
5450 *scope = NULL_TREE;
5452 /* Look for the `~'. */
5453 cp_parser_require (parser, CPP_COMPL, "%<~%>");
5454 /* Look for the type-name again. We are not responsible for
5455 checking that it matches the first type-name. */
5456 *type = cp_parser_nonclass_name (parser);
5459 /* Parse a unary-expression.
5461 unary-expression:
5462 postfix-expression
5463 ++ cast-expression
5464 -- cast-expression
5465 unary-operator cast-expression
5466 sizeof unary-expression
5467 sizeof ( type-id )
5468 new-expression
5469 delete-expression
5471 GNU Extensions:
5473 unary-expression:
5474 __extension__ cast-expression
5475 __alignof__ unary-expression
5476 __alignof__ ( type-id )
5477 __real__ cast-expression
5478 __imag__ cast-expression
5479 && identifier
5481 ADDRESS_P is true iff the unary-expression is appearing as the
5482 operand of the `&' operator. CAST_P is true if this expression is
5483 the target of a cast.
5485 Returns a representation of the expression. */
5487 static tree
5488 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5489 cp_id_kind * pidk)
5491 cp_token *token;
5492 enum tree_code unary_operator;
5494 /* Peek at the next token. */
5495 token = cp_lexer_peek_token (parser->lexer);
5496 /* Some keywords give away the kind of expression. */
5497 if (token->type == CPP_KEYWORD)
5499 enum rid keyword = token->keyword;
5501 switch (keyword)
5503 case RID_ALIGNOF:
5504 case RID_SIZEOF:
5506 tree operand;
5507 enum tree_code op;
5509 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5510 /* Consume the token. */
5511 cp_lexer_consume_token (parser->lexer);
5512 /* Parse the operand. */
5513 operand = cp_parser_sizeof_operand (parser, keyword);
5515 if (TYPE_P (operand))
5516 return cxx_sizeof_or_alignof_type (operand, op, true);
5517 else
5518 return cxx_sizeof_or_alignof_expr (operand, op, true);
5521 case RID_NEW:
5522 return cp_parser_new_expression (parser);
5524 case RID_DELETE:
5525 return cp_parser_delete_expression (parser);
5527 case RID_EXTENSION:
5529 /* The saved value of the PEDANTIC flag. */
5530 int saved_pedantic;
5531 tree expr;
5533 /* Save away the PEDANTIC flag. */
5534 cp_parser_extension_opt (parser, &saved_pedantic);
5535 /* Parse the cast-expression. */
5536 expr = cp_parser_simple_cast_expression (parser);
5537 /* Restore the PEDANTIC flag. */
5538 pedantic = saved_pedantic;
5540 return expr;
5543 case RID_REALPART:
5544 case RID_IMAGPART:
5546 tree expression;
5548 /* Consume the `__real__' or `__imag__' token. */
5549 cp_lexer_consume_token (parser->lexer);
5550 /* Parse the cast-expression. */
5551 expression = cp_parser_simple_cast_expression (parser);
5552 /* Create the complete representation. */
5553 return build_x_unary_op ((keyword == RID_REALPART
5554 ? REALPART_EXPR : IMAGPART_EXPR),
5555 expression,
5556 tf_warning_or_error);
5558 break;
5560 default:
5561 break;
5565 /* Look for the `:: new' and `:: delete', which also signal the
5566 beginning of a new-expression, or delete-expression,
5567 respectively. If the next token is `::', then it might be one of
5568 these. */
5569 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5571 enum rid keyword;
5573 /* See if the token after the `::' is one of the keywords in
5574 which we're interested. */
5575 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5576 /* If it's `new', we have a new-expression. */
5577 if (keyword == RID_NEW)
5578 return cp_parser_new_expression (parser);
5579 /* Similarly, for `delete'. */
5580 else if (keyword == RID_DELETE)
5581 return cp_parser_delete_expression (parser);
5584 /* Look for a unary operator. */
5585 unary_operator = cp_parser_unary_operator (token);
5586 /* The `++' and `--' operators can be handled similarly, even though
5587 they are not technically unary-operators in the grammar. */
5588 if (unary_operator == ERROR_MARK)
5590 if (token->type == CPP_PLUS_PLUS)
5591 unary_operator = PREINCREMENT_EXPR;
5592 else if (token->type == CPP_MINUS_MINUS)
5593 unary_operator = PREDECREMENT_EXPR;
5594 /* Handle the GNU address-of-label extension. */
5595 else if (cp_parser_allow_gnu_extensions_p (parser)
5596 && token->type == CPP_AND_AND)
5598 tree identifier;
5599 tree expression;
5600 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5602 /* Consume the '&&' token. */
5603 cp_lexer_consume_token (parser->lexer);
5604 /* Look for the identifier. */
5605 identifier = cp_parser_identifier (parser);
5606 /* Create an expression representing the address. */
5607 expression = finish_label_address_expr (identifier, loc);
5608 if (cp_parser_non_integral_constant_expression (parser,
5609 "the address of a label"))
5610 expression = error_mark_node;
5611 return expression;
5614 if (unary_operator != ERROR_MARK)
5616 tree cast_expression;
5617 tree expression = error_mark_node;
5618 const char *non_constant_p = NULL;
5620 /* Consume the operator token. */
5621 token = cp_lexer_consume_token (parser->lexer);
5622 /* Parse the cast-expression. */
5623 cast_expression
5624 = cp_parser_cast_expression (parser,
5625 unary_operator == ADDR_EXPR,
5626 /*cast_p=*/false, pidk);
5627 /* Now, build an appropriate representation. */
5628 switch (unary_operator)
5630 case INDIRECT_REF:
5631 non_constant_p = "%<*%>";
5632 expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR,
5633 tf_warning_or_error);
5634 break;
5636 case ADDR_EXPR:
5637 non_constant_p = "%<&%>";
5638 /* Fall through. */
5639 case BIT_NOT_EXPR:
5640 expression = build_x_unary_op (unary_operator, cast_expression,
5641 tf_warning_or_error);
5642 break;
5644 case PREINCREMENT_EXPR:
5645 case PREDECREMENT_EXPR:
5646 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5647 ? "%<++%>" : "%<--%>");
5648 /* Fall through. */
5649 case UNARY_PLUS_EXPR:
5650 case NEGATE_EXPR:
5651 case TRUTH_NOT_EXPR:
5652 expression = finish_unary_op_expr (unary_operator, cast_expression);
5653 break;
5655 default:
5656 gcc_unreachable ();
5659 if (non_constant_p
5660 && cp_parser_non_integral_constant_expression (parser,
5661 non_constant_p))
5662 expression = error_mark_node;
5664 return expression;
5667 return cp_parser_postfix_expression (parser, address_p, cast_p,
5668 /*member_access_only_p=*/false,
5669 pidk);
5672 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5673 unary-operator, the corresponding tree code is returned. */
5675 static enum tree_code
5676 cp_parser_unary_operator (cp_token* token)
5678 switch (token->type)
5680 case CPP_MULT:
5681 return INDIRECT_REF;
5683 case CPP_AND:
5684 return ADDR_EXPR;
5686 case CPP_PLUS:
5687 return UNARY_PLUS_EXPR;
5689 case CPP_MINUS:
5690 return NEGATE_EXPR;
5692 case CPP_NOT:
5693 return TRUTH_NOT_EXPR;
5695 case CPP_COMPL:
5696 return BIT_NOT_EXPR;
5698 default:
5699 return ERROR_MARK;
5703 /* Parse a new-expression.
5705 new-expression:
5706 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5707 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5709 Returns a representation of the expression. */
5711 static tree
5712 cp_parser_new_expression (cp_parser* parser)
5714 bool global_scope_p;
5715 VEC(tree,gc) *placement;
5716 tree type;
5717 VEC(tree,gc) *initializer;
5718 tree nelts;
5719 tree ret;
5721 /* Look for the optional `::' operator. */
5722 global_scope_p
5723 = (cp_parser_global_scope_opt (parser,
5724 /*current_scope_valid_p=*/false)
5725 != NULL_TREE);
5726 /* Look for the `new' operator. */
5727 cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5728 /* There's no easy way to tell a new-placement from the
5729 `( type-id )' construct. */
5730 cp_parser_parse_tentatively (parser);
5731 /* Look for a new-placement. */
5732 placement = cp_parser_new_placement (parser);
5733 /* If that didn't work out, there's no new-placement. */
5734 if (!cp_parser_parse_definitely (parser))
5736 if (placement != NULL)
5737 release_tree_vector (placement);
5738 placement = NULL;
5741 /* If the next token is a `(', then we have a parenthesized
5742 type-id. */
5743 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5745 cp_token *token;
5746 /* Consume the `('. */
5747 cp_lexer_consume_token (parser->lexer);
5748 /* Parse the type-id. */
5749 type = cp_parser_type_id (parser);
5750 /* Look for the closing `)'. */
5751 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5752 token = cp_lexer_peek_token (parser->lexer);
5753 /* There should not be a direct-new-declarator in this production,
5754 but GCC used to allowed this, so we check and emit a sensible error
5755 message for this case. */
5756 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5758 error_at (token->location,
5759 "array bound forbidden after parenthesized type-id");
5760 inform (token->location,
5761 "try removing the parentheses around the type-id");
5762 cp_parser_direct_new_declarator (parser);
5764 nelts = NULL_TREE;
5766 /* Otherwise, there must be a new-type-id. */
5767 else
5768 type = cp_parser_new_type_id (parser, &nelts);
5770 /* If the next token is a `(' or '{', then we have a new-initializer. */
5771 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5772 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5773 initializer = cp_parser_new_initializer (parser);
5774 else
5775 initializer = NULL;
5777 /* A new-expression may not appear in an integral constant
5778 expression. */
5779 if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5780 ret = error_mark_node;
5781 else
5783 /* Create a representation of the new-expression. */
5784 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
5785 tf_warning_or_error);
5788 if (placement != NULL)
5789 release_tree_vector (placement);
5790 if (initializer != NULL)
5791 release_tree_vector (initializer);
5793 return ret;
5796 /* Parse a new-placement.
5798 new-placement:
5799 ( expression-list )
5801 Returns the same representation as for an expression-list. */
5803 static VEC(tree,gc) *
5804 cp_parser_new_placement (cp_parser* parser)
5806 VEC(tree,gc) *expression_list;
5808 /* Parse the expression-list. */
5809 expression_list = (cp_parser_parenthesized_expression_list
5810 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5811 /*non_constant_p=*/NULL));
5813 return expression_list;
5816 /* Parse a new-type-id.
5818 new-type-id:
5819 type-specifier-seq new-declarator [opt]
5821 Returns the TYPE allocated. If the new-type-id indicates an array
5822 type, *NELTS is set to the number of elements in the last array
5823 bound; the TYPE will not include the last array bound. */
5825 static tree
5826 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5828 cp_decl_specifier_seq type_specifier_seq;
5829 cp_declarator *new_declarator;
5830 cp_declarator *declarator;
5831 cp_declarator *outer_declarator;
5832 const char *saved_message;
5833 tree type;
5835 /* The type-specifier sequence must not contain type definitions.
5836 (It cannot contain declarations of new types either, but if they
5837 are not definitions we will catch that because they are not
5838 complete.) */
5839 saved_message = parser->type_definition_forbidden_message;
5840 parser->type_definition_forbidden_message
5841 = G_("types may not be defined in a new-type-id");
5842 /* Parse the type-specifier-seq. */
5843 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
5844 /*is_trailing_return=*/false,
5845 &type_specifier_seq);
5846 /* Restore the old message. */
5847 parser->type_definition_forbidden_message = saved_message;
5848 /* Parse the new-declarator. */
5849 new_declarator = cp_parser_new_declarator_opt (parser);
5851 /* Determine the number of elements in the last array dimension, if
5852 any. */
5853 *nelts = NULL_TREE;
5854 /* Skip down to the last array dimension. */
5855 declarator = new_declarator;
5856 outer_declarator = NULL;
5857 while (declarator && (declarator->kind == cdk_pointer
5858 || declarator->kind == cdk_ptrmem))
5860 outer_declarator = declarator;
5861 declarator = declarator->declarator;
5863 while (declarator
5864 && declarator->kind == cdk_array
5865 && declarator->declarator
5866 && declarator->declarator->kind == cdk_array)
5868 outer_declarator = declarator;
5869 declarator = declarator->declarator;
5872 if (declarator && declarator->kind == cdk_array)
5874 *nelts = declarator->u.array.bounds;
5875 if (*nelts == error_mark_node)
5876 *nelts = integer_one_node;
5878 if (outer_declarator)
5879 outer_declarator->declarator = declarator->declarator;
5880 else
5881 new_declarator = NULL;
5884 type = groktypename (&type_specifier_seq, new_declarator, false);
5885 return type;
5888 /* Parse an (optional) new-declarator.
5890 new-declarator:
5891 ptr-operator new-declarator [opt]
5892 direct-new-declarator
5894 Returns the declarator. */
5896 static cp_declarator *
5897 cp_parser_new_declarator_opt (cp_parser* parser)
5899 enum tree_code code;
5900 tree type;
5901 cp_cv_quals cv_quals;
5903 /* We don't know if there's a ptr-operator next, or not. */
5904 cp_parser_parse_tentatively (parser);
5905 /* Look for a ptr-operator. */
5906 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5907 /* If that worked, look for more new-declarators. */
5908 if (cp_parser_parse_definitely (parser))
5910 cp_declarator *declarator;
5912 /* Parse another optional declarator. */
5913 declarator = cp_parser_new_declarator_opt (parser);
5915 return cp_parser_make_indirect_declarator
5916 (code, type, cv_quals, declarator);
5919 /* If the next token is a `[', there is a direct-new-declarator. */
5920 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5921 return cp_parser_direct_new_declarator (parser);
5923 return NULL;
5926 /* Parse a direct-new-declarator.
5928 direct-new-declarator:
5929 [ expression ]
5930 direct-new-declarator [constant-expression]
5934 static cp_declarator *
5935 cp_parser_direct_new_declarator (cp_parser* parser)
5937 cp_declarator *declarator = NULL;
5939 while (true)
5941 tree expression;
5943 /* Look for the opening `['. */
5944 cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5945 /* The first expression is not required to be constant. */
5946 if (!declarator)
5948 cp_token *token = cp_lexer_peek_token (parser->lexer);
5949 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5950 /* The standard requires that the expression have integral
5951 type. DR 74 adds enumeration types. We believe that the
5952 real intent is that these expressions be handled like the
5953 expression in a `switch' condition, which also allows
5954 classes with a single conversion to integral or
5955 enumeration type. */
5956 if (!processing_template_decl)
5958 expression
5959 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5960 expression,
5961 /*complain=*/true);
5962 if (!expression)
5964 error_at (token->location,
5965 "expression in new-declarator must have integral "
5966 "or enumeration type");
5967 expression = error_mark_node;
5971 /* But all the other expressions must be. */
5972 else
5973 expression
5974 = cp_parser_constant_expression (parser,
5975 /*allow_non_constant=*/false,
5976 NULL);
5977 /* Look for the closing `]'. */
5978 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5980 /* Add this bound to the declarator. */
5981 declarator = make_array_declarator (declarator, expression);
5983 /* If the next token is not a `[', then there are no more
5984 bounds. */
5985 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5986 break;
5989 return declarator;
5992 /* Parse a new-initializer.
5994 new-initializer:
5995 ( expression-list [opt] )
5996 braced-init-list
5998 Returns a representation of the expression-list. */
6000 static VEC(tree,gc) *
6001 cp_parser_new_initializer (cp_parser* parser)
6003 VEC(tree,gc) *expression_list;
6005 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6007 tree t;
6008 bool expr_non_constant_p;
6009 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6010 t = cp_parser_braced_list (parser, &expr_non_constant_p);
6011 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6012 expression_list = make_tree_vector_single (t);
6014 else
6015 expression_list = (cp_parser_parenthesized_expression_list
6016 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
6017 /*non_constant_p=*/NULL));
6019 return expression_list;
6022 /* Parse a delete-expression.
6024 delete-expression:
6025 :: [opt] delete cast-expression
6026 :: [opt] delete [ ] cast-expression
6028 Returns a representation of the expression. */
6030 static tree
6031 cp_parser_delete_expression (cp_parser* parser)
6033 bool global_scope_p;
6034 bool array_p;
6035 tree expression;
6037 /* Look for the optional `::' operator. */
6038 global_scope_p
6039 = (cp_parser_global_scope_opt (parser,
6040 /*current_scope_valid_p=*/false)
6041 != NULL_TREE);
6042 /* Look for the `delete' keyword. */
6043 cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
6044 /* See if the array syntax is in use. */
6045 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6047 /* Consume the `[' token. */
6048 cp_lexer_consume_token (parser->lexer);
6049 /* Look for the `]' token. */
6050 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
6051 /* Remember that this is the `[]' construct. */
6052 array_p = true;
6054 else
6055 array_p = false;
6057 /* Parse the cast-expression. */
6058 expression = cp_parser_simple_cast_expression (parser);
6060 /* A delete-expression may not appear in an integral constant
6061 expression. */
6062 if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
6063 return error_mark_node;
6065 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
6068 /* Returns true if TOKEN may start a cast-expression and false
6069 otherwise. */
6071 static bool
6072 cp_parser_token_starts_cast_expression (cp_token *token)
6074 switch (token->type)
6076 case CPP_COMMA:
6077 case CPP_SEMICOLON:
6078 case CPP_QUERY:
6079 case CPP_COLON:
6080 case CPP_CLOSE_SQUARE:
6081 case CPP_CLOSE_PAREN:
6082 case CPP_CLOSE_BRACE:
6083 case CPP_DOT:
6084 case CPP_DOT_STAR:
6085 case CPP_DEREF:
6086 case CPP_DEREF_STAR:
6087 case CPP_DIV:
6088 case CPP_MOD:
6089 case CPP_LSHIFT:
6090 case CPP_RSHIFT:
6091 case CPP_LESS:
6092 case CPP_GREATER:
6093 case CPP_LESS_EQ:
6094 case CPP_GREATER_EQ:
6095 case CPP_EQ_EQ:
6096 case CPP_NOT_EQ:
6097 case CPP_EQ:
6098 case CPP_MULT_EQ:
6099 case CPP_DIV_EQ:
6100 case CPP_MOD_EQ:
6101 case CPP_PLUS_EQ:
6102 case CPP_MINUS_EQ:
6103 case CPP_RSHIFT_EQ:
6104 case CPP_LSHIFT_EQ:
6105 case CPP_AND_EQ:
6106 case CPP_XOR_EQ:
6107 case CPP_OR_EQ:
6108 case CPP_XOR:
6109 case CPP_OR:
6110 case CPP_OR_OR:
6111 case CPP_EOF:
6112 return false;
6114 /* '[' may start a primary-expression in obj-c++. */
6115 case CPP_OPEN_SQUARE:
6116 return c_dialect_objc ();
6118 default:
6119 return true;
6123 /* Parse a cast-expression.
6125 cast-expression:
6126 unary-expression
6127 ( type-id ) cast-expression
6129 ADDRESS_P is true iff the unary-expression is appearing as the
6130 operand of the `&' operator. CAST_P is true if this expression is
6131 the target of a cast.
6133 Returns a representation of the expression. */
6135 static tree
6136 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6137 cp_id_kind * pidk)
6139 /* If it's a `(', then we might be looking at a cast. */
6140 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6142 tree type = NULL_TREE;
6143 tree expr = NULL_TREE;
6144 bool compound_literal_p;
6145 const char *saved_message;
6147 /* There's no way to know yet whether or not this is a cast.
6148 For example, `(int (3))' is a unary-expression, while `(int)
6149 3' is a cast. So, we resort to parsing tentatively. */
6150 cp_parser_parse_tentatively (parser);
6151 /* Types may not be defined in a cast. */
6152 saved_message = parser->type_definition_forbidden_message;
6153 parser->type_definition_forbidden_message
6154 = G_("types may not be defined in casts");
6155 /* Consume the `('. */
6156 cp_lexer_consume_token (parser->lexer);
6157 /* A very tricky bit is that `(struct S) { 3 }' is a
6158 compound-literal (which we permit in C++ as an extension).
6159 But, that construct is not a cast-expression -- it is a
6160 postfix-expression. (The reason is that `(struct S) { 3 }.i'
6161 is legal; if the compound-literal were a cast-expression,
6162 you'd need an extra set of parentheses.) But, if we parse
6163 the type-id, and it happens to be a class-specifier, then we
6164 will commit to the parse at that point, because we cannot
6165 undo the action that is done when creating a new class. So,
6166 then we cannot back up and do a postfix-expression.
6168 Therefore, we scan ahead to the closing `)', and check to see
6169 if the token after the `)' is a `{'. If so, we are not
6170 looking at a cast-expression.
6172 Save tokens so that we can put them back. */
6173 cp_lexer_save_tokens (parser->lexer);
6174 /* Skip tokens until the next token is a closing parenthesis.
6175 If we find the closing `)', and the next token is a `{', then
6176 we are looking at a compound-literal. */
6177 compound_literal_p
6178 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6179 /*consume_paren=*/true)
6180 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6181 /* Roll back the tokens we skipped. */
6182 cp_lexer_rollback_tokens (parser->lexer);
6183 /* If we were looking at a compound-literal, simulate an error
6184 so that the call to cp_parser_parse_definitely below will
6185 fail. */
6186 if (compound_literal_p)
6187 cp_parser_simulate_error (parser);
6188 else
6190 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6191 parser->in_type_id_in_expr_p = true;
6192 /* Look for the type-id. */
6193 type = cp_parser_type_id (parser);
6194 /* Look for the closing `)'. */
6195 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6196 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6199 /* Restore the saved message. */
6200 parser->type_definition_forbidden_message = saved_message;
6202 /* At this point this can only be either a cast or a
6203 parenthesized ctor such as `(T ())' that looks like a cast to
6204 function returning T. */
6205 if (!cp_parser_error_occurred (parser)
6206 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6207 (parser->lexer)))
6209 cp_parser_parse_definitely (parser);
6210 expr = cp_parser_cast_expression (parser,
6211 /*address_p=*/false,
6212 /*cast_p=*/true, pidk);
6214 /* Warn about old-style casts, if so requested. */
6215 if (warn_old_style_cast
6216 && !in_system_header
6217 && !VOID_TYPE_P (type)
6218 && current_lang_name != lang_name_c)
6219 warning (OPT_Wold_style_cast, "use of old-style cast");
6221 /* Only type conversions to integral or enumeration types
6222 can be used in constant-expressions. */
6223 if (!cast_valid_in_integral_constant_expression_p (type)
6224 && (cp_parser_non_integral_constant_expression
6225 (parser,
6226 "a cast to a type other than an integral or "
6227 "enumeration type")))
6228 return error_mark_node;
6230 /* Perform the cast. */
6231 expr = build_c_cast (input_location, type, expr);
6232 return expr;
6234 else
6235 cp_parser_abort_tentative_parse (parser);
6238 /* If we get here, then it's not a cast, so it must be a
6239 unary-expression. */
6240 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6243 /* Parse a binary expression of the general form:
6245 pm-expression:
6246 cast-expression
6247 pm-expression .* cast-expression
6248 pm-expression ->* cast-expression
6250 multiplicative-expression:
6251 pm-expression
6252 multiplicative-expression * pm-expression
6253 multiplicative-expression / pm-expression
6254 multiplicative-expression % pm-expression
6256 additive-expression:
6257 multiplicative-expression
6258 additive-expression + multiplicative-expression
6259 additive-expression - multiplicative-expression
6261 shift-expression:
6262 additive-expression
6263 shift-expression << additive-expression
6264 shift-expression >> additive-expression
6266 relational-expression:
6267 shift-expression
6268 relational-expression < shift-expression
6269 relational-expression > shift-expression
6270 relational-expression <= shift-expression
6271 relational-expression >= shift-expression
6273 GNU Extension:
6275 relational-expression:
6276 relational-expression <? shift-expression
6277 relational-expression >? shift-expression
6279 equality-expression:
6280 relational-expression
6281 equality-expression == relational-expression
6282 equality-expression != relational-expression
6284 and-expression:
6285 equality-expression
6286 and-expression & equality-expression
6288 exclusive-or-expression:
6289 and-expression
6290 exclusive-or-expression ^ and-expression
6292 inclusive-or-expression:
6293 exclusive-or-expression
6294 inclusive-or-expression | exclusive-or-expression
6296 logical-and-expression:
6297 inclusive-or-expression
6298 logical-and-expression && inclusive-or-expression
6300 logical-or-expression:
6301 logical-and-expression
6302 logical-or-expression || logical-and-expression
6304 All these are implemented with a single function like:
6306 binary-expression:
6307 simple-cast-expression
6308 binary-expression <token> binary-expression
6310 CAST_P is true if this expression is the target of a cast.
6312 The binops_by_token map is used to get the tree codes for each <token> type.
6313 binary-expressions are associated according to a precedence table. */
6315 #define TOKEN_PRECEDENCE(token) \
6316 (((token->type == CPP_GREATER \
6317 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6318 && !parser->greater_than_is_operator_p) \
6319 ? PREC_NOT_OPERATOR \
6320 : binops_by_token[token->type].prec)
6322 static tree
6323 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6324 bool no_toplevel_fold_p,
6325 enum cp_parser_prec prec,
6326 cp_id_kind * pidk)
6328 cp_parser_expression_stack stack;
6329 cp_parser_expression_stack_entry *sp = &stack[0];
6330 tree lhs, rhs;
6331 cp_token *token;
6332 enum tree_code tree_type, lhs_type, rhs_type;
6333 enum cp_parser_prec new_prec, lookahead_prec;
6334 bool overloaded_p;
6336 /* Parse the first expression. */
6337 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6338 lhs_type = ERROR_MARK;
6340 for (;;)
6342 /* Get an operator token. */
6343 token = cp_lexer_peek_token (parser->lexer);
6345 if (warn_cxx0x_compat
6346 && token->type == CPP_RSHIFT
6347 && !parser->greater_than_is_operator_p)
6349 if (warning_at (token->location, OPT_Wc__0x_compat,
6350 "%<>>%> operator will be treated as"
6351 " two right angle brackets in C++0x"))
6352 inform (token->location,
6353 "suggest parentheses around %<>>%> expression");
6356 new_prec = TOKEN_PRECEDENCE (token);
6358 /* Popping an entry off the stack means we completed a subexpression:
6359 - either we found a token which is not an operator (`>' where it is not
6360 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6361 will happen repeatedly;
6362 - or, we found an operator which has lower priority. This is the case
6363 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6364 parsing `3 * 4'. */
6365 if (new_prec <= prec)
6367 if (sp == stack)
6368 break;
6369 else
6370 goto pop;
6373 get_rhs:
6374 tree_type = binops_by_token[token->type].tree_type;
6376 /* We used the operator token. */
6377 cp_lexer_consume_token (parser->lexer);
6379 /* For "false && x" or "true || x", x will never be executed;
6380 disable warnings while evaluating it. */
6381 if (tree_type == TRUTH_ANDIF_EXPR)
6382 c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6383 else if (tree_type == TRUTH_ORIF_EXPR)
6384 c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6386 /* Extract another operand. It may be the RHS of this expression
6387 or the LHS of a new, higher priority expression. */
6388 rhs = cp_parser_simple_cast_expression (parser);
6389 rhs_type = ERROR_MARK;
6391 /* Get another operator token. Look up its precedence to avoid
6392 building a useless (immediately popped) stack entry for common
6393 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6394 token = cp_lexer_peek_token (parser->lexer);
6395 lookahead_prec = TOKEN_PRECEDENCE (token);
6396 if (lookahead_prec > new_prec)
6398 /* ... and prepare to parse the RHS of the new, higher priority
6399 expression. Since precedence levels on the stack are
6400 monotonically increasing, we do not have to care about
6401 stack overflows. */
6402 sp->prec = prec;
6403 sp->tree_type = tree_type;
6404 sp->lhs = lhs;
6405 sp->lhs_type = lhs_type;
6406 sp++;
6407 lhs = rhs;
6408 lhs_type = rhs_type;
6409 prec = new_prec;
6410 new_prec = lookahead_prec;
6411 goto get_rhs;
6413 pop:
6414 lookahead_prec = new_prec;
6415 /* If the stack is not empty, we have parsed into LHS the right side
6416 (`4' in the example above) of an expression we had suspended.
6417 We can use the information on the stack to recover the LHS (`3')
6418 from the stack together with the tree code (`MULT_EXPR'), and
6419 the precedence of the higher level subexpression
6420 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6421 which will be used to actually build the additive expression. */
6422 --sp;
6423 prec = sp->prec;
6424 tree_type = sp->tree_type;
6425 rhs = lhs;
6426 rhs_type = lhs_type;
6427 lhs = sp->lhs;
6428 lhs_type = sp->lhs_type;
6431 /* Undo the disabling of warnings done above. */
6432 if (tree_type == TRUTH_ANDIF_EXPR)
6433 c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6434 else if (tree_type == TRUTH_ORIF_EXPR)
6435 c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6437 overloaded_p = false;
6438 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6439 ERROR_MARK for everything that is not a binary expression.
6440 This makes warn_about_parentheses miss some warnings that
6441 involve unary operators. For unary expressions we should
6442 pass the correct tree_code unless the unary expression was
6443 surrounded by parentheses.
6445 if (no_toplevel_fold_p
6446 && lookahead_prec <= prec
6447 && sp == stack
6448 && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6449 lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6450 else
6451 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6452 &overloaded_p, tf_warning_or_error);
6453 lhs_type = tree_type;
6455 /* If the binary operator required the use of an overloaded operator,
6456 then this expression cannot be an integral constant-expression.
6457 An overloaded operator can be used even if both operands are
6458 otherwise permissible in an integral constant-expression if at
6459 least one of the operands is of enumeration type. */
6461 if (overloaded_p
6462 && (cp_parser_non_integral_constant_expression
6463 (parser, "calls to overloaded operators")))
6464 return error_mark_node;
6467 return lhs;
6471 /* Parse the `? expression : assignment-expression' part of a
6472 conditional-expression. The LOGICAL_OR_EXPR is the
6473 logical-or-expression that started the conditional-expression.
6474 Returns a representation of the entire conditional-expression.
6476 This routine is used by cp_parser_assignment_expression.
6478 ? expression : assignment-expression
6480 GNU Extensions:
6482 ? : assignment-expression */
6484 static tree
6485 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6487 tree expr;
6488 tree assignment_expr;
6490 /* Consume the `?' token. */
6491 cp_lexer_consume_token (parser->lexer);
6492 if (cp_parser_allow_gnu_extensions_p (parser)
6493 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6495 /* Implicit true clause. */
6496 expr = NULL_TREE;
6497 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6499 else
6501 /* Parse the expression. */
6502 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6503 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6504 c_inhibit_evaluation_warnings +=
6505 ((logical_or_expr == truthvalue_true_node)
6506 - (logical_or_expr == truthvalue_false_node));
6509 /* The next token should be a `:'. */
6510 cp_parser_require (parser, CPP_COLON, "%<:%>");
6511 /* Parse the assignment-expression. */
6512 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6513 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6515 /* Build the conditional-expression. */
6516 return build_x_conditional_expr (logical_or_expr,
6517 expr,
6518 assignment_expr,
6519 tf_warning_or_error);
6522 /* Parse an assignment-expression.
6524 assignment-expression:
6525 conditional-expression
6526 logical-or-expression assignment-operator assignment_expression
6527 throw-expression
6529 CAST_P is true if this expression is the target of a cast.
6531 Returns a representation for the expression. */
6533 static tree
6534 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6535 cp_id_kind * pidk)
6537 tree expr;
6539 /* If the next token is the `throw' keyword, then we're looking at
6540 a throw-expression. */
6541 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6542 expr = cp_parser_throw_expression (parser);
6543 /* Otherwise, it must be that we are looking at a
6544 logical-or-expression. */
6545 else
6547 /* Parse the binary expressions (logical-or-expression). */
6548 expr = cp_parser_binary_expression (parser, cast_p, false,
6549 PREC_NOT_OPERATOR, pidk);
6550 /* If the next token is a `?' then we're actually looking at a
6551 conditional-expression. */
6552 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6553 return cp_parser_question_colon_clause (parser, expr);
6554 else
6556 enum tree_code assignment_operator;
6558 /* If it's an assignment-operator, we're using the second
6559 production. */
6560 assignment_operator
6561 = cp_parser_assignment_operator_opt (parser);
6562 if (assignment_operator != ERROR_MARK)
6564 bool non_constant_p;
6566 /* Parse the right-hand side of the assignment. */
6567 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6569 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6570 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6572 /* An assignment may not appear in a
6573 constant-expression. */
6574 if (cp_parser_non_integral_constant_expression (parser,
6575 "an assignment"))
6576 return error_mark_node;
6577 /* Build the assignment expression. */
6578 expr = build_x_modify_expr (expr,
6579 assignment_operator,
6580 rhs,
6581 tf_warning_or_error);
6586 return expr;
6589 /* Parse an (optional) assignment-operator.
6591 assignment-operator: one of
6592 = *= /= %= += -= >>= <<= &= ^= |=
6594 GNU Extension:
6596 assignment-operator: one of
6597 <?= >?=
6599 If the next token is an assignment operator, the corresponding tree
6600 code is returned, and the token is consumed. For example, for
6601 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6602 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6603 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6604 operator, ERROR_MARK is returned. */
6606 static enum tree_code
6607 cp_parser_assignment_operator_opt (cp_parser* parser)
6609 enum tree_code op;
6610 cp_token *token;
6612 /* Peek at the next token. */
6613 token = cp_lexer_peek_token (parser->lexer);
6615 switch (token->type)
6617 case CPP_EQ:
6618 op = NOP_EXPR;
6619 break;
6621 case CPP_MULT_EQ:
6622 op = MULT_EXPR;
6623 break;
6625 case CPP_DIV_EQ:
6626 op = TRUNC_DIV_EXPR;
6627 break;
6629 case CPP_MOD_EQ:
6630 op = TRUNC_MOD_EXPR;
6631 break;
6633 case CPP_PLUS_EQ:
6634 op = PLUS_EXPR;
6635 break;
6637 case CPP_MINUS_EQ:
6638 op = MINUS_EXPR;
6639 break;
6641 case CPP_RSHIFT_EQ:
6642 op = RSHIFT_EXPR;
6643 break;
6645 case CPP_LSHIFT_EQ:
6646 op = LSHIFT_EXPR;
6647 break;
6649 case CPP_AND_EQ:
6650 op = BIT_AND_EXPR;
6651 break;
6653 case CPP_XOR_EQ:
6654 op = BIT_XOR_EXPR;
6655 break;
6657 case CPP_OR_EQ:
6658 op = BIT_IOR_EXPR;
6659 break;
6661 default:
6662 /* Nothing else is an assignment operator. */
6663 op = ERROR_MARK;
6666 /* If it was an assignment operator, consume it. */
6667 if (op != ERROR_MARK)
6668 cp_lexer_consume_token (parser->lexer);
6670 return op;
6673 /* Parse an expression.
6675 expression:
6676 assignment-expression
6677 expression , assignment-expression
6679 CAST_P is true if this expression is the target of a cast.
6681 Returns a representation of the expression. */
6683 static tree
6684 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6686 tree expression = NULL_TREE;
6688 while (true)
6690 tree assignment_expression;
6692 /* Parse the next assignment-expression. */
6693 assignment_expression
6694 = cp_parser_assignment_expression (parser, cast_p, pidk);
6695 /* If this is the first assignment-expression, we can just
6696 save it away. */
6697 if (!expression)
6698 expression = assignment_expression;
6699 else
6700 expression = build_x_compound_expr (expression,
6701 assignment_expression,
6702 tf_warning_or_error);
6703 /* If the next token is not a comma, then we are done with the
6704 expression. */
6705 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6706 break;
6707 /* Consume the `,'. */
6708 cp_lexer_consume_token (parser->lexer);
6709 /* A comma operator cannot appear in a constant-expression. */
6710 if (cp_parser_non_integral_constant_expression (parser,
6711 "a comma operator"))
6712 expression = error_mark_node;
6715 return expression;
6718 /* Parse a constant-expression.
6720 constant-expression:
6721 conditional-expression
6723 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6724 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6725 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6726 is false, NON_CONSTANT_P should be NULL. */
6728 static tree
6729 cp_parser_constant_expression (cp_parser* parser,
6730 bool allow_non_constant_p,
6731 bool *non_constant_p)
6733 bool saved_integral_constant_expression_p;
6734 bool saved_allow_non_integral_constant_expression_p;
6735 bool saved_non_integral_constant_expression_p;
6736 tree expression;
6738 /* It might seem that we could simply parse the
6739 conditional-expression, and then check to see if it were
6740 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6741 one that the compiler can figure out is constant, possibly after
6742 doing some simplifications or optimizations. The standard has a
6743 precise definition of constant-expression, and we must honor
6744 that, even though it is somewhat more restrictive.
6746 For example:
6748 int i[(2, 3)];
6750 is not a legal declaration, because `(2, 3)' is not a
6751 constant-expression. The `,' operator is forbidden in a
6752 constant-expression. However, GCC's constant-folding machinery
6753 will fold this operation to an INTEGER_CST for `3'. */
6755 /* Save the old settings. */
6756 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6757 saved_allow_non_integral_constant_expression_p
6758 = parser->allow_non_integral_constant_expression_p;
6759 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6760 /* We are now parsing a constant-expression. */
6761 parser->integral_constant_expression_p = true;
6762 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6763 parser->non_integral_constant_expression_p = false;
6764 /* Although the grammar says "conditional-expression", we parse an
6765 "assignment-expression", which also permits "throw-expression"
6766 and the use of assignment operators. In the case that
6767 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6768 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6769 actually essential that we look for an assignment-expression.
6770 For example, cp_parser_initializer_clauses uses this function to
6771 determine whether a particular assignment-expression is in fact
6772 constant. */
6773 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6774 /* Restore the old settings. */
6775 parser->integral_constant_expression_p
6776 = saved_integral_constant_expression_p;
6777 parser->allow_non_integral_constant_expression_p
6778 = saved_allow_non_integral_constant_expression_p;
6779 if (allow_non_constant_p)
6780 *non_constant_p = parser->non_integral_constant_expression_p;
6781 else if (parser->non_integral_constant_expression_p)
6782 expression = error_mark_node;
6783 parser->non_integral_constant_expression_p
6784 = saved_non_integral_constant_expression_p;
6786 return expression;
6789 /* Parse __builtin_offsetof.
6791 offsetof-expression:
6792 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6794 offsetof-member-designator:
6795 id-expression
6796 | offsetof-member-designator "." id-expression
6797 | offsetof-member-designator "[" expression "]"
6798 | offsetof-member-designator "->" id-expression */
6800 static tree
6801 cp_parser_builtin_offsetof (cp_parser *parser)
6803 int save_ice_p, save_non_ice_p;
6804 tree type, expr;
6805 cp_id_kind dummy;
6806 cp_token *token;
6808 /* We're about to accept non-integral-constant things, but will
6809 definitely yield an integral constant expression. Save and
6810 restore these values around our local parsing. */
6811 save_ice_p = parser->integral_constant_expression_p;
6812 save_non_ice_p = parser->non_integral_constant_expression_p;
6814 /* Consume the "__builtin_offsetof" token. */
6815 cp_lexer_consume_token (parser->lexer);
6816 /* Consume the opening `('. */
6817 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6818 /* Parse the type-id. */
6819 type = cp_parser_type_id (parser);
6820 /* Look for the `,'. */
6821 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6822 token = cp_lexer_peek_token (parser->lexer);
6824 /* Build the (type *)null that begins the traditional offsetof macro. */
6825 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6826 tf_warning_or_error);
6828 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6829 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6830 true, &dummy, token->location);
6831 while (true)
6833 token = cp_lexer_peek_token (parser->lexer);
6834 switch (token->type)
6836 case CPP_OPEN_SQUARE:
6837 /* offsetof-member-designator "[" expression "]" */
6838 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6839 break;
6841 case CPP_DEREF:
6842 /* offsetof-member-designator "->" identifier */
6843 expr = grok_array_decl (expr, integer_zero_node);
6844 /* FALLTHRU */
6846 case CPP_DOT:
6847 /* offsetof-member-designator "." identifier */
6848 cp_lexer_consume_token (parser->lexer);
6849 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
6850 expr, true, &dummy,
6851 token->location);
6852 break;
6854 case CPP_CLOSE_PAREN:
6855 /* Consume the ")" token. */
6856 cp_lexer_consume_token (parser->lexer);
6857 goto success;
6859 default:
6860 /* Error. We know the following require will fail, but
6861 that gives the proper error message. */
6862 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6863 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6864 expr = error_mark_node;
6865 goto failure;
6869 success:
6870 /* If we're processing a template, we can't finish the semantics yet.
6871 Otherwise we can fold the entire expression now. */
6872 if (processing_template_decl)
6873 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6874 else
6875 expr = finish_offsetof (expr);
6877 failure:
6878 parser->integral_constant_expression_p = save_ice_p;
6879 parser->non_integral_constant_expression_p = save_non_ice_p;
6881 return expr;
6884 /* Parse a trait expression. */
6886 static tree
6887 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6889 cp_trait_kind kind;
6890 tree type1, type2 = NULL_TREE;
6891 bool binary = false;
6892 cp_decl_specifier_seq decl_specs;
6894 switch (keyword)
6896 case RID_HAS_NOTHROW_ASSIGN:
6897 kind = CPTK_HAS_NOTHROW_ASSIGN;
6898 break;
6899 case RID_HAS_NOTHROW_CONSTRUCTOR:
6900 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6901 break;
6902 case RID_HAS_NOTHROW_COPY:
6903 kind = CPTK_HAS_NOTHROW_COPY;
6904 break;
6905 case RID_HAS_TRIVIAL_ASSIGN:
6906 kind = CPTK_HAS_TRIVIAL_ASSIGN;
6907 break;
6908 case RID_HAS_TRIVIAL_CONSTRUCTOR:
6909 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6910 break;
6911 case RID_HAS_TRIVIAL_COPY:
6912 kind = CPTK_HAS_TRIVIAL_COPY;
6913 break;
6914 case RID_HAS_TRIVIAL_DESTRUCTOR:
6915 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6916 break;
6917 case RID_HAS_VIRTUAL_DESTRUCTOR:
6918 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6919 break;
6920 case RID_IS_ABSTRACT:
6921 kind = CPTK_IS_ABSTRACT;
6922 break;
6923 case RID_IS_BASE_OF:
6924 kind = CPTK_IS_BASE_OF;
6925 binary = true;
6926 break;
6927 case RID_IS_CLASS:
6928 kind = CPTK_IS_CLASS;
6929 break;
6930 case RID_IS_CONVERTIBLE_TO:
6931 kind = CPTK_IS_CONVERTIBLE_TO;
6932 binary = true;
6933 break;
6934 case RID_IS_EMPTY:
6935 kind = CPTK_IS_EMPTY;
6936 break;
6937 case RID_IS_ENUM:
6938 kind = CPTK_IS_ENUM;
6939 break;
6940 case RID_IS_POD:
6941 kind = CPTK_IS_POD;
6942 break;
6943 case RID_IS_POLYMORPHIC:
6944 kind = CPTK_IS_POLYMORPHIC;
6945 break;
6946 case RID_IS_STD_LAYOUT:
6947 kind = CPTK_IS_STD_LAYOUT;
6948 break;
6949 case RID_IS_TRIVIAL:
6950 kind = CPTK_IS_TRIVIAL;
6951 break;
6952 case RID_IS_UNION:
6953 kind = CPTK_IS_UNION;
6954 break;
6955 default:
6956 gcc_unreachable ();
6959 /* Consume the token. */
6960 cp_lexer_consume_token (parser->lexer);
6962 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6964 type1 = cp_parser_type_id (parser);
6966 if (type1 == error_mark_node)
6967 return error_mark_node;
6969 /* Build a trivial decl-specifier-seq. */
6970 clear_decl_specs (&decl_specs);
6971 decl_specs.type = type1;
6973 /* Call grokdeclarator to figure out what type this is. */
6974 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6975 /*initialized=*/0, /*attrlist=*/NULL);
6977 if (binary)
6979 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6981 type2 = cp_parser_type_id (parser);
6983 if (type2 == error_mark_node)
6984 return error_mark_node;
6986 /* Build a trivial decl-specifier-seq. */
6987 clear_decl_specs (&decl_specs);
6988 decl_specs.type = type2;
6990 /* Call grokdeclarator to figure out what type this is. */
6991 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6992 /*initialized=*/0, /*attrlist=*/NULL);
6995 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6997 /* Complete the trait expression, which may mean either processing
6998 the trait expr now or saving it for template instantiation. */
6999 return finish_trait_expr (kind, type1, type2);
7002 /* Lambdas that appear in variable initializer or default argument scope
7003 get that in their mangling, so we need to record it. We might as well
7004 use the count for function and namespace scopes as well. */
7005 static GTY(()) tree lambda_scope;
7006 static GTY(()) int lambda_count;
7007 typedef struct GTY(()) tree_int
7009 tree t;
7010 int i;
7011 } tree_int;
7012 DEF_VEC_O(tree_int);
7013 DEF_VEC_ALLOC_O(tree_int,gc);
7014 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7016 static void
7017 start_lambda_scope (tree decl)
7019 tree_int ti;
7020 gcc_assert (decl);
7021 /* Once we're inside a function, we ignore other scopes and just push
7022 the function again so that popping works properly. */
7023 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7024 decl = current_function_decl;
7025 ti.t = lambda_scope;
7026 ti.i = lambda_count;
7027 VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7028 if (lambda_scope != decl)
7030 /* Don't reset the count if we're still in the same function. */
7031 lambda_scope = decl;
7032 lambda_count = 0;
7036 static void
7037 record_lambda_scope (tree lambda)
7039 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7040 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7043 static void
7044 finish_lambda_scope (void)
7046 tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7047 if (lambda_scope != p->t)
7049 lambda_scope = p->t;
7050 lambda_count = p->i;
7052 VEC_pop (tree_int, lambda_scope_stack);
7055 /* Parse a lambda expression.
7057 lambda-expression:
7058 lambda-introducer lambda-declarator [opt] compound-statement
7060 Returns a representation of the expression. */
7062 static tree
7063 cp_parser_lambda_expression (cp_parser* parser)
7065 tree lambda_expr = build_lambda_expr ();
7066 tree type;
7068 LAMBDA_EXPR_LOCATION (lambda_expr)
7069 = cp_lexer_peek_token (parser->lexer)->location;
7071 /* We may be in the middle of deferred access check. Disable
7072 it now. */
7073 push_deferring_access_checks (dk_no_deferred);
7075 cp_parser_lambda_introducer (parser, lambda_expr);
7077 type = begin_lambda_type (lambda_expr);
7079 record_lambda_scope (lambda_expr);
7081 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
7082 determine_visibility (TYPE_NAME (type));
7084 /* Now that we've started the type, add the capture fields for any
7085 explicit captures. */
7086 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
7089 /* Inside the class, surrounding template-parameter-lists do not apply. */
7090 unsigned int saved_num_template_parameter_lists
7091 = parser->num_template_parameter_lists;
7093 parser->num_template_parameter_lists = 0;
7095 /* By virtue of defining a local class, a lambda expression has access to
7096 the private variables of enclosing classes. */
7098 cp_parser_lambda_declarator_opt (parser, lambda_expr);
7100 cp_parser_lambda_body (parser, lambda_expr);
7102 /* The capture list was built up in reverse order; fix that now. */
7104 tree newlist = NULL_TREE;
7105 tree elt, next;
7107 for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7108 elt; elt = next)
7110 tree field = TREE_PURPOSE (elt);
7111 char *buf;
7113 next = TREE_CHAIN (elt);
7114 TREE_CHAIN (elt) = newlist;
7115 newlist = elt;
7117 /* Also add __ to the beginning of the field name so that code
7118 outside the lambda body can't see the captured name. We could
7119 just remove the name entirely, but this is more useful for
7120 debugging. */
7121 if (field == LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
7122 /* The 'this' capture already starts with __. */
7123 continue;
7125 buf = (char *) alloca (IDENTIFIER_LENGTH (DECL_NAME (field)) + 3);
7126 buf[1] = buf[0] = '_';
7127 memcpy (buf + 2, IDENTIFIER_POINTER (DECL_NAME (field)),
7128 IDENTIFIER_LENGTH (DECL_NAME (field)) + 1);
7129 DECL_NAME (field) = get_identifier (buf);
7131 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7134 maybe_add_lambda_conv_op (type);
7136 type = finish_struct (type, /*attributes=*/NULL_TREE);
7138 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7141 pop_deferring_access_checks ();
7143 return build_lambda_object (lambda_expr);
7146 /* Parse the beginning of a lambda expression.
7148 lambda-introducer:
7149 [ lambda-capture [opt] ]
7151 LAMBDA_EXPR is the current representation of the lambda expression. */
7153 static void
7154 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7156 /* Need commas after the first capture. */
7157 bool first = true;
7159 /* Eat the leading `['. */
7160 cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
7162 /* Record default capture mode. "[&" "[=" "[&," "[=," */
7163 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7164 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7165 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7166 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7167 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7169 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7171 cp_lexer_consume_token (parser->lexer);
7172 first = false;
7175 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7177 cp_token* capture_token;
7178 tree capture_id;
7179 tree capture_init_expr;
7180 cp_id_kind idk = CP_ID_KIND_NONE;
7181 bool explicit_init_p = false;
7183 enum capture_kind_type
7185 BY_COPY,
7186 BY_REFERENCE
7188 enum capture_kind_type capture_kind = BY_COPY;
7190 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7192 error ("expected end of capture-list");
7193 return;
7196 if (first)
7197 first = false;
7198 else
7199 cp_parser_require (parser, CPP_COMMA, "%<,%>");
7201 /* Possibly capture `this'. */
7202 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7204 cp_lexer_consume_token (parser->lexer);
7205 add_capture (lambda_expr,
7206 /*id=*/get_identifier ("__this"),
7207 /*initializer=*/finish_this_expr(),
7208 /*by_reference_p=*/false,
7209 explicit_init_p);
7210 continue;
7213 /* Remember whether we want to capture as a reference or not. */
7214 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7216 capture_kind = BY_REFERENCE;
7217 cp_lexer_consume_token (parser->lexer);
7220 /* Get the identifier. */
7221 capture_token = cp_lexer_peek_token (parser->lexer);
7222 capture_id = cp_parser_identifier (parser);
7224 if (capture_id == error_mark_node)
7225 /* Would be nice to have a cp_parser_skip_to_closing_x for general
7226 delimiters, but I modified this to stop on unnested ']' as well. It
7227 was already changed to stop on unnested '}', so the
7228 "closing_parenthesis" name is no more misleading with my change. */
7230 cp_parser_skip_to_closing_parenthesis (parser,
7231 /*recovering=*/true,
7232 /*or_comma=*/true,
7233 /*consume_paren=*/true);
7234 break;
7237 /* Find the initializer for this capture. */
7238 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7240 /* An explicit expression exists. */
7241 cp_lexer_consume_token (parser->lexer);
7242 pedwarn (input_location, OPT_pedantic,
7243 "ISO C++ does not allow initializers "
7244 "in lambda expression capture lists");
7245 capture_init_expr = cp_parser_assignment_expression (parser,
7246 /*cast_p=*/true,
7247 &idk);
7248 explicit_init_p = true;
7250 else
7252 const char* error_msg;
7254 /* Turn the identifier into an id-expression. */
7255 capture_init_expr
7256 = cp_parser_lookup_name
7257 (parser,
7258 capture_id,
7259 none_type,
7260 /*is_template=*/false,
7261 /*is_namespace=*/false,
7262 /*check_dependency=*/true,
7263 /*ambiguous_decls=*/NULL,
7264 capture_token->location);
7266 capture_init_expr
7267 = finish_id_expression
7268 (capture_id,
7269 capture_init_expr,
7270 parser->scope,
7271 &idk,
7272 /*integral_constant_expression_p=*/false,
7273 /*allow_non_integral_constant_expression_p=*/false,
7274 /*non_integral_constant_expression_p=*/NULL,
7275 /*template_p=*/false,
7276 /*done=*/true,
7277 /*address_p=*/false,
7278 /*template_arg_p=*/false,
7279 &error_msg,
7280 capture_token->location);
7283 if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7284 capture_init_expr
7285 = unqualified_name_lookup_error (capture_init_expr);
7287 add_capture (lambda_expr,
7288 capture_id,
7289 capture_init_expr,
7290 /*by_reference_p=*/capture_kind == BY_REFERENCE,
7291 explicit_init_p);
7294 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
7297 /* Parse the (optional) middle of a lambda expression.
7299 lambda-declarator:
7300 ( parameter-declaration-clause [opt] )
7301 attribute-specifier [opt]
7302 mutable [opt]
7303 exception-specification [opt]
7304 lambda-return-type-clause [opt]
7306 LAMBDA_EXPR is the current representation of the lambda expression. */
7308 static void
7309 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7311 /* 5.1.1.4 of the standard says:
7312 If a lambda-expression does not include a lambda-declarator, it is as if
7313 the lambda-declarator were ().
7314 This means an empty parameter list, no attributes, and no exception
7315 specification. */
7316 tree param_list = void_list_node;
7317 tree attributes = NULL_TREE;
7318 tree exception_spec = NULL_TREE;
7319 tree t;
7321 /* The lambda-declarator is optional, but must begin with an opening
7322 parenthesis if present. */
7323 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7325 cp_lexer_consume_token (parser->lexer);
7327 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7329 /* Parse parameters. */
7330 param_list = cp_parser_parameter_declaration_clause (parser);
7332 /* Default arguments shall not be specified in the
7333 parameter-declaration-clause of a lambda-declarator. */
7334 for (t = param_list; t; t = TREE_CHAIN (t))
7335 if (TREE_PURPOSE (t))
7336 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7337 "default argument specified for lambda parameter");
7339 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7341 attributes = cp_parser_attributes_opt (parser);
7343 /* Parse optional `mutable' keyword. */
7344 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7346 cp_lexer_consume_token (parser->lexer);
7347 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7350 /* Parse optional exception specification. */
7351 exception_spec = cp_parser_exception_specification_opt (parser);
7353 /* Parse optional trailing return type. */
7354 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7356 cp_lexer_consume_token (parser->lexer);
7357 LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7360 /* The function parameters must be in scope all the way until after the
7361 trailing-return-type in case of decltype. */
7362 for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
7363 pop_binding (DECL_NAME (t), t);
7365 leave_scope ();
7368 /* Create the function call operator.
7370 Messing with declarators like this is no uglier than building up the
7371 FUNCTION_DECL by hand, and this is less likely to get out of sync with
7372 other code. */
7374 cp_decl_specifier_seq return_type_specs;
7375 cp_declarator* declarator;
7376 tree fco;
7377 int quals;
7378 void *p;
7380 clear_decl_specs (&return_type_specs);
7381 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7382 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7383 else
7384 /* Maybe we will deduce the return type later, but we can use void
7385 as a placeholder return type anyways. */
7386 return_type_specs.type = void_type_node;
7388 p = obstack_alloc (&declarator_obstack, 0);
7390 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7391 sfk_none);
7393 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
7394 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
7395 declarator = make_call_declarator (declarator, param_list, quals,
7396 exception_spec,
7397 /*late_return_type=*/NULL_TREE);
7399 fco = grokmethod (&return_type_specs,
7400 declarator,
7401 attributes);
7402 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7403 DECL_ARTIFICIAL (fco) = 1;
7405 finish_member_declaration (fco);
7407 obstack_free (&declarator_obstack, p);
7411 /* Parse the body of a lambda expression, which is simply
7413 compound-statement
7415 but which requires special handling.
7416 LAMBDA_EXPR is the current representation of the lambda expression. */
7418 static void
7419 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7421 bool nested = (current_function_decl != NULL_TREE);
7422 if (nested)
7423 push_function_context ();
7425 /* Finish the function call operator
7426 - class_specifier
7427 + late_parsing_for_member
7428 + function_definition_after_declarator
7429 + ctor_initializer_opt_and_function_body */
7431 tree fco = lambda_function (lambda_expr);
7432 tree body;
7433 bool done = false;
7435 /* Let the front end know that we are going to be defining this
7436 function. */
7437 start_preparsed_function (fco,
7438 NULL_TREE,
7439 SF_PRE_PARSED | SF_INCLASS_INLINE);
7441 start_lambda_scope (fco);
7442 body = begin_function_body ();
7444 /* 5.1.1.4 of the standard says:
7445 If a lambda-expression does not include a trailing-return-type, it
7446 is as if the trailing-return-type denotes the following type:
7447 * if the compound-statement is of the form
7448 { return attribute-specifier [opt] expression ; }
7449 the type of the returned expression after lvalue-to-rvalue
7450 conversion (_conv.lval_ 4.1), array-to-pointer conversion
7451 (_conv.array_ 4.2), and function-to-pointer conversion
7452 (_conv.func_ 4.3);
7453 * otherwise, void. */
7455 /* In a lambda that has neither a lambda-return-type-clause
7456 nor a deducible form, errors should be reported for return statements
7457 in the body. Since we used void as the placeholder return type, parsing
7458 the body as usual will give such desired behavior. */
7459 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7460 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
7461 && cp_lexer_peek_nth_token (parser->lexer, 2)->keyword == RID_RETURN
7462 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_SEMICOLON)
7464 tree compound_stmt;
7465 tree expr = NULL_TREE;
7466 cp_id_kind idk = CP_ID_KIND_NONE;
7468 /* Parse tentatively in case there's more after the initial return
7469 statement. */
7470 cp_parser_parse_tentatively (parser);
7472 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7473 cp_parser_require_keyword (parser, RID_RETURN, "%<return%>");
7475 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7477 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7478 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7480 if (cp_parser_parse_definitely (parser))
7482 apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7484 compound_stmt = begin_compound_stmt (0);
7485 /* Will get error here if type not deduced yet. */
7486 finish_return_stmt (expr);
7487 finish_compound_stmt (compound_stmt);
7489 done = true;
7493 if (!done)
7495 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7496 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
7497 /* TODO: does begin_compound_stmt want BCS_FN_BODY?
7498 cp_parser_compound_stmt does not pass it. */
7499 cp_parser_function_body (parser);
7500 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
7503 finish_function_body (body);
7504 finish_lambda_scope ();
7506 /* Finish the function and generate code for it if necessary. */
7507 expand_or_defer_fn (finish_function (/*inline*/2));
7510 if (nested)
7511 pop_function_context();
7514 /* Statements [gram.stmt.stmt] */
7516 /* Parse a statement.
7518 statement:
7519 labeled-statement
7520 expression-statement
7521 compound-statement
7522 selection-statement
7523 iteration-statement
7524 jump-statement
7525 declaration-statement
7526 try-block
7528 IN_COMPOUND is true when the statement is nested inside a
7529 cp_parser_compound_statement; this matters for certain pragmas.
7531 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7532 is a (possibly labeled) if statement which is not enclosed in braces
7533 and has an else clause. This is used to implement -Wparentheses. */
7535 static void
7536 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
7537 bool in_compound, bool *if_p)
7539 tree statement;
7540 cp_token *token;
7541 location_t statement_location;
7543 restart:
7544 if (if_p != NULL)
7545 *if_p = false;
7546 /* There is no statement yet. */
7547 statement = NULL_TREE;
7548 /* Peek at the next token. */
7549 token = cp_lexer_peek_token (parser->lexer);
7550 /* Remember the location of the first token in the statement. */
7551 statement_location = token->location;
7552 /* If this is a keyword, then that will often determine what kind of
7553 statement we have. */
7554 if (token->type == CPP_KEYWORD)
7556 enum rid keyword = token->keyword;
7558 switch (keyword)
7560 case RID_CASE:
7561 case RID_DEFAULT:
7562 /* Looks like a labeled-statement with a case label.
7563 Parse the label, and then use tail recursion to parse
7564 the statement. */
7565 cp_parser_label_for_labeled_statement (parser);
7566 goto restart;
7568 case RID_IF:
7569 case RID_SWITCH:
7570 statement = cp_parser_selection_statement (parser, if_p);
7571 break;
7573 case RID_WHILE:
7574 case RID_DO:
7575 case RID_FOR:
7576 statement = cp_parser_iteration_statement (parser);
7577 break;
7579 case RID_BREAK:
7580 case RID_CONTINUE:
7581 case RID_RETURN:
7582 case RID_GOTO:
7583 statement = cp_parser_jump_statement (parser);
7584 break;
7586 /* Objective-C++ exception-handling constructs. */
7587 case RID_AT_TRY:
7588 case RID_AT_CATCH:
7589 case RID_AT_FINALLY:
7590 case RID_AT_SYNCHRONIZED:
7591 case RID_AT_THROW:
7592 statement = cp_parser_objc_statement (parser);
7593 break;
7595 case RID_TRY:
7596 statement = cp_parser_try_block (parser);
7597 break;
7599 case RID_NAMESPACE:
7600 /* This must be a namespace alias definition. */
7601 cp_parser_declaration_statement (parser);
7602 return;
7604 default:
7605 /* It might be a keyword like `int' that can start a
7606 declaration-statement. */
7607 break;
7610 else if (token->type == CPP_NAME)
7612 /* If the next token is a `:', then we are looking at a
7613 labeled-statement. */
7614 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7615 if (token->type == CPP_COLON)
7617 /* Looks like a labeled-statement with an ordinary label.
7618 Parse the label, and then use tail recursion to parse
7619 the statement. */
7620 cp_parser_label_for_labeled_statement (parser);
7621 goto restart;
7624 /* Anything that starts with a `{' must be a compound-statement. */
7625 else if (token->type == CPP_OPEN_BRACE)
7626 statement = cp_parser_compound_statement (parser, NULL, false);
7627 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
7628 a statement all its own. */
7629 else if (token->type == CPP_PRAGMA)
7631 /* Only certain OpenMP pragmas are attached to statements, and thus
7632 are considered statements themselves. All others are not. In
7633 the context of a compound, accept the pragma as a "statement" and
7634 return so that we can check for a close brace. Otherwise we
7635 require a real statement and must go back and read one. */
7636 if (in_compound)
7637 cp_parser_pragma (parser, pragma_compound);
7638 else if (!cp_parser_pragma (parser, pragma_stmt))
7639 goto restart;
7640 return;
7642 else if (token->type == CPP_EOF)
7644 cp_parser_error (parser, "expected statement");
7645 return;
7648 /* Everything else must be a declaration-statement or an
7649 expression-statement. Try for the declaration-statement
7650 first, unless we are looking at a `;', in which case we know that
7651 we have an expression-statement. */
7652 if (!statement)
7654 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7656 cp_parser_parse_tentatively (parser);
7657 /* Try to parse the declaration-statement. */
7658 cp_parser_declaration_statement (parser);
7659 /* If that worked, we're done. */
7660 if (cp_parser_parse_definitely (parser))
7661 return;
7663 /* Look for an expression-statement instead. */
7664 statement = cp_parser_expression_statement (parser, in_statement_expr);
7667 /* Set the line number for the statement. */
7668 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7669 SET_EXPR_LOCATION (statement, statement_location);
7672 /* Parse the label for a labeled-statement, i.e.
7674 identifier :
7675 case constant-expression :
7676 default :
7678 GNU Extension:
7679 case constant-expression ... constant-expression : statement
7681 When a label is parsed without errors, the label is added to the
7682 parse tree by the finish_* functions, so this function doesn't
7683 have to return the label. */
7685 static void
7686 cp_parser_label_for_labeled_statement (cp_parser* parser)
7688 cp_token *token;
7689 tree label = NULL_TREE;
7691 /* The next token should be an identifier. */
7692 token = cp_lexer_peek_token (parser->lexer);
7693 if (token->type != CPP_NAME
7694 && token->type != CPP_KEYWORD)
7696 cp_parser_error (parser, "expected labeled-statement");
7697 return;
7700 switch (token->keyword)
7702 case RID_CASE:
7704 tree expr, expr_hi;
7705 cp_token *ellipsis;
7707 /* Consume the `case' token. */
7708 cp_lexer_consume_token (parser->lexer);
7709 /* Parse the constant-expression. */
7710 expr = cp_parser_constant_expression (parser,
7711 /*allow_non_constant_p=*/false,
7712 NULL);
7714 ellipsis = cp_lexer_peek_token (parser->lexer);
7715 if (ellipsis->type == CPP_ELLIPSIS)
7717 /* Consume the `...' token. */
7718 cp_lexer_consume_token (parser->lexer);
7719 expr_hi =
7720 cp_parser_constant_expression (parser,
7721 /*allow_non_constant_p=*/false,
7722 NULL);
7723 /* We don't need to emit warnings here, as the common code
7724 will do this for us. */
7726 else
7727 expr_hi = NULL_TREE;
7729 if (parser->in_switch_statement_p)
7730 finish_case_label (token->location, expr, expr_hi);
7731 else
7732 error_at (token->location,
7733 "case label %qE not within a switch statement",
7734 expr);
7736 break;
7738 case RID_DEFAULT:
7739 /* Consume the `default' token. */
7740 cp_lexer_consume_token (parser->lexer);
7742 if (parser->in_switch_statement_p)
7743 finish_case_label (token->location, NULL_TREE, NULL_TREE);
7744 else
7745 error_at (token->location, "case label not within a switch statement");
7746 break;
7748 default:
7749 /* Anything else must be an ordinary label. */
7750 label = finish_label_stmt (cp_parser_identifier (parser));
7751 break;
7754 /* Require the `:' token. */
7755 cp_parser_require (parser, CPP_COLON, "%<:%>");
7757 /* An ordinary label may optionally be followed by attributes.
7758 However, this is only permitted if the attributes are then
7759 followed by a semicolon. This is because, for backward
7760 compatibility, when parsing
7761 lab: __attribute__ ((unused)) int i;
7762 we want the attribute to attach to "i", not "lab". */
7763 if (label != NULL_TREE
7764 && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
7766 tree attrs;
7768 cp_parser_parse_tentatively (parser);
7769 attrs = cp_parser_attributes_opt (parser);
7770 if (attrs == NULL_TREE
7771 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7772 cp_parser_abort_tentative_parse (parser);
7773 else if (!cp_parser_parse_definitely (parser))
7775 else
7776 cplus_decl_attributes (&label, attrs, 0);
7780 /* Parse an expression-statement.
7782 expression-statement:
7783 expression [opt] ;
7785 Returns the new EXPR_STMT -- or NULL_TREE if the expression
7786 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
7787 indicates whether this expression-statement is part of an
7788 expression statement. */
7790 static tree
7791 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
7793 tree statement = NULL_TREE;
7794 cp_token *token = cp_lexer_peek_token (parser->lexer);
7796 /* If the next token is a ';', then there is no expression
7797 statement. */
7798 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7799 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7801 /* Give a helpful message for "A<T>::type t;" and the like. */
7802 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
7803 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
7805 if (TREE_CODE (statement) == SCOPE_REF)
7806 error_at (token->location, "need %<typename%> before %qE because "
7807 "%qT is a dependent scope",
7808 statement, TREE_OPERAND (statement, 0));
7809 else if (is_overloaded_fn (statement)
7810 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
7812 /* A::A a; */
7813 tree fn = get_first_fn (statement);
7814 error_at (token->location,
7815 "%<%T::%D%> names the constructor, not the type",
7816 DECL_CONTEXT (fn), DECL_NAME (fn));
7820 /* Consume the final `;'. */
7821 cp_parser_consume_semicolon_at_end_of_statement (parser);
7823 if (in_statement_expr
7824 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
7825 /* This is the final expression statement of a statement
7826 expression. */
7827 statement = finish_stmt_expr_expr (statement, in_statement_expr);
7828 else if (statement)
7829 statement = finish_expr_stmt (statement);
7830 else
7831 finish_stmt ();
7833 return statement;
7836 /* Parse a compound-statement.
7838 compound-statement:
7839 { statement-seq [opt] }
7841 GNU extension:
7843 compound-statement:
7844 { label-declaration-seq [opt] statement-seq [opt] }
7846 label-declaration-seq:
7847 label-declaration
7848 label-declaration-seq label-declaration
7850 Returns a tree representing the statement. */
7852 static tree
7853 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7854 bool in_try)
7856 tree compound_stmt;
7858 /* Consume the `{'. */
7859 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7860 return error_mark_node;
7861 /* Begin the compound-statement. */
7862 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7863 /* If the next keyword is `__label__' we have a label declaration. */
7864 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7865 cp_parser_label_declaration (parser);
7866 /* Parse an (optional) statement-seq. */
7867 cp_parser_statement_seq_opt (parser, in_statement_expr);
7868 /* Finish the compound-statement. */
7869 finish_compound_stmt (compound_stmt);
7870 /* Consume the `}'. */
7871 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7873 return compound_stmt;
7876 /* Parse an (optional) statement-seq.
7878 statement-seq:
7879 statement
7880 statement-seq [opt] statement */
7882 static void
7883 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7885 /* Scan statements until there aren't any more. */
7886 while (true)
7888 cp_token *token = cp_lexer_peek_token (parser->lexer);
7890 /* If we're looking at a `}', then we've run out of statements. */
7891 if (token->type == CPP_CLOSE_BRACE
7892 || token->type == CPP_EOF
7893 || token->type == CPP_PRAGMA_EOL)
7894 break;
7896 /* If we are in a compound statement and find 'else' then
7897 something went wrong. */
7898 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7900 if (parser->in_statement & IN_IF_STMT)
7901 break;
7902 else
7904 token = cp_lexer_consume_token (parser->lexer);
7905 error_at (token->location, "%<else%> without a previous %<if%>");
7909 /* Parse the statement. */
7910 cp_parser_statement (parser, in_statement_expr, true, NULL);
7914 /* Parse a selection-statement.
7916 selection-statement:
7917 if ( condition ) statement
7918 if ( condition ) statement else statement
7919 switch ( condition ) statement
7921 Returns the new IF_STMT or SWITCH_STMT.
7923 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7924 is a (possibly labeled) if statement which is not enclosed in
7925 braces and has an else clause. This is used to implement
7926 -Wparentheses. */
7928 static tree
7929 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7931 cp_token *token;
7932 enum rid keyword;
7934 if (if_p != NULL)
7935 *if_p = false;
7937 /* Peek at the next token. */
7938 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7940 /* See what kind of keyword it is. */
7941 keyword = token->keyword;
7942 switch (keyword)
7944 case RID_IF:
7945 case RID_SWITCH:
7947 tree statement;
7948 tree condition;
7950 /* Look for the `('. */
7951 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7953 cp_parser_skip_to_end_of_statement (parser);
7954 return error_mark_node;
7957 /* Begin the selection-statement. */
7958 if (keyword == RID_IF)
7959 statement = begin_if_stmt ();
7960 else
7961 statement = begin_switch_stmt ();
7963 /* Parse the condition. */
7964 condition = cp_parser_condition (parser);
7965 /* Look for the `)'. */
7966 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7967 cp_parser_skip_to_closing_parenthesis (parser, true, false,
7968 /*consume_paren=*/true);
7970 if (keyword == RID_IF)
7972 bool nested_if;
7973 unsigned char in_statement;
7975 /* Add the condition. */
7976 finish_if_stmt_cond (condition, statement);
7978 /* Parse the then-clause. */
7979 in_statement = parser->in_statement;
7980 parser->in_statement |= IN_IF_STMT;
7981 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7983 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7984 add_stmt (build_empty_stmt (loc));
7985 cp_lexer_consume_token (parser->lexer);
7986 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
7987 warning_at (loc, OPT_Wempty_body, "suggest braces around "
7988 "empty body in an %<if%> statement");
7989 nested_if = false;
7991 else
7992 cp_parser_implicitly_scoped_statement (parser, &nested_if);
7993 parser->in_statement = in_statement;
7995 finish_then_clause (statement);
7997 /* If the next token is `else', parse the else-clause. */
7998 if (cp_lexer_next_token_is_keyword (parser->lexer,
7999 RID_ELSE))
8001 /* Consume the `else' keyword. */
8002 cp_lexer_consume_token (parser->lexer);
8003 begin_else_clause (statement);
8004 /* Parse the else-clause. */
8005 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8007 location_t loc;
8008 loc = cp_lexer_peek_token (parser->lexer)->location;
8009 warning_at (loc,
8010 OPT_Wempty_body, "suggest braces around "
8011 "empty body in an %<else%> statement");
8012 add_stmt (build_empty_stmt (loc));
8013 cp_lexer_consume_token (parser->lexer);
8015 else
8016 cp_parser_implicitly_scoped_statement (parser, NULL);
8018 finish_else_clause (statement);
8020 /* If we are currently parsing a then-clause, then
8021 IF_P will not be NULL. We set it to true to
8022 indicate that this if statement has an else clause.
8023 This may trigger the Wparentheses warning below
8024 when we get back up to the parent if statement. */
8025 if (if_p != NULL)
8026 *if_p = true;
8028 else
8030 /* This if statement does not have an else clause. If
8031 NESTED_IF is true, then the then-clause is an if
8032 statement which does have an else clause. We warn
8033 about the potential ambiguity. */
8034 if (nested_if)
8035 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
8036 "suggest explicit braces to avoid ambiguous"
8037 " %<else%>");
8040 /* Now we're all done with the if-statement. */
8041 finish_if_stmt (statement);
8043 else
8045 bool in_switch_statement_p;
8046 unsigned char in_statement;
8048 /* Add the condition. */
8049 finish_switch_cond (condition, statement);
8051 /* Parse the body of the switch-statement. */
8052 in_switch_statement_p = parser->in_switch_statement_p;
8053 in_statement = parser->in_statement;
8054 parser->in_switch_statement_p = true;
8055 parser->in_statement |= IN_SWITCH_STMT;
8056 cp_parser_implicitly_scoped_statement (parser, NULL);
8057 parser->in_switch_statement_p = in_switch_statement_p;
8058 parser->in_statement = in_statement;
8060 /* Now we're all done with the switch-statement. */
8061 finish_switch_stmt (statement);
8064 return statement;
8066 break;
8068 default:
8069 cp_parser_error (parser, "expected selection-statement");
8070 return error_mark_node;
8074 /* Parse a condition.
8076 condition:
8077 expression
8078 type-specifier-seq declarator = initializer-clause
8079 type-specifier-seq declarator braced-init-list
8081 GNU Extension:
8083 condition:
8084 type-specifier-seq declarator asm-specification [opt]
8085 attributes [opt] = assignment-expression
8087 Returns the expression that should be tested. */
8089 static tree
8090 cp_parser_condition (cp_parser* parser)
8092 cp_decl_specifier_seq type_specifiers;
8093 const char *saved_message;
8095 /* Try the declaration first. */
8096 cp_parser_parse_tentatively (parser);
8097 /* New types are not allowed in the type-specifier-seq for a
8098 condition. */
8099 saved_message = parser->type_definition_forbidden_message;
8100 parser->type_definition_forbidden_message
8101 = G_("types may not be defined in conditions");
8102 /* Parse the type-specifier-seq. */
8103 cp_parser_type_specifier_seq (parser, /*is_declaration==*/true,
8104 /*is_trailing_return=*/false,
8105 &type_specifiers);
8106 /* Restore the saved message. */
8107 parser->type_definition_forbidden_message = saved_message;
8108 /* If all is well, we might be looking at a declaration. */
8109 if (!cp_parser_error_occurred (parser))
8111 tree decl;
8112 tree asm_specification;
8113 tree attributes;
8114 cp_declarator *declarator;
8115 tree initializer = NULL_TREE;
8117 /* Parse the declarator. */
8118 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8119 /*ctor_dtor_or_conv_p=*/NULL,
8120 /*parenthesized_p=*/NULL,
8121 /*member_p=*/false);
8122 /* Parse the attributes. */
8123 attributes = cp_parser_attributes_opt (parser);
8124 /* Parse the asm-specification. */
8125 asm_specification = cp_parser_asm_specification_opt (parser);
8126 /* If the next token is not an `=' or '{', then we might still be
8127 looking at an expression. For example:
8129 if (A(a).x)
8131 looks like a decl-specifier-seq and a declarator -- but then
8132 there is no `=', so this is an expression. */
8133 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8134 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8135 cp_parser_simulate_error (parser);
8137 /* If we did see an `=' or '{', then we are looking at a declaration
8138 for sure. */
8139 if (cp_parser_parse_definitely (parser))
8141 tree pushed_scope;
8142 bool non_constant_p;
8143 bool flags = LOOKUP_ONLYCONVERTING;
8145 /* Create the declaration. */
8146 decl = start_decl (declarator, &type_specifiers,
8147 /*initialized_p=*/true,
8148 attributes, /*prefix_attributes=*/NULL_TREE,
8149 &pushed_scope);
8151 /* Parse the initializer. */
8152 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8154 initializer = cp_parser_braced_list (parser, &non_constant_p);
8155 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8156 flags = 0;
8158 else
8160 /* Consume the `='. */
8161 cp_parser_require (parser, CPP_EQ, "%<=%>");
8162 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8164 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
8165 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8167 if (!non_constant_p)
8168 initializer = fold_non_dependent_expr (initializer);
8170 /* Process the initializer. */
8171 cp_finish_decl (decl,
8172 initializer, !non_constant_p,
8173 asm_specification,
8174 flags);
8176 if (pushed_scope)
8177 pop_scope (pushed_scope);
8179 return convert_from_reference (decl);
8182 /* If we didn't even get past the declarator successfully, we are
8183 definitely not looking at a declaration. */
8184 else
8185 cp_parser_abort_tentative_parse (parser);
8187 /* Otherwise, we are looking at an expression. */
8188 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
8191 /* Parse an iteration-statement.
8193 iteration-statement:
8194 while ( condition ) statement
8195 do statement while ( expression ) ;
8196 for ( for-init-statement condition [opt] ; expression [opt] )
8197 statement
8199 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
8201 static tree
8202 cp_parser_iteration_statement (cp_parser* parser)
8204 cp_token *token;
8205 enum rid keyword;
8206 tree statement;
8207 unsigned char in_statement;
8209 /* Peek at the next token. */
8210 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
8211 if (!token)
8212 return error_mark_node;
8214 /* Remember whether or not we are already within an iteration
8215 statement. */
8216 in_statement = parser->in_statement;
8218 /* See what kind of keyword it is. */
8219 keyword = token->keyword;
8220 switch (keyword)
8222 case RID_WHILE:
8224 tree condition;
8226 /* Begin the while-statement. */
8227 statement = begin_while_stmt ();
8228 /* Look for the `('. */
8229 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8230 /* Parse the condition. */
8231 condition = cp_parser_condition (parser);
8232 finish_while_stmt_cond (condition, statement);
8233 /* Look for the `)'. */
8234 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8235 /* Parse the dependent statement. */
8236 parser->in_statement = IN_ITERATION_STMT;
8237 cp_parser_already_scoped_statement (parser);
8238 parser->in_statement = in_statement;
8239 /* We're done with the while-statement. */
8240 finish_while_stmt (statement);
8242 break;
8244 case RID_DO:
8246 tree expression;
8248 /* Begin the do-statement. */
8249 statement = begin_do_stmt ();
8250 /* Parse the body of the do-statement. */
8251 parser->in_statement = IN_ITERATION_STMT;
8252 cp_parser_implicitly_scoped_statement (parser, NULL);
8253 parser->in_statement = in_statement;
8254 finish_do_body (statement);
8255 /* Look for the `while' keyword. */
8256 cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
8257 /* Look for the `('. */
8258 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8259 /* Parse the expression. */
8260 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8261 /* We're done with the do-statement. */
8262 finish_do_stmt (expression, statement);
8263 /* Look for the `)'. */
8264 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8265 /* Look for the `;'. */
8266 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8268 break;
8270 case RID_FOR:
8272 tree condition = NULL_TREE;
8273 tree expression = NULL_TREE;
8275 /* Begin the for-statement. */
8276 statement = begin_for_stmt ();
8277 /* Look for the `('. */
8278 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8279 /* Parse the initialization. */
8280 cp_parser_for_init_statement (parser);
8281 finish_for_init_stmt (statement);
8283 /* If there's a condition, process it. */
8284 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8285 condition = cp_parser_condition (parser);
8286 finish_for_cond (condition, statement);
8287 /* Look for the `;'. */
8288 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8290 /* If there's an expression, process it. */
8291 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8292 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8293 finish_for_expr (expression, statement);
8294 /* Look for the `)'. */
8295 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8297 /* Parse the body of the for-statement. */
8298 parser->in_statement = IN_ITERATION_STMT;
8299 cp_parser_already_scoped_statement (parser);
8300 parser->in_statement = in_statement;
8302 /* We're done with the for-statement. */
8303 finish_for_stmt (statement);
8305 break;
8307 default:
8308 cp_parser_error (parser, "expected iteration-statement");
8309 statement = error_mark_node;
8310 break;
8313 return statement;
8316 /* Parse a for-init-statement.
8318 for-init-statement:
8319 expression-statement
8320 simple-declaration */
8322 static void
8323 cp_parser_for_init_statement (cp_parser* parser)
8325 /* If the next token is a `;', then we have an empty
8326 expression-statement. Grammatically, this is also a
8327 simple-declaration, but an invalid one, because it does not
8328 declare anything. Therefore, if we did not handle this case
8329 specially, we would issue an error message about an invalid
8330 declaration. */
8331 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8333 /* We're going to speculatively look for a declaration, falling back
8334 to an expression, if necessary. */
8335 cp_parser_parse_tentatively (parser);
8336 /* Parse the declaration. */
8337 cp_parser_simple_declaration (parser,
8338 /*function_definition_allowed_p=*/false);
8339 /* If the tentative parse failed, then we shall need to look for an
8340 expression-statement. */
8341 if (cp_parser_parse_definitely (parser))
8342 return;
8345 cp_parser_expression_statement (parser, NULL_TREE);
8348 /* Parse a jump-statement.
8350 jump-statement:
8351 break ;
8352 continue ;
8353 return expression [opt] ;
8354 return braced-init-list ;
8355 goto identifier ;
8357 GNU extension:
8359 jump-statement:
8360 goto * expression ;
8362 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
8364 static tree
8365 cp_parser_jump_statement (cp_parser* parser)
8367 tree statement = error_mark_node;
8368 cp_token *token;
8369 enum rid keyword;
8370 unsigned char in_statement;
8372 /* Peek at the next token. */
8373 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
8374 if (!token)
8375 return error_mark_node;
8377 /* See what kind of keyword it is. */
8378 keyword = token->keyword;
8379 switch (keyword)
8381 case RID_BREAK:
8382 in_statement = parser->in_statement & ~IN_IF_STMT;
8383 switch (in_statement)
8385 case 0:
8386 error_at (token->location, "break statement not within loop or switch");
8387 break;
8388 default:
8389 gcc_assert ((in_statement & IN_SWITCH_STMT)
8390 || in_statement == IN_ITERATION_STMT);
8391 statement = finish_break_stmt ();
8392 break;
8393 case IN_OMP_BLOCK:
8394 error_at (token->location, "invalid exit from OpenMP structured block");
8395 break;
8396 case IN_OMP_FOR:
8397 error_at (token->location, "break statement used with OpenMP for loop");
8398 break;
8400 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8401 break;
8403 case RID_CONTINUE:
8404 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
8406 case 0:
8407 error_at (token->location, "continue statement not within a loop");
8408 break;
8409 case IN_ITERATION_STMT:
8410 case IN_OMP_FOR:
8411 statement = finish_continue_stmt ();
8412 break;
8413 case IN_OMP_BLOCK:
8414 error_at (token->location, "invalid exit from OpenMP structured block");
8415 break;
8416 default:
8417 gcc_unreachable ();
8419 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8420 break;
8422 case RID_RETURN:
8424 tree expr;
8425 bool expr_non_constant_p;
8427 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8429 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8430 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8432 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8433 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8434 else
8435 /* If the next token is a `;', then there is no
8436 expression. */
8437 expr = NULL_TREE;
8438 /* Build the return-statement. */
8439 statement = finish_return_stmt (expr);
8440 /* Look for the final `;'. */
8441 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8443 break;
8445 case RID_GOTO:
8446 /* Create the goto-statement. */
8447 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
8449 /* Issue a warning about this use of a GNU extension. */
8450 pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
8451 /* Consume the '*' token. */
8452 cp_lexer_consume_token (parser->lexer);
8453 /* Parse the dependent expression. */
8454 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
8456 else
8457 finish_goto_stmt (cp_parser_identifier (parser));
8458 /* Look for the final `;'. */
8459 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8460 break;
8462 default:
8463 cp_parser_error (parser, "expected jump-statement");
8464 break;
8467 return statement;
8470 /* Parse a declaration-statement.
8472 declaration-statement:
8473 block-declaration */
8475 static void
8476 cp_parser_declaration_statement (cp_parser* parser)
8478 void *p;
8480 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
8481 p = obstack_alloc (&declarator_obstack, 0);
8483 /* Parse the block-declaration. */
8484 cp_parser_block_declaration (parser, /*statement_p=*/true);
8486 /* Free any declarators allocated. */
8487 obstack_free (&declarator_obstack, p);
8489 /* Finish off the statement. */
8490 finish_stmt ();
8493 /* Some dependent statements (like `if (cond) statement'), are
8494 implicitly in their own scope. In other words, if the statement is
8495 a single statement (as opposed to a compound-statement), it is
8496 none-the-less treated as if it were enclosed in braces. Any
8497 declarations appearing in the dependent statement are out of scope
8498 after control passes that point. This function parses a statement,
8499 but ensures that is in its own scope, even if it is not a
8500 compound-statement.
8502 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8503 is a (possibly labeled) if statement which is not enclosed in
8504 braces and has an else clause. This is used to implement
8505 -Wparentheses.
8507 Returns the new statement. */
8509 static tree
8510 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
8512 tree statement;
8514 if (if_p != NULL)
8515 *if_p = false;
8517 /* Mark if () ; with a special NOP_EXPR. */
8518 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8520 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8521 cp_lexer_consume_token (parser->lexer);
8522 statement = add_stmt (build_empty_stmt (loc));
8524 /* if a compound is opened, we simply parse the statement directly. */
8525 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8526 statement = cp_parser_compound_statement (parser, NULL, false);
8527 /* If the token is not a `{', then we must take special action. */
8528 else
8530 /* Create a compound-statement. */
8531 statement = begin_compound_stmt (0);
8532 /* Parse the dependent-statement. */
8533 cp_parser_statement (parser, NULL_TREE, false, if_p);
8534 /* Finish the dummy compound-statement. */
8535 finish_compound_stmt (statement);
8538 /* Return the statement. */
8539 return statement;
8542 /* For some dependent statements (like `while (cond) statement'), we
8543 have already created a scope. Therefore, even if the dependent
8544 statement is a compound-statement, we do not want to create another
8545 scope. */
8547 static void
8548 cp_parser_already_scoped_statement (cp_parser* parser)
8550 /* If the token is a `{', then we must take special action. */
8551 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8552 cp_parser_statement (parser, NULL_TREE, false, NULL);
8553 else
8555 /* Avoid calling cp_parser_compound_statement, so that we
8556 don't create a new scope. Do everything else by hand. */
8557 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
8558 /* If the next keyword is `__label__' we have a label declaration. */
8559 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8560 cp_parser_label_declaration (parser);
8561 /* Parse an (optional) statement-seq. */
8562 cp_parser_statement_seq_opt (parser, NULL_TREE);
8563 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8567 /* Declarations [gram.dcl.dcl] */
8569 /* Parse an optional declaration-sequence.
8571 declaration-seq:
8572 declaration
8573 declaration-seq declaration */
8575 static void
8576 cp_parser_declaration_seq_opt (cp_parser* parser)
8578 while (true)
8580 cp_token *token;
8582 token = cp_lexer_peek_token (parser->lexer);
8584 if (token->type == CPP_CLOSE_BRACE
8585 || token->type == CPP_EOF
8586 || token->type == CPP_PRAGMA_EOL)
8587 break;
8589 if (token->type == CPP_SEMICOLON)
8591 /* A declaration consisting of a single semicolon is
8592 invalid. Allow it unless we're being pedantic. */
8593 cp_lexer_consume_token (parser->lexer);
8594 if (!in_system_header)
8595 pedwarn (input_location, OPT_pedantic, "extra %<;%>");
8596 continue;
8599 /* If we're entering or exiting a region that's implicitly
8600 extern "C", modify the lang context appropriately. */
8601 if (!parser->implicit_extern_c && token->implicit_extern_c)
8603 push_lang_context (lang_name_c);
8604 parser->implicit_extern_c = true;
8606 else if (parser->implicit_extern_c && !token->implicit_extern_c)
8608 pop_lang_context ();
8609 parser->implicit_extern_c = false;
8612 if (token->type == CPP_PRAGMA)
8614 /* A top-level declaration can consist solely of a #pragma.
8615 A nested declaration cannot, so this is done here and not
8616 in cp_parser_declaration. (A #pragma at block scope is
8617 handled in cp_parser_statement.) */
8618 cp_parser_pragma (parser, pragma_external);
8619 continue;
8622 /* Parse the declaration itself. */
8623 cp_parser_declaration (parser);
8627 /* Parse a declaration.
8629 declaration:
8630 block-declaration
8631 function-definition
8632 template-declaration
8633 explicit-instantiation
8634 explicit-specialization
8635 linkage-specification
8636 namespace-definition
8638 GNU extension:
8640 declaration:
8641 __extension__ declaration */
8643 static void
8644 cp_parser_declaration (cp_parser* parser)
8646 cp_token token1;
8647 cp_token token2;
8648 int saved_pedantic;
8649 void *p;
8651 /* Check for the `__extension__' keyword. */
8652 if (cp_parser_extension_opt (parser, &saved_pedantic))
8654 /* Parse the qualified declaration. */
8655 cp_parser_declaration (parser);
8656 /* Restore the PEDANTIC flag. */
8657 pedantic = saved_pedantic;
8659 return;
8662 /* Try to figure out what kind of declaration is present. */
8663 token1 = *cp_lexer_peek_token (parser->lexer);
8665 if (token1.type != CPP_EOF)
8666 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
8667 else
8669 token2.type = CPP_EOF;
8670 token2.keyword = RID_MAX;
8673 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
8674 p = obstack_alloc (&declarator_obstack, 0);
8676 /* If the next token is `extern' and the following token is a string
8677 literal, then we have a linkage specification. */
8678 if (token1.keyword == RID_EXTERN
8679 && cp_parser_is_string_literal (&token2))
8680 cp_parser_linkage_specification (parser);
8681 /* If the next token is `template', then we have either a template
8682 declaration, an explicit instantiation, or an explicit
8683 specialization. */
8684 else if (token1.keyword == RID_TEMPLATE)
8686 /* `template <>' indicates a template specialization. */
8687 if (token2.type == CPP_LESS
8688 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
8689 cp_parser_explicit_specialization (parser);
8690 /* `template <' indicates a template declaration. */
8691 else if (token2.type == CPP_LESS)
8692 cp_parser_template_declaration (parser, /*member_p=*/false);
8693 /* Anything else must be an explicit instantiation. */
8694 else
8695 cp_parser_explicit_instantiation (parser);
8697 /* If the next token is `export', then we have a template
8698 declaration. */
8699 else if (token1.keyword == RID_EXPORT)
8700 cp_parser_template_declaration (parser, /*member_p=*/false);
8701 /* If the next token is `extern', 'static' or 'inline' and the one
8702 after that is `template', we have a GNU extended explicit
8703 instantiation directive. */
8704 else if (cp_parser_allow_gnu_extensions_p (parser)
8705 && (token1.keyword == RID_EXTERN
8706 || token1.keyword == RID_STATIC
8707 || token1.keyword == RID_INLINE)
8708 && token2.keyword == RID_TEMPLATE)
8709 cp_parser_explicit_instantiation (parser);
8710 /* If the next token is `namespace', check for a named or unnamed
8711 namespace definition. */
8712 else if (token1.keyword == RID_NAMESPACE
8713 && (/* A named namespace definition. */
8714 (token2.type == CPP_NAME
8715 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
8716 != CPP_EQ))
8717 /* An unnamed namespace definition. */
8718 || token2.type == CPP_OPEN_BRACE
8719 || token2.keyword == RID_ATTRIBUTE))
8720 cp_parser_namespace_definition (parser);
8721 /* An inline (associated) namespace definition. */
8722 else if (token1.keyword == RID_INLINE
8723 && token2.keyword == RID_NAMESPACE)
8724 cp_parser_namespace_definition (parser);
8725 /* Objective-C++ declaration/definition. */
8726 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
8727 cp_parser_objc_declaration (parser);
8728 /* We must have either a block declaration or a function
8729 definition. */
8730 else
8731 /* Try to parse a block-declaration, or a function-definition. */
8732 cp_parser_block_declaration (parser, /*statement_p=*/false);
8734 /* Free any declarators allocated. */
8735 obstack_free (&declarator_obstack, p);
8738 /* Parse a block-declaration.
8740 block-declaration:
8741 simple-declaration
8742 asm-definition
8743 namespace-alias-definition
8744 using-declaration
8745 using-directive
8747 GNU Extension:
8749 block-declaration:
8750 __extension__ block-declaration
8752 C++0x Extension:
8754 block-declaration:
8755 static_assert-declaration
8757 If STATEMENT_P is TRUE, then this block-declaration is occurring as
8758 part of a declaration-statement. */
8760 static void
8761 cp_parser_block_declaration (cp_parser *parser,
8762 bool statement_p)
8764 cp_token *token1;
8765 int saved_pedantic;
8767 /* Check for the `__extension__' keyword. */
8768 if (cp_parser_extension_opt (parser, &saved_pedantic))
8770 /* Parse the qualified declaration. */
8771 cp_parser_block_declaration (parser, statement_p);
8772 /* Restore the PEDANTIC flag. */
8773 pedantic = saved_pedantic;
8775 return;
8778 /* Peek at the next token to figure out which kind of declaration is
8779 present. */
8780 token1 = cp_lexer_peek_token (parser->lexer);
8782 /* If the next keyword is `asm', we have an asm-definition. */
8783 if (token1->keyword == RID_ASM)
8785 if (statement_p)
8786 cp_parser_commit_to_tentative_parse (parser);
8787 cp_parser_asm_definition (parser);
8789 /* If the next keyword is `namespace', we have a
8790 namespace-alias-definition. */
8791 else if (token1->keyword == RID_NAMESPACE)
8792 cp_parser_namespace_alias_definition (parser);
8793 /* If the next keyword is `using', we have either a
8794 using-declaration or a using-directive. */
8795 else if (token1->keyword == RID_USING)
8797 cp_token *token2;
8799 if (statement_p)
8800 cp_parser_commit_to_tentative_parse (parser);
8801 /* If the token after `using' is `namespace', then we have a
8802 using-directive. */
8803 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8804 if (token2->keyword == RID_NAMESPACE)
8805 cp_parser_using_directive (parser);
8806 /* Otherwise, it's a using-declaration. */
8807 else
8808 cp_parser_using_declaration (parser,
8809 /*access_declaration_p=*/false);
8811 /* If the next keyword is `__label__' we have a misplaced label
8812 declaration. */
8813 else if (token1->keyword == RID_LABEL)
8815 cp_lexer_consume_token (parser->lexer);
8816 error_at (token1->location, "%<__label__%> not at the beginning of a block");
8817 cp_parser_skip_to_end_of_statement (parser);
8818 /* If the next token is now a `;', consume it. */
8819 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8820 cp_lexer_consume_token (parser->lexer);
8822 /* If the next token is `static_assert' we have a static assertion. */
8823 else if (token1->keyword == RID_STATIC_ASSERT)
8824 cp_parser_static_assert (parser, /*member_p=*/false);
8825 /* Anything else must be a simple-declaration. */
8826 else
8827 cp_parser_simple_declaration (parser, !statement_p);
8830 /* Parse a simple-declaration.
8832 simple-declaration:
8833 decl-specifier-seq [opt] init-declarator-list [opt] ;
8835 init-declarator-list:
8836 init-declarator
8837 init-declarator-list , init-declarator
8839 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8840 function-definition as a simple-declaration. */
8842 static void
8843 cp_parser_simple_declaration (cp_parser* parser,
8844 bool function_definition_allowed_p)
8846 cp_decl_specifier_seq decl_specifiers;
8847 int declares_class_or_enum;
8848 bool saw_declarator;
8850 /* Defer access checks until we know what is being declared; the
8851 checks for names appearing in the decl-specifier-seq should be
8852 done as if we were in the scope of the thing being declared. */
8853 push_deferring_access_checks (dk_deferred);
8855 /* Parse the decl-specifier-seq. We have to keep track of whether
8856 or not the decl-specifier-seq declares a named class or
8857 enumeration type, since that is the only case in which the
8858 init-declarator-list is allowed to be empty.
8860 [dcl.dcl]
8862 In a simple-declaration, the optional init-declarator-list can be
8863 omitted only when declaring a class or enumeration, that is when
8864 the decl-specifier-seq contains either a class-specifier, an
8865 elaborated-type-specifier, or an enum-specifier. */
8866 cp_parser_decl_specifier_seq (parser,
8867 CP_PARSER_FLAGS_OPTIONAL,
8868 &decl_specifiers,
8869 &declares_class_or_enum);
8870 /* We no longer need to defer access checks. */
8871 stop_deferring_access_checks ();
8873 /* In a block scope, a valid declaration must always have a
8874 decl-specifier-seq. By not trying to parse declarators, we can
8875 resolve the declaration/expression ambiguity more quickly. */
8876 if (!function_definition_allowed_p
8877 && !decl_specifiers.any_specifiers_p)
8879 cp_parser_error (parser, "expected declaration");
8880 goto done;
8883 /* If the next two tokens are both identifiers, the code is
8884 erroneous. The usual cause of this situation is code like:
8886 T t;
8888 where "T" should name a type -- but does not. */
8889 if (!decl_specifiers.any_type_specifiers_p
8890 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8892 /* If parsing tentatively, we should commit; we really are
8893 looking at a declaration. */
8894 cp_parser_commit_to_tentative_parse (parser);
8895 /* Give up. */
8896 goto done;
8899 /* If we have seen at least one decl-specifier, and the next token
8900 is not a parenthesis, then we must be looking at a declaration.
8901 (After "int (" we might be looking at a functional cast.) */
8902 if (decl_specifiers.any_specifiers_p
8903 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8904 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
8905 && !cp_parser_error_occurred (parser))
8906 cp_parser_commit_to_tentative_parse (parser);
8908 /* Keep going until we hit the `;' at the end of the simple
8909 declaration. */
8910 saw_declarator = false;
8911 while (cp_lexer_next_token_is_not (parser->lexer,
8912 CPP_SEMICOLON))
8914 cp_token *token;
8915 bool function_definition_p;
8916 tree decl;
8918 if (saw_declarator)
8920 /* If we are processing next declarator, coma is expected */
8921 token = cp_lexer_peek_token (parser->lexer);
8922 gcc_assert (token->type == CPP_COMMA);
8923 cp_lexer_consume_token (parser->lexer);
8925 else
8926 saw_declarator = true;
8928 /* Parse the init-declarator. */
8929 decl = cp_parser_init_declarator (parser, &decl_specifiers,
8930 /*checks=*/NULL,
8931 function_definition_allowed_p,
8932 /*member_p=*/false,
8933 declares_class_or_enum,
8934 &function_definition_p);
8935 /* If an error occurred while parsing tentatively, exit quickly.
8936 (That usually happens when in the body of a function; each
8937 statement is treated as a declaration-statement until proven
8938 otherwise.) */
8939 if (cp_parser_error_occurred (parser))
8940 goto done;
8941 /* Handle function definitions specially. */
8942 if (function_definition_p)
8944 /* If the next token is a `,', then we are probably
8945 processing something like:
8947 void f() {}, *p;
8949 which is erroneous. */
8950 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8952 cp_token *token = cp_lexer_peek_token (parser->lexer);
8953 error_at (token->location,
8954 "mixing"
8955 " declarations and function-definitions is forbidden");
8957 /* Otherwise, we're done with the list of declarators. */
8958 else
8960 pop_deferring_access_checks ();
8961 return;
8964 /* The next token should be either a `,' or a `;'. */
8965 token = cp_lexer_peek_token (parser->lexer);
8966 /* If it's a `,', there are more declarators to come. */
8967 if (token->type == CPP_COMMA)
8968 /* will be consumed next time around */;
8969 /* If it's a `;', we are done. */
8970 else if (token->type == CPP_SEMICOLON)
8971 break;
8972 /* Anything else is an error. */
8973 else
8975 /* If we have already issued an error message we don't need
8976 to issue another one. */
8977 if (decl != error_mark_node
8978 || cp_parser_uncommitted_to_tentative_parse_p (parser))
8979 cp_parser_error (parser, "expected %<,%> or %<;%>");
8980 /* Skip tokens until we reach the end of the statement. */
8981 cp_parser_skip_to_end_of_statement (parser);
8982 /* If the next token is now a `;', consume it. */
8983 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8984 cp_lexer_consume_token (parser->lexer);
8985 goto done;
8987 /* After the first time around, a function-definition is not
8988 allowed -- even if it was OK at first. For example:
8990 int i, f() {}
8992 is not valid. */
8993 function_definition_allowed_p = false;
8996 /* Issue an error message if no declarators are present, and the
8997 decl-specifier-seq does not itself declare a class or
8998 enumeration. */
8999 if (!saw_declarator)
9001 if (cp_parser_declares_only_class_p (parser))
9002 shadow_tag (&decl_specifiers);
9003 /* Perform any deferred access checks. */
9004 perform_deferred_access_checks ();
9007 /* Consume the `;'. */
9008 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
9010 done:
9011 pop_deferring_access_checks ();
9014 /* Parse a decl-specifier-seq.
9016 decl-specifier-seq:
9017 decl-specifier-seq [opt] decl-specifier
9019 decl-specifier:
9020 storage-class-specifier
9021 type-specifier
9022 function-specifier
9023 friend
9024 typedef
9026 GNU Extension:
9028 decl-specifier:
9029 attributes
9031 Set *DECL_SPECS to a representation of the decl-specifier-seq.
9033 The parser flags FLAGS is used to control type-specifier parsing.
9035 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
9036 flags:
9038 1: one of the decl-specifiers is an elaborated-type-specifier
9039 (i.e., a type declaration)
9040 2: one of the decl-specifiers is an enum-specifier or a
9041 class-specifier (i.e., a type definition)
9045 static void
9046 cp_parser_decl_specifier_seq (cp_parser* parser,
9047 cp_parser_flags flags,
9048 cp_decl_specifier_seq *decl_specs,
9049 int* declares_class_or_enum)
9051 bool constructor_possible_p = !parser->in_declarator_p;
9052 cp_token *start_token = NULL;
9054 /* Clear DECL_SPECS. */
9055 clear_decl_specs (decl_specs);
9057 /* Assume no class or enumeration type is declared. */
9058 *declares_class_or_enum = 0;
9060 /* Keep reading specifiers until there are no more to read. */
9061 while (true)
9063 bool constructor_p;
9064 bool found_decl_spec;
9065 cp_token *token;
9067 /* Peek at the next token. */
9068 token = cp_lexer_peek_token (parser->lexer);
9070 /* Save the first token of the decl spec list for error
9071 reporting. */
9072 if (!start_token)
9073 start_token = token;
9074 /* Handle attributes. */
9075 if (token->keyword == RID_ATTRIBUTE)
9077 /* Parse the attributes. */
9078 decl_specs->attributes
9079 = chainon (decl_specs->attributes,
9080 cp_parser_attributes_opt (parser));
9081 continue;
9083 /* Assume we will find a decl-specifier keyword. */
9084 found_decl_spec = true;
9085 /* If the next token is an appropriate keyword, we can simply
9086 add it to the list. */
9087 switch (token->keyword)
9089 /* decl-specifier:
9090 friend
9091 constexpr */
9092 case RID_FRIEND:
9093 if (!at_class_scope_p ())
9095 error_at (token->location, "%<friend%> used outside of class");
9096 cp_lexer_purge_token (parser->lexer);
9098 else
9100 ++decl_specs->specs[(int) ds_friend];
9101 /* Consume the token. */
9102 cp_lexer_consume_token (parser->lexer);
9104 break;
9106 case RID_CONSTEXPR:
9107 ++decl_specs->specs[(int) ds_constexpr];
9108 cp_lexer_consume_token (parser->lexer);
9109 break;
9111 /* function-specifier:
9112 inline
9113 virtual
9114 explicit */
9115 case RID_INLINE:
9116 case RID_VIRTUAL:
9117 case RID_EXPLICIT:
9118 cp_parser_function_specifier_opt (parser, decl_specs);
9119 break;
9121 /* decl-specifier:
9122 typedef */
9123 case RID_TYPEDEF:
9124 ++decl_specs->specs[(int) ds_typedef];
9125 /* Consume the token. */
9126 cp_lexer_consume_token (parser->lexer);
9127 /* A constructor declarator cannot appear in a typedef. */
9128 constructor_possible_p = false;
9129 /* The "typedef" keyword can only occur in a declaration; we
9130 may as well commit at this point. */
9131 cp_parser_commit_to_tentative_parse (parser);
9133 if (decl_specs->storage_class != sc_none)
9134 decl_specs->conflicting_specifiers_p = true;
9135 break;
9137 /* storage-class-specifier:
9138 auto
9139 register
9140 static
9141 extern
9142 mutable
9144 GNU Extension:
9145 thread */
9146 case RID_AUTO:
9147 if (cxx_dialect == cxx98)
9149 /* Consume the token. */
9150 cp_lexer_consume_token (parser->lexer);
9152 /* Complain about `auto' as a storage specifier, if
9153 we're complaining about C++0x compatibility. */
9154 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9155 " will change meaning in C++0x; please remove it");
9157 /* Set the storage class anyway. */
9158 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9159 token->location);
9161 else
9162 /* C++0x auto type-specifier. */
9163 found_decl_spec = false;
9164 break;
9166 case RID_REGISTER:
9167 case RID_STATIC:
9168 case RID_EXTERN:
9169 case RID_MUTABLE:
9170 /* Consume the token. */
9171 cp_lexer_consume_token (parser->lexer);
9172 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9173 token->location);
9174 break;
9175 case RID_THREAD:
9176 /* Consume the token. */
9177 cp_lexer_consume_token (parser->lexer);
9178 ++decl_specs->specs[(int) ds_thread];
9179 break;
9181 default:
9182 /* We did not yet find a decl-specifier yet. */
9183 found_decl_spec = false;
9184 break;
9187 /* Constructors are a special case. The `S' in `S()' is not a
9188 decl-specifier; it is the beginning of the declarator. */
9189 constructor_p
9190 = (!found_decl_spec
9191 && constructor_possible_p
9192 && (cp_parser_constructor_declarator_p
9193 (parser, decl_specs->specs[(int) ds_friend] != 0)));
9195 /* If we don't have a DECL_SPEC yet, then we must be looking at
9196 a type-specifier. */
9197 if (!found_decl_spec && !constructor_p)
9199 int decl_spec_declares_class_or_enum;
9200 bool is_cv_qualifier;
9201 tree type_spec;
9203 type_spec
9204 = cp_parser_type_specifier (parser, flags,
9205 decl_specs,
9206 /*is_declaration=*/true,
9207 &decl_spec_declares_class_or_enum,
9208 &is_cv_qualifier);
9209 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
9211 /* If this type-specifier referenced a user-defined type
9212 (a typedef, class-name, etc.), then we can't allow any
9213 more such type-specifiers henceforth.
9215 [dcl.spec]
9217 The longest sequence of decl-specifiers that could
9218 possibly be a type name is taken as the
9219 decl-specifier-seq of a declaration. The sequence shall
9220 be self-consistent as described below.
9222 [dcl.type]
9224 As a general rule, at most one type-specifier is allowed
9225 in the complete decl-specifier-seq of a declaration. The
9226 only exceptions are the following:
9228 -- const or volatile can be combined with any other
9229 type-specifier.
9231 -- signed or unsigned can be combined with char, long,
9232 short, or int.
9234 -- ..
9236 Example:
9238 typedef char* Pc;
9239 void g (const int Pc);
9241 Here, Pc is *not* part of the decl-specifier seq; it's
9242 the declarator. Therefore, once we see a type-specifier
9243 (other than a cv-qualifier), we forbid any additional
9244 user-defined types. We *do* still allow things like `int
9245 int' to be considered a decl-specifier-seq, and issue the
9246 error message later. */
9247 if (type_spec && !is_cv_qualifier)
9248 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
9249 /* A constructor declarator cannot follow a type-specifier. */
9250 if (type_spec)
9252 constructor_possible_p = false;
9253 found_decl_spec = true;
9254 if (!is_cv_qualifier)
9255 decl_specs->any_type_specifiers_p = true;
9259 /* If we still do not have a DECL_SPEC, then there are no more
9260 decl-specifiers. */
9261 if (!found_decl_spec)
9262 break;
9264 decl_specs->any_specifiers_p = true;
9265 /* After we see one decl-specifier, further decl-specifiers are
9266 always optional. */
9267 flags |= CP_PARSER_FLAGS_OPTIONAL;
9270 cp_parser_check_decl_spec (decl_specs, start_token->location);
9272 /* Don't allow a friend specifier with a class definition. */
9273 if (decl_specs->specs[(int) ds_friend] != 0
9274 && (*declares_class_or_enum & 2))
9275 error_at (start_token->location,
9276 "class definition may not be declared a friend");
9279 /* Parse an (optional) storage-class-specifier.
9281 storage-class-specifier:
9282 auto
9283 register
9284 static
9285 extern
9286 mutable
9288 GNU Extension:
9290 storage-class-specifier:
9291 thread
9293 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
9295 static tree
9296 cp_parser_storage_class_specifier_opt (cp_parser* parser)
9298 switch (cp_lexer_peek_token (parser->lexer)->keyword)
9300 case RID_AUTO:
9301 if (cxx_dialect != cxx98)
9302 return NULL_TREE;
9303 /* Fall through for C++98. */
9305 case RID_REGISTER:
9306 case RID_STATIC:
9307 case RID_EXTERN:
9308 case RID_MUTABLE:
9309 case RID_THREAD:
9310 /* Consume the token. */
9311 return cp_lexer_consume_token (parser->lexer)->u.value;
9313 default:
9314 return NULL_TREE;
9318 /* Parse an (optional) function-specifier.
9320 function-specifier:
9321 inline
9322 virtual
9323 explicit
9325 Returns an IDENTIFIER_NODE corresponding to the keyword used.
9326 Updates DECL_SPECS, if it is non-NULL. */
9328 static tree
9329 cp_parser_function_specifier_opt (cp_parser* parser,
9330 cp_decl_specifier_seq *decl_specs)
9332 cp_token *token = cp_lexer_peek_token (parser->lexer);
9333 switch (token->keyword)
9335 case RID_INLINE:
9336 if (decl_specs)
9337 ++decl_specs->specs[(int) ds_inline];
9338 break;
9340 case RID_VIRTUAL:
9341 /* 14.5.2.3 [temp.mem]
9343 A member function template shall not be virtual. */
9344 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
9345 error_at (token->location, "templates may not be %<virtual%>");
9346 else if (decl_specs)
9347 ++decl_specs->specs[(int) ds_virtual];
9348 break;
9350 case RID_EXPLICIT:
9351 if (decl_specs)
9352 ++decl_specs->specs[(int) ds_explicit];
9353 break;
9355 default:
9356 return NULL_TREE;
9359 /* Consume the token. */
9360 return cp_lexer_consume_token (parser->lexer)->u.value;
9363 /* Parse a linkage-specification.
9365 linkage-specification:
9366 extern string-literal { declaration-seq [opt] }
9367 extern string-literal declaration */
9369 static void
9370 cp_parser_linkage_specification (cp_parser* parser)
9372 tree linkage;
9374 /* Look for the `extern' keyword. */
9375 cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
9377 /* Look for the string-literal. */
9378 linkage = cp_parser_string_literal (parser, false, false);
9380 /* Transform the literal into an identifier. If the literal is a
9381 wide-character string, or contains embedded NULs, then we can't
9382 handle it as the user wants. */
9383 if (strlen (TREE_STRING_POINTER (linkage))
9384 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
9386 cp_parser_error (parser, "invalid linkage-specification");
9387 /* Assume C++ linkage. */
9388 linkage = lang_name_cplusplus;
9390 else
9391 linkage = get_identifier (TREE_STRING_POINTER (linkage));
9393 /* We're now using the new linkage. */
9394 push_lang_context (linkage);
9396 /* If the next token is a `{', then we're using the first
9397 production. */
9398 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9400 /* Consume the `{' token. */
9401 cp_lexer_consume_token (parser->lexer);
9402 /* Parse the declarations. */
9403 cp_parser_declaration_seq_opt (parser);
9404 /* Look for the closing `}'. */
9405 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
9407 /* Otherwise, there's just one declaration. */
9408 else
9410 bool saved_in_unbraced_linkage_specification_p;
9412 saved_in_unbraced_linkage_specification_p
9413 = parser->in_unbraced_linkage_specification_p;
9414 parser->in_unbraced_linkage_specification_p = true;
9415 cp_parser_declaration (parser);
9416 parser->in_unbraced_linkage_specification_p
9417 = saved_in_unbraced_linkage_specification_p;
9420 /* We're done with the linkage-specification. */
9421 pop_lang_context ();
9424 /* Parse a static_assert-declaration.
9426 static_assert-declaration:
9427 static_assert ( constant-expression , string-literal ) ;
9429 If MEMBER_P, this static_assert is a class member. */
9431 static void
9432 cp_parser_static_assert(cp_parser *parser, bool member_p)
9434 tree condition;
9435 tree message;
9436 cp_token *token;
9437 location_t saved_loc;
9439 /* Peek at the `static_assert' token so we can keep track of exactly
9440 where the static assertion started. */
9441 token = cp_lexer_peek_token (parser->lexer);
9442 saved_loc = token->location;
9444 /* Look for the `static_assert' keyword. */
9445 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
9446 "%<static_assert%>"))
9447 return;
9449 /* We know we are in a static assertion; commit to any tentative
9450 parse. */
9451 if (cp_parser_parsing_tentatively (parser))
9452 cp_parser_commit_to_tentative_parse (parser);
9454 /* Parse the `(' starting the static assertion condition. */
9455 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
9457 /* Parse the constant-expression. */
9458 condition =
9459 cp_parser_constant_expression (parser,
9460 /*allow_non_constant_p=*/false,
9461 /*non_constant_p=*/NULL);
9463 /* Parse the separating `,'. */
9464 cp_parser_require (parser, CPP_COMMA, "%<,%>");
9466 /* Parse the string-literal message. */
9467 message = cp_parser_string_literal (parser,
9468 /*translate=*/false,
9469 /*wide_ok=*/true);
9471 /* A `)' completes the static assertion. */
9472 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
9473 cp_parser_skip_to_closing_parenthesis (parser,
9474 /*recovering=*/true,
9475 /*or_comma=*/false,
9476 /*consume_paren=*/true);
9478 /* A semicolon terminates the declaration. */
9479 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
9481 /* Complete the static assertion, which may mean either processing
9482 the static assert now or saving it for template instantiation. */
9483 finish_static_assert (condition, message, saved_loc, member_p);
9486 /* Parse a `decltype' type. Returns the type.
9488 simple-type-specifier:
9489 decltype ( expression ) */
9491 static tree
9492 cp_parser_decltype (cp_parser *parser)
9494 tree expr;
9495 bool id_expression_or_member_access_p = false;
9496 const char *saved_message;
9497 bool saved_integral_constant_expression_p;
9498 bool saved_non_integral_constant_expression_p;
9499 cp_token *id_expr_start_token;
9501 /* Look for the `decltype' token. */
9502 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
9503 return error_mark_node;
9505 /* Types cannot be defined in a `decltype' expression. Save away the
9506 old message. */
9507 saved_message = parser->type_definition_forbidden_message;
9509 /* And create the new one. */
9510 parser->type_definition_forbidden_message
9511 = G_("types may not be defined in %<decltype%> expressions");
9513 /* The restrictions on constant-expressions do not apply inside
9514 decltype expressions. */
9515 saved_integral_constant_expression_p
9516 = parser->integral_constant_expression_p;
9517 saved_non_integral_constant_expression_p
9518 = parser->non_integral_constant_expression_p;
9519 parser->integral_constant_expression_p = false;
9521 /* Do not actually evaluate the expression. */
9522 ++cp_unevaluated_operand;
9524 /* Do not warn about problems with the expression. */
9525 ++c_inhibit_evaluation_warnings;
9527 /* Parse the opening `('. */
9528 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
9529 return error_mark_node;
9531 /* First, try parsing an id-expression. */
9532 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
9533 cp_parser_parse_tentatively (parser);
9534 expr = cp_parser_id_expression (parser,
9535 /*template_keyword_p=*/false,
9536 /*check_dependency_p=*/true,
9537 /*template_p=*/NULL,
9538 /*declarator_p=*/false,
9539 /*optional_p=*/false);
9541 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
9543 bool non_integral_constant_expression_p = false;
9544 tree id_expression = expr;
9545 cp_id_kind idk;
9546 const char *error_msg;
9548 if (TREE_CODE (expr) == IDENTIFIER_NODE)
9549 /* Lookup the name we got back from the id-expression. */
9550 expr = cp_parser_lookup_name (parser, expr,
9551 none_type,
9552 /*is_template=*/false,
9553 /*is_namespace=*/false,
9554 /*check_dependency=*/true,
9555 /*ambiguous_decls=*/NULL,
9556 id_expr_start_token->location);
9558 if (expr
9559 && expr != error_mark_node
9560 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
9561 && TREE_CODE (expr) != TYPE_DECL
9562 && (TREE_CODE (expr) != BIT_NOT_EXPR
9563 || !TYPE_P (TREE_OPERAND (expr, 0)))
9564 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9566 /* Complete lookup of the id-expression. */
9567 expr = (finish_id_expression
9568 (id_expression, expr, parser->scope, &idk,
9569 /*integral_constant_expression_p=*/false,
9570 /*allow_non_integral_constant_expression_p=*/true,
9571 &non_integral_constant_expression_p,
9572 /*template_p=*/false,
9573 /*done=*/true,
9574 /*address_p=*/false,
9575 /*template_arg_p=*/false,
9576 &error_msg,
9577 id_expr_start_token->location));
9579 if (expr == error_mark_node)
9580 /* We found an id-expression, but it was something that we
9581 should not have found. This is an error, not something
9582 we can recover from, so note that we found an
9583 id-expression and we'll recover as gracefully as
9584 possible. */
9585 id_expression_or_member_access_p = true;
9588 if (expr
9589 && expr != error_mark_node
9590 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9591 /* We have an id-expression. */
9592 id_expression_or_member_access_p = true;
9595 if (!id_expression_or_member_access_p)
9597 /* Abort the id-expression parse. */
9598 cp_parser_abort_tentative_parse (parser);
9600 /* Parsing tentatively, again. */
9601 cp_parser_parse_tentatively (parser);
9603 /* Parse a class member access. */
9604 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
9605 /*cast_p=*/false,
9606 /*member_access_only_p=*/true, NULL);
9608 if (expr
9609 && expr != error_mark_node
9610 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9611 /* We have an id-expression. */
9612 id_expression_or_member_access_p = true;
9615 if (id_expression_or_member_access_p)
9616 /* We have parsed the complete id-expression or member access. */
9617 cp_parser_parse_definitely (parser);
9618 else
9620 bool saved_greater_than_is_operator_p;
9622 /* Abort our attempt to parse an id-expression or member access
9623 expression. */
9624 cp_parser_abort_tentative_parse (parser);
9626 /* Within a parenthesized expression, a `>' token is always
9627 the greater-than operator. */
9628 saved_greater_than_is_operator_p
9629 = parser->greater_than_is_operator_p;
9630 parser->greater_than_is_operator_p = true;
9632 /* Parse a full expression. */
9633 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9635 /* The `>' token might be the end of a template-id or
9636 template-parameter-list now. */
9637 parser->greater_than_is_operator_p
9638 = saved_greater_than_is_operator_p;
9641 /* Go back to evaluating expressions. */
9642 --cp_unevaluated_operand;
9643 --c_inhibit_evaluation_warnings;
9645 /* Restore the old message and the integral constant expression
9646 flags. */
9647 parser->type_definition_forbidden_message = saved_message;
9648 parser->integral_constant_expression_p
9649 = saved_integral_constant_expression_p;
9650 parser->non_integral_constant_expression_p
9651 = saved_non_integral_constant_expression_p;
9653 if (expr == error_mark_node)
9655 /* Skip everything up to the closing `)'. */
9656 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9657 /*consume_paren=*/true);
9658 return error_mark_node;
9661 /* Parse to the closing `)'. */
9662 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
9664 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9665 /*consume_paren=*/true);
9666 return error_mark_node;
9669 return finish_decltype_type (expr, id_expression_or_member_access_p);
9672 /* Special member functions [gram.special] */
9674 /* Parse a conversion-function-id.
9676 conversion-function-id:
9677 operator conversion-type-id
9679 Returns an IDENTIFIER_NODE representing the operator. */
9681 static tree
9682 cp_parser_conversion_function_id (cp_parser* parser)
9684 tree type;
9685 tree saved_scope;
9686 tree saved_qualifying_scope;
9687 tree saved_object_scope;
9688 tree pushed_scope = NULL_TREE;
9690 /* Look for the `operator' token. */
9691 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9692 return error_mark_node;
9693 /* When we parse the conversion-type-id, the current scope will be
9694 reset. However, we need that information in able to look up the
9695 conversion function later, so we save it here. */
9696 saved_scope = parser->scope;
9697 saved_qualifying_scope = parser->qualifying_scope;
9698 saved_object_scope = parser->object_scope;
9699 /* We must enter the scope of the class so that the names of
9700 entities declared within the class are available in the
9701 conversion-type-id. For example, consider:
9703 struct S {
9704 typedef int I;
9705 operator I();
9708 S::operator I() { ... }
9710 In order to see that `I' is a type-name in the definition, we
9711 must be in the scope of `S'. */
9712 if (saved_scope)
9713 pushed_scope = push_scope (saved_scope);
9714 /* Parse the conversion-type-id. */
9715 type = cp_parser_conversion_type_id (parser);
9716 /* Leave the scope of the class, if any. */
9717 if (pushed_scope)
9718 pop_scope (pushed_scope);
9719 /* Restore the saved scope. */
9720 parser->scope = saved_scope;
9721 parser->qualifying_scope = saved_qualifying_scope;
9722 parser->object_scope = saved_object_scope;
9723 /* If the TYPE is invalid, indicate failure. */
9724 if (type == error_mark_node)
9725 return error_mark_node;
9726 return mangle_conv_op_name_for_type (type);
9729 /* Parse a conversion-type-id:
9731 conversion-type-id:
9732 type-specifier-seq conversion-declarator [opt]
9734 Returns the TYPE specified. */
9736 static tree
9737 cp_parser_conversion_type_id (cp_parser* parser)
9739 tree attributes;
9740 cp_decl_specifier_seq type_specifiers;
9741 cp_declarator *declarator;
9742 tree type_specified;
9744 /* Parse the attributes. */
9745 attributes = cp_parser_attributes_opt (parser);
9746 /* Parse the type-specifiers. */
9747 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
9748 /*is_trailing_return=*/false,
9749 &type_specifiers);
9750 /* If that didn't work, stop. */
9751 if (type_specifiers.type == error_mark_node)
9752 return error_mark_node;
9753 /* Parse the conversion-declarator. */
9754 declarator = cp_parser_conversion_declarator_opt (parser);
9756 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
9757 /*initialized=*/0, &attributes);
9758 if (attributes)
9759 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
9761 /* Don't give this error when parsing tentatively. This happens to
9762 work because we always parse this definitively once. */
9763 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
9764 && type_uses_auto (type_specified))
9766 error ("invalid use of %<auto%> in conversion operator");
9767 return error_mark_node;
9770 return type_specified;
9773 /* Parse an (optional) conversion-declarator.
9775 conversion-declarator:
9776 ptr-operator conversion-declarator [opt]
9780 static cp_declarator *
9781 cp_parser_conversion_declarator_opt (cp_parser* parser)
9783 enum tree_code code;
9784 tree class_type;
9785 cp_cv_quals cv_quals;
9787 /* We don't know if there's a ptr-operator next, or not. */
9788 cp_parser_parse_tentatively (parser);
9789 /* Try the ptr-operator. */
9790 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
9791 /* If it worked, look for more conversion-declarators. */
9792 if (cp_parser_parse_definitely (parser))
9794 cp_declarator *declarator;
9796 /* Parse another optional declarator. */
9797 declarator = cp_parser_conversion_declarator_opt (parser);
9799 return cp_parser_make_indirect_declarator
9800 (code, class_type, cv_quals, declarator);
9803 return NULL;
9806 /* Parse an (optional) ctor-initializer.
9808 ctor-initializer:
9809 : mem-initializer-list
9811 Returns TRUE iff the ctor-initializer was actually present. */
9813 static bool
9814 cp_parser_ctor_initializer_opt (cp_parser* parser)
9816 /* If the next token is not a `:', then there is no
9817 ctor-initializer. */
9818 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
9820 /* Do default initialization of any bases and members. */
9821 if (DECL_CONSTRUCTOR_P (current_function_decl))
9822 finish_mem_initializers (NULL_TREE);
9824 return false;
9827 /* Consume the `:' token. */
9828 cp_lexer_consume_token (parser->lexer);
9829 /* And the mem-initializer-list. */
9830 cp_parser_mem_initializer_list (parser);
9832 return true;
9835 /* Parse a mem-initializer-list.
9837 mem-initializer-list:
9838 mem-initializer ... [opt]
9839 mem-initializer ... [opt] , mem-initializer-list */
9841 static void
9842 cp_parser_mem_initializer_list (cp_parser* parser)
9844 tree mem_initializer_list = NULL_TREE;
9845 cp_token *token = cp_lexer_peek_token (parser->lexer);
9847 /* Let the semantic analysis code know that we are starting the
9848 mem-initializer-list. */
9849 if (!DECL_CONSTRUCTOR_P (current_function_decl))
9850 error_at (token->location,
9851 "only constructors take base initializers");
9853 /* Loop through the list. */
9854 while (true)
9856 tree mem_initializer;
9858 token = cp_lexer_peek_token (parser->lexer);
9859 /* Parse the mem-initializer. */
9860 mem_initializer = cp_parser_mem_initializer (parser);
9861 /* If the next token is a `...', we're expanding member initializers. */
9862 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9864 /* Consume the `...'. */
9865 cp_lexer_consume_token (parser->lexer);
9867 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9868 can be expanded but members cannot. */
9869 if (mem_initializer != error_mark_node
9870 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9872 error_at (token->location,
9873 "cannot expand initializer for member %<%D%>",
9874 TREE_PURPOSE (mem_initializer));
9875 mem_initializer = error_mark_node;
9878 /* Construct the pack expansion type. */
9879 if (mem_initializer != error_mark_node)
9880 mem_initializer = make_pack_expansion (mem_initializer);
9882 /* Add it to the list, unless it was erroneous. */
9883 if (mem_initializer != error_mark_node)
9885 TREE_CHAIN (mem_initializer) = mem_initializer_list;
9886 mem_initializer_list = mem_initializer;
9888 /* If the next token is not a `,', we're done. */
9889 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9890 break;
9891 /* Consume the `,' token. */
9892 cp_lexer_consume_token (parser->lexer);
9895 /* Perform semantic analysis. */
9896 if (DECL_CONSTRUCTOR_P (current_function_decl))
9897 finish_mem_initializers (mem_initializer_list);
9900 /* Parse a mem-initializer.
9902 mem-initializer:
9903 mem-initializer-id ( expression-list [opt] )
9904 mem-initializer-id braced-init-list
9906 GNU extension:
9908 mem-initializer:
9909 ( expression-list [opt] )
9911 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
9912 class) or FIELD_DECL (for a non-static data member) to initialize;
9913 the TREE_VALUE is the expression-list. An empty initialization
9914 list is represented by void_list_node. */
9916 static tree
9917 cp_parser_mem_initializer (cp_parser* parser)
9919 tree mem_initializer_id;
9920 tree expression_list;
9921 tree member;
9922 cp_token *token = cp_lexer_peek_token (parser->lexer);
9924 /* Find out what is being initialized. */
9925 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9927 permerror (token->location,
9928 "anachronistic old-style base class initializer");
9929 mem_initializer_id = NULL_TREE;
9931 else
9933 mem_initializer_id = cp_parser_mem_initializer_id (parser);
9934 if (mem_initializer_id == error_mark_node)
9935 return mem_initializer_id;
9937 member = expand_member_init (mem_initializer_id);
9938 if (member && !DECL_P (member))
9939 in_base_initializer = 1;
9941 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9943 bool expr_non_constant_p;
9944 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9945 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9946 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9947 expression_list = build_tree_list (NULL_TREE, expression_list);
9949 else
9951 VEC(tree,gc)* vec;
9952 vec = cp_parser_parenthesized_expression_list (parser, false,
9953 /*cast_p=*/false,
9954 /*allow_expansion_p=*/true,
9955 /*non_constant_p=*/NULL);
9956 if (vec == NULL)
9957 return error_mark_node;
9958 expression_list = build_tree_list_vec (vec);
9959 release_tree_vector (vec);
9962 if (expression_list == error_mark_node)
9963 return error_mark_node;
9964 if (!expression_list)
9965 expression_list = void_type_node;
9967 in_base_initializer = 0;
9969 return member ? build_tree_list (member, expression_list) : error_mark_node;
9972 /* Parse a mem-initializer-id.
9974 mem-initializer-id:
9975 :: [opt] nested-name-specifier [opt] class-name
9976 identifier
9978 Returns a TYPE indicating the class to be initializer for the first
9979 production. Returns an IDENTIFIER_NODE indicating the data member
9980 to be initialized for the second production. */
9982 static tree
9983 cp_parser_mem_initializer_id (cp_parser* parser)
9985 bool global_scope_p;
9986 bool nested_name_specifier_p;
9987 bool template_p = false;
9988 tree id;
9990 cp_token *token = cp_lexer_peek_token (parser->lexer);
9992 /* `typename' is not allowed in this context ([temp.res]). */
9993 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
9995 error_at (token->location,
9996 "keyword %<typename%> not allowed in this context (a qualified "
9997 "member initializer is implicitly a type)");
9998 cp_lexer_consume_token (parser->lexer);
10000 /* Look for the optional `::' operator. */
10001 global_scope_p
10002 = (cp_parser_global_scope_opt (parser,
10003 /*current_scope_valid_p=*/false)
10004 != NULL_TREE);
10005 /* Look for the optional nested-name-specifier. The simplest way to
10006 implement:
10008 [temp.res]
10010 The keyword `typename' is not permitted in a base-specifier or
10011 mem-initializer; in these contexts a qualified name that
10012 depends on a template-parameter is implicitly assumed to be a
10013 type name.
10015 is to assume that we have seen the `typename' keyword at this
10016 point. */
10017 nested_name_specifier_p
10018 = (cp_parser_nested_name_specifier_opt (parser,
10019 /*typename_keyword_p=*/true,
10020 /*check_dependency_p=*/true,
10021 /*type_p=*/true,
10022 /*is_declaration=*/true)
10023 != NULL_TREE);
10024 if (nested_name_specifier_p)
10025 template_p = cp_parser_optional_template_keyword (parser);
10026 /* If there is a `::' operator or a nested-name-specifier, then we
10027 are definitely looking for a class-name. */
10028 if (global_scope_p || nested_name_specifier_p)
10029 return cp_parser_class_name (parser,
10030 /*typename_keyword_p=*/true,
10031 /*template_keyword_p=*/template_p,
10032 typename_type,
10033 /*check_dependency_p=*/true,
10034 /*class_head_p=*/false,
10035 /*is_declaration=*/true);
10036 /* Otherwise, we could also be looking for an ordinary identifier. */
10037 cp_parser_parse_tentatively (parser);
10038 /* Try a class-name. */
10039 id = cp_parser_class_name (parser,
10040 /*typename_keyword_p=*/true,
10041 /*template_keyword_p=*/false,
10042 none_type,
10043 /*check_dependency_p=*/true,
10044 /*class_head_p=*/false,
10045 /*is_declaration=*/true);
10046 /* If we found one, we're done. */
10047 if (cp_parser_parse_definitely (parser))
10048 return id;
10049 /* Otherwise, look for an ordinary identifier. */
10050 return cp_parser_identifier (parser);
10053 /* Overloading [gram.over] */
10055 /* Parse an operator-function-id.
10057 operator-function-id:
10058 operator operator
10060 Returns an IDENTIFIER_NODE for the operator which is a
10061 human-readable spelling of the identifier, e.g., `operator +'. */
10063 static tree
10064 cp_parser_operator_function_id (cp_parser* parser)
10066 /* Look for the `operator' keyword. */
10067 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
10068 return error_mark_node;
10069 /* And then the name of the operator itself. */
10070 return cp_parser_operator (parser);
10073 /* Parse an operator.
10075 operator:
10076 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10077 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10078 || ++ -- , ->* -> () []
10080 GNU Extensions:
10082 operator:
10083 <? >? <?= >?=
10085 Returns an IDENTIFIER_NODE for the operator which is a
10086 human-readable spelling of the identifier, e.g., `operator +'. */
10088 static tree
10089 cp_parser_operator (cp_parser* parser)
10091 tree id = NULL_TREE;
10092 cp_token *token;
10094 /* Peek at the next token. */
10095 token = cp_lexer_peek_token (parser->lexer);
10096 /* Figure out which operator we have. */
10097 switch (token->type)
10099 case CPP_KEYWORD:
10101 enum tree_code op;
10103 /* The keyword should be either `new' or `delete'. */
10104 if (token->keyword == RID_NEW)
10105 op = NEW_EXPR;
10106 else if (token->keyword == RID_DELETE)
10107 op = DELETE_EXPR;
10108 else
10109 break;
10111 /* Consume the `new' or `delete' token. */
10112 cp_lexer_consume_token (parser->lexer);
10114 /* Peek at the next token. */
10115 token = cp_lexer_peek_token (parser->lexer);
10116 /* If it's a `[' token then this is the array variant of the
10117 operator. */
10118 if (token->type == CPP_OPEN_SQUARE)
10120 /* Consume the `[' token. */
10121 cp_lexer_consume_token (parser->lexer);
10122 /* Look for the `]' token. */
10123 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
10124 id = ansi_opname (op == NEW_EXPR
10125 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10127 /* Otherwise, we have the non-array variant. */
10128 else
10129 id = ansi_opname (op);
10131 return id;
10134 case CPP_PLUS:
10135 id = ansi_opname (PLUS_EXPR);
10136 break;
10138 case CPP_MINUS:
10139 id = ansi_opname (MINUS_EXPR);
10140 break;
10142 case CPP_MULT:
10143 id = ansi_opname (MULT_EXPR);
10144 break;
10146 case CPP_DIV:
10147 id = ansi_opname (TRUNC_DIV_EXPR);
10148 break;
10150 case CPP_MOD:
10151 id = ansi_opname (TRUNC_MOD_EXPR);
10152 break;
10154 case CPP_XOR:
10155 id = ansi_opname (BIT_XOR_EXPR);
10156 break;
10158 case CPP_AND:
10159 id = ansi_opname (BIT_AND_EXPR);
10160 break;
10162 case CPP_OR:
10163 id = ansi_opname (BIT_IOR_EXPR);
10164 break;
10166 case CPP_COMPL:
10167 id = ansi_opname (BIT_NOT_EXPR);
10168 break;
10170 case CPP_NOT:
10171 id = ansi_opname (TRUTH_NOT_EXPR);
10172 break;
10174 case CPP_EQ:
10175 id = ansi_assopname (NOP_EXPR);
10176 break;
10178 case CPP_LESS:
10179 id = ansi_opname (LT_EXPR);
10180 break;
10182 case CPP_GREATER:
10183 id = ansi_opname (GT_EXPR);
10184 break;
10186 case CPP_PLUS_EQ:
10187 id = ansi_assopname (PLUS_EXPR);
10188 break;
10190 case CPP_MINUS_EQ:
10191 id = ansi_assopname (MINUS_EXPR);
10192 break;
10194 case CPP_MULT_EQ:
10195 id = ansi_assopname (MULT_EXPR);
10196 break;
10198 case CPP_DIV_EQ:
10199 id = ansi_assopname (TRUNC_DIV_EXPR);
10200 break;
10202 case CPP_MOD_EQ:
10203 id = ansi_assopname (TRUNC_MOD_EXPR);
10204 break;
10206 case CPP_XOR_EQ:
10207 id = ansi_assopname (BIT_XOR_EXPR);
10208 break;
10210 case CPP_AND_EQ:
10211 id = ansi_assopname (BIT_AND_EXPR);
10212 break;
10214 case CPP_OR_EQ:
10215 id = ansi_assopname (BIT_IOR_EXPR);
10216 break;
10218 case CPP_LSHIFT:
10219 id = ansi_opname (LSHIFT_EXPR);
10220 break;
10222 case CPP_RSHIFT:
10223 id = ansi_opname (RSHIFT_EXPR);
10224 break;
10226 case CPP_LSHIFT_EQ:
10227 id = ansi_assopname (LSHIFT_EXPR);
10228 break;
10230 case CPP_RSHIFT_EQ:
10231 id = ansi_assopname (RSHIFT_EXPR);
10232 break;
10234 case CPP_EQ_EQ:
10235 id = ansi_opname (EQ_EXPR);
10236 break;
10238 case CPP_NOT_EQ:
10239 id = ansi_opname (NE_EXPR);
10240 break;
10242 case CPP_LESS_EQ:
10243 id = ansi_opname (LE_EXPR);
10244 break;
10246 case CPP_GREATER_EQ:
10247 id = ansi_opname (GE_EXPR);
10248 break;
10250 case CPP_AND_AND:
10251 id = ansi_opname (TRUTH_ANDIF_EXPR);
10252 break;
10254 case CPP_OR_OR:
10255 id = ansi_opname (TRUTH_ORIF_EXPR);
10256 break;
10258 case CPP_PLUS_PLUS:
10259 id = ansi_opname (POSTINCREMENT_EXPR);
10260 break;
10262 case CPP_MINUS_MINUS:
10263 id = ansi_opname (PREDECREMENT_EXPR);
10264 break;
10266 case CPP_COMMA:
10267 id = ansi_opname (COMPOUND_EXPR);
10268 break;
10270 case CPP_DEREF_STAR:
10271 id = ansi_opname (MEMBER_REF);
10272 break;
10274 case CPP_DEREF:
10275 id = ansi_opname (COMPONENT_REF);
10276 break;
10278 case CPP_OPEN_PAREN:
10279 /* Consume the `('. */
10280 cp_lexer_consume_token (parser->lexer);
10281 /* Look for the matching `)'. */
10282 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
10283 return ansi_opname (CALL_EXPR);
10285 case CPP_OPEN_SQUARE:
10286 /* Consume the `['. */
10287 cp_lexer_consume_token (parser->lexer);
10288 /* Look for the matching `]'. */
10289 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
10290 return ansi_opname (ARRAY_REF);
10292 default:
10293 /* Anything else is an error. */
10294 break;
10297 /* If we have selected an identifier, we need to consume the
10298 operator token. */
10299 if (id)
10300 cp_lexer_consume_token (parser->lexer);
10301 /* Otherwise, no valid operator name was present. */
10302 else
10304 cp_parser_error (parser, "expected operator");
10305 id = error_mark_node;
10308 return id;
10311 /* Parse a template-declaration.
10313 template-declaration:
10314 export [opt] template < template-parameter-list > declaration
10316 If MEMBER_P is TRUE, this template-declaration occurs within a
10317 class-specifier.
10319 The grammar rule given by the standard isn't correct. What
10320 is really meant is:
10322 template-declaration:
10323 export [opt] template-parameter-list-seq
10324 decl-specifier-seq [opt] init-declarator [opt] ;
10325 export [opt] template-parameter-list-seq
10326 function-definition
10328 template-parameter-list-seq:
10329 template-parameter-list-seq [opt]
10330 template < template-parameter-list > */
10332 static void
10333 cp_parser_template_declaration (cp_parser* parser, bool member_p)
10335 /* Check for `export'. */
10336 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
10338 /* Consume the `export' token. */
10339 cp_lexer_consume_token (parser->lexer);
10340 /* Warn that we do not support `export'. */
10341 warning (0, "keyword %<export%> not implemented, and will be ignored");
10344 cp_parser_template_declaration_after_export (parser, member_p);
10347 /* Parse a template-parameter-list.
10349 template-parameter-list:
10350 template-parameter
10351 template-parameter-list , template-parameter
10353 Returns a TREE_LIST. Each node represents a template parameter.
10354 The nodes are connected via their TREE_CHAINs. */
10356 static tree
10357 cp_parser_template_parameter_list (cp_parser* parser)
10359 tree parameter_list = NULL_TREE;
10361 begin_template_parm_list ();
10362 while (true)
10364 tree parameter;
10365 bool is_non_type;
10366 bool is_parameter_pack;
10367 location_t parm_loc;
10369 /* Parse the template-parameter. */
10370 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
10371 parameter = cp_parser_template_parameter (parser,
10372 &is_non_type,
10373 &is_parameter_pack);
10374 /* Add it to the list. */
10375 if (parameter != error_mark_node)
10376 parameter_list = process_template_parm (parameter_list,
10377 parm_loc,
10378 parameter,
10379 is_non_type,
10380 is_parameter_pack);
10381 else
10383 tree err_parm = build_tree_list (parameter, parameter);
10384 TREE_VALUE (err_parm) = error_mark_node;
10385 parameter_list = chainon (parameter_list, err_parm);
10388 /* If the next token is not a `,', we're done. */
10389 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10390 break;
10391 /* Otherwise, consume the `,' token. */
10392 cp_lexer_consume_token (parser->lexer);
10395 return end_template_parm_list (parameter_list);
10398 /* Parse a template-parameter.
10400 template-parameter:
10401 type-parameter
10402 parameter-declaration
10404 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
10405 the parameter. The TREE_PURPOSE is the default value, if any.
10406 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
10407 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
10408 set to true iff this parameter is a parameter pack. */
10410 static tree
10411 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
10412 bool *is_parameter_pack)
10414 cp_token *token;
10415 cp_parameter_declarator *parameter_declarator;
10416 cp_declarator *id_declarator;
10417 tree parm;
10419 /* Assume it is a type parameter or a template parameter. */
10420 *is_non_type = false;
10421 /* Assume it not a parameter pack. */
10422 *is_parameter_pack = false;
10423 /* Peek at the next token. */
10424 token = cp_lexer_peek_token (parser->lexer);
10425 /* If it is `class' or `template', we have a type-parameter. */
10426 if (token->keyword == RID_TEMPLATE)
10427 return cp_parser_type_parameter (parser, is_parameter_pack);
10428 /* If it is `class' or `typename' we do not know yet whether it is a
10429 type parameter or a non-type parameter. Consider:
10431 template <typename T, typename T::X X> ...
10435 template <class C, class D*> ...
10437 Here, the first parameter is a type parameter, and the second is
10438 a non-type parameter. We can tell by looking at the token after
10439 the identifier -- if it is a `,', `=', or `>' then we have a type
10440 parameter. */
10441 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
10443 /* Peek at the token after `class' or `typename'. */
10444 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10445 /* If it's an ellipsis, we have a template type parameter
10446 pack. */
10447 if (token->type == CPP_ELLIPSIS)
10448 return cp_parser_type_parameter (parser, is_parameter_pack);
10449 /* If it's an identifier, skip it. */
10450 if (token->type == CPP_NAME)
10451 token = cp_lexer_peek_nth_token (parser->lexer, 3);
10452 /* Now, see if the token looks like the end of a template
10453 parameter. */
10454 if (token->type == CPP_COMMA
10455 || token->type == CPP_EQ
10456 || token->type == CPP_GREATER)
10457 return cp_parser_type_parameter (parser, is_parameter_pack);
10460 /* Otherwise, it is a non-type parameter.
10462 [temp.param]
10464 When parsing a default template-argument for a non-type
10465 template-parameter, the first non-nested `>' is taken as the end
10466 of the template parameter-list rather than a greater-than
10467 operator. */
10468 *is_non_type = true;
10469 parameter_declarator
10470 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
10471 /*parenthesized_p=*/NULL);
10473 /* If the parameter declaration is marked as a parameter pack, set
10474 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
10475 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
10476 grokdeclarator. */
10477 if (parameter_declarator
10478 && parameter_declarator->declarator
10479 && parameter_declarator->declarator->parameter_pack_p)
10481 *is_parameter_pack = true;
10482 parameter_declarator->declarator->parameter_pack_p = false;
10485 /* If the next token is an ellipsis, and we don't already have it
10486 marked as a parameter pack, then we have a parameter pack (that
10487 has no declarator). */
10488 if (!*is_parameter_pack
10489 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
10490 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
10492 /* Consume the `...'. */
10493 cp_lexer_consume_token (parser->lexer);
10494 maybe_warn_variadic_templates ();
10496 *is_parameter_pack = true;
10498 /* We might end up with a pack expansion as the type of the non-type
10499 template parameter, in which case this is a non-type template
10500 parameter pack. */
10501 else if (parameter_declarator
10502 && parameter_declarator->decl_specifiers.type
10503 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
10505 *is_parameter_pack = true;
10506 parameter_declarator->decl_specifiers.type =
10507 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
10510 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10512 /* Parameter packs cannot have default arguments. However, a
10513 user may try to do so, so we'll parse them and give an
10514 appropriate diagnostic here. */
10516 /* Consume the `='. */
10517 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
10518 cp_lexer_consume_token (parser->lexer);
10520 /* Find the name of the parameter pack. */
10521 id_declarator = parameter_declarator->declarator;
10522 while (id_declarator && id_declarator->kind != cdk_id)
10523 id_declarator = id_declarator->declarator;
10525 if (id_declarator && id_declarator->kind == cdk_id)
10526 error_at (start_token->location,
10527 "template parameter pack %qD cannot have a default argument",
10528 id_declarator->u.id.unqualified_name);
10529 else
10530 error_at (start_token->location,
10531 "template parameter pack cannot have a default argument");
10533 /* Parse the default argument, but throw away the result. */
10534 cp_parser_default_argument (parser, /*template_parm_p=*/true);
10537 parm = grokdeclarator (parameter_declarator->declarator,
10538 &parameter_declarator->decl_specifiers,
10539 TPARM, /*initialized=*/0,
10540 /*attrlist=*/NULL);
10541 if (parm == error_mark_node)
10542 return error_mark_node;
10544 return build_tree_list (parameter_declarator->default_argument, parm);
10547 /* Parse a type-parameter.
10549 type-parameter:
10550 class identifier [opt]
10551 class identifier [opt] = type-id
10552 typename identifier [opt]
10553 typename identifier [opt] = type-id
10554 template < template-parameter-list > class identifier [opt]
10555 template < template-parameter-list > class identifier [opt]
10556 = id-expression
10558 GNU Extension (variadic templates):
10560 type-parameter:
10561 class ... identifier [opt]
10562 typename ... identifier [opt]
10564 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
10565 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
10566 the declaration of the parameter.
10568 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
10570 static tree
10571 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
10573 cp_token *token;
10574 tree parameter;
10576 /* Look for a keyword to tell us what kind of parameter this is. */
10577 token = cp_parser_require (parser, CPP_KEYWORD,
10578 "%<class%>, %<typename%>, or %<template%>");
10579 if (!token)
10580 return error_mark_node;
10582 switch (token->keyword)
10584 case RID_CLASS:
10585 case RID_TYPENAME:
10587 tree identifier;
10588 tree default_argument;
10590 /* If the next token is an ellipsis, we have a template
10591 argument pack. */
10592 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10594 /* Consume the `...' token. */
10595 cp_lexer_consume_token (parser->lexer);
10596 maybe_warn_variadic_templates ();
10598 *is_parameter_pack = true;
10601 /* If the next token is an identifier, then it names the
10602 parameter. */
10603 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10604 identifier = cp_parser_identifier (parser);
10605 else
10606 identifier = NULL_TREE;
10608 /* Create the parameter. */
10609 parameter = finish_template_type_parm (class_type_node, identifier);
10611 /* If the next token is an `=', we have a default argument. */
10612 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10614 /* Consume the `=' token. */
10615 cp_lexer_consume_token (parser->lexer);
10616 /* Parse the default-argument. */
10617 push_deferring_access_checks (dk_no_deferred);
10618 default_argument = cp_parser_type_id (parser);
10620 /* Template parameter packs cannot have default
10621 arguments. */
10622 if (*is_parameter_pack)
10624 if (identifier)
10625 error_at (token->location,
10626 "template parameter pack %qD cannot have a "
10627 "default argument", identifier);
10628 else
10629 error_at (token->location,
10630 "template parameter packs cannot have "
10631 "default arguments");
10632 default_argument = NULL_TREE;
10634 pop_deferring_access_checks ();
10636 else
10637 default_argument = NULL_TREE;
10639 /* Create the combined representation of the parameter and the
10640 default argument. */
10641 parameter = build_tree_list (default_argument, parameter);
10643 break;
10645 case RID_TEMPLATE:
10647 tree identifier;
10648 tree default_argument;
10650 /* Look for the `<'. */
10651 cp_parser_require (parser, CPP_LESS, "%<<%>");
10652 /* Parse the template-parameter-list. */
10653 cp_parser_template_parameter_list (parser);
10654 /* Look for the `>'. */
10655 cp_parser_require (parser, CPP_GREATER, "%<>%>");
10656 /* Look for the `class' keyword. */
10657 cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
10658 /* If the next token is an ellipsis, we have a template
10659 argument pack. */
10660 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10662 /* Consume the `...' token. */
10663 cp_lexer_consume_token (parser->lexer);
10664 maybe_warn_variadic_templates ();
10666 *is_parameter_pack = true;
10668 /* If the next token is an `=', then there is a
10669 default-argument. If the next token is a `>', we are at
10670 the end of the parameter-list. If the next token is a `,',
10671 then we are at the end of this parameter. */
10672 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10673 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
10674 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10676 identifier = cp_parser_identifier (parser);
10677 /* Treat invalid names as if the parameter were nameless. */
10678 if (identifier == error_mark_node)
10679 identifier = NULL_TREE;
10681 else
10682 identifier = NULL_TREE;
10684 /* Create the template parameter. */
10685 parameter = finish_template_template_parm (class_type_node,
10686 identifier);
10688 /* If the next token is an `=', then there is a
10689 default-argument. */
10690 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10692 bool is_template;
10694 /* Consume the `='. */
10695 cp_lexer_consume_token (parser->lexer);
10696 /* Parse the id-expression. */
10697 push_deferring_access_checks (dk_no_deferred);
10698 /* save token before parsing the id-expression, for error
10699 reporting */
10700 token = cp_lexer_peek_token (parser->lexer);
10701 default_argument
10702 = cp_parser_id_expression (parser,
10703 /*template_keyword_p=*/false,
10704 /*check_dependency_p=*/true,
10705 /*template_p=*/&is_template,
10706 /*declarator_p=*/false,
10707 /*optional_p=*/false);
10708 if (TREE_CODE (default_argument) == TYPE_DECL)
10709 /* If the id-expression was a template-id that refers to
10710 a template-class, we already have the declaration here,
10711 so no further lookup is needed. */
10713 else
10714 /* Look up the name. */
10715 default_argument
10716 = cp_parser_lookup_name (parser, default_argument,
10717 none_type,
10718 /*is_template=*/is_template,
10719 /*is_namespace=*/false,
10720 /*check_dependency=*/true,
10721 /*ambiguous_decls=*/NULL,
10722 token->location);
10723 /* See if the default argument is valid. */
10724 default_argument
10725 = check_template_template_default_arg (default_argument);
10727 /* Template parameter packs cannot have default
10728 arguments. */
10729 if (*is_parameter_pack)
10731 if (identifier)
10732 error_at (token->location,
10733 "template parameter pack %qD cannot "
10734 "have a default argument",
10735 identifier);
10736 else
10737 error_at (token->location, "template parameter packs cannot "
10738 "have default arguments");
10739 default_argument = NULL_TREE;
10741 pop_deferring_access_checks ();
10743 else
10744 default_argument = NULL_TREE;
10746 /* Create the combined representation of the parameter and the
10747 default argument. */
10748 parameter = build_tree_list (default_argument, parameter);
10750 break;
10752 default:
10753 gcc_unreachable ();
10754 break;
10757 return parameter;
10760 /* Parse a template-id.
10762 template-id:
10763 template-name < template-argument-list [opt] >
10765 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
10766 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
10767 returned. Otherwise, if the template-name names a function, or set
10768 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
10769 names a class, returns a TYPE_DECL for the specialization.
10771 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
10772 uninstantiated templates. */
10774 static tree
10775 cp_parser_template_id (cp_parser *parser,
10776 bool template_keyword_p,
10777 bool check_dependency_p,
10778 bool is_declaration)
10780 int i;
10781 tree templ;
10782 tree arguments;
10783 tree template_id;
10784 cp_token_position start_of_id = 0;
10785 deferred_access_check *chk;
10786 VEC (deferred_access_check,gc) *access_check;
10787 cp_token *next_token = NULL, *next_token_2 = NULL;
10788 bool is_identifier;
10790 /* If the next token corresponds to a template-id, there is no need
10791 to reparse it. */
10792 next_token = cp_lexer_peek_token (parser->lexer);
10793 if (next_token->type == CPP_TEMPLATE_ID)
10795 struct tree_check *check_value;
10797 /* Get the stored value. */
10798 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
10799 /* Perform any access checks that were deferred. */
10800 access_check = check_value->checks;
10801 if (access_check)
10803 for (i = 0 ;
10804 VEC_iterate (deferred_access_check, access_check, i, chk) ;
10805 ++i)
10807 perform_or_defer_access_check (chk->binfo,
10808 chk->decl,
10809 chk->diag_decl);
10812 /* Return the stored value. */
10813 return check_value->value;
10816 /* Avoid performing name lookup if there is no possibility of
10817 finding a template-id. */
10818 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
10819 || (next_token->type == CPP_NAME
10820 && !cp_parser_nth_token_starts_template_argument_list_p
10821 (parser, 2)))
10823 cp_parser_error (parser, "expected template-id");
10824 return error_mark_node;
10827 /* Remember where the template-id starts. */
10828 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
10829 start_of_id = cp_lexer_token_position (parser->lexer, false);
10831 push_deferring_access_checks (dk_deferred);
10833 /* Parse the template-name. */
10834 is_identifier = false;
10835 templ = cp_parser_template_name (parser, template_keyword_p,
10836 check_dependency_p,
10837 is_declaration,
10838 &is_identifier);
10839 if (templ == error_mark_node || is_identifier)
10841 pop_deferring_access_checks ();
10842 return templ;
10845 /* If we find the sequence `[:' after a template-name, it's probably
10846 a digraph-typo for `< ::'. Substitute the tokens and check if we can
10847 parse correctly the argument list. */
10848 next_token = cp_lexer_peek_token (parser->lexer);
10849 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10850 if (next_token->type == CPP_OPEN_SQUARE
10851 && next_token->flags & DIGRAPH
10852 && next_token_2->type == CPP_COLON
10853 && !(next_token_2->flags & PREV_WHITE))
10855 cp_parser_parse_tentatively (parser);
10856 /* Change `:' into `::'. */
10857 next_token_2->type = CPP_SCOPE;
10858 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
10859 CPP_LESS. */
10860 cp_lexer_consume_token (parser->lexer);
10862 /* Parse the arguments. */
10863 arguments = cp_parser_enclosed_template_argument_list (parser);
10864 if (!cp_parser_parse_definitely (parser))
10866 /* If we couldn't parse an argument list, then we revert our changes
10867 and return simply an error. Maybe this is not a template-id
10868 after all. */
10869 next_token_2->type = CPP_COLON;
10870 cp_parser_error (parser, "expected %<<%>");
10871 pop_deferring_access_checks ();
10872 return error_mark_node;
10874 /* Otherwise, emit an error about the invalid digraph, but continue
10875 parsing because we got our argument list. */
10876 if (permerror (next_token->location,
10877 "%<<::%> cannot begin a template-argument list"))
10879 static bool hint = false;
10880 inform (next_token->location,
10881 "%<<:%> is an alternate spelling for %<[%>."
10882 " Insert whitespace between %<<%> and %<::%>");
10883 if (!hint && !flag_permissive)
10885 inform (next_token->location, "(if you use %<-fpermissive%>"
10886 " G++ will accept your code)");
10887 hint = true;
10891 else
10893 /* Look for the `<' that starts the template-argument-list. */
10894 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10896 pop_deferring_access_checks ();
10897 return error_mark_node;
10899 /* Parse the arguments. */
10900 arguments = cp_parser_enclosed_template_argument_list (parser);
10903 /* Build a representation of the specialization. */
10904 if (TREE_CODE (templ) == IDENTIFIER_NODE)
10905 template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10906 else if (DECL_CLASS_TEMPLATE_P (templ)
10907 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10909 bool entering_scope;
10910 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10911 template (rather than some instantiation thereof) only if
10912 is not nested within some other construct. For example, in
10913 "template <typename T> void f(T) { A<T>::", A<T> is just an
10914 instantiation of A. */
10915 entering_scope = (template_parm_scope_p ()
10916 && cp_lexer_next_token_is (parser->lexer,
10917 CPP_SCOPE));
10918 template_id
10919 = finish_template_type (templ, arguments, entering_scope);
10921 else
10923 /* If it's not a class-template or a template-template, it should be
10924 a function-template. */
10925 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10926 || TREE_CODE (templ) == OVERLOAD
10927 || BASELINK_P (templ)));
10929 template_id = lookup_template_function (templ, arguments);
10932 /* If parsing tentatively, replace the sequence of tokens that makes
10933 up the template-id with a CPP_TEMPLATE_ID token. That way,
10934 should we re-parse the token stream, we will not have to repeat
10935 the effort required to do the parse, nor will we issue duplicate
10936 error messages about problems during instantiation of the
10937 template. */
10938 if (start_of_id)
10940 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10942 /* Reset the contents of the START_OF_ID token. */
10943 token->type = CPP_TEMPLATE_ID;
10944 /* Retrieve any deferred checks. Do not pop this access checks yet
10945 so the memory will not be reclaimed during token replacing below. */
10946 token->u.tree_check_value = GGC_CNEW (struct tree_check);
10947 token->u.tree_check_value->value = template_id;
10948 token->u.tree_check_value->checks = get_deferred_access_checks ();
10949 token->keyword = RID_MAX;
10951 /* Purge all subsequent tokens. */
10952 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10954 /* ??? Can we actually assume that, if template_id ==
10955 error_mark_node, we will have issued a diagnostic to the
10956 user, as opposed to simply marking the tentative parse as
10957 failed? */
10958 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10959 error_at (token->location, "parse error in template argument list");
10962 pop_deferring_access_checks ();
10963 return template_id;
10966 /* Parse a template-name.
10968 template-name:
10969 identifier
10971 The standard should actually say:
10973 template-name:
10974 identifier
10975 operator-function-id
10977 A defect report has been filed about this issue.
10979 A conversion-function-id cannot be a template name because they cannot
10980 be part of a template-id. In fact, looking at this code:
10982 a.operator K<int>()
10984 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10985 It is impossible to call a templated conversion-function-id with an
10986 explicit argument list, since the only allowed template parameter is
10987 the type to which it is converting.
10989 If TEMPLATE_KEYWORD_P is true, then we have just seen the
10990 `template' keyword, in a construction like:
10992 T::template f<3>()
10994 In that case `f' is taken to be a template-name, even though there
10995 is no way of knowing for sure.
10997 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
10998 name refers to a set of overloaded functions, at least one of which
10999 is a template, or an IDENTIFIER_NODE with the name of the template,
11000 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
11001 names are looked up inside uninstantiated templates. */
11003 static tree
11004 cp_parser_template_name (cp_parser* parser,
11005 bool template_keyword_p,
11006 bool check_dependency_p,
11007 bool is_declaration,
11008 bool *is_identifier)
11010 tree identifier;
11011 tree decl;
11012 tree fns;
11013 cp_token *token = cp_lexer_peek_token (parser->lexer);
11015 /* If the next token is `operator', then we have either an
11016 operator-function-id or a conversion-function-id. */
11017 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
11019 /* We don't know whether we're looking at an
11020 operator-function-id or a conversion-function-id. */
11021 cp_parser_parse_tentatively (parser);
11022 /* Try an operator-function-id. */
11023 identifier = cp_parser_operator_function_id (parser);
11024 /* If that didn't work, try a conversion-function-id. */
11025 if (!cp_parser_parse_definitely (parser))
11027 cp_parser_error (parser, "expected template-name");
11028 return error_mark_node;
11031 /* Look for the identifier. */
11032 else
11033 identifier = cp_parser_identifier (parser);
11035 /* If we didn't find an identifier, we don't have a template-id. */
11036 if (identifier == error_mark_node)
11037 return error_mark_node;
11039 /* If the name immediately followed the `template' keyword, then it
11040 is a template-name. However, if the next token is not `<', then
11041 we do not treat it as a template-name, since it is not being used
11042 as part of a template-id. This enables us to handle constructs
11043 like:
11045 template <typename T> struct S { S(); };
11046 template <typename T> S<T>::S();
11048 correctly. We would treat `S' as a template -- if it were `S<T>'
11049 -- but we do not if there is no `<'. */
11051 if (processing_template_decl
11052 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
11054 /* In a declaration, in a dependent context, we pretend that the
11055 "template" keyword was present in order to improve error
11056 recovery. For example, given:
11058 template <typename T> void f(T::X<int>);
11060 we want to treat "X<int>" as a template-id. */
11061 if (is_declaration
11062 && !template_keyword_p
11063 && parser->scope && TYPE_P (parser->scope)
11064 && check_dependency_p
11065 && dependent_scope_p (parser->scope)
11066 /* Do not do this for dtors (or ctors), since they never
11067 need the template keyword before their name. */
11068 && !constructor_name_p (identifier, parser->scope))
11070 cp_token_position start = 0;
11072 /* Explain what went wrong. */
11073 error_at (token->location, "non-template %qD used as template",
11074 identifier);
11075 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
11076 parser->scope, identifier);
11077 /* If parsing tentatively, find the location of the "<" token. */
11078 if (cp_parser_simulate_error (parser))
11079 start = cp_lexer_token_position (parser->lexer, true);
11080 /* Parse the template arguments so that we can issue error
11081 messages about them. */
11082 cp_lexer_consume_token (parser->lexer);
11083 cp_parser_enclosed_template_argument_list (parser);
11084 /* Skip tokens until we find a good place from which to
11085 continue parsing. */
11086 cp_parser_skip_to_closing_parenthesis (parser,
11087 /*recovering=*/true,
11088 /*or_comma=*/true,
11089 /*consume_paren=*/false);
11090 /* If parsing tentatively, permanently remove the
11091 template argument list. That will prevent duplicate
11092 error messages from being issued about the missing
11093 "template" keyword. */
11094 if (start)
11095 cp_lexer_purge_tokens_after (parser->lexer, start);
11096 if (is_identifier)
11097 *is_identifier = true;
11098 return identifier;
11101 /* If the "template" keyword is present, then there is generally
11102 no point in doing name-lookup, so we just return IDENTIFIER.
11103 But, if the qualifying scope is non-dependent then we can
11104 (and must) do name-lookup normally. */
11105 if (template_keyword_p
11106 && (!parser->scope
11107 || (TYPE_P (parser->scope)
11108 && dependent_type_p (parser->scope))))
11109 return identifier;
11112 /* Look up the name. */
11113 decl = cp_parser_lookup_name (parser, identifier,
11114 none_type,
11115 /*is_template=*/true,
11116 /*is_namespace=*/false,
11117 check_dependency_p,
11118 /*ambiguous_decls=*/NULL,
11119 token->location);
11121 /* If DECL is a template, then the name was a template-name. */
11122 if (TREE_CODE (decl) == TEMPLATE_DECL)
11124 else
11126 tree fn = NULL_TREE;
11128 /* The standard does not explicitly indicate whether a name that
11129 names a set of overloaded declarations, some of which are
11130 templates, is a template-name. However, such a name should
11131 be a template-name; otherwise, there is no way to form a
11132 template-id for the overloaded templates. */
11133 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11134 if (TREE_CODE (fns) == OVERLOAD)
11135 for (fn = fns; fn; fn = OVL_NEXT (fn))
11136 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11137 break;
11139 if (!fn)
11141 /* The name does not name a template. */
11142 cp_parser_error (parser, "expected template-name");
11143 return error_mark_node;
11147 /* If DECL is dependent, and refers to a function, then just return
11148 its name; we will look it up again during template instantiation. */
11149 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11151 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
11152 if (TYPE_P (scope) && dependent_type_p (scope))
11153 return identifier;
11156 return decl;
11159 /* Parse a template-argument-list.
11161 template-argument-list:
11162 template-argument ... [opt]
11163 template-argument-list , template-argument ... [opt]
11165 Returns a TREE_VEC containing the arguments. */
11167 static tree
11168 cp_parser_template_argument_list (cp_parser* parser)
11170 tree fixed_args[10];
11171 unsigned n_args = 0;
11172 unsigned alloced = 10;
11173 tree *arg_ary = fixed_args;
11174 tree vec;
11175 bool saved_in_template_argument_list_p;
11176 bool saved_ice_p;
11177 bool saved_non_ice_p;
11179 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
11180 parser->in_template_argument_list_p = true;
11181 /* Even if the template-id appears in an integral
11182 constant-expression, the contents of the argument list do
11183 not. */
11184 saved_ice_p = parser->integral_constant_expression_p;
11185 parser->integral_constant_expression_p = false;
11186 saved_non_ice_p = parser->non_integral_constant_expression_p;
11187 parser->non_integral_constant_expression_p = false;
11188 /* Parse the arguments. */
11191 tree argument;
11193 if (n_args)
11194 /* Consume the comma. */
11195 cp_lexer_consume_token (parser->lexer);
11197 /* Parse the template-argument. */
11198 argument = cp_parser_template_argument (parser);
11200 /* If the next token is an ellipsis, we're expanding a template
11201 argument pack. */
11202 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11204 if (argument == error_mark_node)
11206 cp_token *token = cp_lexer_peek_token (parser->lexer);
11207 error_at (token->location,
11208 "expected parameter pack before %<...%>");
11210 /* Consume the `...' token. */
11211 cp_lexer_consume_token (parser->lexer);
11213 /* Make the argument into a TYPE_PACK_EXPANSION or
11214 EXPR_PACK_EXPANSION. */
11215 argument = make_pack_expansion (argument);
11218 if (n_args == alloced)
11220 alloced *= 2;
11222 if (arg_ary == fixed_args)
11224 arg_ary = XNEWVEC (tree, alloced);
11225 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
11227 else
11228 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
11230 arg_ary[n_args++] = argument;
11232 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
11234 vec = make_tree_vec (n_args);
11236 while (n_args--)
11237 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
11239 if (arg_ary != fixed_args)
11240 free (arg_ary);
11241 parser->non_integral_constant_expression_p = saved_non_ice_p;
11242 parser->integral_constant_expression_p = saved_ice_p;
11243 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
11244 #ifdef ENABLE_CHECKING
11245 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
11246 #endif
11247 return vec;
11250 /* Parse a template-argument.
11252 template-argument:
11253 assignment-expression
11254 type-id
11255 id-expression
11257 The representation is that of an assignment-expression, type-id, or
11258 id-expression -- except that the qualified id-expression is
11259 evaluated, so that the value returned is either a DECL or an
11260 OVERLOAD.
11262 Although the standard says "assignment-expression", it forbids
11263 throw-expressions or assignments in the template argument.
11264 Therefore, we use "conditional-expression" instead. */
11266 static tree
11267 cp_parser_template_argument (cp_parser* parser)
11269 tree argument;
11270 bool template_p;
11271 bool address_p;
11272 bool maybe_type_id = false;
11273 cp_token *token = NULL, *argument_start_token = NULL;
11274 cp_id_kind idk;
11276 /* There's really no way to know what we're looking at, so we just
11277 try each alternative in order.
11279 [temp.arg]
11281 In a template-argument, an ambiguity between a type-id and an
11282 expression is resolved to a type-id, regardless of the form of
11283 the corresponding template-parameter.
11285 Therefore, we try a type-id first. */
11286 cp_parser_parse_tentatively (parser);
11287 argument = cp_parser_template_type_arg (parser);
11288 /* If there was no error parsing the type-id but the next token is a
11289 '>>', our behavior depends on which dialect of C++ we're
11290 parsing. In C++98, we probably found a typo for '> >'. But there
11291 are type-id which are also valid expressions. For instance:
11293 struct X { int operator >> (int); };
11294 template <int V> struct Foo {};
11295 Foo<X () >> 5> r;
11297 Here 'X()' is a valid type-id of a function type, but the user just
11298 wanted to write the expression "X() >> 5". Thus, we remember that we
11299 found a valid type-id, but we still try to parse the argument as an
11300 expression to see what happens.
11302 In C++0x, the '>>' will be considered two separate '>'
11303 tokens. */
11304 if (!cp_parser_error_occurred (parser)
11305 && cxx_dialect == cxx98
11306 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
11308 maybe_type_id = true;
11309 cp_parser_abort_tentative_parse (parser);
11311 else
11313 /* If the next token isn't a `,' or a `>', then this argument wasn't
11314 really finished. This means that the argument is not a valid
11315 type-id. */
11316 if (!cp_parser_next_token_ends_template_argument_p (parser))
11317 cp_parser_error (parser, "expected template-argument");
11318 /* If that worked, we're done. */
11319 if (cp_parser_parse_definitely (parser))
11320 return argument;
11322 /* We're still not sure what the argument will be. */
11323 cp_parser_parse_tentatively (parser);
11324 /* Try a template. */
11325 argument_start_token = cp_lexer_peek_token (parser->lexer);
11326 argument = cp_parser_id_expression (parser,
11327 /*template_keyword_p=*/false,
11328 /*check_dependency_p=*/true,
11329 &template_p,
11330 /*declarator_p=*/false,
11331 /*optional_p=*/false);
11332 /* If the next token isn't a `,' or a `>', then this argument wasn't
11333 really finished. */
11334 if (!cp_parser_next_token_ends_template_argument_p (parser))
11335 cp_parser_error (parser, "expected template-argument");
11336 if (!cp_parser_error_occurred (parser))
11338 /* Figure out what is being referred to. If the id-expression
11339 was for a class template specialization, then we will have a
11340 TYPE_DECL at this point. There is no need to do name lookup
11341 at this point in that case. */
11342 if (TREE_CODE (argument) != TYPE_DECL)
11343 argument = cp_parser_lookup_name (parser, argument,
11344 none_type,
11345 /*is_template=*/template_p,
11346 /*is_namespace=*/false,
11347 /*check_dependency=*/true,
11348 /*ambiguous_decls=*/NULL,
11349 argument_start_token->location);
11350 if (TREE_CODE (argument) != TEMPLATE_DECL
11351 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
11352 cp_parser_error (parser, "expected template-name");
11354 if (cp_parser_parse_definitely (parser))
11355 return argument;
11356 /* It must be a non-type argument. There permitted cases are given
11357 in [temp.arg.nontype]:
11359 -- an integral constant-expression of integral or enumeration
11360 type; or
11362 -- the name of a non-type template-parameter; or
11364 -- the name of an object or function with external linkage...
11366 -- the address of an object or function with external linkage...
11368 -- a pointer to member... */
11369 /* Look for a non-type template parameter. */
11370 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11372 cp_parser_parse_tentatively (parser);
11373 argument = cp_parser_primary_expression (parser,
11374 /*address_p=*/false,
11375 /*cast_p=*/false,
11376 /*template_arg_p=*/true,
11377 &idk);
11378 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
11379 || !cp_parser_next_token_ends_template_argument_p (parser))
11380 cp_parser_simulate_error (parser);
11381 if (cp_parser_parse_definitely (parser))
11382 return argument;
11385 /* If the next token is "&", the argument must be the address of an
11386 object or function with external linkage. */
11387 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
11388 if (address_p)
11389 cp_lexer_consume_token (parser->lexer);
11390 /* See if we might have an id-expression. */
11391 token = cp_lexer_peek_token (parser->lexer);
11392 if (token->type == CPP_NAME
11393 || token->keyword == RID_OPERATOR
11394 || token->type == CPP_SCOPE
11395 || token->type == CPP_TEMPLATE_ID
11396 || token->type == CPP_NESTED_NAME_SPECIFIER)
11398 cp_parser_parse_tentatively (parser);
11399 argument = cp_parser_primary_expression (parser,
11400 address_p,
11401 /*cast_p=*/false,
11402 /*template_arg_p=*/true,
11403 &idk);
11404 if (cp_parser_error_occurred (parser)
11405 || !cp_parser_next_token_ends_template_argument_p (parser))
11406 cp_parser_abort_tentative_parse (parser);
11407 else
11409 tree probe;
11411 if (TREE_CODE (argument) == INDIRECT_REF)
11413 gcc_assert (REFERENCE_REF_P (argument));
11414 argument = TREE_OPERAND (argument, 0);
11417 /* If we're in a template, we represent a qualified-id referring
11418 to a static data member as a SCOPE_REF even if the scope isn't
11419 dependent so that we can check access control later. */
11420 probe = argument;
11421 if (TREE_CODE (probe) == SCOPE_REF)
11422 probe = TREE_OPERAND (probe, 1);
11423 if (TREE_CODE (probe) == VAR_DECL)
11425 /* A variable without external linkage might still be a
11426 valid constant-expression, so no error is issued here
11427 if the external-linkage check fails. */
11428 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
11429 cp_parser_simulate_error (parser);
11431 else if (is_overloaded_fn (argument))
11432 /* All overloaded functions are allowed; if the external
11433 linkage test does not pass, an error will be issued
11434 later. */
11436 else if (address_p
11437 && (TREE_CODE (argument) == OFFSET_REF
11438 || TREE_CODE (argument) == SCOPE_REF))
11439 /* A pointer-to-member. */
11441 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
11443 else
11444 cp_parser_simulate_error (parser);
11446 if (cp_parser_parse_definitely (parser))
11448 if (address_p)
11449 argument = build_x_unary_op (ADDR_EXPR, argument,
11450 tf_warning_or_error);
11451 return argument;
11455 /* If the argument started with "&", there are no other valid
11456 alternatives at this point. */
11457 if (address_p)
11459 cp_parser_error (parser, "invalid non-type template argument");
11460 return error_mark_node;
11463 /* If the argument wasn't successfully parsed as a type-id followed
11464 by '>>', the argument can only be a constant expression now.
11465 Otherwise, we try parsing the constant-expression tentatively,
11466 because the argument could really be a type-id. */
11467 if (maybe_type_id)
11468 cp_parser_parse_tentatively (parser);
11469 argument = cp_parser_constant_expression (parser,
11470 /*allow_non_constant_p=*/false,
11471 /*non_constant_p=*/NULL);
11472 argument = fold_non_dependent_expr (argument);
11473 if (!maybe_type_id)
11474 return argument;
11475 if (!cp_parser_next_token_ends_template_argument_p (parser))
11476 cp_parser_error (parser, "expected template-argument");
11477 if (cp_parser_parse_definitely (parser))
11478 return argument;
11479 /* We did our best to parse the argument as a non type-id, but that
11480 was the only alternative that matched (albeit with a '>' after
11481 it). We can assume it's just a typo from the user, and a
11482 diagnostic will then be issued. */
11483 return cp_parser_template_type_arg (parser);
11486 /* Parse an explicit-instantiation.
11488 explicit-instantiation:
11489 template declaration
11491 Although the standard says `declaration', what it really means is:
11493 explicit-instantiation:
11494 template decl-specifier-seq [opt] declarator [opt] ;
11496 Things like `template int S<int>::i = 5, int S<double>::j;' are not
11497 supposed to be allowed. A defect report has been filed about this
11498 issue.
11500 GNU Extension:
11502 explicit-instantiation:
11503 storage-class-specifier template
11504 decl-specifier-seq [opt] declarator [opt] ;
11505 function-specifier template
11506 decl-specifier-seq [opt] declarator [opt] ; */
11508 static void
11509 cp_parser_explicit_instantiation (cp_parser* parser)
11511 int declares_class_or_enum;
11512 cp_decl_specifier_seq decl_specifiers;
11513 tree extension_specifier = NULL_TREE;
11515 /* Look for an (optional) storage-class-specifier or
11516 function-specifier. */
11517 if (cp_parser_allow_gnu_extensions_p (parser))
11519 extension_specifier
11520 = cp_parser_storage_class_specifier_opt (parser);
11521 if (!extension_specifier)
11522 extension_specifier
11523 = cp_parser_function_specifier_opt (parser,
11524 /*decl_specs=*/NULL);
11527 /* Look for the `template' keyword. */
11528 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
11529 /* Let the front end know that we are processing an explicit
11530 instantiation. */
11531 begin_explicit_instantiation ();
11532 /* [temp.explicit] says that we are supposed to ignore access
11533 control while processing explicit instantiation directives. */
11534 push_deferring_access_checks (dk_no_check);
11535 /* Parse a decl-specifier-seq. */
11536 cp_parser_decl_specifier_seq (parser,
11537 CP_PARSER_FLAGS_OPTIONAL,
11538 &decl_specifiers,
11539 &declares_class_or_enum);
11540 /* If there was exactly one decl-specifier, and it declared a class,
11541 and there's no declarator, then we have an explicit type
11542 instantiation. */
11543 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
11545 tree type;
11547 type = check_tag_decl (&decl_specifiers);
11548 /* Turn access control back on for names used during
11549 template instantiation. */
11550 pop_deferring_access_checks ();
11551 if (type)
11552 do_type_instantiation (type, extension_specifier,
11553 /*complain=*/tf_error);
11555 else
11557 cp_declarator *declarator;
11558 tree decl;
11560 /* Parse the declarator. */
11561 declarator
11562 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11563 /*ctor_dtor_or_conv_p=*/NULL,
11564 /*parenthesized_p=*/NULL,
11565 /*member_p=*/false);
11566 if (declares_class_or_enum & 2)
11567 cp_parser_check_for_definition_in_return_type (declarator,
11568 decl_specifiers.type,
11569 decl_specifiers.type_location);
11570 if (declarator != cp_error_declarator)
11572 decl = grokdeclarator (declarator, &decl_specifiers,
11573 NORMAL, 0, &decl_specifiers.attributes);
11574 /* Turn access control back on for names used during
11575 template instantiation. */
11576 pop_deferring_access_checks ();
11577 /* Do the explicit instantiation. */
11578 do_decl_instantiation (decl, extension_specifier);
11580 else
11582 pop_deferring_access_checks ();
11583 /* Skip the body of the explicit instantiation. */
11584 cp_parser_skip_to_end_of_statement (parser);
11587 /* We're done with the instantiation. */
11588 end_explicit_instantiation ();
11590 cp_parser_consume_semicolon_at_end_of_statement (parser);
11593 /* Parse an explicit-specialization.
11595 explicit-specialization:
11596 template < > declaration
11598 Although the standard says `declaration', what it really means is:
11600 explicit-specialization:
11601 template <> decl-specifier [opt] init-declarator [opt] ;
11602 template <> function-definition
11603 template <> explicit-specialization
11604 template <> template-declaration */
11606 static void
11607 cp_parser_explicit_specialization (cp_parser* parser)
11609 bool need_lang_pop;
11610 cp_token *token = cp_lexer_peek_token (parser->lexer);
11612 /* Look for the `template' keyword. */
11613 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
11614 /* Look for the `<'. */
11615 cp_parser_require (parser, CPP_LESS, "%<<%>");
11616 /* Look for the `>'. */
11617 cp_parser_require (parser, CPP_GREATER, "%<>%>");
11618 /* We have processed another parameter list. */
11619 ++parser->num_template_parameter_lists;
11620 /* [temp]
11622 A template ... explicit specialization ... shall not have C
11623 linkage. */
11624 if (current_lang_name == lang_name_c)
11626 error_at (token->location, "template specialization with C linkage");
11627 /* Give it C++ linkage to avoid confusing other parts of the
11628 front end. */
11629 push_lang_context (lang_name_cplusplus);
11630 need_lang_pop = true;
11632 else
11633 need_lang_pop = false;
11634 /* Let the front end know that we are beginning a specialization. */
11635 if (!begin_specialization ())
11637 end_specialization ();
11638 return;
11641 /* If the next keyword is `template', we need to figure out whether
11642 or not we're looking a template-declaration. */
11643 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
11645 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
11646 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
11647 cp_parser_template_declaration_after_export (parser,
11648 /*member_p=*/false);
11649 else
11650 cp_parser_explicit_specialization (parser);
11652 else
11653 /* Parse the dependent declaration. */
11654 cp_parser_single_declaration (parser,
11655 /*checks=*/NULL,
11656 /*member_p=*/false,
11657 /*explicit_specialization_p=*/true,
11658 /*friend_p=*/NULL);
11659 /* We're done with the specialization. */
11660 end_specialization ();
11661 /* For the erroneous case of a template with C linkage, we pushed an
11662 implicit C++ linkage scope; exit that scope now. */
11663 if (need_lang_pop)
11664 pop_lang_context ();
11665 /* We're done with this parameter list. */
11666 --parser->num_template_parameter_lists;
11669 /* Parse a type-specifier.
11671 type-specifier:
11672 simple-type-specifier
11673 class-specifier
11674 enum-specifier
11675 elaborated-type-specifier
11676 cv-qualifier
11678 GNU Extension:
11680 type-specifier:
11681 __complex__
11683 Returns a representation of the type-specifier. For a
11684 class-specifier, enum-specifier, or elaborated-type-specifier, a
11685 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
11687 The parser flags FLAGS is used to control type-specifier parsing.
11689 If IS_DECLARATION is TRUE, then this type-specifier is appearing
11690 in a decl-specifier-seq.
11692 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
11693 class-specifier, enum-specifier, or elaborated-type-specifier, then
11694 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
11695 if a type is declared; 2 if it is defined. Otherwise, it is set to
11696 zero.
11698 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
11699 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
11700 is set to FALSE. */
11702 static tree
11703 cp_parser_type_specifier (cp_parser* parser,
11704 cp_parser_flags flags,
11705 cp_decl_specifier_seq *decl_specs,
11706 bool is_declaration,
11707 int* declares_class_or_enum,
11708 bool* is_cv_qualifier)
11710 tree type_spec = NULL_TREE;
11711 cp_token *token;
11712 enum rid keyword;
11713 cp_decl_spec ds = ds_last;
11715 /* Assume this type-specifier does not declare a new type. */
11716 if (declares_class_or_enum)
11717 *declares_class_or_enum = 0;
11718 /* And that it does not specify a cv-qualifier. */
11719 if (is_cv_qualifier)
11720 *is_cv_qualifier = false;
11721 /* Peek at the next token. */
11722 token = cp_lexer_peek_token (parser->lexer);
11724 /* If we're looking at a keyword, we can use that to guide the
11725 production we choose. */
11726 keyword = token->keyword;
11727 switch (keyword)
11729 case RID_ENUM:
11730 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
11731 goto elaborated_type_specifier;
11733 /* Look for the enum-specifier. */
11734 type_spec = cp_parser_enum_specifier (parser);
11735 /* If that worked, we're done. */
11736 if (type_spec)
11738 if (declares_class_or_enum)
11739 *declares_class_or_enum = 2;
11740 if (decl_specs)
11741 cp_parser_set_decl_spec_type (decl_specs,
11742 type_spec,
11743 token->location,
11744 /*user_defined_p=*/true);
11745 return type_spec;
11747 else
11748 goto elaborated_type_specifier;
11750 /* Any of these indicate either a class-specifier, or an
11751 elaborated-type-specifier. */
11752 case RID_CLASS:
11753 case RID_STRUCT:
11754 case RID_UNION:
11755 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
11756 goto elaborated_type_specifier;
11758 /* Parse tentatively so that we can back up if we don't find a
11759 class-specifier. */
11760 cp_parser_parse_tentatively (parser);
11761 /* Look for the class-specifier. */
11762 type_spec = cp_parser_class_specifier (parser);
11763 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
11764 /* If that worked, we're done. */
11765 if (cp_parser_parse_definitely (parser))
11767 if (declares_class_or_enum)
11768 *declares_class_or_enum = 2;
11769 if (decl_specs)
11770 cp_parser_set_decl_spec_type (decl_specs,
11771 type_spec,
11772 token->location,
11773 /*user_defined_p=*/true);
11774 return type_spec;
11777 /* Fall through. */
11778 elaborated_type_specifier:
11779 /* We're declaring (not defining) a class or enum. */
11780 if (declares_class_or_enum)
11781 *declares_class_or_enum = 1;
11783 /* Fall through. */
11784 case RID_TYPENAME:
11785 /* Look for an elaborated-type-specifier. */
11786 type_spec
11787 = (cp_parser_elaborated_type_specifier
11788 (parser,
11789 decl_specs && decl_specs->specs[(int) ds_friend],
11790 is_declaration));
11791 if (decl_specs)
11792 cp_parser_set_decl_spec_type (decl_specs,
11793 type_spec,
11794 token->location,
11795 /*user_defined_p=*/true);
11796 return type_spec;
11798 case RID_CONST:
11799 ds = ds_const;
11800 if (is_cv_qualifier)
11801 *is_cv_qualifier = true;
11802 break;
11804 case RID_VOLATILE:
11805 ds = ds_volatile;
11806 if (is_cv_qualifier)
11807 *is_cv_qualifier = true;
11808 break;
11810 case RID_RESTRICT:
11811 ds = ds_restrict;
11812 if (is_cv_qualifier)
11813 *is_cv_qualifier = true;
11814 break;
11816 case RID_COMPLEX:
11817 /* The `__complex__' keyword is a GNU extension. */
11818 ds = ds_complex;
11819 break;
11821 default:
11822 break;
11825 /* Handle simple keywords. */
11826 if (ds != ds_last)
11828 if (decl_specs)
11830 ++decl_specs->specs[(int)ds];
11831 decl_specs->any_specifiers_p = true;
11833 return cp_lexer_consume_token (parser->lexer)->u.value;
11836 /* If we do not already have a type-specifier, assume we are looking
11837 at a simple-type-specifier. */
11838 type_spec = cp_parser_simple_type_specifier (parser,
11839 decl_specs,
11840 flags);
11842 /* If we didn't find a type-specifier, and a type-specifier was not
11843 optional in this context, issue an error message. */
11844 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11846 cp_parser_error (parser, "expected type specifier");
11847 return error_mark_node;
11850 return type_spec;
11853 /* Parse a simple-type-specifier.
11855 simple-type-specifier:
11856 :: [opt] nested-name-specifier [opt] type-name
11857 :: [opt] nested-name-specifier template template-id
11858 char
11859 wchar_t
11860 bool
11861 short
11863 long
11864 signed
11865 unsigned
11866 float
11867 double
11868 void
11870 C++0x Extension:
11872 simple-type-specifier:
11873 auto
11874 decltype ( expression )
11875 char16_t
11876 char32_t
11878 GNU Extension:
11880 simple-type-specifier:
11881 __typeof__ unary-expression
11882 __typeof__ ( type-id )
11884 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
11885 appropriately updated. */
11887 static tree
11888 cp_parser_simple_type_specifier (cp_parser* parser,
11889 cp_decl_specifier_seq *decl_specs,
11890 cp_parser_flags flags)
11892 tree type = NULL_TREE;
11893 cp_token *token;
11895 /* Peek at the next token. */
11896 token = cp_lexer_peek_token (parser->lexer);
11898 /* If we're looking at a keyword, things are easy. */
11899 switch (token->keyword)
11901 case RID_CHAR:
11902 if (decl_specs)
11903 decl_specs->explicit_char_p = true;
11904 type = char_type_node;
11905 break;
11906 case RID_CHAR16:
11907 type = char16_type_node;
11908 break;
11909 case RID_CHAR32:
11910 type = char32_type_node;
11911 break;
11912 case RID_WCHAR:
11913 type = wchar_type_node;
11914 break;
11915 case RID_BOOL:
11916 type = boolean_type_node;
11917 break;
11918 case RID_SHORT:
11919 if (decl_specs)
11920 ++decl_specs->specs[(int) ds_short];
11921 type = short_integer_type_node;
11922 break;
11923 case RID_INT:
11924 if (decl_specs)
11925 decl_specs->explicit_int_p = true;
11926 type = integer_type_node;
11927 break;
11928 case RID_LONG:
11929 if (decl_specs)
11930 ++decl_specs->specs[(int) ds_long];
11931 type = long_integer_type_node;
11932 break;
11933 case RID_SIGNED:
11934 if (decl_specs)
11935 ++decl_specs->specs[(int) ds_signed];
11936 type = integer_type_node;
11937 break;
11938 case RID_UNSIGNED:
11939 if (decl_specs)
11940 ++decl_specs->specs[(int) ds_unsigned];
11941 type = unsigned_type_node;
11942 break;
11943 case RID_FLOAT:
11944 type = float_type_node;
11945 break;
11946 case RID_DOUBLE:
11947 type = double_type_node;
11948 break;
11949 case RID_VOID:
11950 type = void_type_node;
11951 break;
11953 case RID_AUTO:
11954 maybe_warn_cpp0x (CPP0X_AUTO);
11955 type = make_auto ();
11956 break;
11958 case RID_DECLTYPE:
11959 /* Parse the `decltype' type. */
11960 type = cp_parser_decltype (parser);
11962 if (decl_specs)
11963 cp_parser_set_decl_spec_type (decl_specs, type,
11964 token->location,
11965 /*user_defined_p=*/true);
11967 return type;
11969 case RID_TYPEOF:
11970 /* Consume the `typeof' token. */
11971 cp_lexer_consume_token (parser->lexer);
11972 /* Parse the operand to `typeof'. */
11973 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11974 /* If it is not already a TYPE, take its type. */
11975 if (!TYPE_P (type))
11976 type = finish_typeof (type);
11978 if (decl_specs)
11979 cp_parser_set_decl_spec_type (decl_specs, type,
11980 token->location,
11981 /*user_defined_p=*/true);
11983 return type;
11985 default:
11986 break;
11989 /* If the type-specifier was for a built-in type, we're done. */
11990 if (type)
11992 /* Record the type. */
11993 if (decl_specs
11994 && (token->keyword != RID_SIGNED
11995 && token->keyword != RID_UNSIGNED
11996 && token->keyword != RID_SHORT
11997 && token->keyword != RID_LONG))
11998 cp_parser_set_decl_spec_type (decl_specs,
11999 type,
12000 token->location,
12001 /*user_defined=*/false);
12002 if (decl_specs)
12003 decl_specs->any_specifiers_p = true;
12005 /* Consume the token. */
12006 cp_lexer_consume_token (parser->lexer);
12008 /* There is no valid C++ program where a non-template type is
12009 followed by a "<". That usually indicates that the user thought
12010 that the type was a template. */
12011 cp_parser_check_for_invalid_template_id (parser, type, token->location);
12013 return TYPE_NAME (type);
12016 /* The type-specifier must be a user-defined type. */
12017 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
12019 bool qualified_p;
12020 bool global_p;
12022 /* Don't gobble tokens or issue error messages if this is an
12023 optional type-specifier. */
12024 if (flags & CP_PARSER_FLAGS_OPTIONAL)
12025 cp_parser_parse_tentatively (parser);
12027 /* Look for the optional `::' operator. */
12028 global_p
12029 = (cp_parser_global_scope_opt (parser,
12030 /*current_scope_valid_p=*/false)
12031 != NULL_TREE);
12032 /* Look for the nested-name specifier. */
12033 qualified_p
12034 = (cp_parser_nested_name_specifier_opt (parser,
12035 /*typename_keyword_p=*/false,
12036 /*check_dependency_p=*/true,
12037 /*type_p=*/false,
12038 /*is_declaration=*/false)
12039 != NULL_TREE);
12040 token = cp_lexer_peek_token (parser->lexer);
12041 /* If we have seen a nested-name-specifier, and the next token
12042 is `template', then we are using the template-id production. */
12043 if (parser->scope
12044 && cp_parser_optional_template_keyword (parser))
12046 /* Look for the template-id. */
12047 type = cp_parser_template_id (parser,
12048 /*template_keyword_p=*/true,
12049 /*check_dependency_p=*/true,
12050 /*is_declaration=*/false);
12051 /* If the template-id did not name a type, we are out of
12052 luck. */
12053 if (TREE_CODE (type) != TYPE_DECL)
12055 cp_parser_error (parser, "expected template-id for type");
12056 type = NULL_TREE;
12059 /* Otherwise, look for a type-name. */
12060 else
12061 type = cp_parser_type_name (parser);
12062 /* Keep track of all name-lookups performed in class scopes. */
12063 if (type
12064 && !global_p
12065 && !qualified_p
12066 && TREE_CODE (type) == TYPE_DECL
12067 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
12068 maybe_note_name_used_in_class (DECL_NAME (type), type);
12069 /* If it didn't work out, we don't have a TYPE. */
12070 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
12071 && !cp_parser_parse_definitely (parser))
12072 type = NULL_TREE;
12073 if (type && decl_specs)
12074 cp_parser_set_decl_spec_type (decl_specs, type,
12075 token->location,
12076 /*user_defined=*/true);
12079 /* If we didn't get a type-name, issue an error message. */
12080 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12082 cp_parser_error (parser, "expected type-name");
12083 return error_mark_node;
12086 /* There is no valid C++ program where a non-template type is
12087 followed by a "<". That usually indicates that the user thought
12088 that the type was a template. */
12089 if (type && type != error_mark_node)
12091 /* As a last-ditch effort, see if TYPE is an Objective-C type.
12092 If it is, then the '<'...'>' enclose protocol names rather than
12093 template arguments, and so everything is fine. */
12094 if (c_dialect_objc ()
12095 && (objc_is_id (type) || objc_is_class_name (type)))
12097 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12098 tree qual_type = objc_get_protocol_qualified_type (type, protos);
12100 /* Clobber the "unqualified" type previously entered into
12101 DECL_SPECS with the new, improved protocol-qualified version. */
12102 if (decl_specs)
12103 decl_specs->type = qual_type;
12105 return qual_type;
12108 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12109 token->location);
12112 return type;
12115 /* Parse a type-name.
12117 type-name:
12118 class-name
12119 enum-name
12120 typedef-name
12122 enum-name:
12123 identifier
12125 typedef-name:
12126 identifier
12128 Returns a TYPE_DECL for the type. */
12130 static tree
12131 cp_parser_type_name (cp_parser* parser)
12133 tree type_decl;
12135 /* We can't know yet whether it is a class-name or not. */
12136 cp_parser_parse_tentatively (parser);
12137 /* Try a class-name. */
12138 type_decl = cp_parser_class_name (parser,
12139 /*typename_keyword_p=*/false,
12140 /*template_keyword_p=*/false,
12141 none_type,
12142 /*check_dependency_p=*/true,
12143 /*class_head_p=*/false,
12144 /*is_declaration=*/false);
12145 /* If it's not a class-name, keep looking. */
12146 if (!cp_parser_parse_definitely (parser))
12148 /* It must be a typedef-name or an enum-name. */
12149 return cp_parser_nonclass_name (parser);
12152 return type_decl;
12155 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12157 enum-name:
12158 identifier
12160 typedef-name:
12161 identifier
12163 Returns a TYPE_DECL for the type. */
12165 static tree
12166 cp_parser_nonclass_name (cp_parser* parser)
12168 tree type_decl;
12169 tree identifier;
12171 cp_token *token = cp_lexer_peek_token (parser->lexer);
12172 identifier = cp_parser_identifier (parser);
12173 if (identifier == error_mark_node)
12174 return error_mark_node;
12176 /* Look up the type-name. */
12177 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
12179 if (TREE_CODE (type_decl) != TYPE_DECL
12180 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
12182 /* See if this is an Objective-C type. */
12183 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12184 tree type = objc_get_protocol_qualified_type (identifier, protos);
12185 if (type)
12186 type_decl = TYPE_NAME (type);
12189 /* Issue an error if we did not find a type-name. */
12190 if (TREE_CODE (type_decl) != TYPE_DECL)
12192 if (!cp_parser_simulate_error (parser))
12193 cp_parser_name_lookup_error (parser, identifier, type_decl,
12194 "is not a type", token->location);
12195 return error_mark_node;
12197 /* Remember that the name was used in the definition of the
12198 current class so that we can check later to see if the
12199 meaning would have been different after the class was
12200 entirely defined. */
12201 else if (type_decl != error_mark_node
12202 && !parser->scope)
12203 maybe_note_name_used_in_class (identifier, type_decl);
12205 return type_decl;
12208 /* Parse an elaborated-type-specifier. Note that the grammar given
12209 here incorporates the resolution to DR68.
12211 elaborated-type-specifier:
12212 class-key :: [opt] nested-name-specifier [opt] identifier
12213 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
12214 enum-key :: [opt] nested-name-specifier [opt] identifier
12215 typename :: [opt] nested-name-specifier identifier
12216 typename :: [opt] nested-name-specifier template [opt]
12217 template-id
12219 GNU extension:
12221 elaborated-type-specifier:
12222 class-key attributes :: [opt] nested-name-specifier [opt] identifier
12223 class-key attributes :: [opt] nested-name-specifier [opt]
12224 template [opt] template-id
12225 enum attributes :: [opt] nested-name-specifier [opt] identifier
12227 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
12228 declared `friend'. If IS_DECLARATION is TRUE, then this
12229 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
12230 something is being declared.
12232 Returns the TYPE specified. */
12234 static tree
12235 cp_parser_elaborated_type_specifier (cp_parser* parser,
12236 bool is_friend,
12237 bool is_declaration)
12239 enum tag_types tag_type;
12240 tree identifier;
12241 tree type = NULL_TREE;
12242 tree attributes = NULL_TREE;
12243 tree globalscope;
12244 cp_token *token = NULL;
12246 /* See if we're looking at the `enum' keyword. */
12247 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
12249 /* Consume the `enum' token. */
12250 cp_lexer_consume_token (parser->lexer);
12251 /* Remember that it's an enumeration type. */
12252 tag_type = enum_type;
12253 /* Parse the optional `struct' or `class' key (for C++0x scoped
12254 enums). */
12255 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12256 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12258 if (cxx_dialect == cxx98)
12259 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
12261 /* Consume the `struct' or `class'. */
12262 cp_lexer_consume_token (parser->lexer);
12264 /* Parse the attributes. */
12265 attributes = cp_parser_attributes_opt (parser);
12267 /* Or, it might be `typename'. */
12268 else if (cp_lexer_next_token_is_keyword (parser->lexer,
12269 RID_TYPENAME))
12271 /* Consume the `typename' token. */
12272 cp_lexer_consume_token (parser->lexer);
12273 /* Remember that it's a `typename' type. */
12274 tag_type = typename_type;
12276 /* Otherwise it must be a class-key. */
12277 else
12279 tag_type = cp_parser_class_key (parser);
12280 if (tag_type == none_type)
12281 return error_mark_node;
12282 /* Parse the attributes. */
12283 attributes = cp_parser_attributes_opt (parser);
12286 /* Look for the `::' operator. */
12287 globalscope = cp_parser_global_scope_opt (parser,
12288 /*current_scope_valid_p=*/false);
12289 /* Look for the nested-name-specifier. */
12290 if (tag_type == typename_type && !globalscope)
12292 if (!cp_parser_nested_name_specifier (parser,
12293 /*typename_keyword_p=*/true,
12294 /*check_dependency_p=*/true,
12295 /*type_p=*/true,
12296 is_declaration))
12297 return error_mark_node;
12299 else
12300 /* Even though `typename' is not present, the proposed resolution
12301 to Core Issue 180 says that in `class A<T>::B', `B' should be
12302 considered a type-name, even if `A<T>' is dependent. */
12303 cp_parser_nested_name_specifier_opt (parser,
12304 /*typename_keyword_p=*/true,
12305 /*check_dependency_p=*/true,
12306 /*type_p=*/true,
12307 is_declaration);
12308 /* For everything but enumeration types, consider a template-id.
12309 For an enumeration type, consider only a plain identifier. */
12310 if (tag_type != enum_type)
12312 bool template_p = false;
12313 tree decl;
12315 /* Allow the `template' keyword. */
12316 template_p = cp_parser_optional_template_keyword (parser);
12317 /* If we didn't see `template', we don't know if there's a
12318 template-id or not. */
12319 if (!template_p)
12320 cp_parser_parse_tentatively (parser);
12321 /* Parse the template-id. */
12322 token = cp_lexer_peek_token (parser->lexer);
12323 decl = cp_parser_template_id (parser, template_p,
12324 /*check_dependency_p=*/true,
12325 is_declaration);
12326 /* If we didn't find a template-id, look for an ordinary
12327 identifier. */
12328 if (!template_p && !cp_parser_parse_definitely (parser))
12330 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
12331 in effect, then we must assume that, upon instantiation, the
12332 template will correspond to a class. */
12333 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12334 && tag_type == typename_type)
12335 type = make_typename_type (parser->scope, decl,
12336 typename_type,
12337 /*complain=*/tf_error);
12338 /* If the `typename' keyword is in effect and DECL is not a type
12339 decl. Then type is non existant. */
12340 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
12341 type = NULL_TREE;
12342 else
12343 type = TREE_TYPE (decl);
12346 if (!type)
12348 token = cp_lexer_peek_token (parser->lexer);
12349 identifier = cp_parser_identifier (parser);
12351 if (identifier == error_mark_node)
12353 parser->scope = NULL_TREE;
12354 return error_mark_node;
12357 /* For a `typename', we needn't call xref_tag. */
12358 if (tag_type == typename_type
12359 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
12360 return cp_parser_make_typename_type (parser, parser->scope,
12361 identifier,
12362 token->location);
12363 /* Look up a qualified name in the usual way. */
12364 if (parser->scope)
12366 tree decl;
12367 tree ambiguous_decls;
12369 decl = cp_parser_lookup_name (parser, identifier,
12370 tag_type,
12371 /*is_template=*/false,
12372 /*is_namespace=*/false,
12373 /*check_dependency=*/true,
12374 &ambiguous_decls,
12375 token->location);
12377 /* If the lookup was ambiguous, an error will already have been
12378 issued. */
12379 if (ambiguous_decls)
12380 return error_mark_node;
12382 /* If we are parsing friend declaration, DECL may be a
12383 TEMPLATE_DECL tree node here. However, we need to check
12384 whether this TEMPLATE_DECL results in valid code. Consider
12385 the following example:
12387 namespace N {
12388 template <class T> class C {};
12390 class X {
12391 template <class T> friend class N::C; // #1, valid code
12393 template <class T> class Y {
12394 friend class N::C; // #2, invalid code
12397 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
12398 name lookup of `N::C'. We see that friend declaration must
12399 be template for the code to be valid. Note that
12400 processing_template_decl does not work here since it is
12401 always 1 for the above two cases. */
12403 decl = (cp_parser_maybe_treat_template_as_class
12404 (decl, /*tag_name_p=*/is_friend
12405 && parser->num_template_parameter_lists));
12407 if (TREE_CODE (decl) != TYPE_DECL)
12409 cp_parser_diagnose_invalid_type_name (parser,
12410 parser->scope,
12411 identifier,
12412 token->location);
12413 return error_mark_node;
12416 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
12418 bool allow_template = (parser->num_template_parameter_lists
12419 || DECL_SELF_REFERENCE_P (decl));
12420 type = check_elaborated_type_specifier (tag_type, decl,
12421 allow_template);
12423 if (type == error_mark_node)
12424 return error_mark_node;
12427 /* Forward declarations of nested types, such as
12429 class C1::C2;
12430 class C1::C2::C3;
12432 are invalid unless all components preceding the final '::'
12433 are complete. If all enclosing types are complete, these
12434 declarations become merely pointless.
12436 Invalid forward declarations of nested types are errors
12437 caught elsewhere in parsing. Those that are pointless arrive
12438 here. */
12440 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12441 && !is_friend && !processing_explicit_instantiation)
12442 warning (0, "declaration %qD does not declare anything", decl);
12444 type = TREE_TYPE (decl);
12446 else
12448 /* An elaborated-type-specifier sometimes introduces a new type and
12449 sometimes names an existing type. Normally, the rule is that it
12450 introduces a new type only if there is not an existing type of
12451 the same name already in scope. For example, given:
12453 struct S {};
12454 void f() { struct S s; }
12456 the `struct S' in the body of `f' is the same `struct S' as in
12457 the global scope; the existing definition is used. However, if
12458 there were no global declaration, this would introduce a new
12459 local class named `S'.
12461 An exception to this rule applies to the following code:
12463 namespace N { struct S; }
12465 Here, the elaborated-type-specifier names a new type
12466 unconditionally; even if there is already an `S' in the
12467 containing scope this declaration names a new type.
12468 This exception only applies if the elaborated-type-specifier
12469 forms the complete declaration:
12471 [class.name]
12473 A declaration consisting solely of `class-key identifier ;' is
12474 either a redeclaration of the name in the current scope or a
12475 forward declaration of the identifier as a class name. It
12476 introduces the name into the current scope.
12478 We are in this situation precisely when the next token is a `;'.
12480 An exception to the exception is that a `friend' declaration does
12481 *not* name a new type; i.e., given:
12483 struct S { friend struct T; };
12485 `T' is not a new type in the scope of `S'.
12487 Also, `new struct S' or `sizeof (struct S)' never results in the
12488 definition of a new type; a new type can only be declared in a
12489 declaration context. */
12491 tag_scope ts;
12492 bool template_p;
12494 if (is_friend)
12495 /* Friends have special name lookup rules. */
12496 ts = ts_within_enclosing_non_class;
12497 else if (is_declaration
12498 && cp_lexer_next_token_is (parser->lexer,
12499 CPP_SEMICOLON))
12500 /* This is a `class-key identifier ;' */
12501 ts = ts_current;
12502 else
12503 ts = ts_global;
12505 template_p =
12506 (parser->num_template_parameter_lists
12507 && (cp_parser_next_token_starts_class_definition_p (parser)
12508 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
12509 /* An unqualified name was used to reference this type, so
12510 there were no qualifying templates. */
12511 if (!cp_parser_check_template_parameters (parser,
12512 /*num_templates=*/0,
12513 token->location,
12514 /*declarator=*/NULL))
12515 return error_mark_node;
12516 type = xref_tag (tag_type, identifier, ts, template_p);
12520 if (type == error_mark_node)
12521 return error_mark_node;
12523 /* Allow attributes on forward declarations of classes. */
12524 if (attributes)
12526 if (TREE_CODE (type) == TYPENAME_TYPE)
12527 warning (OPT_Wattributes,
12528 "attributes ignored on uninstantiated type");
12529 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
12530 && ! processing_explicit_instantiation)
12531 warning (OPT_Wattributes,
12532 "attributes ignored on template instantiation");
12533 else if (is_declaration && cp_parser_declares_only_class_p (parser))
12534 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
12535 else
12536 warning (OPT_Wattributes,
12537 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
12540 if (tag_type != enum_type)
12541 cp_parser_check_class_key (tag_type, type);
12543 /* A "<" cannot follow an elaborated type specifier. If that
12544 happens, the user was probably trying to form a template-id. */
12545 cp_parser_check_for_invalid_template_id (parser, type, token->location);
12547 return type;
12550 /* Parse an enum-specifier.
12552 enum-specifier:
12553 enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
12555 enum-key:
12556 enum
12557 enum class [C++0x]
12558 enum struct [C++0x]
12560 enum-base: [C++0x]
12561 : type-specifier-seq
12563 GNU Extensions:
12564 enum-key attributes[opt] identifier [opt] enum-base [opt]
12565 { enumerator-list [opt] }attributes[opt]
12567 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
12568 if the token stream isn't an enum-specifier after all. */
12570 static tree
12571 cp_parser_enum_specifier (cp_parser* parser)
12573 tree identifier;
12574 tree type;
12575 tree attributes;
12576 bool scoped_enum_p = false;
12577 bool has_underlying_type = false;
12578 tree underlying_type = NULL_TREE;
12580 /* Parse tentatively so that we can back up if we don't find a
12581 enum-specifier. */
12582 cp_parser_parse_tentatively (parser);
12584 /* Caller guarantees that the current token is 'enum', an identifier
12585 possibly follows, and the token after that is an opening brace.
12586 If we don't have an identifier, fabricate an anonymous name for
12587 the enumeration being defined. */
12588 cp_lexer_consume_token (parser->lexer);
12590 /* Parse the "class" or "struct", which indicates a scoped
12591 enumeration type in C++0x. */
12592 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12593 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12595 if (cxx_dialect == cxx98)
12596 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
12598 /* Consume the `struct' or `class' token. */
12599 cp_lexer_consume_token (parser->lexer);
12601 scoped_enum_p = true;
12604 attributes = cp_parser_attributes_opt (parser);
12606 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12607 identifier = cp_parser_identifier (parser);
12608 else
12609 identifier = make_anon_name ();
12611 /* Check for the `:' that denotes a specified underlying type in C++0x.
12612 Note that a ':' could also indicate a bitfield width, however. */
12613 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12615 cp_decl_specifier_seq type_specifiers;
12617 /* Consume the `:'. */
12618 cp_lexer_consume_token (parser->lexer);
12620 /* Parse the type-specifier-seq. */
12621 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12622 /*is_trailing_return=*/false,
12623 &type_specifiers);
12625 /* At this point this is surely not elaborated type specifier. */
12626 if (!cp_parser_parse_definitely (parser))
12627 return NULL_TREE;
12629 if (cxx_dialect == cxx98)
12630 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
12632 has_underlying_type = true;
12634 /* If that didn't work, stop. */
12635 if (type_specifiers.type != error_mark_node)
12637 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
12638 /*initialized=*/0, NULL);
12639 if (underlying_type == error_mark_node)
12640 underlying_type = NULL_TREE;
12644 /* Look for the `{' but don't consume it yet. */
12645 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12647 cp_parser_error (parser, "expected %<{%>");
12648 if (has_underlying_type)
12649 return NULL_TREE;
12652 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
12653 return NULL_TREE;
12655 /* Issue an error message if type-definitions are forbidden here. */
12656 if (!cp_parser_check_type_definition (parser))
12657 type = error_mark_node;
12658 else
12659 /* Create the new type. We do this before consuming the opening
12660 brace so the enum will be recorded as being on the line of its
12661 tag (or the 'enum' keyword, if there is no tag). */
12662 type = start_enum (identifier, underlying_type, scoped_enum_p);
12664 /* Consume the opening brace. */
12665 cp_lexer_consume_token (parser->lexer);
12667 if (type == error_mark_node)
12669 cp_parser_skip_to_end_of_block_or_statement (parser);
12670 return error_mark_node;
12673 /* If the next token is not '}', then there are some enumerators. */
12674 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12675 cp_parser_enumerator_list (parser, type);
12677 /* Consume the final '}'. */
12678 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12680 /* Look for trailing attributes to apply to this enumeration, and
12681 apply them if appropriate. */
12682 if (cp_parser_allow_gnu_extensions_p (parser))
12684 tree trailing_attr = cp_parser_attributes_opt (parser);
12685 trailing_attr = chainon (trailing_attr, attributes);
12686 cplus_decl_attributes (&type,
12687 trailing_attr,
12688 (int) ATTR_FLAG_TYPE_IN_PLACE);
12691 /* Finish up the enumeration. */
12692 finish_enum (type);
12694 return type;
12697 /* Parse an enumerator-list. The enumerators all have the indicated
12698 TYPE.
12700 enumerator-list:
12701 enumerator-definition
12702 enumerator-list , enumerator-definition */
12704 static void
12705 cp_parser_enumerator_list (cp_parser* parser, tree type)
12707 while (true)
12709 /* Parse an enumerator-definition. */
12710 cp_parser_enumerator_definition (parser, type);
12712 /* If the next token is not a ',', we've reached the end of
12713 the list. */
12714 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12715 break;
12716 /* Otherwise, consume the `,' and keep going. */
12717 cp_lexer_consume_token (parser->lexer);
12718 /* If the next token is a `}', there is a trailing comma. */
12719 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
12721 if (!in_system_header)
12722 pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
12723 break;
12728 /* Parse an enumerator-definition. The enumerator has the indicated
12729 TYPE.
12731 enumerator-definition:
12732 enumerator
12733 enumerator = constant-expression
12735 enumerator:
12736 identifier */
12738 static void
12739 cp_parser_enumerator_definition (cp_parser* parser, tree type)
12741 tree identifier;
12742 tree value;
12744 /* Look for the identifier. */
12745 identifier = cp_parser_identifier (parser);
12746 if (identifier == error_mark_node)
12747 return;
12749 /* If the next token is an '=', then there is an explicit value. */
12750 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12752 /* Consume the `=' token. */
12753 cp_lexer_consume_token (parser->lexer);
12754 /* Parse the value. */
12755 value = cp_parser_constant_expression (parser,
12756 /*allow_non_constant_p=*/false,
12757 NULL);
12759 else
12760 value = NULL_TREE;
12762 /* If we are processing a template, make sure the initializer of the
12763 enumerator doesn't contain any bare template parameter pack. */
12764 if (check_for_bare_parameter_packs (value))
12765 value = error_mark_node;
12767 /* Create the enumerator. */
12768 build_enumerator (identifier, value, type);
12771 /* Parse a namespace-name.
12773 namespace-name:
12774 original-namespace-name
12775 namespace-alias
12777 Returns the NAMESPACE_DECL for the namespace. */
12779 static tree
12780 cp_parser_namespace_name (cp_parser* parser)
12782 tree identifier;
12783 tree namespace_decl;
12785 cp_token *token = cp_lexer_peek_token (parser->lexer);
12787 /* Get the name of the namespace. */
12788 identifier = cp_parser_identifier (parser);
12789 if (identifier == error_mark_node)
12790 return error_mark_node;
12792 /* Look up the identifier in the currently active scope. Look only
12793 for namespaces, due to:
12795 [basic.lookup.udir]
12797 When looking up a namespace-name in a using-directive or alias
12798 definition, only namespace names are considered.
12800 And:
12802 [basic.lookup.qual]
12804 During the lookup of a name preceding the :: scope resolution
12805 operator, object, function, and enumerator names are ignored.
12807 (Note that cp_parser_qualifying_entity only calls this
12808 function if the token after the name is the scope resolution
12809 operator.) */
12810 namespace_decl = cp_parser_lookup_name (parser, identifier,
12811 none_type,
12812 /*is_template=*/false,
12813 /*is_namespace=*/true,
12814 /*check_dependency=*/true,
12815 /*ambiguous_decls=*/NULL,
12816 token->location);
12817 /* If it's not a namespace, issue an error. */
12818 if (namespace_decl == error_mark_node
12819 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
12821 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12822 error_at (token->location, "%qD is not a namespace-name", identifier);
12823 cp_parser_error (parser, "expected namespace-name");
12824 namespace_decl = error_mark_node;
12827 return namespace_decl;
12830 /* Parse a namespace-definition.
12832 namespace-definition:
12833 named-namespace-definition
12834 unnamed-namespace-definition
12836 named-namespace-definition:
12837 original-namespace-definition
12838 extension-namespace-definition
12840 original-namespace-definition:
12841 namespace identifier { namespace-body }
12843 extension-namespace-definition:
12844 namespace original-namespace-name { namespace-body }
12846 unnamed-namespace-definition:
12847 namespace { namespace-body } */
12849 static void
12850 cp_parser_namespace_definition (cp_parser* parser)
12852 tree identifier, attribs;
12853 bool has_visibility;
12854 bool is_inline;
12856 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
12858 is_inline = true;
12859 cp_lexer_consume_token (parser->lexer);
12861 else
12862 is_inline = false;
12864 /* Look for the `namespace' keyword. */
12865 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12867 /* Get the name of the namespace. We do not attempt to distinguish
12868 between an original-namespace-definition and an
12869 extension-namespace-definition at this point. The semantic
12870 analysis routines are responsible for that. */
12871 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12872 identifier = cp_parser_identifier (parser);
12873 else
12874 identifier = NULL_TREE;
12876 /* Parse any specified attributes. */
12877 attribs = cp_parser_attributes_opt (parser);
12879 /* Look for the `{' to start the namespace. */
12880 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
12881 /* Start the namespace. */
12882 push_namespace (identifier);
12884 /* "inline namespace" is equivalent to a stub namespace definition
12885 followed by a strong using directive. */
12886 if (is_inline)
12888 tree name_space = current_namespace;
12889 /* Set up namespace association. */
12890 DECL_NAMESPACE_ASSOCIATIONS (name_space)
12891 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
12892 DECL_NAMESPACE_ASSOCIATIONS (name_space));
12893 /* Import the contents of the inline namespace. */
12894 pop_namespace ();
12895 do_using_directive (name_space);
12896 push_namespace (identifier);
12899 has_visibility = handle_namespace_attrs (current_namespace, attribs);
12901 /* Parse the body of the namespace. */
12902 cp_parser_namespace_body (parser);
12904 #ifdef HANDLE_PRAGMA_VISIBILITY
12905 if (has_visibility)
12906 pop_visibility (1);
12907 #endif
12909 /* Finish the namespace. */
12910 pop_namespace ();
12911 /* Look for the final `}'. */
12912 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12915 /* Parse a namespace-body.
12917 namespace-body:
12918 declaration-seq [opt] */
12920 static void
12921 cp_parser_namespace_body (cp_parser* parser)
12923 cp_parser_declaration_seq_opt (parser);
12926 /* Parse a namespace-alias-definition.
12928 namespace-alias-definition:
12929 namespace identifier = qualified-namespace-specifier ; */
12931 static void
12932 cp_parser_namespace_alias_definition (cp_parser* parser)
12934 tree identifier;
12935 tree namespace_specifier;
12937 cp_token *token = cp_lexer_peek_token (parser->lexer);
12939 /* Look for the `namespace' keyword. */
12940 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12941 /* Look for the identifier. */
12942 identifier = cp_parser_identifier (parser);
12943 if (identifier == error_mark_node)
12944 return;
12945 /* Look for the `=' token. */
12946 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12947 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12949 error_at (token->location, "%<namespace%> definition is not allowed here");
12950 /* Skip the definition. */
12951 cp_lexer_consume_token (parser->lexer);
12952 if (cp_parser_skip_to_closing_brace (parser))
12953 cp_lexer_consume_token (parser->lexer);
12954 return;
12956 cp_parser_require (parser, CPP_EQ, "%<=%>");
12957 /* Look for the qualified-namespace-specifier. */
12958 namespace_specifier
12959 = cp_parser_qualified_namespace_specifier (parser);
12960 /* Look for the `;' token. */
12961 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12963 /* Register the alias in the symbol table. */
12964 do_namespace_alias (identifier, namespace_specifier);
12967 /* Parse a qualified-namespace-specifier.
12969 qualified-namespace-specifier:
12970 :: [opt] nested-name-specifier [opt] namespace-name
12972 Returns a NAMESPACE_DECL corresponding to the specified
12973 namespace. */
12975 static tree
12976 cp_parser_qualified_namespace_specifier (cp_parser* parser)
12978 /* Look for the optional `::'. */
12979 cp_parser_global_scope_opt (parser,
12980 /*current_scope_valid_p=*/false);
12982 /* Look for the optional nested-name-specifier. */
12983 cp_parser_nested_name_specifier_opt (parser,
12984 /*typename_keyword_p=*/false,
12985 /*check_dependency_p=*/true,
12986 /*type_p=*/false,
12987 /*is_declaration=*/true);
12989 return cp_parser_namespace_name (parser);
12992 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
12993 access declaration.
12995 using-declaration:
12996 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
12997 using :: unqualified-id ;
12999 access-declaration:
13000 qualified-id ;
13004 static bool
13005 cp_parser_using_declaration (cp_parser* parser,
13006 bool access_declaration_p)
13008 cp_token *token;
13009 bool typename_p = false;
13010 bool global_scope_p;
13011 tree decl;
13012 tree identifier;
13013 tree qscope;
13015 if (access_declaration_p)
13016 cp_parser_parse_tentatively (parser);
13017 else
13019 /* Look for the `using' keyword. */
13020 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
13022 /* Peek at the next token. */
13023 token = cp_lexer_peek_token (parser->lexer);
13024 /* See if it's `typename'. */
13025 if (token->keyword == RID_TYPENAME)
13027 /* Remember that we've seen it. */
13028 typename_p = true;
13029 /* Consume the `typename' token. */
13030 cp_lexer_consume_token (parser->lexer);
13034 /* Look for the optional global scope qualification. */
13035 global_scope_p
13036 = (cp_parser_global_scope_opt (parser,
13037 /*current_scope_valid_p=*/false)
13038 != NULL_TREE);
13040 /* If we saw `typename', or didn't see `::', then there must be a
13041 nested-name-specifier present. */
13042 if (typename_p || !global_scope_p)
13043 qscope = cp_parser_nested_name_specifier (parser, typename_p,
13044 /*check_dependency_p=*/true,
13045 /*type_p=*/false,
13046 /*is_declaration=*/true);
13047 /* Otherwise, we could be in either of the two productions. In that
13048 case, treat the nested-name-specifier as optional. */
13049 else
13050 qscope = cp_parser_nested_name_specifier_opt (parser,
13051 /*typename_keyword_p=*/false,
13052 /*check_dependency_p=*/true,
13053 /*type_p=*/false,
13054 /*is_declaration=*/true);
13055 if (!qscope)
13056 qscope = global_namespace;
13058 if (access_declaration_p && cp_parser_error_occurred (parser))
13059 /* Something has already gone wrong; there's no need to parse
13060 further. Since an error has occurred, the return value of
13061 cp_parser_parse_definitely will be false, as required. */
13062 return cp_parser_parse_definitely (parser);
13064 token = cp_lexer_peek_token (parser->lexer);
13065 /* Parse the unqualified-id. */
13066 identifier = cp_parser_unqualified_id (parser,
13067 /*template_keyword_p=*/false,
13068 /*check_dependency_p=*/true,
13069 /*declarator_p=*/true,
13070 /*optional_p=*/false);
13072 if (access_declaration_p)
13074 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13075 cp_parser_simulate_error (parser);
13076 if (!cp_parser_parse_definitely (parser))
13077 return false;
13080 /* The function we call to handle a using-declaration is different
13081 depending on what scope we are in. */
13082 if (qscope == error_mark_node || identifier == error_mark_node)
13084 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
13085 && TREE_CODE (identifier) != BIT_NOT_EXPR)
13086 /* [namespace.udecl]
13088 A using declaration shall not name a template-id. */
13089 error_at (token->location,
13090 "a template-id may not appear in a using-declaration");
13091 else
13093 if (at_class_scope_p ())
13095 /* Create the USING_DECL. */
13096 decl = do_class_using_decl (parser->scope, identifier);
13098 if (check_for_bare_parameter_packs (decl))
13099 return false;
13100 else
13101 /* Add it to the list of members in this class. */
13102 finish_member_declaration (decl);
13104 else
13106 decl = cp_parser_lookup_name_simple (parser,
13107 identifier,
13108 token->location);
13109 if (decl == error_mark_node)
13110 cp_parser_name_lookup_error (parser, identifier,
13111 decl, NULL,
13112 token->location);
13113 else if (check_for_bare_parameter_packs (decl))
13114 return false;
13115 else if (!at_namespace_scope_p ())
13116 do_local_using_decl (decl, qscope, identifier);
13117 else
13118 do_toplevel_using_decl (decl, qscope, identifier);
13122 /* Look for the final `;'. */
13123 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13125 return true;
13128 /* Parse a using-directive.
13130 using-directive:
13131 using namespace :: [opt] nested-name-specifier [opt]
13132 namespace-name ; */
13134 static void
13135 cp_parser_using_directive (cp_parser* parser)
13137 tree namespace_decl;
13138 tree attribs;
13140 /* Look for the `using' keyword. */
13141 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
13142 /* And the `namespace' keyword. */
13143 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
13144 /* Look for the optional `::' operator. */
13145 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13146 /* And the optional nested-name-specifier. */
13147 cp_parser_nested_name_specifier_opt (parser,
13148 /*typename_keyword_p=*/false,
13149 /*check_dependency_p=*/true,
13150 /*type_p=*/false,
13151 /*is_declaration=*/true);
13152 /* Get the namespace being used. */
13153 namespace_decl = cp_parser_namespace_name (parser);
13154 /* And any specified attributes. */
13155 attribs = cp_parser_attributes_opt (parser);
13156 /* Update the symbol table. */
13157 parse_using_directive (namespace_decl, attribs);
13158 /* Look for the final `;'. */
13159 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13162 /* Parse an asm-definition.
13164 asm-definition:
13165 asm ( string-literal ) ;
13167 GNU Extension:
13169 asm-definition:
13170 asm volatile [opt] ( string-literal ) ;
13171 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
13172 asm volatile [opt] ( string-literal : asm-operand-list [opt]
13173 : asm-operand-list [opt] ) ;
13174 asm volatile [opt] ( string-literal : asm-operand-list [opt]
13175 : asm-operand-list [opt]
13176 : asm-clobber-list [opt] ) ;
13177 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
13178 : asm-clobber-list [opt]
13179 : asm-goto-list ) ; */
13181 static void
13182 cp_parser_asm_definition (cp_parser* parser)
13184 tree string;
13185 tree outputs = NULL_TREE;
13186 tree inputs = NULL_TREE;
13187 tree clobbers = NULL_TREE;
13188 tree labels = NULL_TREE;
13189 tree asm_stmt;
13190 bool volatile_p = false;
13191 bool extended_p = false;
13192 bool invalid_inputs_p = false;
13193 bool invalid_outputs_p = false;
13194 bool goto_p = false;
13195 const char *missing = NULL;
13197 /* Look for the `asm' keyword. */
13198 cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
13199 /* See if the next token is `volatile'. */
13200 if (cp_parser_allow_gnu_extensions_p (parser)
13201 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
13203 /* Remember that we saw the `volatile' keyword. */
13204 volatile_p = true;
13205 /* Consume the token. */
13206 cp_lexer_consume_token (parser->lexer);
13208 if (cp_parser_allow_gnu_extensions_p (parser)
13209 && parser->in_function_body
13210 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
13212 /* Remember that we saw the `goto' keyword. */
13213 goto_p = true;
13214 /* Consume the token. */
13215 cp_lexer_consume_token (parser->lexer);
13217 /* Look for the opening `('. */
13218 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
13219 return;
13220 /* Look for the string. */
13221 string = cp_parser_string_literal (parser, false, false);
13222 if (string == error_mark_node)
13224 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13225 /*consume_paren=*/true);
13226 return;
13229 /* If we're allowing GNU extensions, check for the extended assembly
13230 syntax. Unfortunately, the `:' tokens need not be separated by
13231 a space in C, and so, for compatibility, we tolerate that here
13232 too. Doing that means that we have to treat the `::' operator as
13233 two `:' tokens. */
13234 if (cp_parser_allow_gnu_extensions_p (parser)
13235 && parser->in_function_body
13236 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
13237 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
13239 bool inputs_p = false;
13240 bool clobbers_p = false;
13241 bool labels_p = false;
13243 /* The extended syntax was used. */
13244 extended_p = true;
13246 /* Look for outputs. */
13247 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13249 /* Consume the `:'. */
13250 cp_lexer_consume_token (parser->lexer);
13251 /* Parse the output-operands. */
13252 if (cp_lexer_next_token_is_not (parser->lexer,
13253 CPP_COLON)
13254 && cp_lexer_next_token_is_not (parser->lexer,
13255 CPP_SCOPE)
13256 && cp_lexer_next_token_is_not (parser->lexer,
13257 CPP_CLOSE_PAREN)
13258 && !goto_p)
13259 outputs = cp_parser_asm_operand_list (parser);
13261 if (outputs == error_mark_node)
13262 invalid_outputs_p = true;
13264 /* If the next token is `::', there are no outputs, and the
13265 next token is the beginning of the inputs. */
13266 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13267 /* The inputs are coming next. */
13268 inputs_p = true;
13270 /* Look for inputs. */
13271 if (inputs_p
13272 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13274 /* Consume the `:' or `::'. */
13275 cp_lexer_consume_token (parser->lexer);
13276 /* Parse the output-operands. */
13277 if (cp_lexer_next_token_is_not (parser->lexer,
13278 CPP_COLON)
13279 && cp_lexer_next_token_is_not (parser->lexer,
13280 CPP_SCOPE)
13281 && cp_lexer_next_token_is_not (parser->lexer,
13282 CPP_CLOSE_PAREN))
13283 inputs = cp_parser_asm_operand_list (parser);
13285 if (inputs == error_mark_node)
13286 invalid_inputs_p = true;
13288 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13289 /* The clobbers are coming next. */
13290 clobbers_p = true;
13292 /* Look for clobbers. */
13293 if (clobbers_p
13294 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13296 clobbers_p = true;
13297 /* Consume the `:' or `::'. */
13298 cp_lexer_consume_token (parser->lexer);
13299 /* Parse the clobbers. */
13300 if (cp_lexer_next_token_is_not (parser->lexer,
13301 CPP_COLON)
13302 && cp_lexer_next_token_is_not (parser->lexer,
13303 CPP_CLOSE_PAREN))
13304 clobbers = cp_parser_asm_clobber_list (parser);
13306 else if (goto_p
13307 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13308 /* The labels are coming next. */
13309 labels_p = true;
13311 /* Look for labels. */
13312 if (labels_p
13313 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
13315 labels_p = true;
13316 /* Consume the `:' or `::'. */
13317 cp_lexer_consume_token (parser->lexer);
13318 /* Parse the labels. */
13319 labels = cp_parser_asm_label_list (parser);
13322 if (goto_p && !labels_p)
13323 missing = clobbers_p ? "%<:%>" : "%<:%> or %<::%>";
13325 else if (goto_p)
13326 missing = "%<:%> or %<::%>";
13328 /* Look for the closing `)'. */
13329 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
13330 missing ? missing : "%<)%>"))
13331 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13332 /*consume_paren=*/true);
13333 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13335 if (!invalid_inputs_p && !invalid_outputs_p)
13337 /* Create the ASM_EXPR. */
13338 if (parser->in_function_body)
13340 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
13341 inputs, clobbers, labels);
13342 /* If the extended syntax was not used, mark the ASM_EXPR. */
13343 if (!extended_p)
13345 tree temp = asm_stmt;
13346 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
13347 temp = TREE_OPERAND (temp, 0);
13349 ASM_INPUT_P (temp) = 1;
13352 else
13353 cgraph_add_asm_node (string);
13357 /* Declarators [gram.dcl.decl] */
13359 /* Parse an init-declarator.
13361 init-declarator:
13362 declarator initializer [opt]
13364 GNU Extension:
13366 init-declarator:
13367 declarator asm-specification [opt] attributes [opt] initializer [opt]
13369 function-definition:
13370 decl-specifier-seq [opt] declarator ctor-initializer [opt]
13371 function-body
13372 decl-specifier-seq [opt] declarator function-try-block
13374 GNU Extension:
13376 function-definition:
13377 __extension__ function-definition
13379 The DECL_SPECIFIERS apply to this declarator. Returns a
13380 representation of the entity declared. If MEMBER_P is TRUE, then
13381 this declarator appears in a class scope. The new DECL created by
13382 this declarator is returned.
13384 The CHECKS are access checks that should be performed once we know
13385 what entity is being declared (and, therefore, what classes have
13386 befriended it).
13388 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
13389 for a function-definition here as well. If the declarator is a
13390 declarator for a function-definition, *FUNCTION_DEFINITION_P will
13391 be TRUE upon return. By that point, the function-definition will
13392 have been completely parsed.
13394 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
13395 is FALSE. */
13397 static tree
13398 cp_parser_init_declarator (cp_parser* parser,
13399 cp_decl_specifier_seq *decl_specifiers,
13400 VEC (deferred_access_check,gc)* checks,
13401 bool function_definition_allowed_p,
13402 bool member_p,
13403 int declares_class_or_enum,
13404 bool* function_definition_p)
13406 cp_token *token = NULL, *asm_spec_start_token = NULL,
13407 *attributes_start_token = NULL;
13408 cp_declarator *declarator;
13409 tree prefix_attributes;
13410 tree attributes;
13411 tree asm_specification;
13412 tree initializer;
13413 tree decl = NULL_TREE;
13414 tree scope;
13415 int is_initialized;
13416 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
13417 initialized with "= ..", CPP_OPEN_PAREN if initialized with
13418 "(...)". */
13419 enum cpp_ttype initialization_kind;
13420 bool is_direct_init = false;
13421 bool is_non_constant_init;
13422 int ctor_dtor_or_conv_p;
13423 bool friend_p;
13424 tree pushed_scope = NULL;
13426 /* Gather the attributes that were provided with the
13427 decl-specifiers. */
13428 prefix_attributes = decl_specifiers->attributes;
13430 /* Assume that this is not the declarator for a function
13431 definition. */
13432 if (function_definition_p)
13433 *function_definition_p = false;
13435 /* Defer access checks while parsing the declarator; we cannot know
13436 what names are accessible until we know what is being
13437 declared. */
13438 resume_deferring_access_checks ();
13440 /* Parse the declarator. */
13441 token = cp_lexer_peek_token (parser->lexer);
13442 declarator
13443 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13444 &ctor_dtor_or_conv_p,
13445 /*parenthesized_p=*/NULL,
13446 /*member_p=*/false);
13447 /* Gather up the deferred checks. */
13448 stop_deferring_access_checks ();
13450 /* If the DECLARATOR was erroneous, there's no need to go
13451 further. */
13452 if (declarator == cp_error_declarator)
13453 return error_mark_node;
13455 /* Check that the number of template-parameter-lists is OK. */
13456 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
13457 token->location))
13458 return error_mark_node;
13460 if (declares_class_or_enum & 2)
13461 cp_parser_check_for_definition_in_return_type (declarator,
13462 decl_specifiers->type,
13463 decl_specifiers->type_location);
13465 /* Figure out what scope the entity declared by the DECLARATOR is
13466 located in. `grokdeclarator' sometimes changes the scope, so
13467 we compute it now. */
13468 scope = get_scope_of_declarator (declarator);
13470 /* Perform any lookups in the declared type which were thought to be
13471 dependent, but are not in the scope of the declarator. */
13472 decl_specifiers->type
13473 = maybe_update_decl_type (decl_specifiers->type, scope);
13475 /* If we're allowing GNU extensions, look for an asm-specification
13476 and attributes. */
13477 if (cp_parser_allow_gnu_extensions_p (parser))
13479 /* Look for an asm-specification. */
13480 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
13481 asm_specification = cp_parser_asm_specification_opt (parser);
13482 /* And attributes. */
13483 attributes_start_token = cp_lexer_peek_token (parser->lexer);
13484 attributes = cp_parser_attributes_opt (parser);
13486 else
13488 asm_specification = NULL_TREE;
13489 attributes = NULL_TREE;
13492 /* Peek at the next token. */
13493 token = cp_lexer_peek_token (parser->lexer);
13494 /* Check to see if the token indicates the start of a
13495 function-definition. */
13496 if (function_declarator_p (declarator)
13497 && cp_parser_token_starts_function_definition_p (token))
13499 if (!function_definition_allowed_p)
13501 /* If a function-definition should not appear here, issue an
13502 error message. */
13503 cp_parser_error (parser,
13504 "a function-definition is not allowed here");
13505 return error_mark_node;
13507 else
13509 location_t func_brace_location
13510 = cp_lexer_peek_token (parser->lexer)->location;
13512 /* Neither attributes nor an asm-specification are allowed
13513 on a function-definition. */
13514 if (asm_specification)
13515 error_at (asm_spec_start_token->location,
13516 "an asm-specification is not allowed "
13517 "on a function-definition");
13518 if (attributes)
13519 error_at (attributes_start_token->location,
13520 "attributes are not allowed on a function-definition");
13521 /* This is a function-definition. */
13522 *function_definition_p = true;
13524 /* Parse the function definition. */
13525 if (member_p)
13526 decl = cp_parser_save_member_function_body (parser,
13527 decl_specifiers,
13528 declarator,
13529 prefix_attributes);
13530 else
13531 decl
13532 = (cp_parser_function_definition_from_specifiers_and_declarator
13533 (parser, decl_specifiers, prefix_attributes, declarator));
13535 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
13537 /* This is where the prologue starts... */
13538 DECL_STRUCT_FUNCTION (decl)->function_start_locus
13539 = func_brace_location;
13542 return decl;
13546 /* [dcl.dcl]
13548 Only in function declarations for constructors, destructors, and
13549 type conversions can the decl-specifier-seq be omitted.
13551 We explicitly postpone this check past the point where we handle
13552 function-definitions because we tolerate function-definitions
13553 that are missing their return types in some modes. */
13554 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
13556 cp_parser_error (parser,
13557 "expected constructor, destructor, or type conversion");
13558 return error_mark_node;
13561 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
13562 if (token->type == CPP_EQ
13563 || token->type == CPP_OPEN_PAREN
13564 || token->type == CPP_OPEN_BRACE)
13566 is_initialized = SD_INITIALIZED;
13567 initialization_kind = token->type;
13569 if (token->type == CPP_EQ
13570 && function_declarator_p (declarator))
13572 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13573 if (t2->keyword == RID_DEFAULT)
13574 is_initialized = SD_DEFAULTED;
13575 else if (t2->keyword == RID_DELETE)
13576 is_initialized = SD_DELETED;
13579 else
13581 /* If the init-declarator isn't initialized and isn't followed by a
13582 `,' or `;', it's not a valid init-declarator. */
13583 if (token->type != CPP_COMMA
13584 && token->type != CPP_SEMICOLON)
13586 cp_parser_error (parser, "expected initializer");
13587 return error_mark_node;
13589 is_initialized = SD_UNINITIALIZED;
13590 initialization_kind = CPP_EOF;
13593 /* Because start_decl has side-effects, we should only call it if we
13594 know we're going ahead. By this point, we know that we cannot
13595 possibly be looking at any other construct. */
13596 cp_parser_commit_to_tentative_parse (parser);
13598 /* If the decl specifiers were bad, issue an error now that we're
13599 sure this was intended to be a declarator. Then continue
13600 declaring the variable(s), as int, to try to cut down on further
13601 errors. */
13602 if (decl_specifiers->any_specifiers_p
13603 && decl_specifiers->type == error_mark_node)
13605 cp_parser_error (parser, "invalid type in declaration");
13606 decl_specifiers->type = integer_type_node;
13609 /* Check to see whether or not this declaration is a friend. */
13610 friend_p = cp_parser_friend_p (decl_specifiers);
13612 /* Enter the newly declared entry in the symbol table. If we're
13613 processing a declaration in a class-specifier, we wait until
13614 after processing the initializer. */
13615 if (!member_p)
13617 if (parser->in_unbraced_linkage_specification_p)
13618 decl_specifiers->storage_class = sc_extern;
13619 decl = start_decl (declarator, decl_specifiers,
13620 is_initialized, attributes, prefix_attributes,
13621 &pushed_scope);
13623 else if (scope)
13624 /* Enter the SCOPE. That way unqualified names appearing in the
13625 initializer will be looked up in SCOPE. */
13626 pushed_scope = push_scope (scope);
13628 /* Perform deferred access control checks, now that we know in which
13629 SCOPE the declared entity resides. */
13630 if (!member_p && decl)
13632 tree saved_current_function_decl = NULL_TREE;
13634 /* If the entity being declared is a function, pretend that we
13635 are in its scope. If it is a `friend', it may have access to
13636 things that would not otherwise be accessible. */
13637 if (TREE_CODE (decl) == FUNCTION_DECL)
13639 saved_current_function_decl = current_function_decl;
13640 current_function_decl = decl;
13643 /* Perform access checks for template parameters. */
13644 cp_parser_perform_template_parameter_access_checks (checks);
13646 /* Perform the access control checks for the declarator and the
13647 decl-specifiers. */
13648 perform_deferred_access_checks ();
13650 /* Restore the saved value. */
13651 if (TREE_CODE (decl) == FUNCTION_DECL)
13652 current_function_decl = saved_current_function_decl;
13655 /* Parse the initializer. */
13656 initializer = NULL_TREE;
13657 is_direct_init = false;
13658 is_non_constant_init = true;
13659 if (is_initialized)
13661 if (function_declarator_p (declarator))
13663 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
13664 if (initialization_kind == CPP_EQ)
13665 initializer = cp_parser_pure_specifier (parser);
13666 else
13668 /* If the declaration was erroneous, we don't really
13669 know what the user intended, so just silently
13670 consume the initializer. */
13671 if (decl != error_mark_node)
13672 error_at (initializer_start_token->location,
13673 "initializer provided for function");
13674 cp_parser_skip_to_closing_parenthesis (parser,
13675 /*recovering=*/true,
13676 /*or_comma=*/false,
13677 /*consume_paren=*/true);
13680 else
13682 /* We want to record the extra mangling scope for in-class
13683 initializers of class members and initializers of static data
13684 member templates. The former is a C++0x feature which isn't
13685 implemented yet, and I expect it will involve deferring
13686 parsing of the initializer until end of class as with default
13687 arguments. So right here we only handle the latter. */
13688 if (!member_p && processing_template_decl)
13689 start_lambda_scope (decl);
13690 initializer = cp_parser_initializer (parser,
13691 &is_direct_init,
13692 &is_non_constant_init);
13693 if (!member_p && processing_template_decl)
13694 finish_lambda_scope ();
13698 /* The old parser allows attributes to appear after a parenthesized
13699 initializer. Mark Mitchell proposed removing this functionality
13700 on the GCC mailing lists on 2002-08-13. This parser accepts the
13701 attributes -- but ignores them. */
13702 if (cp_parser_allow_gnu_extensions_p (parser)
13703 && initialization_kind == CPP_OPEN_PAREN)
13704 if (cp_parser_attributes_opt (parser))
13705 warning (OPT_Wattributes,
13706 "attributes after parenthesized initializer ignored");
13708 /* For an in-class declaration, use `grokfield' to create the
13709 declaration. */
13710 if (member_p)
13712 if (pushed_scope)
13714 pop_scope (pushed_scope);
13715 pushed_scope = false;
13717 decl = grokfield (declarator, decl_specifiers,
13718 initializer, !is_non_constant_init,
13719 /*asmspec=*/NULL_TREE,
13720 prefix_attributes);
13721 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
13722 cp_parser_save_default_args (parser, decl);
13725 /* Finish processing the declaration. But, skip friend
13726 declarations. */
13727 if (!friend_p && decl && decl != error_mark_node)
13729 cp_finish_decl (decl,
13730 initializer, !is_non_constant_init,
13731 asm_specification,
13732 /* If the initializer is in parentheses, then this is
13733 a direct-initialization, which means that an
13734 `explicit' constructor is OK. Otherwise, an
13735 `explicit' constructor cannot be used. */
13736 ((is_direct_init || !is_initialized)
13737 ? 0 : LOOKUP_ONLYCONVERTING));
13739 else if ((cxx_dialect != cxx98) && friend_p
13740 && decl && TREE_CODE (decl) == FUNCTION_DECL)
13741 /* Core issue #226 (C++0x only): A default template-argument
13742 shall not be specified in a friend class template
13743 declaration. */
13744 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
13745 /*is_partial=*/0, /*is_friend_decl=*/1);
13747 if (!friend_p && pushed_scope)
13748 pop_scope (pushed_scope);
13750 return decl;
13753 /* Parse a declarator.
13755 declarator:
13756 direct-declarator
13757 ptr-operator declarator
13759 abstract-declarator:
13760 ptr-operator abstract-declarator [opt]
13761 direct-abstract-declarator
13763 GNU Extensions:
13765 declarator:
13766 attributes [opt] direct-declarator
13767 attributes [opt] ptr-operator declarator
13769 abstract-declarator:
13770 attributes [opt] ptr-operator abstract-declarator [opt]
13771 attributes [opt] direct-abstract-declarator
13773 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
13774 detect constructor, destructor or conversion operators. It is set
13775 to -1 if the declarator is a name, and +1 if it is a
13776 function. Otherwise it is set to zero. Usually you just want to
13777 test for >0, but internally the negative value is used.
13779 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
13780 a decl-specifier-seq unless it declares a constructor, destructor,
13781 or conversion. It might seem that we could check this condition in
13782 semantic analysis, rather than parsing, but that makes it difficult
13783 to handle something like `f()'. We want to notice that there are
13784 no decl-specifiers, and therefore realize that this is an
13785 expression, not a declaration.)
13787 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
13788 the declarator is a direct-declarator of the form "(...)".
13790 MEMBER_P is true iff this declarator is a member-declarator. */
13792 static cp_declarator *
13793 cp_parser_declarator (cp_parser* parser,
13794 cp_parser_declarator_kind dcl_kind,
13795 int* ctor_dtor_or_conv_p,
13796 bool* parenthesized_p,
13797 bool member_p)
13799 cp_declarator *declarator;
13800 enum tree_code code;
13801 cp_cv_quals cv_quals;
13802 tree class_type;
13803 tree attributes = NULL_TREE;
13805 /* Assume this is not a constructor, destructor, or type-conversion
13806 operator. */
13807 if (ctor_dtor_or_conv_p)
13808 *ctor_dtor_or_conv_p = 0;
13810 if (cp_parser_allow_gnu_extensions_p (parser))
13811 attributes = cp_parser_attributes_opt (parser);
13813 /* Check for the ptr-operator production. */
13814 cp_parser_parse_tentatively (parser);
13815 /* Parse the ptr-operator. */
13816 code = cp_parser_ptr_operator (parser,
13817 &class_type,
13818 &cv_quals);
13819 /* If that worked, then we have a ptr-operator. */
13820 if (cp_parser_parse_definitely (parser))
13822 /* If a ptr-operator was found, then this declarator was not
13823 parenthesized. */
13824 if (parenthesized_p)
13825 *parenthesized_p = true;
13826 /* The dependent declarator is optional if we are parsing an
13827 abstract-declarator. */
13828 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13829 cp_parser_parse_tentatively (parser);
13831 /* Parse the dependent declarator. */
13832 declarator = cp_parser_declarator (parser, dcl_kind,
13833 /*ctor_dtor_or_conv_p=*/NULL,
13834 /*parenthesized_p=*/NULL,
13835 /*member_p=*/false);
13837 /* If we are parsing an abstract-declarator, we must handle the
13838 case where the dependent declarator is absent. */
13839 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
13840 && !cp_parser_parse_definitely (parser))
13841 declarator = NULL;
13843 declarator = cp_parser_make_indirect_declarator
13844 (code, class_type, cv_quals, declarator);
13846 /* Everything else is a direct-declarator. */
13847 else
13849 if (parenthesized_p)
13850 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
13851 CPP_OPEN_PAREN);
13852 declarator = cp_parser_direct_declarator (parser, dcl_kind,
13853 ctor_dtor_or_conv_p,
13854 member_p);
13857 if (attributes && declarator && declarator != cp_error_declarator)
13858 declarator->attributes = attributes;
13860 return declarator;
13863 /* Parse a direct-declarator or direct-abstract-declarator.
13865 direct-declarator:
13866 declarator-id
13867 direct-declarator ( parameter-declaration-clause )
13868 cv-qualifier-seq [opt]
13869 exception-specification [opt]
13870 direct-declarator [ constant-expression [opt] ]
13871 ( declarator )
13873 direct-abstract-declarator:
13874 direct-abstract-declarator [opt]
13875 ( parameter-declaration-clause )
13876 cv-qualifier-seq [opt]
13877 exception-specification [opt]
13878 direct-abstract-declarator [opt] [ constant-expression [opt] ]
13879 ( abstract-declarator )
13881 Returns a representation of the declarator. DCL_KIND is
13882 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
13883 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
13884 we are parsing a direct-declarator. It is
13885 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
13886 of ambiguity we prefer an abstract declarator, as per
13887 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
13888 cp_parser_declarator. */
13890 static cp_declarator *
13891 cp_parser_direct_declarator (cp_parser* parser,
13892 cp_parser_declarator_kind dcl_kind,
13893 int* ctor_dtor_or_conv_p,
13894 bool member_p)
13896 cp_token *token;
13897 cp_declarator *declarator = NULL;
13898 tree scope = NULL_TREE;
13899 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13900 bool saved_in_declarator_p = parser->in_declarator_p;
13901 bool first = true;
13902 tree pushed_scope = NULL_TREE;
13904 while (true)
13906 /* Peek at the next token. */
13907 token = cp_lexer_peek_token (parser->lexer);
13908 if (token->type == CPP_OPEN_PAREN)
13910 /* This is either a parameter-declaration-clause, or a
13911 parenthesized declarator. When we know we are parsing a
13912 named declarator, it must be a parenthesized declarator
13913 if FIRST is true. For instance, `(int)' is a
13914 parameter-declaration-clause, with an omitted
13915 direct-abstract-declarator. But `((*))', is a
13916 parenthesized abstract declarator. Finally, when T is a
13917 template parameter `(T)' is a
13918 parameter-declaration-clause, and not a parenthesized
13919 named declarator.
13921 We first try and parse a parameter-declaration-clause,
13922 and then try a nested declarator (if FIRST is true).
13924 It is not an error for it not to be a
13925 parameter-declaration-clause, even when FIRST is
13926 false. Consider,
13928 int i (int);
13929 int i (3);
13931 The first is the declaration of a function while the
13932 second is the definition of a variable, including its
13933 initializer.
13935 Having seen only the parenthesis, we cannot know which of
13936 these two alternatives should be selected. Even more
13937 complex are examples like:
13939 int i (int (a));
13940 int i (int (3));
13942 The former is a function-declaration; the latter is a
13943 variable initialization.
13945 Thus again, we try a parameter-declaration-clause, and if
13946 that fails, we back out and return. */
13948 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13950 tree params;
13951 unsigned saved_num_template_parameter_lists;
13952 bool is_declarator = false;
13953 tree t;
13955 /* In a member-declarator, the only valid interpretation
13956 of a parenthesis is the start of a
13957 parameter-declaration-clause. (It is invalid to
13958 initialize a static data member with a parenthesized
13959 initializer; only the "=" form of initialization is
13960 permitted.) */
13961 if (!member_p)
13962 cp_parser_parse_tentatively (parser);
13964 /* Consume the `('. */
13965 cp_lexer_consume_token (parser->lexer);
13966 if (first)
13968 /* If this is going to be an abstract declarator, we're
13969 in a declarator and we can't have default args. */
13970 parser->default_arg_ok_p = false;
13971 parser->in_declarator_p = true;
13974 /* Inside the function parameter list, surrounding
13975 template-parameter-lists do not apply. */
13976 saved_num_template_parameter_lists
13977 = parser->num_template_parameter_lists;
13978 parser->num_template_parameter_lists = 0;
13980 begin_scope (sk_function_parms, NULL_TREE);
13982 /* Parse the parameter-declaration-clause. */
13983 params = cp_parser_parameter_declaration_clause (parser);
13985 parser->num_template_parameter_lists
13986 = saved_num_template_parameter_lists;
13988 /* If all went well, parse the cv-qualifier-seq and the
13989 exception-specification. */
13990 if (member_p || cp_parser_parse_definitely (parser))
13992 cp_cv_quals cv_quals;
13993 tree exception_specification;
13994 tree late_return;
13996 is_declarator = true;
13998 if (ctor_dtor_or_conv_p)
13999 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
14000 first = false;
14001 /* Consume the `)'. */
14002 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
14004 /* Parse the cv-qualifier-seq. */
14005 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14006 /* And the exception-specification. */
14007 exception_specification
14008 = cp_parser_exception_specification_opt (parser);
14010 late_return
14011 = cp_parser_late_return_type_opt (parser);
14013 /* Create the function-declarator. */
14014 declarator = make_call_declarator (declarator,
14015 params,
14016 cv_quals,
14017 exception_specification,
14018 late_return);
14019 /* Any subsequent parameter lists are to do with
14020 return type, so are not those of the declared
14021 function. */
14022 parser->default_arg_ok_p = false;
14025 /* Remove the function parms from scope. */
14026 for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
14027 pop_binding (DECL_NAME (t), t);
14028 leave_scope();
14030 if (is_declarator)
14031 /* Repeat the main loop. */
14032 continue;
14035 /* If this is the first, we can try a parenthesized
14036 declarator. */
14037 if (first)
14039 bool saved_in_type_id_in_expr_p;
14041 parser->default_arg_ok_p = saved_default_arg_ok_p;
14042 parser->in_declarator_p = saved_in_declarator_p;
14044 /* Consume the `('. */
14045 cp_lexer_consume_token (parser->lexer);
14046 /* Parse the nested declarator. */
14047 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14048 parser->in_type_id_in_expr_p = true;
14049 declarator
14050 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
14051 /*parenthesized_p=*/NULL,
14052 member_p);
14053 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14054 first = false;
14055 /* Expect a `)'. */
14056 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
14057 declarator = cp_error_declarator;
14058 if (declarator == cp_error_declarator)
14059 break;
14061 goto handle_declarator;
14063 /* Otherwise, we must be done. */
14064 else
14065 break;
14067 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14068 && token->type == CPP_OPEN_SQUARE)
14070 /* Parse an array-declarator. */
14071 tree bounds;
14073 if (ctor_dtor_or_conv_p)
14074 *ctor_dtor_or_conv_p = 0;
14076 first = false;
14077 parser->default_arg_ok_p = false;
14078 parser->in_declarator_p = true;
14079 /* Consume the `['. */
14080 cp_lexer_consume_token (parser->lexer);
14081 /* Peek at the next token. */
14082 token = cp_lexer_peek_token (parser->lexer);
14083 /* If the next token is `]', then there is no
14084 constant-expression. */
14085 if (token->type != CPP_CLOSE_SQUARE)
14087 bool non_constant_p;
14089 bounds
14090 = cp_parser_constant_expression (parser,
14091 /*allow_non_constant=*/true,
14092 &non_constant_p);
14093 if (!non_constant_p)
14094 bounds = fold_non_dependent_expr (bounds);
14095 /* Normally, the array bound must be an integral constant
14096 expression. However, as an extension, we allow VLAs
14097 in function scopes. */
14098 else if (!parser->in_function_body)
14100 error_at (token->location,
14101 "array bound is not an integer constant");
14102 bounds = error_mark_node;
14104 else if (processing_template_decl && !error_operand_p (bounds))
14106 /* Remember this wasn't a constant-expression. */
14107 bounds = build_nop (TREE_TYPE (bounds), bounds);
14108 TREE_SIDE_EFFECTS (bounds) = 1;
14111 else
14112 bounds = NULL_TREE;
14113 /* Look for the closing `]'. */
14114 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
14116 declarator = cp_error_declarator;
14117 break;
14120 declarator = make_array_declarator (declarator, bounds);
14122 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
14125 tree qualifying_scope;
14126 tree unqualified_name;
14127 special_function_kind sfk;
14128 bool abstract_ok;
14129 bool pack_expansion_p = false;
14130 cp_token *declarator_id_start_token;
14132 /* Parse a declarator-id */
14133 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
14134 if (abstract_ok)
14136 cp_parser_parse_tentatively (parser);
14138 /* If we see an ellipsis, we should be looking at a
14139 parameter pack. */
14140 if (token->type == CPP_ELLIPSIS)
14142 /* Consume the `...' */
14143 cp_lexer_consume_token (parser->lexer);
14145 pack_expansion_p = true;
14149 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
14150 unqualified_name
14151 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
14152 qualifying_scope = parser->scope;
14153 if (abstract_ok)
14155 bool okay = false;
14157 if (!unqualified_name && pack_expansion_p)
14159 /* Check whether an error occurred. */
14160 okay = !cp_parser_error_occurred (parser);
14162 /* We already consumed the ellipsis to mark a
14163 parameter pack, but we have no way to report it,
14164 so abort the tentative parse. We will be exiting
14165 immediately anyway. */
14166 cp_parser_abort_tentative_parse (parser);
14168 else
14169 okay = cp_parser_parse_definitely (parser);
14171 if (!okay)
14172 unqualified_name = error_mark_node;
14173 else if (unqualified_name
14174 && (qualifying_scope
14175 || (TREE_CODE (unqualified_name)
14176 != IDENTIFIER_NODE)))
14178 cp_parser_error (parser, "expected unqualified-id");
14179 unqualified_name = error_mark_node;
14183 if (!unqualified_name)
14184 return NULL;
14185 if (unqualified_name == error_mark_node)
14187 declarator = cp_error_declarator;
14188 pack_expansion_p = false;
14189 declarator->parameter_pack_p = false;
14190 break;
14193 if (qualifying_scope && at_namespace_scope_p ()
14194 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
14196 /* In the declaration of a member of a template class
14197 outside of the class itself, the SCOPE will sometimes
14198 be a TYPENAME_TYPE. For example, given:
14200 template <typename T>
14201 int S<T>::R::i = 3;
14203 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
14204 this context, we must resolve S<T>::R to an ordinary
14205 type, rather than a typename type.
14207 The reason we normally avoid resolving TYPENAME_TYPEs
14208 is that a specialization of `S' might render
14209 `S<T>::R' not a type. However, if `S' is
14210 specialized, then this `i' will not be used, so there
14211 is no harm in resolving the types here. */
14212 tree type;
14214 /* Resolve the TYPENAME_TYPE. */
14215 type = resolve_typename_type (qualifying_scope,
14216 /*only_current_p=*/false);
14217 /* If that failed, the declarator is invalid. */
14218 if (TREE_CODE (type) == TYPENAME_TYPE)
14220 if (typedef_variant_p (type))
14221 error_at (declarator_id_start_token->location,
14222 "cannot define member of dependent typedef "
14223 "%qT", type);
14224 else
14225 error_at (declarator_id_start_token->location,
14226 "%<%T::%E%> is not a type",
14227 TYPE_CONTEXT (qualifying_scope),
14228 TYPE_IDENTIFIER (qualifying_scope));
14230 qualifying_scope = type;
14233 sfk = sfk_none;
14235 if (unqualified_name)
14237 tree class_type;
14239 if (qualifying_scope
14240 && CLASS_TYPE_P (qualifying_scope))
14241 class_type = qualifying_scope;
14242 else
14243 class_type = current_class_type;
14245 if (TREE_CODE (unqualified_name) == TYPE_DECL)
14247 tree name_type = TREE_TYPE (unqualified_name);
14248 if (class_type && same_type_p (name_type, class_type))
14250 if (qualifying_scope
14251 && CLASSTYPE_USE_TEMPLATE (name_type))
14253 error_at (declarator_id_start_token->location,
14254 "invalid use of constructor as a template");
14255 inform (declarator_id_start_token->location,
14256 "use %<%T::%D%> instead of %<%T::%D%> to "
14257 "name the constructor in a qualified name",
14258 class_type,
14259 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
14260 class_type, name_type);
14261 declarator = cp_error_declarator;
14262 break;
14264 else
14265 unqualified_name = constructor_name (class_type);
14267 else
14269 /* We do not attempt to print the declarator
14270 here because we do not have enough
14271 information about its original syntactic
14272 form. */
14273 cp_parser_error (parser, "invalid declarator");
14274 declarator = cp_error_declarator;
14275 break;
14279 if (class_type)
14281 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
14282 sfk = sfk_destructor;
14283 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
14284 sfk = sfk_conversion;
14285 else if (/* There's no way to declare a constructor
14286 for an anonymous type, even if the type
14287 got a name for linkage purposes. */
14288 !TYPE_WAS_ANONYMOUS (class_type)
14289 && constructor_name_p (unqualified_name,
14290 class_type))
14292 unqualified_name = constructor_name (class_type);
14293 sfk = sfk_constructor;
14295 else if (is_overloaded_fn (unqualified_name)
14296 && DECL_CONSTRUCTOR_P (get_first_fn
14297 (unqualified_name)))
14298 sfk = sfk_constructor;
14300 if (ctor_dtor_or_conv_p && sfk != sfk_none)
14301 *ctor_dtor_or_conv_p = -1;
14304 declarator = make_id_declarator (qualifying_scope,
14305 unqualified_name,
14306 sfk);
14307 declarator->id_loc = token->location;
14308 declarator->parameter_pack_p = pack_expansion_p;
14310 if (pack_expansion_p)
14311 maybe_warn_variadic_templates ();
14314 handle_declarator:;
14315 scope = get_scope_of_declarator (declarator);
14316 if (scope)
14317 /* Any names that appear after the declarator-id for a
14318 member are looked up in the containing scope. */
14319 pushed_scope = push_scope (scope);
14320 parser->in_declarator_p = true;
14321 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
14322 || (declarator && declarator->kind == cdk_id))
14323 /* Default args are only allowed on function
14324 declarations. */
14325 parser->default_arg_ok_p = saved_default_arg_ok_p;
14326 else
14327 parser->default_arg_ok_p = false;
14329 first = false;
14331 /* We're done. */
14332 else
14333 break;
14336 /* For an abstract declarator, we might wind up with nothing at this
14337 point. That's an error; the declarator is not optional. */
14338 if (!declarator)
14339 cp_parser_error (parser, "expected declarator");
14341 /* If we entered a scope, we must exit it now. */
14342 if (pushed_scope)
14343 pop_scope (pushed_scope);
14345 parser->default_arg_ok_p = saved_default_arg_ok_p;
14346 parser->in_declarator_p = saved_in_declarator_p;
14348 return declarator;
14351 /* Parse a ptr-operator.
14353 ptr-operator:
14354 * cv-qualifier-seq [opt]
14356 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
14358 GNU Extension:
14360 ptr-operator:
14361 & cv-qualifier-seq [opt]
14363 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
14364 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
14365 an rvalue reference. In the case of a pointer-to-member, *TYPE is
14366 filled in with the TYPE containing the member. *CV_QUALS is
14367 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
14368 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
14369 Note that the tree codes returned by this function have nothing
14370 to do with the types of trees that will be eventually be created
14371 to represent the pointer or reference type being parsed. They are
14372 just constants with suggestive names. */
14373 static enum tree_code
14374 cp_parser_ptr_operator (cp_parser* parser,
14375 tree* type,
14376 cp_cv_quals *cv_quals)
14378 enum tree_code code = ERROR_MARK;
14379 cp_token *token;
14381 /* Assume that it's not a pointer-to-member. */
14382 *type = NULL_TREE;
14383 /* And that there are no cv-qualifiers. */
14384 *cv_quals = TYPE_UNQUALIFIED;
14386 /* Peek at the next token. */
14387 token = cp_lexer_peek_token (parser->lexer);
14389 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
14390 if (token->type == CPP_MULT)
14391 code = INDIRECT_REF;
14392 else if (token->type == CPP_AND)
14393 code = ADDR_EXPR;
14394 else if ((cxx_dialect != cxx98) &&
14395 token->type == CPP_AND_AND) /* C++0x only */
14396 code = NON_LVALUE_EXPR;
14398 if (code != ERROR_MARK)
14400 /* Consume the `*', `&' or `&&'. */
14401 cp_lexer_consume_token (parser->lexer);
14403 /* A `*' can be followed by a cv-qualifier-seq, and so can a
14404 `&', if we are allowing GNU extensions. (The only qualifier
14405 that can legally appear after `&' is `restrict', but that is
14406 enforced during semantic analysis. */
14407 if (code == INDIRECT_REF
14408 || cp_parser_allow_gnu_extensions_p (parser))
14409 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14411 else
14413 /* Try the pointer-to-member case. */
14414 cp_parser_parse_tentatively (parser);
14415 /* Look for the optional `::' operator. */
14416 cp_parser_global_scope_opt (parser,
14417 /*current_scope_valid_p=*/false);
14418 /* Look for the nested-name specifier. */
14419 token = cp_lexer_peek_token (parser->lexer);
14420 cp_parser_nested_name_specifier (parser,
14421 /*typename_keyword_p=*/false,
14422 /*check_dependency_p=*/true,
14423 /*type_p=*/false,
14424 /*is_declaration=*/false);
14425 /* If we found it, and the next token is a `*', then we are
14426 indeed looking at a pointer-to-member operator. */
14427 if (!cp_parser_error_occurred (parser)
14428 && cp_parser_require (parser, CPP_MULT, "%<*%>"))
14430 /* Indicate that the `*' operator was used. */
14431 code = INDIRECT_REF;
14433 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
14434 error_at (token->location, "%qD is a namespace", parser->scope);
14435 else
14437 /* The type of which the member is a member is given by the
14438 current SCOPE. */
14439 *type = parser->scope;
14440 /* The next name will not be qualified. */
14441 parser->scope = NULL_TREE;
14442 parser->qualifying_scope = NULL_TREE;
14443 parser->object_scope = NULL_TREE;
14444 /* Look for the optional cv-qualifier-seq. */
14445 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14448 /* If that didn't work we don't have a ptr-operator. */
14449 if (!cp_parser_parse_definitely (parser))
14450 cp_parser_error (parser, "expected ptr-operator");
14453 return code;
14456 /* Parse an (optional) cv-qualifier-seq.
14458 cv-qualifier-seq:
14459 cv-qualifier cv-qualifier-seq [opt]
14461 cv-qualifier:
14462 const
14463 volatile
14465 GNU Extension:
14467 cv-qualifier:
14468 __restrict__
14470 Returns a bitmask representing the cv-qualifiers. */
14472 static cp_cv_quals
14473 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
14475 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
14477 while (true)
14479 cp_token *token;
14480 cp_cv_quals cv_qualifier;
14482 /* Peek at the next token. */
14483 token = cp_lexer_peek_token (parser->lexer);
14484 /* See if it's a cv-qualifier. */
14485 switch (token->keyword)
14487 case RID_CONST:
14488 cv_qualifier = TYPE_QUAL_CONST;
14489 break;
14491 case RID_VOLATILE:
14492 cv_qualifier = TYPE_QUAL_VOLATILE;
14493 break;
14495 case RID_RESTRICT:
14496 cv_qualifier = TYPE_QUAL_RESTRICT;
14497 break;
14499 default:
14500 cv_qualifier = TYPE_UNQUALIFIED;
14501 break;
14504 if (!cv_qualifier)
14505 break;
14507 if (cv_quals & cv_qualifier)
14509 error_at (token->location, "duplicate cv-qualifier");
14510 cp_lexer_purge_token (parser->lexer);
14512 else
14514 cp_lexer_consume_token (parser->lexer);
14515 cv_quals |= cv_qualifier;
14519 return cv_quals;
14522 /* Parse a late-specified return type, if any. This is not a separate
14523 non-terminal, but part of a function declarator, which looks like
14525 -> trailing-type-specifier-seq abstract-declarator(opt)
14527 Returns the type indicated by the type-id. */
14529 static tree
14530 cp_parser_late_return_type_opt (cp_parser* parser)
14532 cp_token *token;
14534 /* Peek at the next token. */
14535 token = cp_lexer_peek_token (parser->lexer);
14536 /* A late-specified return type is indicated by an initial '->'. */
14537 if (token->type != CPP_DEREF)
14538 return NULL_TREE;
14540 /* Consume the ->. */
14541 cp_lexer_consume_token (parser->lexer);
14543 return cp_parser_trailing_type_id (parser);
14546 /* Parse a declarator-id.
14548 declarator-id:
14549 id-expression
14550 :: [opt] nested-name-specifier [opt] type-name
14552 In the `id-expression' case, the value returned is as for
14553 cp_parser_id_expression if the id-expression was an unqualified-id.
14554 If the id-expression was a qualified-id, then a SCOPE_REF is
14555 returned. The first operand is the scope (either a NAMESPACE_DECL
14556 or TREE_TYPE), but the second is still just a representation of an
14557 unqualified-id. */
14559 static tree
14560 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
14562 tree id;
14563 /* The expression must be an id-expression. Assume that qualified
14564 names are the names of types so that:
14566 template <class T>
14567 int S<T>::R::i = 3;
14569 will work; we must treat `S<T>::R' as the name of a type.
14570 Similarly, assume that qualified names are templates, where
14571 required, so that:
14573 template <class T>
14574 int S<T>::R<T>::i = 3;
14576 will work, too. */
14577 id = cp_parser_id_expression (parser,
14578 /*template_keyword_p=*/false,
14579 /*check_dependency_p=*/false,
14580 /*template_p=*/NULL,
14581 /*declarator_p=*/true,
14582 optional_p);
14583 if (id && BASELINK_P (id))
14584 id = BASELINK_FUNCTIONS (id);
14585 return id;
14588 /* Parse a type-id.
14590 type-id:
14591 type-specifier-seq abstract-declarator [opt]
14593 Returns the TYPE specified. */
14595 static tree
14596 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
14597 bool is_trailing_return)
14599 cp_decl_specifier_seq type_specifier_seq;
14600 cp_declarator *abstract_declarator;
14602 /* Parse the type-specifier-seq. */
14603 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14604 is_trailing_return,
14605 &type_specifier_seq);
14606 if (type_specifier_seq.type == error_mark_node)
14607 return error_mark_node;
14609 /* There might or might not be an abstract declarator. */
14610 cp_parser_parse_tentatively (parser);
14611 /* Look for the declarator. */
14612 abstract_declarator
14613 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
14614 /*parenthesized_p=*/NULL,
14615 /*member_p=*/false);
14616 /* Check to see if there really was a declarator. */
14617 if (!cp_parser_parse_definitely (parser))
14618 abstract_declarator = NULL;
14620 if (type_specifier_seq.type
14621 && type_uses_auto (type_specifier_seq.type))
14623 /* A type-id with type 'auto' is only ok if the abstract declarator
14624 is a function declarator with a late-specified return type. */
14625 if (abstract_declarator
14626 && abstract_declarator->kind == cdk_function
14627 && abstract_declarator->u.function.late_return_type)
14628 /* OK */;
14629 else
14631 error ("invalid use of %<auto%>");
14632 return error_mark_node;
14636 return groktypename (&type_specifier_seq, abstract_declarator,
14637 is_template_arg);
14640 static tree cp_parser_type_id (cp_parser *parser)
14642 return cp_parser_type_id_1 (parser, false, false);
14645 static tree cp_parser_template_type_arg (cp_parser *parser)
14647 return cp_parser_type_id_1 (parser, true, false);
14650 static tree cp_parser_trailing_type_id (cp_parser *parser)
14652 return cp_parser_type_id_1 (parser, false, true);
14655 /* Parse a type-specifier-seq.
14657 type-specifier-seq:
14658 type-specifier type-specifier-seq [opt]
14660 GNU extension:
14662 type-specifier-seq:
14663 attributes type-specifier-seq [opt]
14665 If IS_DECLARATION is true, we are at the start of a "condition" or
14666 exception-declaration, so we might be followed by a declarator-id.
14668 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
14669 i.e. we've just seen "->".
14671 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
14673 static void
14674 cp_parser_type_specifier_seq (cp_parser* parser,
14675 bool is_declaration,
14676 bool is_trailing_return,
14677 cp_decl_specifier_seq *type_specifier_seq)
14679 bool seen_type_specifier = false;
14680 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
14681 cp_token *start_token = NULL;
14683 /* Clear the TYPE_SPECIFIER_SEQ. */
14684 clear_decl_specs (type_specifier_seq);
14686 /* In the context of a trailing return type, enum E { } is an
14687 elaborated-type-specifier followed by a function-body, not an
14688 enum-specifier. */
14689 if (is_trailing_return)
14690 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
14692 /* Parse the type-specifiers and attributes. */
14693 while (true)
14695 tree type_specifier;
14696 bool is_cv_qualifier;
14698 /* Check for attributes first. */
14699 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
14701 type_specifier_seq->attributes =
14702 chainon (type_specifier_seq->attributes,
14703 cp_parser_attributes_opt (parser));
14704 continue;
14707 /* record the token of the beginning of the type specifier seq,
14708 for error reporting purposes*/
14709 if (!start_token)
14710 start_token = cp_lexer_peek_token (parser->lexer);
14712 /* Look for the type-specifier. */
14713 type_specifier = cp_parser_type_specifier (parser,
14714 flags,
14715 type_specifier_seq,
14716 /*is_declaration=*/false,
14717 NULL,
14718 &is_cv_qualifier);
14719 if (!type_specifier)
14721 /* If the first type-specifier could not be found, this is not a
14722 type-specifier-seq at all. */
14723 if (!seen_type_specifier)
14725 cp_parser_error (parser, "expected type-specifier");
14726 type_specifier_seq->type = error_mark_node;
14727 return;
14729 /* If subsequent type-specifiers could not be found, the
14730 type-specifier-seq is complete. */
14731 break;
14734 seen_type_specifier = true;
14735 /* The standard says that a condition can be:
14737 type-specifier-seq declarator = assignment-expression
14739 However, given:
14741 struct S {};
14742 if (int S = ...)
14744 we should treat the "S" as a declarator, not as a
14745 type-specifier. The standard doesn't say that explicitly for
14746 type-specifier-seq, but it does say that for
14747 decl-specifier-seq in an ordinary declaration. Perhaps it
14748 would be clearer just to allow a decl-specifier-seq here, and
14749 then add a semantic restriction that if any decl-specifiers
14750 that are not type-specifiers appear, the program is invalid. */
14751 if (is_declaration && !is_cv_qualifier)
14752 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
14755 cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
14758 /* Parse a parameter-declaration-clause.
14760 parameter-declaration-clause:
14761 parameter-declaration-list [opt] ... [opt]
14762 parameter-declaration-list , ...
14764 Returns a representation for the parameter declarations. A return
14765 value of NULL indicates a parameter-declaration-clause consisting
14766 only of an ellipsis. */
14768 static tree
14769 cp_parser_parameter_declaration_clause (cp_parser* parser)
14771 tree parameters;
14772 cp_token *token;
14773 bool ellipsis_p;
14774 bool is_error;
14776 /* Peek at the next token. */
14777 token = cp_lexer_peek_token (parser->lexer);
14778 /* Check for trivial parameter-declaration-clauses. */
14779 if (token->type == CPP_ELLIPSIS)
14781 /* Consume the `...' token. */
14782 cp_lexer_consume_token (parser->lexer);
14783 return NULL_TREE;
14785 else if (token->type == CPP_CLOSE_PAREN)
14786 /* There are no parameters. */
14788 #ifndef NO_IMPLICIT_EXTERN_C
14789 if (in_system_header && current_class_type == NULL
14790 && current_lang_name == lang_name_c)
14791 return NULL_TREE;
14792 else
14793 #endif
14794 return void_list_node;
14796 /* Check for `(void)', too, which is a special case. */
14797 else if (token->keyword == RID_VOID
14798 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
14799 == CPP_CLOSE_PAREN))
14801 /* Consume the `void' token. */
14802 cp_lexer_consume_token (parser->lexer);
14803 /* There are no parameters. */
14804 return void_list_node;
14807 /* Parse the parameter-declaration-list. */
14808 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
14809 /* If a parse error occurred while parsing the
14810 parameter-declaration-list, then the entire
14811 parameter-declaration-clause is erroneous. */
14812 if (is_error)
14813 return NULL;
14815 /* Peek at the next token. */
14816 token = cp_lexer_peek_token (parser->lexer);
14817 /* If it's a `,', the clause should terminate with an ellipsis. */
14818 if (token->type == CPP_COMMA)
14820 /* Consume the `,'. */
14821 cp_lexer_consume_token (parser->lexer);
14822 /* Expect an ellipsis. */
14823 ellipsis_p
14824 = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
14826 /* It might also be `...' if the optional trailing `,' was
14827 omitted. */
14828 else if (token->type == CPP_ELLIPSIS)
14830 /* Consume the `...' token. */
14831 cp_lexer_consume_token (parser->lexer);
14832 /* And remember that we saw it. */
14833 ellipsis_p = true;
14835 else
14836 ellipsis_p = false;
14838 /* Finish the parameter list. */
14839 if (!ellipsis_p)
14840 parameters = chainon (parameters, void_list_node);
14842 return parameters;
14845 /* Parse a parameter-declaration-list.
14847 parameter-declaration-list:
14848 parameter-declaration
14849 parameter-declaration-list , parameter-declaration
14851 Returns a representation of the parameter-declaration-list, as for
14852 cp_parser_parameter_declaration_clause. However, the
14853 `void_list_node' is never appended to the list. Upon return,
14854 *IS_ERROR will be true iff an error occurred. */
14856 static tree
14857 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
14859 tree parameters = NULL_TREE;
14860 tree *tail = &parameters;
14861 bool saved_in_unbraced_linkage_specification_p;
14862 int index = 0;
14864 /* Assume all will go well. */
14865 *is_error = false;
14866 /* The special considerations that apply to a function within an
14867 unbraced linkage specifications do not apply to the parameters
14868 to the function. */
14869 saved_in_unbraced_linkage_specification_p
14870 = parser->in_unbraced_linkage_specification_p;
14871 parser->in_unbraced_linkage_specification_p = false;
14873 /* Look for more parameters. */
14874 while (true)
14876 cp_parameter_declarator *parameter;
14877 tree decl = error_mark_node;
14878 bool parenthesized_p;
14879 /* Parse the parameter. */
14880 parameter
14881 = cp_parser_parameter_declaration (parser,
14882 /*template_parm_p=*/false,
14883 &parenthesized_p);
14885 /* We don't know yet if the enclosing context is deprecated, so wait
14886 and warn in grokparms if appropriate. */
14887 deprecated_state = DEPRECATED_SUPPRESS;
14889 if (parameter)
14890 decl = grokdeclarator (parameter->declarator,
14891 &parameter->decl_specifiers,
14892 PARM,
14893 parameter->default_argument != NULL_TREE,
14894 &parameter->decl_specifiers.attributes);
14896 deprecated_state = DEPRECATED_NORMAL;
14898 /* If a parse error occurred parsing the parameter declaration,
14899 then the entire parameter-declaration-list is erroneous. */
14900 if (decl == error_mark_node)
14902 *is_error = true;
14903 parameters = error_mark_node;
14904 break;
14907 if (parameter->decl_specifiers.attributes)
14908 cplus_decl_attributes (&decl,
14909 parameter->decl_specifiers.attributes,
14911 if (DECL_NAME (decl))
14912 decl = pushdecl (decl);
14914 if (decl != error_mark_node)
14916 retrofit_lang_decl (decl);
14917 DECL_PARM_INDEX (decl) = ++index;
14920 /* Add the new parameter to the list. */
14921 *tail = build_tree_list (parameter->default_argument, decl);
14922 tail = &TREE_CHAIN (*tail);
14924 /* Peek at the next token. */
14925 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
14926 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
14927 /* These are for Objective-C++ */
14928 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14929 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14930 /* The parameter-declaration-list is complete. */
14931 break;
14932 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14934 cp_token *token;
14936 /* Peek at the next token. */
14937 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14938 /* If it's an ellipsis, then the list is complete. */
14939 if (token->type == CPP_ELLIPSIS)
14940 break;
14941 /* Otherwise, there must be more parameters. Consume the
14942 `,'. */
14943 cp_lexer_consume_token (parser->lexer);
14944 /* When parsing something like:
14946 int i(float f, double d)
14948 we can tell after seeing the declaration for "f" that we
14949 are not looking at an initialization of a variable "i",
14950 but rather at the declaration of a function "i".
14952 Due to the fact that the parsing of template arguments
14953 (as specified to a template-id) requires backtracking we
14954 cannot use this technique when inside a template argument
14955 list. */
14956 if (!parser->in_template_argument_list_p
14957 && !parser->in_type_id_in_expr_p
14958 && cp_parser_uncommitted_to_tentative_parse_p (parser)
14959 /* However, a parameter-declaration of the form
14960 "foat(f)" (which is a valid declaration of a
14961 parameter "f") can also be interpreted as an
14962 expression (the conversion of "f" to "float"). */
14963 && !parenthesized_p)
14964 cp_parser_commit_to_tentative_parse (parser);
14966 else
14968 cp_parser_error (parser, "expected %<,%> or %<...%>");
14969 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14970 cp_parser_skip_to_closing_parenthesis (parser,
14971 /*recovering=*/true,
14972 /*or_comma=*/false,
14973 /*consume_paren=*/false);
14974 break;
14978 parser->in_unbraced_linkage_specification_p
14979 = saved_in_unbraced_linkage_specification_p;
14981 return parameters;
14984 /* Parse a parameter declaration.
14986 parameter-declaration:
14987 decl-specifier-seq ... [opt] declarator
14988 decl-specifier-seq declarator = assignment-expression
14989 decl-specifier-seq ... [opt] abstract-declarator [opt]
14990 decl-specifier-seq abstract-declarator [opt] = assignment-expression
14992 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
14993 declares a template parameter. (In that case, a non-nested `>'
14994 token encountered during the parsing of the assignment-expression
14995 is not interpreted as a greater-than operator.)
14997 Returns a representation of the parameter, or NULL if an error
14998 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
14999 true iff the declarator is of the form "(p)". */
15001 static cp_parameter_declarator *
15002 cp_parser_parameter_declaration (cp_parser *parser,
15003 bool template_parm_p,
15004 bool *parenthesized_p)
15006 int declares_class_or_enum;
15007 cp_decl_specifier_seq decl_specifiers;
15008 cp_declarator *declarator;
15009 tree default_argument;
15010 cp_token *token = NULL, *declarator_token_start = NULL;
15011 const char *saved_message;
15013 /* In a template parameter, `>' is not an operator.
15015 [temp.param]
15017 When parsing a default template-argument for a non-type
15018 template-parameter, the first non-nested `>' is taken as the end
15019 of the template parameter-list rather than a greater-than
15020 operator. */
15022 /* Type definitions may not appear in parameter types. */
15023 saved_message = parser->type_definition_forbidden_message;
15024 parser->type_definition_forbidden_message
15025 = G_("types may not be defined in parameter types");
15027 /* Parse the declaration-specifiers. */
15028 cp_parser_decl_specifier_seq (parser,
15029 CP_PARSER_FLAGS_NONE,
15030 &decl_specifiers,
15031 &declares_class_or_enum);
15033 /* Complain about missing 'typename' or other invalid type names. */
15034 if (!decl_specifiers.any_type_specifiers_p)
15035 cp_parser_parse_and_diagnose_invalid_type_name (parser);
15037 /* If an error occurred, there's no reason to attempt to parse the
15038 rest of the declaration. */
15039 if (cp_parser_error_occurred (parser))
15041 parser->type_definition_forbidden_message = saved_message;
15042 return NULL;
15045 /* Peek at the next token. */
15046 token = cp_lexer_peek_token (parser->lexer);
15048 /* If the next token is a `)', `,', `=', `>', or `...', then there
15049 is no declarator. However, when variadic templates are enabled,
15050 there may be a declarator following `...'. */
15051 if (token->type == CPP_CLOSE_PAREN
15052 || token->type == CPP_COMMA
15053 || token->type == CPP_EQ
15054 || token->type == CPP_GREATER)
15056 declarator = NULL;
15057 if (parenthesized_p)
15058 *parenthesized_p = false;
15060 /* Otherwise, there should be a declarator. */
15061 else
15063 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
15064 parser->default_arg_ok_p = false;
15066 /* After seeing a decl-specifier-seq, if the next token is not a
15067 "(", there is no possibility that the code is a valid
15068 expression. Therefore, if parsing tentatively, we commit at
15069 this point. */
15070 if (!parser->in_template_argument_list_p
15071 /* In an expression context, having seen:
15073 (int((char ...
15075 we cannot be sure whether we are looking at a
15076 function-type (taking a "char" as a parameter) or a cast
15077 of some object of type "char" to "int". */
15078 && !parser->in_type_id_in_expr_p
15079 && cp_parser_uncommitted_to_tentative_parse_p (parser)
15080 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
15081 cp_parser_commit_to_tentative_parse (parser);
15082 /* Parse the declarator. */
15083 declarator_token_start = token;
15084 declarator = cp_parser_declarator (parser,
15085 CP_PARSER_DECLARATOR_EITHER,
15086 /*ctor_dtor_or_conv_p=*/NULL,
15087 parenthesized_p,
15088 /*member_p=*/false);
15089 parser->default_arg_ok_p = saved_default_arg_ok_p;
15090 /* After the declarator, allow more attributes. */
15091 decl_specifiers.attributes
15092 = chainon (decl_specifiers.attributes,
15093 cp_parser_attributes_opt (parser));
15096 /* If the next token is an ellipsis, and we have not seen a
15097 declarator name, and the type of the declarator contains parameter
15098 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
15099 a parameter pack expansion expression. Otherwise, leave the
15100 ellipsis for a C-style variadic function. */
15101 token = cp_lexer_peek_token (parser->lexer);
15102 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15104 tree type = decl_specifiers.type;
15106 if (type && DECL_P (type))
15107 type = TREE_TYPE (type);
15109 if (type
15110 && TREE_CODE (type) != TYPE_PACK_EXPANSION
15111 && declarator_can_be_parameter_pack (declarator)
15112 && (!declarator || !declarator->parameter_pack_p)
15113 && uses_parameter_packs (type))
15115 /* Consume the `...'. */
15116 cp_lexer_consume_token (parser->lexer);
15117 maybe_warn_variadic_templates ();
15119 /* Build a pack expansion type */
15120 if (declarator)
15121 declarator->parameter_pack_p = true;
15122 else
15123 decl_specifiers.type = make_pack_expansion (type);
15127 /* The restriction on defining new types applies only to the type
15128 of the parameter, not to the default argument. */
15129 parser->type_definition_forbidden_message = saved_message;
15131 /* If the next token is `=', then process a default argument. */
15132 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15134 /* Consume the `='. */
15135 cp_lexer_consume_token (parser->lexer);
15137 /* If we are defining a class, then the tokens that make up the
15138 default argument must be saved and processed later. */
15139 if (!template_parm_p && at_class_scope_p ()
15140 && TYPE_BEING_DEFINED (current_class_type)
15141 && !LAMBDA_TYPE_P (current_class_type))
15143 unsigned depth = 0;
15144 int maybe_template_id = 0;
15145 cp_token *first_token;
15146 cp_token *token;
15148 /* Add tokens until we have processed the entire default
15149 argument. We add the range [first_token, token). */
15150 first_token = cp_lexer_peek_token (parser->lexer);
15151 while (true)
15153 bool done = false;
15155 /* Peek at the next token. */
15156 token = cp_lexer_peek_token (parser->lexer);
15157 /* What we do depends on what token we have. */
15158 switch (token->type)
15160 /* In valid code, a default argument must be
15161 immediately followed by a `,' `)', or `...'. */
15162 case CPP_COMMA:
15163 if (depth == 0 && maybe_template_id)
15165 /* If we've seen a '<', we might be in a
15166 template-argument-list. Until Core issue 325 is
15167 resolved, we don't know how this situation ought
15168 to be handled, so try to DTRT. We check whether
15169 what comes after the comma is a valid parameter
15170 declaration list. If it is, then the comma ends
15171 the default argument; otherwise the default
15172 argument continues. */
15173 bool error = false;
15175 /* Set ITALP so cp_parser_parameter_declaration_list
15176 doesn't decide to commit to this parse. */
15177 bool saved_italp = parser->in_template_argument_list_p;
15178 parser->in_template_argument_list_p = true;
15180 cp_parser_parse_tentatively (parser);
15181 cp_lexer_consume_token (parser->lexer);
15182 cp_parser_parameter_declaration_list (parser, &error);
15183 if (!cp_parser_error_occurred (parser) && !error)
15184 done = true;
15185 cp_parser_abort_tentative_parse (parser);
15187 parser->in_template_argument_list_p = saved_italp;
15188 break;
15190 case CPP_CLOSE_PAREN:
15191 case CPP_ELLIPSIS:
15192 /* If we run into a non-nested `;', `}', or `]',
15193 then the code is invalid -- but the default
15194 argument is certainly over. */
15195 case CPP_SEMICOLON:
15196 case CPP_CLOSE_BRACE:
15197 case CPP_CLOSE_SQUARE:
15198 if (depth == 0)
15199 done = true;
15200 /* Update DEPTH, if necessary. */
15201 else if (token->type == CPP_CLOSE_PAREN
15202 || token->type == CPP_CLOSE_BRACE
15203 || token->type == CPP_CLOSE_SQUARE)
15204 --depth;
15205 break;
15207 case CPP_OPEN_PAREN:
15208 case CPP_OPEN_SQUARE:
15209 case CPP_OPEN_BRACE:
15210 ++depth;
15211 break;
15213 case CPP_LESS:
15214 if (depth == 0)
15215 /* This might be the comparison operator, or it might
15216 start a template argument list. */
15217 ++maybe_template_id;
15218 break;
15220 case CPP_RSHIFT:
15221 if (cxx_dialect == cxx98)
15222 break;
15223 /* Fall through for C++0x, which treats the `>>'
15224 operator like two `>' tokens in certain
15225 cases. */
15227 case CPP_GREATER:
15228 if (depth == 0)
15230 /* This might be an operator, or it might close a
15231 template argument list. But if a previous '<'
15232 started a template argument list, this will have
15233 closed it, so we can't be in one anymore. */
15234 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
15235 if (maybe_template_id < 0)
15236 maybe_template_id = 0;
15238 break;
15240 /* If we run out of tokens, issue an error message. */
15241 case CPP_EOF:
15242 case CPP_PRAGMA_EOL:
15243 error_at (token->location, "file ends in default argument");
15244 done = true;
15245 break;
15247 case CPP_NAME:
15248 case CPP_SCOPE:
15249 /* In these cases, we should look for template-ids.
15250 For example, if the default argument is
15251 `X<int, double>()', we need to do name lookup to
15252 figure out whether or not `X' is a template; if
15253 so, the `,' does not end the default argument.
15255 That is not yet done. */
15256 break;
15258 default:
15259 break;
15262 /* If we've reached the end, stop. */
15263 if (done)
15264 break;
15266 /* Add the token to the token block. */
15267 token = cp_lexer_consume_token (parser->lexer);
15270 /* Create a DEFAULT_ARG to represent the unparsed default
15271 argument. */
15272 default_argument = make_node (DEFAULT_ARG);
15273 DEFARG_TOKENS (default_argument)
15274 = cp_token_cache_new (first_token, token);
15275 DEFARG_INSTANTIATIONS (default_argument) = NULL;
15277 /* Outside of a class definition, we can just parse the
15278 assignment-expression. */
15279 else
15281 token = cp_lexer_peek_token (parser->lexer);
15282 default_argument
15283 = cp_parser_default_argument (parser, template_parm_p);
15286 if (!parser->default_arg_ok_p)
15288 if (flag_permissive)
15289 warning (0, "deprecated use of default argument for parameter of non-function");
15290 else
15292 error_at (token->location,
15293 "default arguments are only "
15294 "permitted for function parameters");
15295 default_argument = NULL_TREE;
15298 else if ((declarator && declarator->parameter_pack_p)
15299 || (decl_specifiers.type
15300 && PACK_EXPANSION_P (decl_specifiers.type)))
15302 /* Find the name of the parameter pack. */
15303 cp_declarator *id_declarator = declarator;
15304 while (id_declarator && id_declarator->kind != cdk_id)
15305 id_declarator = id_declarator->declarator;
15307 if (id_declarator && id_declarator->kind == cdk_id)
15308 error_at (declarator_token_start->location,
15309 template_parm_p
15310 ? "template parameter pack %qD"
15311 " cannot have a default argument"
15312 : "parameter pack %qD cannot have a default argument",
15313 id_declarator->u.id.unqualified_name);
15314 else
15315 error_at (declarator_token_start->location,
15316 template_parm_p
15317 ? "template parameter pack cannot have a default argument"
15318 : "parameter pack cannot have a default argument");
15320 default_argument = NULL_TREE;
15323 else
15324 default_argument = NULL_TREE;
15326 return make_parameter_declarator (&decl_specifiers,
15327 declarator,
15328 default_argument);
15331 /* Parse a default argument and return it.
15333 TEMPLATE_PARM_P is true if this is a default argument for a
15334 non-type template parameter. */
15335 static tree
15336 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
15338 tree default_argument = NULL_TREE;
15339 bool saved_greater_than_is_operator_p;
15340 bool saved_local_variables_forbidden_p;
15342 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
15343 set correctly. */
15344 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
15345 parser->greater_than_is_operator_p = !template_parm_p;
15346 /* Local variable names (and the `this' keyword) may not
15347 appear in a default argument. */
15348 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15349 parser->local_variables_forbidden_p = true;
15350 /* Parse the assignment-expression. */
15351 if (template_parm_p)
15352 push_deferring_access_checks (dk_no_deferred);
15353 default_argument
15354 = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
15355 if (template_parm_p)
15356 pop_deferring_access_checks ();
15357 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
15358 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15360 return default_argument;
15363 /* Parse a function-body.
15365 function-body:
15366 compound_statement */
15368 static void
15369 cp_parser_function_body (cp_parser *parser)
15371 cp_parser_compound_statement (parser, NULL, false);
15374 /* Parse a ctor-initializer-opt followed by a function-body. Return
15375 true if a ctor-initializer was present. */
15377 static bool
15378 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
15380 tree body;
15381 bool ctor_initializer_p;
15383 /* Begin the function body. */
15384 body = begin_function_body ();
15385 /* Parse the optional ctor-initializer. */
15386 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
15387 /* Parse the function-body. */
15388 cp_parser_function_body (parser);
15389 /* Finish the function body. */
15390 finish_function_body (body);
15392 return ctor_initializer_p;
15395 /* Parse an initializer.
15397 initializer:
15398 = initializer-clause
15399 ( expression-list )
15401 Returns an expression representing the initializer. If no
15402 initializer is present, NULL_TREE is returned.
15404 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
15405 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
15406 set to TRUE if there is no initializer present. If there is an
15407 initializer, and it is not a constant-expression, *NON_CONSTANT_P
15408 is set to true; otherwise it is set to false. */
15410 static tree
15411 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
15412 bool* non_constant_p)
15414 cp_token *token;
15415 tree init;
15417 /* Peek at the next token. */
15418 token = cp_lexer_peek_token (parser->lexer);
15420 /* Let our caller know whether or not this initializer was
15421 parenthesized. */
15422 *is_direct_init = (token->type != CPP_EQ);
15423 /* Assume that the initializer is constant. */
15424 *non_constant_p = false;
15426 if (token->type == CPP_EQ)
15428 /* Consume the `='. */
15429 cp_lexer_consume_token (parser->lexer);
15430 /* Parse the initializer-clause. */
15431 init = cp_parser_initializer_clause (parser, non_constant_p);
15433 else if (token->type == CPP_OPEN_PAREN)
15435 VEC(tree,gc) *vec;
15436 vec = cp_parser_parenthesized_expression_list (parser, false,
15437 /*cast_p=*/false,
15438 /*allow_expansion_p=*/true,
15439 non_constant_p);
15440 if (vec == NULL)
15441 return error_mark_node;
15442 init = build_tree_list_vec (vec);
15443 release_tree_vector (vec);
15445 else if (token->type == CPP_OPEN_BRACE)
15447 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
15448 init = cp_parser_braced_list (parser, non_constant_p);
15449 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
15451 else
15453 /* Anything else is an error. */
15454 cp_parser_error (parser, "expected initializer");
15455 init = error_mark_node;
15458 return init;
15461 /* Parse an initializer-clause.
15463 initializer-clause:
15464 assignment-expression
15465 braced-init-list
15467 Returns an expression representing the initializer.
15469 If the `assignment-expression' production is used the value
15470 returned is simply a representation for the expression.
15472 Otherwise, calls cp_parser_braced_list. */
15474 static tree
15475 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
15477 tree initializer;
15479 /* Assume the expression is constant. */
15480 *non_constant_p = false;
15482 /* If it is not a `{', then we are looking at an
15483 assignment-expression. */
15484 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
15486 initializer
15487 = cp_parser_constant_expression (parser,
15488 /*allow_non_constant_p=*/true,
15489 non_constant_p);
15490 if (!*non_constant_p)
15491 initializer = fold_non_dependent_expr (initializer);
15493 else
15494 initializer = cp_parser_braced_list (parser, non_constant_p);
15496 return initializer;
15499 /* Parse a brace-enclosed initializer list.
15501 braced-init-list:
15502 { initializer-list , [opt] }
15505 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
15506 the elements of the initializer-list (or NULL, if the last
15507 production is used). The TREE_TYPE for the CONSTRUCTOR will be
15508 NULL_TREE. There is no way to detect whether or not the optional
15509 trailing `,' was provided. NON_CONSTANT_P is as for
15510 cp_parser_initializer. */
15512 static tree
15513 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
15515 tree initializer;
15517 /* Consume the `{' token. */
15518 cp_lexer_consume_token (parser->lexer);
15519 /* Create a CONSTRUCTOR to represent the braced-initializer. */
15520 initializer = make_node (CONSTRUCTOR);
15521 /* If it's not a `}', then there is a non-trivial initializer. */
15522 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
15524 /* Parse the initializer list. */
15525 CONSTRUCTOR_ELTS (initializer)
15526 = cp_parser_initializer_list (parser, non_constant_p);
15527 /* A trailing `,' token is allowed. */
15528 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15529 cp_lexer_consume_token (parser->lexer);
15531 /* Now, there should be a trailing `}'. */
15532 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15533 TREE_TYPE (initializer) = init_list_type_node;
15534 return initializer;
15537 /* Parse an initializer-list.
15539 initializer-list:
15540 initializer-clause ... [opt]
15541 initializer-list , initializer-clause ... [opt]
15543 GNU Extension:
15545 initializer-list:
15546 identifier : initializer-clause
15547 initializer-list, identifier : initializer-clause
15549 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
15550 for the initializer. If the INDEX of the elt is non-NULL, it is the
15551 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
15552 as for cp_parser_initializer. */
15554 static VEC(constructor_elt,gc) *
15555 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
15557 VEC(constructor_elt,gc) *v = NULL;
15559 /* Assume all of the expressions are constant. */
15560 *non_constant_p = false;
15562 /* Parse the rest of the list. */
15563 while (true)
15565 cp_token *token;
15566 tree identifier;
15567 tree initializer;
15568 bool clause_non_constant_p;
15570 /* If the next token is an identifier and the following one is a
15571 colon, we are looking at the GNU designated-initializer
15572 syntax. */
15573 if (cp_parser_allow_gnu_extensions_p (parser)
15574 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
15575 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
15577 /* Warn the user that they are using an extension. */
15578 pedwarn (input_location, OPT_pedantic,
15579 "ISO C++ does not allow designated initializers");
15580 /* Consume the identifier. */
15581 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
15582 /* Consume the `:'. */
15583 cp_lexer_consume_token (parser->lexer);
15585 else
15586 identifier = NULL_TREE;
15588 /* Parse the initializer. */
15589 initializer = cp_parser_initializer_clause (parser,
15590 &clause_non_constant_p);
15591 /* If any clause is non-constant, so is the entire initializer. */
15592 if (clause_non_constant_p)
15593 *non_constant_p = true;
15595 /* If we have an ellipsis, this is an initializer pack
15596 expansion. */
15597 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15599 /* Consume the `...'. */
15600 cp_lexer_consume_token (parser->lexer);
15602 /* Turn the initializer into an initializer expansion. */
15603 initializer = make_pack_expansion (initializer);
15606 /* Add it to the vector. */
15607 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
15609 /* If the next token is not a comma, we have reached the end of
15610 the list. */
15611 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15612 break;
15614 /* Peek at the next token. */
15615 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15616 /* If the next token is a `}', then we're still done. An
15617 initializer-clause can have a trailing `,' after the
15618 initializer-list and before the closing `}'. */
15619 if (token->type == CPP_CLOSE_BRACE)
15620 break;
15622 /* Consume the `,' token. */
15623 cp_lexer_consume_token (parser->lexer);
15626 return v;
15629 /* Classes [gram.class] */
15631 /* Parse a class-name.
15633 class-name:
15634 identifier
15635 template-id
15637 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
15638 to indicate that names looked up in dependent types should be
15639 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
15640 keyword has been used to indicate that the name that appears next
15641 is a template. TAG_TYPE indicates the explicit tag given before
15642 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
15643 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
15644 is the class being defined in a class-head.
15646 Returns the TYPE_DECL representing the class. */
15648 static tree
15649 cp_parser_class_name (cp_parser *parser,
15650 bool typename_keyword_p,
15651 bool template_keyword_p,
15652 enum tag_types tag_type,
15653 bool check_dependency_p,
15654 bool class_head_p,
15655 bool is_declaration)
15657 tree decl;
15658 tree scope;
15659 bool typename_p;
15660 cp_token *token;
15661 tree identifier = NULL_TREE;
15663 /* All class-names start with an identifier. */
15664 token = cp_lexer_peek_token (parser->lexer);
15665 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
15667 cp_parser_error (parser, "expected class-name");
15668 return error_mark_node;
15671 /* PARSER->SCOPE can be cleared when parsing the template-arguments
15672 to a template-id, so we save it here. */
15673 scope = parser->scope;
15674 if (scope == error_mark_node)
15675 return error_mark_node;
15677 /* Any name names a type if we're following the `typename' keyword
15678 in a qualified name where the enclosing scope is type-dependent. */
15679 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
15680 && dependent_type_p (scope));
15681 /* Handle the common case (an identifier, but not a template-id)
15682 efficiently. */
15683 if (token->type == CPP_NAME
15684 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
15686 cp_token *identifier_token;
15687 bool ambiguous_p;
15689 /* Look for the identifier. */
15690 identifier_token = cp_lexer_peek_token (parser->lexer);
15691 ambiguous_p = identifier_token->ambiguous_p;
15692 identifier = cp_parser_identifier (parser);
15693 /* If the next token isn't an identifier, we are certainly not
15694 looking at a class-name. */
15695 if (identifier == error_mark_node)
15696 decl = error_mark_node;
15697 /* If we know this is a type-name, there's no need to look it
15698 up. */
15699 else if (typename_p)
15700 decl = identifier;
15701 else
15703 tree ambiguous_decls;
15704 /* If we already know that this lookup is ambiguous, then
15705 we've already issued an error message; there's no reason
15706 to check again. */
15707 if (ambiguous_p)
15709 cp_parser_simulate_error (parser);
15710 return error_mark_node;
15712 /* If the next token is a `::', then the name must be a type
15713 name.
15715 [basic.lookup.qual]
15717 During the lookup for a name preceding the :: scope
15718 resolution operator, object, function, and enumerator
15719 names are ignored. */
15720 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15721 tag_type = typename_type;
15722 /* Look up the name. */
15723 decl = cp_parser_lookup_name (parser, identifier,
15724 tag_type,
15725 /*is_template=*/false,
15726 /*is_namespace=*/false,
15727 check_dependency_p,
15728 &ambiguous_decls,
15729 identifier_token->location);
15730 if (ambiguous_decls)
15732 if (cp_parser_parsing_tentatively (parser))
15733 cp_parser_simulate_error (parser);
15734 return error_mark_node;
15738 else
15740 /* Try a template-id. */
15741 decl = cp_parser_template_id (parser, template_keyword_p,
15742 check_dependency_p,
15743 is_declaration);
15744 if (decl == error_mark_node)
15745 return error_mark_node;
15748 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
15750 /* If this is a typename, create a TYPENAME_TYPE. */
15751 if (typename_p && decl != error_mark_node)
15753 decl = make_typename_type (scope, decl, typename_type,
15754 /*complain=*/tf_error);
15755 if (decl != error_mark_node)
15756 decl = TYPE_NAME (decl);
15759 /* Check to see that it is really the name of a class. */
15760 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15761 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
15762 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15763 /* Situations like this:
15765 template <typename T> struct A {
15766 typename T::template X<int>::I i;
15769 are problematic. Is `T::template X<int>' a class-name? The
15770 standard does not seem to be definitive, but there is no other
15771 valid interpretation of the following `::'. Therefore, those
15772 names are considered class-names. */
15774 decl = make_typename_type (scope, decl, tag_type, tf_error);
15775 if (decl != error_mark_node)
15776 decl = TYPE_NAME (decl);
15778 else if (TREE_CODE (decl) != TYPE_DECL
15779 || TREE_TYPE (decl) == error_mark_node
15780 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
15781 decl = error_mark_node;
15783 if (decl == error_mark_node)
15784 cp_parser_error (parser, "expected class-name");
15785 else if (identifier && !parser->scope)
15786 maybe_note_name_used_in_class (identifier, decl);
15788 return decl;
15791 /* Parse a class-specifier.
15793 class-specifier:
15794 class-head { member-specification [opt] }
15796 Returns the TREE_TYPE representing the class. */
15798 static tree
15799 cp_parser_class_specifier (cp_parser* parser)
15801 tree type;
15802 tree attributes = NULL_TREE;
15803 bool nested_name_specifier_p;
15804 unsigned saved_num_template_parameter_lists;
15805 bool saved_in_function_body;
15806 bool saved_in_unbraced_linkage_specification_p;
15807 tree old_scope = NULL_TREE;
15808 tree scope = NULL_TREE;
15809 tree bases;
15811 push_deferring_access_checks (dk_no_deferred);
15813 /* Parse the class-head. */
15814 type = cp_parser_class_head (parser,
15815 &nested_name_specifier_p,
15816 &attributes,
15817 &bases);
15818 /* If the class-head was a semantic disaster, skip the entire body
15819 of the class. */
15820 if (!type)
15822 cp_parser_skip_to_end_of_block_or_statement (parser);
15823 pop_deferring_access_checks ();
15824 return error_mark_node;
15827 /* Look for the `{'. */
15828 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
15830 pop_deferring_access_checks ();
15831 return error_mark_node;
15834 /* Process the base classes. If they're invalid, skip the
15835 entire class body. */
15836 if (!xref_basetypes (type, bases))
15838 /* Consuming the closing brace yields better error messages
15839 later on. */
15840 if (cp_parser_skip_to_closing_brace (parser))
15841 cp_lexer_consume_token (parser->lexer);
15842 pop_deferring_access_checks ();
15843 return error_mark_node;
15846 /* Issue an error message if type-definitions are forbidden here. */
15847 cp_parser_check_type_definition (parser);
15848 /* Remember that we are defining one more class. */
15849 ++parser->num_classes_being_defined;
15850 /* Inside the class, surrounding template-parameter-lists do not
15851 apply. */
15852 saved_num_template_parameter_lists
15853 = parser->num_template_parameter_lists;
15854 parser->num_template_parameter_lists = 0;
15855 /* We are not in a function body. */
15856 saved_in_function_body = parser->in_function_body;
15857 parser->in_function_body = false;
15858 /* We are not immediately inside an extern "lang" block. */
15859 saved_in_unbraced_linkage_specification_p
15860 = parser->in_unbraced_linkage_specification_p;
15861 parser->in_unbraced_linkage_specification_p = false;
15863 /* Start the class. */
15864 if (nested_name_specifier_p)
15866 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
15867 old_scope = push_inner_scope (scope);
15869 type = begin_class_definition (type, attributes);
15871 if (type == error_mark_node)
15872 /* If the type is erroneous, skip the entire body of the class. */
15873 cp_parser_skip_to_closing_brace (parser);
15874 else
15875 /* Parse the member-specification. */
15876 cp_parser_member_specification_opt (parser);
15878 /* Look for the trailing `}'. */
15879 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15880 /* Look for trailing attributes to apply to this class. */
15881 if (cp_parser_allow_gnu_extensions_p (parser))
15882 attributes = cp_parser_attributes_opt (parser);
15883 if (type != error_mark_node)
15884 type = finish_struct (type, attributes);
15885 if (nested_name_specifier_p)
15886 pop_inner_scope (old_scope, scope);
15887 /* If this class is not itself within the scope of another class,
15888 then we need to parse the bodies of all of the queued function
15889 definitions. Note that the queued functions defined in a class
15890 are not always processed immediately following the
15891 class-specifier for that class. Consider:
15893 struct A {
15894 struct B { void f() { sizeof (A); } };
15897 If `f' were processed before the processing of `A' were
15898 completed, there would be no way to compute the size of `A'.
15899 Note that the nesting we are interested in here is lexical --
15900 not the semantic nesting given by TYPE_CONTEXT. In particular,
15901 for:
15903 struct A { struct B; };
15904 struct A::B { void f() { } };
15906 there is no need to delay the parsing of `A::B::f'. */
15907 if (--parser->num_classes_being_defined == 0)
15909 tree queue_entry;
15910 tree fn;
15911 tree class_type = NULL_TREE;
15912 tree pushed_scope = NULL_TREE;
15914 /* In a first pass, parse default arguments to the functions.
15915 Then, in a second pass, parse the bodies of the functions.
15916 This two-phased approach handles cases like:
15918 struct S {
15919 void f() { g(); }
15920 void g(int i = 3);
15924 for (TREE_PURPOSE (parser->unparsed_functions_queues)
15925 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
15926 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
15927 TREE_PURPOSE (parser->unparsed_functions_queues)
15928 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
15930 fn = TREE_VALUE (queue_entry);
15931 /* If there are default arguments that have not yet been processed,
15932 take care of them now. */
15933 if (class_type != TREE_PURPOSE (queue_entry))
15935 if (pushed_scope)
15936 pop_scope (pushed_scope);
15937 class_type = TREE_PURPOSE (queue_entry);
15938 pushed_scope = push_scope (class_type);
15940 /* Make sure that any template parameters are in scope. */
15941 maybe_begin_member_template_processing (fn);
15942 /* Parse the default argument expressions. */
15943 cp_parser_late_parsing_default_args (parser, fn);
15944 /* Remove any template parameters from the symbol table. */
15945 maybe_end_member_template_processing ();
15947 if (pushed_scope)
15948 pop_scope (pushed_scope);
15949 /* Now parse the body of the functions. */
15950 for (TREE_VALUE (parser->unparsed_functions_queues)
15951 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
15952 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
15953 TREE_VALUE (parser->unparsed_functions_queues)
15954 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
15956 /* Figure out which function we need to process. */
15957 fn = TREE_VALUE (queue_entry);
15958 /* Parse the function. */
15959 cp_parser_late_parsing_for_member (parser, fn);
15963 /* Put back any saved access checks. */
15964 pop_deferring_access_checks ();
15966 /* Restore saved state. */
15967 parser->in_function_body = saved_in_function_body;
15968 parser->num_template_parameter_lists
15969 = saved_num_template_parameter_lists;
15970 parser->in_unbraced_linkage_specification_p
15971 = saved_in_unbraced_linkage_specification_p;
15973 return type;
15976 /* Parse a class-head.
15978 class-head:
15979 class-key identifier [opt] base-clause [opt]
15980 class-key nested-name-specifier identifier base-clause [opt]
15981 class-key nested-name-specifier [opt] template-id
15982 base-clause [opt]
15984 GNU Extensions:
15985 class-key attributes identifier [opt] base-clause [opt]
15986 class-key attributes nested-name-specifier identifier base-clause [opt]
15987 class-key attributes nested-name-specifier [opt] template-id
15988 base-clause [opt]
15990 Upon return BASES is initialized to the list of base classes (or
15991 NULL, if there are none) in the same form returned by
15992 cp_parser_base_clause.
15994 Returns the TYPE of the indicated class. Sets
15995 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
15996 involving a nested-name-specifier was used, and FALSE otherwise.
15998 Returns error_mark_node if this is not a class-head.
16000 Returns NULL_TREE if the class-head is syntactically valid, but
16001 semantically invalid in a way that means we should skip the entire
16002 body of the class. */
16004 static tree
16005 cp_parser_class_head (cp_parser* parser,
16006 bool* nested_name_specifier_p,
16007 tree *attributes_p,
16008 tree *bases)
16010 tree nested_name_specifier;
16011 enum tag_types class_key;
16012 tree id = NULL_TREE;
16013 tree type = NULL_TREE;
16014 tree attributes;
16015 bool template_id_p = false;
16016 bool qualified_p = false;
16017 bool invalid_nested_name_p = false;
16018 bool invalid_explicit_specialization_p = false;
16019 tree pushed_scope = NULL_TREE;
16020 unsigned num_templates;
16021 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
16022 /* Assume no nested-name-specifier will be present. */
16023 *nested_name_specifier_p = false;
16024 /* Assume no template parameter lists will be used in defining the
16025 type. */
16026 num_templates = 0;
16028 *bases = NULL_TREE;
16030 /* Look for the class-key. */
16031 class_key = cp_parser_class_key (parser);
16032 if (class_key == none_type)
16033 return error_mark_node;
16035 /* Parse the attributes. */
16036 attributes = cp_parser_attributes_opt (parser);
16038 /* If the next token is `::', that is invalid -- but sometimes
16039 people do try to write:
16041 struct ::S {};
16043 Handle this gracefully by accepting the extra qualifier, and then
16044 issuing an error about it later if this really is a
16045 class-head. If it turns out just to be an elaborated type
16046 specifier, remain silent. */
16047 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
16048 qualified_p = true;
16050 push_deferring_access_checks (dk_no_check);
16052 /* Determine the name of the class. Begin by looking for an
16053 optional nested-name-specifier. */
16054 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
16055 nested_name_specifier
16056 = cp_parser_nested_name_specifier_opt (parser,
16057 /*typename_keyword_p=*/false,
16058 /*check_dependency_p=*/false,
16059 /*type_p=*/false,
16060 /*is_declaration=*/false);
16061 /* If there was a nested-name-specifier, then there *must* be an
16062 identifier. */
16063 if (nested_name_specifier)
16065 type_start_token = cp_lexer_peek_token (parser->lexer);
16066 /* Although the grammar says `identifier', it really means
16067 `class-name' or `template-name'. You are only allowed to
16068 define a class that has already been declared with this
16069 syntax.
16071 The proposed resolution for Core Issue 180 says that wherever
16072 you see `class T::X' you should treat `X' as a type-name.
16074 It is OK to define an inaccessible class; for example:
16076 class A { class B; };
16077 class A::B {};
16079 We do not know if we will see a class-name, or a
16080 template-name. We look for a class-name first, in case the
16081 class-name is a template-id; if we looked for the
16082 template-name first we would stop after the template-name. */
16083 cp_parser_parse_tentatively (parser);
16084 type = cp_parser_class_name (parser,
16085 /*typename_keyword_p=*/false,
16086 /*template_keyword_p=*/false,
16087 class_type,
16088 /*check_dependency_p=*/false,
16089 /*class_head_p=*/true,
16090 /*is_declaration=*/false);
16091 /* If that didn't work, ignore the nested-name-specifier. */
16092 if (!cp_parser_parse_definitely (parser))
16094 invalid_nested_name_p = true;
16095 type_start_token = cp_lexer_peek_token (parser->lexer);
16096 id = cp_parser_identifier (parser);
16097 if (id == error_mark_node)
16098 id = NULL_TREE;
16100 /* If we could not find a corresponding TYPE, treat this
16101 declaration like an unqualified declaration. */
16102 if (type == error_mark_node)
16103 nested_name_specifier = NULL_TREE;
16104 /* Otherwise, count the number of templates used in TYPE and its
16105 containing scopes. */
16106 else
16108 tree scope;
16110 for (scope = TREE_TYPE (type);
16111 scope && TREE_CODE (scope) != NAMESPACE_DECL;
16112 scope = (TYPE_P (scope)
16113 ? TYPE_CONTEXT (scope)
16114 : DECL_CONTEXT (scope)))
16115 if (TYPE_P (scope)
16116 && CLASS_TYPE_P (scope)
16117 && CLASSTYPE_TEMPLATE_INFO (scope)
16118 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
16119 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
16120 ++num_templates;
16123 /* Otherwise, the identifier is optional. */
16124 else
16126 /* We don't know whether what comes next is a template-id,
16127 an identifier, or nothing at all. */
16128 cp_parser_parse_tentatively (parser);
16129 /* Check for a template-id. */
16130 type_start_token = cp_lexer_peek_token (parser->lexer);
16131 id = cp_parser_template_id (parser,
16132 /*template_keyword_p=*/false,
16133 /*check_dependency_p=*/true,
16134 /*is_declaration=*/true);
16135 /* If that didn't work, it could still be an identifier. */
16136 if (!cp_parser_parse_definitely (parser))
16138 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16140 type_start_token = cp_lexer_peek_token (parser->lexer);
16141 id = cp_parser_identifier (parser);
16143 else
16144 id = NULL_TREE;
16146 else
16148 template_id_p = true;
16149 ++num_templates;
16153 pop_deferring_access_checks ();
16155 if (id)
16156 cp_parser_check_for_invalid_template_id (parser, id,
16157 type_start_token->location);
16159 /* If it's not a `:' or a `{' then we can't really be looking at a
16160 class-head, since a class-head only appears as part of a
16161 class-specifier. We have to detect this situation before calling
16162 xref_tag, since that has irreversible side-effects. */
16163 if (!cp_parser_next_token_starts_class_definition_p (parser))
16165 cp_parser_error (parser, "expected %<{%> or %<:%>");
16166 return error_mark_node;
16169 /* At this point, we're going ahead with the class-specifier, even
16170 if some other problem occurs. */
16171 cp_parser_commit_to_tentative_parse (parser);
16172 /* Issue the error about the overly-qualified name now. */
16173 if (qualified_p)
16175 cp_parser_error (parser,
16176 "global qualification of class name is invalid");
16177 return error_mark_node;
16179 else if (invalid_nested_name_p)
16181 cp_parser_error (parser,
16182 "qualified name does not name a class");
16183 return error_mark_node;
16185 else if (nested_name_specifier)
16187 tree scope;
16189 /* Reject typedef-names in class heads. */
16190 if (!DECL_IMPLICIT_TYPEDEF_P (type))
16192 error_at (type_start_token->location,
16193 "invalid class name in declaration of %qD",
16194 type);
16195 type = NULL_TREE;
16196 goto done;
16199 /* Figure out in what scope the declaration is being placed. */
16200 scope = current_scope ();
16201 /* If that scope does not contain the scope in which the
16202 class was originally declared, the program is invalid. */
16203 if (scope && !is_ancestor (scope, nested_name_specifier))
16205 if (at_namespace_scope_p ())
16206 error_at (type_start_token->location,
16207 "declaration of %qD in namespace %qD which does not "
16208 "enclose %qD",
16209 type, scope, nested_name_specifier);
16210 else
16211 error_at (type_start_token->location,
16212 "declaration of %qD in %qD which does not enclose %qD",
16213 type, scope, nested_name_specifier);
16214 type = NULL_TREE;
16215 goto done;
16217 /* [dcl.meaning]
16219 A declarator-id shall not be qualified except for the
16220 definition of a ... nested class outside of its class
16221 ... [or] the definition or explicit instantiation of a
16222 class member of a namespace outside of its namespace. */
16223 if (scope == nested_name_specifier)
16225 permerror (nested_name_specifier_token_start->location,
16226 "extra qualification not allowed");
16227 nested_name_specifier = NULL_TREE;
16228 num_templates = 0;
16231 /* An explicit-specialization must be preceded by "template <>". If
16232 it is not, try to recover gracefully. */
16233 if (at_namespace_scope_p ()
16234 && parser->num_template_parameter_lists == 0
16235 && template_id_p)
16237 error_at (type_start_token->location,
16238 "an explicit specialization must be preceded by %<template <>%>");
16239 invalid_explicit_specialization_p = true;
16240 /* Take the same action that would have been taken by
16241 cp_parser_explicit_specialization. */
16242 ++parser->num_template_parameter_lists;
16243 begin_specialization ();
16245 /* There must be no "return" statements between this point and the
16246 end of this function; set "type "to the correct return value and
16247 use "goto done;" to return. */
16248 /* Make sure that the right number of template parameters were
16249 present. */
16250 if (!cp_parser_check_template_parameters (parser, num_templates,
16251 type_start_token->location,
16252 /*declarator=*/NULL))
16254 /* If something went wrong, there is no point in even trying to
16255 process the class-definition. */
16256 type = NULL_TREE;
16257 goto done;
16260 /* Look up the type. */
16261 if (template_id_p)
16263 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
16264 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
16265 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
16267 error_at (type_start_token->location,
16268 "function template %qD redeclared as a class template", id);
16269 type = error_mark_node;
16271 else
16273 type = TREE_TYPE (id);
16274 type = maybe_process_partial_specialization (type);
16276 if (nested_name_specifier)
16277 pushed_scope = push_scope (nested_name_specifier);
16279 else if (nested_name_specifier)
16281 tree class_type;
16283 /* Given:
16285 template <typename T> struct S { struct T };
16286 template <typename T> struct S<T>::T { };
16288 we will get a TYPENAME_TYPE when processing the definition of
16289 `S::T'. We need to resolve it to the actual type before we
16290 try to define it. */
16291 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
16293 class_type = resolve_typename_type (TREE_TYPE (type),
16294 /*only_current_p=*/false);
16295 if (TREE_CODE (class_type) != TYPENAME_TYPE)
16296 type = TYPE_NAME (class_type);
16297 else
16299 cp_parser_error (parser, "could not resolve typename type");
16300 type = error_mark_node;
16304 if (maybe_process_partial_specialization (TREE_TYPE (type))
16305 == error_mark_node)
16307 type = NULL_TREE;
16308 goto done;
16311 class_type = current_class_type;
16312 /* Enter the scope indicated by the nested-name-specifier. */
16313 pushed_scope = push_scope (nested_name_specifier);
16314 /* Get the canonical version of this type. */
16315 type = TYPE_MAIN_DECL (TREE_TYPE (type));
16316 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
16317 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
16319 type = push_template_decl (type);
16320 if (type == error_mark_node)
16322 type = NULL_TREE;
16323 goto done;
16327 type = TREE_TYPE (type);
16328 *nested_name_specifier_p = true;
16330 else /* The name is not a nested name. */
16332 /* If the class was unnamed, create a dummy name. */
16333 if (!id)
16334 id = make_anon_name ();
16335 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
16336 parser->num_template_parameter_lists);
16339 /* Indicate whether this class was declared as a `class' or as a
16340 `struct'. */
16341 if (TREE_CODE (type) == RECORD_TYPE)
16342 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
16343 cp_parser_check_class_key (class_key, type);
16345 /* If this type was already complete, and we see another definition,
16346 that's an error. */
16347 if (type != error_mark_node && COMPLETE_TYPE_P (type))
16349 error_at (type_start_token->location, "redefinition of %q#T",
16350 type);
16351 error_at (type_start_token->location, "previous definition of %q+#T",
16352 type);
16353 type = NULL_TREE;
16354 goto done;
16356 else if (type == error_mark_node)
16357 type = NULL_TREE;
16359 /* We will have entered the scope containing the class; the names of
16360 base classes should be looked up in that context. For example:
16362 struct A { struct B {}; struct C; };
16363 struct A::C : B {};
16365 is valid. */
16367 /* Get the list of base-classes, if there is one. */
16368 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16369 *bases = cp_parser_base_clause (parser);
16371 done:
16372 /* Leave the scope given by the nested-name-specifier. We will
16373 enter the class scope itself while processing the members. */
16374 if (pushed_scope)
16375 pop_scope (pushed_scope);
16377 if (invalid_explicit_specialization_p)
16379 end_specialization ();
16380 --parser->num_template_parameter_lists;
16382 *attributes_p = attributes;
16383 return type;
16386 /* Parse a class-key.
16388 class-key:
16389 class
16390 struct
16391 union
16393 Returns the kind of class-key specified, or none_type to indicate
16394 error. */
16396 static enum tag_types
16397 cp_parser_class_key (cp_parser* parser)
16399 cp_token *token;
16400 enum tag_types tag_type;
16402 /* Look for the class-key. */
16403 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
16404 if (!token)
16405 return none_type;
16407 /* Check to see if the TOKEN is a class-key. */
16408 tag_type = cp_parser_token_is_class_key (token);
16409 if (!tag_type)
16410 cp_parser_error (parser, "expected class-key");
16411 return tag_type;
16414 /* Parse an (optional) member-specification.
16416 member-specification:
16417 member-declaration member-specification [opt]
16418 access-specifier : member-specification [opt] */
16420 static void
16421 cp_parser_member_specification_opt (cp_parser* parser)
16423 while (true)
16425 cp_token *token;
16426 enum rid keyword;
16428 /* Peek at the next token. */
16429 token = cp_lexer_peek_token (parser->lexer);
16430 /* If it's a `}', or EOF then we've seen all the members. */
16431 if (token->type == CPP_CLOSE_BRACE
16432 || token->type == CPP_EOF
16433 || token->type == CPP_PRAGMA_EOL)
16434 break;
16436 /* See if this token is a keyword. */
16437 keyword = token->keyword;
16438 switch (keyword)
16440 case RID_PUBLIC:
16441 case RID_PROTECTED:
16442 case RID_PRIVATE:
16443 /* Consume the access-specifier. */
16444 cp_lexer_consume_token (parser->lexer);
16445 /* Remember which access-specifier is active. */
16446 current_access_specifier = token->u.value;
16447 /* Look for the `:'. */
16448 cp_parser_require (parser, CPP_COLON, "%<:%>");
16449 break;
16451 default:
16452 /* Accept #pragmas at class scope. */
16453 if (token->type == CPP_PRAGMA)
16455 cp_parser_pragma (parser, pragma_external);
16456 break;
16459 /* Otherwise, the next construction must be a
16460 member-declaration. */
16461 cp_parser_member_declaration (parser);
16466 /* Parse a member-declaration.
16468 member-declaration:
16469 decl-specifier-seq [opt] member-declarator-list [opt] ;
16470 function-definition ; [opt]
16471 :: [opt] nested-name-specifier template [opt] unqualified-id ;
16472 using-declaration
16473 template-declaration
16475 member-declarator-list:
16476 member-declarator
16477 member-declarator-list , member-declarator
16479 member-declarator:
16480 declarator pure-specifier [opt]
16481 declarator constant-initializer [opt]
16482 identifier [opt] : constant-expression
16484 GNU Extensions:
16486 member-declaration:
16487 __extension__ member-declaration
16489 member-declarator:
16490 declarator attributes [opt] pure-specifier [opt]
16491 declarator attributes [opt] constant-initializer [opt]
16492 identifier [opt] attributes [opt] : constant-expression
16494 C++0x Extensions:
16496 member-declaration:
16497 static_assert-declaration */
16499 static void
16500 cp_parser_member_declaration (cp_parser* parser)
16502 cp_decl_specifier_seq decl_specifiers;
16503 tree prefix_attributes;
16504 tree decl;
16505 int declares_class_or_enum;
16506 bool friend_p;
16507 cp_token *token = NULL;
16508 cp_token *decl_spec_token_start = NULL;
16509 cp_token *initializer_token_start = NULL;
16510 int saved_pedantic;
16512 /* Check for the `__extension__' keyword. */
16513 if (cp_parser_extension_opt (parser, &saved_pedantic))
16515 /* Recurse. */
16516 cp_parser_member_declaration (parser);
16517 /* Restore the old value of the PEDANTIC flag. */
16518 pedantic = saved_pedantic;
16520 return;
16523 /* Check for a template-declaration. */
16524 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16526 /* An explicit specialization here is an error condition, and we
16527 expect the specialization handler to detect and report this. */
16528 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16529 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
16530 cp_parser_explicit_specialization (parser);
16531 else
16532 cp_parser_template_declaration (parser, /*member_p=*/true);
16534 return;
16537 /* Check for a using-declaration. */
16538 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
16540 /* Parse the using-declaration. */
16541 cp_parser_using_declaration (parser,
16542 /*access_declaration_p=*/false);
16543 return;
16546 /* Check for @defs. */
16547 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
16549 tree ivar, member;
16550 tree ivar_chains = cp_parser_objc_defs_expression (parser);
16551 ivar = ivar_chains;
16552 while (ivar)
16554 member = ivar;
16555 ivar = TREE_CHAIN (member);
16556 TREE_CHAIN (member) = NULL_TREE;
16557 finish_member_declaration (member);
16559 return;
16562 /* If the next token is `static_assert' we have a static assertion. */
16563 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
16565 cp_parser_static_assert (parser, /*member_p=*/true);
16566 return;
16569 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
16570 return;
16572 /* Parse the decl-specifier-seq. */
16573 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
16574 cp_parser_decl_specifier_seq (parser,
16575 CP_PARSER_FLAGS_OPTIONAL,
16576 &decl_specifiers,
16577 &declares_class_or_enum);
16578 prefix_attributes = decl_specifiers.attributes;
16579 decl_specifiers.attributes = NULL_TREE;
16580 /* Check for an invalid type-name. */
16581 if (!decl_specifiers.any_type_specifiers_p
16582 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
16583 return;
16584 /* If there is no declarator, then the decl-specifier-seq should
16585 specify a type. */
16586 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16588 /* If there was no decl-specifier-seq, and the next token is a
16589 `;', then we have something like:
16591 struct S { ; };
16593 [class.mem]
16595 Each member-declaration shall declare at least one member
16596 name of the class. */
16597 if (!decl_specifiers.any_specifiers_p)
16599 cp_token *token = cp_lexer_peek_token (parser->lexer);
16600 if (!in_system_header_at (token->location))
16601 pedwarn (token->location, OPT_pedantic, "extra %<;%>");
16603 else
16605 tree type;
16607 /* See if this declaration is a friend. */
16608 friend_p = cp_parser_friend_p (&decl_specifiers);
16609 /* If there were decl-specifiers, check to see if there was
16610 a class-declaration. */
16611 type = check_tag_decl (&decl_specifiers);
16612 /* Nested classes have already been added to the class, but
16613 a `friend' needs to be explicitly registered. */
16614 if (friend_p)
16616 /* If the `friend' keyword was present, the friend must
16617 be introduced with a class-key. */
16618 if (!declares_class_or_enum)
16619 error_at (decl_spec_token_start->location,
16620 "a class-key must be used when declaring a friend");
16621 /* In this case:
16623 template <typename T> struct A {
16624 friend struct A<T>::B;
16627 A<T>::B will be represented by a TYPENAME_TYPE, and
16628 therefore not recognized by check_tag_decl. */
16629 if (!type
16630 && decl_specifiers.type
16631 && TYPE_P (decl_specifiers.type))
16632 type = decl_specifiers.type;
16633 if (!type || !TYPE_P (type))
16634 error_at (decl_spec_token_start->location,
16635 "friend declaration does not name a class or "
16636 "function");
16637 else
16638 make_friend_class (current_class_type, type,
16639 /*complain=*/true);
16641 /* If there is no TYPE, an error message will already have
16642 been issued. */
16643 else if (!type || type == error_mark_node)
16645 /* An anonymous aggregate has to be handled specially; such
16646 a declaration really declares a data member (with a
16647 particular type), as opposed to a nested class. */
16648 else if (ANON_AGGR_TYPE_P (type))
16650 /* Remove constructors and such from TYPE, now that we
16651 know it is an anonymous aggregate. */
16652 fixup_anonymous_aggr (type);
16653 /* And make the corresponding data member. */
16654 decl = build_decl (decl_spec_token_start->location,
16655 FIELD_DECL, NULL_TREE, type);
16656 /* Add it to the class. */
16657 finish_member_declaration (decl);
16659 else
16660 cp_parser_check_access_in_redeclaration
16661 (TYPE_NAME (type),
16662 decl_spec_token_start->location);
16665 else
16667 /* See if these declarations will be friends. */
16668 friend_p = cp_parser_friend_p (&decl_specifiers);
16670 /* Keep going until we hit the `;' at the end of the
16671 declaration. */
16672 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16674 tree attributes = NULL_TREE;
16675 tree first_attribute;
16677 /* Peek at the next token. */
16678 token = cp_lexer_peek_token (parser->lexer);
16680 /* Check for a bitfield declaration. */
16681 if (token->type == CPP_COLON
16682 || (token->type == CPP_NAME
16683 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
16684 == CPP_COLON))
16686 tree identifier;
16687 tree width;
16689 /* Get the name of the bitfield. Note that we cannot just
16690 check TOKEN here because it may have been invalidated by
16691 the call to cp_lexer_peek_nth_token above. */
16692 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
16693 identifier = cp_parser_identifier (parser);
16694 else
16695 identifier = NULL_TREE;
16697 /* Consume the `:' token. */
16698 cp_lexer_consume_token (parser->lexer);
16699 /* Get the width of the bitfield. */
16700 width
16701 = cp_parser_constant_expression (parser,
16702 /*allow_non_constant=*/false,
16703 NULL);
16705 /* Look for attributes that apply to the bitfield. */
16706 attributes = cp_parser_attributes_opt (parser);
16707 /* Remember which attributes are prefix attributes and
16708 which are not. */
16709 first_attribute = attributes;
16710 /* Combine the attributes. */
16711 attributes = chainon (prefix_attributes, attributes);
16713 /* Create the bitfield declaration. */
16714 decl = grokbitfield (identifier
16715 ? make_id_declarator (NULL_TREE,
16716 identifier,
16717 sfk_none)
16718 : NULL,
16719 &decl_specifiers,
16720 width,
16721 attributes);
16723 else
16725 cp_declarator *declarator;
16726 tree initializer;
16727 tree asm_specification;
16728 int ctor_dtor_or_conv_p;
16730 /* Parse the declarator. */
16731 declarator
16732 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16733 &ctor_dtor_or_conv_p,
16734 /*parenthesized_p=*/NULL,
16735 /*member_p=*/true);
16737 /* If something went wrong parsing the declarator, make sure
16738 that we at least consume some tokens. */
16739 if (declarator == cp_error_declarator)
16741 /* Skip to the end of the statement. */
16742 cp_parser_skip_to_end_of_statement (parser);
16743 /* If the next token is not a semicolon, that is
16744 probably because we just skipped over the body of
16745 a function. So, we consume a semicolon if
16746 present, but do not issue an error message if it
16747 is not present. */
16748 if (cp_lexer_next_token_is (parser->lexer,
16749 CPP_SEMICOLON))
16750 cp_lexer_consume_token (parser->lexer);
16751 return;
16754 if (declares_class_or_enum & 2)
16755 cp_parser_check_for_definition_in_return_type
16756 (declarator, decl_specifiers.type,
16757 decl_specifiers.type_location);
16759 /* Look for an asm-specification. */
16760 asm_specification = cp_parser_asm_specification_opt (parser);
16761 /* Look for attributes that apply to the declaration. */
16762 attributes = cp_parser_attributes_opt (parser);
16763 /* Remember which attributes are prefix attributes and
16764 which are not. */
16765 first_attribute = attributes;
16766 /* Combine the attributes. */
16767 attributes = chainon (prefix_attributes, attributes);
16769 /* If it's an `=', then we have a constant-initializer or a
16770 pure-specifier. It is not correct to parse the
16771 initializer before registering the member declaration
16772 since the member declaration should be in scope while
16773 its initializer is processed. However, the rest of the
16774 front end does not yet provide an interface that allows
16775 us to handle this correctly. */
16776 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16778 /* In [class.mem]:
16780 A pure-specifier shall be used only in the declaration of
16781 a virtual function.
16783 A member-declarator can contain a constant-initializer
16784 only if it declares a static member of integral or
16785 enumeration type.
16787 Therefore, if the DECLARATOR is for a function, we look
16788 for a pure-specifier; otherwise, we look for a
16789 constant-initializer. When we call `grokfield', it will
16790 perform more stringent semantics checks. */
16791 initializer_token_start = cp_lexer_peek_token (parser->lexer);
16792 if (function_declarator_p (declarator))
16793 initializer = cp_parser_pure_specifier (parser);
16794 else
16795 /* Parse the initializer. */
16796 initializer = cp_parser_constant_initializer (parser);
16798 /* Otherwise, there is no initializer. */
16799 else
16800 initializer = NULL_TREE;
16802 /* See if we are probably looking at a function
16803 definition. We are certainly not looking at a
16804 member-declarator. Calling `grokfield' has
16805 side-effects, so we must not do it unless we are sure
16806 that we are looking at a member-declarator. */
16807 if (cp_parser_token_starts_function_definition_p
16808 (cp_lexer_peek_token (parser->lexer)))
16810 /* The grammar does not allow a pure-specifier to be
16811 used when a member function is defined. (It is
16812 possible that this fact is an oversight in the
16813 standard, since a pure function may be defined
16814 outside of the class-specifier. */
16815 if (initializer)
16816 error_at (initializer_token_start->location,
16817 "pure-specifier on function-definition");
16818 decl = cp_parser_save_member_function_body (parser,
16819 &decl_specifiers,
16820 declarator,
16821 attributes);
16822 /* If the member was not a friend, declare it here. */
16823 if (!friend_p)
16824 finish_member_declaration (decl);
16825 /* Peek at the next token. */
16826 token = cp_lexer_peek_token (parser->lexer);
16827 /* If the next token is a semicolon, consume it. */
16828 if (token->type == CPP_SEMICOLON)
16829 cp_lexer_consume_token (parser->lexer);
16830 return;
16832 else
16833 if (declarator->kind == cdk_function)
16834 declarator->id_loc = token->location;
16835 /* Create the declaration. */
16836 decl = grokfield (declarator, &decl_specifiers,
16837 initializer, /*init_const_expr_p=*/true,
16838 asm_specification,
16839 attributes);
16842 /* Reset PREFIX_ATTRIBUTES. */
16843 while (attributes && TREE_CHAIN (attributes) != first_attribute)
16844 attributes = TREE_CHAIN (attributes);
16845 if (attributes)
16846 TREE_CHAIN (attributes) = NULL_TREE;
16848 /* If there is any qualification still in effect, clear it
16849 now; we will be starting fresh with the next declarator. */
16850 parser->scope = NULL_TREE;
16851 parser->qualifying_scope = NULL_TREE;
16852 parser->object_scope = NULL_TREE;
16853 /* If it's a `,', then there are more declarators. */
16854 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16855 cp_lexer_consume_token (parser->lexer);
16856 /* If the next token isn't a `;', then we have a parse error. */
16857 else if (cp_lexer_next_token_is_not (parser->lexer,
16858 CPP_SEMICOLON))
16860 cp_parser_error (parser, "expected %<;%>");
16861 /* Skip tokens until we find a `;'. */
16862 cp_parser_skip_to_end_of_statement (parser);
16864 break;
16867 if (decl)
16869 /* Add DECL to the list of members. */
16870 if (!friend_p)
16871 finish_member_declaration (decl);
16873 if (TREE_CODE (decl) == FUNCTION_DECL)
16874 cp_parser_save_default_args (parser, decl);
16879 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16882 /* Parse a pure-specifier.
16884 pure-specifier:
16887 Returns INTEGER_ZERO_NODE if a pure specifier is found.
16888 Otherwise, ERROR_MARK_NODE is returned. */
16890 static tree
16891 cp_parser_pure_specifier (cp_parser* parser)
16893 cp_token *token;
16895 /* Look for the `=' token. */
16896 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16897 return error_mark_node;
16898 /* Look for the `0' token. */
16899 token = cp_lexer_peek_token (parser->lexer);
16901 if (token->type == CPP_EOF
16902 || token->type == CPP_PRAGMA_EOL)
16903 return error_mark_node;
16905 cp_lexer_consume_token (parser->lexer);
16907 /* Accept = default or = delete in c++0x mode. */
16908 if (token->keyword == RID_DEFAULT
16909 || token->keyword == RID_DELETE)
16911 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
16912 return token->u.value;
16915 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
16916 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
16918 cp_parser_error (parser,
16919 "invalid pure specifier (only %<= 0%> is allowed)");
16920 cp_parser_skip_to_end_of_statement (parser);
16921 return error_mark_node;
16923 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
16925 error_at (token->location, "templates may not be %<virtual%>");
16926 return error_mark_node;
16929 return integer_zero_node;
16932 /* Parse a constant-initializer.
16934 constant-initializer:
16935 = constant-expression
16937 Returns a representation of the constant-expression. */
16939 static tree
16940 cp_parser_constant_initializer (cp_parser* parser)
16942 /* Look for the `=' token. */
16943 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16944 return error_mark_node;
16946 /* It is invalid to write:
16948 struct S { static const int i = { 7 }; };
16951 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16953 cp_parser_error (parser,
16954 "a brace-enclosed initializer is not allowed here");
16955 /* Consume the opening brace. */
16956 cp_lexer_consume_token (parser->lexer);
16957 /* Skip the initializer. */
16958 cp_parser_skip_to_closing_brace (parser);
16959 /* Look for the trailing `}'. */
16960 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
16962 return error_mark_node;
16965 return cp_parser_constant_expression (parser,
16966 /*allow_non_constant=*/false,
16967 NULL);
16970 /* Derived classes [gram.class.derived] */
16972 /* Parse a base-clause.
16974 base-clause:
16975 : base-specifier-list
16977 base-specifier-list:
16978 base-specifier ... [opt]
16979 base-specifier-list , base-specifier ... [opt]
16981 Returns a TREE_LIST representing the base-classes, in the order in
16982 which they were declared. The representation of each node is as
16983 described by cp_parser_base_specifier.
16985 In the case that no bases are specified, this function will return
16986 NULL_TREE, not ERROR_MARK_NODE. */
16988 static tree
16989 cp_parser_base_clause (cp_parser* parser)
16991 tree bases = NULL_TREE;
16993 /* Look for the `:' that begins the list. */
16994 cp_parser_require (parser, CPP_COLON, "%<:%>");
16996 /* Scan the base-specifier-list. */
16997 while (true)
16999 cp_token *token;
17000 tree base;
17001 bool pack_expansion_p = false;
17003 /* Look for the base-specifier. */
17004 base = cp_parser_base_specifier (parser);
17005 /* Look for the (optional) ellipsis. */
17006 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17008 /* Consume the `...'. */
17009 cp_lexer_consume_token (parser->lexer);
17011 pack_expansion_p = true;
17014 /* Add BASE to the front of the list. */
17015 if (base != error_mark_node)
17017 if (pack_expansion_p)
17018 /* Make this a pack expansion type. */
17019 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
17022 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
17024 TREE_CHAIN (base) = bases;
17025 bases = base;
17028 /* Peek at the next token. */
17029 token = cp_lexer_peek_token (parser->lexer);
17030 /* If it's not a comma, then the list is complete. */
17031 if (token->type != CPP_COMMA)
17032 break;
17033 /* Consume the `,'. */
17034 cp_lexer_consume_token (parser->lexer);
17037 /* PARSER->SCOPE may still be non-NULL at this point, if the last
17038 base class had a qualified name. However, the next name that
17039 appears is certainly not qualified. */
17040 parser->scope = NULL_TREE;
17041 parser->qualifying_scope = NULL_TREE;
17042 parser->object_scope = NULL_TREE;
17044 return nreverse (bases);
17047 /* Parse a base-specifier.
17049 base-specifier:
17050 :: [opt] nested-name-specifier [opt] class-name
17051 virtual access-specifier [opt] :: [opt] nested-name-specifier
17052 [opt] class-name
17053 access-specifier virtual [opt] :: [opt] nested-name-specifier
17054 [opt] class-name
17056 Returns a TREE_LIST. The TREE_PURPOSE will be one of
17057 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
17058 indicate the specifiers provided. The TREE_VALUE will be a TYPE
17059 (or the ERROR_MARK_NODE) indicating the type that was specified. */
17061 static tree
17062 cp_parser_base_specifier (cp_parser* parser)
17064 cp_token *token;
17065 bool done = false;
17066 bool virtual_p = false;
17067 bool duplicate_virtual_error_issued_p = false;
17068 bool duplicate_access_error_issued_p = false;
17069 bool class_scope_p, template_p;
17070 tree access = access_default_node;
17071 tree type;
17073 /* Process the optional `virtual' and `access-specifier'. */
17074 while (!done)
17076 /* Peek at the next token. */
17077 token = cp_lexer_peek_token (parser->lexer);
17078 /* Process `virtual'. */
17079 switch (token->keyword)
17081 case RID_VIRTUAL:
17082 /* If `virtual' appears more than once, issue an error. */
17083 if (virtual_p && !duplicate_virtual_error_issued_p)
17085 cp_parser_error (parser,
17086 "%<virtual%> specified more than once in base-specified");
17087 duplicate_virtual_error_issued_p = true;
17090 virtual_p = true;
17092 /* Consume the `virtual' token. */
17093 cp_lexer_consume_token (parser->lexer);
17095 break;
17097 case RID_PUBLIC:
17098 case RID_PROTECTED:
17099 case RID_PRIVATE:
17100 /* If more than one access specifier appears, issue an
17101 error. */
17102 if (access != access_default_node
17103 && !duplicate_access_error_issued_p)
17105 cp_parser_error (parser,
17106 "more than one access specifier in base-specified");
17107 duplicate_access_error_issued_p = true;
17110 access = ridpointers[(int) token->keyword];
17112 /* Consume the access-specifier. */
17113 cp_lexer_consume_token (parser->lexer);
17115 break;
17117 default:
17118 done = true;
17119 break;
17122 /* It is not uncommon to see programs mechanically, erroneously, use
17123 the 'typename' keyword to denote (dependent) qualified types
17124 as base classes. */
17125 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
17127 token = cp_lexer_peek_token (parser->lexer);
17128 if (!processing_template_decl)
17129 error_at (token->location,
17130 "keyword %<typename%> not allowed outside of templates");
17131 else
17132 error_at (token->location,
17133 "keyword %<typename%> not allowed in this context "
17134 "(the base class is implicitly a type)");
17135 cp_lexer_consume_token (parser->lexer);
17138 /* Look for the optional `::' operator. */
17139 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
17140 /* Look for the nested-name-specifier. The simplest way to
17141 implement:
17143 [temp.res]
17145 The keyword `typename' is not permitted in a base-specifier or
17146 mem-initializer; in these contexts a qualified name that
17147 depends on a template-parameter is implicitly assumed to be a
17148 type name.
17150 is to pretend that we have seen the `typename' keyword at this
17151 point. */
17152 cp_parser_nested_name_specifier_opt (parser,
17153 /*typename_keyword_p=*/true,
17154 /*check_dependency_p=*/true,
17155 typename_type,
17156 /*is_declaration=*/true);
17157 /* If the base class is given by a qualified name, assume that names
17158 we see are type names or templates, as appropriate. */
17159 class_scope_p = (parser->scope && TYPE_P (parser->scope));
17160 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
17162 /* Finally, look for the class-name. */
17163 type = cp_parser_class_name (parser,
17164 class_scope_p,
17165 template_p,
17166 typename_type,
17167 /*check_dependency_p=*/true,
17168 /*class_head_p=*/false,
17169 /*is_declaration=*/true);
17171 if (type == error_mark_node)
17172 return error_mark_node;
17174 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
17177 /* Exception handling [gram.exception] */
17179 /* Parse an (optional) exception-specification.
17181 exception-specification:
17182 throw ( type-id-list [opt] )
17184 Returns a TREE_LIST representing the exception-specification. The
17185 TREE_VALUE of each node is a type. */
17187 static tree
17188 cp_parser_exception_specification_opt (cp_parser* parser)
17190 cp_token *token;
17191 tree type_id_list;
17193 /* Peek at the next token. */
17194 token = cp_lexer_peek_token (parser->lexer);
17195 /* If it's not `throw', then there's no exception-specification. */
17196 if (!cp_parser_is_keyword (token, RID_THROW))
17197 return NULL_TREE;
17199 /* Consume the `throw'. */
17200 cp_lexer_consume_token (parser->lexer);
17202 /* Look for the `('. */
17203 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17205 /* Peek at the next token. */
17206 token = cp_lexer_peek_token (parser->lexer);
17207 /* If it's not a `)', then there is a type-id-list. */
17208 if (token->type != CPP_CLOSE_PAREN)
17210 const char *saved_message;
17212 /* Types may not be defined in an exception-specification. */
17213 saved_message = parser->type_definition_forbidden_message;
17214 parser->type_definition_forbidden_message
17215 = G_("types may not be defined in an exception-specification");
17216 /* Parse the type-id-list. */
17217 type_id_list = cp_parser_type_id_list (parser);
17218 /* Restore the saved message. */
17219 parser->type_definition_forbidden_message = saved_message;
17221 else
17222 type_id_list = empty_except_spec;
17224 /* Look for the `)'. */
17225 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17227 return type_id_list;
17230 /* Parse an (optional) type-id-list.
17232 type-id-list:
17233 type-id ... [opt]
17234 type-id-list , type-id ... [opt]
17236 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
17237 in the order that the types were presented. */
17239 static tree
17240 cp_parser_type_id_list (cp_parser* parser)
17242 tree types = NULL_TREE;
17244 while (true)
17246 cp_token *token;
17247 tree type;
17249 /* Get the next type-id. */
17250 type = cp_parser_type_id (parser);
17251 /* Parse the optional ellipsis. */
17252 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17254 /* Consume the `...'. */
17255 cp_lexer_consume_token (parser->lexer);
17257 /* Turn the type into a pack expansion expression. */
17258 type = make_pack_expansion (type);
17260 /* Add it to the list. */
17261 types = add_exception_specifier (types, type, /*complain=*/1);
17262 /* Peek at the next token. */
17263 token = cp_lexer_peek_token (parser->lexer);
17264 /* If it is not a `,', we are done. */
17265 if (token->type != CPP_COMMA)
17266 break;
17267 /* Consume the `,'. */
17268 cp_lexer_consume_token (parser->lexer);
17271 return nreverse (types);
17274 /* Parse a try-block.
17276 try-block:
17277 try compound-statement handler-seq */
17279 static tree
17280 cp_parser_try_block (cp_parser* parser)
17282 tree try_block;
17284 cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
17285 try_block = begin_try_block ();
17286 cp_parser_compound_statement (parser, NULL, true);
17287 finish_try_block (try_block);
17288 cp_parser_handler_seq (parser);
17289 finish_handler_sequence (try_block);
17291 return try_block;
17294 /* Parse a function-try-block.
17296 function-try-block:
17297 try ctor-initializer [opt] function-body handler-seq */
17299 static bool
17300 cp_parser_function_try_block (cp_parser* parser)
17302 tree compound_stmt;
17303 tree try_block;
17304 bool ctor_initializer_p;
17306 /* Look for the `try' keyword. */
17307 if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
17308 return false;
17309 /* Let the rest of the front end know where we are. */
17310 try_block = begin_function_try_block (&compound_stmt);
17311 /* Parse the function-body. */
17312 ctor_initializer_p
17313 = cp_parser_ctor_initializer_opt_and_function_body (parser);
17314 /* We're done with the `try' part. */
17315 finish_function_try_block (try_block);
17316 /* Parse the handlers. */
17317 cp_parser_handler_seq (parser);
17318 /* We're done with the handlers. */
17319 finish_function_handler_sequence (try_block, compound_stmt);
17321 return ctor_initializer_p;
17324 /* Parse a handler-seq.
17326 handler-seq:
17327 handler handler-seq [opt] */
17329 static void
17330 cp_parser_handler_seq (cp_parser* parser)
17332 while (true)
17334 cp_token *token;
17336 /* Parse the handler. */
17337 cp_parser_handler (parser);
17338 /* Peek at the next token. */
17339 token = cp_lexer_peek_token (parser->lexer);
17340 /* If it's not `catch' then there are no more handlers. */
17341 if (!cp_parser_is_keyword (token, RID_CATCH))
17342 break;
17346 /* Parse a handler.
17348 handler:
17349 catch ( exception-declaration ) compound-statement */
17351 static void
17352 cp_parser_handler (cp_parser* parser)
17354 tree handler;
17355 tree declaration;
17357 cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
17358 handler = begin_handler ();
17359 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17360 declaration = cp_parser_exception_declaration (parser);
17361 finish_handler_parms (declaration, handler);
17362 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17363 cp_parser_compound_statement (parser, NULL, false);
17364 finish_handler (handler);
17367 /* Parse an exception-declaration.
17369 exception-declaration:
17370 type-specifier-seq declarator
17371 type-specifier-seq abstract-declarator
17372 type-specifier-seq
17375 Returns a VAR_DECL for the declaration, or NULL_TREE if the
17376 ellipsis variant is used. */
17378 static tree
17379 cp_parser_exception_declaration (cp_parser* parser)
17381 cp_decl_specifier_seq type_specifiers;
17382 cp_declarator *declarator;
17383 const char *saved_message;
17385 /* If it's an ellipsis, it's easy to handle. */
17386 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17388 /* Consume the `...' token. */
17389 cp_lexer_consume_token (parser->lexer);
17390 return NULL_TREE;
17393 /* Types may not be defined in exception-declarations. */
17394 saved_message = parser->type_definition_forbidden_message;
17395 parser->type_definition_forbidden_message
17396 = G_("types may not be defined in exception-declarations");
17398 /* Parse the type-specifier-seq. */
17399 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
17400 /*is_trailing_return=*/false,
17401 &type_specifiers);
17402 /* If it's a `)', then there is no declarator. */
17403 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
17404 declarator = NULL;
17405 else
17406 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
17407 /*ctor_dtor_or_conv_p=*/NULL,
17408 /*parenthesized_p=*/NULL,
17409 /*member_p=*/false);
17411 /* Restore the saved message. */
17412 parser->type_definition_forbidden_message = saved_message;
17414 if (!type_specifiers.any_specifiers_p)
17415 return error_mark_node;
17417 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
17420 /* Parse a throw-expression.
17422 throw-expression:
17423 throw assignment-expression [opt]
17425 Returns a THROW_EXPR representing the throw-expression. */
17427 static tree
17428 cp_parser_throw_expression (cp_parser* parser)
17430 tree expression;
17431 cp_token* token;
17433 cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
17434 token = cp_lexer_peek_token (parser->lexer);
17435 /* Figure out whether or not there is an assignment-expression
17436 following the "throw" keyword. */
17437 if (token->type == CPP_COMMA
17438 || token->type == CPP_SEMICOLON
17439 || token->type == CPP_CLOSE_PAREN
17440 || token->type == CPP_CLOSE_SQUARE
17441 || token->type == CPP_CLOSE_BRACE
17442 || token->type == CPP_COLON)
17443 expression = NULL_TREE;
17444 else
17445 expression = cp_parser_assignment_expression (parser,
17446 /*cast_p=*/false, NULL);
17448 return build_throw (expression);
17451 /* GNU Extensions */
17453 /* Parse an (optional) asm-specification.
17455 asm-specification:
17456 asm ( string-literal )
17458 If the asm-specification is present, returns a STRING_CST
17459 corresponding to the string-literal. Otherwise, returns
17460 NULL_TREE. */
17462 static tree
17463 cp_parser_asm_specification_opt (cp_parser* parser)
17465 cp_token *token;
17466 tree asm_specification;
17468 /* Peek at the next token. */
17469 token = cp_lexer_peek_token (parser->lexer);
17470 /* If the next token isn't the `asm' keyword, then there's no
17471 asm-specification. */
17472 if (!cp_parser_is_keyword (token, RID_ASM))
17473 return NULL_TREE;
17475 /* Consume the `asm' token. */
17476 cp_lexer_consume_token (parser->lexer);
17477 /* Look for the `('. */
17478 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17480 /* Look for the string-literal. */
17481 asm_specification = cp_parser_string_literal (parser, false, false);
17483 /* Look for the `)'. */
17484 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17486 return asm_specification;
17489 /* Parse an asm-operand-list.
17491 asm-operand-list:
17492 asm-operand
17493 asm-operand-list , asm-operand
17495 asm-operand:
17496 string-literal ( expression )
17497 [ string-literal ] string-literal ( expression )
17499 Returns a TREE_LIST representing the operands. The TREE_VALUE of
17500 each node is the expression. The TREE_PURPOSE is itself a
17501 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
17502 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
17503 is a STRING_CST for the string literal before the parenthesis. Returns
17504 ERROR_MARK_NODE if any of the operands are invalid. */
17506 static tree
17507 cp_parser_asm_operand_list (cp_parser* parser)
17509 tree asm_operands = NULL_TREE;
17510 bool invalid_operands = false;
17512 while (true)
17514 tree string_literal;
17515 tree expression;
17516 tree name;
17518 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
17520 /* Consume the `[' token. */
17521 cp_lexer_consume_token (parser->lexer);
17522 /* Read the operand name. */
17523 name = cp_parser_identifier (parser);
17524 if (name != error_mark_node)
17525 name = build_string (IDENTIFIER_LENGTH (name),
17526 IDENTIFIER_POINTER (name));
17527 /* Look for the closing `]'. */
17528 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
17530 else
17531 name = NULL_TREE;
17532 /* Look for the string-literal. */
17533 string_literal = cp_parser_string_literal (parser, false, false);
17535 /* Look for the `('. */
17536 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17537 /* Parse the expression. */
17538 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
17539 /* Look for the `)'. */
17540 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17542 if (name == error_mark_node
17543 || string_literal == error_mark_node
17544 || expression == error_mark_node)
17545 invalid_operands = true;
17547 /* Add this operand to the list. */
17548 asm_operands = tree_cons (build_tree_list (name, string_literal),
17549 expression,
17550 asm_operands);
17551 /* If the next token is not a `,', there are no more
17552 operands. */
17553 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17554 break;
17555 /* Consume the `,'. */
17556 cp_lexer_consume_token (parser->lexer);
17559 return invalid_operands ? error_mark_node : nreverse (asm_operands);
17562 /* Parse an asm-clobber-list.
17564 asm-clobber-list:
17565 string-literal
17566 asm-clobber-list , string-literal
17568 Returns a TREE_LIST, indicating the clobbers in the order that they
17569 appeared. The TREE_VALUE of each node is a STRING_CST. */
17571 static tree
17572 cp_parser_asm_clobber_list (cp_parser* parser)
17574 tree clobbers = NULL_TREE;
17576 while (true)
17578 tree string_literal;
17580 /* Look for the string literal. */
17581 string_literal = cp_parser_string_literal (parser, false, false);
17582 /* Add it to the list. */
17583 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
17584 /* If the next token is not a `,', then the list is
17585 complete. */
17586 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17587 break;
17588 /* Consume the `,' token. */
17589 cp_lexer_consume_token (parser->lexer);
17592 return clobbers;
17595 /* Parse an asm-label-list.
17597 asm-label-list:
17598 identifier
17599 asm-label-list , identifier
17601 Returns a TREE_LIST, indicating the labels in the order that they
17602 appeared. The TREE_VALUE of each node is a label. */
17604 static tree
17605 cp_parser_asm_label_list (cp_parser* parser)
17607 tree labels = NULL_TREE;
17609 while (true)
17611 tree identifier, label, name;
17613 /* Look for the identifier. */
17614 identifier = cp_parser_identifier (parser);
17615 if (!error_operand_p (identifier))
17617 label = lookup_label (identifier);
17618 if (TREE_CODE (label) == LABEL_DECL)
17620 TREE_USED (label) = 1;
17621 check_goto (label);
17622 name = build_string (IDENTIFIER_LENGTH (identifier),
17623 IDENTIFIER_POINTER (identifier));
17624 labels = tree_cons (name, label, labels);
17627 /* If the next token is not a `,', then the list is
17628 complete. */
17629 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17630 break;
17631 /* Consume the `,' token. */
17632 cp_lexer_consume_token (parser->lexer);
17635 return nreverse (labels);
17638 /* Parse an (optional) series of attributes.
17640 attributes:
17641 attributes attribute
17643 attribute:
17644 __attribute__ (( attribute-list [opt] ))
17646 The return value is as for cp_parser_attribute_list. */
17648 static tree
17649 cp_parser_attributes_opt (cp_parser* parser)
17651 tree attributes = NULL_TREE;
17653 while (true)
17655 cp_token *token;
17656 tree attribute_list;
17658 /* Peek at the next token. */
17659 token = cp_lexer_peek_token (parser->lexer);
17660 /* If it's not `__attribute__', then we're done. */
17661 if (token->keyword != RID_ATTRIBUTE)
17662 break;
17664 /* Consume the `__attribute__' keyword. */
17665 cp_lexer_consume_token (parser->lexer);
17666 /* Look for the two `(' tokens. */
17667 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17668 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17670 /* Peek at the next token. */
17671 token = cp_lexer_peek_token (parser->lexer);
17672 if (token->type != CPP_CLOSE_PAREN)
17673 /* Parse the attribute-list. */
17674 attribute_list = cp_parser_attribute_list (parser);
17675 else
17676 /* If the next token is a `)', then there is no attribute
17677 list. */
17678 attribute_list = NULL;
17680 /* Look for the two `)' tokens. */
17681 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17682 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17684 /* Add these new attributes to the list. */
17685 attributes = chainon (attributes, attribute_list);
17688 return attributes;
17691 /* Parse an attribute-list.
17693 attribute-list:
17694 attribute
17695 attribute-list , attribute
17697 attribute:
17698 identifier
17699 identifier ( identifier )
17700 identifier ( identifier , expression-list )
17701 identifier ( expression-list )
17703 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
17704 to an attribute. The TREE_PURPOSE of each node is the identifier
17705 indicating which attribute is in use. The TREE_VALUE represents
17706 the arguments, if any. */
17708 static tree
17709 cp_parser_attribute_list (cp_parser* parser)
17711 tree attribute_list = NULL_TREE;
17712 bool save_translate_strings_p = parser->translate_strings_p;
17714 parser->translate_strings_p = false;
17715 while (true)
17717 cp_token *token;
17718 tree identifier;
17719 tree attribute;
17721 /* Look for the identifier. We also allow keywords here; for
17722 example `__attribute__ ((const))' is legal. */
17723 token = cp_lexer_peek_token (parser->lexer);
17724 if (token->type == CPP_NAME
17725 || token->type == CPP_KEYWORD)
17727 tree arguments = NULL_TREE;
17729 /* Consume the token. */
17730 token = cp_lexer_consume_token (parser->lexer);
17732 /* Save away the identifier that indicates which attribute
17733 this is. */
17734 identifier = (token->type == CPP_KEYWORD)
17735 /* For keywords, use the canonical spelling, not the
17736 parsed identifier. */
17737 ? ridpointers[(int) token->keyword]
17738 : token->u.value;
17740 attribute = build_tree_list (identifier, NULL_TREE);
17742 /* Peek at the next token. */
17743 token = cp_lexer_peek_token (parser->lexer);
17744 /* If it's an `(', then parse the attribute arguments. */
17745 if (token->type == CPP_OPEN_PAREN)
17747 VEC(tree,gc) *vec;
17748 vec = cp_parser_parenthesized_expression_list
17749 (parser, true, /*cast_p=*/false,
17750 /*allow_expansion_p=*/false,
17751 /*non_constant_p=*/NULL);
17752 if (vec == NULL)
17753 arguments = error_mark_node;
17754 else
17756 arguments = build_tree_list_vec (vec);
17757 release_tree_vector (vec);
17759 /* Save the arguments away. */
17760 TREE_VALUE (attribute) = arguments;
17763 if (arguments != error_mark_node)
17765 /* Add this attribute to the list. */
17766 TREE_CHAIN (attribute) = attribute_list;
17767 attribute_list = attribute;
17770 token = cp_lexer_peek_token (parser->lexer);
17772 /* Now, look for more attributes. If the next token isn't a
17773 `,', we're done. */
17774 if (token->type != CPP_COMMA)
17775 break;
17777 /* Consume the comma and keep going. */
17778 cp_lexer_consume_token (parser->lexer);
17780 parser->translate_strings_p = save_translate_strings_p;
17782 /* We built up the list in reverse order. */
17783 return nreverse (attribute_list);
17786 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
17787 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
17788 current value of the PEDANTIC flag, regardless of whether or not
17789 the `__extension__' keyword is present. The caller is responsible
17790 for restoring the value of the PEDANTIC flag. */
17792 static bool
17793 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
17795 /* Save the old value of the PEDANTIC flag. */
17796 *saved_pedantic = pedantic;
17798 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
17800 /* Consume the `__extension__' token. */
17801 cp_lexer_consume_token (parser->lexer);
17802 /* We're not being pedantic while the `__extension__' keyword is
17803 in effect. */
17804 pedantic = 0;
17806 return true;
17809 return false;
17812 /* Parse a label declaration.
17814 label-declaration:
17815 __label__ label-declarator-seq ;
17817 label-declarator-seq:
17818 identifier , label-declarator-seq
17819 identifier */
17821 static void
17822 cp_parser_label_declaration (cp_parser* parser)
17824 /* Look for the `__label__' keyword. */
17825 cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
17827 while (true)
17829 tree identifier;
17831 /* Look for an identifier. */
17832 identifier = cp_parser_identifier (parser);
17833 /* If we failed, stop. */
17834 if (identifier == error_mark_node)
17835 break;
17836 /* Declare it as a label. */
17837 finish_label_decl (identifier);
17838 /* If the next token is a `;', stop. */
17839 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17840 break;
17841 /* Look for the `,' separating the label declarations. */
17842 cp_parser_require (parser, CPP_COMMA, "%<,%>");
17845 /* Look for the final `;'. */
17846 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
17849 /* Support Functions */
17851 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
17852 NAME should have one of the representations used for an
17853 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
17854 is returned. If PARSER->SCOPE is a dependent type, then a
17855 SCOPE_REF is returned.
17857 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
17858 returned; the name was already resolved when the TEMPLATE_ID_EXPR
17859 was formed. Abstractly, such entities should not be passed to this
17860 function, because they do not need to be looked up, but it is
17861 simpler to check for this special case here, rather than at the
17862 call-sites.
17864 In cases not explicitly covered above, this function returns a
17865 DECL, OVERLOAD, or baselink representing the result of the lookup.
17866 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
17867 is returned.
17869 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
17870 (e.g., "struct") that was used. In that case bindings that do not
17871 refer to types are ignored.
17873 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
17874 ignored.
17876 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
17877 are ignored.
17879 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
17880 types.
17882 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
17883 TREE_LIST of candidates if name-lookup results in an ambiguity, and
17884 NULL_TREE otherwise. */
17886 static tree
17887 cp_parser_lookup_name (cp_parser *parser, tree name,
17888 enum tag_types tag_type,
17889 bool is_template,
17890 bool is_namespace,
17891 bool check_dependency,
17892 tree *ambiguous_decls,
17893 location_t name_location)
17895 int flags = 0;
17896 tree decl;
17897 tree object_type = parser->context->object_type;
17899 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17900 flags |= LOOKUP_COMPLAIN;
17902 /* Assume that the lookup will be unambiguous. */
17903 if (ambiguous_decls)
17904 *ambiguous_decls = NULL_TREE;
17906 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
17907 no longer valid. Note that if we are parsing tentatively, and
17908 the parse fails, OBJECT_TYPE will be automatically restored. */
17909 parser->context->object_type = NULL_TREE;
17911 if (name == error_mark_node)
17912 return error_mark_node;
17914 /* A template-id has already been resolved; there is no lookup to
17915 do. */
17916 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
17917 return name;
17918 if (BASELINK_P (name))
17920 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
17921 == TEMPLATE_ID_EXPR);
17922 return name;
17925 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
17926 it should already have been checked to make sure that the name
17927 used matches the type being destroyed. */
17928 if (TREE_CODE (name) == BIT_NOT_EXPR)
17930 tree type;
17932 /* Figure out to which type this destructor applies. */
17933 if (parser->scope)
17934 type = parser->scope;
17935 else if (object_type)
17936 type = object_type;
17937 else
17938 type = current_class_type;
17939 /* If that's not a class type, there is no destructor. */
17940 if (!type || !CLASS_TYPE_P (type))
17941 return error_mark_node;
17942 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
17943 lazily_declare_fn (sfk_destructor, type);
17944 if (!CLASSTYPE_DESTRUCTORS (type))
17945 return error_mark_node;
17946 /* If it was a class type, return the destructor. */
17947 return CLASSTYPE_DESTRUCTORS (type);
17950 /* By this point, the NAME should be an ordinary identifier. If
17951 the id-expression was a qualified name, the qualifying scope is
17952 stored in PARSER->SCOPE at this point. */
17953 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
17955 /* Perform the lookup. */
17956 if (parser->scope)
17958 bool dependent_p;
17960 if (parser->scope == error_mark_node)
17961 return error_mark_node;
17963 /* If the SCOPE is dependent, the lookup must be deferred until
17964 the template is instantiated -- unless we are explicitly
17965 looking up names in uninstantiated templates. Even then, we
17966 cannot look up the name if the scope is not a class type; it
17967 might, for example, be a template type parameter. */
17968 dependent_p = (TYPE_P (parser->scope)
17969 && dependent_scope_p (parser->scope));
17970 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
17971 && dependent_p)
17972 /* Defer lookup. */
17973 decl = error_mark_node;
17974 else
17976 tree pushed_scope = NULL_TREE;
17978 /* If PARSER->SCOPE is a dependent type, then it must be a
17979 class type, and we must not be checking dependencies;
17980 otherwise, we would have processed this lookup above. So
17981 that PARSER->SCOPE is not considered a dependent base by
17982 lookup_member, we must enter the scope here. */
17983 if (dependent_p)
17984 pushed_scope = push_scope (parser->scope);
17986 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
17987 lookup result and the nested-name-specifier nominates a class C:
17988 * if the name specified after the nested-name-specifier, when
17989 looked up in C, is the injected-class-name of C (Clause 9), or
17990 * if the name specified after the nested-name-specifier is the
17991 same as the identifier or the simple-template-id's template-
17992 name in the last component of the nested-name-specifier,
17993 the name is instead considered to name the constructor of
17994 class C. [ Note: for example, the constructor is not an
17995 acceptable lookup result in an elaborated-type-specifier so
17996 the constructor would not be used in place of the
17997 injected-class-name. --end note ] Such a constructor name
17998 shall be used only in the declarator-id of a declaration that
17999 names a constructor or in a using-declaration. */
18000 if (tag_type == none_type
18001 && CLASS_TYPE_P (parser->scope)
18002 && constructor_name_p (name, parser->scope))
18003 name = ctor_identifier;
18005 /* If the PARSER->SCOPE is a template specialization, it
18006 may be instantiated during name lookup. In that case,
18007 errors may be issued. Even if we rollback the current
18008 tentative parse, those errors are valid. */
18009 decl = lookup_qualified_name (parser->scope, name,
18010 tag_type != none_type,
18011 /*complain=*/true);
18013 /* If we have a single function from a using decl, pull it out. */
18014 if (TREE_CODE (decl) == OVERLOAD
18015 && !really_overloaded_fn (decl))
18016 decl = OVL_FUNCTION (decl);
18018 if (pushed_scope)
18019 pop_scope (pushed_scope);
18022 /* If the scope is a dependent type and either we deferred lookup or
18023 we did lookup but didn't find the name, rememeber the name. */
18024 if (decl == error_mark_node && TYPE_P (parser->scope)
18025 && dependent_type_p (parser->scope))
18027 if (tag_type)
18029 tree type;
18031 /* The resolution to Core Issue 180 says that `struct
18032 A::B' should be considered a type-name, even if `A'
18033 is dependent. */
18034 type = make_typename_type (parser->scope, name, tag_type,
18035 /*complain=*/tf_error);
18036 decl = TYPE_NAME (type);
18038 else if (is_template
18039 && (cp_parser_next_token_ends_template_argument_p (parser)
18040 || cp_lexer_next_token_is (parser->lexer,
18041 CPP_CLOSE_PAREN)))
18042 decl = make_unbound_class_template (parser->scope,
18043 name, NULL_TREE,
18044 /*complain=*/tf_error);
18045 else
18046 decl = build_qualified_name (/*type=*/NULL_TREE,
18047 parser->scope, name,
18048 is_template);
18050 parser->qualifying_scope = parser->scope;
18051 parser->object_scope = NULL_TREE;
18053 else if (object_type)
18055 tree object_decl = NULL_TREE;
18056 /* Look up the name in the scope of the OBJECT_TYPE, unless the
18057 OBJECT_TYPE is not a class. */
18058 if (CLASS_TYPE_P (object_type))
18059 /* If the OBJECT_TYPE is a template specialization, it may
18060 be instantiated during name lookup. In that case, errors
18061 may be issued. Even if we rollback the current tentative
18062 parse, those errors are valid. */
18063 object_decl = lookup_member (object_type,
18064 name,
18065 /*protect=*/0,
18066 tag_type != none_type);
18067 /* Look it up in the enclosing context, too. */
18068 decl = lookup_name_real (name, tag_type != none_type,
18069 /*nonclass=*/0,
18070 /*block_p=*/true, is_namespace, flags);
18071 parser->object_scope = object_type;
18072 parser->qualifying_scope = NULL_TREE;
18073 if (object_decl)
18074 decl = object_decl;
18076 else
18078 decl = lookup_name_real (name, tag_type != none_type,
18079 /*nonclass=*/0,
18080 /*block_p=*/true, is_namespace, flags);
18081 parser->qualifying_scope = NULL_TREE;
18082 parser->object_scope = NULL_TREE;
18085 /* If the lookup failed, let our caller know. */
18086 if (!decl || decl == error_mark_node)
18087 return error_mark_node;
18089 /* Pull out the template from an injected-class-name (or multiple). */
18090 if (is_template)
18091 decl = maybe_get_template_decl_from_type_decl (decl);
18093 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
18094 if (TREE_CODE (decl) == TREE_LIST)
18096 if (ambiguous_decls)
18097 *ambiguous_decls = decl;
18098 /* The error message we have to print is too complicated for
18099 cp_parser_error, so we incorporate its actions directly. */
18100 if (!cp_parser_simulate_error (parser))
18102 error_at (name_location, "reference to %qD is ambiguous",
18103 name);
18104 print_candidates (decl);
18106 return error_mark_node;
18109 gcc_assert (DECL_P (decl)
18110 || TREE_CODE (decl) == OVERLOAD
18111 || TREE_CODE (decl) == SCOPE_REF
18112 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
18113 || BASELINK_P (decl));
18115 /* If we have resolved the name of a member declaration, check to
18116 see if the declaration is accessible. When the name resolves to
18117 set of overloaded functions, accessibility is checked when
18118 overload resolution is done.
18120 During an explicit instantiation, access is not checked at all,
18121 as per [temp.explicit]. */
18122 if (DECL_P (decl))
18123 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
18125 return decl;
18128 /* Like cp_parser_lookup_name, but for use in the typical case where
18129 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
18130 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
18132 static tree
18133 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
18135 return cp_parser_lookup_name (parser, name,
18136 none_type,
18137 /*is_template=*/false,
18138 /*is_namespace=*/false,
18139 /*check_dependency=*/true,
18140 /*ambiguous_decls=*/NULL,
18141 location);
18144 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
18145 the current context, return the TYPE_DECL. If TAG_NAME_P is
18146 true, the DECL indicates the class being defined in a class-head,
18147 or declared in an elaborated-type-specifier.
18149 Otherwise, return DECL. */
18151 static tree
18152 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
18154 /* If the TEMPLATE_DECL is being declared as part of a class-head,
18155 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
18157 struct A {
18158 template <typename T> struct B;
18161 template <typename T> struct A::B {};
18163 Similarly, in an elaborated-type-specifier:
18165 namespace N { struct X{}; }
18167 struct A {
18168 template <typename T> friend struct N::X;
18171 However, if the DECL refers to a class type, and we are in
18172 the scope of the class, then the name lookup automatically
18173 finds the TYPE_DECL created by build_self_reference rather
18174 than a TEMPLATE_DECL. For example, in:
18176 template <class T> struct S {
18177 S s;
18180 there is no need to handle such case. */
18182 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
18183 return DECL_TEMPLATE_RESULT (decl);
18185 return decl;
18188 /* If too many, or too few, template-parameter lists apply to the
18189 declarator, issue an error message. Returns TRUE if all went well,
18190 and FALSE otherwise. */
18192 static bool
18193 cp_parser_check_declarator_template_parameters (cp_parser* parser,
18194 cp_declarator *declarator,
18195 location_t declarator_location)
18197 unsigned num_templates;
18199 /* We haven't seen any classes that involve template parameters yet. */
18200 num_templates = 0;
18202 switch (declarator->kind)
18204 case cdk_id:
18205 if (declarator->u.id.qualifying_scope)
18207 tree scope;
18209 scope = declarator->u.id.qualifying_scope;
18211 while (scope && CLASS_TYPE_P (scope))
18213 /* You're supposed to have one `template <...>'
18214 for every template class, but you don't need one
18215 for a full specialization. For example:
18217 template <class T> struct S{};
18218 template <> struct S<int> { void f(); };
18219 void S<int>::f () {}
18221 is correct; there shouldn't be a `template <>' for
18222 the definition of `S<int>::f'. */
18223 if (!CLASSTYPE_TEMPLATE_INFO (scope))
18224 /* If SCOPE does not have template information of any
18225 kind, then it is not a template, nor is it nested
18226 within a template. */
18227 break;
18228 if (explicit_class_specialization_p (scope))
18229 break;
18230 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
18231 ++num_templates;
18233 scope = TYPE_CONTEXT (scope);
18236 else if (TREE_CODE (declarator->u.id.unqualified_name)
18237 == TEMPLATE_ID_EXPR)
18238 /* If the DECLARATOR has the form `X<y>' then it uses one
18239 additional level of template parameters. */
18240 ++num_templates;
18242 return cp_parser_check_template_parameters
18243 (parser, num_templates, declarator_location, declarator);
18246 case cdk_function:
18247 case cdk_array:
18248 case cdk_pointer:
18249 case cdk_reference:
18250 case cdk_ptrmem:
18251 return (cp_parser_check_declarator_template_parameters
18252 (parser, declarator->declarator, declarator_location));
18254 case cdk_error:
18255 return true;
18257 default:
18258 gcc_unreachable ();
18260 return false;
18263 /* NUM_TEMPLATES were used in the current declaration. If that is
18264 invalid, return FALSE and issue an error messages. Otherwise,
18265 return TRUE. If DECLARATOR is non-NULL, then we are checking a
18266 declarator and we can print more accurate diagnostics. */
18268 static bool
18269 cp_parser_check_template_parameters (cp_parser* parser,
18270 unsigned num_templates,
18271 location_t location,
18272 cp_declarator *declarator)
18274 /* If there are the same number of template classes and parameter
18275 lists, that's OK. */
18276 if (parser->num_template_parameter_lists == num_templates)
18277 return true;
18278 /* If there are more, but only one more, then we are referring to a
18279 member template. That's OK too. */
18280 if (parser->num_template_parameter_lists == num_templates + 1)
18281 return true;
18282 /* If there are more template classes than parameter lists, we have
18283 something like:
18285 template <class T> void S<T>::R<T>::f (); */
18286 if (parser->num_template_parameter_lists < num_templates)
18288 if (declarator && !current_function_decl)
18289 error_at (location, "specializing member %<%T::%E%> "
18290 "requires %<template<>%> syntax",
18291 declarator->u.id.qualifying_scope,
18292 declarator->u.id.unqualified_name);
18293 else if (declarator)
18294 error_at (location, "invalid declaration of %<%T::%E%>",
18295 declarator->u.id.qualifying_scope,
18296 declarator->u.id.unqualified_name);
18297 else
18298 error_at (location, "too few template-parameter-lists");
18299 return false;
18301 /* Otherwise, there are too many template parameter lists. We have
18302 something like:
18304 template <class T> template <class U> void S::f(); */
18305 error_at (location, "too many template-parameter-lists");
18306 return false;
18309 /* Parse an optional `::' token indicating that the following name is
18310 from the global namespace. If so, PARSER->SCOPE is set to the
18311 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
18312 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
18313 Returns the new value of PARSER->SCOPE, if the `::' token is
18314 present, and NULL_TREE otherwise. */
18316 static tree
18317 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
18319 cp_token *token;
18321 /* Peek at the next token. */
18322 token = cp_lexer_peek_token (parser->lexer);
18323 /* If we're looking at a `::' token then we're starting from the
18324 global namespace, not our current location. */
18325 if (token->type == CPP_SCOPE)
18327 /* Consume the `::' token. */
18328 cp_lexer_consume_token (parser->lexer);
18329 /* Set the SCOPE so that we know where to start the lookup. */
18330 parser->scope = global_namespace;
18331 parser->qualifying_scope = global_namespace;
18332 parser->object_scope = NULL_TREE;
18334 return parser->scope;
18336 else if (!current_scope_valid_p)
18338 parser->scope = NULL_TREE;
18339 parser->qualifying_scope = NULL_TREE;
18340 parser->object_scope = NULL_TREE;
18343 return NULL_TREE;
18346 /* Returns TRUE if the upcoming token sequence is the start of a
18347 constructor declarator. If FRIEND_P is true, the declarator is
18348 preceded by the `friend' specifier. */
18350 static bool
18351 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
18353 bool constructor_p;
18354 tree nested_name_specifier;
18355 cp_token *next_token;
18357 /* The common case is that this is not a constructor declarator, so
18358 try to avoid doing lots of work if at all possible. It's not
18359 valid declare a constructor at function scope. */
18360 if (parser->in_function_body)
18361 return false;
18362 /* And only certain tokens can begin a constructor declarator. */
18363 next_token = cp_lexer_peek_token (parser->lexer);
18364 if (next_token->type != CPP_NAME
18365 && next_token->type != CPP_SCOPE
18366 && next_token->type != CPP_NESTED_NAME_SPECIFIER
18367 && next_token->type != CPP_TEMPLATE_ID)
18368 return false;
18370 /* Parse tentatively; we are going to roll back all of the tokens
18371 consumed here. */
18372 cp_parser_parse_tentatively (parser);
18373 /* Assume that we are looking at a constructor declarator. */
18374 constructor_p = true;
18376 /* Look for the optional `::' operator. */
18377 cp_parser_global_scope_opt (parser,
18378 /*current_scope_valid_p=*/false);
18379 /* Look for the nested-name-specifier. */
18380 nested_name_specifier
18381 = (cp_parser_nested_name_specifier_opt (parser,
18382 /*typename_keyword_p=*/false,
18383 /*check_dependency_p=*/false,
18384 /*type_p=*/false,
18385 /*is_declaration=*/false));
18386 /* Outside of a class-specifier, there must be a
18387 nested-name-specifier. */
18388 if (!nested_name_specifier &&
18389 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
18390 || friend_p))
18391 constructor_p = false;
18392 else if (nested_name_specifier == error_mark_node)
18393 constructor_p = false;
18395 /* If we have a class scope, this is easy; DR 147 says that S::S always
18396 names the constructor, and no other qualified name could. */
18397 if (constructor_p && nested_name_specifier
18398 && TYPE_P (nested_name_specifier))
18400 tree id = cp_parser_unqualified_id (parser,
18401 /*template_keyword_p=*/false,
18402 /*check_dependency_p=*/false,
18403 /*declarator_p=*/true,
18404 /*optional_p=*/false);
18405 if (is_overloaded_fn (id))
18406 id = DECL_NAME (get_first_fn (id));
18407 if (!constructor_name_p (id, nested_name_specifier))
18408 constructor_p = false;
18410 /* If we still think that this might be a constructor-declarator,
18411 look for a class-name. */
18412 else if (constructor_p)
18414 /* If we have:
18416 template <typename T> struct S {
18417 S();
18420 we must recognize that the nested `S' names a class. */
18421 tree type_decl;
18422 type_decl = cp_parser_class_name (parser,
18423 /*typename_keyword_p=*/false,
18424 /*template_keyword_p=*/false,
18425 none_type,
18426 /*check_dependency_p=*/false,
18427 /*class_head_p=*/false,
18428 /*is_declaration=*/false);
18429 /* If there was no class-name, then this is not a constructor. */
18430 constructor_p = !cp_parser_error_occurred (parser);
18432 /* If we're still considering a constructor, we have to see a `(',
18433 to begin the parameter-declaration-clause, followed by either a
18434 `)', an `...', or a decl-specifier. We need to check for a
18435 type-specifier to avoid being fooled into thinking that:
18437 S (f) (int);
18439 is a constructor. (It is actually a function named `f' that
18440 takes one parameter (of type `int') and returns a value of type
18441 `S'. */
18442 if (constructor_p
18443 && !cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
18444 constructor_p = false;
18446 if (constructor_p
18447 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
18448 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
18449 /* A parameter declaration begins with a decl-specifier,
18450 which is either the "attribute" keyword, a storage class
18451 specifier, or (usually) a type-specifier. */
18452 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
18454 tree type;
18455 tree pushed_scope = NULL_TREE;
18456 unsigned saved_num_template_parameter_lists;
18458 /* Names appearing in the type-specifier should be looked up
18459 in the scope of the class. */
18460 if (current_class_type)
18461 type = NULL_TREE;
18462 else
18464 type = TREE_TYPE (type_decl);
18465 if (TREE_CODE (type) == TYPENAME_TYPE)
18467 type = resolve_typename_type (type,
18468 /*only_current_p=*/false);
18469 if (TREE_CODE (type) == TYPENAME_TYPE)
18471 cp_parser_abort_tentative_parse (parser);
18472 return false;
18475 pushed_scope = push_scope (type);
18478 /* Inside the constructor parameter list, surrounding
18479 template-parameter-lists do not apply. */
18480 saved_num_template_parameter_lists
18481 = parser->num_template_parameter_lists;
18482 parser->num_template_parameter_lists = 0;
18484 /* Look for the type-specifier. */
18485 cp_parser_type_specifier (parser,
18486 CP_PARSER_FLAGS_NONE,
18487 /*decl_specs=*/NULL,
18488 /*is_declarator=*/true,
18489 /*declares_class_or_enum=*/NULL,
18490 /*is_cv_qualifier=*/NULL);
18492 parser->num_template_parameter_lists
18493 = saved_num_template_parameter_lists;
18495 /* Leave the scope of the class. */
18496 if (pushed_scope)
18497 pop_scope (pushed_scope);
18499 constructor_p = !cp_parser_error_occurred (parser);
18503 /* We did not really want to consume any tokens. */
18504 cp_parser_abort_tentative_parse (parser);
18506 return constructor_p;
18509 /* Parse the definition of the function given by the DECL_SPECIFIERS,
18510 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
18511 they must be performed once we are in the scope of the function.
18513 Returns the function defined. */
18515 static tree
18516 cp_parser_function_definition_from_specifiers_and_declarator
18517 (cp_parser* parser,
18518 cp_decl_specifier_seq *decl_specifiers,
18519 tree attributes,
18520 const cp_declarator *declarator)
18522 tree fn;
18523 bool success_p;
18525 /* Begin the function-definition. */
18526 success_p = start_function (decl_specifiers, declarator, attributes);
18528 /* The things we're about to see are not directly qualified by any
18529 template headers we've seen thus far. */
18530 reset_specialization ();
18532 /* If there were names looked up in the decl-specifier-seq that we
18533 did not check, check them now. We must wait until we are in the
18534 scope of the function to perform the checks, since the function
18535 might be a friend. */
18536 perform_deferred_access_checks ();
18538 if (!success_p)
18540 /* Skip the entire function. */
18541 cp_parser_skip_to_end_of_block_or_statement (parser);
18542 fn = error_mark_node;
18544 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
18546 /* Seen already, skip it. An error message has already been output. */
18547 cp_parser_skip_to_end_of_block_or_statement (parser);
18548 fn = current_function_decl;
18549 current_function_decl = NULL_TREE;
18550 /* If this is a function from a class, pop the nested class. */
18551 if (current_class_name)
18552 pop_nested_class ();
18554 else
18555 fn = cp_parser_function_definition_after_declarator (parser,
18556 /*inline_p=*/false);
18558 return fn;
18561 /* Parse the part of a function-definition that follows the
18562 declarator. INLINE_P is TRUE iff this function is an inline
18563 function defined within a class-specifier.
18565 Returns the function defined. */
18567 static tree
18568 cp_parser_function_definition_after_declarator (cp_parser* parser,
18569 bool inline_p)
18571 tree fn;
18572 bool ctor_initializer_p = false;
18573 bool saved_in_unbraced_linkage_specification_p;
18574 bool saved_in_function_body;
18575 unsigned saved_num_template_parameter_lists;
18576 cp_token *token;
18578 saved_in_function_body = parser->in_function_body;
18579 parser->in_function_body = true;
18580 /* If the next token is `return', then the code may be trying to
18581 make use of the "named return value" extension that G++ used to
18582 support. */
18583 token = cp_lexer_peek_token (parser->lexer);
18584 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
18586 /* Consume the `return' keyword. */
18587 cp_lexer_consume_token (parser->lexer);
18588 /* Look for the identifier that indicates what value is to be
18589 returned. */
18590 cp_parser_identifier (parser);
18591 /* Issue an error message. */
18592 error_at (token->location,
18593 "named return values are no longer supported");
18594 /* Skip tokens until we reach the start of the function body. */
18595 while (true)
18597 cp_token *token = cp_lexer_peek_token (parser->lexer);
18598 if (token->type == CPP_OPEN_BRACE
18599 || token->type == CPP_EOF
18600 || token->type == CPP_PRAGMA_EOL)
18601 break;
18602 cp_lexer_consume_token (parser->lexer);
18605 /* The `extern' in `extern "C" void f () { ... }' does not apply to
18606 anything declared inside `f'. */
18607 saved_in_unbraced_linkage_specification_p
18608 = parser->in_unbraced_linkage_specification_p;
18609 parser->in_unbraced_linkage_specification_p = false;
18610 /* Inside the function, surrounding template-parameter-lists do not
18611 apply. */
18612 saved_num_template_parameter_lists
18613 = parser->num_template_parameter_lists;
18614 parser->num_template_parameter_lists = 0;
18616 start_lambda_scope (current_function_decl);
18618 /* If the next token is `try', then we are looking at a
18619 function-try-block. */
18620 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
18621 ctor_initializer_p = cp_parser_function_try_block (parser);
18622 /* A function-try-block includes the function-body, so we only do
18623 this next part if we're not processing a function-try-block. */
18624 else
18625 ctor_initializer_p
18626 = cp_parser_ctor_initializer_opt_and_function_body (parser);
18628 finish_lambda_scope ();
18630 /* Finish the function. */
18631 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
18632 (inline_p ? 2 : 0));
18633 /* Generate code for it, if necessary. */
18634 expand_or_defer_fn (fn);
18635 /* Restore the saved values. */
18636 parser->in_unbraced_linkage_specification_p
18637 = saved_in_unbraced_linkage_specification_p;
18638 parser->num_template_parameter_lists
18639 = saved_num_template_parameter_lists;
18640 parser->in_function_body = saved_in_function_body;
18642 return fn;
18645 /* Parse a template-declaration, assuming that the `export' (and
18646 `extern') keywords, if present, has already been scanned. MEMBER_P
18647 is as for cp_parser_template_declaration. */
18649 static void
18650 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
18652 tree decl = NULL_TREE;
18653 VEC (deferred_access_check,gc) *checks;
18654 tree parameter_list;
18655 bool friend_p = false;
18656 bool need_lang_pop;
18657 cp_token *token;
18659 /* Look for the `template' keyword. */
18660 token = cp_lexer_peek_token (parser->lexer);
18661 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
18662 return;
18664 /* And the `<'. */
18665 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
18666 return;
18667 if (at_class_scope_p () && current_function_decl)
18669 /* 14.5.2.2 [temp.mem]
18671 A local class shall not have member templates. */
18672 error_at (token->location,
18673 "invalid declaration of member template in local class");
18674 cp_parser_skip_to_end_of_block_or_statement (parser);
18675 return;
18677 /* [temp]
18679 A template ... shall not have C linkage. */
18680 if (current_lang_name == lang_name_c)
18682 error_at (token->location, "template with C linkage");
18683 /* Give it C++ linkage to avoid confusing other parts of the
18684 front end. */
18685 push_lang_context (lang_name_cplusplus);
18686 need_lang_pop = true;
18688 else
18689 need_lang_pop = false;
18691 /* We cannot perform access checks on the template parameter
18692 declarations until we know what is being declared, just as we
18693 cannot check the decl-specifier list. */
18694 push_deferring_access_checks (dk_deferred);
18696 /* If the next token is `>', then we have an invalid
18697 specialization. Rather than complain about an invalid template
18698 parameter, issue an error message here. */
18699 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
18701 cp_parser_error (parser, "invalid explicit specialization");
18702 begin_specialization ();
18703 parameter_list = NULL_TREE;
18705 else
18706 /* Parse the template parameters. */
18707 parameter_list = cp_parser_template_parameter_list (parser);
18709 /* Get the deferred access checks from the parameter list. These
18710 will be checked once we know what is being declared, as for a
18711 member template the checks must be performed in the scope of the
18712 class containing the member. */
18713 checks = get_deferred_access_checks ();
18715 /* Look for the `>'. */
18716 cp_parser_skip_to_end_of_template_parameter_list (parser);
18717 /* We just processed one more parameter list. */
18718 ++parser->num_template_parameter_lists;
18719 /* If the next token is `template', there are more template
18720 parameters. */
18721 if (cp_lexer_next_token_is_keyword (parser->lexer,
18722 RID_TEMPLATE))
18723 cp_parser_template_declaration_after_export (parser, member_p);
18724 else
18726 /* There are no access checks when parsing a template, as we do not
18727 know if a specialization will be a friend. */
18728 push_deferring_access_checks (dk_no_check);
18729 token = cp_lexer_peek_token (parser->lexer);
18730 decl = cp_parser_single_declaration (parser,
18731 checks,
18732 member_p,
18733 /*explicit_specialization_p=*/false,
18734 &friend_p);
18735 pop_deferring_access_checks ();
18737 /* If this is a member template declaration, let the front
18738 end know. */
18739 if (member_p && !friend_p && decl)
18741 if (TREE_CODE (decl) == TYPE_DECL)
18742 cp_parser_check_access_in_redeclaration (decl, token->location);
18744 decl = finish_member_template_decl (decl);
18746 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
18747 make_friend_class (current_class_type, TREE_TYPE (decl),
18748 /*complain=*/true);
18750 /* We are done with the current parameter list. */
18751 --parser->num_template_parameter_lists;
18753 pop_deferring_access_checks ();
18755 /* Finish up. */
18756 finish_template_decl (parameter_list);
18758 /* Register member declarations. */
18759 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
18760 finish_member_declaration (decl);
18761 /* For the erroneous case of a template with C linkage, we pushed an
18762 implicit C++ linkage scope; exit that scope now. */
18763 if (need_lang_pop)
18764 pop_lang_context ();
18765 /* If DECL is a function template, we must return to parse it later.
18766 (Even though there is no definition, there might be default
18767 arguments that need handling.) */
18768 if (member_p && decl
18769 && (TREE_CODE (decl) == FUNCTION_DECL
18770 || DECL_FUNCTION_TEMPLATE_P (decl)))
18771 TREE_VALUE (parser->unparsed_functions_queues)
18772 = tree_cons (NULL_TREE, decl,
18773 TREE_VALUE (parser->unparsed_functions_queues));
18776 /* Perform the deferred access checks from a template-parameter-list.
18777 CHECKS is a TREE_LIST of access checks, as returned by
18778 get_deferred_access_checks. */
18780 static void
18781 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
18783 ++processing_template_parmlist;
18784 perform_access_checks (checks);
18785 --processing_template_parmlist;
18788 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
18789 `function-definition' sequence. MEMBER_P is true, this declaration
18790 appears in a class scope.
18792 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
18793 *FRIEND_P is set to TRUE iff the declaration is a friend. */
18795 static tree
18796 cp_parser_single_declaration (cp_parser* parser,
18797 VEC (deferred_access_check,gc)* checks,
18798 bool member_p,
18799 bool explicit_specialization_p,
18800 bool* friend_p)
18802 int declares_class_or_enum;
18803 tree decl = NULL_TREE;
18804 cp_decl_specifier_seq decl_specifiers;
18805 bool function_definition_p = false;
18806 cp_token *decl_spec_token_start;
18808 /* This function is only used when processing a template
18809 declaration. */
18810 gcc_assert (innermost_scope_kind () == sk_template_parms
18811 || innermost_scope_kind () == sk_template_spec);
18813 /* Defer access checks until we know what is being declared. */
18814 push_deferring_access_checks (dk_deferred);
18816 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
18817 alternative. */
18818 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
18819 cp_parser_decl_specifier_seq (parser,
18820 CP_PARSER_FLAGS_OPTIONAL,
18821 &decl_specifiers,
18822 &declares_class_or_enum);
18823 if (friend_p)
18824 *friend_p = cp_parser_friend_p (&decl_specifiers);
18826 /* There are no template typedefs. */
18827 if (decl_specifiers.specs[(int) ds_typedef])
18829 error_at (decl_spec_token_start->location,
18830 "template declaration of %<typedef%>");
18831 decl = error_mark_node;
18834 /* Gather up the access checks that occurred the
18835 decl-specifier-seq. */
18836 stop_deferring_access_checks ();
18838 /* Check for the declaration of a template class. */
18839 if (declares_class_or_enum)
18841 if (cp_parser_declares_only_class_p (parser))
18843 decl = shadow_tag (&decl_specifiers);
18845 /* In this case:
18847 struct C {
18848 friend template <typename T> struct A<T>::B;
18851 A<T>::B will be represented by a TYPENAME_TYPE, and
18852 therefore not recognized by shadow_tag. */
18853 if (friend_p && *friend_p
18854 && !decl
18855 && decl_specifiers.type
18856 && TYPE_P (decl_specifiers.type))
18857 decl = decl_specifiers.type;
18859 if (decl && decl != error_mark_node)
18860 decl = TYPE_NAME (decl);
18861 else
18862 decl = error_mark_node;
18864 /* Perform access checks for template parameters. */
18865 cp_parser_perform_template_parameter_access_checks (checks);
18869 /* Complain about missing 'typename' or other invalid type names. */
18870 if (!decl_specifiers.any_type_specifiers_p)
18871 cp_parser_parse_and_diagnose_invalid_type_name (parser);
18873 /* If it's not a template class, try for a template function. If
18874 the next token is a `;', then this declaration does not declare
18875 anything. But, if there were errors in the decl-specifiers, then
18876 the error might well have come from an attempted class-specifier.
18877 In that case, there's no need to warn about a missing declarator. */
18878 if (!decl
18879 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
18880 || decl_specifiers.type != error_mark_node))
18882 decl = cp_parser_init_declarator (parser,
18883 &decl_specifiers,
18884 checks,
18885 /*function_definition_allowed_p=*/true,
18886 member_p,
18887 declares_class_or_enum,
18888 &function_definition_p);
18890 /* 7.1.1-1 [dcl.stc]
18892 A storage-class-specifier shall not be specified in an explicit
18893 specialization... */
18894 if (decl
18895 && explicit_specialization_p
18896 && decl_specifiers.storage_class != sc_none)
18898 error_at (decl_spec_token_start->location,
18899 "explicit template specialization cannot have a storage class");
18900 decl = error_mark_node;
18904 pop_deferring_access_checks ();
18906 /* Clear any current qualification; whatever comes next is the start
18907 of something new. */
18908 parser->scope = NULL_TREE;
18909 parser->qualifying_scope = NULL_TREE;
18910 parser->object_scope = NULL_TREE;
18911 /* Look for a trailing `;' after the declaration. */
18912 if (!function_definition_p
18913 && (decl == error_mark_node
18914 || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
18915 cp_parser_skip_to_end_of_block_or_statement (parser);
18917 return decl;
18920 /* Parse a cast-expression that is not the operand of a unary "&". */
18922 static tree
18923 cp_parser_simple_cast_expression (cp_parser *parser)
18925 return cp_parser_cast_expression (parser, /*address_p=*/false,
18926 /*cast_p=*/false, NULL);
18929 /* Parse a functional cast to TYPE. Returns an expression
18930 representing the cast. */
18932 static tree
18933 cp_parser_functional_cast (cp_parser* parser, tree type)
18935 VEC(tree,gc) *vec;
18936 tree expression_list;
18937 tree cast;
18938 bool nonconst_p;
18940 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18942 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
18943 expression_list = cp_parser_braced_list (parser, &nonconst_p);
18944 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
18945 if (TREE_CODE (type) == TYPE_DECL)
18946 type = TREE_TYPE (type);
18947 return finish_compound_literal (type, expression_list);
18951 vec = cp_parser_parenthesized_expression_list (parser, false,
18952 /*cast_p=*/true,
18953 /*allow_expansion_p=*/true,
18954 /*non_constant_p=*/NULL);
18955 if (vec == NULL)
18956 expression_list = error_mark_node;
18957 else
18959 expression_list = build_tree_list_vec (vec);
18960 release_tree_vector (vec);
18963 cast = build_functional_cast (type, expression_list,
18964 tf_warning_or_error);
18965 /* [expr.const]/1: In an integral constant expression "only type
18966 conversions to integral or enumeration type can be used". */
18967 if (TREE_CODE (type) == TYPE_DECL)
18968 type = TREE_TYPE (type);
18969 if (cast != error_mark_node
18970 && !cast_valid_in_integral_constant_expression_p (type)
18971 && (cp_parser_non_integral_constant_expression
18972 (parser, "a call to a constructor")))
18973 return error_mark_node;
18974 return cast;
18977 /* Save the tokens that make up the body of a member function defined
18978 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
18979 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
18980 specifiers applied to the declaration. Returns the FUNCTION_DECL
18981 for the member function. */
18983 static tree
18984 cp_parser_save_member_function_body (cp_parser* parser,
18985 cp_decl_specifier_seq *decl_specifiers,
18986 cp_declarator *declarator,
18987 tree attributes)
18989 cp_token *first;
18990 cp_token *last;
18991 tree fn;
18993 /* Create the FUNCTION_DECL. */
18994 fn = grokmethod (decl_specifiers, declarator, attributes);
18995 /* If something went badly wrong, bail out now. */
18996 if (fn == error_mark_node)
18998 /* If there's a function-body, skip it. */
18999 if (cp_parser_token_starts_function_definition_p
19000 (cp_lexer_peek_token (parser->lexer)))
19001 cp_parser_skip_to_end_of_block_or_statement (parser);
19002 return error_mark_node;
19005 /* Remember it, if there default args to post process. */
19006 cp_parser_save_default_args (parser, fn);
19008 /* Save away the tokens that make up the body of the
19009 function. */
19010 first = parser->lexer->next_token;
19011 /* We can have braced-init-list mem-initializers before the fn body. */
19012 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19014 cp_lexer_consume_token (parser->lexer);
19015 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
19016 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
19018 /* cache_group will stop after an un-nested { } pair, too. */
19019 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
19020 break;
19022 /* variadic mem-inits have ... after the ')'. */
19023 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19024 cp_lexer_consume_token (parser->lexer);
19027 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
19028 /* Handle function try blocks. */
19029 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
19030 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
19031 last = parser->lexer->next_token;
19033 /* Save away the inline definition; we will process it when the
19034 class is complete. */
19035 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
19036 DECL_PENDING_INLINE_P (fn) = 1;
19038 /* We need to know that this was defined in the class, so that
19039 friend templates are handled correctly. */
19040 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
19042 /* Add FN to the queue of functions to be parsed later. */
19043 TREE_VALUE (parser->unparsed_functions_queues)
19044 = tree_cons (NULL_TREE, fn,
19045 TREE_VALUE (parser->unparsed_functions_queues));
19047 return fn;
19050 /* Parse a template-argument-list, as well as the trailing ">" (but
19051 not the opening ">"). See cp_parser_template_argument_list for the
19052 return value. */
19054 static tree
19055 cp_parser_enclosed_template_argument_list (cp_parser* parser)
19057 tree arguments;
19058 tree saved_scope;
19059 tree saved_qualifying_scope;
19060 tree saved_object_scope;
19061 bool saved_greater_than_is_operator_p;
19062 int saved_unevaluated_operand;
19063 int saved_inhibit_evaluation_warnings;
19065 /* [temp.names]
19067 When parsing a template-id, the first non-nested `>' is taken as
19068 the end of the template-argument-list rather than a greater-than
19069 operator. */
19070 saved_greater_than_is_operator_p
19071 = parser->greater_than_is_operator_p;
19072 parser->greater_than_is_operator_p = false;
19073 /* Parsing the argument list may modify SCOPE, so we save it
19074 here. */
19075 saved_scope = parser->scope;
19076 saved_qualifying_scope = parser->qualifying_scope;
19077 saved_object_scope = parser->object_scope;
19078 /* We need to evaluate the template arguments, even though this
19079 template-id may be nested within a "sizeof". */
19080 saved_unevaluated_operand = cp_unevaluated_operand;
19081 cp_unevaluated_operand = 0;
19082 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
19083 c_inhibit_evaluation_warnings = 0;
19084 /* Parse the template-argument-list itself. */
19085 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
19086 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
19087 arguments = NULL_TREE;
19088 else
19089 arguments = cp_parser_template_argument_list (parser);
19090 /* Look for the `>' that ends the template-argument-list. If we find
19091 a '>>' instead, it's probably just a typo. */
19092 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
19094 if (cxx_dialect != cxx98)
19096 /* In C++0x, a `>>' in a template argument list or cast
19097 expression is considered to be two separate `>'
19098 tokens. So, change the current token to a `>', but don't
19099 consume it: it will be consumed later when the outer
19100 template argument list (or cast expression) is parsed.
19101 Note that this replacement of `>' for `>>' is necessary
19102 even if we are parsing tentatively: in the tentative
19103 case, after calling
19104 cp_parser_enclosed_template_argument_list we will always
19105 throw away all of the template arguments and the first
19106 closing `>', either because the template argument list
19107 was erroneous or because we are replacing those tokens
19108 with a CPP_TEMPLATE_ID token. The second `>' (which will
19109 not have been thrown away) is needed either to close an
19110 outer template argument list or to complete a new-style
19111 cast. */
19112 cp_token *token = cp_lexer_peek_token (parser->lexer);
19113 token->type = CPP_GREATER;
19115 else if (!saved_greater_than_is_operator_p)
19117 /* If we're in a nested template argument list, the '>>' has
19118 to be a typo for '> >'. We emit the error message, but we
19119 continue parsing and we push a '>' as next token, so that
19120 the argument list will be parsed correctly. Note that the
19121 global source location is still on the token before the
19122 '>>', so we need to say explicitly where we want it. */
19123 cp_token *token = cp_lexer_peek_token (parser->lexer);
19124 error_at (token->location, "%<>>%> should be %<> >%> "
19125 "within a nested template argument list");
19127 token->type = CPP_GREATER;
19129 else
19131 /* If this is not a nested template argument list, the '>>'
19132 is a typo for '>'. Emit an error message and continue.
19133 Same deal about the token location, but here we can get it
19134 right by consuming the '>>' before issuing the diagnostic. */
19135 cp_token *token = cp_lexer_consume_token (parser->lexer);
19136 error_at (token->location,
19137 "spurious %<>>%>, use %<>%> to terminate "
19138 "a template argument list");
19141 else
19142 cp_parser_skip_to_end_of_template_parameter_list (parser);
19143 /* The `>' token might be a greater-than operator again now. */
19144 parser->greater_than_is_operator_p
19145 = saved_greater_than_is_operator_p;
19146 /* Restore the SAVED_SCOPE. */
19147 parser->scope = saved_scope;
19148 parser->qualifying_scope = saved_qualifying_scope;
19149 parser->object_scope = saved_object_scope;
19150 cp_unevaluated_operand = saved_unevaluated_operand;
19151 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
19153 return arguments;
19156 /* MEMBER_FUNCTION is a member function, or a friend. If default
19157 arguments, or the body of the function have not yet been parsed,
19158 parse them now. */
19160 static void
19161 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
19163 /* If this member is a template, get the underlying
19164 FUNCTION_DECL. */
19165 if (DECL_FUNCTION_TEMPLATE_P (member_function))
19166 member_function = DECL_TEMPLATE_RESULT (member_function);
19168 /* There should not be any class definitions in progress at this
19169 point; the bodies of members are only parsed outside of all class
19170 definitions. */
19171 gcc_assert (parser->num_classes_being_defined == 0);
19172 /* While we're parsing the member functions we might encounter more
19173 classes. We want to handle them right away, but we don't want
19174 them getting mixed up with functions that are currently in the
19175 queue. */
19176 parser->unparsed_functions_queues
19177 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
19179 /* Make sure that any template parameters are in scope. */
19180 maybe_begin_member_template_processing (member_function);
19182 /* If the body of the function has not yet been parsed, parse it
19183 now. */
19184 if (DECL_PENDING_INLINE_P (member_function))
19186 tree function_scope;
19187 cp_token_cache *tokens;
19189 /* The function is no longer pending; we are processing it. */
19190 tokens = DECL_PENDING_INLINE_INFO (member_function);
19191 DECL_PENDING_INLINE_INFO (member_function) = NULL;
19192 DECL_PENDING_INLINE_P (member_function) = 0;
19194 /* If this is a local class, enter the scope of the containing
19195 function. */
19196 function_scope = current_function_decl;
19197 if (function_scope)
19198 push_function_context ();
19200 /* Push the body of the function onto the lexer stack. */
19201 cp_parser_push_lexer_for_tokens (parser, tokens);
19203 /* Let the front end know that we going to be defining this
19204 function. */
19205 start_preparsed_function (member_function, NULL_TREE,
19206 SF_PRE_PARSED | SF_INCLASS_INLINE);
19208 /* Don't do access checking if it is a templated function. */
19209 if (processing_template_decl)
19210 push_deferring_access_checks (dk_no_check);
19212 /* Now, parse the body of the function. */
19213 cp_parser_function_definition_after_declarator (parser,
19214 /*inline_p=*/true);
19216 if (processing_template_decl)
19217 pop_deferring_access_checks ();
19219 /* Leave the scope of the containing function. */
19220 if (function_scope)
19221 pop_function_context ();
19222 cp_parser_pop_lexer (parser);
19225 /* Remove any template parameters from the symbol table. */
19226 maybe_end_member_template_processing ();
19228 /* Restore the queue. */
19229 parser->unparsed_functions_queues
19230 = TREE_CHAIN (parser->unparsed_functions_queues);
19233 /* If DECL contains any default args, remember it on the unparsed
19234 functions queue. */
19236 static void
19237 cp_parser_save_default_args (cp_parser* parser, tree decl)
19239 tree probe;
19241 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
19242 probe;
19243 probe = TREE_CHAIN (probe))
19244 if (TREE_PURPOSE (probe))
19246 TREE_PURPOSE (parser->unparsed_functions_queues)
19247 = tree_cons (current_class_type, decl,
19248 TREE_PURPOSE (parser->unparsed_functions_queues));
19249 break;
19253 /* FN is a FUNCTION_DECL which may contains a parameter with an
19254 unparsed DEFAULT_ARG. Parse the default args now. This function
19255 assumes that the current scope is the scope in which the default
19256 argument should be processed. */
19258 static void
19259 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
19261 bool saved_local_variables_forbidden_p;
19262 tree parm, parmdecl;
19264 /* While we're parsing the default args, we might (due to the
19265 statement expression extension) encounter more classes. We want
19266 to handle them right away, but we don't want them getting mixed
19267 up with default args that are currently in the queue. */
19268 parser->unparsed_functions_queues
19269 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
19271 /* Local variable names (and the `this' keyword) may not appear
19272 in a default argument. */
19273 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
19274 parser->local_variables_forbidden_p = true;
19276 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
19277 parmdecl = DECL_ARGUMENTS (fn);
19278 parm && parm != void_list_node;
19279 parm = TREE_CHAIN (parm),
19280 parmdecl = TREE_CHAIN (parmdecl))
19282 cp_token_cache *tokens;
19283 tree default_arg = TREE_PURPOSE (parm);
19284 tree parsed_arg;
19285 VEC(tree,gc) *insts;
19286 tree copy;
19287 unsigned ix;
19289 if (!default_arg)
19290 continue;
19292 if (TREE_CODE (default_arg) != DEFAULT_ARG)
19293 /* This can happen for a friend declaration for a function
19294 already declared with default arguments. */
19295 continue;
19297 /* Push the saved tokens for the default argument onto the parser's
19298 lexer stack. */
19299 tokens = DEFARG_TOKENS (default_arg);
19300 cp_parser_push_lexer_for_tokens (parser, tokens);
19302 start_lambda_scope (parmdecl);
19304 /* Parse the assignment-expression. */
19305 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
19306 if (parsed_arg == error_mark_node)
19308 cp_parser_pop_lexer (parser);
19309 continue;
19312 if (!processing_template_decl)
19313 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
19315 TREE_PURPOSE (parm) = parsed_arg;
19317 /* Update any instantiations we've already created. */
19318 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
19319 VEC_iterate (tree, insts, ix, copy); ix++)
19320 TREE_PURPOSE (copy) = parsed_arg;
19322 finish_lambda_scope ();
19324 /* If the token stream has not been completely used up, then
19325 there was extra junk after the end of the default
19326 argument. */
19327 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
19328 cp_parser_error (parser, "expected %<,%>");
19330 /* Revert to the main lexer. */
19331 cp_parser_pop_lexer (parser);
19334 /* Make sure no default arg is missing. */
19335 check_default_args (fn);
19337 /* Restore the state of local_variables_forbidden_p. */
19338 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
19340 /* Restore the queue. */
19341 parser->unparsed_functions_queues
19342 = TREE_CHAIN (parser->unparsed_functions_queues);
19345 /* Parse the operand of `sizeof' (or a similar operator). Returns
19346 either a TYPE or an expression, depending on the form of the
19347 input. The KEYWORD indicates which kind of expression we have
19348 encountered. */
19350 static tree
19351 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
19353 tree expr = NULL_TREE;
19354 const char *saved_message;
19355 char *tmp;
19356 bool saved_integral_constant_expression_p;
19357 bool saved_non_integral_constant_expression_p;
19358 bool pack_expansion_p = false;
19360 /* Types cannot be defined in a `sizeof' expression. Save away the
19361 old message. */
19362 saved_message = parser->type_definition_forbidden_message;
19363 /* And create the new one. */
19364 tmp = concat ("types may not be defined in %<",
19365 IDENTIFIER_POINTER (ridpointers[keyword]),
19366 "%> expressions", NULL);
19367 parser->type_definition_forbidden_message = tmp;
19369 /* The restrictions on constant-expressions do not apply inside
19370 sizeof expressions. */
19371 saved_integral_constant_expression_p
19372 = parser->integral_constant_expression_p;
19373 saved_non_integral_constant_expression_p
19374 = parser->non_integral_constant_expression_p;
19375 parser->integral_constant_expression_p = false;
19377 /* If it's a `...', then we are computing the length of a parameter
19378 pack. */
19379 if (keyword == RID_SIZEOF
19380 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19382 /* Consume the `...'. */
19383 cp_lexer_consume_token (parser->lexer);
19384 maybe_warn_variadic_templates ();
19386 /* Note that this is an expansion. */
19387 pack_expansion_p = true;
19390 /* Do not actually evaluate the expression. */
19391 ++cp_unevaluated_operand;
19392 ++c_inhibit_evaluation_warnings;
19393 /* If it's a `(', then we might be looking at the type-id
19394 construction. */
19395 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19397 tree type;
19398 bool saved_in_type_id_in_expr_p;
19400 /* We can't be sure yet whether we're looking at a type-id or an
19401 expression. */
19402 cp_parser_parse_tentatively (parser);
19403 /* Consume the `('. */
19404 cp_lexer_consume_token (parser->lexer);
19405 /* Parse the type-id. */
19406 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
19407 parser->in_type_id_in_expr_p = true;
19408 type = cp_parser_type_id (parser);
19409 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
19410 /* Now, look for the trailing `)'. */
19411 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19412 /* If all went well, then we're done. */
19413 if (cp_parser_parse_definitely (parser))
19415 cp_decl_specifier_seq decl_specs;
19417 /* Build a trivial decl-specifier-seq. */
19418 clear_decl_specs (&decl_specs);
19419 decl_specs.type = type;
19421 /* Call grokdeclarator to figure out what type this is. */
19422 expr = grokdeclarator (NULL,
19423 &decl_specs,
19424 TYPENAME,
19425 /*initialized=*/0,
19426 /*attrlist=*/NULL);
19430 /* If the type-id production did not work out, then we must be
19431 looking at the unary-expression production. */
19432 if (!expr)
19433 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
19434 /*cast_p=*/false, NULL);
19436 if (pack_expansion_p)
19437 /* Build a pack expansion. */
19438 expr = make_pack_expansion (expr);
19440 /* Go back to evaluating expressions. */
19441 --cp_unevaluated_operand;
19442 --c_inhibit_evaluation_warnings;
19444 /* Free the message we created. */
19445 free (tmp);
19446 /* And restore the old one. */
19447 parser->type_definition_forbidden_message = saved_message;
19448 parser->integral_constant_expression_p
19449 = saved_integral_constant_expression_p;
19450 parser->non_integral_constant_expression_p
19451 = saved_non_integral_constant_expression_p;
19453 return expr;
19456 /* If the current declaration has no declarator, return true. */
19458 static bool
19459 cp_parser_declares_only_class_p (cp_parser *parser)
19461 /* If the next token is a `;' or a `,' then there is no
19462 declarator. */
19463 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
19464 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
19467 /* Update the DECL_SPECS to reflect the storage class indicated by
19468 KEYWORD. */
19470 static void
19471 cp_parser_set_storage_class (cp_parser *parser,
19472 cp_decl_specifier_seq *decl_specs,
19473 enum rid keyword,
19474 location_t location)
19476 cp_storage_class storage_class;
19478 if (parser->in_unbraced_linkage_specification_p)
19480 error_at (location, "invalid use of %qD in linkage specification",
19481 ridpointers[keyword]);
19482 return;
19484 else if (decl_specs->storage_class != sc_none)
19486 decl_specs->conflicting_specifiers_p = true;
19487 return;
19490 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
19491 && decl_specs->specs[(int) ds_thread])
19493 error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
19494 decl_specs->specs[(int) ds_thread] = 0;
19497 switch (keyword)
19499 case RID_AUTO:
19500 storage_class = sc_auto;
19501 break;
19502 case RID_REGISTER:
19503 storage_class = sc_register;
19504 break;
19505 case RID_STATIC:
19506 storage_class = sc_static;
19507 break;
19508 case RID_EXTERN:
19509 storage_class = sc_extern;
19510 break;
19511 case RID_MUTABLE:
19512 storage_class = sc_mutable;
19513 break;
19514 default:
19515 gcc_unreachable ();
19517 decl_specs->storage_class = storage_class;
19519 /* A storage class specifier cannot be applied alongside a typedef
19520 specifier. If there is a typedef specifier present then set
19521 conflicting_specifiers_p which will trigger an error later
19522 on in grokdeclarator. */
19523 if (decl_specs->specs[(int)ds_typedef])
19524 decl_specs->conflicting_specifiers_p = true;
19527 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
19528 is true, the type is a user-defined type; otherwise it is a
19529 built-in type specified by a keyword. */
19531 static void
19532 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
19533 tree type_spec,
19534 location_t location,
19535 bool user_defined_p)
19537 decl_specs->any_specifiers_p = true;
19539 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
19540 (with, for example, in "typedef int wchar_t;") we remember that
19541 this is what happened. In system headers, we ignore these
19542 declarations so that G++ can work with system headers that are not
19543 C++-safe. */
19544 if (decl_specs->specs[(int) ds_typedef]
19545 && !user_defined_p
19546 && (type_spec == boolean_type_node
19547 || type_spec == char16_type_node
19548 || type_spec == char32_type_node
19549 || type_spec == wchar_type_node)
19550 && (decl_specs->type
19551 || decl_specs->specs[(int) ds_long]
19552 || decl_specs->specs[(int) ds_short]
19553 || decl_specs->specs[(int) ds_unsigned]
19554 || decl_specs->specs[(int) ds_signed]))
19556 decl_specs->redefined_builtin_type = type_spec;
19557 if (!decl_specs->type)
19559 decl_specs->type = type_spec;
19560 decl_specs->user_defined_type_p = false;
19561 decl_specs->type_location = location;
19564 else if (decl_specs->type)
19565 decl_specs->multiple_types_p = true;
19566 else
19568 decl_specs->type = type_spec;
19569 decl_specs->user_defined_type_p = user_defined_p;
19570 decl_specs->redefined_builtin_type = NULL_TREE;
19571 decl_specs->type_location = location;
19575 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
19576 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
19578 static bool
19579 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
19581 return decl_specifiers->specs[(int) ds_friend] != 0;
19584 /* If the next token is of the indicated TYPE, consume it. Otherwise,
19585 issue an error message indicating that TOKEN_DESC was expected.
19587 Returns the token consumed, if the token had the appropriate type.
19588 Otherwise, returns NULL. */
19590 static cp_token *
19591 cp_parser_require (cp_parser* parser,
19592 enum cpp_ttype type,
19593 const char* token_desc)
19595 if (cp_lexer_next_token_is (parser->lexer, type))
19596 return cp_lexer_consume_token (parser->lexer);
19597 else
19599 /* Output the MESSAGE -- unless we're parsing tentatively. */
19600 if (!cp_parser_simulate_error (parser))
19602 char *message = concat ("expected ", token_desc, NULL);
19603 cp_parser_error (parser, message);
19604 free (message);
19606 return NULL;
19610 /* An error message is produced if the next token is not '>'.
19611 All further tokens are skipped until the desired token is
19612 found or '{', '}', ';' or an unbalanced ')' or ']'. */
19614 static void
19615 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
19617 /* Current level of '< ... >'. */
19618 unsigned level = 0;
19619 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
19620 unsigned nesting_depth = 0;
19622 /* Are we ready, yet? If not, issue error message. */
19623 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
19624 return;
19626 /* Skip tokens until the desired token is found. */
19627 while (true)
19629 /* Peek at the next token. */
19630 switch (cp_lexer_peek_token (parser->lexer)->type)
19632 case CPP_LESS:
19633 if (!nesting_depth)
19634 ++level;
19635 break;
19637 case CPP_RSHIFT:
19638 if (cxx_dialect == cxx98)
19639 /* C++0x views the `>>' operator as two `>' tokens, but
19640 C++98 does not. */
19641 break;
19642 else if (!nesting_depth && level-- == 0)
19644 /* We've hit a `>>' where the first `>' closes the
19645 template argument list, and the second `>' is
19646 spurious. Just consume the `>>' and stop; we've
19647 already produced at least one error. */
19648 cp_lexer_consume_token (parser->lexer);
19649 return;
19651 /* Fall through for C++0x, so we handle the second `>' in
19652 the `>>'. */
19654 case CPP_GREATER:
19655 if (!nesting_depth && level-- == 0)
19657 /* We've reached the token we want, consume it and stop. */
19658 cp_lexer_consume_token (parser->lexer);
19659 return;
19661 break;
19663 case CPP_OPEN_PAREN:
19664 case CPP_OPEN_SQUARE:
19665 ++nesting_depth;
19666 break;
19668 case CPP_CLOSE_PAREN:
19669 case CPP_CLOSE_SQUARE:
19670 if (nesting_depth-- == 0)
19671 return;
19672 break;
19674 case CPP_EOF:
19675 case CPP_PRAGMA_EOL:
19676 case CPP_SEMICOLON:
19677 case CPP_OPEN_BRACE:
19678 case CPP_CLOSE_BRACE:
19679 /* The '>' was probably forgotten, don't look further. */
19680 return;
19682 default:
19683 break;
19686 /* Consume this token. */
19687 cp_lexer_consume_token (parser->lexer);
19691 /* If the next token is the indicated keyword, consume it. Otherwise,
19692 issue an error message indicating that TOKEN_DESC was expected.
19694 Returns the token consumed, if the token had the appropriate type.
19695 Otherwise, returns NULL. */
19697 static cp_token *
19698 cp_parser_require_keyword (cp_parser* parser,
19699 enum rid keyword,
19700 const char* token_desc)
19702 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
19704 if (token && token->keyword != keyword)
19706 dyn_string_t error_msg;
19708 /* Format the error message. */
19709 error_msg = dyn_string_new (0);
19710 dyn_string_append_cstr (error_msg, "expected ");
19711 dyn_string_append_cstr (error_msg, token_desc);
19712 cp_parser_error (parser, error_msg->s);
19713 dyn_string_delete (error_msg);
19714 return NULL;
19717 return token;
19720 /* Returns TRUE iff TOKEN is a token that can begin the body of a
19721 function-definition. */
19723 static bool
19724 cp_parser_token_starts_function_definition_p (cp_token* token)
19726 return (/* An ordinary function-body begins with an `{'. */
19727 token->type == CPP_OPEN_BRACE
19728 /* A ctor-initializer begins with a `:'. */
19729 || token->type == CPP_COLON
19730 /* A function-try-block begins with `try'. */
19731 || token->keyword == RID_TRY
19732 /* The named return value extension begins with `return'. */
19733 || token->keyword == RID_RETURN);
19736 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
19737 definition. */
19739 static bool
19740 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
19742 cp_token *token;
19744 token = cp_lexer_peek_token (parser->lexer);
19745 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
19748 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
19749 C++0x) ending a template-argument. */
19751 static bool
19752 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
19754 cp_token *token;
19756 token = cp_lexer_peek_token (parser->lexer);
19757 return (token->type == CPP_COMMA
19758 || token->type == CPP_GREATER
19759 || token->type == CPP_ELLIPSIS
19760 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
19763 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
19764 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
19766 static bool
19767 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
19768 size_t n)
19770 cp_token *token;
19772 token = cp_lexer_peek_nth_token (parser->lexer, n);
19773 if (token->type == CPP_LESS)
19774 return true;
19775 /* Check for the sequence `<::' in the original code. It would be lexed as
19776 `[:', where `[' is a digraph, and there is no whitespace before
19777 `:'. */
19778 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
19780 cp_token *token2;
19781 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
19782 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
19783 return true;
19785 return false;
19788 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
19789 or none_type otherwise. */
19791 static enum tag_types
19792 cp_parser_token_is_class_key (cp_token* token)
19794 switch (token->keyword)
19796 case RID_CLASS:
19797 return class_type;
19798 case RID_STRUCT:
19799 return record_type;
19800 case RID_UNION:
19801 return union_type;
19803 default:
19804 return none_type;
19808 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
19810 static void
19811 cp_parser_check_class_key (enum tag_types class_key, tree type)
19813 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
19814 permerror (input_location, "%qs tag used in naming %q#T",
19815 class_key == union_type ? "union"
19816 : class_key == record_type ? "struct" : "class",
19817 type);
19820 /* Issue an error message if DECL is redeclared with different
19821 access than its original declaration [class.access.spec/3].
19822 This applies to nested classes and nested class templates.
19823 [class.mem/1]. */
19825 static void
19826 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
19828 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
19829 return;
19831 if ((TREE_PRIVATE (decl)
19832 != (current_access_specifier == access_private_node))
19833 || (TREE_PROTECTED (decl)
19834 != (current_access_specifier == access_protected_node)))
19835 error_at (location, "%qD redeclared with different access", decl);
19838 /* Look for the `template' keyword, as a syntactic disambiguator.
19839 Return TRUE iff it is present, in which case it will be
19840 consumed. */
19842 static bool
19843 cp_parser_optional_template_keyword (cp_parser *parser)
19845 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
19847 /* The `template' keyword can only be used within templates;
19848 outside templates the parser can always figure out what is a
19849 template and what is not. */
19850 if (!processing_template_decl)
19852 cp_token *token = cp_lexer_peek_token (parser->lexer);
19853 error_at (token->location,
19854 "%<template%> (as a disambiguator) is only allowed "
19855 "within templates");
19856 /* If this part of the token stream is rescanned, the same
19857 error message would be generated. So, we purge the token
19858 from the stream. */
19859 cp_lexer_purge_token (parser->lexer);
19860 return false;
19862 else
19864 /* Consume the `template' keyword. */
19865 cp_lexer_consume_token (parser->lexer);
19866 return true;
19870 return false;
19873 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
19874 set PARSER->SCOPE, and perform other related actions. */
19876 static void
19877 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
19879 int i;
19880 struct tree_check *check_value;
19881 deferred_access_check *chk;
19882 VEC (deferred_access_check,gc) *checks;
19884 /* Get the stored value. */
19885 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
19886 /* Perform any access checks that were deferred. */
19887 checks = check_value->checks;
19888 if (checks)
19890 for (i = 0 ;
19891 VEC_iterate (deferred_access_check, checks, i, chk) ;
19892 ++i)
19894 perform_or_defer_access_check (chk->binfo,
19895 chk->decl,
19896 chk->diag_decl);
19899 /* Set the scope from the stored value. */
19900 parser->scope = check_value->value;
19901 parser->qualifying_scope = check_value->qualifying_scope;
19902 parser->object_scope = NULL_TREE;
19905 /* Consume tokens up through a non-nested END token. Returns TRUE if we
19906 encounter the end of a block before what we were looking for. */
19908 static bool
19909 cp_parser_cache_group (cp_parser *parser,
19910 enum cpp_ttype end,
19911 unsigned depth)
19913 while (true)
19915 cp_token *token = cp_lexer_peek_token (parser->lexer);
19917 /* Abort a parenthesized expression if we encounter a semicolon. */
19918 if ((end == CPP_CLOSE_PAREN || depth == 0)
19919 && token->type == CPP_SEMICOLON)
19920 return true;
19921 /* If we've reached the end of the file, stop. */
19922 if (token->type == CPP_EOF
19923 || (end != CPP_PRAGMA_EOL
19924 && token->type == CPP_PRAGMA_EOL))
19925 return true;
19926 if (token->type == CPP_CLOSE_BRACE && depth == 0)
19927 /* We've hit the end of an enclosing block, so there's been some
19928 kind of syntax error. */
19929 return true;
19931 /* Consume the token. */
19932 cp_lexer_consume_token (parser->lexer);
19933 /* See if it starts a new group. */
19934 if (token->type == CPP_OPEN_BRACE)
19936 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
19937 /* In theory this should probably check end == '}', but
19938 cp_parser_save_member_function_body needs it to exit
19939 after either '}' or ')' when called with ')'. */
19940 if (depth == 0)
19941 return false;
19943 else if (token->type == CPP_OPEN_PAREN)
19945 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
19946 if (depth == 0 && end == CPP_CLOSE_PAREN)
19947 return false;
19949 else if (token->type == CPP_PRAGMA)
19950 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
19951 else if (token->type == end)
19952 return false;
19956 /* Begin parsing tentatively. We always save tokens while parsing
19957 tentatively so that if the tentative parsing fails we can restore the
19958 tokens. */
19960 static void
19961 cp_parser_parse_tentatively (cp_parser* parser)
19963 /* Enter a new parsing context. */
19964 parser->context = cp_parser_context_new (parser->context);
19965 /* Begin saving tokens. */
19966 cp_lexer_save_tokens (parser->lexer);
19967 /* In order to avoid repetitive access control error messages,
19968 access checks are queued up until we are no longer parsing
19969 tentatively. */
19970 push_deferring_access_checks (dk_deferred);
19973 /* Commit to the currently active tentative parse. */
19975 static void
19976 cp_parser_commit_to_tentative_parse (cp_parser* parser)
19978 cp_parser_context *context;
19979 cp_lexer *lexer;
19981 /* Mark all of the levels as committed. */
19982 lexer = parser->lexer;
19983 for (context = parser->context; context->next; context = context->next)
19985 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
19986 break;
19987 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
19988 while (!cp_lexer_saving_tokens (lexer))
19989 lexer = lexer->next;
19990 cp_lexer_commit_tokens (lexer);
19994 /* Abort the currently active tentative parse. All consumed tokens
19995 will be rolled back, and no diagnostics will be issued. */
19997 static void
19998 cp_parser_abort_tentative_parse (cp_parser* parser)
20000 cp_parser_simulate_error (parser);
20001 /* Now, pretend that we want to see if the construct was
20002 successfully parsed. */
20003 cp_parser_parse_definitely (parser);
20006 /* Stop parsing tentatively. If a parse error has occurred, restore the
20007 token stream. Otherwise, commit to the tokens we have consumed.
20008 Returns true if no error occurred; false otherwise. */
20010 static bool
20011 cp_parser_parse_definitely (cp_parser* parser)
20013 bool error_occurred;
20014 cp_parser_context *context;
20016 /* Remember whether or not an error occurred, since we are about to
20017 destroy that information. */
20018 error_occurred = cp_parser_error_occurred (parser);
20019 /* Remove the topmost context from the stack. */
20020 context = parser->context;
20021 parser->context = context->next;
20022 /* If no parse errors occurred, commit to the tentative parse. */
20023 if (!error_occurred)
20025 /* Commit to the tokens read tentatively, unless that was
20026 already done. */
20027 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
20028 cp_lexer_commit_tokens (parser->lexer);
20030 pop_to_parent_deferring_access_checks ();
20032 /* Otherwise, if errors occurred, roll back our state so that things
20033 are just as they were before we began the tentative parse. */
20034 else
20036 cp_lexer_rollback_tokens (parser->lexer);
20037 pop_deferring_access_checks ();
20039 /* Add the context to the front of the free list. */
20040 context->next = cp_parser_context_free_list;
20041 cp_parser_context_free_list = context;
20043 return !error_occurred;
20046 /* Returns true if we are parsing tentatively and are not committed to
20047 this tentative parse. */
20049 static bool
20050 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
20052 return (cp_parser_parsing_tentatively (parser)
20053 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
20056 /* Returns nonzero iff an error has occurred during the most recent
20057 tentative parse. */
20059 static bool
20060 cp_parser_error_occurred (cp_parser* parser)
20062 return (cp_parser_parsing_tentatively (parser)
20063 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
20066 /* Returns nonzero if GNU extensions are allowed. */
20068 static bool
20069 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
20071 return parser->allow_gnu_extensions_p;
20074 /* Objective-C++ Productions */
20077 /* Parse an Objective-C expression, which feeds into a primary-expression
20078 above.
20080 objc-expression:
20081 objc-message-expression
20082 objc-string-literal
20083 objc-encode-expression
20084 objc-protocol-expression
20085 objc-selector-expression
20087 Returns a tree representation of the expression. */
20089 static tree
20090 cp_parser_objc_expression (cp_parser* parser)
20092 /* Try to figure out what kind of declaration is present. */
20093 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20095 switch (kwd->type)
20097 case CPP_OPEN_SQUARE:
20098 return cp_parser_objc_message_expression (parser);
20100 case CPP_OBJC_STRING:
20101 kwd = cp_lexer_consume_token (parser->lexer);
20102 return objc_build_string_object (kwd->u.value);
20104 case CPP_KEYWORD:
20105 switch (kwd->keyword)
20107 case RID_AT_ENCODE:
20108 return cp_parser_objc_encode_expression (parser);
20110 case RID_AT_PROTOCOL:
20111 return cp_parser_objc_protocol_expression (parser);
20113 case RID_AT_SELECTOR:
20114 return cp_parser_objc_selector_expression (parser);
20116 default:
20117 break;
20119 default:
20120 error_at (kwd->location,
20121 "misplaced %<@%D%> Objective-C++ construct",
20122 kwd->u.value);
20123 cp_parser_skip_to_end_of_block_or_statement (parser);
20126 return error_mark_node;
20129 /* Parse an Objective-C message expression.
20131 objc-message-expression:
20132 [ objc-message-receiver objc-message-args ]
20134 Returns a representation of an Objective-C message. */
20136 static tree
20137 cp_parser_objc_message_expression (cp_parser* parser)
20139 tree receiver, messageargs;
20141 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
20142 receiver = cp_parser_objc_message_receiver (parser);
20143 messageargs = cp_parser_objc_message_args (parser);
20144 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
20146 return objc_build_message_expr (build_tree_list (receiver, messageargs));
20149 /* Parse an objc-message-receiver.
20151 objc-message-receiver:
20152 expression
20153 simple-type-specifier
20155 Returns a representation of the type or expression. */
20157 static tree
20158 cp_parser_objc_message_receiver (cp_parser* parser)
20160 tree rcv;
20162 /* An Objective-C message receiver may be either (1) a type
20163 or (2) an expression. */
20164 cp_parser_parse_tentatively (parser);
20165 rcv = cp_parser_expression (parser, false, NULL);
20167 if (cp_parser_parse_definitely (parser))
20168 return rcv;
20170 rcv = cp_parser_simple_type_specifier (parser,
20171 /*decl_specs=*/NULL,
20172 CP_PARSER_FLAGS_NONE);
20174 return objc_get_class_reference (rcv);
20177 /* Parse the arguments and selectors comprising an Objective-C message.
20179 objc-message-args:
20180 objc-selector
20181 objc-selector-args
20182 objc-selector-args , objc-comma-args
20184 objc-selector-args:
20185 objc-selector [opt] : assignment-expression
20186 objc-selector-args objc-selector [opt] : assignment-expression
20188 objc-comma-args:
20189 assignment-expression
20190 objc-comma-args , assignment-expression
20192 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
20193 selector arguments and TREE_VALUE containing a list of comma
20194 arguments. */
20196 static tree
20197 cp_parser_objc_message_args (cp_parser* parser)
20199 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
20200 bool maybe_unary_selector_p = true;
20201 cp_token *token = cp_lexer_peek_token (parser->lexer);
20203 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
20205 tree selector = NULL_TREE, arg;
20207 if (token->type != CPP_COLON)
20208 selector = cp_parser_objc_selector (parser);
20210 /* Detect if we have a unary selector. */
20211 if (maybe_unary_selector_p
20212 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
20213 return build_tree_list (selector, NULL_TREE);
20215 maybe_unary_selector_p = false;
20216 cp_parser_require (parser, CPP_COLON, "%<:%>");
20217 arg = cp_parser_assignment_expression (parser, false, NULL);
20219 sel_args
20220 = chainon (sel_args,
20221 build_tree_list (selector, arg));
20223 token = cp_lexer_peek_token (parser->lexer);
20226 /* Handle non-selector arguments, if any. */
20227 while (token->type == CPP_COMMA)
20229 tree arg;
20231 cp_lexer_consume_token (parser->lexer);
20232 arg = cp_parser_assignment_expression (parser, false, NULL);
20234 addl_args
20235 = chainon (addl_args,
20236 build_tree_list (NULL_TREE, arg));
20238 token = cp_lexer_peek_token (parser->lexer);
20241 return build_tree_list (sel_args, addl_args);
20244 /* Parse an Objective-C encode expression.
20246 objc-encode-expression:
20247 @encode objc-typename
20249 Returns an encoded representation of the type argument. */
20251 static tree
20252 cp_parser_objc_encode_expression (cp_parser* parser)
20254 tree type;
20255 cp_token *token;
20257 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
20258 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20259 token = cp_lexer_peek_token (parser->lexer);
20260 type = complete_type (cp_parser_type_id (parser));
20261 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20263 if (!type)
20265 error_at (token->location,
20266 "%<@encode%> must specify a type as an argument");
20267 return error_mark_node;
20270 return objc_build_encode_expr (type);
20273 /* Parse an Objective-C @defs expression. */
20275 static tree
20276 cp_parser_objc_defs_expression (cp_parser *parser)
20278 tree name;
20280 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
20281 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20282 name = cp_parser_identifier (parser);
20283 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20285 return objc_get_class_ivars (name);
20288 /* Parse an Objective-C protocol expression.
20290 objc-protocol-expression:
20291 @protocol ( identifier )
20293 Returns a representation of the protocol expression. */
20295 static tree
20296 cp_parser_objc_protocol_expression (cp_parser* parser)
20298 tree proto;
20300 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
20301 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20302 proto = cp_parser_identifier (parser);
20303 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20305 return objc_build_protocol_expr (proto);
20308 /* Parse an Objective-C selector expression.
20310 objc-selector-expression:
20311 @selector ( objc-method-signature )
20313 objc-method-signature:
20314 objc-selector
20315 objc-selector-seq
20317 objc-selector-seq:
20318 objc-selector :
20319 objc-selector-seq objc-selector :
20321 Returns a representation of the method selector. */
20323 static tree
20324 cp_parser_objc_selector_expression (cp_parser* parser)
20326 tree sel_seq = NULL_TREE;
20327 bool maybe_unary_selector_p = true;
20328 cp_token *token;
20329 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
20331 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
20332 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20333 token = cp_lexer_peek_token (parser->lexer);
20335 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
20336 || token->type == CPP_SCOPE)
20338 tree selector = NULL_TREE;
20340 if (token->type != CPP_COLON
20341 || token->type == CPP_SCOPE)
20342 selector = cp_parser_objc_selector (parser);
20344 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
20345 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
20347 /* Detect if we have a unary selector. */
20348 if (maybe_unary_selector_p)
20350 sel_seq = selector;
20351 goto finish_selector;
20353 else
20355 cp_parser_error (parser, "expected %<:%>");
20358 maybe_unary_selector_p = false;
20359 token = cp_lexer_consume_token (parser->lexer);
20361 if (token->type == CPP_SCOPE)
20363 sel_seq
20364 = chainon (sel_seq,
20365 build_tree_list (selector, NULL_TREE));
20366 sel_seq
20367 = chainon (sel_seq,
20368 build_tree_list (NULL_TREE, NULL_TREE));
20370 else
20371 sel_seq
20372 = chainon (sel_seq,
20373 build_tree_list (selector, NULL_TREE));
20375 token = cp_lexer_peek_token (parser->lexer);
20378 finish_selector:
20379 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20381 return objc_build_selector_expr (loc, sel_seq);
20384 /* Parse a list of identifiers.
20386 objc-identifier-list:
20387 identifier
20388 objc-identifier-list , identifier
20390 Returns a TREE_LIST of identifier nodes. */
20392 static tree
20393 cp_parser_objc_identifier_list (cp_parser* parser)
20395 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
20396 cp_token *sep = cp_lexer_peek_token (parser->lexer);
20398 while (sep->type == CPP_COMMA)
20400 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
20401 list = chainon (list,
20402 build_tree_list (NULL_TREE,
20403 cp_parser_identifier (parser)));
20404 sep = cp_lexer_peek_token (parser->lexer);
20407 return list;
20410 /* Parse an Objective-C alias declaration.
20412 objc-alias-declaration:
20413 @compatibility_alias identifier identifier ;
20415 This function registers the alias mapping with the Objective-C front end.
20416 It returns nothing. */
20418 static void
20419 cp_parser_objc_alias_declaration (cp_parser* parser)
20421 tree alias, orig;
20423 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
20424 alias = cp_parser_identifier (parser);
20425 orig = cp_parser_identifier (parser);
20426 objc_declare_alias (alias, orig);
20427 cp_parser_consume_semicolon_at_end_of_statement (parser);
20430 /* Parse an Objective-C class forward-declaration.
20432 objc-class-declaration:
20433 @class objc-identifier-list ;
20435 The function registers the forward declarations with the Objective-C
20436 front end. It returns nothing. */
20438 static void
20439 cp_parser_objc_class_declaration (cp_parser* parser)
20441 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
20442 objc_declare_class (cp_parser_objc_identifier_list (parser));
20443 cp_parser_consume_semicolon_at_end_of_statement (parser);
20446 /* Parse a list of Objective-C protocol references.
20448 objc-protocol-refs-opt:
20449 objc-protocol-refs [opt]
20451 objc-protocol-refs:
20452 < objc-identifier-list >
20454 Returns a TREE_LIST of identifiers, if any. */
20456 static tree
20457 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
20459 tree protorefs = NULL_TREE;
20461 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
20463 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
20464 protorefs = cp_parser_objc_identifier_list (parser);
20465 cp_parser_require (parser, CPP_GREATER, "%<>%>");
20468 return protorefs;
20471 /* Parse a Objective-C visibility specification. */
20473 static void
20474 cp_parser_objc_visibility_spec (cp_parser* parser)
20476 cp_token *vis = cp_lexer_peek_token (parser->lexer);
20478 switch (vis->keyword)
20480 case RID_AT_PRIVATE:
20481 objc_set_visibility (2);
20482 break;
20483 case RID_AT_PROTECTED:
20484 objc_set_visibility (0);
20485 break;
20486 case RID_AT_PUBLIC:
20487 objc_set_visibility (1);
20488 break;
20489 default:
20490 return;
20493 /* Eat '@private'/'@protected'/'@public'. */
20494 cp_lexer_consume_token (parser->lexer);
20497 /* Parse an Objective-C method type. */
20499 static void
20500 cp_parser_objc_method_type (cp_parser* parser)
20502 objc_set_method_type
20503 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
20504 ? PLUS_EXPR
20505 : MINUS_EXPR);
20508 /* Parse an Objective-C protocol qualifier. */
20510 static tree
20511 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
20513 tree quals = NULL_TREE, node;
20514 cp_token *token = cp_lexer_peek_token (parser->lexer);
20516 node = token->u.value;
20518 while (node && TREE_CODE (node) == IDENTIFIER_NODE
20519 && (node == ridpointers [(int) RID_IN]
20520 || node == ridpointers [(int) RID_OUT]
20521 || node == ridpointers [(int) RID_INOUT]
20522 || node == ridpointers [(int) RID_BYCOPY]
20523 || node == ridpointers [(int) RID_BYREF]
20524 || node == ridpointers [(int) RID_ONEWAY]))
20526 quals = tree_cons (NULL_TREE, node, quals);
20527 cp_lexer_consume_token (parser->lexer);
20528 token = cp_lexer_peek_token (parser->lexer);
20529 node = token->u.value;
20532 return quals;
20535 /* Parse an Objective-C typename. */
20537 static tree
20538 cp_parser_objc_typename (cp_parser* parser)
20540 tree type_name = NULL_TREE;
20542 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20544 tree proto_quals, cp_type = NULL_TREE;
20546 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
20547 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
20549 /* An ObjC type name may consist of just protocol qualifiers, in which
20550 case the type shall default to 'id'. */
20551 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
20552 cp_type = cp_parser_type_id (parser);
20554 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20555 type_name = build_tree_list (proto_quals, cp_type);
20558 return type_name;
20561 /* Check to see if TYPE refers to an Objective-C selector name. */
20563 static bool
20564 cp_parser_objc_selector_p (enum cpp_ttype type)
20566 return (type == CPP_NAME || type == CPP_KEYWORD
20567 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
20568 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
20569 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
20570 || type == CPP_XOR || type == CPP_XOR_EQ);
20573 /* Parse an Objective-C selector. */
20575 static tree
20576 cp_parser_objc_selector (cp_parser* parser)
20578 cp_token *token = cp_lexer_consume_token (parser->lexer);
20580 if (!cp_parser_objc_selector_p (token->type))
20582 error_at (token->location, "invalid Objective-C++ selector name");
20583 return error_mark_node;
20586 /* C++ operator names are allowed to appear in ObjC selectors. */
20587 switch (token->type)
20589 case CPP_AND_AND: return get_identifier ("and");
20590 case CPP_AND_EQ: return get_identifier ("and_eq");
20591 case CPP_AND: return get_identifier ("bitand");
20592 case CPP_OR: return get_identifier ("bitor");
20593 case CPP_COMPL: return get_identifier ("compl");
20594 case CPP_NOT: return get_identifier ("not");
20595 case CPP_NOT_EQ: return get_identifier ("not_eq");
20596 case CPP_OR_OR: return get_identifier ("or");
20597 case CPP_OR_EQ: return get_identifier ("or_eq");
20598 case CPP_XOR: return get_identifier ("xor");
20599 case CPP_XOR_EQ: return get_identifier ("xor_eq");
20600 default: return token->u.value;
20604 /* Parse an Objective-C params list. */
20606 static tree
20607 cp_parser_objc_method_keyword_params (cp_parser* parser)
20609 tree params = NULL_TREE;
20610 bool maybe_unary_selector_p = true;
20611 cp_token *token = cp_lexer_peek_token (parser->lexer);
20613 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
20615 tree selector = NULL_TREE, type_name, identifier;
20617 if (token->type != CPP_COLON)
20618 selector = cp_parser_objc_selector (parser);
20620 /* Detect if we have a unary selector. */
20621 if (maybe_unary_selector_p
20622 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
20623 return selector;
20625 maybe_unary_selector_p = false;
20626 cp_parser_require (parser, CPP_COLON, "%<:%>");
20627 type_name = cp_parser_objc_typename (parser);
20628 identifier = cp_parser_identifier (parser);
20630 params
20631 = chainon (params,
20632 objc_build_keyword_decl (selector,
20633 type_name,
20634 identifier));
20636 token = cp_lexer_peek_token (parser->lexer);
20639 return params;
20642 /* Parse the non-keyword Objective-C params. */
20644 static tree
20645 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
20647 tree params = make_node (TREE_LIST);
20648 cp_token *token = cp_lexer_peek_token (parser->lexer);
20649 *ellipsisp = false; /* Initially, assume no ellipsis. */
20651 while (token->type == CPP_COMMA)
20653 cp_parameter_declarator *parmdecl;
20654 tree parm;
20656 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
20657 token = cp_lexer_peek_token (parser->lexer);
20659 if (token->type == CPP_ELLIPSIS)
20661 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
20662 *ellipsisp = true;
20663 break;
20666 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20667 parm = grokdeclarator (parmdecl->declarator,
20668 &parmdecl->decl_specifiers,
20669 PARM, /*initialized=*/0,
20670 /*attrlist=*/NULL);
20672 chainon (params, build_tree_list (NULL_TREE, parm));
20673 token = cp_lexer_peek_token (parser->lexer);
20676 return params;
20679 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
20681 static void
20682 cp_parser_objc_interstitial_code (cp_parser* parser)
20684 cp_token *token = cp_lexer_peek_token (parser->lexer);
20686 /* If the next token is `extern' and the following token is a string
20687 literal, then we have a linkage specification. */
20688 if (token->keyword == RID_EXTERN
20689 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
20690 cp_parser_linkage_specification (parser);
20691 /* Handle #pragma, if any. */
20692 else if (token->type == CPP_PRAGMA)
20693 cp_parser_pragma (parser, pragma_external);
20694 /* Allow stray semicolons. */
20695 else if (token->type == CPP_SEMICOLON)
20696 cp_lexer_consume_token (parser->lexer);
20697 /* Finally, try to parse a block-declaration, or a function-definition. */
20698 else
20699 cp_parser_block_declaration (parser, /*statement_p=*/false);
20702 /* Parse a method signature. */
20704 static tree
20705 cp_parser_objc_method_signature (cp_parser* parser)
20707 tree rettype, kwdparms, optparms;
20708 bool ellipsis = false;
20710 cp_parser_objc_method_type (parser);
20711 rettype = cp_parser_objc_typename (parser);
20712 kwdparms = cp_parser_objc_method_keyword_params (parser);
20713 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
20715 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
20718 /* Pars an Objective-C method prototype list. */
20720 static void
20721 cp_parser_objc_method_prototype_list (cp_parser* parser)
20723 cp_token *token = cp_lexer_peek_token (parser->lexer);
20725 while (token->keyword != RID_AT_END)
20727 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
20729 objc_add_method_declaration
20730 (cp_parser_objc_method_signature (parser));
20731 cp_parser_consume_semicolon_at_end_of_statement (parser);
20733 else
20734 /* Allow for interspersed non-ObjC++ code. */
20735 cp_parser_objc_interstitial_code (parser);
20737 token = cp_lexer_peek_token (parser->lexer);
20740 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
20741 objc_finish_interface ();
20744 /* Parse an Objective-C method definition list. */
20746 static void
20747 cp_parser_objc_method_definition_list (cp_parser* parser)
20749 cp_token *token = cp_lexer_peek_token (parser->lexer);
20751 while (token->keyword != RID_AT_END)
20753 tree meth;
20755 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
20757 push_deferring_access_checks (dk_deferred);
20758 objc_start_method_definition
20759 (cp_parser_objc_method_signature (parser));
20761 /* For historical reasons, we accept an optional semicolon. */
20762 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20763 cp_lexer_consume_token (parser->lexer);
20765 perform_deferred_access_checks ();
20766 stop_deferring_access_checks ();
20767 meth = cp_parser_function_definition_after_declarator (parser,
20768 false);
20769 pop_deferring_access_checks ();
20770 objc_finish_method_definition (meth);
20772 else
20773 /* Allow for interspersed non-ObjC++ code. */
20774 cp_parser_objc_interstitial_code (parser);
20776 token = cp_lexer_peek_token (parser->lexer);
20779 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
20780 objc_finish_implementation ();
20783 /* Parse Objective-C ivars. */
20785 static void
20786 cp_parser_objc_class_ivars (cp_parser* parser)
20788 cp_token *token = cp_lexer_peek_token (parser->lexer);
20790 if (token->type != CPP_OPEN_BRACE)
20791 return; /* No ivars specified. */
20793 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
20794 token = cp_lexer_peek_token (parser->lexer);
20796 while (token->type != CPP_CLOSE_BRACE)
20798 cp_decl_specifier_seq declspecs;
20799 int decl_class_or_enum_p;
20800 tree prefix_attributes;
20802 cp_parser_objc_visibility_spec (parser);
20804 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
20805 break;
20807 cp_parser_decl_specifier_seq (parser,
20808 CP_PARSER_FLAGS_OPTIONAL,
20809 &declspecs,
20810 &decl_class_or_enum_p);
20811 prefix_attributes = declspecs.attributes;
20812 declspecs.attributes = NULL_TREE;
20814 /* Keep going until we hit the `;' at the end of the
20815 declaration. */
20816 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20818 tree width = NULL_TREE, attributes, first_attribute, decl;
20819 cp_declarator *declarator = NULL;
20820 int ctor_dtor_or_conv_p;
20822 /* Check for a (possibly unnamed) bitfield declaration. */
20823 token = cp_lexer_peek_token (parser->lexer);
20824 if (token->type == CPP_COLON)
20825 goto eat_colon;
20827 if (token->type == CPP_NAME
20828 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
20829 == CPP_COLON))
20831 /* Get the name of the bitfield. */
20832 declarator = make_id_declarator (NULL_TREE,
20833 cp_parser_identifier (parser),
20834 sfk_none);
20836 eat_colon:
20837 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
20838 /* Get the width of the bitfield. */
20839 width
20840 = cp_parser_constant_expression (parser,
20841 /*allow_non_constant=*/false,
20842 NULL);
20844 else
20846 /* Parse the declarator. */
20847 declarator
20848 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20849 &ctor_dtor_or_conv_p,
20850 /*parenthesized_p=*/NULL,
20851 /*member_p=*/false);
20854 /* Look for attributes that apply to the ivar. */
20855 attributes = cp_parser_attributes_opt (parser);
20856 /* Remember which attributes are prefix attributes and
20857 which are not. */
20858 first_attribute = attributes;
20859 /* Combine the attributes. */
20860 attributes = chainon (prefix_attributes, attributes);
20862 if (width)
20863 /* Create the bitfield declaration. */
20864 decl = grokbitfield (declarator, &declspecs,
20865 width,
20866 attributes);
20867 else
20868 decl = grokfield (declarator, &declspecs,
20869 NULL_TREE, /*init_const_expr_p=*/false,
20870 NULL_TREE, attributes);
20872 /* Add the instance variable. */
20873 objc_add_instance_variable (decl);
20875 /* Reset PREFIX_ATTRIBUTES. */
20876 while (attributes && TREE_CHAIN (attributes) != first_attribute)
20877 attributes = TREE_CHAIN (attributes);
20878 if (attributes)
20879 TREE_CHAIN (attributes) = NULL_TREE;
20881 token = cp_lexer_peek_token (parser->lexer);
20883 if (token->type == CPP_COMMA)
20885 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
20886 continue;
20888 break;
20891 cp_parser_consume_semicolon_at_end_of_statement (parser);
20892 token = cp_lexer_peek_token (parser->lexer);
20895 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
20896 /* For historical reasons, we accept an optional semicolon. */
20897 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20898 cp_lexer_consume_token (parser->lexer);
20901 /* Parse an Objective-C protocol declaration. */
20903 static void
20904 cp_parser_objc_protocol_declaration (cp_parser* parser)
20906 tree proto, protorefs;
20907 cp_token *tok;
20909 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
20910 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
20912 tok = cp_lexer_peek_token (parser->lexer);
20913 error_at (tok->location, "identifier expected after %<@protocol%>");
20914 goto finish;
20917 /* See if we have a forward declaration or a definition. */
20918 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
20920 /* Try a forward declaration first. */
20921 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
20923 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
20924 finish:
20925 cp_parser_consume_semicolon_at_end_of_statement (parser);
20928 /* Ok, we got a full-fledged definition (or at least should). */
20929 else
20931 proto = cp_parser_identifier (parser);
20932 protorefs = cp_parser_objc_protocol_refs_opt (parser);
20933 objc_start_protocol (proto, protorefs);
20934 cp_parser_objc_method_prototype_list (parser);
20938 /* Parse an Objective-C superclass or category. */
20940 static void
20941 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
20942 tree *categ)
20944 cp_token *next = cp_lexer_peek_token (parser->lexer);
20946 *super = *categ = NULL_TREE;
20947 if (next->type == CPP_COLON)
20949 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
20950 *super = cp_parser_identifier (parser);
20952 else if (next->type == CPP_OPEN_PAREN)
20954 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
20955 *categ = cp_parser_identifier (parser);
20956 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20960 /* Parse an Objective-C class interface. */
20962 static void
20963 cp_parser_objc_class_interface (cp_parser* parser)
20965 tree name, super, categ, protos;
20967 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
20968 name = cp_parser_identifier (parser);
20969 cp_parser_objc_superclass_or_category (parser, &super, &categ);
20970 protos = cp_parser_objc_protocol_refs_opt (parser);
20972 /* We have either a class or a category on our hands. */
20973 if (categ)
20974 objc_start_category_interface (name, categ, protos);
20975 else
20977 objc_start_class_interface (name, super, protos);
20978 /* Handle instance variable declarations, if any. */
20979 cp_parser_objc_class_ivars (parser);
20980 objc_continue_interface ();
20983 cp_parser_objc_method_prototype_list (parser);
20986 /* Parse an Objective-C class implementation. */
20988 static void
20989 cp_parser_objc_class_implementation (cp_parser* parser)
20991 tree name, super, categ;
20993 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
20994 name = cp_parser_identifier (parser);
20995 cp_parser_objc_superclass_or_category (parser, &super, &categ);
20997 /* We have either a class or a category on our hands. */
20998 if (categ)
20999 objc_start_category_implementation (name, categ);
21000 else
21002 objc_start_class_implementation (name, super);
21003 /* Handle instance variable declarations, if any. */
21004 cp_parser_objc_class_ivars (parser);
21005 objc_continue_implementation ();
21008 cp_parser_objc_method_definition_list (parser);
21011 /* Consume the @end token and finish off the implementation. */
21013 static void
21014 cp_parser_objc_end_implementation (cp_parser* parser)
21016 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
21017 objc_finish_implementation ();
21020 /* Parse an Objective-C declaration. */
21022 static void
21023 cp_parser_objc_declaration (cp_parser* parser)
21025 /* Try to figure out what kind of declaration is present. */
21026 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21028 switch (kwd->keyword)
21030 case RID_AT_ALIAS:
21031 cp_parser_objc_alias_declaration (parser);
21032 break;
21033 case RID_AT_CLASS:
21034 cp_parser_objc_class_declaration (parser);
21035 break;
21036 case RID_AT_PROTOCOL:
21037 cp_parser_objc_protocol_declaration (parser);
21038 break;
21039 case RID_AT_INTERFACE:
21040 cp_parser_objc_class_interface (parser);
21041 break;
21042 case RID_AT_IMPLEMENTATION:
21043 cp_parser_objc_class_implementation (parser);
21044 break;
21045 case RID_AT_END:
21046 cp_parser_objc_end_implementation (parser);
21047 break;
21048 default:
21049 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
21050 kwd->u.value);
21051 cp_parser_skip_to_end_of_block_or_statement (parser);
21055 /* Parse an Objective-C try-catch-finally statement.
21057 objc-try-catch-finally-stmt:
21058 @try compound-statement objc-catch-clause-seq [opt]
21059 objc-finally-clause [opt]
21061 objc-catch-clause-seq:
21062 objc-catch-clause objc-catch-clause-seq [opt]
21064 objc-catch-clause:
21065 @catch ( exception-declaration ) compound-statement
21067 objc-finally-clause
21068 @finally compound-statement
21070 Returns NULL_TREE. */
21072 static tree
21073 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
21074 location_t location;
21075 tree stmt;
21077 cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
21078 location = cp_lexer_peek_token (parser->lexer)->location;
21079 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
21080 node, lest it get absorbed into the surrounding block. */
21081 stmt = push_stmt_list ();
21082 cp_parser_compound_statement (parser, NULL, false);
21083 objc_begin_try_stmt (location, pop_stmt_list (stmt));
21085 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
21087 cp_parameter_declarator *parmdecl;
21088 tree parm;
21090 cp_lexer_consume_token (parser->lexer);
21091 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
21092 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
21093 parm = grokdeclarator (parmdecl->declarator,
21094 &parmdecl->decl_specifiers,
21095 PARM, /*initialized=*/0,
21096 /*attrlist=*/NULL);
21097 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
21098 objc_begin_catch_clause (parm);
21099 cp_parser_compound_statement (parser, NULL, false);
21100 objc_finish_catch_clause ();
21103 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
21105 cp_lexer_consume_token (parser->lexer);
21106 location = cp_lexer_peek_token (parser->lexer)->location;
21107 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
21108 node, lest it get absorbed into the surrounding block. */
21109 stmt = push_stmt_list ();
21110 cp_parser_compound_statement (parser, NULL, false);
21111 objc_build_finally_clause (location, pop_stmt_list (stmt));
21114 return objc_finish_try_stmt ();
21117 /* Parse an Objective-C synchronized statement.
21119 objc-synchronized-stmt:
21120 @synchronized ( expression ) compound-statement
21122 Returns NULL_TREE. */
21124 static tree
21125 cp_parser_objc_synchronized_statement (cp_parser *parser) {
21126 location_t location;
21127 tree lock, stmt;
21129 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
21131 location = cp_lexer_peek_token (parser->lexer)->location;
21132 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
21133 lock = cp_parser_expression (parser, false, NULL);
21134 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
21136 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
21137 node, lest it get absorbed into the surrounding block. */
21138 stmt = push_stmt_list ();
21139 cp_parser_compound_statement (parser, NULL, false);
21141 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
21144 /* Parse an Objective-C throw statement.
21146 objc-throw-stmt:
21147 @throw assignment-expression [opt] ;
21149 Returns a constructed '@throw' statement. */
21151 static tree
21152 cp_parser_objc_throw_statement (cp_parser *parser) {
21153 tree expr = NULL_TREE;
21154 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21156 cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
21158 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21159 expr = cp_parser_assignment_expression (parser, false, NULL);
21161 cp_parser_consume_semicolon_at_end_of_statement (parser);
21163 return objc_build_throw_stmt (loc, expr);
21166 /* Parse an Objective-C statement. */
21168 static tree
21169 cp_parser_objc_statement (cp_parser * parser) {
21170 /* Try to figure out what kind of declaration is present. */
21171 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21173 switch (kwd->keyword)
21175 case RID_AT_TRY:
21176 return cp_parser_objc_try_catch_finally_statement (parser);
21177 case RID_AT_SYNCHRONIZED:
21178 return cp_parser_objc_synchronized_statement (parser);
21179 case RID_AT_THROW:
21180 return cp_parser_objc_throw_statement (parser);
21181 default:
21182 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
21183 kwd->u.value);
21184 cp_parser_skip_to_end_of_block_or_statement (parser);
21187 return error_mark_node;
21190 /* OpenMP 2.5 parsing routines. */
21192 /* Returns name of the next clause.
21193 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
21194 the token is not consumed. Otherwise appropriate pragma_omp_clause is
21195 returned and the token is consumed. */
21197 static pragma_omp_clause
21198 cp_parser_omp_clause_name (cp_parser *parser)
21200 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
21202 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
21203 result = PRAGMA_OMP_CLAUSE_IF;
21204 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
21205 result = PRAGMA_OMP_CLAUSE_DEFAULT;
21206 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
21207 result = PRAGMA_OMP_CLAUSE_PRIVATE;
21208 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21210 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21211 const char *p = IDENTIFIER_POINTER (id);
21213 switch (p[0])
21215 case 'c':
21216 if (!strcmp ("collapse", p))
21217 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
21218 else if (!strcmp ("copyin", p))
21219 result = PRAGMA_OMP_CLAUSE_COPYIN;
21220 else if (!strcmp ("copyprivate", p))
21221 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
21222 break;
21223 case 'f':
21224 if (!strcmp ("firstprivate", p))
21225 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
21226 break;
21227 case 'l':
21228 if (!strcmp ("lastprivate", p))
21229 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
21230 break;
21231 case 'n':
21232 if (!strcmp ("nowait", p))
21233 result = PRAGMA_OMP_CLAUSE_NOWAIT;
21234 else if (!strcmp ("num_threads", p))
21235 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
21236 break;
21237 case 'o':
21238 if (!strcmp ("ordered", p))
21239 result = PRAGMA_OMP_CLAUSE_ORDERED;
21240 break;
21241 case 'r':
21242 if (!strcmp ("reduction", p))
21243 result = PRAGMA_OMP_CLAUSE_REDUCTION;
21244 break;
21245 case 's':
21246 if (!strcmp ("schedule", p))
21247 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
21248 else if (!strcmp ("shared", p))
21249 result = PRAGMA_OMP_CLAUSE_SHARED;
21250 break;
21251 case 'u':
21252 if (!strcmp ("untied", p))
21253 result = PRAGMA_OMP_CLAUSE_UNTIED;
21254 break;
21258 if (result != PRAGMA_OMP_CLAUSE_NONE)
21259 cp_lexer_consume_token (parser->lexer);
21261 return result;
21264 /* Validate that a clause of the given type does not already exist. */
21266 static void
21267 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
21268 const char *name, location_t location)
21270 tree c;
21272 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21273 if (OMP_CLAUSE_CODE (c) == code)
21275 error_at (location, "too many %qs clauses", name);
21276 break;
21280 /* OpenMP 2.5:
21281 variable-list:
21282 identifier
21283 variable-list , identifier
21285 In addition, we match a closing parenthesis. An opening parenthesis
21286 will have been consumed by the caller.
21288 If KIND is nonzero, create the appropriate node and install the decl
21289 in OMP_CLAUSE_DECL and add the node to the head of the list.
21291 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
21292 return the list created. */
21294 static tree
21295 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
21296 tree list)
21298 cp_token *token;
21299 while (1)
21301 tree name, decl;
21303 token = cp_lexer_peek_token (parser->lexer);
21304 name = cp_parser_id_expression (parser, /*template_p=*/false,
21305 /*check_dependency_p=*/true,
21306 /*template_p=*/NULL,
21307 /*declarator_p=*/false,
21308 /*optional_p=*/false);
21309 if (name == error_mark_node)
21310 goto skip_comma;
21312 decl = cp_parser_lookup_name_simple (parser, name, token->location);
21313 if (decl == error_mark_node)
21314 cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
21315 else if (kind != 0)
21317 tree u = build_omp_clause (token->location, kind);
21318 OMP_CLAUSE_DECL (u) = decl;
21319 OMP_CLAUSE_CHAIN (u) = list;
21320 list = u;
21322 else
21323 list = tree_cons (decl, NULL_TREE, list);
21325 get_comma:
21326 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21327 break;
21328 cp_lexer_consume_token (parser->lexer);
21331 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21333 int ending;
21335 /* Try to resync to an unnested comma. Copied from
21336 cp_parser_parenthesized_expression_list. */
21337 skip_comma:
21338 ending = cp_parser_skip_to_closing_parenthesis (parser,
21339 /*recovering=*/true,
21340 /*or_comma=*/true,
21341 /*consume_paren=*/true);
21342 if (ending < 0)
21343 goto get_comma;
21346 return list;
21349 /* Similarly, but expect leading and trailing parenthesis. This is a very
21350 common case for omp clauses. */
21352 static tree
21353 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
21355 if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21356 return cp_parser_omp_var_list_no_open (parser, kind, list);
21357 return list;
21360 /* OpenMP 3.0:
21361 collapse ( constant-expression ) */
21363 static tree
21364 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
21366 tree c, num;
21367 location_t loc;
21368 HOST_WIDE_INT n;
21370 loc = cp_lexer_peek_token (parser->lexer)->location;
21371 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21372 return list;
21374 num = cp_parser_constant_expression (parser, false, NULL);
21376 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21377 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21378 /*or_comma=*/false,
21379 /*consume_paren=*/true);
21381 if (num == error_mark_node)
21382 return list;
21383 num = fold_non_dependent_expr (num);
21384 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
21385 || !host_integerp (num, 0)
21386 || (n = tree_low_cst (num, 0)) <= 0
21387 || (int) n != n)
21389 error_at (loc, "collapse argument needs positive constant integer expression");
21390 return list;
21393 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
21394 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
21395 OMP_CLAUSE_CHAIN (c) = list;
21396 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
21398 return c;
21401 /* OpenMP 2.5:
21402 default ( shared | none ) */
21404 static tree
21405 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
21407 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
21408 tree c;
21410 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21411 return list;
21412 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21414 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21415 const char *p = IDENTIFIER_POINTER (id);
21417 switch (p[0])
21419 case 'n':
21420 if (strcmp ("none", p) != 0)
21421 goto invalid_kind;
21422 kind = OMP_CLAUSE_DEFAULT_NONE;
21423 break;
21425 case 's':
21426 if (strcmp ("shared", p) != 0)
21427 goto invalid_kind;
21428 kind = OMP_CLAUSE_DEFAULT_SHARED;
21429 break;
21431 default:
21432 goto invalid_kind;
21435 cp_lexer_consume_token (parser->lexer);
21437 else
21439 invalid_kind:
21440 cp_parser_error (parser, "expected %<none%> or %<shared%>");
21443 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21444 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21445 /*or_comma=*/false,
21446 /*consume_paren=*/true);
21448 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
21449 return list;
21451 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
21452 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
21453 OMP_CLAUSE_CHAIN (c) = list;
21454 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
21456 return c;
21459 /* OpenMP 2.5:
21460 if ( expression ) */
21462 static tree
21463 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
21465 tree t, c;
21467 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21468 return list;
21470 t = cp_parser_condition (parser);
21472 if (t == error_mark_node
21473 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21474 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21475 /*or_comma=*/false,
21476 /*consume_paren=*/true);
21478 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
21480 c = build_omp_clause (location, OMP_CLAUSE_IF);
21481 OMP_CLAUSE_IF_EXPR (c) = t;
21482 OMP_CLAUSE_CHAIN (c) = list;
21484 return c;
21487 /* OpenMP 2.5:
21488 nowait */
21490 static tree
21491 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
21492 tree list, location_t location)
21494 tree c;
21496 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
21498 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
21499 OMP_CLAUSE_CHAIN (c) = list;
21500 return c;
21503 /* OpenMP 2.5:
21504 num_threads ( expression ) */
21506 static tree
21507 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
21508 location_t location)
21510 tree t, c;
21512 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21513 return list;
21515 t = cp_parser_expression (parser, false, NULL);
21517 if (t == error_mark_node
21518 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21519 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21520 /*or_comma=*/false,
21521 /*consume_paren=*/true);
21523 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
21524 "num_threads", location);
21526 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
21527 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
21528 OMP_CLAUSE_CHAIN (c) = list;
21530 return c;
21533 /* OpenMP 2.5:
21534 ordered */
21536 static tree
21537 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
21538 tree list, location_t location)
21540 tree c;
21542 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
21543 "ordered", location);
21545 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
21546 OMP_CLAUSE_CHAIN (c) = list;
21547 return c;
21550 /* OpenMP 2.5:
21551 reduction ( reduction-operator : variable-list )
21553 reduction-operator:
21554 One of: + * - & ^ | && || */
21556 static tree
21557 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
21559 enum tree_code code;
21560 tree nlist, c;
21562 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21563 return list;
21565 switch (cp_lexer_peek_token (parser->lexer)->type)
21567 case CPP_PLUS:
21568 code = PLUS_EXPR;
21569 break;
21570 case CPP_MULT:
21571 code = MULT_EXPR;
21572 break;
21573 case CPP_MINUS:
21574 code = MINUS_EXPR;
21575 break;
21576 case CPP_AND:
21577 code = BIT_AND_EXPR;
21578 break;
21579 case CPP_XOR:
21580 code = BIT_XOR_EXPR;
21581 break;
21582 case CPP_OR:
21583 code = BIT_IOR_EXPR;
21584 break;
21585 case CPP_AND_AND:
21586 code = TRUTH_ANDIF_EXPR;
21587 break;
21588 case CPP_OR_OR:
21589 code = TRUTH_ORIF_EXPR;
21590 break;
21591 default:
21592 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
21593 "%<|%>, %<&&%>, or %<||%>");
21594 resync_fail:
21595 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21596 /*or_comma=*/false,
21597 /*consume_paren=*/true);
21598 return list;
21600 cp_lexer_consume_token (parser->lexer);
21602 if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
21603 goto resync_fail;
21605 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
21606 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
21607 OMP_CLAUSE_REDUCTION_CODE (c) = code;
21609 return nlist;
21612 /* OpenMP 2.5:
21613 schedule ( schedule-kind )
21614 schedule ( schedule-kind , expression )
21616 schedule-kind:
21617 static | dynamic | guided | runtime | auto */
21619 static tree
21620 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
21622 tree c, t;
21624 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21625 return list;
21627 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
21629 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21631 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21632 const char *p = IDENTIFIER_POINTER (id);
21634 switch (p[0])
21636 case 'd':
21637 if (strcmp ("dynamic", p) != 0)
21638 goto invalid_kind;
21639 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
21640 break;
21642 case 'g':
21643 if (strcmp ("guided", p) != 0)
21644 goto invalid_kind;
21645 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
21646 break;
21648 case 'r':
21649 if (strcmp ("runtime", p) != 0)
21650 goto invalid_kind;
21651 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
21652 break;
21654 default:
21655 goto invalid_kind;
21658 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
21659 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
21660 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
21661 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
21662 else
21663 goto invalid_kind;
21664 cp_lexer_consume_token (parser->lexer);
21666 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21668 cp_token *token;
21669 cp_lexer_consume_token (parser->lexer);
21671 token = cp_lexer_peek_token (parser->lexer);
21672 t = cp_parser_assignment_expression (parser, false, NULL);
21674 if (t == error_mark_node)
21675 goto resync_fail;
21676 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
21677 error_at (token->location, "schedule %<runtime%> does not take "
21678 "a %<chunk_size%> parameter");
21679 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
21680 error_at (token->location, "schedule %<auto%> does not take "
21681 "a %<chunk_size%> parameter");
21682 else
21683 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
21685 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21686 goto resync_fail;
21688 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
21689 goto resync_fail;
21691 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
21692 OMP_CLAUSE_CHAIN (c) = list;
21693 return c;
21695 invalid_kind:
21696 cp_parser_error (parser, "invalid schedule kind");
21697 resync_fail:
21698 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21699 /*or_comma=*/false,
21700 /*consume_paren=*/true);
21701 return list;
21704 /* OpenMP 3.0:
21705 untied */
21707 static tree
21708 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
21709 tree list, location_t location)
21711 tree c;
21713 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
21715 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
21716 OMP_CLAUSE_CHAIN (c) = list;
21717 return c;
21720 /* Parse all OpenMP clauses. The set clauses allowed by the directive
21721 is a bitmask in MASK. Return the list of clauses found; the result
21722 of clause default goes in *pdefault. */
21724 static tree
21725 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
21726 const char *where, cp_token *pragma_tok)
21728 tree clauses = NULL;
21729 bool first = true;
21730 cp_token *token = NULL;
21732 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
21734 pragma_omp_clause c_kind;
21735 const char *c_name;
21736 tree prev = clauses;
21738 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21739 cp_lexer_consume_token (parser->lexer);
21741 token = cp_lexer_peek_token (parser->lexer);
21742 c_kind = cp_parser_omp_clause_name (parser);
21743 first = false;
21745 switch (c_kind)
21747 case PRAGMA_OMP_CLAUSE_COLLAPSE:
21748 clauses = cp_parser_omp_clause_collapse (parser, clauses,
21749 token->location);
21750 c_name = "collapse";
21751 break;
21752 case PRAGMA_OMP_CLAUSE_COPYIN:
21753 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
21754 c_name = "copyin";
21755 break;
21756 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
21757 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
21758 clauses);
21759 c_name = "copyprivate";
21760 break;
21761 case PRAGMA_OMP_CLAUSE_DEFAULT:
21762 clauses = cp_parser_omp_clause_default (parser, clauses,
21763 token->location);
21764 c_name = "default";
21765 break;
21766 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
21767 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
21768 clauses);
21769 c_name = "firstprivate";
21770 break;
21771 case PRAGMA_OMP_CLAUSE_IF:
21772 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
21773 c_name = "if";
21774 break;
21775 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
21776 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
21777 clauses);
21778 c_name = "lastprivate";
21779 break;
21780 case PRAGMA_OMP_CLAUSE_NOWAIT:
21781 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
21782 c_name = "nowait";
21783 break;
21784 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
21785 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
21786 token->location);
21787 c_name = "num_threads";
21788 break;
21789 case PRAGMA_OMP_CLAUSE_ORDERED:
21790 clauses = cp_parser_omp_clause_ordered (parser, clauses,
21791 token->location);
21792 c_name = "ordered";
21793 break;
21794 case PRAGMA_OMP_CLAUSE_PRIVATE:
21795 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
21796 clauses);
21797 c_name = "private";
21798 break;
21799 case PRAGMA_OMP_CLAUSE_REDUCTION:
21800 clauses = cp_parser_omp_clause_reduction (parser, clauses);
21801 c_name = "reduction";
21802 break;
21803 case PRAGMA_OMP_CLAUSE_SCHEDULE:
21804 clauses = cp_parser_omp_clause_schedule (parser, clauses,
21805 token->location);
21806 c_name = "schedule";
21807 break;
21808 case PRAGMA_OMP_CLAUSE_SHARED:
21809 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
21810 clauses);
21811 c_name = "shared";
21812 break;
21813 case PRAGMA_OMP_CLAUSE_UNTIED:
21814 clauses = cp_parser_omp_clause_untied (parser, clauses,
21815 token->location);
21816 c_name = "nowait";
21817 break;
21818 default:
21819 cp_parser_error (parser, "expected %<#pragma omp%> clause");
21820 goto saw_error;
21823 if (((mask >> c_kind) & 1) == 0)
21825 /* Remove the invalid clause(s) from the list to avoid
21826 confusing the rest of the compiler. */
21827 clauses = prev;
21828 error_at (token->location, "%qs is not valid for %qs", c_name, where);
21831 saw_error:
21832 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
21833 return finish_omp_clauses (clauses);
21836 /* OpenMP 2.5:
21837 structured-block:
21838 statement
21840 In practice, we're also interested in adding the statement to an
21841 outer node. So it is convenient if we work around the fact that
21842 cp_parser_statement calls add_stmt. */
21844 static unsigned
21845 cp_parser_begin_omp_structured_block (cp_parser *parser)
21847 unsigned save = parser->in_statement;
21849 /* Only move the values to IN_OMP_BLOCK if they weren't false.
21850 This preserves the "not within loop or switch" style error messages
21851 for nonsense cases like
21852 void foo() {
21853 #pragma omp single
21854 break;
21857 if (parser->in_statement)
21858 parser->in_statement = IN_OMP_BLOCK;
21860 return save;
21863 static void
21864 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
21866 parser->in_statement = save;
21869 static tree
21870 cp_parser_omp_structured_block (cp_parser *parser)
21872 tree stmt = begin_omp_structured_block ();
21873 unsigned int save = cp_parser_begin_omp_structured_block (parser);
21875 cp_parser_statement (parser, NULL_TREE, false, NULL);
21877 cp_parser_end_omp_structured_block (parser, save);
21878 return finish_omp_structured_block (stmt);
21881 /* OpenMP 2.5:
21882 # pragma omp atomic new-line
21883 expression-stmt
21885 expression-stmt:
21886 x binop= expr | x++ | ++x | x-- | --x
21887 binop:
21888 +, *, -, /, &, ^, |, <<, >>
21890 where x is an lvalue expression with scalar type. */
21892 static void
21893 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
21895 tree lhs, rhs;
21896 enum tree_code code;
21898 cp_parser_require_pragma_eol (parser, pragma_tok);
21900 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
21901 /*cast_p=*/false, NULL);
21902 switch (TREE_CODE (lhs))
21904 case ERROR_MARK:
21905 goto saw_error;
21907 case PREINCREMENT_EXPR:
21908 case POSTINCREMENT_EXPR:
21909 lhs = TREE_OPERAND (lhs, 0);
21910 code = PLUS_EXPR;
21911 rhs = integer_one_node;
21912 break;
21914 case PREDECREMENT_EXPR:
21915 case POSTDECREMENT_EXPR:
21916 lhs = TREE_OPERAND (lhs, 0);
21917 code = MINUS_EXPR;
21918 rhs = integer_one_node;
21919 break;
21921 default:
21922 switch (cp_lexer_peek_token (parser->lexer)->type)
21924 case CPP_MULT_EQ:
21925 code = MULT_EXPR;
21926 break;
21927 case CPP_DIV_EQ:
21928 code = TRUNC_DIV_EXPR;
21929 break;
21930 case CPP_PLUS_EQ:
21931 code = PLUS_EXPR;
21932 break;
21933 case CPP_MINUS_EQ:
21934 code = MINUS_EXPR;
21935 break;
21936 case CPP_LSHIFT_EQ:
21937 code = LSHIFT_EXPR;
21938 break;
21939 case CPP_RSHIFT_EQ:
21940 code = RSHIFT_EXPR;
21941 break;
21942 case CPP_AND_EQ:
21943 code = BIT_AND_EXPR;
21944 break;
21945 case CPP_OR_EQ:
21946 code = BIT_IOR_EXPR;
21947 break;
21948 case CPP_XOR_EQ:
21949 code = BIT_XOR_EXPR;
21950 break;
21951 default:
21952 cp_parser_error (parser,
21953 "invalid operator for %<#pragma omp atomic%>");
21954 goto saw_error;
21956 cp_lexer_consume_token (parser->lexer);
21958 rhs = cp_parser_expression (parser, false, NULL);
21959 if (rhs == error_mark_node)
21960 goto saw_error;
21961 break;
21963 finish_omp_atomic (code, lhs, rhs);
21964 cp_parser_consume_semicolon_at_end_of_statement (parser);
21965 return;
21967 saw_error:
21968 cp_parser_skip_to_end_of_block_or_statement (parser);
21972 /* OpenMP 2.5:
21973 # pragma omp barrier new-line */
21975 static void
21976 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
21978 cp_parser_require_pragma_eol (parser, pragma_tok);
21979 finish_omp_barrier ();
21982 /* OpenMP 2.5:
21983 # pragma omp critical [(name)] new-line
21984 structured-block */
21986 static tree
21987 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
21989 tree stmt, name = NULL;
21991 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21993 cp_lexer_consume_token (parser->lexer);
21995 name = cp_parser_identifier (parser);
21997 if (name == error_mark_node
21998 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21999 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22000 /*or_comma=*/false,
22001 /*consume_paren=*/true);
22002 if (name == error_mark_node)
22003 name = NULL;
22005 cp_parser_require_pragma_eol (parser, pragma_tok);
22007 stmt = cp_parser_omp_structured_block (parser);
22008 return c_finish_omp_critical (input_location, stmt, name);
22011 /* OpenMP 2.5:
22012 # pragma omp flush flush-vars[opt] new-line
22014 flush-vars:
22015 ( variable-list ) */
22017 static void
22018 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
22020 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22021 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
22022 cp_parser_require_pragma_eol (parser, pragma_tok);
22024 finish_omp_flush ();
22027 /* Helper function, to parse omp for increment expression. */
22029 static tree
22030 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
22032 tree cond = cp_parser_binary_expression (parser, false, true,
22033 PREC_NOT_OPERATOR, NULL);
22034 bool overloaded_p;
22036 if (cond == error_mark_node
22037 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22039 cp_parser_skip_to_end_of_statement (parser);
22040 return error_mark_node;
22043 switch (TREE_CODE (cond))
22045 case GT_EXPR:
22046 case GE_EXPR:
22047 case LT_EXPR:
22048 case LE_EXPR:
22049 break;
22050 default:
22051 return error_mark_node;
22054 /* If decl is an iterator, preserve LHS and RHS of the relational
22055 expr until finish_omp_for. */
22056 if (decl
22057 && (type_dependent_expression_p (decl)
22058 || CLASS_TYPE_P (TREE_TYPE (decl))))
22059 return cond;
22061 return build_x_binary_op (TREE_CODE (cond),
22062 TREE_OPERAND (cond, 0), ERROR_MARK,
22063 TREE_OPERAND (cond, 1), ERROR_MARK,
22064 &overloaded_p, tf_warning_or_error);
22067 /* Helper function, to parse omp for increment expression. */
22069 static tree
22070 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
22072 cp_token *token = cp_lexer_peek_token (parser->lexer);
22073 enum tree_code op;
22074 tree lhs, rhs;
22075 cp_id_kind idk;
22076 bool decl_first;
22078 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
22080 op = (token->type == CPP_PLUS_PLUS
22081 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
22082 cp_lexer_consume_token (parser->lexer);
22083 lhs = cp_parser_cast_expression (parser, false, false, NULL);
22084 if (lhs != decl)
22085 return error_mark_node;
22086 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
22089 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
22090 if (lhs != decl)
22091 return error_mark_node;
22093 token = cp_lexer_peek_token (parser->lexer);
22094 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
22096 op = (token->type == CPP_PLUS_PLUS
22097 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
22098 cp_lexer_consume_token (parser->lexer);
22099 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
22102 op = cp_parser_assignment_operator_opt (parser);
22103 if (op == ERROR_MARK)
22104 return error_mark_node;
22106 if (op != NOP_EXPR)
22108 rhs = cp_parser_assignment_expression (parser, false, NULL);
22109 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
22110 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
22113 lhs = cp_parser_binary_expression (parser, false, false,
22114 PREC_ADDITIVE_EXPRESSION, NULL);
22115 token = cp_lexer_peek_token (parser->lexer);
22116 decl_first = lhs == decl;
22117 if (decl_first)
22118 lhs = NULL_TREE;
22119 if (token->type != CPP_PLUS
22120 && token->type != CPP_MINUS)
22121 return error_mark_node;
22125 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
22126 cp_lexer_consume_token (parser->lexer);
22127 rhs = cp_parser_binary_expression (parser, false, false,
22128 PREC_ADDITIVE_EXPRESSION, NULL);
22129 token = cp_lexer_peek_token (parser->lexer);
22130 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
22132 if (lhs == NULL_TREE)
22134 if (op == PLUS_EXPR)
22135 lhs = rhs;
22136 else
22137 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
22139 else
22140 lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
22141 NULL, tf_warning_or_error);
22144 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
22146 if (!decl_first)
22148 if (rhs != decl || op == MINUS_EXPR)
22149 return error_mark_node;
22150 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
22152 else
22153 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
22155 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
22158 /* Parse the restricted form of the for statement allowed by OpenMP. */
22160 static tree
22161 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
22163 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
22164 tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
22165 tree this_pre_body, cl;
22166 location_t loc_first;
22167 bool collapse_err = false;
22168 int i, collapse = 1, nbraces = 0;
22170 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
22171 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
22172 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
22174 gcc_assert (collapse >= 1);
22176 declv = make_tree_vec (collapse);
22177 initv = make_tree_vec (collapse);
22178 condv = make_tree_vec (collapse);
22179 incrv = make_tree_vec (collapse);
22181 loc_first = cp_lexer_peek_token (parser->lexer)->location;
22183 for (i = 0; i < collapse; i++)
22185 int bracecount = 0;
22186 bool add_private_clause = false;
22187 location_t loc;
22189 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22191 cp_parser_error (parser, "for statement expected");
22192 return NULL;
22194 loc = cp_lexer_consume_token (parser->lexer)->location;
22196 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
22197 return NULL;
22199 init = decl = real_decl = NULL;
22200 this_pre_body = push_stmt_list ();
22201 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22203 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
22205 init-expr:
22206 var = lb
22207 integer-type var = lb
22208 random-access-iterator-type var = lb
22209 pointer-type var = lb
22211 cp_decl_specifier_seq type_specifiers;
22213 /* First, try to parse as an initialized declaration. See
22214 cp_parser_condition, from whence the bulk of this is copied. */
22216 cp_parser_parse_tentatively (parser);
22217 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
22218 /*is_trailing_return=*/false,
22219 &type_specifiers);
22220 if (cp_parser_parse_definitely (parser))
22222 /* If parsing a type specifier seq succeeded, then this
22223 MUST be a initialized declaration. */
22224 tree asm_specification, attributes;
22225 cp_declarator *declarator;
22227 declarator = cp_parser_declarator (parser,
22228 CP_PARSER_DECLARATOR_NAMED,
22229 /*ctor_dtor_or_conv_p=*/NULL,
22230 /*parenthesized_p=*/NULL,
22231 /*member_p=*/false);
22232 attributes = cp_parser_attributes_opt (parser);
22233 asm_specification = cp_parser_asm_specification_opt (parser);
22235 if (declarator == cp_error_declarator)
22236 cp_parser_skip_to_end_of_statement (parser);
22238 else
22240 tree pushed_scope, auto_node;
22242 decl = start_decl (declarator, &type_specifiers,
22243 SD_INITIALIZED, attributes,
22244 /*prefix_attributes=*/NULL_TREE,
22245 &pushed_scope);
22247 auto_node = type_uses_auto (TREE_TYPE (decl));
22248 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
22250 if (cp_lexer_next_token_is (parser->lexer,
22251 CPP_OPEN_PAREN))
22252 error ("parenthesized initialization is not allowed in "
22253 "OpenMP %<for%> loop");
22254 else
22255 /* Trigger an error. */
22256 cp_parser_require (parser, CPP_EQ, "%<=%>");
22258 init = error_mark_node;
22259 cp_parser_skip_to_end_of_statement (parser);
22261 else if (CLASS_TYPE_P (TREE_TYPE (decl))
22262 || type_dependent_expression_p (decl)
22263 || auto_node)
22265 bool is_direct_init, is_non_constant_init;
22267 init = cp_parser_initializer (parser,
22268 &is_direct_init,
22269 &is_non_constant_init);
22271 if (auto_node && describable_type (init))
22273 TREE_TYPE (decl)
22274 = do_auto_deduction (TREE_TYPE (decl), init,
22275 auto_node);
22277 if (!CLASS_TYPE_P (TREE_TYPE (decl))
22278 && !type_dependent_expression_p (decl))
22279 goto non_class;
22282 cp_finish_decl (decl, init, !is_non_constant_init,
22283 asm_specification,
22284 LOOKUP_ONLYCONVERTING);
22285 if (CLASS_TYPE_P (TREE_TYPE (decl)))
22287 for_block
22288 = tree_cons (NULL, this_pre_body, for_block);
22289 init = NULL_TREE;
22291 else
22292 init = pop_stmt_list (this_pre_body);
22293 this_pre_body = NULL_TREE;
22295 else
22297 /* Consume '='. */
22298 cp_lexer_consume_token (parser->lexer);
22299 init = cp_parser_assignment_expression (parser, false, NULL);
22301 non_class:
22302 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
22303 init = error_mark_node;
22304 else
22305 cp_finish_decl (decl, NULL_TREE,
22306 /*init_const_expr_p=*/false,
22307 asm_specification,
22308 LOOKUP_ONLYCONVERTING);
22311 if (pushed_scope)
22312 pop_scope (pushed_scope);
22315 else
22317 cp_id_kind idk;
22318 /* If parsing a type specifier sequence failed, then
22319 this MUST be a simple expression. */
22320 cp_parser_parse_tentatively (parser);
22321 decl = cp_parser_primary_expression (parser, false, false,
22322 false, &idk);
22323 if (!cp_parser_error_occurred (parser)
22324 && decl
22325 && DECL_P (decl)
22326 && CLASS_TYPE_P (TREE_TYPE (decl)))
22328 tree rhs;
22330 cp_parser_parse_definitely (parser);
22331 cp_parser_require (parser, CPP_EQ, "%<=%>");
22332 rhs = cp_parser_assignment_expression (parser, false, NULL);
22333 finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
22334 rhs,
22335 tf_warning_or_error));
22336 add_private_clause = true;
22338 else
22340 decl = NULL;
22341 cp_parser_abort_tentative_parse (parser);
22342 init = cp_parser_expression (parser, false, NULL);
22343 if (init)
22345 if (TREE_CODE (init) == MODIFY_EXPR
22346 || TREE_CODE (init) == MODOP_EXPR)
22347 real_decl = TREE_OPERAND (init, 0);
22352 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
22353 if (this_pre_body)
22355 this_pre_body = pop_stmt_list (this_pre_body);
22356 if (pre_body)
22358 tree t = pre_body;
22359 pre_body = push_stmt_list ();
22360 add_stmt (t);
22361 add_stmt (this_pre_body);
22362 pre_body = pop_stmt_list (pre_body);
22364 else
22365 pre_body = this_pre_body;
22368 if (decl)
22369 real_decl = decl;
22370 if (par_clauses != NULL && real_decl != NULL_TREE)
22372 tree *c;
22373 for (c = par_clauses; *c ; )
22374 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
22375 && OMP_CLAUSE_DECL (*c) == real_decl)
22377 error_at (loc, "iteration variable %qD"
22378 " should not be firstprivate", real_decl);
22379 *c = OMP_CLAUSE_CHAIN (*c);
22381 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
22382 && OMP_CLAUSE_DECL (*c) == real_decl)
22384 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
22385 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
22386 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
22387 OMP_CLAUSE_DECL (l) = real_decl;
22388 OMP_CLAUSE_CHAIN (l) = clauses;
22389 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
22390 clauses = l;
22391 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
22392 CP_OMP_CLAUSE_INFO (*c) = NULL;
22393 add_private_clause = false;
22395 else
22397 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
22398 && OMP_CLAUSE_DECL (*c) == real_decl)
22399 add_private_clause = false;
22400 c = &OMP_CLAUSE_CHAIN (*c);
22404 if (add_private_clause)
22406 tree c;
22407 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
22409 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
22410 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
22411 && OMP_CLAUSE_DECL (c) == decl)
22412 break;
22413 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
22414 && OMP_CLAUSE_DECL (c) == decl)
22415 error_at (loc, "iteration variable %qD "
22416 "should not be firstprivate",
22417 decl);
22418 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
22419 && OMP_CLAUSE_DECL (c) == decl)
22420 error_at (loc, "iteration variable %qD should not be reduction",
22421 decl);
22423 if (c == NULL)
22425 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
22426 OMP_CLAUSE_DECL (c) = decl;
22427 c = finish_omp_clauses (c);
22428 if (c)
22430 OMP_CLAUSE_CHAIN (c) = clauses;
22431 clauses = c;
22436 cond = NULL;
22437 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22438 cond = cp_parser_omp_for_cond (parser, decl);
22439 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
22441 incr = NULL;
22442 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
22444 /* If decl is an iterator, preserve the operator on decl
22445 until finish_omp_for. */
22446 if (decl
22447 && (type_dependent_expression_p (decl)
22448 || CLASS_TYPE_P (TREE_TYPE (decl))))
22449 incr = cp_parser_omp_for_incr (parser, decl);
22450 else
22451 incr = cp_parser_expression (parser, false, NULL);
22454 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
22455 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22456 /*or_comma=*/false,
22457 /*consume_paren=*/true);
22459 TREE_VEC_ELT (declv, i) = decl;
22460 TREE_VEC_ELT (initv, i) = init;
22461 TREE_VEC_ELT (condv, i) = cond;
22462 TREE_VEC_ELT (incrv, i) = incr;
22464 if (i == collapse - 1)
22465 break;
22467 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
22468 in between the collapsed for loops to be still considered perfectly
22469 nested. Hopefully the final version clarifies this.
22470 For now handle (multiple) {'s and empty statements. */
22471 cp_parser_parse_tentatively (parser);
22474 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22475 break;
22476 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22478 cp_lexer_consume_token (parser->lexer);
22479 bracecount++;
22481 else if (bracecount
22482 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22483 cp_lexer_consume_token (parser->lexer);
22484 else
22486 loc = cp_lexer_peek_token (parser->lexer)->location;
22487 error_at (loc, "not enough collapsed for loops");
22488 collapse_err = true;
22489 cp_parser_abort_tentative_parse (parser);
22490 declv = NULL_TREE;
22491 break;
22494 while (1);
22496 if (declv)
22498 cp_parser_parse_definitely (parser);
22499 nbraces += bracecount;
22503 /* Note that we saved the original contents of this flag when we entered
22504 the structured block, and so we don't need to re-save it here. */
22505 parser->in_statement = IN_OMP_FOR;
22507 /* Note that the grammar doesn't call for a structured block here,
22508 though the loop as a whole is a structured block. */
22509 body = push_stmt_list ();
22510 cp_parser_statement (parser, NULL_TREE, false, NULL);
22511 body = pop_stmt_list (body);
22513 if (declv == NULL_TREE)
22514 ret = NULL_TREE;
22515 else
22516 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
22517 pre_body, clauses);
22519 while (nbraces)
22521 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
22523 cp_lexer_consume_token (parser->lexer);
22524 nbraces--;
22526 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22527 cp_lexer_consume_token (parser->lexer);
22528 else
22530 if (!collapse_err)
22532 error_at (cp_lexer_peek_token (parser->lexer)->location,
22533 "collapsed loops not perfectly nested");
22535 collapse_err = true;
22536 cp_parser_statement_seq_opt (parser, NULL);
22537 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
22538 break;
22542 while (for_block)
22544 add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
22545 for_block = TREE_CHAIN (for_block);
22548 return ret;
22551 /* OpenMP 2.5:
22552 #pragma omp for for-clause[optseq] new-line
22553 for-loop */
22555 #define OMP_FOR_CLAUSE_MASK \
22556 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22557 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22558 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
22559 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
22560 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
22561 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
22562 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
22563 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
22565 static tree
22566 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
22568 tree clauses, sb, ret;
22569 unsigned int save;
22571 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
22572 "#pragma omp for", pragma_tok);
22574 sb = begin_omp_structured_block ();
22575 save = cp_parser_begin_omp_structured_block (parser);
22577 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
22579 cp_parser_end_omp_structured_block (parser, save);
22580 add_stmt (finish_omp_structured_block (sb));
22582 return ret;
22585 /* OpenMP 2.5:
22586 # pragma omp master new-line
22587 structured-block */
22589 static tree
22590 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
22592 cp_parser_require_pragma_eol (parser, pragma_tok);
22593 return c_finish_omp_master (input_location,
22594 cp_parser_omp_structured_block (parser));
22597 /* OpenMP 2.5:
22598 # pragma omp ordered new-line
22599 structured-block */
22601 static tree
22602 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
22604 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22605 cp_parser_require_pragma_eol (parser, pragma_tok);
22606 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
22609 /* OpenMP 2.5:
22611 section-scope:
22612 { section-sequence }
22614 section-sequence:
22615 section-directive[opt] structured-block
22616 section-sequence section-directive structured-block */
22618 static tree
22619 cp_parser_omp_sections_scope (cp_parser *parser)
22621 tree stmt, substmt;
22622 bool error_suppress = false;
22623 cp_token *tok;
22625 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
22626 return NULL_TREE;
22628 stmt = push_stmt_list ();
22630 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
22632 unsigned save;
22634 substmt = begin_omp_structured_block ();
22635 save = cp_parser_begin_omp_structured_block (parser);
22637 while (1)
22639 cp_parser_statement (parser, NULL_TREE, false, NULL);
22641 tok = cp_lexer_peek_token (parser->lexer);
22642 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
22643 break;
22644 if (tok->type == CPP_CLOSE_BRACE)
22645 break;
22646 if (tok->type == CPP_EOF)
22647 break;
22650 cp_parser_end_omp_structured_block (parser, save);
22651 substmt = finish_omp_structured_block (substmt);
22652 substmt = build1 (OMP_SECTION, void_type_node, substmt);
22653 add_stmt (substmt);
22656 while (1)
22658 tok = cp_lexer_peek_token (parser->lexer);
22659 if (tok->type == CPP_CLOSE_BRACE)
22660 break;
22661 if (tok->type == CPP_EOF)
22662 break;
22664 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
22666 cp_lexer_consume_token (parser->lexer);
22667 cp_parser_require_pragma_eol (parser, tok);
22668 error_suppress = false;
22670 else if (!error_suppress)
22672 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
22673 error_suppress = true;
22676 substmt = cp_parser_omp_structured_block (parser);
22677 substmt = build1 (OMP_SECTION, void_type_node, substmt);
22678 add_stmt (substmt);
22680 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
22682 substmt = pop_stmt_list (stmt);
22684 stmt = make_node (OMP_SECTIONS);
22685 TREE_TYPE (stmt) = void_type_node;
22686 OMP_SECTIONS_BODY (stmt) = substmt;
22688 add_stmt (stmt);
22689 return stmt;
22692 /* OpenMP 2.5:
22693 # pragma omp sections sections-clause[optseq] newline
22694 sections-scope */
22696 #define OMP_SECTIONS_CLAUSE_MASK \
22697 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22698 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22699 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
22700 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
22701 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
22703 static tree
22704 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
22706 tree clauses, ret;
22708 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
22709 "#pragma omp sections", pragma_tok);
22711 ret = cp_parser_omp_sections_scope (parser);
22712 if (ret)
22713 OMP_SECTIONS_CLAUSES (ret) = clauses;
22715 return ret;
22718 /* OpenMP 2.5:
22719 # pragma parallel parallel-clause new-line
22720 # pragma parallel for parallel-for-clause new-line
22721 # pragma parallel sections parallel-sections-clause new-line */
22723 #define OMP_PARALLEL_CLAUSE_MASK \
22724 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
22725 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22726 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22727 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
22728 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
22729 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
22730 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
22731 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
22733 static tree
22734 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
22736 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
22737 const char *p_name = "#pragma omp parallel";
22738 tree stmt, clauses, par_clause, ws_clause, block;
22739 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
22740 unsigned int save;
22741 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22743 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22745 cp_lexer_consume_token (parser->lexer);
22746 p_kind = PRAGMA_OMP_PARALLEL_FOR;
22747 p_name = "#pragma omp parallel for";
22748 mask |= OMP_FOR_CLAUSE_MASK;
22749 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
22751 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22753 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
22754 const char *p = IDENTIFIER_POINTER (id);
22755 if (strcmp (p, "sections") == 0)
22757 cp_lexer_consume_token (parser->lexer);
22758 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
22759 p_name = "#pragma omp parallel sections";
22760 mask |= OMP_SECTIONS_CLAUSE_MASK;
22761 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
22765 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
22766 block = begin_omp_parallel ();
22767 save = cp_parser_begin_omp_structured_block (parser);
22769 switch (p_kind)
22771 case PRAGMA_OMP_PARALLEL:
22772 cp_parser_statement (parser, NULL_TREE, false, NULL);
22773 par_clause = clauses;
22774 break;
22776 case PRAGMA_OMP_PARALLEL_FOR:
22777 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
22778 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
22779 break;
22781 case PRAGMA_OMP_PARALLEL_SECTIONS:
22782 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
22783 stmt = cp_parser_omp_sections_scope (parser);
22784 if (stmt)
22785 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
22786 break;
22788 default:
22789 gcc_unreachable ();
22792 cp_parser_end_omp_structured_block (parser, save);
22793 stmt = finish_omp_parallel (par_clause, block);
22794 if (p_kind != PRAGMA_OMP_PARALLEL)
22795 OMP_PARALLEL_COMBINED (stmt) = 1;
22796 return stmt;
22799 /* OpenMP 2.5:
22800 # pragma omp single single-clause[optseq] new-line
22801 structured-block */
22803 #define OMP_SINGLE_CLAUSE_MASK \
22804 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22805 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22806 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
22807 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
22809 static tree
22810 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
22812 tree stmt = make_node (OMP_SINGLE);
22813 TREE_TYPE (stmt) = void_type_node;
22815 OMP_SINGLE_CLAUSES (stmt)
22816 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
22817 "#pragma omp single", pragma_tok);
22818 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
22820 return add_stmt (stmt);
22823 /* OpenMP 3.0:
22824 # pragma omp task task-clause[optseq] new-line
22825 structured-block */
22827 #define OMP_TASK_CLAUSE_MASK \
22828 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
22829 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
22830 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
22831 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22832 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22833 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
22835 static tree
22836 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
22838 tree clauses, block;
22839 unsigned int save;
22841 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
22842 "#pragma omp task", pragma_tok);
22843 block = begin_omp_task ();
22844 save = cp_parser_begin_omp_structured_block (parser);
22845 cp_parser_statement (parser, NULL_TREE, false, NULL);
22846 cp_parser_end_omp_structured_block (parser, save);
22847 return finish_omp_task (clauses, block);
22850 /* OpenMP 3.0:
22851 # pragma omp taskwait new-line */
22853 static void
22854 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
22856 cp_parser_require_pragma_eol (parser, pragma_tok);
22857 finish_omp_taskwait ();
22860 /* OpenMP 2.5:
22861 # pragma omp threadprivate (variable-list) */
22863 static void
22864 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
22866 tree vars;
22868 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
22869 cp_parser_require_pragma_eol (parser, pragma_tok);
22871 finish_omp_threadprivate (vars);
22874 /* Main entry point to OpenMP statement pragmas. */
22876 static void
22877 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
22879 tree stmt;
22881 switch (pragma_tok->pragma_kind)
22883 case PRAGMA_OMP_ATOMIC:
22884 cp_parser_omp_atomic (parser, pragma_tok);
22885 return;
22886 case PRAGMA_OMP_CRITICAL:
22887 stmt = cp_parser_omp_critical (parser, pragma_tok);
22888 break;
22889 case PRAGMA_OMP_FOR:
22890 stmt = cp_parser_omp_for (parser, pragma_tok);
22891 break;
22892 case PRAGMA_OMP_MASTER:
22893 stmt = cp_parser_omp_master (parser, pragma_tok);
22894 break;
22895 case PRAGMA_OMP_ORDERED:
22896 stmt = cp_parser_omp_ordered (parser, pragma_tok);
22897 break;
22898 case PRAGMA_OMP_PARALLEL:
22899 stmt = cp_parser_omp_parallel (parser, pragma_tok);
22900 break;
22901 case PRAGMA_OMP_SECTIONS:
22902 stmt = cp_parser_omp_sections (parser, pragma_tok);
22903 break;
22904 case PRAGMA_OMP_SINGLE:
22905 stmt = cp_parser_omp_single (parser, pragma_tok);
22906 break;
22907 case PRAGMA_OMP_TASK:
22908 stmt = cp_parser_omp_task (parser, pragma_tok);
22909 break;
22910 default:
22911 gcc_unreachable ();
22914 if (stmt)
22915 SET_EXPR_LOCATION (stmt, pragma_tok->location);
22918 /* The parser. */
22920 static GTY (()) cp_parser *the_parser;
22923 /* Special handling for the first token or line in the file. The first
22924 thing in the file might be #pragma GCC pch_preprocess, which loads a
22925 PCH file, which is a GC collection point. So we need to handle this
22926 first pragma without benefit of an existing lexer structure.
22928 Always returns one token to the caller in *FIRST_TOKEN. This is
22929 either the true first token of the file, or the first token after
22930 the initial pragma. */
22932 static void
22933 cp_parser_initial_pragma (cp_token *first_token)
22935 tree name = NULL;
22937 cp_lexer_get_preprocessor_token (NULL, first_token);
22938 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
22939 return;
22941 cp_lexer_get_preprocessor_token (NULL, first_token);
22942 if (first_token->type == CPP_STRING)
22944 name = first_token->u.value;
22946 cp_lexer_get_preprocessor_token (NULL, first_token);
22947 if (first_token->type != CPP_PRAGMA_EOL)
22948 error_at (first_token->location,
22949 "junk at end of %<#pragma GCC pch_preprocess%>");
22951 else
22952 error_at (first_token->location, "expected string literal");
22954 /* Skip to the end of the pragma. */
22955 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
22956 cp_lexer_get_preprocessor_token (NULL, first_token);
22958 /* Now actually load the PCH file. */
22959 if (name)
22960 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
22962 /* Read one more token to return to our caller. We have to do this
22963 after reading the PCH file in, since its pointers have to be
22964 live. */
22965 cp_lexer_get_preprocessor_token (NULL, first_token);
22968 /* Normal parsing of a pragma token. Here we can (and must) use the
22969 regular lexer. */
22971 static bool
22972 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
22974 cp_token *pragma_tok;
22975 unsigned int id;
22977 pragma_tok = cp_lexer_consume_token (parser->lexer);
22978 gcc_assert (pragma_tok->type == CPP_PRAGMA);
22979 parser->lexer->in_pragma = true;
22981 id = pragma_tok->pragma_kind;
22982 switch (id)
22984 case PRAGMA_GCC_PCH_PREPROCESS:
22985 error_at (pragma_tok->location,
22986 "%<#pragma GCC pch_preprocess%> must be first");
22987 break;
22989 case PRAGMA_OMP_BARRIER:
22990 switch (context)
22992 case pragma_compound:
22993 cp_parser_omp_barrier (parser, pragma_tok);
22994 return false;
22995 case pragma_stmt:
22996 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
22997 "used in compound statements");
22998 break;
22999 default:
23000 goto bad_stmt;
23002 break;
23004 case PRAGMA_OMP_FLUSH:
23005 switch (context)
23007 case pragma_compound:
23008 cp_parser_omp_flush (parser, pragma_tok);
23009 return false;
23010 case pragma_stmt:
23011 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
23012 "used in compound statements");
23013 break;
23014 default:
23015 goto bad_stmt;
23017 break;
23019 case PRAGMA_OMP_TASKWAIT:
23020 switch (context)
23022 case pragma_compound:
23023 cp_parser_omp_taskwait (parser, pragma_tok);
23024 return false;
23025 case pragma_stmt:
23026 error_at (pragma_tok->location,
23027 "%<#pragma omp taskwait%> may only be "
23028 "used in compound statements");
23029 break;
23030 default:
23031 goto bad_stmt;
23033 break;
23035 case PRAGMA_OMP_THREADPRIVATE:
23036 cp_parser_omp_threadprivate (parser, pragma_tok);
23037 return false;
23039 case PRAGMA_OMP_ATOMIC:
23040 case PRAGMA_OMP_CRITICAL:
23041 case PRAGMA_OMP_FOR:
23042 case PRAGMA_OMP_MASTER:
23043 case PRAGMA_OMP_ORDERED:
23044 case PRAGMA_OMP_PARALLEL:
23045 case PRAGMA_OMP_SECTIONS:
23046 case PRAGMA_OMP_SINGLE:
23047 case PRAGMA_OMP_TASK:
23048 if (context == pragma_external)
23049 goto bad_stmt;
23050 cp_parser_omp_construct (parser, pragma_tok);
23051 return true;
23053 case PRAGMA_OMP_SECTION:
23054 error_at (pragma_tok->location,
23055 "%<#pragma omp section%> may only be used in "
23056 "%<#pragma omp sections%> construct");
23057 break;
23059 default:
23060 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
23061 c_invoke_pragma_handler (id);
23062 break;
23064 bad_stmt:
23065 cp_parser_error (parser, "expected declaration specifiers");
23066 break;
23069 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
23070 return false;
23073 /* The interface the pragma parsers have to the lexer. */
23075 enum cpp_ttype
23076 pragma_lex (tree *value)
23078 cp_token *tok;
23079 enum cpp_ttype ret;
23081 tok = cp_lexer_peek_token (the_parser->lexer);
23083 ret = tok->type;
23084 *value = tok->u.value;
23086 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
23087 ret = CPP_EOF;
23088 else if (ret == CPP_STRING)
23089 *value = cp_parser_string_literal (the_parser, false, false);
23090 else
23092 cp_lexer_consume_token (the_parser->lexer);
23093 if (ret == CPP_KEYWORD)
23094 ret = CPP_NAME;
23097 return ret;
23101 /* External interface. */
23103 /* Parse one entire translation unit. */
23105 void
23106 c_parse_file (void)
23108 static bool already_called = false;
23110 if (already_called)
23112 sorry ("inter-module optimizations not implemented for C++");
23113 return;
23115 already_called = true;
23117 the_parser = cp_parser_new ();
23118 push_deferring_access_checks (flag_access_control
23119 ? dk_no_deferred : dk_no_check);
23120 cp_parser_translation_unit (the_parser);
23121 the_parser = NULL;
23124 #include "gt-cp-parser.h"