Merged r157653 through r157895 into branch.
[official-gcc.git] / gcc / cp / parser.c
blob6b119b6907e17a3eb0d84de10f7b0e882a6e21f0
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;
893 declarator->id_loc = UNKNOWN_LOCATION;
895 return declarator;
898 /* Make a declarator for a generalized identifier. If
899 QUALIFYING_SCOPE is non-NULL, the identifier is
900 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
901 UNQUALIFIED_NAME. SFK indicates the kind of special function this
902 is, if any. */
904 static cp_declarator *
905 make_id_declarator (tree qualifying_scope, tree unqualified_name,
906 special_function_kind sfk)
908 cp_declarator *declarator;
910 /* It is valid to write:
912 class C { void f(); };
913 typedef C D;
914 void D::f();
916 The standard is not clear about whether `typedef const C D' is
917 legal; as of 2002-09-15 the committee is considering that
918 question. EDG 3.0 allows that syntax. Therefore, we do as
919 well. */
920 if (qualifying_scope && TYPE_P (qualifying_scope))
921 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
923 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
924 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
925 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
927 declarator = make_declarator (cdk_id);
928 declarator->u.id.qualifying_scope = qualifying_scope;
929 declarator->u.id.unqualified_name = unqualified_name;
930 declarator->u.id.sfk = sfk;
932 return declarator;
935 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
936 of modifiers such as const or volatile to apply to the pointer
937 type, represented as identifiers. */
939 cp_declarator *
940 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
942 cp_declarator *declarator;
944 declarator = make_declarator (cdk_pointer);
945 declarator->declarator = target;
946 declarator->u.pointer.qualifiers = cv_qualifiers;
947 declarator->u.pointer.class_type = NULL_TREE;
948 if (target)
950 declarator->parameter_pack_p = target->parameter_pack_p;
951 target->parameter_pack_p = false;
953 else
954 declarator->parameter_pack_p = false;
956 return declarator;
959 /* Like make_pointer_declarator -- but for references. */
961 cp_declarator *
962 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
963 bool rvalue_ref)
965 cp_declarator *declarator;
967 declarator = make_declarator (cdk_reference);
968 declarator->declarator = target;
969 declarator->u.reference.qualifiers = cv_qualifiers;
970 declarator->u.reference.rvalue_ref = rvalue_ref;
971 if (target)
973 declarator->parameter_pack_p = target->parameter_pack_p;
974 target->parameter_pack_p = false;
976 else
977 declarator->parameter_pack_p = false;
979 return declarator;
982 /* Like make_pointer_declarator -- but for a pointer to a non-static
983 member of CLASS_TYPE. */
985 cp_declarator *
986 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
987 cp_declarator *pointee)
989 cp_declarator *declarator;
991 declarator = make_declarator (cdk_ptrmem);
992 declarator->declarator = pointee;
993 declarator->u.pointer.qualifiers = cv_qualifiers;
994 declarator->u.pointer.class_type = class_type;
996 if (pointee)
998 declarator->parameter_pack_p = pointee->parameter_pack_p;
999 pointee->parameter_pack_p = false;
1001 else
1002 declarator->parameter_pack_p = false;
1004 return declarator;
1007 /* Make a declarator for the function given by TARGET, with the
1008 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1009 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1010 indicates what exceptions can be thrown. */
1012 cp_declarator *
1013 make_call_declarator (cp_declarator *target,
1014 tree parms,
1015 cp_cv_quals cv_qualifiers,
1016 tree exception_specification,
1017 tree late_return_type)
1019 cp_declarator *declarator;
1021 declarator = make_declarator (cdk_function);
1022 declarator->declarator = target;
1023 declarator->u.function.parameters = parms;
1024 declarator->u.function.qualifiers = cv_qualifiers;
1025 declarator->u.function.exception_specification = exception_specification;
1026 declarator->u.function.late_return_type = late_return_type;
1027 if (target)
1029 declarator->parameter_pack_p = target->parameter_pack_p;
1030 target->parameter_pack_p = false;
1032 else
1033 declarator->parameter_pack_p = false;
1035 return declarator;
1038 /* Make a declarator for an array of BOUNDS elements, each of which is
1039 defined by ELEMENT. */
1041 cp_declarator *
1042 make_array_declarator (cp_declarator *element, tree bounds)
1044 cp_declarator *declarator;
1046 declarator = make_declarator (cdk_array);
1047 declarator->declarator = element;
1048 declarator->u.array.bounds = bounds;
1049 if (element)
1051 declarator->parameter_pack_p = element->parameter_pack_p;
1052 element->parameter_pack_p = false;
1054 else
1055 declarator->parameter_pack_p = false;
1057 return declarator;
1060 /* Determine whether the declarator we've seen so far can be a
1061 parameter pack, when followed by an ellipsis. */
1062 static bool
1063 declarator_can_be_parameter_pack (cp_declarator *declarator)
1065 /* Search for a declarator name, or any other declarator that goes
1066 after the point where the ellipsis could appear in a parameter
1067 pack. If we find any of these, then this declarator can not be
1068 made into a parameter pack. */
1069 bool found = false;
1070 while (declarator && !found)
1072 switch ((int)declarator->kind)
1074 case cdk_id:
1075 case cdk_array:
1076 found = true;
1077 break;
1079 case cdk_error:
1080 return true;
1082 default:
1083 declarator = declarator->declarator;
1084 break;
1088 return !found;
1091 cp_parameter_declarator *no_parameters;
1093 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1094 DECLARATOR and DEFAULT_ARGUMENT. */
1096 cp_parameter_declarator *
1097 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1098 cp_declarator *declarator,
1099 tree default_argument)
1101 cp_parameter_declarator *parameter;
1103 parameter = ((cp_parameter_declarator *)
1104 alloc_declarator (sizeof (cp_parameter_declarator)));
1105 parameter->next = NULL;
1106 if (decl_specifiers)
1107 parameter->decl_specifiers = *decl_specifiers;
1108 else
1109 clear_decl_specs (&parameter->decl_specifiers);
1110 parameter->declarator = declarator;
1111 parameter->default_argument = default_argument;
1112 parameter->ellipsis_p = false;
1114 return parameter;
1117 /* Returns true iff DECLARATOR is a declaration for a function. */
1119 static bool
1120 function_declarator_p (const cp_declarator *declarator)
1122 while (declarator)
1124 if (declarator->kind == cdk_function
1125 && declarator->declarator->kind == cdk_id)
1126 return true;
1127 if (declarator->kind == cdk_id
1128 || declarator->kind == cdk_error)
1129 return false;
1130 declarator = declarator->declarator;
1132 return false;
1135 /* The parser. */
1137 /* Overview
1138 --------
1140 A cp_parser parses the token stream as specified by the C++
1141 grammar. Its job is purely parsing, not semantic analysis. For
1142 example, the parser breaks the token stream into declarators,
1143 expressions, statements, and other similar syntactic constructs.
1144 It does not check that the types of the expressions on either side
1145 of an assignment-statement are compatible, or that a function is
1146 not declared with a parameter of type `void'.
1148 The parser invokes routines elsewhere in the compiler to perform
1149 semantic analysis and to build up the abstract syntax tree for the
1150 code processed.
1152 The parser (and the template instantiation code, which is, in a
1153 way, a close relative of parsing) are the only parts of the
1154 compiler that should be calling push_scope and pop_scope, or
1155 related functions. The parser (and template instantiation code)
1156 keeps track of what scope is presently active; everything else
1157 should simply honor that. (The code that generates static
1158 initializers may also need to set the scope, in order to check
1159 access control correctly when emitting the initializers.)
1161 Methodology
1162 -----------
1164 The parser is of the standard recursive-descent variety. Upcoming
1165 tokens in the token stream are examined in order to determine which
1166 production to use when parsing a non-terminal. Some C++ constructs
1167 require arbitrary look ahead to disambiguate. For example, it is
1168 impossible, in the general case, to tell whether a statement is an
1169 expression or declaration without scanning the entire statement.
1170 Therefore, the parser is capable of "parsing tentatively." When the
1171 parser is not sure what construct comes next, it enters this mode.
1172 Then, while we attempt to parse the construct, the parser queues up
1173 error messages, rather than issuing them immediately, and saves the
1174 tokens it consumes. If the construct is parsed successfully, the
1175 parser "commits", i.e., it issues any queued error messages and
1176 the tokens that were being preserved are permanently discarded.
1177 If, however, the construct is not parsed successfully, the parser
1178 rolls back its state completely so that it can resume parsing using
1179 a different alternative.
1181 Future Improvements
1182 -------------------
1184 The performance of the parser could probably be improved substantially.
1185 We could often eliminate the need to parse tentatively by looking ahead
1186 a little bit. In some places, this approach might not entirely eliminate
1187 the need to parse tentatively, but it might still speed up the average
1188 case. */
1190 /* Flags that are passed to some parsing functions. These values can
1191 be bitwise-ored together. */
1193 enum
1195 /* No flags. */
1196 CP_PARSER_FLAGS_NONE = 0x0,
1197 /* The construct is optional. If it is not present, then no error
1198 should be issued. */
1199 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1200 /* When parsing a type-specifier, treat user-defined type-names
1201 as non-type identifiers. */
1202 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1203 /* When parsing a type-specifier, do not try to parse a class-specifier
1204 or enum-specifier. */
1205 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4
1208 /* This type is used for parameters and variables which hold
1209 combinations of the above flags. */
1210 typedef int cp_parser_flags;
1212 /* The different kinds of declarators we want to parse. */
1214 typedef enum cp_parser_declarator_kind
1216 /* We want an abstract declarator. */
1217 CP_PARSER_DECLARATOR_ABSTRACT,
1218 /* We want a named declarator. */
1219 CP_PARSER_DECLARATOR_NAMED,
1220 /* We don't mind, but the name must be an unqualified-id. */
1221 CP_PARSER_DECLARATOR_EITHER
1222 } cp_parser_declarator_kind;
1224 /* The precedence values used to parse binary expressions. The minimum value
1225 of PREC must be 1, because zero is reserved to quickly discriminate
1226 binary operators from other tokens. */
1228 enum cp_parser_prec
1230 PREC_NOT_OPERATOR,
1231 PREC_LOGICAL_OR_EXPRESSION,
1232 PREC_LOGICAL_AND_EXPRESSION,
1233 PREC_INCLUSIVE_OR_EXPRESSION,
1234 PREC_EXCLUSIVE_OR_EXPRESSION,
1235 PREC_AND_EXPRESSION,
1236 PREC_EQUALITY_EXPRESSION,
1237 PREC_RELATIONAL_EXPRESSION,
1238 PREC_SHIFT_EXPRESSION,
1239 PREC_ADDITIVE_EXPRESSION,
1240 PREC_MULTIPLICATIVE_EXPRESSION,
1241 PREC_PM_EXPRESSION,
1242 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1245 /* A mapping from a token type to a corresponding tree node type, with a
1246 precedence value. */
1248 typedef struct cp_parser_binary_operations_map_node
1250 /* The token type. */
1251 enum cpp_ttype token_type;
1252 /* The corresponding tree code. */
1253 enum tree_code tree_type;
1254 /* The precedence of this operator. */
1255 enum cp_parser_prec prec;
1256 } cp_parser_binary_operations_map_node;
1258 /* The status of a tentative parse. */
1260 typedef enum cp_parser_status_kind
1262 /* No errors have occurred. */
1263 CP_PARSER_STATUS_KIND_NO_ERROR,
1264 /* An error has occurred. */
1265 CP_PARSER_STATUS_KIND_ERROR,
1266 /* We are committed to this tentative parse, whether or not an error
1267 has occurred. */
1268 CP_PARSER_STATUS_KIND_COMMITTED
1269 } cp_parser_status_kind;
1271 typedef struct cp_parser_expression_stack_entry
1273 /* Left hand side of the binary operation we are currently
1274 parsing. */
1275 tree lhs;
1276 /* Original tree code for left hand side, if it was a binary
1277 expression itself (used for -Wparentheses). */
1278 enum tree_code lhs_type;
1279 /* Tree code for the binary operation we are parsing. */
1280 enum tree_code tree_type;
1281 /* Precedence of the binary operation we are parsing. */
1282 enum cp_parser_prec prec;
1283 } cp_parser_expression_stack_entry;
1285 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1286 entries because precedence levels on the stack are monotonically
1287 increasing. */
1288 typedef struct cp_parser_expression_stack_entry
1289 cp_parser_expression_stack[NUM_PREC_VALUES];
1291 /* Context that is saved and restored when parsing tentatively. */
1292 typedef struct GTY (()) cp_parser_context {
1293 /* If this is a tentative parsing context, the status of the
1294 tentative parse. */
1295 enum cp_parser_status_kind status;
1296 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1297 that are looked up in this context must be looked up both in the
1298 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1299 the context of the containing expression. */
1300 tree object_type;
1302 /* The next parsing context in the stack. */
1303 struct cp_parser_context *next;
1304 } cp_parser_context;
1306 /* Prototypes. */
1308 /* Constructors and destructors. */
1310 static cp_parser_context *cp_parser_context_new
1311 (cp_parser_context *);
1313 /* Class variables. */
1315 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1317 /* The operator-precedence table used by cp_parser_binary_expression.
1318 Transformed into an associative array (binops_by_token) by
1319 cp_parser_new. */
1321 static const cp_parser_binary_operations_map_node binops[] = {
1322 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1323 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1325 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1326 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1327 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1329 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1330 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1332 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1333 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1335 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1336 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1337 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1338 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1340 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1341 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1343 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1345 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1347 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1349 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1351 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1354 /* The same as binops, but initialized by cp_parser_new so that
1355 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1356 for speed. */
1357 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1359 /* Constructors and destructors. */
1361 /* Construct a new context. The context below this one on the stack
1362 is given by NEXT. */
1364 static cp_parser_context *
1365 cp_parser_context_new (cp_parser_context* next)
1367 cp_parser_context *context;
1369 /* Allocate the storage. */
1370 if (cp_parser_context_free_list != NULL)
1372 /* Pull the first entry from the free list. */
1373 context = cp_parser_context_free_list;
1374 cp_parser_context_free_list = context->next;
1375 memset (context, 0, sizeof (*context));
1377 else
1378 context = GGC_CNEW (cp_parser_context);
1380 /* No errors have occurred yet in this context. */
1381 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1382 /* If this is not the bottommost context, copy information that we
1383 need from the previous context. */
1384 if (next)
1386 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1387 expression, then we are parsing one in this context, too. */
1388 context->object_type = next->object_type;
1389 /* Thread the stack. */
1390 context->next = next;
1393 return context;
1396 /* The cp_parser structure represents the C++ parser. */
1398 typedef struct GTY(()) cp_parser {
1399 /* The lexer from which we are obtaining tokens. */
1400 cp_lexer *lexer;
1402 /* The scope in which names should be looked up. If NULL_TREE, then
1403 we look up names in the scope that is currently open in the
1404 source program. If non-NULL, this is either a TYPE or
1405 NAMESPACE_DECL for the scope in which we should look. It can
1406 also be ERROR_MARK, when we've parsed a bogus scope.
1408 This value is not cleared automatically after a name is looked
1409 up, so we must be careful to clear it before starting a new look
1410 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1411 will look up `Z' in the scope of `X', rather than the current
1412 scope.) Unfortunately, it is difficult to tell when name lookup
1413 is complete, because we sometimes peek at a token, look it up,
1414 and then decide not to consume it. */
1415 tree scope;
1417 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1418 last lookup took place. OBJECT_SCOPE is used if an expression
1419 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1420 respectively. QUALIFYING_SCOPE is used for an expression of the
1421 form "X::Y"; it refers to X. */
1422 tree object_scope;
1423 tree qualifying_scope;
1425 /* A stack of parsing contexts. All but the bottom entry on the
1426 stack will be tentative contexts.
1428 We parse tentatively in order to determine which construct is in
1429 use in some situations. For example, in order to determine
1430 whether a statement is an expression-statement or a
1431 declaration-statement we parse it tentatively as a
1432 declaration-statement. If that fails, we then reparse the same
1433 token stream as an expression-statement. */
1434 cp_parser_context *context;
1436 /* True if we are parsing GNU C++. If this flag is not set, then
1437 GNU extensions are not recognized. */
1438 bool allow_gnu_extensions_p;
1440 /* TRUE if the `>' token should be interpreted as the greater-than
1441 operator. FALSE if it is the end of a template-id or
1442 template-parameter-list. In C++0x mode, this flag also applies to
1443 `>>' tokens, which are viewed as two consecutive `>' tokens when
1444 this flag is FALSE. */
1445 bool greater_than_is_operator_p;
1447 /* TRUE if default arguments are allowed within a parameter list
1448 that starts at this point. FALSE if only a gnu extension makes
1449 them permissible. */
1450 bool default_arg_ok_p;
1452 /* TRUE if we are parsing an integral constant-expression. See
1453 [expr.const] for a precise definition. */
1454 bool integral_constant_expression_p;
1456 /* TRUE if we are parsing an integral constant-expression -- but a
1457 non-constant expression should be permitted as well. This flag
1458 is used when parsing an array bound so that GNU variable-length
1459 arrays are tolerated. */
1460 bool allow_non_integral_constant_expression_p;
1462 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1463 been seen that makes the expression non-constant. */
1464 bool non_integral_constant_expression_p;
1466 /* TRUE if local variable names and `this' are forbidden in the
1467 current context. */
1468 bool local_variables_forbidden_p;
1470 /* TRUE if the declaration we are parsing is part of a
1471 linkage-specification of the form `extern string-literal
1472 declaration'. */
1473 bool in_unbraced_linkage_specification_p;
1475 /* TRUE if we are presently parsing a declarator, after the
1476 direct-declarator. */
1477 bool in_declarator_p;
1479 /* TRUE if we are presently parsing a template-argument-list. */
1480 bool in_template_argument_list_p;
1482 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1483 to IN_OMP_BLOCK if parsing OpenMP structured block and
1484 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1485 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1486 iteration-statement, OpenMP block or loop within that switch. */
1487 #define IN_SWITCH_STMT 1
1488 #define IN_ITERATION_STMT 2
1489 #define IN_OMP_BLOCK 4
1490 #define IN_OMP_FOR 8
1491 #define IN_IF_STMT 16
1492 unsigned char in_statement;
1494 /* TRUE if we are presently parsing the body of a switch statement.
1495 Note that this doesn't quite overlap with in_statement above.
1496 The difference relates to giving the right sets of error messages:
1497 "case not in switch" vs "break statement used with OpenMP...". */
1498 bool in_switch_statement_p;
1500 /* TRUE if we are parsing a type-id in an expression context. In
1501 such a situation, both "type (expr)" and "type (type)" are valid
1502 alternatives. */
1503 bool in_type_id_in_expr_p;
1505 /* TRUE if we are currently in a header file where declarations are
1506 implicitly extern "C". */
1507 bool implicit_extern_c;
1509 /* TRUE if strings in expressions should be translated to the execution
1510 character set. */
1511 bool translate_strings_p;
1513 /* TRUE if we are presently parsing the body of a function, but not
1514 a local class. */
1515 bool in_function_body;
1517 /* If non-NULL, then we are parsing a construct where new type
1518 definitions are not permitted. The string stored here will be
1519 issued as an error message if a type is defined. */
1520 const char *type_definition_forbidden_message;
1522 /* A list of lists. The outer list is a stack, used for member
1523 functions of local classes. At each level there are two sub-list,
1524 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1525 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1526 TREE_VALUE's. The functions are chained in reverse declaration
1527 order.
1529 The TREE_PURPOSE sublist contains those functions with default
1530 arguments that need post processing, and the TREE_VALUE sublist
1531 contains those functions with definitions that need post
1532 processing.
1534 These lists can only be processed once the outermost class being
1535 defined is complete. */
1536 tree unparsed_functions_queues;
1538 /* The number of classes whose definitions are currently in
1539 progress. */
1540 unsigned num_classes_being_defined;
1542 /* The number of template parameter lists that apply directly to the
1543 current declaration. */
1544 unsigned num_template_parameter_lists;
1545 } cp_parser;
1547 /* Prototypes. */
1549 /* Constructors and destructors. */
1551 static cp_parser *cp_parser_new
1552 (void);
1554 /* Routines to parse various constructs.
1556 Those that return `tree' will return the error_mark_node (rather
1557 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1558 Sometimes, they will return an ordinary node if error-recovery was
1559 attempted, even though a parse error occurred. So, to check
1560 whether or not a parse error occurred, you should always use
1561 cp_parser_error_occurred. If the construct is optional (indicated
1562 either by an `_opt' in the name of the function that does the
1563 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1564 the construct is not present. */
1566 /* Lexical conventions [gram.lex] */
1568 static tree cp_parser_identifier
1569 (cp_parser *);
1570 static tree cp_parser_string_literal
1571 (cp_parser *, bool, bool);
1573 /* Basic concepts [gram.basic] */
1575 static bool cp_parser_translation_unit
1576 (cp_parser *);
1578 /* Expressions [gram.expr] */
1580 static tree cp_parser_primary_expression
1581 (cp_parser *, bool, bool, bool, cp_id_kind *);
1582 static tree cp_parser_id_expression
1583 (cp_parser *, bool, bool, bool *, bool, bool);
1584 static tree cp_parser_unqualified_id
1585 (cp_parser *, bool, bool, bool, bool);
1586 static tree cp_parser_nested_name_specifier_opt
1587 (cp_parser *, bool, bool, bool, bool);
1588 static tree cp_parser_nested_name_specifier
1589 (cp_parser *, bool, bool, bool, bool);
1590 static tree cp_parser_qualifying_entity
1591 (cp_parser *, bool, bool, bool, bool, bool);
1592 static tree cp_parser_postfix_expression
1593 (cp_parser *, bool, bool, bool, cp_id_kind *);
1594 static tree cp_parser_postfix_open_square_expression
1595 (cp_parser *, tree, bool);
1596 static tree cp_parser_postfix_dot_deref_expression
1597 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1598 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1599 (cp_parser *, bool, bool, bool, bool *);
1600 static void cp_parser_pseudo_destructor_name
1601 (cp_parser *, tree *, tree *);
1602 static tree cp_parser_unary_expression
1603 (cp_parser *, bool, bool, cp_id_kind *);
1604 static enum tree_code cp_parser_unary_operator
1605 (cp_token *);
1606 static tree cp_parser_new_expression
1607 (cp_parser *);
1608 static VEC(tree,gc) *cp_parser_new_placement
1609 (cp_parser *);
1610 static tree cp_parser_new_type_id
1611 (cp_parser *, tree *);
1612 static cp_declarator *cp_parser_new_declarator_opt
1613 (cp_parser *);
1614 static cp_declarator *cp_parser_direct_new_declarator
1615 (cp_parser *);
1616 static VEC(tree,gc) *cp_parser_new_initializer
1617 (cp_parser *);
1618 static tree cp_parser_delete_expression
1619 (cp_parser *);
1620 static tree cp_parser_cast_expression
1621 (cp_parser *, bool, bool, cp_id_kind *);
1622 static tree cp_parser_binary_expression
1623 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1624 static tree cp_parser_question_colon_clause
1625 (cp_parser *, tree);
1626 static tree cp_parser_assignment_expression
1627 (cp_parser *, bool, cp_id_kind *);
1628 static enum tree_code cp_parser_assignment_operator_opt
1629 (cp_parser *);
1630 static tree cp_parser_expression
1631 (cp_parser *, bool, cp_id_kind *);
1632 static tree cp_parser_constant_expression
1633 (cp_parser *, bool, bool *);
1634 static tree cp_parser_builtin_offsetof
1635 (cp_parser *);
1636 static tree cp_parser_lambda_expression
1637 (cp_parser *);
1638 static void cp_parser_lambda_introducer
1639 (cp_parser *, tree);
1640 static void cp_parser_lambda_declarator_opt
1641 (cp_parser *, tree);
1642 static void cp_parser_lambda_body
1643 (cp_parser *, tree);
1645 /* Statements [gram.stmt.stmt] */
1647 static void cp_parser_statement
1648 (cp_parser *, tree, bool, bool *);
1649 static void cp_parser_label_for_labeled_statement
1650 (cp_parser *);
1651 static tree cp_parser_expression_statement
1652 (cp_parser *, tree);
1653 static tree cp_parser_compound_statement
1654 (cp_parser *, tree, bool);
1655 static void cp_parser_statement_seq_opt
1656 (cp_parser *, tree);
1657 static tree cp_parser_selection_statement
1658 (cp_parser *, bool *);
1659 static tree cp_parser_condition
1660 (cp_parser *);
1661 static tree cp_parser_iteration_statement
1662 (cp_parser *);
1663 static void cp_parser_for_init_statement
1664 (cp_parser *);
1665 static tree cp_parser_jump_statement
1666 (cp_parser *);
1667 static void cp_parser_declaration_statement
1668 (cp_parser *);
1670 static tree cp_parser_implicitly_scoped_statement
1671 (cp_parser *, bool *);
1672 static void cp_parser_already_scoped_statement
1673 (cp_parser *);
1675 /* Declarations [gram.dcl.dcl] */
1677 static void cp_parser_declaration_seq_opt
1678 (cp_parser *);
1679 static void cp_parser_declaration
1680 (cp_parser *);
1681 static void cp_parser_block_declaration
1682 (cp_parser *, bool);
1683 static void cp_parser_simple_declaration
1684 (cp_parser *, bool);
1685 static void cp_parser_decl_specifier_seq
1686 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1687 static tree cp_parser_storage_class_specifier_opt
1688 (cp_parser *);
1689 static tree cp_parser_function_specifier_opt
1690 (cp_parser *, cp_decl_specifier_seq *);
1691 static tree cp_parser_type_specifier
1692 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1693 int *, bool *);
1694 static tree cp_parser_simple_type_specifier
1695 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1696 static tree cp_parser_type_name
1697 (cp_parser *);
1698 static tree cp_parser_nonclass_name
1699 (cp_parser* parser);
1700 static tree cp_parser_elaborated_type_specifier
1701 (cp_parser *, bool, bool);
1702 static tree cp_parser_enum_specifier
1703 (cp_parser *);
1704 static void cp_parser_enumerator_list
1705 (cp_parser *, tree);
1706 static void cp_parser_enumerator_definition
1707 (cp_parser *, tree);
1708 static tree cp_parser_namespace_name
1709 (cp_parser *);
1710 static void cp_parser_namespace_definition
1711 (cp_parser *);
1712 static void cp_parser_namespace_body
1713 (cp_parser *);
1714 static tree cp_parser_qualified_namespace_specifier
1715 (cp_parser *);
1716 static void cp_parser_namespace_alias_definition
1717 (cp_parser *);
1718 static bool cp_parser_using_declaration
1719 (cp_parser *, bool);
1720 static void cp_parser_using_directive
1721 (cp_parser *);
1722 static void cp_parser_asm_definition
1723 (cp_parser *);
1724 static void cp_parser_linkage_specification
1725 (cp_parser *);
1726 static void cp_parser_static_assert
1727 (cp_parser *, bool);
1728 static tree cp_parser_decltype
1729 (cp_parser *);
1731 /* Declarators [gram.dcl.decl] */
1733 static tree cp_parser_init_declarator
1734 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1735 static cp_declarator *cp_parser_declarator
1736 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1737 static cp_declarator *cp_parser_direct_declarator
1738 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1739 static enum tree_code cp_parser_ptr_operator
1740 (cp_parser *, tree *, cp_cv_quals *);
1741 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1742 (cp_parser *);
1743 static tree cp_parser_late_return_type_opt
1744 (cp_parser *);
1745 static tree cp_parser_declarator_id
1746 (cp_parser *, bool);
1747 static tree cp_parser_type_id
1748 (cp_parser *);
1749 static tree cp_parser_template_type_arg
1750 (cp_parser *);
1751 static tree cp_parser_trailing_type_id (cp_parser *);
1752 static tree cp_parser_type_id_1
1753 (cp_parser *, bool, bool);
1754 static void cp_parser_type_specifier_seq
1755 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1756 static tree cp_parser_parameter_declaration_clause
1757 (cp_parser *);
1758 static tree cp_parser_parameter_declaration_list
1759 (cp_parser *, bool *);
1760 static cp_parameter_declarator *cp_parser_parameter_declaration
1761 (cp_parser *, bool, bool *);
1762 static tree cp_parser_default_argument
1763 (cp_parser *, bool);
1764 static void cp_parser_function_body
1765 (cp_parser *);
1766 static tree cp_parser_initializer
1767 (cp_parser *, bool *, bool *);
1768 static tree cp_parser_initializer_clause
1769 (cp_parser *, bool *);
1770 static tree cp_parser_braced_list
1771 (cp_parser*, bool*);
1772 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1773 (cp_parser *, bool *);
1775 static bool cp_parser_ctor_initializer_opt_and_function_body
1776 (cp_parser *);
1778 /* Classes [gram.class] */
1780 static tree cp_parser_class_name
1781 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1782 static tree cp_parser_class_specifier
1783 (cp_parser *);
1784 static tree cp_parser_class_head
1785 (cp_parser *, bool *, tree *, tree *);
1786 static enum tag_types cp_parser_class_key
1787 (cp_parser *);
1788 static void cp_parser_member_specification_opt
1789 (cp_parser *);
1790 static void cp_parser_member_declaration
1791 (cp_parser *);
1792 static tree cp_parser_pure_specifier
1793 (cp_parser *);
1794 static tree cp_parser_constant_initializer
1795 (cp_parser *);
1797 /* Derived classes [gram.class.derived] */
1799 static tree cp_parser_base_clause
1800 (cp_parser *);
1801 static tree cp_parser_base_specifier
1802 (cp_parser *);
1804 /* Special member functions [gram.special] */
1806 static tree cp_parser_conversion_function_id
1807 (cp_parser *);
1808 static tree cp_parser_conversion_type_id
1809 (cp_parser *);
1810 static cp_declarator *cp_parser_conversion_declarator_opt
1811 (cp_parser *);
1812 static bool cp_parser_ctor_initializer_opt
1813 (cp_parser *);
1814 static void cp_parser_mem_initializer_list
1815 (cp_parser *);
1816 static tree cp_parser_mem_initializer
1817 (cp_parser *);
1818 static tree cp_parser_mem_initializer_id
1819 (cp_parser *);
1821 /* Overloading [gram.over] */
1823 static tree cp_parser_operator_function_id
1824 (cp_parser *);
1825 static tree cp_parser_operator
1826 (cp_parser *);
1828 /* Templates [gram.temp] */
1830 static void cp_parser_template_declaration
1831 (cp_parser *, bool);
1832 static tree cp_parser_template_parameter_list
1833 (cp_parser *);
1834 static tree cp_parser_template_parameter
1835 (cp_parser *, bool *, bool *);
1836 static tree cp_parser_type_parameter
1837 (cp_parser *, bool *);
1838 static tree cp_parser_template_id
1839 (cp_parser *, bool, bool, bool);
1840 static tree cp_parser_template_name
1841 (cp_parser *, bool, bool, bool, bool *);
1842 static tree cp_parser_template_argument_list
1843 (cp_parser *);
1844 static tree cp_parser_template_argument
1845 (cp_parser *);
1846 static void cp_parser_explicit_instantiation
1847 (cp_parser *);
1848 static void cp_parser_explicit_specialization
1849 (cp_parser *);
1851 /* Exception handling [gram.exception] */
1853 static tree cp_parser_try_block
1854 (cp_parser *);
1855 static bool cp_parser_function_try_block
1856 (cp_parser *);
1857 static void cp_parser_handler_seq
1858 (cp_parser *);
1859 static void cp_parser_handler
1860 (cp_parser *);
1861 static tree cp_parser_exception_declaration
1862 (cp_parser *);
1863 static tree cp_parser_throw_expression
1864 (cp_parser *);
1865 static tree cp_parser_exception_specification_opt
1866 (cp_parser *);
1867 static tree cp_parser_type_id_list
1868 (cp_parser *);
1870 /* GNU Extensions */
1872 static tree cp_parser_asm_specification_opt
1873 (cp_parser *);
1874 static tree cp_parser_asm_operand_list
1875 (cp_parser *);
1876 static tree cp_parser_asm_clobber_list
1877 (cp_parser *);
1878 static tree cp_parser_asm_label_list
1879 (cp_parser *);
1880 static tree cp_parser_attributes_opt
1881 (cp_parser *);
1882 static tree cp_parser_attribute_list
1883 (cp_parser *);
1884 static bool cp_parser_extension_opt
1885 (cp_parser *, int *);
1886 static void cp_parser_label_declaration
1887 (cp_parser *);
1889 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1890 static bool cp_parser_pragma
1891 (cp_parser *, enum pragma_context);
1893 /* Objective-C++ Productions */
1895 static tree cp_parser_objc_message_receiver
1896 (cp_parser *);
1897 static tree cp_parser_objc_message_args
1898 (cp_parser *);
1899 static tree cp_parser_objc_message_expression
1900 (cp_parser *);
1901 static tree cp_parser_objc_encode_expression
1902 (cp_parser *);
1903 static tree cp_parser_objc_defs_expression
1904 (cp_parser *);
1905 static tree cp_parser_objc_protocol_expression
1906 (cp_parser *);
1907 static tree cp_parser_objc_selector_expression
1908 (cp_parser *);
1909 static tree cp_parser_objc_expression
1910 (cp_parser *);
1911 static bool cp_parser_objc_selector_p
1912 (enum cpp_ttype);
1913 static tree cp_parser_objc_selector
1914 (cp_parser *);
1915 static tree cp_parser_objc_protocol_refs_opt
1916 (cp_parser *);
1917 static void cp_parser_objc_declaration
1918 (cp_parser *);
1919 static tree cp_parser_objc_statement
1920 (cp_parser *);
1922 /* Utility Routines */
1924 static tree cp_parser_lookup_name
1925 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1926 static tree cp_parser_lookup_name_simple
1927 (cp_parser *, tree, location_t);
1928 static tree cp_parser_maybe_treat_template_as_class
1929 (tree, bool);
1930 static bool cp_parser_check_declarator_template_parameters
1931 (cp_parser *, cp_declarator *, location_t);
1932 static bool cp_parser_check_template_parameters
1933 (cp_parser *, unsigned, location_t, cp_declarator *);
1934 static tree cp_parser_simple_cast_expression
1935 (cp_parser *);
1936 static tree cp_parser_global_scope_opt
1937 (cp_parser *, bool);
1938 static bool cp_parser_constructor_declarator_p
1939 (cp_parser *, bool);
1940 static tree cp_parser_function_definition_from_specifiers_and_declarator
1941 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1942 static tree cp_parser_function_definition_after_declarator
1943 (cp_parser *, bool);
1944 static void cp_parser_template_declaration_after_export
1945 (cp_parser *, bool);
1946 static void cp_parser_perform_template_parameter_access_checks
1947 (VEC (deferred_access_check,gc)*);
1948 static tree cp_parser_single_declaration
1949 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1950 static tree cp_parser_functional_cast
1951 (cp_parser *, tree);
1952 static tree cp_parser_save_member_function_body
1953 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1954 static tree cp_parser_enclosed_template_argument_list
1955 (cp_parser *);
1956 static void cp_parser_save_default_args
1957 (cp_parser *, tree);
1958 static void cp_parser_late_parsing_for_member
1959 (cp_parser *, tree);
1960 static void cp_parser_late_parsing_default_args
1961 (cp_parser *, tree);
1962 static tree cp_parser_sizeof_operand
1963 (cp_parser *, enum rid);
1964 static tree cp_parser_trait_expr
1965 (cp_parser *, enum rid);
1966 static bool cp_parser_declares_only_class_p
1967 (cp_parser *);
1968 static void cp_parser_set_storage_class
1969 (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1970 static void cp_parser_set_decl_spec_type
1971 (cp_decl_specifier_seq *, tree, location_t, bool);
1972 static bool cp_parser_friend_p
1973 (const cp_decl_specifier_seq *);
1974 static cp_token *cp_parser_require
1975 (cp_parser *, enum cpp_ttype, const char *);
1976 static cp_token *cp_parser_require_keyword
1977 (cp_parser *, enum rid, const char *);
1978 static bool cp_parser_token_starts_function_definition_p
1979 (cp_token *);
1980 static bool cp_parser_next_token_starts_class_definition_p
1981 (cp_parser *);
1982 static bool cp_parser_next_token_ends_template_argument_p
1983 (cp_parser *);
1984 static bool cp_parser_nth_token_starts_template_argument_list_p
1985 (cp_parser *, size_t);
1986 static enum tag_types cp_parser_token_is_class_key
1987 (cp_token *);
1988 static void cp_parser_check_class_key
1989 (enum tag_types, tree type);
1990 static void cp_parser_check_access_in_redeclaration
1991 (tree type, location_t location);
1992 static bool cp_parser_optional_template_keyword
1993 (cp_parser *);
1994 static void cp_parser_pre_parsed_nested_name_specifier
1995 (cp_parser *);
1996 static bool cp_parser_cache_group
1997 (cp_parser *, enum cpp_ttype, unsigned);
1998 static void cp_parser_parse_tentatively
1999 (cp_parser *);
2000 static void cp_parser_commit_to_tentative_parse
2001 (cp_parser *);
2002 static void cp_parser_abort_tentative_parse
2003 (cp_parser *);
2004 static bool cp_parser_parse_definitely
2005 (cp_parser *);
2006 static inline bool cp_parser_parsing_tentatively
2007 (cp_parser *);
2008 static bool cp_parser_uncommitted_to_tentative_parse_p
2009 (cp_parser *);
2010 static void cp_parser_error
2011 (cp_parser *, const char *);
2012 static void cp_parser_name_lookup_error
2013 (cp_parser *, tree, tree, const char *, location_t);
2014 static bool cp_parser_simulate_error
2015 (cp_parser *);
2016 static bool cp_parser_check_type_definition
2017 (cp_parser *);
2018 static void cp_parser_check_for_definition_in_return_type
2019 (cp_declarator *, tree, location_t type_location);
2020 static void cp_parser_check_for_invalid_template_id
2021 (cp_parser *, tree, location_t location);
2022 static bool cp_parser_non_integral_constant_expression
2023 (cp_parser *, const char *);
2024 static void cp_parser_diagnose_invalid_type_name
2025 (cp_parser *, tree, tree, location_t);
2026 static bool cp_parser_parse_and_diagnose_invalid_type_name
2027 (cp_parser *);
2028 static int cp_parser_skip_to_closing_parenthesis
2029 (cp_parser *, bool, bool, bool);
2030 static void cp_parser_skip_to_end_of_statement
2031 (cp_parser *);
2032 static void cp_parser_consume_semicolon_at_end_of_statement
2033 (cp_parser *);
2034 static void cp_parser_skip_to_end_of_block_or_statement
2035 (cp_parser *);
2036 static bool cp_parser_skip_to_closing_brace
2037 (cp_parser *);
2038 static void cp_parser_skip_to_end_of_template_parameter_list
2039 (cp_parser *);
2040 static void cp_parser_skip_to_pragma_eol
2041 (cp_parser*, cp_token *);
2042 static bool cp_parser_error_occurred
2043 (cp_parser *);
2044 static bool cp_parser_allow_gnu_extensions_p
2045 (cp_parser *);
2046 static bool cp_parser_is_string_literal
2047 (cp_token *);
2048 static bool cp_parser_is_keyword
2049 (cp_token *, enum rid);
2050 static tree cp_parser_make_typename_type
2051 (cp_parser *, tree, tree, location_t location);
2052 static cp_declarator * cp_parser_make_indirect_declarator
2053 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2055 /* Returns nonzero if we are parsing tentatively. */
2057 static inline bool
2058 cp_parser_parsing_tentatively (cp_parser* parser)
2060 return parser->context->next != NULL;
2063 /* Returns nonzero if TOKEN is a string literal. */
2065 static bool
2066 cp_parser_is_string_literal (cp_token* token)
2068 return (token->type == CPP_STRING ||
2069 token->type == CPP_STRING16 ||
2070 token->type == CPP_STRING32 ||
2071 token->type == CPP_WSTRING ||
2072 token->type == CPP_UTF8STRING);
2075 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2077 static bool
2078 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2080 return token->keyword == keyword;
2083 /* If not parsing tentatively, issue a diagnostic of the form
2084 FILE:LINE: MESSAGE before TOKEN
2085 where TOKEN is the next token in the input stream. MESSAGE
2086 (specified by the caller) is usually of the form "expected
2087 OTHER-TOKEN". */
2089 static void
2090 cp_parser_error (cp_parser* parser, const char* message)
2092 if (!cp_parser_simulate_error (parser))
2094 cp_token *token = cp_lexer_peek_token (parser->lexer);
2095 /* This diagnostic makes more sense if it is tagged to the line
2096 of the token we just peeked at. */
2097 cp_lexer_set_source_position_from_token (token);
2099 if (token->type == CPP_PRAGMA)
2101 error_at (token->location,
2102 "%<#pragma%> is not allowed here");
2103 cp_parser_skip_to_pragma_eol (parser, token);
2104 return;
2107 c_parse_error (message,
2108 /* Because c_parser_error does not understand
2109 CPP_KEYWORD, keywords are treated like
2110 identifiers. */
2111 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2112 token->u.value, token->flags);
2116 /* Issue an error about name-lookup failing. NAME is the
2117 IDENTIFIER_NODE DECL is the result of
2118 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2119 the thing that we hoped to find. */
2121 static void
2122 cp_parser_name_lookup_error (cp_parser* parser,
2123 tree name,
2124 tree decl,
2125 const char* desired,
2126 location_t location)
2128 /* If name lookup completely failed, tell the user that NAME was not
2129 declared. */
2130 if (decl == error_mark_node)
2132 if (parser->scope && parser->scope != global_namespace)
2133 error_at (location, "%<%E::%E%> has not been declared",
2134 parser->scope, name);
2135 else if (parser->scope == global_namespace)
2136 error_at (location, "%<::%E%> has not been declared", name);
2137 else if (parser->object_scope
2138 && !CLASS_TYPE_P (parser->object_scope))
2139 error_at (location, "request for member %qE in non-class type %qT",
2140 name, parser->object_scope);
2141 else if (parser->object_scope)
2142 error_at (location, "%<%T::%E%> has not been declared",
2143 parser->object_scope, name);
2144 else
2145 error_at (location, "%qE has not been declared", name);
2147 else if (parser->scope && parser->scope != global_namespace)
2148 error_at (location, "%<%E::%E%> %s", parser->scope, name, desired);
2149 else if (parser->scope == global_namespace)
2150 error_at (location, "%<::%E%> %s", name, desired);
2151 else
2152 error_at (location, "%qE %s", name, desired);
2155 /* If we are parsing tentatively, remember that an error has occurred
2156 during this tentative parse. Returns true if the error was
2157 simulated; false if a message should be issued by the caller. */
2159 static bool
2160 cp_parser_simulate_error (cp_parser* parser)
2162 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2164 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2165 return true;
2167 return false;
2170 /* Check for repeated decl-specifiers. */
2172 static void
2173 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2174 location_t location)
2176 int ds;
2178 for (ds = ds_first; ds != ds_last; ++ds)
2180 unsigned count = decl_specs->specs[ds];
2181 if (count < 2)
2182 continue;
2183 /* The "long" specifier is a special case because of "long long". */
2184 if (ds == ds_long)
2186 if (count > 2)
2187 error_at (location, "%<long long long%> is too long for GCC");
2188 else
2189 pedwarn_cxx98 (location, OPT_Wlong_long,
2190 "ISO C++ 1998 does not support %<long long%>");
2192 else if (count > 1)
2194 static const char *const decl_spec_names[] = {
2195 "signed",
2196 "unsigned",
2197 "short",
2198 "long",
2199 "const",
2200 "volatile",
2201 "restrict",
2202 "inline",
2203 "virtual",
2204 "explicit",
2205 "friend",
2206 "typedef",
2207 "constexpr",
2208 "__complex",
2209 "__thread"
2211 error_at (location, "duplicate %qs", decl_spec_names[ds]);
2216 /* This function is called when a type is defined. If type
2217 definitions are forbidden at this point, an error message is
2218 issued. */
2220 static bool
2221 cp_parser_check_type_definition (cp_parser* parser)
2223 /* If types are forbidden here, issue a message. */
2224 if (parser->type_definition_forbidden_message)
2226 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2227 in the message need to be interpreted. */
2228 error (parser->type_definition_forbidden_message);
2229 return false;
2231 return true;
2234 /* This function is called when the DECLARATOR is processed. The TYPE
2235 was a type defined in the decl-specifiers. If it is invalid to
2236 define a type in the decl-specifiers for DECLARATOR, an error is
2237 issued. TYPE_LOCATION is the location of TYPE and is used
2238 for error reporting. */
2240 static void
2241 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2242 tree type, location_t type_location)
2244 /* [dcl.fct] forbids type definitions in return types.
2245 Unfortunately, it's not easy to know whether or not we are
2246 processing a return type until after the fact. */
2247 while (declarator
2248 && (declarator->kind == cdk_pointer
2249 || declarator->kind == cdk_reference
2250 || declarator->kind == cdk_ptrmem))
2251 declarator = declarator->declarator;
2252 if (declarator
2253 && declarator->kind == cdk_function)
2255 error_at (type_location,
2256 "new types may not be defined in a return type");
2257 inform (type_location,
2258 "(perhaps a semicolon is missing after the definition of %qT)",
2259 type);
2263 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2264 "<" in any valid C++ program. If the next token is indeed "<",
2265 issue a message warning the user about what appears to be an
2266 invalid attempt to form a template-id. LOCATION is the location
2267 of the type-specifier (TYPE) */
2269 static void
2270 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2271 tree type, location_t location)
2273 cp_token_position start = 0;
2275 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2277 if (TYPE_P (type))
2278 error_at (location, "%qT is not a template", type);
2279 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2280 error_at (location, "%qE is not a template", type);
2281 else
2282 error_at (location, "invalid template-id");
2283 /* Remember the location of the invalid "<". */
2284 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2285 start = cp_lexer_token_position (parser->lexer, true);
2286 /* Consume the "<". */
2287 cp_lexer_consume_token (parser->lexer);
2288 /* Parse the template arguments. */
2289 cp_parser_enclosed_template_argument_list (parser);
2290 /* Permanently remove the invalid template arguments so that
2291 this error message is not issued again. */
2292 if (start)
2293 cp_lexer_purge_tokens_after (parser->lexer, start);
2297 /* If parsing an integral constant-expression, issue an error message
2298 about the fact that THING appeared and return true. Otherwise,
2299 return false. In either case, set
2300 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2302 static bool
2303 cp_parser_non_integral_constant_expression (cp_parser *parser,
2304 const char *thing)
2306 parser->non_integral_constant_expression_p = true;
2307 if (parser->integral_constant_expression_p)
2309 if (!parser->allow_non_integral_constant_expression_p)
2311 /* Don't use `%s' to print THING, because quotations (`%<', `%>')
2312 in the message need to be interpreted. */
2313 char *message = concat (thing,
2314 " cannot appear in a constant-expression",
2315 NULL);
2316 error (message);
2317 free (message);
2318 return true;
2321 return false;
2324 /* Emit a diagnostic for an invalid type name. SCOPE is the
2325 qualifying scope (or NULL, if none) for ID. This function commits
2326 to the current active tentative parse, if any. (Otherwise, the
2327 problematic construct might be encountered again later, resulting
2328 in duplicate error messages.) LOCATION is the location of ID. */
2330 static void
2331 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2332 tree scope, tree id,
2333 location_t location)
2335 tree decl, old_scope;
2336 /* Try to lookup the identifier. */
2337 old_scope = parser->scope;
2338 parser->scope = scope;
2339 decl = cp_parser_lookup_name_simple (parser, id, location);
2340 parser->scope = old_scope;
2341 /* If the lookup found a template-name, it means that the user forgot
2342 to specify an argument list. Emit a useful error message. */
2343 if (TREE_CODE (decl) == TEMPLATE_DECL)
2344 error_at (location,
2345 "invalid use of template-name %qE without an argument list",
2346 decl);
2347 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2348 error_at (location, "invalid use of destructor %qD as a type", id);
2349 else if (TREE_CODE (decl) == TYPE_DECL)
2350 /* Something like 'unsigned A a;' */
2351 error_at (location, "invalid combination of multiple type-specifiers");
2352 else if (!parser->scope)
2354 /* Issue an error message. */
2355 error_at (location, "%qE does not name a type", id);
2356 /* If we're in a template class, it's possible that the user was
2357 referring to a type from a base class. For example:
2359 template <typename T> struct A { typedef T X; };
2360 template <typename T> struct B : public A<T> { X x; };
2362 The user should have said "typename A<T>::X". */
2363 if (processing_template_decl && current_class_type
2364 && TYPE_BINFO (current_class_type))
2366 tree b;
2368 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2370 b = TREE_CHAIN (b))
2372 tree base_type = BINFO_TYPE (b);
2373 if (CLASS_TYPE_P (base_type)
2374 && dependent_type_p (base_type))
2376 tree field;
2377 /* Go from a particular instantiation of the
2378 template (which will have an empty TYPE_FIELDs),
2379 to the main version. */
2380 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2381 for (field = TYPE_FIELDS (base_type);
2382 field;
2383 field = TREE_CHAIN (field))
2384 if (TREE_CODE (field) == TYPE_DECL
2385 && DECL_NAME (field) == id)
2387 inform (location,
2388 "(perhaps %<typename %T::%E%> was intended)",
2389 BINFO_TYPE (b), id);
2390 break;
2392 if (field)
2393 break;
2398 /* Here we diagnose qualified-ids where the scope is actually correct,
2399 but the identifier does not resolve to a valid type name. */
2400 else if (parser->scope != error_mark_node)
2402 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2403 error_at (location, "%qE in namespace %qE does not name a type",
2404 id, parser->scope);
2405 else if (CLASS_TYPE_P (parser->scope)
2406 && constructor_name_p (id, parser->scope))
2408 /* A<T>::A<T>() */
2409 error_at (location, "%<%T::%E%> names the constructor, not"
2410 " the type", parser->scope, id);
2411 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2412 error_at (location, "and %qT has no template constructors",
2413 parser->scope);
2415 else if (TYPE_P (parser->scope)
2416 && dependent_scope_p (parser->scope))
2417 error_at (location, "need %<typename%> before %<%T::%E%> because "
2418 "%qT is a dependent scope",
2419 parser->scope, id, parser->scope);
2420 else if (TYPE_P (parser->scope))
2421 error_at (location, "%qE in class %qT does not name a type",
2422 id, parser->scope);
2423 else
2424 gcc_unreachable ();
2426 cp_parser_commit_to_tentative_parse (parser);
2429 /* Check for a common situation where a type-name should be present,
2430 but is not, and issue a sensible error message. Returns true if an
2431 invalid type-name was detected.
2433 The situation handled by this function are variable declarations of the
2434 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2435 Usually, `ID' should name a type, but if we got here it means that it
2436 does not. We try to emit the best possible error message depending on
2437 how exactly the id-expression looks like. */
2439 static bool
2440 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2442 tree id;
2443 cp_token *token = cp_lexer_peek_token (parser->lexer);
2445 /* Avoid duplicate error about ambiguous lookup. */
2446 if (token->type == CPP_NESTED_NAME_SPECIFIER)
2448 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2449 if (next->type == CPP_NAME && next->ambiguous_p)
2450 goto out;
2453 cp_parser_parse_tentatively (parser);
2454 id = cp_parser_id_expression (parser,
2455 /*template_keyword_p=*/false,
2456 /*check_dependency_p=*/true,
2457 /*template_p=*/NULL,
2458 /*declarator_p=*/true,
2459 /*optional_p=*/false);
2460 /* If the next token is a (, this is a function with no explicit return
2461 type, i.e. constructor, destructor or conversion op. */
2462 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2463 || TREE_CODE (id) == TYPE_DECL)
2465 cp_parser_abort_tentative_parse (parser);
2466 return false;
2468 if (!cp_parser_parse_definitely (parser))
2469 return false;
2471 /* Emit a diagnostic for the invalid type. */
2472 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2473 id, token->location);
2474 out:
2475 /* If we aren't in the middle of a declarator (i.e. in a
2476 parameter-declaration-clause), skip to the end of the declaration;
2477 there's no point in trying to process it. */
2478 if (!parser->in_declarator_p)
2479 cp_parser_skip_to_end_of_block_or_statement (parser);
2480 return true;
2483 /* Consume tokens up to, and including, the next non-nested closing `)'.
2484 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2485 are doing error recovery. Returns -1 if OR_COMMA is true and we
2486 found an unnested comma. */
2488 static int
2489 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2490 bool recovering,
2491 bool or_comma,
2492 bool consume_paren)
2494 unsigned paren_depth = 0;
2495 unsigned brace_depth = 0;
2496 unsigned square_depth = 0;
2498 if (recovering && !or_comma
2499 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2500 return 0;
2502 while (true)
2504 cp_token * token = cp_lexer_peek_token (parser->lexer);
2506 switch (token->type)
2508 case CPP_EOF:
2509 case CPP_PRAGMA_EOL:
2510 /* If we've run out of tokens, then there is no closing `)'. */
2511 return 0;
2513 /* This is good for lambda expression capture-lists. */
2514 case CPP_OPEN_SQUARE:
2515 ++square_depth;
2516 break;
2517 case CPP_CLOSE_SQUARE:
2518 if (!square_depth--)
2519 return 0;
2520 break;
2522 case CPP_SEMICOLON:
2523 /* This matches the processing in skip_to_end_of_statement. */
2524 if (!brace_depth)
2525 return 0;
2526 break;
2528 case CPP_OPEN_BRACE:
2529 ++brace_depth;
2530 break;
2531 case CPP_CLOSE_BRACE:
2532 if (!brace_depth--)
2533 return 0;
2534 break;
2536 case CPP_COMMA:
2537 if (recovering && or_comma && !brace_depth && !paren_depth
2538 && !square_depth)
2539 return -1;
2540 break;
2542 case CPP_OPEN_PAREN:
2543 if (!brace_depth)
2544 ++paren_depth;
2545 break;
2547 case CPP_CLOSE_PAREN:
2548 if (!brace_depth && !paren_depth--)
2550 if (consume_paren)
2551 cp_lexer_consume_token (parser->lexer);
2552 return 1;
2554 break;
2556 default:
2557 break;
2560 /* Consume the token. */
2561 cp_lexer_consume_token (parser->lexer);
2565 /* Consume tokens until we reach the end of the current statement.
2566 Normally, that will be just before consuming a `;'. However, if a
2567 non-nested `}' comes first, then we stop before consuming that. */
2569 static void
2570 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2572 unsigned nesting_depth = 0;
2574 while (true)
2576 cp_token *token = cp_lexer_peek_token (parser->lexer);
2578 switch (token->type)
2580 case CPP_EOF:
2581 case CPP_PRAGMA_EOL:
2582 /* If we've run out of tokens, stop. */
2583 return;
2585 case CPP_SEMICOLON:
2586 /* If the next token is a `;', we have reached the end of the
2587 statement. */
2588 if (!nesting_depth)
2589 return;
2590 break;
2592 case CPP_CLOSE_BRACE:
2593 /* If this is a non-nested '}', stop before consuming it.
2594 That way, when confronted with something like:
2596 { 3 + }
2598 we stop before consuming the closing '}', even though we
2599 have not yet reached a `;'. */
2600 if (nesting_depth == 0)
2601 return;
2603 /* If it is the closing '}' for a block that we have
2604 scanned, stop -- but only after consuming the token.
2605 That way given:
2607 void f g () { ... }
2608 typedef int I;
2610 we will stop after the body of the erroneously declared
2611 function, but before consuming the following `typedef'
2612 declaration. */
2613 if (--nesting_depth == 0)
2615 cp_lexer_consume_token (parser->lexer);
2616 return;
2619 case CPP_OPEN_BRACE:
2620 ++nesting_depth;
2621 break;
2623 default:
2624 break;
2627 /* Consume the token. */
2628 cp_lexer_consume_token (parser->lexer);
2632 /* This function is called at the end of a statement or declaration.
2633 If the next token is a semicolon, it is consumed; otherwise, error
2634 recovery is attempted. */
2636 static void
2637 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2639 /* Look for the trailing `;'. */
2640 if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2642 /* If there is additional (erroneous) input, skip to the end of
2643 the statement. */
2644 cp_parser_skip_to_end_of_statement (parser);
2645 /* If the next token is now a `;', consume it. */
2646 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2647 cp_lexer_consume_token (parser->lexer);
2651 /* Skip tokens until we have consumed an entire block, or until we
2652 have consumed a non-nested `;'. */
2654 static void
2655 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2657 int nesting_depth = 0;
2659 while (nesting_depth >= 0)
2661 cp_token *token = cp_lexer_peek_token (parser->lexer);
2663 switch (token->type)
2665 case CPP_EOF:
2666 case CPP_PRAGMA_EOL:
2667 /* If we've run out of tokens, stop. */
2668 return;
2670 case CPP_SEMICOLON:
2671 /* Stop if this is an unnested ';'. */
2672 if (!nesting_depth)
2673 nesting_depth = -1;
2674 break;
2676 case CPP_CLOSE_BRACE:
2677 /* Stop if this is an unnested '}', or closes the outermost
2678 nesting level. */
2679 nesting_depth--;
2680 if (nesting_depth < 0)
2681 return;
2682 if (!nesting_depth)
2683 nesting_depth = -1;
2684 break;
2686 case CPP_OPEN_BRACE:
2687 /* Nest. */
2688 nesting_depth++;
2689 break;
2691 default:
2692 break;
2695 /* Consume the token. */
2696 cp_lexer_consume_token (parser->lexer);
2700 /* Skip tokens until a non-nested closing curly brace is the next
2701 token, or there are no more tokens. Return true in the first case,
2702 false otherwise. */
2704 static bool
2705 cp_parser_skip_to_closing_brace (cp_parser *parser)
2707 unsigned nesting_depth = 0;
2709 while (true)
2711 cp_token *token = cp_lexer_peek_token (parser->lexer);
2713 switch (token->type)
2715 case CPP_EOF:
2716 case CPP_PRAGMA_EOL:
2717 /* If we've run out of tokens, stop. */
2718 return false;
2720 case CPP_CLOSE_BRACE:
2721 /* If the next token is a non-nested `}', then we have reached
2722 the end of the current block. */
2723 if (nesting_depth-- == 0)
2724 return true;
2725 break;
2727 case CPP_OPEN_BRACE:
2728 /* If it the next token is a `{', then we are entering a new
2729 block. Consume the entire block. */
2730 ++nesting_depth;
2731 break;
2733 default:
2734 break;
2737 /* Consume the token. */
2738 cp_lexer_consume_token (parser->lexer);
2742 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2743 parameter is the PRAGMA token, allowing us to purge the entire pragma
2744 sequence. */
2746 static void
2747 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2749 cp_token *token;
2751 parser->lexer->in_pragma = false;
2754 token = cp_lexer_consume_token (parser->lexer);
2755 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2757 /* Ensure that the pragma is not parsed again. */
2758 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2761 /* Require pragma end of line, resyncing with it as necessary. The
2762 arguments are as for cp_parser_skip_to_pragma_eol. */
2764 static void
2765 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2767 parser->lexer->in_pragma = false;
2768 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2769 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2772 /* This is a simple wrapper around make_typename_type. When the id is
2773 an unresolved identifier node, we can provide a superior diagnostic
2774 using cp_parser_diagnose_invalid_type_name. */
2776 static tree
2777 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2778 tree id, location_t id_location)
2780 tree result;
2781 if (TREE_CODE (id) == IDENTIFIER_NODE)
2783 result = make_typename_type (scope, id, typename_type,
2784 /*complain=*/tf_none);
2785 if (result == error_mark_node)
2786 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2787 return result;
2789 return make_typename_type (scope, id, typename_type, tf_error);
2792 /* This is a wrapper around the
2793 make_{pointer,ptrmem,reference}_declarator functions that decides
2794 which one to call based on the CODE and CLASS_TYPE arguments. The
2795 CODE argument should be one of the values returned by
2796 cp_parser_ptr_operator. */
2797 static cp_declarator *
2798 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2799 cp_cv_quals cv_qualifiers,
2800 cp_declarator *target)
2802 if (code == ERROR_MARK)
2803 return cp_error_declarator;
2805 if (code == INDIRECT_REF)
2806 if (class_type == NULL_TREE)
2807 return make_pointer_declarator (cv_qualifiers, target);
2808 else
2809 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2810 else if (code == ADDR_EXPR && class_type == NULL_TREE)
2811 return make_reference_declarator (cv_qualifiers, target, false);
2812 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2813 return make_reference_declarator (cv_qualifiers, target, true);
2814 gcc_unreachable ();
2817 /* Create a new C++ parser. */
2819 static cp_parser *
2820 cp_parser_new (void)
2822 cp_parser *parser;
2823 cp_lexer *lexer;
2824 unsigned i;
2826 /* cp_lexer_new_main is called before calling ggc_alloc because
2827 cp_lexer_new_main might load a PCH file. */
2828 lexer = cp_lexer_new_main ();
2830 /* Initialize the binops_by_token so that we can get the tree
2831 directly from the token. */
2832 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2833 binops_by_token[binops[i].token_type] = binops[i];
2835 parser = GGC_CNEW (cp_parser);
2836 parser->lexer = lexer;
2837 parser->context = cp_parser_context_new (NULL);
2839 /* For now, we always accept GNU extensions. */
2840 parser->allow_gnu_extensions_p = 1;
2842 /* The `>' token is a greater-than operator, not the end of a
2843 template-id. */
2844 parser->greater_than_is_operator_p = true;
2846 parser->default_arg_ok_p = true;
2848 /* We are not parsing a constant-expression. */
2849 parser->integral_constant_expression_p = false;
2850 parser->allow_non_integral_constant_expression_p = false;
2851 parser->non_integral_constant_expression_p = false;
2853 /* Local variable names are not forbidden. */
2854 parser->local_variables_forbidden_p = false;
2856 /* We are not processing an `extern "C"' declaration. */
2857 parser->in_unbraced_linkage_specification_p = false;
2859 /* We are not processing a declarator. */
2860 parser->in_declarator_p = false;
2862 /* We are not processing a template-argument-list. */
2863 parser->in_template_argument_list_p = false;
2865 /* We are not in an iteration statement. */
2866 parser->in_statement = 0;
2868 /* We are not in a switch statement. */
2869 parser->in_switch_statement_p = false;
2871 /* We are not parsing a type-id inside an expression. */
2872 parser->in_type_id_in_expr_p = false;
2874 /* Declarations aren't implicitly extern "C". */
2875 parser->implicit_extern_c = false;
2877 /* String literals should be translated to the execution character set. */
2878 parser->translate_strings_p = true;
2880 /* We are not parsing a function body. */
2881 parser->in_function_body = false;
2883 /* The unparsed function queue is empty. */
2884 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2886 /* There are no classes being defined. */
2887 parser->num_classes_being_defined = 0;
2889 /* No template parameters apply. */
2890 parser->num_template_parameter_lists = 0;
2892 return parser;
2895 /* Create a cp_lexer structure which will emit the tokens in CACHE
2896 and push it onto the parser's lexer stack. This is used for delayed
2897 parsing of in-class method bodies and default arguments, and should
2898 not be confused with tentative parsing. */
2899 static void
2900 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2902 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2903 lexer->next = parser->lexer;
2904 parser->lexer = lexer;
2906 /* Move the current source position to that of the first token in the
2907 new lexer. */
2908 cp_lexer_set_source_position_from_token (lexer->next_token);
2911 /* Pop the top lexer off the parser stack. This is never used for the
2912 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2913 static void
2914 cp_parser_pop_lexer (cp_parser *parser)
2916 cp_lexer *lexer = parser->lexer;
2917 parser->lexer = lexer->next;
2918 cp_lexer_destroy (lexer);
2920 /* Put the current source position back where it was before this
2921 lexer was pushed. */
2922 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2925 /* Lexical conventions [gram.lex] */
2927 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2928 identifier. */
2930 static tree
2931 cp_parser_identifier (cp_parser* parser)
2933 cp_token *token;
2935 /* Look for the identifier. */
2936 token = cp_parser_require (parser, CPP_NAME, "identifier");
2937 /* Return the value. */
2938 return token ? token->u.value : error_mark_node;
2941 /* Parse a sequence of adjacent string constants. Returns a
2942 TREE_STRING representing the combined, nul-terminated string
2943 constant. If TRANSLATE is true, translate the string to the
2944 execution character set. If WIDE_OK is true, a wide string is
2945 invalid here.
2947 C++98 [lex.string] says that if a narrow string literal token is
2948 adjacent to a wide string literal token, the behavior is undefined.
2949 However, C99 6.4.5p4 says that this results in a wide string literal.
2950 We follow C99 here, for consistency with the C front end.
2952 This code is largely lifted from lex_string() in c-lex.c.
2954 FUTURE: ObjC++ will need to handle @-strings here. */
2955 static tree
2956 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2958 tree value;
2959 size_t count;
2960 struct obstack str_ob;
2961 cpp_string str, istr, *strs;
2962 cp_token *tok;
2963 enum cpp_ttype type;
2965 tok = cp_lexer_peek_token (parser->lexer);
2966 if (!cp_parser_is_string_literal (tok))
2968 cp_parser_error (parser, "expected string-literal");
2969 return error_mark_node;
2972 type = tok->type;
2974 /* Try to avoid the overhead of creating and destroying an obstack
2975 for the common case of just one string. */
2976 if (!cp_parser_is_string_literal
2977 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2979 cp_lexer_consume_token (parser->lexer);
2981 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2982 str.len = TREE_STRING_LENGTH (tok->u.value);
2983 count = 1;
2985 strs = &str;
2987 else
2989 gcc_obstack_init (&str_ob);
2990 count = 0;
2994 cp_lexer_consume_token (parser->lexer);
2995 count++;
2996 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2997 str.len = TREE_STRING_LENGTH (tok->u.value);
2999 if (type != tok->type)
3001 if (type == CPP_STRING)
3002 type = tok->type;
3003 else if (tok->type != CPP_STRING)
3004 error_at (tok->location,
3005 "unsupported non-standard concatenation "
3006 "of string literals");
3009 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3011 tok = cp_lexer_peek_token (parser->lexer);
3013 while (cp_parser_is_string_literal (tok));
3015 strs = (cpp_string *) obstack_finish (&str_ob);
3018 if (type != CPP_STRING && !wide_ok)
3020 cp_parser_error (parser, "a wide string is invalid in this context");
3021 type = CPP_STRING;
3024 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3025 (parse_in, strs, count, &istr, type))
3027 value = build_string (istr.len, (const char *)istr.text);
3028 free (CONST_CAST (unsigned char *, istr.text));
3030 switch (type)
3032 default:
3033 case CPP_STRING:
3034 case CPP_UTF8STRING:
3035 TREE_TYPE (value) = char_array_type_node;
3036 break;
3037 case CPP_STRING16:
3038 TREE_TYPE (value) = char16_array_type_node;
3039 break;
3040 case CPP_STRING32:
3041 TREE_TYPE (value) = char32_array_type_node;
3042 break;
3043 case CPP_WSTRING:
3044 TREE_TYPE (value) = wchar_array_type_node;
3045 break;
3048 value = fix_string_type (value);
3050 else
3051 /* cpp_interpret_string has issued an error. */
3052 value = error_mark_node;
3054 if (count > 1)
3055 obstack_free (&str_ob, 0);
3057 return value;
3061 /* Basic concepts [gram.basic] */
3063 /* Parse a translation-unit.
3065 translation-unit:
3066 declaration-seq [opt]
3068 Returns TRUE if all went well. */
3070 static bool
3071 cp_parser_translation_unit (cp_parser* parser)
3073 /* The address of the first non-permanent object on the declarator
3074 obstack. */
3075 static void *declarator_obstack_base;
3077 bool success;
3079 /* Create the declarator obstack, if necessary. */
3080 if (!cp_error_declarator)
3082 gcc_obstack_init (&declarator_obstack);
3083 /* Create the error declarator. */
3084 cp_error_declarator = make_declarator (cdk_error);
3085 /* Create the empty parameter list. */
3086 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3087 /* Remember where the base of the declarator obstack lies. */
3088 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3091 cp_parser_declaration_seq_opt (parser);
3093 /* If there are no tokens left then all went well. */
3094 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3096 /* Get rid of the token array; we don't need it any more. */
3097 cp_lexer_destroy (parser->lexer);
3098 parser->lexer = NULL;
3100 /* This file might have been a context that's implicitly extern
3101 "C". If so, pop the lang context. (Only relevant for PCH.) */
3102 if (parser->implicit_extern_c)
3104 pop_lang_context ();
3105 parser->implicit_extern_c = false;
3108 /* Finish up. */
3109 finish_translation_unit ();
3111 success = true;
3113 else
3115 cp_parser_error (parser, "expected declaration");
3116 success = false;
3119 /* Make sure the declarator obstack was fully cleaned up. */
3120 gcc_assert (obstack_next_free (&declarator_obstack)
3121 == declarator_obstack_base);
3123 /* All went well. */
3124 return success;
3127 /* Expressions [gram.expr] */
3129 /* Parse a primary-expression.
3131 primary-expression:
3132 literal
3133 this
3134 ( expression )
3135 id-expression
3137 GNU Extensions:
3139 primary-expression:
3140 ( compound-statement )
3141 __builtin_va_arg ( assignment-expression , type-id )
3142 __builtin_offsetof ( type-id , offsetof-expression )
3144 C++ Extensions:
3145 __has_nothrow_assign ( type-id )
3146 __has_nothrow_constructor ( type-id )
3147 __has_nothrow_copy ( type-id )
3148 __has_trivial_assign ( type-id )
3149 __has_trivial_constructor ( type-id )
3150 __has_trivial_copy ( type-id )
3151 __has_trivial_destructor ( type-id )
3152 __has_virtual_destructor ( type-id )
3153 __is_abstract ( type-id )
3154 __is_base_of ( type-id , type-id )
3155 __is_class ( type-id )
3156 __is_convertible_to ( type-id , type-id )
3157 __is_empty ( type-id )
3158 __is_enum ( type-id )
3159 __is_pod ( type-id )
3160 __is_polymorphic ( type-id )
3161 __is_union ( type-id )
3163 Objective-C++ Extension:
3165 primary-expression:
3166 objc-expression
3168 literal:
3169 __null
3171 ADDRESS_P is true iff this expression was immediately preceded by
3172 "&" and therefore might denote a pointer-to-member. CAST_P is true
3173 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3174 true iff this expression is a template argument.
3176 Returns a representation of the expression. Upon return, *IDK
3177 indicates what kind of id-expression (if any) was present. */
3179 static tree
3180 cp_parser_primary_expression (cp_parser *parser,
3181 bool address_p,
3182 bool cast_p,
3183 bool template_arg_p,
3184 cp_id_kind *idk)
3186 cp_token *token = NULL;
3188 /* Assume the primary expression is not an id-expression. */
3189 *idk = CP_ID_KIND_NONE;
3191 /* Peek at the next token. */
3192 token = cp_lexer_peek_token (parser->lexer);
3193 switch (token->type)
3195 /* literal:
3196 integer-literal
3197 character-literal
3198 floating-literal
3199 string-literal
3200 boolean-literal */
3201 case CPP_CHAR:
3202 case CPP_CHAR16:
3203 case CPP_CHAR32:
3204 case CPP_WCHAR:
3205 case CPP_NUMBER:
3206 token = cp_lexer_consume_token (parser->lexer);
3207 if (TREE_CODE (token->u.value) == FIXED_CST)
3209 error_at (token->location,
3210 "fixed-point types not supported in C++");
3211 return error_mark_node;
3213 /* Floating-point literals are only allowed in an integral
3214 constant expression if they are cast to an integral or
3215 enumeration type. */
3216 if (TREE_CODE (token->u.value) == REAL_CST
3217 && parser->integral_constant_expression_p
3218 && pedantic)
3220 /* CAST_P will be set even in invalid code like "int(2.7 +
3221 ...)". Therefore, we have to check that the next token
3222 is sure to end the cast. */
3223 if (cast_p)
3225 cp_token *next_token;
3227 next_token = cp_lexer_peek_token (parser->lexer);
3228 if (/* The comma at the end of an
3229 enumerator-definition. */
3230 next_token->type != CPP_COMMA
3231 /* The curly brace at the end of an enum-specifier. */
3232 && next_token->type != CPP_CLOSE_BRACE
3233 /* The end of a statement. */
3234 && next_token->type != CPP_SEMICOLON
3235 /* The end of the cast-expression. */
3236 && next_token->type != CPP_CLOSE_PAREN
3237 /* The end of an array bound. */
3238 && next_token->type != CPP_CLOSE_SQUARE
3239 /* The closing ">" in a template-argument-list. */
3240 && (next_token->type != CPP_GREATER
3241 || parser->greater_than_is_operator_p)
3242 /* C++0x only: A ">>" treated like two ">" tokens,
3243 in a template-argument-list. */
3244 && (next_token->type != CPP_RSHIFT
3245 || (cxx_dialect == cxx98)
3246 || parser->greater_than_is_operator_p))
3247 cast_p = false;
3250 /* If we are within a cast, then the constraint that the
3251 cast is to an integral or enumeration type will be
3252 checked at that point. If we are not within a cast, then
3253 this code is invalid. */
3254 if (!cast_p)
3255 cp_parser_non_integral_constant_expression
3256 (parser, "floating-point literal");
3258 return token->u.value;
3260 case CPP_STRING:
3261 case CPP_STRING16:
3262 case CPP_STRING32:
3263 case CPP_WSTRING:
3264 case CPP_UTF8STRING:
3265 /* ??? Should wide strings be allowed when parser->translate_strings_p
3266 is false (i.e. in attributes)? If not, we can kill the third
3267 argument to cp_parser_string_literal. */
3268 return cp_parser_string_literal (parser,
3269 parser->translate_strings_p,
3270 true);
3272 case CPP_OPEN_PAREN:
3274 tree expr;
3275 bool saved_greater_than_is_operator_p;
3277 /* Consume the `('. */
3278 cp_lexer_consume_token (parser->lexer);
3279 /* Within a parenthesized expression, a `>' token is always
3280 the greater-than operator. */
3281 saved_greater_than_is_operator_p
3282 = parser->greater_than_is_operator_p;
3283 parser->greater_than_is_operator_p = true;
3284 /* If we see `( { ' then we are looking at the beginning of
3285 a GNU statement-expression. */
3286 if (cp_parser_allow_gnu_extensions_p (parser)
3287 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3289 /* Statement-expressions are not allowed by the standard. */
3290 pedwarn (token->location, OPT_pedantic,
3291 "ISO C++ forbids braced-groups within expressions");
3293 /* And they're not allowed outside of a function-body; you
3294 cannot, for example, write:
3296 int i = ({ int j = 3; j + 1; });
3298 at class or namespace scope. */
3299 if (!parser->in_function_body
3300 || parser->in_template_argument_list_p)
3302 error_at (token->location,
3303 "statement-expressions are not allowed outside "
3304 "functions nor in template-argument lists");
3305 cp_parser_skip_to_end_of_block_or_statement (parser);
3306 expr = error_mark_node;
3308 else
3310 /* Start the statement-expression. */
3311 expr = begin_stmt_expr ();
3312 /* Parse the compound-statement. */
3313 cp_parser_compound_statement (parser, expr, false);
3314 /* Finish up. */
3315 expr = finish_stmt_expr (expr, false);
3318 else
3320 /* Parse the parenthesized expression. */
3321 expr = cp_parser_expression (parser, cast_p, idk);
3322 /* Let the front end know that this expression was
3323 enclosed in parentheses. This matters in case, for
3324 example, the expression is of the form `A::B', since
3325 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3326 not. */
3327 finish_parenthesized_expr (expr);
3329 /* The `>' token might be the end of a template-id or
3330 template-parameter-list now. */
3331 parser->greater_than_is_operator_p
3332 = saved_greater_than_is_operator_p;
3333 /* Consume the `)'. */
3334 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3335 cp_parser_skip_to_end_of_statement (parser);
3337 return expr;
3340 case CPP_OPEN_SQUARE:
3341 if (c_dialect_objc ())
3342 /* We have an Objective-C++ message. */
3343 return cp_parser_objc_expression (parser);
3344 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
3345 return cp_parser_lambda_expression (parser);
3347 case CPP_OBJC_STRING:
3348 if (c_dialect_objc ())
3349 /* We have an Objective-C++ string literal. */
3350 return cp_parser_objc_expression (parser);
3351 cp_parser_error (parser, "expected primary-expression");
3352 return error_mark_node;
3354 case CPP_KEYWORD:
3355 switch (token->keyword)
3357 /* These two are the boolean literals. */
3358 case RID_TRUE:
3359 cp_lexer_consume_token (parser->lexer);
3360 return boolean_true_node;
3361 case RID_FALSE:
3362 cp_lexer_consume_token (parser->lexer);
3363 return boolean_false_node;
3365 /* The `__null' literal. */
3366 case RID_NULL:
3367 cp_lexer_consume_token (parser->lexer);
3368 return null_node;
3370 /* Recognize the `this' keyword. */
3371 case RID_THIS:
3372 cp_lexer_consume_token (parser->lexer);
3373 if (parser->local_variables_forbidden_p)
3375 error_at (token->location,
3376 "%<this%> may not be used in this context");
3377 return error_mark_node;
3379 /* Pointers cannot appear in constant-expressions. */
3380 if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3381 return error_mark_node;
3382 return finish_this_expr ();
3384 /* The `operator' keyword can be the beginning of an
3385 id-expression. */
3386 case RID_OPERATOR:
3387 goto id_expression;
3389 case RID_FUNCTION_NAME:
3390 case RID_PRETTY_FUNCTION_NAME:
3391 case RID_C99_FUNCTION_NAME:
3393 const char *name;
3395 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3396 __func__ are the names of variables -- but they are
3397 treated specially. Therefore, they are handled here,
3398 rather than relying on the generic id-expression logic
3399 below. Grammatically, these names are id-expressions.
3401 Consume the token. */
3402 token = cp_lexer_consume_token (parser->lexer);
3404 switch (token->keyword)
3406 case RID_FUNCTION_NAME:
3407 name = "%<__FUNCTION__%>";
3408 break;
3409 case RID_PRETTY_FUNCTION_NAME:
3410 name = "%<__PRETTY_FUNCTION__%>";
3411 break;
3412 case RID_C99_FUNCTION_NAME:
3413 name = "%<__func__%>";
3414 break;
3415 default:
3416 gcc_unreachable ();
3419 if (cp_parser_non_integral_constant_expression (parser, name))
3420 return error_mark_node;
3422 /* Look up the name. */
3423 return finish_fname (token->u.value);
3426 case RID_VA_ARG:
3428 tree expression;
3429 tree type;
3431 /* The `__builtin_va_arg' construct is used to handle
3432 `va_arg'. Consume the `__builtin_va_arg' token. */
3433 cp_lexer_consume_token (parser->lexer);
3434 /* Look for the opening `('. */
3435 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3436 /* Now, parse the assignment-expression. */
3437 expression = cp_parser_assignment_expression (parser,
3438 /*cast_p=*/false, NULL);
3439 /* Look for the `,'. */
3440 cp_parser_require (parser, CPP_COMMA, "%<,%>");
3441 /* Parse the type-id. */
3442 type = cp_parser_type_id (parser);
3443 /* Look for the closing `)'. */
3444 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3445 /* Using `va_arg' in a constant-expression is not
3446 allowed. */
3447 if (cp_parser_non_integral_constant_expression (parser,
3448 "%<va_arg%>"))
3449 return error_mark_node;
3450 return build_x_va_arg (expression, type);
3453 case RID_OFFSETOF:
3454 return cp_parser_builtin_offsetof (parser);
3456 case RID_HAS_NOTHROW_ASSIGN:
3457 case RID_HAS_NOTHROW_CONSTRUCTOR:
3458 case RID_HAS_NOTHROW_COPY:
3459 case RID_HAS_TRIVIAL_ASSIGN:
3460 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3461 case RID_HAS_TRIVIAL_COPY:
3462 case RID_HAS_TRIVIAL_DESTRUCTOR:
3463 case RID_HAS_VIRTUAL_DESTRUCTOR:
3464 case RID_IS_ABSTRACT:
3465 case RID_IS_BASE_OF:
3466 case RID_IS_CLASS:
3467 case RID_IS_CONVERTIBLE_TO:
3468 case RID_IS_EMPTY:
3469 case RID_IS_ENUM:
3470 case RID_IS_POD:
3471 case RID_IS_POLYMORPHIC:
3472 case RID_IS_STD_LAYOUT:
3473 case RID_IS_TRIVIAL:
3474 case RID_IS_UNION:
3475 return cp_parser_trait_expr (parser, token->keyword);
3477 /* Objective-C++ expressions. */
3478 case RID_AT_ENCODE:
3479 case RID_AT_PROTOCOL:
3480 case RID_AT_SELECTOR:
3481 return cp_parser_objc_expression (parser);
3483 default:
3484 cp_parser_error (parser, "expected primary-expression");
3485 return error_mark_node;
3488 /* An id-expression can start with either an identifier, a
3489 `::' as the beginning of a qualified-id, or the "operator"
3490 keyword. */
3491 case CPP_NAME:
3492 case CPP_SCOPE:
3493 case CPP_TEMPLATE_ID:
3494 case CPP_NESTED_NAME_SPECIFIER:
3496 tree id_expression;
3497 tree decl;
3498 const char *error_msg;
3499 bool template_p;
3500 bool done;
3501 cp_token *id_expr_token;
3503 id_expression:
3504 /* Parse the id-expression. */
3505 id_expression
3506 = cp_parser_id_expression (parser,
3507 /*template_keyword_p=*/false,
3508 /*check_dependency_p=*/true,
3509 &template_p,
3510 /*declarator_p=*/false,
3511 /*optional_p=*/false);
3512 if (id_expression == error_mark_node)
3513 return error_mark_node;
3514 id_expr_token = token;
3515 token = cp_lexer_peek_token (parser->lexer);
3516 done = (token->type != CPP_OPEN_SQUARE
3517 && token->type != CPP_OPEN_PAREN
3518 && token->type != CPP_DOT
3519 && token->type != CPP_DEREF
3520 && token->type != CPP_PLUS_PLUS
3521 && token->type != CPP_MINUS_MINUS);
3522 /* If we have a template-id, then no further lookup is
3523 required. If the template-id was for a template-class, we
3524 will sometimes have a TYPE_DECL at this point. */
3525 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3526 || TREE_CODE (id_expression) == TYPE_DECL)
3527 decl = id_expression;
3528 /* Look up the name. */
3529 else
3531 tree ambiguous_decls;
3533 /* If we already know that this lookup is ambiguous, then
3534 we've already issued an error message; there's no reason
3535 to check again. */
3536 if (id_expr_token->type == CPP_NAME
3537 && id_expr_token->ambiguous_p)
3539 cp_parser_simulate_error (parser);
3540 return error_mark_node;
3543 decl = cp_parser_lookup_name (parser, id_expression,
3544 none_type,
3545 template_p,
3546 /*is_namespace=*/false,
3547 /*check_dependency=*/true,
3548 &ambiguous_decls,
3549 id_expr_token->location);
3550 /* If the lookup was ambiguous, an error will already have
3551 been issued. */
3552 if (ambiguous_decls)
3553 return error_mark_node;
3555 /* In Objective-C++, an instance variable (ivar) may be preferred
3556 to whatever cp_parser_lookup_name() found. */
3557 decl = objc_lookup_ivar (decl, id_expression);
3559 /* If name lookup gives us a SCOPE_REF, then the
3560 qualifying scope was dependent. */
3561 if (TREE_CODE (decl) == SCOPE_REF)
3563 /* At this point, we do not know if DECL is a valid
3564 integral constant expression. We assume that it is
3565 in fact such an expression, so that code like:
3567 template <int N> struct A {
3568 int a[B<N>::i];
3571 is accepted. At template-instantiation time, we
3572 will check that B<N>::i is actually a constant. */
3573 return decl;
3575 /* Check to see if DECL is a local variable in a context
3576 where that is forbidden. */
3577 if (parser->local_variables_forbidden_p
3578 && local_variable_p (decl))
3580 /* It might be that we only found DECL because we are
3581 trying to be generous with pre-ISO scoping rules.
3582 For example, consider:
3584 int i;
3585 void g() {
3586 for (int i = 0; i < 10; ++i) {}
3587 extern void f(int j = i);
3590 Here, name look up will originally find the out
3591 of scope `i'. We need to issue a warning message,
3592 but then use the global `i'. */
3593 decl = check_for_out_of_scope_variable (decl);
3594 if (local_variable_p (decl))
3596 error_at (id_expr_token->location,
3597 "local variable %qD may not appear in this context",
3598 decl);
3599 return error_mark_node;
3604 decl = (finish_id_expression
3605 (id_expression, decl, parser->scope,
3606 idk,
3607 parser->integral_constant_expression_p,
3608 parser->allow_non_integral_constant_expression_p,
3609 &parser->non_integral_constant_expression_p,
3610 template_p, done, address_p,
3611 template_arg_p,
3612 &error_msg,
3613 id_expr_token->location));
3614 if (error_msg)
3615 cp_parser_error (parser, error_msg);
3616 return decl;
3619 /* Anything else is an error. */
3620 default:
3621 cp_parser_error (parser, "expected primary-expression");
3622 return error_mark_node;
3626 /* Parse an id-expression.
3628 id-expression:
3629 unqualified-id
3630 qualified-id
3632 qualified-id:
3633 :: [opt] nested-name-specifier template [opt] unqualified-id
3634 :: identifier
3635 :: operator-function-id
3636 :: template-id
3638 Return a representation of the unqualified portion of the
3639 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3640 a `::' or nested-name-specifier.
3642 Often, if the id-expression was a qualified-id, the caller will
3643 want to make a SCOPE_REF to represent the qualified-id. This
3644 function does not do this in order to avoid wastefully creating
3645 SCOPE_REFs when they are not required.
3647 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3648 `template' keyword.
3650 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3651 uninstantiated templates.
3653 If *TEMPLATE_P is non-NULL, it is set to true iff the
3654 `template' keyword is used to explicitly indicate that the entity
3655 named is a template.
3657 If DECLARATOR_P is true, the id-expression is appearing as part of
3658 a declarator, rather than as part of an expression. */
3660 static tree
3661 cp_parser_id_expression (cp_parser *parser,
3662 bool template_keyword_p,
3663 bool check_dependency_p,
3664 bool *template_p,
3665 bool declarator_p,
3666 bool optional_p)
3668 bool global_scope_p;
3669 bool nested_name_specifier_p;
3671 /* Assume the `template' keyword was not used. */
3672 if (template_p)
3673 *template_p = template_keyword_p;
3675 /* Look for the optional `::' operator. */
3676 global_scope_p
3677 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3678 != NULL_TREE);
3679 /* Look for the optional nested-name-specifier. */
3680 nested_name_specifier_p
3681 = (cp_parser_nested_name_specifier_opt (parser,
3682 /*typename_keyword_p=*/false,
3683 check_dependency_p,
3684 /*type_p=*/false,
3685 declarator_p)
3686 != NULL_TREE);
3687 /* If there is a nested-name-specifier, then we are looking at
3688 the first qualified-id production. */
3689 if (nested_name_specifier_p)
3691 tree saved_scope;
3692 tree saved_object_scope;
3693 tree saved_qualifying_scope;
3694 tree unqualified_id;
3695 bool is_template;
3697 /* See if the next token is the `template' keyword. */
3698 if (!template_p)
3699 template_p = &is_template;
3700 *template_p = cp_parser_optional_template_keyword (parser);
3701 /* Name lookup we do during the processing of the
3702 unqualified-id might obliterate SCOPE. */
3703 saved_scope = parser->scope;
3704 saved_object_scope = parser->object_scope;
3705 saved_qualifying_scope = parser->qualifying_scope;
3706 /* Process the final unqualified-id. */
3707 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3708 check_dependency_p,
3709 declarator_p,
3710 /*optional_p=*/false);
3711 /* Restore the SAVED_SCOPE for our caller. */
3712 parser->scope = saved_scope;
3713 parser->object_scope = saved_object_scope;
3714 parser->qualifying_scope = saved_qualifying_scope;
3716 return unqualified_id;
3718 /* Otherwise, if we are in global scope, then we are looking at one
3719 of the other qualified-id productions. */
3720 else if (global_scope_p)
3722 cp_token *token;
3723 tree id;
3725 /* Peek at the next token. */
3726 token = cp_lexer_peek_token (parser->lexer);
3728 /* If it's an identifier, and the next token is not a "<", then
3729 we can avoid the template-id case. This is an optimization
3730 for this common case. */
3731 if (token->type == CPP_NAME
3732 && !cp_parser_nth_token_starts_template_argument_list_p
3733 (parser, 2))
3734 return cp_parser_identifier (parser);
3736 cp_parser_parse_tentatively (parser);
3737 /* Try a template-id. */
3738 id = cp_parser_template_id (parser,
3739 /*template_keyword_p=*/false,
3740 /*check_dependency_p=*/true,
3741 declarator_p);
3742 /* If that worked, we're done. */
3743 if (cp_parser_parse_definitely (parser))
3744 return id;
3746 /* Peek at the next token. (Changes in the token buffer may
3747 have invalidated the pointer obtained above.) */
3748 token = cp_lexer_peek_token (parser->lexer);
3750 switch (token->type)
3752 case CPP_NAME:
3753 return cp_parser_identifier (parser);
3755 case CPP_KEYWORD:
3756 if (token->keyword == RID_OPERATOR)
3757 return cp_parser_operator_function_id (parser);
3758 /* Fall through. */
3760 default:
3761 cp_parser_error (parser, "expected id-expression");
3762 return error_mark_node;
3765 else
3766 return cp_parser_unqualified_id (parser, template_keyword_p,
3767 /*check_dependency_p=*/true,
3768 declarator_p,
3769 optional_p);
3772 /* Parse an unqualified-id.
3774 unqualified-id:
3775 identifier
3776 operator-function-id
3777 conversion-function-id
3778 ~ class-name
3779 template-id
3781 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3782 keyword, in a construct like `A::template ...'.
3784 Returns a representation of unqualified-id. For the `identifier'
3785 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3786 production a BIT_NOT_EXPR is returned; the operand of the
3787 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3788 other productions, see the documentation accompanying the
3789 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3790 names are looked up in uninstantiated templates. If DECLARATOR_P
3791 is true, the unqualified-id is appearing as part of a declarator,
3792 rather than as part of an expression. */
3794 static tree
3795 cp_parser_unqualified_id (cp_parser* parser,
3796 bool template_keyword_p,
3797 bool check_dependency_p,
3798 bool declarator_p,
3799 bool optional_p)
3801 cp_token *token;
3803 /* Peek at the next token. */
3804 token = cp_lexer_peek_token (parser->lexer);
3806 switch (token->type)
3808 case CPP_NAME:
3810 tree id;
3812 /* We don't know yet whether or not this will be a
3813 template-id. */
3814 cp_parser_parse_tentatively (parser);
3815 /* Try a template-id. */
3816 id = cp_parser_template_id (parser, template_keyword_p,
3817 check_dependency_p,
3818 declarator_p);
3819 /* If it worked, we're done. */
3820 if (cp_parser_parse_definitely (parser))
3821 return id;
3822 /* Otherwise, it's an ordinary identifier. */
3823 return cp_parser_identifier (parser);
3826 case CPP_TEMPLATE_ID:
3827 return cp_parser_template_id (parser, template_keyword_p,
3828 check_dependency_p,
3829 declarator_p);
3831 case CPP_COMPL:
3833 tree type_decl;
3834 tree qualifying_scope;
3835 tree object_scope;
3836 tree scope;
3837 bool done;
3839 /* Consume the `~' token. */
3840 cp_lexer_consume_token (parser->lexer);
3841 /* Parse the class-name. The standard, as written, seems to
3842 say that:
3844 template <typename T> struct S { ~S (); };
3845 template <typename T> S<T>::~S() {}
3847 is invalid, since `~' must be followed by a class-name, but
3848 `S<T>' is dependent, and so not known to be a class.
3849 That's not right; we need to look in uninstantiated
3850 templates. A further complication arises from:
3852 template <typename T> void f(T t) {
3853 t.T::~T();
3856 Here, it is not possible to look up `T' in the scope of `T'
3857 itself. We must look in both the current scope, and the
3858 scope of the containing complete expression.
3860 Yet another issue is:
3862 struct S {
3863 int S;
3864 ~S();
3867 S::~S() {}
3869 The standard does not seem to say that the `S' in `~S'
3870 should refer to the type `S' and not the data member
3871 `S::S'. */
3873 /* DR 244 says that we look up the name after the "~" in the
3874 same scope as we looked up the qualifying name. That idea
3875 isn't fully worked out; it's more complicated than that. */
3876 scope = parser->scope;
3877 object_scope = parser->object_scope;
3878 qualifying_scope = parser->qualifying_scope;
3880 /* Check for invalid scopes. */
3881 if (scope == error_mark_node)
3883 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3884 cp_lexer_consume_token (parser->lexer);
3885 return error_mark_node;
3887 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3889 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3890 error_at (token->location,
3891 "scope %qT before %<~%> is not a class-name",
3892 scope);
3893 cp_parser_simulate_error (parser);
3894 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3895 cp_lexer_consume_token (parser->lexer);
3896 return error_mark_node;
3898 gcc_assert (!scope || TYPE_P (scope));
3900 /* If the name is of the form "X::~X" it's OK. */
3901 token = cp_lexer_peek_token (parser->lexer);
3902 if (scope
3903 && token->type == CPP_NAME
3904 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3905 != CPP_LESS)
3906 && constructor_name_p (token->u.value, scope))
3908 cp_lexer_consume_token (parser->lexer);
3909 return build_nt (BIT_NOT_EXPR, scope);
3912 /* If there was an explicit qualification (S::~T), first look
3913 in the scope given by the qualification (i.e., S).
3915 Note: in the calls to cp_parser_class_name below we pass
3916 typename_type so that lookup finds the injected-class-name
3917 rather than the constructor. */
3918 done = false;
3919 type_decl = NULL_TREE;
3920 if (scope)
3922 cp_parser_parse_tentatively (parser);
3923 type_decl = cp_parser_class_name (parser,
3924 /*typename_keyword_p=*/false,
3925 /*template_keyword_p=*/false,
3926 typename_type,
3927 /*check_dependency=*/false,
3928 /*class_head_p=*/false,
3929 declarator_p);
3930 if (cp_parser_parse_definitely (parser))
3931 done = true;
3933 /* In "N::S::~S", look in "N" as well. */
3934 if (!done && scope && qualifying_scope)
3936 cp_parser_parse_tentatively (parser);
3937 parser->scope = qualifying_scope;
3938 parser->object_scope = NULL_TREE;
3939 parser->qualifying_scope = NULL_TREE;
3940 type_decl
3941 = cp_parser_class_name (parser,
3942 /*typename_keyword_p=*/false,
3943 /*template_keyword_p=*/false,
3944 typename_type,
3945 /*check_dependency=*/false,
3946 /*class_head_p=*/false,
3947 declarator_p);
3948 if (cp_parser_parse_definitely (parser))
3949 done = true;
3951 /* In "p->S::~T", look in the scope given by "*p" as well. */
3952 else if (!done && object_scope)
3954 cp_parser_parse_tentatively (parser);
3955 parser->scope = object_scope;
3956 parser->object_scope = NULL_TREE;
3957 parser->qualifying_scope = NULL_TREE;
3958 type_decl
3959 = cp_parser_class_name (parser,
3960 /*typename_keyword_p=*/false,
3961 /*template_keyword_p=*/false,
3962 typename_type,
3963 /*check_dependency=*/false,
3964 /*class_head_p=*/false,
3965 declarator_p);
3966 if (cp_parser_parse_definitely (parser))
3967 done = true;
3969 /* Look in the surrounding context. */
3970 if (!done)
3972 parser->scope = NULL_TREE;
3973 parser->object_scope = NULL_TREE;
3974 parser->qualifying_scope = NULL_TREE;
3975 if (processing_template_decl)
3976 cp_parser_parse_tentatively (parser);
3977 type_decl
3978 = cp_parser_class_name (parser,
3979 /*typename_keyword_p=*/false,
3980 /*template_keyword_p=*/false,
3981 typename_type,
3982 /*check_dependency=*/false,
3983 /*class_head_p=*/false,
3984 declarator_p);
3985 if (processing_template_decl
3986 && ! cp_parser_parse_definitely (parser))
3988 /* We couldn't find a type with this name, so just accept
3989 it and check for a match at instantiation time. */
3990 type_decl = cp_parser_identifier (parser);
3991 if (type_decl != error_mark_node)
3992 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
3993 return type_decl;
3996 /* If an error occurred, assume that the name of the
3997 destructor is the same as the name of the qualifying
3998 class. That allows us to keep parsing after running
3999 into ill-formed destructor names. */
4000 if (type_decl == error_mark_node && scope)
4001 return build_nt (BIT_NOT_EXPR, scope);
4002 else if (type_decl == error_mark_node)
4003 return error_mark_node;
4005 /* Check that destructor name and scope match. */
4006 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4008 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4009 error_at (token->location,
4010 "declaration of %<~%T%> as member of %qT",
4011 type_decl, scope);
4012 cp_parser_simulate_error (parser);
4013 return error_mark_node;
4016 /* [class.dtor]
4018 A typedef-name that names a class shall not be used as the
4019 identifier in the declarator for a destructor declaration. */
4020 if (declarator_p
4021 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4022 && !DECL_SELF_REFERENCE_P (type_decl)
4023 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4024 error_at (token->location,
4025 "typedef-name %qD used as destructor declarator",
4026 type_decl);
4028 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4031 case CPP_KEYWORD:
4032 if (token->keyword == RID_OPERATOR)
4034 tree id;
4036 /* This could be a template-id, so we try that first. */
4037 cp_parser_parse_tentatively (parser);
4038 /* Try a template-id. */
4039 id = cp_parser_template_id (parser, template_keyword_p,
4040 /*check_dependency_p=*/true,
4041 declarator_p);
4042 /* If that worked, we're done. */
4043 if (cp_parser_parse_definitely (parser))
4044 return id;
4045 /* We still don't know whether we're looking at an
4046 operator-function-id or a conversion-function-id. */
4047 cp_parser_parse_tentatively (parser);
4048 /* Try an operator-function-id. */
4049 id = cp_parser_operator_function_id (parser);
4050 /* If that didn't work, try a conversion-function-id. */
4051 if (!cp_parser_parse_definitely (parser))
4052 id = cp_parser_conversion_function_id (parser);
4054 return id;
4056 /* Fall through. */
4058 default:
4059 if (optional_p)
4060 return NULL_TREE;
4061 cp_parser_error (parser, "expected unqualified-id");
4062 return error_mark_node;
4066 /* Parse an (optional) nested-name-specifier.
4068 nested-name-specifier: [C++98]
4069 class-or-namespace-name :: nested-name-specifier [opt]
4070 class-or-namespace-name :: template nested-name-specifier [opt]
4072 nested-name-specifier: [C++0x]
4073 type-name ::
4074 namespace-name ::
4075 nested-name-specifier identifier ::
4076 nested-name-specifier template [opt] simple-template-id ::
4078 PARSER->SCOPE should be set appropriately before this function is
4079 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4080 effect. TYPE_P is TRUE if we non-type bindings should be ignored
4081 in name lookups.
4083 Sets PARSER->SCOPE to the class (TYPE) or namespace
4084 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4085 it unchanged if there is no nested-name-specifier. Returns the new
4086 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4088 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4089 part of a declaration and/or decl-specifier. */
4091 static tree
4092 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4093 bool typename_keyword_p,
4094 bool check_dependency_p,
4095 bool type_p,
4096 bool is_declaration)
4098 bool success = false;
4099 cp_token_position start = 0;
4100 cp_token *token;
4102 /* Remember where the nested-name-specifier starts. */
4103 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4105 start = cp_lexer_token_position (parser->lexer, false);
4106 push_deferring_access_checks (dk_deferred);
4109 while (true)
4111 tree new_scope;
4112 tree old_scope;
4113 tree saved_qualifying_scope;
4114 bool template_keyword_p;
4116 /* Spot cases that cannot be the beginning of a
4117 nested-name-specifier. */
4118 token = cp_lexer_peek_token (parser->lexer);
4120 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4121 the already parsed nested-name-specifier. */
4122 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4124 /* Grab the nested-name-specifier and continue the loop. */
4125 cp_parser_pre_parsed_nested_name_specifier (parser);
4126 /* If we originally encountered this nested-name-specifier
4127 with IS_DECLARATION set to false, we will not have
4128 resolved TYPENAME_TYPEs, so we must do so here. */
4129 if (is_declaration
4130 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4132 new_scope = resolve_typename_type (parser->scope,
4133 /*only_current_p=*/false);
4134 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4135 parser->scope = new_scope;
4137 success = true;
4138 continue;
4141 /* Spot cases that cannot be the beginning of a
4142 nested-name-specifier. On the second and subsequent times
4143 through the loop, we look for the `template' keyword. */
4144 if (success && token->keyword == RID_TEMPLATE)
4146 /* A template-id can start a nested-name-specifier. */
4147 else if (token->type == CPP_TEMPLATE_ID)
4149 else
4151 /* If the next token is not an identifier, then it is
4152 definitely not a type-name or namespace-name. */
4153 if (token->type != CPP_NAME)
4154 break;
4155 /* If the following token is neither a `<' (to begin a
4156 template-id), nor a `::', then we are not looking at a
4157 nested-name-specifier. */
4158 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4159 if (token->type != CPP_SCOPE
4160 && !cp_parser_nth_token_starts_template_argument_list_p
4161 (parser, 2))
4162 break;
4165 /* The nested-name-specifier is optional, so we parse
4166 tentatively. */
4167 cp_parser_parse_tentatively (parser);
4169 /* Look for the optional `template' keyword, if this isn't the
4170 first time through the loop. */
4171 if (success)
4172 template_keyword_p = cp_parser_optional_template_keyword (parser);
4173 else
4174 template_keyword_p = false;
4176 /* Save the old scope since the name lookup we are about to do
4177 might destroy it. */
4178 old_scope = parser->scope;
4179 saved_qualifying_scope = parser->qualifying_scope;
4180 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4181 look up names in "X<T>::I" in order to determine that "Y" is
4182 a template. So, if we have a typename at this point, we make
4183 an effort to look through it. */
4184 if (is_declaration
4185 && !typename_keyword_p
4186 && parser->scope
4187 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4188 parser->scope = resolve_typename_type (parser->scope,
4189 /*only_current_p=*/false);
4190 /* Parse the qualifying entity. */
4191 new_scope
4192 = cp_parser_qualifying_entity (parser,
4193 typename_keyword_p,
4194 template_keyword_p,
4195 check_dependency_p,
4196 type_p,
4197 is_declaration);
4198 /* Look for the `::' token. */
4199 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4201 /* If we found what we wanted, we keep going; otherwise, we're
4202 done. */
4203 if (!cp_parser_parse_definitely (parser))
4205 bool error_p = false;
4207 /* Restore the OLD_SCOPE since it was valid before the
4208 failed attempt at finding the last
4209 class-or-namespace-name. */
4210 parser->scope = old_scope;
4211 parser->qualifying_scope = saved_qualifying_scope;
4212 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4213 break;
4214 /* If the next token is an identifier, and the one after
4215 that is a `::', then any valid interpretation would have
4216 found a class-or-namespace-name. */
4217 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4218 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4219 == CPP_SCOPE)
4220 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4221 != CPP_COMPL))
4223 token = cp_lexer_consume_token (parser->lexer);
4224 if (!error_p)
4226 if (!token->ambiguous_p)
4228 tree decl;
4229 tree ambiguous_decls;
4231 decl = cp_parser_lookup_name (parser, token->u.value,
4232 none_type,
4233 /*is_template=*/false,
4234 /*is_namespace=*/false,
4235 /*check_dependency=*/true,
4236 &ambiguous_decls,
4237 token->location);
4238 if (TREE_CODE (decl) == TEMPLATE_DECL)
4239 error_at (token->location,
4240 "%qD used without template parameters",
4241 decl);
4242 else if (ambiguous_decls)
4244 error_at (token->location,
4245 "reference to %qD is ambiguous",
4246 token->u.value);
4247 print_candidates (ambiguous_decls);
4248 decl = error_mark_node;
4250 else
4252 const char* msg = "is not a class or namespace";
4253 if (cxx_dialect != cxx98)
4254 msg = "is not a class, namespace, or enumeration";
4255 cp_parser_name_lookup_error
4256 (parser, token->u.value, decl, msg,
4257 token->location);
4260 parser->scope = error_mark_node;
4261 error_p = true;
4262 /* Treat this as a successful nested-name-specifier
4263 due to:
4265 [basic.lookup.qual]
4267 If the name found is not a class-name (clause
4268 _class_) or namespace-name (_namespace.def_), the
4269 program is ill-formed. */
4270 success = true;
4272 cp_lexer_consume_token (parser->lexer);
4274 break;
4276 /* We've found one valid nested-name-specifier. */
4277 success = true;
4278 /* Name lookup always gives us a DECL. */
4279 if (TREE_CODE (new_scope) == TYPE_DECL)
4280 new_scope = TREE_TYPE (new_scope);
4281 /* Uses of "template" must be followed by actual templates. */
4282 if (template_keyword_p
4283 && !(CLASS_TYPE_P (new_scope)
4284 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4285 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4286 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4287 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4288 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4289 == TEMPLATE_ID_EXPR)))
4290 permerror (input_location, TYPE_P (new_scope)
4291 ? "%qT is not a template"
4292 : "%qD is not a template",
4293 new_scope);
4294 /* If it is a class scope, try to complete it; we are about to
4295 be looking up names inside the class. */
4296 if (TYPE_P (new_scope)
4297 /* Since checking types for dependency can be expensive,
4298 avoid doing it if the type is already complete. */
4299 && !COMPLETE_TYPE_P (new_scope)
4300 /* Do not try to complete dependent types. */
4301 && !dependent_type_p (new_scope))
4303 new_scope = complete_type (new_scope);
4304 /* If it is a typedef to current class, use the current
4305 class instead, as the typedef won't have any names inside
4306 it yet. */
4307 if (!COMPLETE_TYPE_P (new_scope)
4308 && currently_open_class (new_scope))
4309 new_scope = TYPE_MAIN_VARIANT (new_scope);
4311 /* Make sure we look in the right scope the next time through
4312 the loop. */
4313 parser->scope = new_scope;
4316 /* If parsing tentatively, replace the sequence of tokens that makes
4317 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4318 token. That way, should we re-parse the token stream, we will
4319 not have to repeat the effort required to do the parse, nor will
4320 we issue duplicate error messages. */
4321 if (success && start)
4323 cp_token *token;
4325 token = cp_lexer_token_at (parser->lexer, start);
4326 /* Reset the contents of the START token. */
4327 token->type = CPP_NESTED_NAME_SPECIFIER;
4328 /* Retrieve any deferred checks. Do not pop this access checks yet
4329 so the memory will not be reclaimed during token replacing below. */
4330 token->u.tree_check_value = GGC_CNEW (struct tree_check);
4331 token->u.tree_check_value->value = parser->scope;
4332 token->u.tree_check_value->checks = get_deferred_access_checks ();
4333 token->u.tree_check_value->qualifying_scope =
4334 parser->qualifying_scope;
4335 token->keyword = RID_MAX;
4337 /* Purge all subsequent tokens. */
4338 cp_lexer_purge_tokens_after (parser->lexer, start);
4341 if (start)
4342 pop_to_parent_deferring_access_checks ();
4344 return success ? parser->scope : NULL_TREE;
4347 /* Parse a nested-name-specifier. See
4348 cp_parser_nested_name_specifier_opt for details. This function
4349 behaves identically, except that it will an issue an error if no
4350 nested-name-specifier is present. */
4352 static tree
4353 cp_parser_nested_name_specifier (cp_parser *parser,
4354 bool typename_keyword_p,
4355 bool check_dependency_p,
4356 bool type_p,
4357 bool is_declaration)
4359 tree scope;
4361 /* Look for the nested-name-specifier. */
4362 scope = cp_parser_nested_name_specifier_opt (parser,
4363 typename_keyword_p,
4364 check_dependency_p,
4365 type_p,
4366 is_declaration);
4367 /* If it was not present, issue an error message. */
4368 if (!scope)
4370 cp_parser_error (parser, "expected nested-name-specifier");
4371 parser->scope = NULL_TREE;
4374 return scope;
4377 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4378 this is either a class-name or a namespace-name (which corresponds
4379 to the class-or-namespace-name production in the grammar). For
4380 C++0x, it can also be a type-name that refers to an enumeration
4381 type.
4383 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4384 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4385 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4386 TYPE_P is TRUE iff the next name should be taken as a class-name,
4387 even the same name is declared to be another entity in the same
4388 scope.
4390 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4391 specified by the class-or-namespace-name. If neither is found the
4392 ERROR_MARK_NODE is returned. */
4394 static tree
4395 cp_parser_qualifying_entity (cp_parser *parser,
4396 bool typename_keyword_p,
4397 bool template_keyword_p,
4398 bool check_dependency_p,
4399 bool type_p,
4400 bool is_declaration)
4402 tree saved_scope;
4403 tree saved_qualifying_scope;
4404 tree saved_object_scope;
4405 tree scope;
4406 bool only_class_p;
4407 bool successful_parse_p;
4409 /* Before we try to parse the class-name, we must save away the
4410 current PARSER->SCOPE since cp_parser_class_name will destroy
4411 it. */
4412 saved_scope = parser->scope;
4413 saved_qualifying_scope = parser->qualifying_scope;
4414 saved_object_scope = parser->object_scope;
4415 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4416 there is no need to look for a namespace-name. */
4417 only_class_p = template_keyword_p
4418 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4419 if (!only_class_p)
4420 cp_parser_parse_tentatively (parser);
4421 scope = cp_parser_class_name (parser,
4422 typename_keyword_p,
4423 template_keyword_p,
4424 type_p ? class_type : none_type,
4425 check_dependency_p,
4426 /*class_head_p=*/false,
4427 is_declaration);
4428 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4429 /* If that didn't work and we're in C++0x mode, try for a type-name. */
4430 if (!only_class_p
4431 && cxx_dialect != cxx98
4432 && !successful_parse_p)
4434 /* Restore the saved scope. */
4435 parser->scope = saved_scope;
4436 parser->qualifying_scope = saved_qualifying_scope;
4437 parser->object_scope = saved_object_scope;
4439 /* Parse tentatively. */
4440 cp_parser_parse_tentatively (parser);
4442 /* Parse a typedef-name or enum-name. */
4443 scope = cp_parser_nonclass_name (parser);
4445 /* "If the name found does not designate a namespace or a class,
4446 enumeration, or dependent type, the program is ill-formed."
4448 We cover classes and dependent types above and namespaces below,
4449 so this code is only looking for enums. */
4450 if (!scope || TREE_CODE (scope) != TYPE_DECL
4451 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
4452 cp_parser_simulate_error (parser);
4454 successful_parse_p = cp_parser_parse_definitely (parser);
4456 /* If that didn't work, try for a namespace-name. */
4457 if (!only_class_p && !successful_parse_p)
4459 /* Restore the saved scope. */
4460 parser->scope = saved_scope;
4461 parser->qualifying_scope = saved_qualifying_scope;
4462 parser->object_scope = saved_object_scope;
4463 /* If we are not looking at an identifier followed by the scope
4464 resolution operator, then this is not part of a
4465 nested-name-specifier. (Note that this function is only used
4466 to parse the components of a nested-name-specifier.) */
4467 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4468 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4469 return error_mark_node;
4470 scope = cp_parser_namespace_name (parser);
4473 return scope;
4476 /* Parse a postfix-expression.
4478 postfix-expression:
4479 primary-expression
4480 postfix-expression [ expression ]
4481 postfix-expression ( expression-list [opt] )
4482 simple-type-specifier ( expression-list [opt] )
4483 typename :: [opt] nested-name-specifier identifier
4484 ( expression-list [opt] )
4485 typename :: [opt] nested-name-specifier template [opt] template-id
4486 ( expression-list [opt] )
4487 postfix-expression . template [opt] id-expression
4488 postfix-expression -> template [opt] id-expression
4489 postfix-expression . pseudo-destructor-name
4490 postfix-expression -> pseudo-destructor-name
4491 postfix-expression ++
4492 postfix-expression --
4493 dynamic_cast < type-id > ( expression )
4494 static_cast < type-id > ( expression )
4495 reinterpret_cast < type-id > ( expression )
4496 const_cast < type-id > ( expression )
4497 typeid ( expression )
4498 typeid ( type-id )
4500 GNU Extension:
4502 postfix-expression:
4503 ( type-id ) { initializer-list , [opt] }
4505 This extension is a GNU version of the C99 compound-literal
4506 construct. (The C99 grammar uses `type-name' instead of `type-id',
4507 but they are essentially the same concept.)
4509 If ADDRESS_P is true, the postfix expression is the operand of the
4510 `&' operator. CAST_P is true if this expression is the target of a
4511 cast.
4513 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4514 class member access expressions [expr.ref].
4516 Returns a representation of the expression. */
4518 static tree
4519 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4520 bool member_access_only_p,
4521 cp_id_kind * pidk_return)
4523 cp_token *token;
4524 enum rid keyword;
4525 cp_id_kind idk = CP_ID_KIND_NONE;
4526 tree postfix_expression = NULL_TREE;
4527 bool is_member_access = false;
4529 /* Peek at the next token. */
4530 token = cp_lexer_peek_token (parser->lexer);
4531 /* Some of the productions are determined by keywords. */
4532 keyword = token->keyword;
4533 switch (keyword)
4535 case RID_DYNCAST:
4536 case RID_STATCAST:
4537 case RID_REINTCAST:
4538 case RID_CONSTCAST:
4540 tree type;
4541 tree expression;
4542 const char *saved_message;
4544 /* All of these can be handled in the same way from the point
4545 of view of parsing. Begin by consuming the token
4546 identifying the cast. */
4547 cp_lexer_consume_token (parser->lexer);
4549 /* New types cannot be defined in the cast. */
4550 saved_message = parser->type_definition_forbidden_message;
4551 parser->type_definition_forbidden_message
4552 = G_("types may not be defined in casts");
4554 /* Look for the opening `<'. */
4555 cp_parser_require (parser, CPP_LESS, "%<<%>");
4556 /* Parse the type to which we are casting. */
4557 type = cp_parser_type_id (parser);
4558 /* Look for the closing `>'. */
4559 cp_parser_require (parser, CPP_GREATER, "%<>%>");
4560 /* Restore the old message. */
4561 parser->type_definition_forbidden_message = saved_message;
4563 /* And the expression which is being cast. */
4564 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4565 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4566 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4568 /* Only type conversions to integral or enumeration types
4569 can be used in constant-expressions. */
4570 if (!cast_valid_in_integral_constant_expression_p (type)
4571 && (cp_parser_non_integral_constant_expression
4572 (parser,
4573 "a cast to a type other than an integral or "
4574 "enumeration type")))
4575 return error_mark_node;
4577 switch (keyword)
4579 case RID_DYNCAST:
4580 postfix_expression
4581 = build_dynamic_cast (type, expression, tf_warning_or_error);
4582 break;
4583 case RID_STATCAST:
4584 postfix_expression
4585 = build_static_cast (type, expression, tf_warning_or_error);
4586 break;
4587 case RID_REINTCAST:
4588 postfix_expression
4589 = build_reinterpret_cast (type, expression,
4590 tf_warning_or_error);
4591 break;
4592 case RID_CONSTCAST:
4593 postfix_expression
4594 = build_const_cast (type, expression, tf_warning_or_error);
4595 break;
4596 default:
4597 gcc_unreachable ();
4600 break;
4602 case RID_TYPEID:
4604 tree type;
4605 const char *saved_message;
4606 bool saved_in_type_id_in_expr_p;
4608 /* Consume the `typeid' token. */
4609 cp_lexer_consume_token (parser->lexer);
4610 /* Look for the `(' token. */
4611 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4612 /* Types cannot be defined in a `typeid' expression. */
4613 saved_message = parser->type_definition_forbidden_message;
4614 parser->type_definition_forbidden_message
4615 = G_("types may not be defined in a %<typeid%> expression");
4616 /* We can't be sure yet whether we're looking at a type-id or an
4617 expression. */
4618 cp_parser_parse_tentatively (parser);
4619 /* Try a type-id first. */
4620 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4621 parser->in_type_id_in_expr_p = true;
4622 type = cp_parser_type_id (parser);
4623 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4624 /* Look for the `)' token. Otherwise, we can't be sure that
4625 we're not looking at an expression: consider `typeid (int
4626 (3))', for example. */
4627 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4628 /* If all went well, simply lookup the type-id. */
4629 if (cp_parser_parse_definitely (parser))
4630 postfix_expression = get_typeid (type);
4631 /* Otherwise, fall back to the expression variant. */
4632 else
4634 tree expression;
4636 /* Look for an expression. */
4637 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4638 /* Compute its typeid. */
4639 postfix_expression = build_typeid (expression);
4640 /* Look for the `)' token. */
4641 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4643 /* Restore the saved message. */
4644 parser->type_definition_forbidden_message = saved_message;
4645 /* `typeid' may not appear in an integral constant expression. */
4646 if (cp_parser_non_integral_constant_expression(parser,
4647 "%<typeid%> operator"))
4648 return error_mark_node;
4650 break;
4652 case RID_TYPENAME:
4654 tree type;
4655 /* The syntax permitted here is the same permitted for an
4656 elaborated-type-specifier. */
4657 type = cp_parser_elaborated_type_specifier (parser,
4658 /*is_friend=*/false,
4659 /*is_declaration=*/false);
4660 postfix_expression = cp_parser_functional_cast (parser, type);
4662 break;
4664 default:
4666 tree type;
4668 /* If the next thing is a simple-type-specifier, we may be
4669 looking at a functional cast. We could also be looking at
4670 an id-expression. So, we try the functional cast, and if
4671 that doesn't work we fall back to the primary-expression. */
4672 cp_parser_parse_tentatively (parser);
4673 /* Look for the simple-type-specifier. */
4674 type = cp_parser_simple_type_specifier (parser,
4675 /*decl_specs=*/NULL,
4676 CP_PARSER_FLAGS_NONE);
4677 /* Parse the cast itself. */
4678 if (!cp_parser_error_occurred (parser))
4679 postfix_expression
4680 = cp_parser_functional_cast (parser, type);
4681 /* If that worked, we're done. */
4682 if (cp_parser_parse_definitely (parser))
4683 break;
4685 /* If the functional-cast didn't work out, try a
4686 compound-literal. */
4687 if (cp_parser_allow_gnu_extensions_p (parser)
4688 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4690 VEC(constructor_elt,gc) *initializer_list = NULL;
4691 bool saved_in_type_id_in_expr_p;
4693 cp_parser_parse_tentatively (parser);
4694 /* Consume the `('. */
4695 cp_lexer_consume_token (parser->lexer);
4696 /* Parse the type. */
4697 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4698 parser->in_type_id_in_expr_p = true;
4699 type = cp_parser_type_id (parser);
4700 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4701 /* Look for the `)'. */
4702 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4703 /* Look for the `{'. */
4704 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4705 /* If things aren't going well, there's no need to
4706 keep going. */
4707 if (!cp_parser_error_occurred (parser))
4709 bool non_constant_p;
4710 /* Parse the initializer-list. */
4711 initializer_list
4712 = cp_parser_initializer_list (parser, &non_constant_p);
4713 /* Allow a trailing `,'. */
4714 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4715 cp_lexer_consume_token (parser->lexer);
4716 /* Look for the final `}'. */
4717 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4719 /* If that worked, we're definitely looking at a
4720 compound-literal expression. */
4721 if (cp_parser_parse_definitely (parser))
4723 /* Warn the user that a compound literal is not
4724 allowed in standard C++. */
4725 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4726 /* For simplicity, we disallow compound literals in
4727 constant-expressions. We could
4728 allow compound literals of integer type, whose
4729 initializer was a constant, in constant
4730 expressions. Permitting that usage, as a further
4731 extension, would not change the meaning of any
4732 currently accepted programs. (Of course, as
4733 compound literals are not part of ISO C++, the
4734 standard has nothing to say.) */
4735 if (cp_parser_non_integral_constant_expression
4736 (parser, "non-constant compound literals"))
4738 postfix_expression = error_mark_node;
4739 break;
4741 /* Form the representation of the compound-literal. */
4742 postfix_expression
4743 = (finish_compound_literal
4744 (type, build_constructor (init_list_type_node,
4745 initializer_list)));
4746 break;
4750 /* It must be a primary-expression. */
4751 postfix_expression
4752 = cp_parser_primary_expression (parser, address_p, cast_p,
4753 /*template_arg_p=*/false,
4754 &idk);
4756 break;
4759 /* Keep looping until the postfix-expression is complete. */
4760 while (true)
4762 if (idk == CP_ID_KIND_UNQUALIFIED
4763 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4764 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4765 /* It is not a Koenig lookup function call. */
4766 postfix_expression
4767 = unqualified_name_lookup_error (postfix_expression);
4769 /* Peek at the next token. */
4770 token = cp_lexer_peek_token (parser->lexer);
4772 switch (token->type)
4774 case CPP_OPEN_SQUARE:
4775 postfix_expression
4776 = cp_parser_postfix_open_square_expression (parser,
4777 postfix_expression,
4778 false);
4779 idk = CP_ID_KIND_NONE;
4780 is_member_access = false;
4781 break;
4783 case CPP_OPEN_PAREN:
4784 /* postfix-expression ( expression-list [opt] ) */
4786 bool koenig_p;
4787 bool is_builtin_constant_p;
4788 bool saved_integral_constant_expression_p = false;
4789 bool saved_non_integral_constant_expression_p = false;
4790 VEC(tree,gc) *args;
4792 is_member_access = false;
4794 is_builtin_constant_p
4795 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4796 if (is_builtin_constant_p)
4798 /* The whole point of __builtin_constant_p is to allow
4799 non-constant expressions to appear as arguments. */
4800 saved_integral_constant_expression_p
4801 = parser->integral_constant_expression_p;
4802 saved_non_integral_constant_expression_p
4803 = parser->non_integral_constant_expression_p;
4804 parser->integral_constant_expression_p = false;
4806 args = (cp_parser_parenthesized_expression_list
4807 (parser, /*is_attribute_list=*/false,
4808 /*cast_p=*/false, /*allow_expansion_p=*/true,
4809 /*non_constant_p=*/NULL));
4810 if (is_builtin_constant_p)
4812 parser->integral_constant_expression_p
4813 = saved_integral_constant_expression_p;
4814 parser->non_integral_constant_expression_p
4815 = saved_non_integral_constant_expression_p;
4818 if (args == NULL)
4820 postfix_expression = error_mark_node;
4821 break;
4824 /* Function calls are not permitted in
4825 constant-expressions. */
4826 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4827 && cp_parser_non_integral_constant_expression (parser,
4828 "a function call"))
4830 postfix_expression = error_mark_node;
4831 release_tree_vector (args);
4832 break;
4835 koenig_p = false;
4836 if (idk == CP_ID_KIND_UNQUALIFIED
4837 || idk == CP_ID_KIND_TEMPLATE_ID)
4839 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4841 if (!VEC_empty (tree, args))
4843 koenig_p = true;
4844 if (!any_type_dependent_arguments_p (args))
4845 postfix_expression
4846 = perform_koenig_lookup (postfix_expression, args);
4848 else
4849 postfix_expression
4850 = unqualified_fn_lookup_error (postfix_expression);
4852 /* We do not perform argument-dependent lookup if
4853 normal lookup finds a non-function, in accordance
4854 with the expected resolution of DR 218. */
4855 else if (!VEC_empty (tree, args)
4856 && is_overloaded_fn (postfix_expression))
4858 tree fn = get_first_fn (postfix_expression);
4859 fn = STRIP_TEMPLATE (fn);
4861 /* Do not do argument dependent lookup if regular
4862 lookup finds a member function or a block-scope
4863 function declaration. [basic.lookup.argdep]/3 */
4864 if (!DECL_FUNCTION_MEMBER_P (fn)
4865 && !DECL_LOCAL_FUNCTION_P (fn))
4867 koenig_p = true;
4868 if (!any_type_dependent_arguments_p (args))
4869 postfix_expression
4870 = perform_koenig_lookup (postfix_expression, args);
4875 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4877 tree instance = TREE_OPERAND (postfix_expression, 0);
4878 tree fn = TREE_OPERAND (postfix_expression, 1);
4880 if (processing_template_decl
4881 && (type_dependent_expression_p (instance)
4882 || (!BASELINK_P (fn)
4883 && TREE_CODE (fn) != FIELD_DECL)
4884 || type_dependent_expression_p (fn)
4885 || any_type_dependent_arguments_p (args)))
4887 postfix_expression
4888 = build_nt_call_vec (postfix_expression, args);
4889 release_tree_vector (args);
4890 break;
4893 if (BASELINK_P (fn))
4895 postfix_expression
4896 = (build_new_method_call
4897 (instance, fn, &args, NULL_TREE,
4898 (idk == CP_ID_KIND_QUALIFIED
4899 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4900 /*fn_p=*/NULL,
4901 tf_warning_or_error));
4903 else
4904 postfix_expression
4905 = finish_call_expr (postfix_expression, &args,
4906 /*disallow_virtual=*/false,
4907 /*koenig_p=*/false,
4908 tf_warning_or_error);
4910 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4911 || TREE_CODE (postfix_expression) == MEMBER_REF
4912 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4913 postfix_expression = (build_offset_ref_call_from_tree
4914 (postfix_expression, &args));
4915 else if (idk == CP_ID_KIND_QUALIFIED)
4916 /* A call to a static class member, or a namespace-scope
4917 function. */
4918 postfix_expression
4919 = finish_call_expr (postfix_expression, &args,
4920 /*disallow_virtual=*/true,
4921 koenig_p,
4922 tf_warning_or_error);
4923 else
4924 /* All other function calls. */
4925 postfix_expression
4926 = finish_call_expr (postfix_expression, &args,
4927 /*disallow_virtual=*/false,
4928 koenig_p,
4929 tf_warning_or_error);
4931 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4932 idk = CP_ID_KIND_NONE;
4934 release_tree_vector (args);
4936 break;
4938 case CPP_DOT:
4939 case CPP_DEREF:
4940 /* postfix-expression . template [opt] id-expression
4941 postfix-expression . pseudo-destructor-name
4942 postfix-expression -> template [opt] id-expression
4943 postfix-expression -> pseudo-destructor-name */
4945 /* Consume the `.' or `->' operator. */
4946 cp_lexer_consume_token (parser->lexer);
4948 postfix_expression
4949 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4950 postfix_expression,
4951 false, &idk,
4952 token->location);
4954 is_member_access = true;
4955 break;
4957 case CPP_PLUS_PLUS:
4958 /* postfix-expression ++ */
4959 /* Consume the `++' token. */
4960 cp_lexer_consume_token (parser->lexer);
4961 /* Generate a representation for the complete expression. */
4962 postfix_expression
4963 = finish_increment_expr (postfix_expression,
4964 POSTINCREMENT_EXPR);
4965 /* Increments may not appear in constant-expressions. */
4966 if (cp_parser_non_integral_constant_expression (parser,
4967 "an increment"))
4968 postfix_expression = error_mark_node;
4969 idk = CP_ID_KIND_NONE;
4970 is_member_access = false;
4971 break;
4973 case CPP_MINUS_MINUS:
4974 /* postfix-expression -- */
4975 /* Consume the `--' token. */
4976 cp_lexer_consume_token (parser->lexer);
4977 /* Generate a representation for the complete expression. */
4978 postfix_expression
4979 = finish_increment_expr (postfix_expression,
4980 POSTDECREMENT_EXPR);
4981 /* Decrements may not appear in constant-expressions. */
4982 if (cp_parser_non_integral_constant_expression (parser,
4983 "a decrement"))
4984 postfix_expression = error_mark_node;
4985 idk = CP_ID_KIND_NONE;
4986 is_member_access = false;
4987 break;
4989 default:
4990 if (pidk_return != NULL)
4991 * pidk_return = idk;
4992 if (member_access_only_p)
4993 return is_member_access? postfix_expression : error_mark_node;
4994 else
4995 return postfix_expression;
4999 /* We should never get here. */
5000 gcc_unreachable ();
5001 return error_mark_node;
5004 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5005 by cp_parser_builtin_offsetof. We're looking for
5007 postfix-expression [ expression ]
5009 FOR_OFFSETOF is set if we're being called in that context, which
5010 changes how we deal with integer constant expressions. */
5012 static tree
5013 cp_parser_postfix_open_square_expression (cp_parser *parser,
5014 tree postfix_expression,
5015 bool for_offsetof)
5017 tree index;
5019 /* Consume the `[' token. */
5020 cp_lexer_consume_token (parser->lexer);
5022 /* Parse the index expression. */
5023 /* ??? For offsetof, there is a question of what to allow here. If
5024 offsetof is not being used in an integral constant expression context,
5025 then we *could* get the right answer by computing the value at runtime.
5026 If we are in an integral constant expression context, then we might
5027 could accept any constant expression; hard to say without analysis.
5028 Rather than open the barn door too wide right away, allow only integer
5029 constant expressions here. */
5030 if (for_offsetof)
5031 index = cp_parser_constant_expression (parser, false, NULL);
5032 else
5033 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5035 /* Look for the closing `]'. */
5036 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5038 /* Build the ARRAY_REF. */
5039 postfix_expression = grok_array_decl (postfix_expression, index);
5041 /* When not doing offsetof, array references are not permitted in
5042 constant-expressions. */
5043 if (!for_offsetof
5044 && (cp_parser_non_integral_constant_expression
5045 (parser, "an array reference")))
5046 postfix_expression = error_mark_node;
5048 return postfix_expression;
5051 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5052 by cp_parser_builtin_offsetof. We're looking for
5054 postfix-expression . template [opt] id-expression
5055 postfix-expression . pseudo-destructor-name
5056 postfix-expression -> template [opt] id-expression
5057 postfix-expression -> pseudo-destructor-name
5059 FOR_OFFSETOF is set if we're being called in that context. That sorta
5060 limits what of the above we'll actually accept, but nevermind.
5061 TOKEN_TYPE is the "." or "->" token, which will already have been
5062 removed from the stream. */
5064 static tree
5065 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5066 enum cpp_ttype token_type,
5067 tree postfix_expression,
5068 bool for_offsetof, cp_id_kind *idk,
5069 location_t location)
5071 tree name;
5072 bool dependent_p;
5073 bool pseudo_destructor_p;
5074 tree scope = NULL_TREE;
5076 /* If this is a `->' operator, dereference the pointer. */
5077 if (token_type == CPP_DEREF)
5078 postfix_expression = build_x_arrow (postfix_expression);
5079 /* Check to see whether or not the expression is type-dependent. */
5080 dependent_p = type_dependent_expression_p (postfix_expression);
5081 /* The identifier following the `->' or `.' is not qualified. */
5082 parser->scope = NULL_TREE;
5083 parser->qualifying_scope = NULL_TREE;
5084 parser->object_scope = NULL_TREE;
5085 *idk = CP_ID_KIND_NONE;
5087 /* Enter the scope corresponding to the type of the object
5088 given by the POSTFIX_EXPRESSION. */
5089 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5091 scope = TREE_TYPE (postfix_expression);
5092 /* According to the standard, no expression should ever have
5093 reference type. Unfortunately, we do not currently match
5094 the standard in this respect in that our internal representation
5095 of an expression may have reference type even when the standard
5096 says it does not. Therefore, we have to manually obtain the
5097 underlying type here. */
5098 scope = non_reference (scope);
5099 /* The type of the POSTFIX_EXPRESSION must be complete. */
5100 if (scope == unknown_type_node)
5102 error_at (location, "%qE does not have class type",
5103 postfix_expression);
5104 scope = NULL_TREE;
5106 else
5107 scope = complete_type_or_else (scope, NULL_TREE);
5108 /* Let the name lookup machinery know that we are processing a
5109 class member access expression. */
5110 parser->context->object_type = scope;
5111 /* If something went wrong, we want to be able to discern that case,
5112 as opposed to the case where there was no SCOPE due to the type
5113 of expression being dependent. */
5114 if (!scope)
5115 scope = error_mark_node;
5116 /* If the SCOPE was erroneous, make the various semantic analysis
5117 functions exit quickly -- and without issuing additional error
5118 messages. */
5119 if (scope == error_mark_node)
5120 postfix_expression = error_mark_node;
5123 /* Assume this expression is not a pseudo-destructor access. */
5124 pseudo_destructor_p = false;
5126 /* If the SCOPE is a scalar type, then, if this is a valid program,
5127 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5128 is type dependent, it can be pseudo-destructor-name or something else.
5129 Try to parse it as pseudo-destructor-name first. */
5130 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5132 tree s;
5133 tree type;
5135 cp_parser_parse_tentatively (parser);
5136 /* Parse the pseudo-destructor-name. */
5137 s = NULL_TREE;
5138 cp_parser_pseudo_destructor_name (parser, &s, &type);
5139 if (dependent_p
5140 && (cp_parser_error_occurred (parser)
5141 || TREE_CODE (type) != TYPE_DECL
5142 || !SCALAR_TYPE_P (TREE_TYPE (type))))
5143 cp_parser_abort_tentative_parse (parser);
5144 else if (cp_parser_parse_definitely (parser))
5146 pseudo_destructor_p = true;
5147 postfix_expression
5148 = finish_pseudo_destructor_expr (postfix_expression,
5149 s, TREE_TYPE (type));
5153 if (!pseudo_destructor_p)
5155 /* If the SCOPE is not a scalar type, we are looking at an
5156 ordinary class member access expression, rather than a
5157 pseudo-destructor-name. */
5158 bool template_p;
5159 cp_token *token = cp_lexer_peek_token (parser->lexer);
5160 /* Parse the id-expression. */
5161 name = (cp_parser_id_expression
5162 (parser,
5163 cp_parser_optional_template_keyword (parser),
5164 /*check_dependency_p=*/true,
5165 &template_p,
5166 /*declarator_p=*/false,
5167 /*optional_p=*/false));
5168 /* In general, build a SCOPE_REF if the member name is qualified.
5169 However, if the name was not dependent and has already been
5170 resolved; there is no need to build the SCOPE_REF. For example;
5172 struct X { void f(); };
5173 template <typename T> void f(T* t) { t->X::f(); }
5175 Even though "t" is dependent, "X::f" is not and has been resolved
5176 to a BASELINK; there is no need to include scope information. */
5178 /* But we do need to remember that there was an explicit scope for
5179 virtual function calls. */
5180 if (parser->scope)
5181 *idk = CP_ID_KIND_QUALIFIED;
5183 /* If the name is a template-id that names a type, we will get a
5184 TYPE_DECL here. That is invalid code. */
5185 if (TREE_CODE (name) == TYPE_DECL)
5187 error_at (token->location, "invalid use of %qD", name);
5188 postfix_expression = error_mark_node;
5190 else
5192 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5194 name = build_qualified_name (/*type=*/NULL_TREE,
5195 parser->scope,
5196 name,
5197 template_p);
5198 parser->scope = NULL_TREE;
5199 parser->qualifying_scope = NULL_TREE;
5200 parser->object_scope = NULL_TREE;
5202 if (scope && name && BASELINK_P (name))
5203 adjust_result_of_qualified_name_lookup
5204 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5205 postfix_expression
5206 = finish_class_member_access_expr (postfix_expression, name,
5207 template_p,
5208 tf_warning_or_error);
5212 /* We no longer need to look up names in the scope of the object on
5213 the left-hand side of the `.' or `->' operator. */
5214 parser->context->object_type = NULL_TREE;
5216 /* Outside of offsetof, these operators may not appear in
5217 constant-expressions. */
5218 if (!for_offsetof
5219 && (cp_parser_non_integral_constant_expression
5220 (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5221 postfix_expression = error_mark_node;
5223 return postfix_expression;
5226 /* Parse a parenthesized expression-list.
5228 expression-list:
5229 assignment-expression
5230 expression-list, assignment-expression
5232 attribute-list:
5233 expression-list
5234 identifier
5235 identifier, expression-list
5237 CAST_P is true if this expression is the target of a cast.
5239 ALLOW_EXPANSION_P is true if this expression allows expansion of an
5240 argument pack.
5242 Returns a vector of trees. Each element is a representation of an
5243 assignment-expression. NULL is returned if the ( and or ) are
5244 missing. An empty, but allocated, vector is returned on no
5245 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is true
5246 if this is really an attribute list being parsed. If
5247 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5248 not all of the expressions in the list were constant. */
5250 static VEC(tree,gc) *
5251 cp_parser_parenthesized_expression_list (cp_parser* parser,
5252 bool is_attribute_list,
5253 bool cast_p,
5254 bool allow_expansion_p,
5255 bool *non_constant_p)
5257 VEC(tree,gc) *expression_list;
5258 bool fold_expr_p = is_attribute_list;
5259 tree identifier = NULL_TREE;
5260 bool saved_greater_than_is_operator_p;
5262 /* Assume all the expressions will be constant. */
5263 if (non_constant_p)
5264 *non_constant_p = false;
5266 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5267 return NULL;
5269 expression_list = make_tree_vector ();
5271 /* Within a parenthesized expression, a `>' token is always
5272 the greater-than operator. */
5273 saved_greater_than_is_operator_p
5274 = parser->greater_than_is_operator_p;
5275 parser->greater_than_is_operator_p = true;
5277 /* Consume expressions until there are no more. */
5278 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5279 while (true)
5281 tree expr;
5283 /* At the beginning of attribute lists, check to see if the
5284 next token is an identifier. */
5285 if (is_attribute_list
5286 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5288 cp_token *token;
5290 /* Consume the identifier. */
5291 token = cp_lexer_consume_token (parser->lexer);
5292 /* Save the identifier. */
5293 identifier = token->u.value;
5295 else
5297 bool expr_non_constant_p;
5299 /* Parse the next assignment-expression. */
5300 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5302 /* A braced-init-list. */
5303 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5304 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5305 if (non_constant_p && expr_non_constant_p)
5306 *non_constant_p = true;
5308 else if (non_constant_p)
5310 expr = (cp_parser_constant_expression
5311 (parser, /*allow_non_constant_p=*/true,
5312 &expr_non_constant_p));
5313 if (expr_non_constant_p)
5314 *non_constant_p = true;
5316 else
5317 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5319 if (fold_expr_p)
5320 expr = fold_non_dependent_expr (expr);
5322 /* If we have an ellipsis, then this is an expression
5323 expansion. */
5324 if (allow_expansion_p
5325 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5327 /* Consume the `...'. */
5328 cp_lexer_consume_token (parser->lexer);
5330 /* Build the argument pack. */
5331 expr = make_pack_expansion (expr);
5334 /* Add it to the list. We add error_mark_node
5335 expressions to the list, so that we can still tell if
5336 the correct form for a parenthesized expression-list
5337 is found. That gives better errors. */
5338 VEC_safe_push (tree, gc, expression_list, expr);
5340 if (expr == error_mark_node)
5341 goto skip_comma;
5344 /* After the first item, attribute lists look the same as
5345 expression lists. */
5346 is_attribute_list = false;
5348 get_comma:;
5349 /* If the next token isn't a `,', then we are done. */
5350 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5351 break;
5353 /* Otherwise, consume the `,' and keep going. */
5354 cp_lexer_consume_token (parser->lexer);
5357 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5359 int ending;
5361 skip_comma:;
5362 /* We try and resync to an unnested comma, as that will give the
5363 user better diagnostics. */
5364 ending = cp_parser_skip_to_closing_parenthesis (parser,
5365 /*recovering=*/true,
5366 /*or_comma=*/true,
5367 /*consume_paren=*/true);
5368 if (ending < 0)
5369 goto get_comma;
5370 if (!ending)
5372 parser->greater_than_is_operator_p
5373 = saved_greater_than_is_operator_p;
5374 return NULL;
5378 parser->greater_than_is_operator_p
5379 = saved_greater_than_is_operator_p;
5381 if (identifier)
5382 VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5384 return expression_list;
5387 /* Parse a pseudo-destructor-name.
5389 pseudo-destructor-name:
5390 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5391 :: [opt] nested-name-specifier template template-id :: ~ type-name
5392 :: [opt] nested-name-specifier [opt] ~ type-name
5394 If either of the first two productions is used, sets *SCOPE to the
5395 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5396 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5397 or ERROR_MARK_NODE if the parse fails. */
5399 static void
5400 cp_parser_pseudo_destructor_name (cp_parser* parser,
5401 tree* scope,
5402 tree* type)
5404 bool nested_name_specifier_p;
5406 /* Assume that things will not work out. */
5407 *type = error_mark_node;
5409 /* Look for the optional `::' operator. */
5410 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5411 /* Look for the optional nested-name-specifier. */
5412 nested_name_specifier_p
5413 = (cp_parser_nested_name_specifier_opt (parser,
5414 /*typename_keyword_p=*/false,
5415 /*check_dependency_p=*/true,
5416 /*type_p=*/false,
5417 /*is_declaration=*/false)
5418 != NULL_TREE);
5419 /* Now, if we saw a nested-name-specifier, we might be doing the
5420 second production. */
5421 if (nested_name_specifier_p
5422 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5424 /* Consume the `template' keyword. */
5425 cp_lexer_consume_token (parser->lexer);
5426 /* Parse the template-id. */
5427 cp_parser_template_id (parser,
5428 /*template_keyword_p=*/true,
5429 /*check_dependency_p=*/false,
5430 /*is_declaration=*/true);
5431 /* Look for the `::' token. */
5432 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5434 /* If the next token is not a `~', then there might be some
5435 additional qualification. */
5436 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5438 /* At this point, we're looking for "type-name :: ~". The type-name
5439 must not be a class-name, since this is a pseudo-destructor. So,
5440 it must be either an enum-name, or a typedef-name -- both of which
5441 are just identifiers. So, we peek ahead to check that the "::"
5442 and "~" tokens are present; if they are not, then we can avoid
5443 calling type_name. */
5444 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5445 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5446 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5448 cp_parser_error (parser, "non-scalar type");
5449 return;
5452 /* Look for the type-name. */
5453 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5454 if (*scope == error_mark_node)
5455 return;
5457 /* Look for the `::' token. */
5458 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5460 else
5461 *scope = NULL_TREE;
5463 /* Look for the `~'. */
5464 cp_parser_require (parser, CPP_COMPL, "%<~%>");
5465 /* Look for the type-name again. We are not responsible for
5466 checking that it matches the first type-name. */
5467 *type = cp_parser_nonclass_name (parser);
5470 /* Parse a unary-expression.
5472 unary-expression:
5473 postfix-expression
5474 ++ cast-expression
5475 -- cast-expression
5476 unary-operator cast-expression
5477 sizeof unary-expression
5478 sizeof ( type-id )
5479 new-expression
5480 delete-expression
5482 GNU Extensions:
5484 unary-expression:
5485 __extension__ cast-expression
5486 __alignof__ unary-expression
5487 __alignof__ ( type-id )
5488 __real__ cast-expression
5489 __imag__ cast-expression
5490 && identifier
5492 ADDRESS_P is true iff the unary-expression is appearing as the
5493 operand of the `&' operator. CAST_P is true if this expression is
5494 the target of a cast.
5496 Returns a representation of the expression. */
5498 static tree
5499 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5500 cp_id_kind * pidk)
5502 cp_token *token;
5503 enum tree_code unary_operator;
5505 /* Peek at the next token. */
5506 token = cp_lexer_peek_token (parser->lexer);
5507 /* Some keywords give away the kind of expression. */
5508 if (token->type == CPP_KEYWORD)
5510 enum rid keyword = token->keyword;
5512 switch (keyword)
5514 case RID_ALIGNOF:
5515 case RID_SIZEOF:
5517 tree operand;
5518 enum tree_code op;
5520 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5521 /* Consume the token. */
5522 cp_lexer_consume_token (parser->lexer);
5523 /* Parse the operand. */
5524 operand = cp_parser_sizeof_operand (parser, keyword);
5526 if (TYPE_P (operand))
5527 return cxx_sizeof_or_alignof_type (operand, op, true);
5528 else
5529 return cxx_sizeof_or_alignof_expr (operand, op, true);
5532 case RID_NEW:
5533 return cp_parser_new_expression (parser);
5535 case RID_DELETE:
5536 return cp_parser_delete_expression (parser);
5538 case RID_EXTENSION:
5540 /* The saved value of the PEDANTIC flag. */
5541 int saved_pedantic;
5542 tree expr;
5544 /* Save away the PEDANTIC flag. */
5545 cp_parser_extension_opt (parser, &saved_pedantic);
5546 /* Parse the cast-expression. */
5547 expr = cp_parser_simple_cast_expression (parser);
5548 /* Restore the PEDANTIC flag. */
5549 pedantic = saved_pedantic;
5551 return expr;
5554 case RID_REALPART:
5555 case RID_IMAGPART:
5557 tree expression;
5559 /* Consume the `__real__' or `__imag__' token. */
5560 cp_lexer_consume_token (parser->lexer);
5561 /* Parse the cast-expression. */
5562 expression = cp_parser_simple_cast_expression (parser);
5563 /* Create the complete representation. */
5564 return build_x_unary_op ((keyword == RID_REALPART
5565 ? REALPART_EXPR : IMAGPART_EXPR),
5566 expression,
5567 tf_warning_or_error);
5569 break;
5571 default:
5572 break;
5576 /* Look for the `:: new' and `:: delete', which also signal the
5577 beginning of a new-expression, or delete-expression,
5578 respectively. If the next token is `::', then it might be one of
5579 these. */
5580 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5582 enum rid keyword;
5584 /* See if the token after the `::' is one of the keywords in
5585 which we're interested. */
5586 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5587 /* If it's `new', we have a new-expression. */
5588 if (keyword == RID_NEW)
5589 return cp_parser_new_expression (parser);
5590 /* Similarly, for `delete'. */
5591 else if (keyword == RID_DELETE)
5592 return cp_parser_delete_expression (parser);
5595 /* Look for a unary operator. */
5596 unary_operator = cp_parser_unary_operator (token);
5597 /* The `++' and `--' operators can be handled similarly, even though
5598 they are not technically unary-operators in the grammar. */
5599 if (unary_operator == ERROR_MARK)
5601 if (token->type == CPP_PLUS_PLUS)
5602 unary_operator = PREINCREMENT_EXPR;
5603 else if (token->type == CPP_MINUS_MINUS)
5604 unary_operator = PREDECREMENT_EXPR;
5605 /* Handle the GNU address-of-label extension. */
5606 else if (cp_parser_allow_gnu_extensions_p (parser)
5607 && token->type == CPP_AND_AND)
5609 tree identifier;
5610 tree expression;
5611 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5613 /* Consume the '&&' token. */
5614 cp_lexer_consume_token (parser->lexer);
5615 /* Look for the identifier. */
5616 identifier = cp_parser_identifier (parser);
5617 /* Create an expression representing the address. */
5618 expression = finish_label_address_expr (identifier, loc);
5619 if (cp_parser_non_integral_constant_expression (parser,
5620 "the address of a label"))
5621 expression = error_mark_node;
5622 return expression;
5625 if (unary_operator != ERROR_MARK)
5627 tree cast_expression;
5628 tree expression = error_mark_node;
5629 const char *non_constant_p = NULL;
5631 /* Consume the operator token. */
5632 token = cp_lexer_consume_token (parser->lexer);
5633 /* Parse the cast-expression. */
5634 cast_expression
5635 = cp_parser_cast_expression (parser,
5636 unary_operator == ADDR_EXPR,
5637 /*cast_p=*/false, pidk);
5638 /* Now, build an appropriate representation. */
5639 switch (unary_operator)
5641 case INDIRECT_REF:
5642 non_constant_p = "%<*%>";
5643 expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR,
5644 tf_warning_or_error);
5645 break;
5647 case ADDR_EXPR:
5648 non_constant_p = "%<&%>";
5649 /* Fall through. */
5650 case BIT_NOT_EXPR:
5651 expression = build_x_unary_op (unary_operator, cast_expression,
5652 tf_warning_or_error);
5653 break;
5655 case PREINCREMENT_EXPR:
5656 case PREDECREMENT_EXPR:
5657 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5658 ? "%<++%>" : "%<--%>");
5659 /* Fall through. */
5660 case UNARY_PLUS_EXPR:
5661 case NEGATE_EXPR:
5662 case TRUTH_NOT_EXPR:
5663 expression = finish_unary_op_expr (unary_operator, cast_expression);
5664 break;
5666 default:
5667 gcc_unreachable ();
5670 if (non_constant_p
5671 && cp_parser_non_integral_constant_expression (parser,
5672 non_constant_p))
5673 expression = error_mark_node;
5675 return expression;
5678 return cp_parser_postfix_expression (parser, address_p, cast_p,
5679 /*member_access_only_p=*/false,
5680 pidk);
5683 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5684 unary-operator, the corresponding tree code is returned. */
5686 static enum tree_code
5687 cp_parser_unary_operator (cp_token* token)
5689 switch (token->type)
5691 case CPP_MULT:
5692 return INDIRECT_REF;
5694 case CPP_AND:
5695 return ADDR_EXPR;
5697 case CPP_PLUS:
5698 return UNARY_PLUS_EXPR;
5700 case CPP_MINUS:
5701 return NEGATE_EXPR;
5703 case CPP_NOT:
5704 return TRUTH_NOT_EXPR;
5706 case CPP_COMPL:
5707 return BIT_NOT_EXPR;
5709 default:
5710 return ERROR_MARK;
5714 /* Parse a new-expression.
5716 new-expression:
5717 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5718 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5720 Returns a representation of the expression. */
5722 static tree
5723 cp_parser_new_expression (cp_parser* parser)
5725 bool global_scope_p;
5726 VEC(tree,gc) *placement;
5727 tree type;
5728 VEC(tree,gc) *initializer;
5729 tree nelts;
5730 tree ret;
5732 /* Look for the optional `::' operator. */
5733 global_scope_p
5734 = (cp_parser_global_scope_opt (parser,
5735 /*current_scope_valid_p=*/false)
5736 != NULL_TREE);
5737 /* Look for the `new' operator. */
5738 cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5739 /* There's no easy way to tell a new-placement from the
5740 `( type-id )' construct. */
5741 cp_parser_parse_tentatively (parser);
5742 /* Look for a new-placement. */
5743 placement = cp_parser_new_placement (parser);
5744 /* If that didn't work out, there's no new-placement. */
5745 if (!cp_parser_parse_definitely (parser))
5747 if (placement != NULL)
5748 release_tree_vector (placement);
5749 placement = NULL;
5752 /* If the next token is a `(', then we have a parenthesized
5753 type-id. */
5754 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5756 cp_token *token;
5757 /* Consume the `('. */
5758 cp_lexer_consume_token (parser->lexer);
5759 /* Parse the type-id. */
5760 type = cp_parser_type_id (parser);
5761 /* Look for the closing `)'. */
5762 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5763 token = cp_lexer_peek_token (parser->lexer);
5764 /* There should not be a direct-new-declarator in this production,
5765 but GCC used to allowed this, so we check and emit a sensible error
5766 message for this case. */
5767 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5769 error_at (token->location,
5770 "array bound forbidden after parenthesized type-id");
5771 inform (token->location,
5772 "try removing the parentheses around the type-id");
5773 cp_parser_direct_new_declarator (parser);
5775 nelts = NULL_TREE;
5777 /* Otherwise, there must be a new-type-id. */
5778 else
5779 type = cp_parser_new_type_id (parser, &nelts);
5781 /* If the next token is a `(' or '{', then we have a new-initializer. */
5782 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5783 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5784 initializer = cp_parser_new_initializer (parser);
5785 else
5786 initializer = NULL;
5788 /* A new-expression may not appear in an integral constant
5789 expression. */
5790 if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5791 ret = error_mark_node;
5792 else
5794 /* Create a representation of the new-expression. */
5795 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
5796 tf_warning_or_error);
5799 if (placement != NULL)
5800 release_tree_vector (placement);
5801 if (initializer != NULL)
5802 release_tree_vector (initializer);
5804 return ret;
5807 /* Parse a new-placement.
5809 new-placement:
5810 ( expression-list )
5812 Returns the same representation as for an expression-list. */
5814 static VEC(tree,gc) *
5815 cp_parser_new_placement (cp_parser* parser)
5817 VEC(tree,gc) *expression_list;
5819 /* Parse the expression-list. */
5820 expression_list = (cp_parser_parenthesized_expression_list
5821 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5822 /*non_constant_p=*/NULL));
5824 return expression_list;
5827 /* Parse a new-type-id.
5829 new-type-id:
5830 type-specifier-seq new-declarator [opt]
5832 Returns the TYPE allocated. If the new-type-id indicates an array
5833 type, *NELTS is set to the number of elements in the last array
5834 bound; the TYPE will not include the last array bound. */
5836 static tree
5837 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5839 cp_decl_specifier_seq type_specifier_seq;
5840 cp_declarator *new_declarator;
5841 cp_declarator *declarator;
5842 cp_declarator *outer_declarator;
5843 const char *saved_message;
5844 tree type;
5846 /* The type-specifier sequence must not contain type definitions.
5847 (It cannot contain declarations of new types either, but if they
5848 are not definitions we will catch that because they are not
5849 complete.) */
5850 saved_message = parser->type_definition_forbidden_message;
5851 parser->type_definition_forbidden_message
5852 = G_("types may not be defined in a new-type-id");
5853 /* Parse the type-specifier-seq. */
5854 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
5855 /*is_trailing_return=*/false,
5856 &type_specifier_seq);
5857 /* Restore the old message. */
5858 parser->type_definition_forbidden_message = saved_message;
5859 /* Parse the new-declarator. */
5860 new_declarator = cp_parser_new_declarator_opt (parser);
5862 /* Determine the number of elements in the last array dimension, if
5863 any. */
5864 *nelts = NULL_TREE;
5865 /* Skip down to the last array dimension. */
5866 declarator = new_declarator;
5867 outer_declarator = NULL;
5868 while (declarator && (declarator->kind == cdk_pointer
5869 || declarator->kind == cdk_ptrmem))
5871 outer_declarator = declarator;
5872 declarator = declarator->declarator;
5874 while (declarator
5875 && declarator->kind == cdk_array
5876 && declarator->declarator
5877 && declarator->declarator->kind == cdk_array)
5879 outer_declarator = declarator;
5880 declarator = declarator->declarator;
5883 if (declarator && declarator->kind == cdk_array)
5885 *nelts = declarator->u.array.bounds;
5886 if (*nelts == error_mark_node)
5887 *nelts = integer_one_node;
5889 if (outer_declarator)
5890 outer_declarator->declarator = declarator->declarator;
5891 else
5892 new_declarator = NULL;
5895 type = groktypename (&type_specifier_seq, new_declarator, false);
5896 return type;
5899 /* Parse an (optional) new-declarator.
5901 new-declarator:
5902 ptr-operator new-declarator [opt]
5903 direct-new-declarator
5905 Returns the declarator. */
5907 static cp_declarator *
5908 cp_parser_new_declarator_opt (cp_parser* parser)
5910 enum tree_code code;
5911 tree type;
5912 cp_cv_quals cv_quals;
5914 /* We don't know if there's a ptr-operator next, or not. */
5915 cp_parser_parse_tentatively (parser);
5916 /* Look for a ptr-operator. */
5917 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5918 /* If that worked, look for more new-declarators. */
5919 if (cp_parser_parse_definitely (parser))
5921 cp_declarator *declarator;
5923 /* Parse another optional declarator. */
5924 declarator = cp_parser_new_declarator_opt (parser);
5926 return cp_parser_make_indirect_declarator
5927 (code, type, cv_quals, declarator);
5930 /* If the next token is a `[', there is a direct-new-declarator. */
5931 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5932 return cp_parser_direct_new_declarator (parser);
5934 return NULL;
5937 /* Parse a direct-new-declarator.
5939 direct-new-declarator:
5940 [ expression ]
5941 direct-new-declarator [constant-expression]
5945 static cp_declarator *
5946 cp_parser_direct_new_declarator (cp_parser* parser)
5948 cp_declarator *declarator = NULL;
5950 while (true)
5952 tree expression;
5954 /* Look for the opening `['. */
5955 cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5956 /* The first expression is not required to be constant. */
5957 if (!declarator)
5959 cp_token *token = cp_lexer_peek_token (parser->lexer);
5960 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5961 /* The standard requires that the expression have integral
5962 type. DR 74 adds enumeration types. We believe that the
5963 real intent is that these expressions be handled like the
5964 expression in a `switch' condition, which also allows
5965 classes with a single conversion to integral or
5966 enumeration type. */
5967 if (!processing_template_decl)
5969 expression
5970 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5971 expression,
5972 /*complain=*/true);
5973 if (!expression)
5975 error_at (token->location,
5976 "expression in new-declarator must have integral "
5977 "or enumeration type");
5978 expression = error_mark_node;
5982 /* But all the other expressions must be. */
5983 else
5984 expression
5985 = cp_parser_constant_expression (parser,
5986 /*allow_non_constant=*/false,
5987 NULL);
5988 /* Look for the closing `]'. */
5989 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5991 /* Add this bound to the declarator. */
5992 declarator = make_array_declarator (declarator, expression);
5994 /* If the next token is not a `[', then there are no more
5995 bounds. */
5996 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5997 break;
6000 return declarator;
6003 /* Parse a new-initializer.
6005 new-initializer:
6006 ( expression-list [opt] )
6007 braced-init-list
6009 Returns a representation of the expression-list. */
6011 static VEC(tree,gc) *
6012 cp_parser_new_initializer (cp_parser* parser)
6014 VEC(tree,gc) *expression_list;
6016 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6018 tree t;
6019 bool expr_non_constant_p;
6020 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6021 t = cp_parser_braced_list (parser, &expr_non_constant_p);
6022 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6023 expression_list = make_tree_vector_single (t);
6025 else
6026 expression_list = (cp_parser_parenthesized_expression_list
6027 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
6028 /*non_constant_p=*/NULL));
6030 return expression_list;
6033 /* Parse a delete-expression.
6035 delete-expression:
6036 :: [opt] delete cast-expression
6037 :: [opt] delete [ ] cast-expression
6039 Returns a representation of the expression. */
6041 static tree
6042 cp_parser_delete_expression (cp_parser* parser)
6044 bool global_scope_p;
6045 bool array_p;
6046 tree expression;
6048 /* Look for the optional `::' operator. */
6049 global_scope_p
6050 = (cp_parser_global_scope_opt (parser,
6051 /*current_scope_valid_p=*/false)
6052 != NULL_TREE);
6053 /* Look for the `delete' keyword. */
6054 cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
6055 /* See if the array syntax is in use. */
6056 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6058 /* Consume the `[' token. */
6059 cp_lexer_consume_token (parser->lexer);
6060 /* Look for the `]' token. */
6061 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
6062 /* Remember that this is the `[]' construct. */
6063 array_p = true;
6065 else
6066 array_p = false;
6068 /* Parse the cast-expression. */
6069 expression = cp_parser_simple_cast_expression (parser);
6071 /* A delete-expression may not appear in an integral constant
6072 expression. */
6073 if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
6074 return error_mark_node;
6076 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
6079 /* Returns true if TOKEN may start a cast-expression and false
6080 otherwise. */
6082 static bool
6083 cp_parser_token_starts_cast_expression (cp_token *token)
6085 switch (token->type)
6087 case CPP_COMMA:
6088 case CPP_SEMICOLON:
6089 case CPP_QUERY:
6090 case CPP_COLON:
6091 case CPP_CLOSE_SQUARE:
6092 case CPP_CLOSE_PAREN:
6093 case CPP_CLOSE_BRACE:
6094 case CPP_DOT:
6095 case CPP_DOT_STAR:
6096 case CPP_DEREF:
6097 case CPP_DEREF_STAR:
6098 case CPP_DIV:
6099 case CPP_MOD:
6100 case CPP_LSHIFT:
6101 case CPP_RSHIFT:
6102 case CPP_LESS:
6103 case CPP_GREATER:
6104 case CPP_LESS_EQ:
6105 case CPP_GREATER_EQ:
6106 case CPP_EQ_EQ:
6107 case CPP_NOT_EQ:
6108 case CPP_EQ:
6109 case CPP_MULT_EQ:
6110 case CPP_DIV_EQ:
6111 case CPP_MOD_EQ:
6112 case CPP_PLUS_EQ:
6113 case CPP_MINUS_EQ:
6114 case CPP_RSHIFT_EQ:
6115 case CPP_LSHIFT_EQ:
6116 case CPP_AND_EQ:
6117 case CPP_XOR_EQ:
6118 case CPP_OR_EQ:
6119 case CPP_XOR:
6120 case CPP_OR:
6121 case CPP_OR_OR:
6122 case CPP_EOF:
6123 return false;
6125 /* '[' may start a primary-expression in obj-c++. */
6126 case CPP_OPEN_SQUARE:
6127 return c_dialect_objc ();
6129 default:
6130 return true;
6134 /* Parse a cast-expression.
6136 cast-expression:
6137 unary-expression
6138 ( type-id ) cast-expression
6140 ADDRESS_P is true iff the unary-expression is appearing as the
6141 operand of the `&' operator. CAST_P is true if this expression is
6142 the target of a cast.
6144 Returns a representation of the expression. */
6146 static tree
6147 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6148 cp_id_kind * pidk)
6150 /* If it's a `(', then we might be looking at a cast. */
6151 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6153 tree type = NULL_TREE;
6154 tree expr = NULL_TREE;
6155 bool compound_literal_p;
6156 const char *saved_message;
6158 /* There's no way to know yet whether or not this is a cast.
6159 For example, `(int (3))' is a unary-expression, while `(int)
6160 3' is a cast. So, we resort to parsing tentatively. */
6161 cp_parser_parse_tentatively (parser);
6162 /* Types may not be defined in a cast. */
6163 saved_message = parser->type_definition_forbidden_message;
6164 parser->type_definition_forbidden_message
6165 = G_("types may not be defined in casts");
6166 /* Consume the `('. */
6167 cp_lexer_consume_token (parser->lexer);
6168 /* A very tricky bit is that `(struct S) { 3 }' is a
6169 compound-literal (which we permit in C++ as an extension).
6170 But, that construct is not a cast-expression -- it is a
6171 postfix-expression. (The reason is that `(struct S) { 3 }.i'
6172 is legal; if the compound-literal were a cast-expression,
6173 you'd need an extra set of parentheses.) But, if we parse
6174 the type-id, and it happens to be a class-specifier, then we
6175 will commit to the parse at that point, because we cannot
6176 undo the action that is done when creating a new class. So,
6177 then we cannot back up and do a postfix-expression.
6179 Therefore, we scan ahead to the closing `)', and check to see
6180 if the token after the `)' is a `{'. If so, we are not
6181 looking at a cast-expression.
6183 Save tokens so that we can put them back. */
6184 cp_lexer_save_tokens (parser->lexer);
6185 /* Skip tokens until the next token is a closing parenthesis.
6186 If we find the closing `)', and the next token is a `{', then
6187 we are looking at a compound-literal. */
6188 compound_literal_p
6189 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6190 /*consume_paren=*/true)
6191 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6192 /* Roll back the tokens we skipped. */
6193 cp_lexer_rollback_tokens (parser->lexer);
6194 /* If we were looking at a compound-literal, simulate an error
6195 so that the call to cp_parser_parse_definitely below will
6196 fail. */
6197 if (compound_literal_p)
6198 cp_parser_simulate_error (parser);
6199 else
6201 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6202 parser->in_type_id_in_expr_p = true;
6203 /* Look for the type-id. */
6204 type = cp_parser_type_id (parser);
6205 /* Look for the closing `)'. */
6206 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6207 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6210 /* Restore the saved message. */
6211 parser->type_definition_forbidden_message = saved_message;
6213 /* At this point this can only be either a cast or a
6214 parenthesized ctor such as `(T ())' that looks like a cast to
6215 function returning T. */
6216 if (!cp_parser_error_occurred (parser)
6217 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6218 (parser->lexer)))
6220 cp_parser_parse_definitely (parser);
6221 expr = cp_parser_cast_expression (parser,
6222 /*address_p=*/false,
6223 /*cast_p=*/true, pidk);
6225 /* Warn about old-style casts, if so requested. */
6226 if (warn_old_style_cast
6227 && !in_system_header
6228 && !VOID_TYPE_P (type)
6229 && current_lang_name != lang_name_c)
6230 warning (OPT_Wold_style_cast, "use of old-style cast");
6232 /* Only type conversions to integral or enumeration types
6233 can be used in constant-expressions. */
6234 if (!cast_valid_in_integral_constant_expression_p (type)
6235 && (cp_parser_non_integral_constant_expression
6236 (parser,
6237 "a cast to a type other than an integral or "
6238 "enumeration type")))
6239 return error_mark_node;
6241 /* Perform the cast. */
6242 expr = build_c_cast (input_location, type, expr);
6243 return expr;
6245 else
6246 cp_parser_abort_tentative_parse (parser);
6249 /* If we get here, then it's not a cast, so it must be a
6250 unary-expression. */
6251 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6254 /* Parse a binary expression of the general form:
6256 pm-expression:
6257 cast-expression
6258 pm-expression .* cast-expression
6259 pm-expression ->* cast-expression
6261 multiplicative-expression:
6262 pm-expression
6263 multiplicative-expression * pm-expression
6264 multiplicative-expression / pm-expression
6265 multiplicative-expression % pm-expression
6267 additive-expression:
6268 multiplicative-expression
6269 additive-expression + multiplicative-expression
6270 additive-expression - multiplicative-expression
6272 shift-expression:
6273 additive-expression
6274 shift-expression << additive-expression
6275 shift-expression >> additive-expression
6277 relational-expression:
6278 shift-expression
6279 relational-expression < shift-expression
6280 relational-expression > shift-expression
6281 relational-expression <= shift-expression
6282 relational-expression >= shift-expression
6284 GNU Extension:
6286 relational-expression:
6287 relational-expression <? shift-expression
6288 relational-expression >? shift-expression
6290 equality-expression:
6291 relational-expression
6292 equality-expression == relational-expression
6293 equality-expression != relational-expression
6295 and-expression:
6296 equality-expression
6297 and-expression & equality-expression
6299 exclusive-or-expression:
6300 and-expression
6301 exclusive-or-expression ^ and-expression
6303 inclusive-or-expression:
6304 exclusive-or-expression
6305 inclusive-or-expression | exclusive-or-expression
6307 logical-and-expression:
6308 inclusive-or-expression
6309 logical-and-expression && inclusive-or-expression
6311 logical-or-expression:
6312 logical-and-expression
6313 logical-or-expression || logical-and-expression
6315 All these are implemented with a single function like:
6317 binary-expression:
6318 simple-cast-expression
6319 binary-expression <token> binary-expression
6321 CAST_P is true if this expression is the target of a cast.
6323 The binops_by_token map is used to get the tree codes for each <token> type.
6324 binary-expressions are associated according to a precedence table. */
6326 #define TOKEN_PRECEDENCE(token) \
6327 (((token->type == CPP_GREATER \
6328 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6329 && !parser->greater_than_is_operator_p) \
6330 ? PREC_NOT_OPERATOR \
6331 : binops_by_token[token->type].prec)
6333 static tree
6334 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6335 bool no_toplevel_fold_p,
6336 enum cp_parser_prec prec,
6337 cp_id_kind * pidk)
6339 cp_parser_expression_stack stack;
6340 cp_parser_expression_stack_entry *sp = &stack[0];
6341 tree lhs, rhs;
6342 cp_token *token;
6343 enum tree_code tree_type, lhs_type, rhs_type;
6344 enum cp_parser_prec new_prec, lookahead_prec;
6345 bool overloaded_p;
6347 /* Parse the first expression. */
6348 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6349 lhs_type = ERROR_MARK;
6351 for (;;)
6353 /* Get an operator token. */
6354 token = cp_lexer_peek_token (parser->lexer);
6356 if (warn_cxx0x_compat
6357 && token->type == CPP_RSHIFT
6358 && !parser->greater_than_is_operator_p)
6360 if (warning_at (token->location, OPT_Wc__0x_compat,
6361 "%<>>%> operator will be treated as"
6362 " two right angle brackets in C++0x"))
6363 inform (token->location,
6364 "suggest parentheses around %<>>%> expression");
6367 new_prec = TOKEN_PRECEDENCE (token);
6369 /* Popping an entry off the stack means we completed a subexpression:
6370 - either we found a token which is not an operator (`>' where it is not
6371 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6372 will happen repeatedly;
6373 - or, we found an operator which has lower priority. This is the case
6374 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6375 parsing `3 * 4'. */
6376 if (new_prec <= prec)
6378 if (sp == stack)
6379 break;
6380 else
6381 goto pop;
6384 get_rhs:
6385 tree_type = binops_by_token[token->type].tree_type;
6387 /* We used the operator token. */
6388 cp_lexer_consume_token (parser->lexer);
6390 /* For "false && x" or "true || x", x will never be executed;
6391 disable warnings while evaluating it. */
6392 if (tree_type == TRUTH_ANDIF_EXPR)
6393 c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6394 else if (tree_type == TRUTH_ORIF_EXPR)
6395 c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6397 /* Extract another operand. It may be the RHS of this expression
6398 or the LHS of a new, higher priority expression. */
6399 rhs = cp_parser_simple_cast_expression (parser);
6400 rhs_type = ERROR_MARK;
6402 /* Get another operator token. Look up its precedence to avoid
6403 building a useless (immediately popped) stack entry for common
6404 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6405 token = cp_lexer_peek_token (parser->lexer);
6406 lookahead_prec = TOKEN_PRECEDENCE (token);
6407 if (lookahead_prec > new_prec)
6409 /* ... and prepare to parse the RHS of the new, higher priority
6410 expression. Since precedence levels on the stack are
6411 monotonically increasing, we do not have to care about
6412 stack overflows. */
6413 sp->prec = prec;
6414 sp->tree_type = tree_type;
6415 sp->lhs = lhs;
6416 sp->lhs_type = lhs_type;
6417 sp++;
6418 lhs = rhs;
6419 lhs_type = rhs_type;
6420 prec = new_prec;
6421 new_prec = lookahead_prec;
6422 goto get_rhs;
6424 pop:
6425 lookahead_prec = new_prec;
6426 /* If the stack is not empty, we have parsed into LHS the right side
6427 (`4' in the example above) of an expression we had suspended.
6428 We can use the information on the stack to recover the LHS (`3')
6429 from the stack together with the tree code (`MULT_EXPR'), and
6430 the precedence of the higher level subexpression
6431 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6432 which will be used to actually build the additive expression. */
6433 --sp;
6434 prec = sp->prec;
6435 tree_type = sp->tree_type;
6436 rhs = lhs;
6437 rhs_type = lhs_type;
6438 lhs = sp->lhs;
6439 lhs_type = sp->lhs_type;
6442 /* Undo the disabling of warnings done above. */
6443 if (tree_type == TRUTH_ANDIF_EXPR)
6444 c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6445 else if (tree_type == TRUTH_ORIF_EXPR)
6446 c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6448 overloaded_p = false;
6449 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6450 ERROR_MARK for everything that is not a binary expression.
6451 This makes warn_about_parentheses miss some warnings that
6452 involve unary operators. For unary expressions we should
6453 pass the correct tree_code unless the unary expression was
6454 surrounded by parentheses.
6456 if (no_toplevel_fold_p
6457 && lookahead_prec <= prec
6458 && sp == stack
6459 && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6460 lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6461 else
6462 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6463 &overloaded_p, tf_warning_or_error);
6464 lhs_type = tree_type;
6466 /* If the binary operator required the use of an overloaded operator,
6467 then this expression cannot be an integral constant-expression.
6468 An overloaded operator can be used even if both operands are
6469 otherwise permissible in an integral constant-expression if at
6470 least one of the operands is of enumeration type. */
6472 if (overloaded_p
6473 && (cp_parser_non_integral_constant_expression
6474 (parser, "calls to overloaded operators")))
6475 return error_mark_node;
6478 return lhs;
6482 /* Parse the `? expression : assignment-expression' part of a
6483 conditional-expression. The LOGICAL_OR_EXPR is the
6484 logical-or-expression that started the conditional-expression.
6485 Returns a representation of the entire conditional-expression.
6487 This routine is used by cp_parser_assignment_expression.
6489 ? expression : assignment-expression
6491 GNU Extensions:
6493 ? : assignment-expression */
6495 static tree
6496 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6498 tree expr;
6499 tree assignment_expr;
6501 /* Consume the `?' token. */
6502 cp_lexer_consume_token (parser->lexer);
6503 if (cp_parser_allow_gnu_extensions_p (parser)
6504 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6506 /* Implicit true clause. */
6507 expr = NULL_TREE;
6508 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6510 else
6512 /* Parse the expression. */
6513 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6514 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6515 c_inhibit_evaluation_warnings +=
6516 ((logical_or_expr == truthvalue_true_node)
6517 - (logical_or_expr == truthvalue_false_node));
6520 /* The next token should be a `:'. */
6521 cp_parser_require (parser, CPP_COLON, "%<:%>");
6522 /* Parse the assignment-expression. */
6523 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6524 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6526 /* Build the conditional-expression. */
6527 return build_x_conditional_expr (logical_or_expr,
6528 expr,
6529 assignment_expr,
6530 tf_warning_or_error);
6533 /* Parse an assignment-expression.
6535 assignment-expression:
6536 conditional-expression
6537 logical-or-expression assignment-operator assignment_expression
6538 throw-expression
6540 CAST_P is true if this expression is the target of a cast.
6542 Returns a representation for the expression. */
6544 static tree
6545 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6546 cp_id_kind * pidk)
6548 tree expr;
6550 /* If the next token is the `throw' keyword, then we're looking at
6551 a throw-expression. */
6552 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6553 expr = cp_parser_throw_expression (parser);
6554 /* Otherwise, it must be that we are looking at a
6555 logical-or-expression. */
6556 else
6558 /* Parse the binary expressions (logical-or-expression). */
6559 expr = cp_parser_binary_expression (parser, cast_p, false,
6560 PREC_NOT_OPERATOR, pidk);
6561 /* If the next token is a `?' then we're actually looking at a
6562 conditional-expression. */
6563 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6564 return cp_parser_question_colon_clause (parser, expr);
6565 else
6567 enum tree_code assignment_operator;
6569 /* If it's an assignment-operator, we're using the second
6570 production. */
6571 assignment_operator
6572 = cp_parser_assignment_operator_opt (parser);
6573 if (assignment_operator != ERROR_MARK)
6575 bool non_constant_p;
6577 /* Parse the right-hand side of the assignment. */
6578 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6580 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6581 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6583 /* An assignment may not appear in a
6584 constant-expression. */
6585 if (cp_parser_non_integral_constant_expression (parser,
6586 "an assignment"))
6587 return error_mark_node;
6588 /* Build the assignment expression. */
6589 expr = build_x_modify_expr (expr,
6590 assignment_operator,
6591 rhs,
6592 tf_warning_or_error);
6597 return expr;
6600 /* Parse an (optional) assignment-operator.
6602 assignment-operator: one of
6603 = *= /= %= += -= >>= <<= &= ^= |=
6605 GNU Extension:
6607 assignment-operator: one of
6608 <?= >?=
6610 If the next token is an assignment operator, the corresponding tree
6611 code is returned, and the token is consumed. For example, for
6612 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6613 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6614 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6615 operator, ERROR_MARK is returned. */
6617 static enum tree_code
6618 cp_parser_assignment_operator_opt (cp_parser* parser)
6620 enum tree_code op;
6621 cp_token *token;
6623 /* Peek at the next token. */
6624 token = cp_lexer_peek_token (parser->lexer);
6626 switch (token->type)
6628 case CPP_EQ:
6629 op = NOP_EXPR;
6630 break;
6632 case CPP_MULT_EQ:
6633 op = MULT_EXPR;
6634 break;
6636 case CPP_DIV_EQ:
6637 op = TRUNC_DIV_EXPR;
6638 break;
6640 case CPP_MOD_EQ:
6641 op = TRUNC_MOD_EXPR;
6642 break;
6644 case CPP_PLUS_EQ:
6645 op = PLUS_EXPR;
6646 break;
6648 case CPP_MINUS_EQ:
6649 op = MINUS_EXPR;
6650 break;
6652 case CPP_RSHIFT_EQ:
6653 op = RSHIFT_EXPR;
6654 break;
6656 case CPP_LSHIFT_EQ:
6657 op = LSHIFT_EXPR;
6658 break;
6660 case CPP_AND_EQ:
6661 op = BIT_AND_EXPR;
6662 break;
6664 case CPP_XOR_EQ:
6665 op = BIT_XOR_EXPR;
6666 break;
6668 case CPP_OR_EQ:
6669 op = BIT_IOR_EXPR;
6670 break;
6672 default:
6673 /* Nothing else is an assignment operator. */
6674 op = ERROR_MARK;
6677 /* If it was an assignment operator, consume it. */
6678 if (op != ERROR_MARK)
6679 cp_lexer_consume_token (parser->lexer);
6681 return op;
6684 /* Parse an expression.
6686 expression:
6687 assignment-expression
6688 expression , assignment-expression
6690 CAST_P is true if this expression is the target of a cast.
6692 Returns a representation of the expression. */
6694 static tree
6695 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6697 tree expression = NULL_TREE;
6699 while (true)
6701 tree assignment_expression;
6703 /* Parse the next assignment-expression. */
6704 assignment_expression
6705 = cp_parser_assignment_expression (parser, cast_p, pidk);
6706 /* If this is the first assignment-expression, we can just
6707 save it away. */
6708 if (!expression)
6709 expression = assignment_expression;
6710 else
6711 expression = build_x_compound_expr (expression,
6712 assignment_expression,
6713 tf_warning_or_error);
6714 /* If the next token is not a comma, then we are done with the
6715 expression. */
6716 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6717 break;
6718 /* Consume the `,'. */
6719 cp_lexer_consume_token (parser->lexer);
6720 /* A comma operator cannot appear in a constant-expression. */
6721 if (cp_parser_non_integral_constant_expression (parser,
6722 "a comma operator"))
6723 expression = error_mark_node;
6726 return expression;
6729 /* Parse a constant-expression.
6731 constant-expression:
6732 conditional-expression
6734 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6735 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6736 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6737 is false, NON_CONSTANT_P should be NULL. */
6739 static tree
6740 cp_parser_constant_expression (cp_parser* parser,
6741 bool allow_non_constant_p,
6742 bool *non_constant_p)
6744 bool saved_integral_constant_expression_p;
6745 bool saved_allow_non_integral_constant_expression_p;
6746 bool saved_non_integral_constant_expression_p;
6747 tree expression;
6749 /* It might seem that we could simply parse the
6750 conditional-expression, and then check to see if it were
6751 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6752 one that the compiler can figure out is constant, possibly after
6753 doing some simplifications or optimizations. The standard has a
6754 precise definition of constant-expression, and we must honor
6755 that, even though it is somewhat more restrictive.
6757 For example:
6759 int i[(2, 3)];
6761 is not a legal declaration, because `(2, 3)' is not a
6762 constant-expression. The `,' operator is forbidden in a
6763 constant-expression. However, GCC's constant-folding machinery
6764 will fold this operation to an INTEGER_CST for `3'. */
6766 /* Save the old settings. */
6767 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6768 saved_allow_non_integral_constant_expression_p
6769 = parser->allow_non_integral_constant_expression_p;
6770 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6771 /* We are now parsing a constant-expression. */
6772 parser->integral_constant_expression_p = true;
6773 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6774 parser->non_integral_constant_expression_p = false;
6775 /* Although the grammar says "conditional-expression", we parse an
6776 "assignment-expression", which also permits "throw-expression"
6777 and the use of assignment operators. In the case that
6778 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6779 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6780 actually essential that we look for an assignment-expression.
6781 For example, cp_parser_initializer_clauses uses this function to
6782 determine whether a particular assignment-expression is in fact
6783 constant. */
6784 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6785 /* Restore the old settings. */
6786 parser->integral_constant_expression_p
6787 = saved_integral_constant_expression_p;
6788 parser->allow_non_integral_constant_expression_p
6789 = saved_allow_non_integral_constant_expression_p;
6790 if (allow_non_constant_p)
6791 *non_constant_p = parser->non_integral_constant_expression_p;
6792 else if (parser->non_integral_constant_expression_p)
6793 expression = error_mark_node;
6794 parser->non_integral_constant_expression_p
6795 = saved_non_integral_constant_expression_p;
6797 return expression;
6800 /* Parse __builtin_offsetof.
6802 offsetof-expression:
6803 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6805 offsetof-member-designator:
6806 id-expression
6807 | offsetof-member-designator "." id-expression
6808 | offsetof-member-designator "[" expression "]"
6809 | offsetof-member-designator "->" id-expression */
6811 static tree
6812 cp_parser_builtin_offsetof (cp_parser *parser)
6814 int save_ice_p, save_non_ice_p;
6815 tree type, expr;
6816 cp_id_kind dummy;
6817 cp_token *token;
6819 /* We're about to accept non-integral-constant things, but will
6820 definitely yield an integral constant expression. Save and
6821 restore these values around our local parsing. */
6822 save_ice_p = parser->integral_constant_expression_p;
6823 save_non_ice_p = parser->non_integral_constant_expression_p;
6825 /* Consume the "__builtin_offsetof" token. */
6826 cp_lexer_consume_token (parser->lexer);
6827 /* Consume the opening `('. */
6828 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6829 /* Parse the type-id. */
6830 type = cp_parser_type_id (parser);
6831 /* Look for the `,'. */
6832 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6833 token = cp_lexer_peek_token (parser->lexer);
6835 /* Build the (type *)null that begins the traditional offsetof macro. */
6836 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6837 tf_warning_or_error);
6839 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6840 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6841 true, &dummy, token->location);
6842 while (true)
6844 token = cp_lexer_peek_token (parser->lexer);
6845 switch (token->type)
6847 case CPP_OPEN_SQUARE:
6848 /* offsetof-member-designator "[" expression "]" */
6849 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6850 break;
6852 case CPP_DEREF:
6853 /* offsetof-member-designator "->" identifier */
6854 expr = grok_array_decl (expr, integer_zero_node);
6855 /* FALLTHRU */
6857 case CPP_DOT:
6858 /* offsetof-member-designator "." identifier */
6859 cp_lexer_consume_token (parser->lexer);
6860 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
6861 expr, true, &dummy,
6862 token->location);
6863 break;
6865 case CPP_CLOSE_PAREN:
6866 /* Consume the ")" token. */
6867 cp_lexer_consume_token (parser->lexer);
6868 goto success;
6870 default:
6871 /* Error. We know the following require will fail, but
6872 that gives the proper error message. */
6873 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6874 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6875 expr = error_mark_node;
6876 goto failure;
6880 success:
6881 /* If we're processing a template, we can't finish the semantics yet.
6882 Otherwise we can fold the entire expression now. */
6883 if (processing_template_decl)
6884 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6885 else
6886 expr = finish_offsetof (expr);
6888 failure:
6889 parser->integral_constant_expression_p = save_ice_p;
6890 parser->non_integral_constant_expression_p = save_non_ice_p;
6892 return expr;
6895 /* Parse a trait expression. */
6897 static tree
6898 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6900 cp_trait_kind kind;
6901 tree type1, type2 = NULL_TREE;
6902 bool binary = false;
6903 cp_decl_specifier_seq decl_specs;
6905 switch (keyword)
6907 case RID_HAS_NOTHROW_ASSIGN:
6908 kind = CPTK_HAS_NOTHROW_ASSIGN;
6909 break;
6910 case RID_HAS_NOTHROW_CONSTRUCTOR:
6911 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6912 break;
6913 case RID_HAS_NOTHROW_COPY:
6914 kind = CPTK_HAS_NOTHROW_COPY;
6915 break;
6916 case RID_HAS_TRIVIAL_ASSIGN:
6917 kind = CPTK_HAS_TRIVIAL_ASSIGN;
6918 break;
6919 case RID_HAS_TRIVIAL_CONSTRUCTOR:
6920 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6921 break;
6922 case RID_HAS_TRIVIAL_COPY:
6923 kind = CPTK_HAS_TRIVIAL_COPY;
6924 break;
6925 case RID_HAS_TRIVIAL_DESTRUCTOR:
6926 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6927 break;
6928 case RID_HAS_VIRTUAL_DESTRUCTOR:
6929 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6930 break;
6931 case RID_IS_ABSTRACT:
6932 kind = CPTK_IS_ABSTRACT;
6933 break;
6934 case RID_IS_BASE_OF:
6935 kind = CPTK_IS_BASE_OF;
6936 binary = true;
6937 break;
6938 case RID_IS_CLASS:
6939 kind = CPTK_IS_CLASS;
6940 break;
6941 case RID_IS_CONVERTIBLE_TO:
6942 kind = CPTK_IS_CONVERTIBLE_TO;
6943 binary = true;
6944 break;
6945 case RID_IS_EMPTY:
6946 kind = CPTK_IS_EMPTY;
6947 break;
6948 case RID_IS_ENUM:
6949 kind = CPTK_IS_ENUM;
6950 break;
6951 case RID_IS_POD:
6952 kind = CPTK_IS_POD;
6953 break;
6954 case RID_IS_POLYMORPHIC:
6955 kind = CPTK_IS_POLYMORPHIC;
6956 break;
6957 case RID_IS_STD_LAYOUT:
6958 kind = CPTK_IS_STD_LAYOUT;
6959 break;
6960 case RID_IS_TRIVIAL:
6961 kind = CPTK_IS_TRIVIAL;
6962 break;
6963 case RID_IS_UNION:
6964 kind = CPTK_IS_UNION;
6965 break;
6966 default:
6967 gcc_unreachable ();
6970 /* Consume the token. */
6971 cp_lexer_consume_token (parser->lexer);
6973 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6975 type1 = cp_parser_type_id (parser);
6977 if (type1 == error_mark_node)
6978 return error_mark_node;
6980 /* Build a trivial decl-specifier-seq. */
6981 clear_decl_specs (&decl_specs);
6982 decl_specs.type = type1;
6984 /* Call grokdeclarator to figure out what type this is. */
6985 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6986 /*initialized=*/0, /*attrlist=*/NULL);
6988 if (binary)
6990 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6992 type2 = cp_parser_type_id (parser);
6994 if (type2 == error_mark_node)
6995 return error_mark_node;
6997 /* Build a trivial decl-specifier-seq. */
6998 clear_decl_specs (&decl_specs);
6999 decl_specs.type = type2;
7001 /* Call grokdeclarator to figure out what type this is. */
7002 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7003 /*initialized=*/0, /*attrlist=*/NULL);
7006 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7008 /* Complete the trait expression, which may mean either processing
7009 the trait expr now or saving it for template instantiation. */
7010 return finish_trait_expr (kind, type1, type2);
7013 /* Lambdas that appear in variable initializer or default argument scope
7014 get that in their mangling, so we need to record it. We might as well
7015 use the count for function and namespace scopes as well. */
7016 static GTY(()) tree lambda_scope;
7017 static GTY(()) int lambda_count;
7018 typedef struct GTY(()) tree_int
7020 tree t;
7021 int i;
7022 } tree_int;
7023 DEF_VEC_O(tree_int);
7024 DEF_VEC_ALLOC_O(tree_int,gc);
7025 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7027 static void
7028 start_lambda_scope (tree decl)
7030 tree_int ti;
7031 gcc_assert (decl);
7032 /* Once we're inside a function, we ignore other scopes and just push
7033 the function again so that popping works properly. */
7034 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7035 decl = current_function_decl;
7036 ti.t = lambda_scope;
7037 ti.i = lambda_count;
7038 VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7039 if (lambda_scope != decl)
7041 /* Don't reset the count if we're still in the same function. */
7042 lambda_scope = decl;
7043 lambda_count = 0;
7047 static void
7048 record_lambda_scope (tree lambda)
7050 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7051 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7054 static void
7055 finish_lambda_scope (void)
7057 tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7058 if (lambda_scope != p->t)
7060 lambda_scope = p->t;
7061 lambda_count = p->i;
7063 VEC_pop (tree_int, lambda_scope_stack);
7066 /* Parse a lambda expression.
7068 lambda-expression:
7069 lambda-introducer lambda-declarator [opt] compound-statement
7071 Returns a representation of the expression. */
7073 static tree
7074 cp_parser_lambda_expression (cp_parser* parser)
7076 tree lambda_expr = build_lambda_expr ();
7077 tree type;
7079 LAMBDA_EXPR_LOCATION (lambda_expr)
7080 = cp_lexer_peek_token (parser->lexer)->location;
7082 /* We may be in the middle of deferred access check. Disable
7083 it now. */
7084 push_deferring_access_checks (dk_no_deferred);
7086 cp_parser_lambda_introducer (parser, lambda_expr);
7088 type = begin_lambda_type (lambda_expr);
7090 record_lambda_scope (lambda_expr);
7092 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
7093 determine_visibility (TYPE_NAME (type));
7095 /* Now that we've started the type, add the capture fields for any
7096 explicit captures. */
7097 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
7100 /* Inside the class, surrounding template-parameter-lists do not apply. */
7101 unsigned int saved_num_template_parameter_lists
7102 = parser->num_template_parameter_lists;
7104 parser->num_template_parameter_lists = 0;
7106 /* By virtue of defining a local class, a lambda expression has access to
7107 the private variables of enclosing classes. */
7109 cp_parser_lambda_declarator_opt (parser, lambda_expr);
7111 cp_parser_lambda_body (parser, lambda_expr);
7113 /* The capture list was built up in reverse order; fix that now. */
7115 tree newlist = NULL_TREE;
7116 tree elt, next;
7118 for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7119 elt; elt = next)
7121 tree field = TREE_PURPOSE (elt);
7122 char *buf;
7124 next = TREE_CHAIN (elt);
7125 TREE_CHAIN (elt) = newlist;
7126 newlist = elt;
7128 /* Also add __ to the beginning of the field name so that code
7129 outside the lambda body can't see the captured name. We could
7130 just remove the name entirely, but this is more useful for
7131 debugging. */
7132 if (field == LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
7133 /* The 'this' capture already starts with __. */
7134 continue;
7136 buf = (char *) alloca (IDENTIFIER_LENGTH (DECL_NAME (field)) + 3);
7137 buf[1] = buf[0] = '_';
7138 memcpy (buf + 2, IDENTIFIER_POINTER (DECL_NAME (field)),
7139 IDENTIFIER_LENGTH (DECL_NAME (field)) + 1);
7140 DECL_NAME (field) = get_identifier (buf);
7142 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7145 maybe_add_lambda_conv_op (type);
7147 type = finish_struct (type, /*attributes=*/NULL_TREE);
7149 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7152 pop_deferring_access_checks ();
7154 return build_lambda_object (lambda_expr);
7157 /* Parse the beginning of a lambda expression.
7159 lambda-introducer:
7160 [ lambda-capture [opt] ]
7162 LAMBDA_EXPR is the current representation of the lambda expression. */
7164 static void
7165 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7167 /* Need commas after the first capture. */
7168 bool first = true;
7170 /* Eat the leading `['. */
7171 cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
7173 /* Record default capture mode. "[&" "[=" "[&," "[=," */
7174 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7175 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7176 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7177 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7178 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7180 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7182 cp_lexer_consume_token (parser->lexer);
7183 first = false;
7186 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7188 cp_token* capture_token;
7189 tree capture_id;
7190 tree capture_init_expr;
7191 cp_id_kind idk = CP_ID_KIND_NONE;
7192 bool explicit_init_p = false;
7194 enum capture_kind_type
7196 BY_COPY,
7197 BY_REFERENCE
7199 enum capture_kind_type capture_kind = BY_COPY;
7201 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7203 error ("expected end of capture-list");
7204 return;
7207 if (first)
7208 first = false;
7209 else
7210 cp_parser_require (parser, CPP_COMMA, "%<,%>");
7212 /* Possibly capture `this'. */
7213 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7215 cp_lexer_consume_token (parser->lexer);
7216 add_capture (lambda_expr,
7217 /*id=*/get_identifier ("__this"),
7218 /*initializer=*/finish_this_expr(),
7219 /*by_reference_p=*/false,
7220 explicit_init_p);
7221 continue;
7224 /* Remember whether we want to capture as a reference or not. */
7225 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7227 capture_kind = BY_REFERENCE;
7228 cp_lexer_consume_token (parser->lexer);
7231 /* Get the identifier. */
7232 capture_token = cp_lexer_peek_token (parser->lexer);
7233 capture_id = cp_parser_identifier (parser);
7235 if (capture_id == error_mark_node)
7236 /* Would be nice to have a cp_parser_skip_to_closing_x for general
7237 delimiters, but I modified this to stop on unnested ']' as well. It
7238 was already changed to stop on unnested '}', so the
7239 "closing_parenthesis" name is no more misleading with my change. */
7241 cp_parser_skip_to_closing_parenthesis (parser,
7242 /*recovering=*/true,
7243 /*or_comma=*/true,
7244 /*consume_paren=*/true);
7245 break;
7248 /* Find the initializer for this capture. */
7249 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7251 /* An explicit expression exists. */
7252 cp_lexer_consume_token (parser->lexer);
7253 pedwarn (input_location, OPT_pedantic,
7254 "ISO C++ does not allow initializers "
7255 "in lambda expression capture lists");
7256 capture_init_expr = cp_parser_assignment_expression (parser,
7257 /*cast_p=*/true,
7258 &idk);
7259 explicit_init_p = true;
7261 else
7263 const char* error_msg;
7265 /* Turn the identifier into an id-expression. */
7266 capture_init_expr
7267 = cp_parser_lookup_name
7268 (parser,
7269 capture_id,
7270 none_type,
7271 /*is_template=*/false,
7272 /*is_namespace=*/false,
7273 /*check_dependency=*/true,
7274 /*ambiguous_decls=*/NULL,
7275 capture_token->location);
7277 capture_init_expr
7278 = finish_id_expression
7279 (capture_id,
7280 capture_init_expr,
7281 parser->scope,
7282 &idk,
7283 /*integral_constant_expression_p=*/false,
7284 /*allow_non_integral_constant_expression_p=*/false,
7285 /*non_integral_constant_expression_p=*/NULL,
7286 /*template_p=*/false,
7287 /*done=*/true,
7288 /*address_p=*/false,
7289 /*template_arg_p=*/false,
7290 &error_msg,
7291 capture_token->location);
7294 if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7295 capture_init_expr
7296 = unqualified_name_lookup_error (capture_init_expr);
7298 add_capture (lambda_expr,
7299 capture_id,
7300 capture_init_expr,
7301 /*by_reference_p=*/capture_kind == BY_REFERENCE,
7302 explicit_init_p);
7305 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
7308 /* Parse the (optional) middle of a lambda expression.
7310 lambda-declarator:
7311 ( parameter-declaration-clause [opt] )
7312 attribute-specifier [opt]
7313 mutable [opt]
7314 exception-specification [opt]
7315 lambda-return-type-clause [opt]
7317 LAMBDA_EXPR is the current representation of the lambda expression. */
7319 static void
7320 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7322 /* 5.1.1.4 of the standard says:
7323 If a lambda-expression does not include a lambda-declarator, it is as if
7324 the lambda-declarator were ().
7325 This means an empty parameter list, no attributes, and no exception
7326 specification. */
7327 tree param_list = void_list_node;
7328 tree attributes = NULL_TREE;
7329 tree exception_spec = NULL_TREE;
7330 tree t;
7332 /* The lambda-declarator is optional, but must begin with an opening
7333 parenthesis if present. */
7334 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7336 cp_lexer_consume_token (parser->lexer);
7338 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7340 /* Parse parameters. */
7341 param_list = cp_parser_parameter_declaration_clause (parser);
7343 /* Default arguments shall not be specified in the
7344 parameter-declaration-clause of a lambda-declarator. */
7345 for (t = param_list; t; t = TREE_CHAIN (t))
7346 if (TREE_PURPOSE (t))
7347 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7348 "default argument specified for lambda parameter");
7350 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7352 attributes = cp_parser_attributes_opt (parser);
7354 /* Parse optional `mutable' keyword. */
7355 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7357 cp_lexer_consume_token (parser->lexer);
7358 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7361 /* Parse optional exception specification. */
7362 exception_spec = cp_parser_exception_specification_opt (parser);
7364 /* Parse optional trailing return type. */
7365 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7367 cp_lexer_consume_token (parser->lexer);
7368 LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7371 /* The function parameters must be in scope all the way until after the
7372 trailing-return-type in case of decltype. */
7373 for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
7374 pop_binding (DECL_NAME (t), t);
7376 leave_scope ();
7379 /* Create the function call operator.
7381 Messing with declarators like this is no uglier than building up the
7382 FUNCTION_DECL by hand, and this is less likely to get out of sync with
7383 other code. */
7385 cp_decl_specifier_seq return_type_specs;
7386 cp_declarator* declarator;
7387 tree fco;
7388 int quals;
7389 void *p;
7391 clear_decl_specs (&return_type_specs);
7392 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7393 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7394 else
7395 /* Maybe we will deduce the return type later, but we can use void
7396 as a placeholder return type anyways. */
7397 return_type_specs.type = void_type_node;
7399 p = obstack_alloc (&declarator_obstack, 0);
7401 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7402 sfk_none);
7404 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
7405 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
7406 declarator = make_call_declarator (declarator, param_list, quals,
7407 exception_spec,
7408 /*late_return_type=*/NULL_TREE);
7409 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
7411 fco = grokmethod (&return_type_specs,
7412 declarator,
7413 attributes);
7414 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7415 DECL_ARTIFICIAL (fco) = 1;
7417 finish_member_declaration (fco);
7419 obstack_free (&declarator_obstack, p);
7423 /* Parse the body of a lambda expression, which is simply
7425 compound-statement
7427 but which requires special handling.
7428 LAMBDA_EXPR is the current representation of the lambda expression. */
7430 static void
7431 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7433 bool nested = (current_function_decl != NULL_TREE);
7434 if (nested)
7435 push_function_context ();
7437 /* Finish the function call operator
7438 - class_specifier
7439 + late_parsing_for_member
7440 + function_definition_after_declarator
7441 + ctor_initializer_opt_and_function_body */
7443 tree fco = lambda_function (lambda_expr);
7444 tree body;
7445 bool done = false;
7447 /* Let the front end know that we are going to be defining this
7448 function. */
7449 start_preparsed_function (fco,
7450 NULL_TREE,
7451 SF_PRE_PARSED | SF_INCLASS_INLINE);
7453 start_lambda_scope (fco);
7454 body = begin_function_body ();
7456 /* 5.1.1.4 of the standard says:
7457 If a lambda-expression does not include a trailing-return-type, it
7458 is as if the trailing-return-type denotes the following type:
7459 * if the compound-statement is of the form
7460 { return attribute-specifier [opt] expression ; }
7461 the type of the returned expression after lvalue-to-rvalue
7462 conversion (_conv.lval_ 4.1), array-to-pointer conversion
7463 (_conv.array_ 4.2), and function-to-pointer conversion
7464 (_conv.func_ 4.3);
7465 * otherwise, void. */
7467 /* In a lambda that has neither a lambda-return-type-clause
7468 nor a deducible form, errors should be reported for return statements
7469 in the body. Since we used void as the placeholder return type, parsing
7470 the body as usual will give such desired behavior. */
7471 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7472 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
7473 && cp_lexer_peek_nth_token (parser->lexer, 2)->keyword == RID_RETURN
7474 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_SEMICOLON)
7476 tree compound_stmt;
7477 tree expr = NULL_TREE;
7478 cp_id_kind idk = CP_ID_KIND_NONE;
7480 /* Parse tentatively in case there's more after the initial return
7481 statement. */
7482 cp_parser_parse_tentatively (parser);
7484 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7485 cp_parser_require_keyword (parser, RID_RETURN, "%<return%>");
7487 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7489 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7490 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7492 if (cp_parser_parse_definitely (parser))
7494 apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7496 compound_stmt = begin_compound_stmt (0);
7497 /* Will get error here if type not deduced yet. */
7498 finish_return_stmt (expr);
7499 finish_compound_stmt (compound_stmt);
7501 done = true;
7505 if (!done)
7507 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7508 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
7509 /* TODO: does begin_compound_stmt want BCS_FN_BODY?
7510 cp_parser_compound_stmt does not pass it. */
7511 cp_parser_function_body (parser);
7512 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
7515 finish_function_body (body);
7516 finish_lambda_scope ();
7518 /* Finish the function and generate code for it if necessary. */
7519 expand_or_defer_fn (finish_function (/*inline*/2));
7522 if (nested)
7523 pop_function_context();
7526 /* Statements [gram.stmt.stmt] */
7528 /* Parse a statement.
7530 statement:
7531 labeled-statement
7532 expression-statement
7533 compound-statement
7534 selection-statement
7535 iteration-statement
7536 jump-statement
7537 declaration-statement
7538 try-block
7540 IN_COMPOUND is true when the statement is nested inside a
7541 cp_parser_compound_statement; this matters for certain pragmas.
7543 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7544 is a (possibly labeled) if statement which is not enclosed in braces
7545 and has an else clause. This is used to implement -Wparentheses. */
7547 static void
7548 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
7549 bool in_compound, bool *if_p)
7551 tree statement;
7552 cp_token *token;
7553 location_t statement_location;
7555 restart:
7556 if (if_p != NULL)
7557 *if_p = false;
7558 /* There is no statement yet. */
7559 statement = NULL_TREE;
7560 /* Peek at the next token. */
7561 token = cp_lexer_peek_token (parser->lexer);
7562 /* Remember the location of the first token in the statement. */
7563 statement_location = token->location;
7564 /* If this is a keyword, then that will often determine what kind of
7565 statement we have. */
7566 if (token->type == CPP_KEYWORD)
7568 enum rid keyword = token->keyword;
7570 switch (keyword)
7572 case RID_CASE:
7573 case RID_DEFAULT:
7574 /* Looks like a labeled-statement with a case label.
7575 Parse the label, and then use tail recursion to parse
7576 the statement. */
7577 cp_parser_label_for_labeled_statement (parser);
7578 goto restart;
7580 case RID_IF:
7581 case RID_SWITCH:
7582 statement = cp_parser_selection_statement (parser, if_p);
7583 break;
7585 case RID_WHILE:
7586 case RID_DO:
7587 case RID_FOR:
7588 statement = cp_parser_iteration_statement (parser);
7589 break;
7591 case RID_BREAK:
7592 case RID_CONTINUE:
7593 case RID_RETURN:
7594 case RID_GOTO:
7595 statement = cp_parser_jump_statement (parser);
7596 break;
7598 /* Objective-C++ exception-handling constructs. */
7599 case RID_AT_TRY:
7600 case RID_AT_CATCH:
7601 case RID_AT_FINALLY:
7602 case RID_AT_SYNCHRONIZED:
7603 case RID_AT_THROW:
7604 statement = cp_parser_objc_statement (parser);
7605 break;
7607 case RID_TRY:
7608 statement = cp_parser_try_block (parser);
7609 break;
7611 case RID_NAMESPACE:
7612 /* This must be a namespace alias definition. */
7613 cp_parser_declaration_statement (parser);
7614 return;
7616 default:
7617 /* It might be a keyword like `int' that can start a
7618 declaration-statement. */
7619 break;
7622 else if (token->type == CPP_NAME)
7624 /* If the next token is a `:', then we are looking at a
7625 labeled-statement. */
7626 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7627 if (token->type == CPP_COLON)
7629 /* Looks like a labeled-statement with an ordinary label.
7630 Parse the label, and then use tail recursion to parse
7631 the statement. */
7632 cp_parser_label_for_labeled_statement (parser);
7633 goto restart;
7636 /* Anything that starts with a `{' must be a compound-statement. */
7637 else if (token->type == CPP_OPEN_BRACE)
7638 statement = cp_parser_compound_statement (parser, NULL, false);
7639 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
7640 a statement all its own. */
7641 else if (token->type == CPP_PRAGMA)
7643 /* Only certain OpenMP pragmas are attached to statements, and thus
7644 are considered statements themselves. All others are not. In
7645 the context of a compound, accept the pragma as a "statement" and
7646 return so that we can check for a close brace. Otherwise we
7647 require a real statement and must go back and read one. */
7648 if (in_compound)
7649 cp_parser_pragma (parser, pragma_compound);
7650 else if (!cp_parser_pragma (parser, pragma_stmt))
7651 goto restart;
7652 return;
7654 else if (token->type == CPP_EOF)
7656 cp_parser_error (parser, "expected statement");
7657 return;
7660 /* Everything else must be a declaration-statement or an
7661 expression-statement. Try for the declaration-statement
7662 first, unless we are looking at a `;', in which case we know that
7663 we have an expression-statement. */
7664 if (!statement)
7666 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7668 cp_parser_parse_tentatively (parser);
7669 /* Try to parse the declaration-statement. */
7670 cp_parser_declaration_statement (parser);
7671 /* If that worked, we're done. */
7672 if (cp_parser_parse_definitely (parser))
7673 return;
7675 /* Look for an expression-statement instead. */
7676 statement = cp_parser_expression_statement (parser, in_statement_expr);
7679 /* Set the line number for the statement. */
7680 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7681 SET_EXPR_LOCATION (statement, statement_location);
7684 /* Parse the label for a labeled-statement, i.e.
7686 identifier :
7687 case constant-expression :
7688 default :
7690 GNU Extension:
7691 case constant-expression ... constant-expression : statement
7693 When a label is parsed without errors, the label is added to the
7694 parse tree by the finish_* functions, so this function doesn't
7695 have to return the label. */
7697 static void
7698 cp_parser_label_for_labeled_statement (cp_parser* parser)
7700 cp_token *token;
7701 tree label = NULL_TREE;
7703 /* The next token should be an identifier. */
7704 token = cp_lexer_peek_token (parser->lexer);
7705 if (token->type != CPP_NAME
7706 && token->type != CPP_KEYWORD)
7708 cp_parser_error (parser, "expected labeled-statement");
7709 return;
7712 switch (token->keyword)
7714 case RID_CASE:
7716 tree expr, expr_hi;
7717 cp_token *ellipsis;
7719 /* Consume the `case' token. */
7720 cp_lexer_consume_token (parser->lexer);
7721 /* Parse the constant-expression. */
7722 expr = cp_parser_constant_expression (parser,
7723 /*allow_non_constant_p=*/false,
7724 NULL);
7726 ellipsis = cp_lexer_peek_token (parser->lexer);
7727 if (ellipsis->type == CPP_ELLIPSIS)
7729 /* Consume the `...' token. */
7730 cp_lexer_consume_token (parser->lexer);
7731 expr_hi =
7732 cp_parser_constant_expression (parser,
7733 /*allow_non_constant_p=*/false,
7734 NULL);
7735 /* We don't need to emit warnings here, as the common code
7736 will do this for us. */
7738 else
7739 expr_hi = NULL_TREE;
7741 if (parser->in_switch_statement_p)
7742 finish_case_label (token->location, expr, expr_hi);
7743 else
7744 error_at (token->location,
7745 "case label %qE not within a switch statement",
7746 expr);
7748 break;
7750 case RID_DEFAULT:
7751 /* Consume the `default' token. */
7752 cp_lexer_consume_token (parser->lexer);
7754 if (parser->in_switch_statement_p)
7755 finish_case_label (token->location, NULL_TREE, NULL_TREE);
7756 else
7757 error_at (token->location, "case label not within a switch statement");
7758 break;
7760 default:
7761 /* Anything else must be an ordinary label. */
7762 label = finish_label_stmt (cp_parser_identifier (parser));
7763 break;
7766 /* Require the `:' token. */
7767 cp_parser_require (parser, CPP_COLON, "%<:%>");
7769 /* An ordinary label may optionally be followed by attributes.
7770 However, this is only permitted if the attributes are then
7771 followed by a semicolon. This is because, for backward
7772 compatibility, when parsing
7773 lab: __attribute__ ((unused)) int i;
7774 we want the attribute to attach to "i", not "lab". */
7775 if (label != NULL_TREE
7776 && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
7778 tree attrs;
7780 cp_parser_parse_tentatively (parser);
7781 attrs = cp_parser_attributes_opt (parser);
7782 if (attrs == NULL_TREE
7783 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7784 cp_parser_abort_tentative_parse (parser);
7785 else if (!cp_parser_parse_definitely (parser))
7787 else
7788 cplus_decl_attributes (&label, attrs, 0);
7792 /* Parse an expression-statement.
7794 expression-statement:
7795 expression [opt] ;
7797 Returns the new EXPR_STMT -- or NULL_TREE if the expression
7798 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
7799 indicates whether this expression-statement is part of an
7800 expression statement. */
7802 static tree
7803 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
7805 tree statement = NULL_TREE;
7806 cp_token *token = cp_lexer_peek_token (parser->lexer);
7808 /* If the next token is a ';', then there is no expression
7809 statement. */
7810 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7811 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7813 /* Give a helpful message for "A<T>::type t;" and the like. */
7814 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
7815 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
7817 if (TREE_CODE (statement) == SCOPE_REF)
7818 error_at (token->location, "need %<typename%> before %qE because "
7819 "%qT is a dependent scope",
7820 statement, TREE_OPERAND (statement, 0));
7821 else if (is_overloaded_fn (statement)
7822 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
7824 /* A::A a; */
7825 tree fn = get_first_fn (statement);
7826 error_at (token->location,
7827 "%<%T::%D%> names the constructor, not the type",
7828 DECL_CONTEXT (fn), DECL_NAME (fn));
7832 /* Consume the final `;'. */
7833 cp_parser_consume_semicolon_at_end_of_statement (parser);
7835 if (in_statement_expr
7836 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
7837 /* This is the final expression statement of a statement
7838 expression. */
7839 statement = finish_stmt_expr_expr (statement, in_statement_expr);
7840 else if (statement)
7841 statement = finish_expr_stmt (statement);
7842 else
7843 finish_stmt ();
7845 return statement;
7848 /* Parse a compound-statement.
7850 compound-statement:
7851 { statement-seq [opt] }
7853 GNU extension:
7855 compound-statement:
7856 { label-declaration-seq [opt] statement-seq [opt] }
7858 label-declaration-seq:
7859 label-declaration
7860 label-declaration-seq label-declaration
7862 Returns a tree representing the statement. */
7864 static tree
7865 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7866 bool in_try)
7868 tree compound_stmt;
7870 /* Consume the `{'. */
7871 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7872 return error_mark_node;
7873 /* Begin the compound-statement. */
7874 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7875 /* If the next keyword is `__label__' we have a label declaration. */
7876 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7877 cp_parser_label_declaration (parser);
7878 /* Parse an (optional) statement-seq. */
7879 cp_parser_statement_seq_opt (parser, in_statement_expr);
7880 /* Finish the compound-statement. */
7881 finish_compound_stmt (compound_stmt);
7882 /* Consume the `}'. */
7883 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7885 return compound_stmt;
7888 /* Parse an (optional) statement-seq.
7890 statement-seq:
7891 statement
7892 statement-seq [opt] statement */
7894 static void
7895 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7897 /* Scan statements until there aren't any more. */
7898 while (true)
7900 cp_token *token = cp_lexer_peek_token (parser->lexer);
7902 /* If we're looking at a `}', then we've run out of statements. */
7903 if (token->type == CPP_CLOSE_BRACE
7904 || token->type == CPP_EOF
7905 || token->type == CPP_PRAGMA_EOL)
7906 break;
7908 /* If we are in a compound statement and find 'else' then
7909 something went wrong. */
7910 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7912 if (parser->in_statement & IN_IF_STMT)
7913 break;
7914 else
7916 token = cp_lexer_consume_token (parser->lexer);
7917 error_at (token->location, "%<else%> without a previous %<if%>");
7921 /* Parse the statement. */
7922 cp_parser_statement (parser, in_statement_expr, true, NULL);
7926 /* Parse a selection-statement.
7928 selection-statement:
7929 if ( condition ) statement
7930 if ( condition ) statement else statement
7931 switch ( condition ) statement
7933 Returns the new IF_STMT or SWITCH_STMT.
7935 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7936 is a (possibly labeled) if statement which is not enclosed in
7937 braces and has an else clause. This is used to implement
7938 -Wparentheses. */
7940 static tree
7941 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7943 cp_token *token;
7944 enum rid keyword;
7946 if (if_p != NULL)
7947 *if_p = false;
7949 /* Peek at the next token. */
7950 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7952 /* See what kind of keyword it is. */
7953 keyword = token->keyword;
7954 switch (keyword)
7956 case RID_IF:
7957 case RID_SWITCH:
7959 tree statement;
7960 tree condition;
7962 /* Look for the `('. */
7963 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7965 cp_parser_skip_to_end_of_statement (parser);
7966 return error_mark_node;
7969 /* Begin the selection-statement. */
7970 if (keyword == RID_IF)
7971 statement = begin_if_stmt ();
7972 else
7973 statement = begin_switch_stmt ();
7975 /* Parse the condition. */
7976 condition = cp_parser_condition (parser);
7977 /* Look for the `)'. */
7978 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7979 cp_parser_skip_to_closing_parenthesis (parser, true, false,
7980 /*consume_paren=*/true);
7982 if (keyword == RID_IF)
7984 bool nested_if;
7985 unsigned char in_statement;
7987 /* Add the condition. */
7988 finish_if_stmt_cond (condition, statement);
7990 /* Parse the then-clause. */
7991 in_statement = parser->in_statement;
7992 parser->in_statement |= IN_IF_STMT;
7993 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7995 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7996 add_stmt (build_empty_stmt (loc));
7997 cp_lexer_consume_token (parser->lexer);
7998 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
7999 warning_at (loc, OPT_Wempty_body, "suggest braces around "
8000 "empty body in an %<if%> statement");
8001 nested_if = false;
8003 else
8004 cp_parser_implicitly_scoped_statement (parser, &nested_if);
8005 parser->in_statement = in_statement;
8007 finish_then_clause (statement);
8009 /* If the next token is `else', parse the else-clause. */
8010 if (cp_lexer_next_token_is_keyword (parser->lexer,
8011 RID_ELSE))
8013 /* Consume the `else' keyword. */
8014 cp_lexer_consume_token (parser->lexer);
8015 begin_else_clause (statement);
8016 /* Parse the else-clause. */
8017 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8019 location_t loc;
8020 loc = cp_lexer_peek_token (parser->lexer)->location;
8021 warning_at (loc,
8022 OPT_Wempty_body, "suggest braces around "
8023 "empty body in an %<else%> statement");
8024 add_stmt (build_empty_stmt (loc));
8025 cp_lexer_consume_token (parser->lexer);
8027 else
8028 cp_parser_implicitly_scoped_statement (parser, NULL);
8030 finish_else_clause (statement);
8032 /* If we are currently parsing a then-clause, then
8033 IF_P will not be NULL. We set it to true to
8034 indicate that this if statement has an else clause.
8035 This may trigger the Wparentheses warning below
8036 when we get back up to the parent if statement. */
8037 if (if_p != NULL)
8038 *if_p = true;
8040 else
8042 /* This if statement does not have an else clause. If
8043 NESTED_IF is true, then the then-clause is an if
8044 statement which does have an else clause. We warn
8045 about the potential ambiguity. */
8046 if (nested_if)
8047 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
8048 "suggest explicit braces to avoid ambiguous"
8049 " %<else%>");
8052 /* Now we're all done with the if-statement. */
8053 finish_if_stmt (statement);
8055 else
8057 bool in_switch_statement_p;
8058 unsigned char in_statement;
8060 /* Add the condition. */
8061 finish_switch_cond (condition, statement);
8063 /* Parse the body of the switch-statement. */
8064 in_switch_statement_p = parser->in_switch_statement_p;
8065 in_statement = parser->in_statement;
8066 parser->in_switch_statement_p = true;
8067 parser->in_statement |= IN_SWITCH_STMT;
8068 cp_parser_implicitly_scoped_statement (parser, NULL);
8069 parser->in_switch_statement_p = in_switch_statement_p;
8070 parser->in_statement = in_statement;
8072 /* Now we're all done with the switch-statement. */
8073 finish_switch_stmt (statement);
8076 return statement;
8078 break;
8080 default:
8081 cp_parser_error (parser, "expected selection-statement");
8082 return error_mark_node;
8086 /* Parse a condition.
8088 condition:
8089 expression
8090 type-specifier-seq declarator = initializer-clause
8091 type-specifier-seq declarator braced-init-list
8093 GNU Extension:
8095 condition:
8096 type-specifier-seq declarator asm-specification [opt]
8097 attributes [opt] = assignment-expression
8099 Returns the expression that should be tested. */
8101 static tree
8102 cp_parser_condition (cp_parser* parser)
8104 cp_decl_specifier_seq type_specifiers;
8105 const char *saved_message;
8107 /* Try the declaration first. */
8108 cp_parser_parse_tentatively (parser);
8109 /* New types are not allowed in the type-specifier-seq for a
8110 condition. */
8111 saved_message = parser->type_definition_forbidden_message;
8112 parser->type_definition_forbidden_message
8113 = G_("types may not be defined in conditions");
8114 /* Parse the type-specifier-seq. */
8115 cp_parser_type_specifier_seq (parser, /*is_declaration==*/true,
8116 /*is_trailing_return=*/false,
8117 &type_specifiers);
8118 /* Restore the saved message. */
8119 parser->type_definition_forbidden_message = saved_message;
8120 /* If all is well, we might be looking at a declaration. */
8121 if (!cp_parser_error_occurred (parser))
8123 tree decl;
8124 tree asm_specification;
8125 tree attributes;
8126 cp_declarator *declarator;
8127 tree initializer = NULL_TREE;
8129 /* Parse the declarator. */
8130 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8131 /*ctor_dtor_or_conv_p=*/NULL,
8132 /*parenthesized_p=*/NULL,
8133 /*member_p=*/false);
8134 /* Parse the attributes. */
8135 attributes = cp_parser_attributes_opt (parser);
8136 /* Parse the asm-specification. */
8137 asm_specification = cp_parser_asm_specification_opt (parser);
8138 /* If the next token is not an `=' or '{', then we might still be
8139 looking at an expression. For example:
8141 if (A(a).x)
8143 looks like a decl-specifier-seq and a declarator -- but then
8144 there is no `=', so this is an expression. */
8145 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8146 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8147 cp_parser_simulate_error (parser);
8149 /* If we did see an `=' or '{', then we are looking at a declaration
8150 for sure. */
8151 if (cp_parser_parse_definitely (parser))
8153 tree pushed_scope;
8154 bool non_constant_p;
8155 bool flags = LOOKUP_ONLYCONVERTING;
8157 /* Create the declaration. */
8158 decl = start_decl (declarator, &type_specifiers,
8159 /*initialized_p=*/true,
8160 attributes, /*prefix_attributes=*/NULL_TREE,
8161 &pushed_scope);
8163 /* Parse the initializer. */
8164 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8166 initializer = cp_parser_braced_list (parser, &non_constant_p);
8167 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8168 flags = 0;
8170 else
8172 /* Consume the `='. */
8173 cp_parser_require (parser, CPP_EQ, "%<=%>");
8174 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8176 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
8177 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8179 if (!non_constant_p)
8180 initializer = fold_non_dependent_expr (initializer);
8182 /* Process the initializer. */
8183 cp_finish_decl (decl,
8184 initializer, !non_constant_p,
8185 asm_specification,
8186 flags);
8188 if (pushed_scope)
8189 pop_scope (pushed_scope);
8191 return convert_from_reference (decl);
8194 /* If we didn't even get past the declarator successfully, we are
8195 definitely not looking at a declaration. */
8196 else
8197 cp_parser_abort_tentative_parse (parser);
8199 /* Otherwise, we are looking at an expression. */
8200 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
8203 /* Parse an iteration-statement.
8205 iteration-statement:
8206 while ( condition ) statement
8207 do statement while ( expression ) ;
8208 for ( for-init-statement condition [opt] ; expression [opt] )
8209 statement
8211 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
8213 static tree
8214 cp_parser_iteration_statement (cp_parser* parser)
8216 cp_token *token;
8217 enum rid keyword;
8218 tree statement;
8219 unsigned char in_statement;
8221 /* Peek at the next token. */
8222 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
8223 if (!token)
8224 return error_mark_node;
8226 /* Remember whether or not we are already within an iteration
8227 statement. */
8228 in_statement = parser->in_statement;
8230 /* See what kind of keyword it is. */
8231 keyword = token->keyword;
8232 switch (keyword)
8234 case RID_WHILE:
8236 tree condition;
8238 /* Begin the while-statement. */
8239 statement = begin_while_stmt ();
8240 /* Look for the `('. */
8241 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8242 /* Parse the condition. */
8243 condition = cp_parser_condition (parser);
8244 finish_while_stmt_cond (condition, statement);
8245 /* Look for the `)'. */
8246 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8247 /* Parse the dependent statement. */
8248 parser->in_statement = IN_ITERATION_STMT;
8249 cp_parser_already_scoped_statement (parser);
8250 parser->in_statement = in_statement;
8251 /* We're done with the while-statement. */
8252 finish_while_stmt (statement);
8254 break;
8256 case RID_DO:
8258 tree expression;
8260 /* Begin the do-statement. */
8261 statement = begin_do_stmt ();
8262 /* Parse the body of the do-statement. */
8263 parser->in_statement = IN_ITERATION_STMT;
8264 cp_parser_implicitly_scoped_statement (parser, NULL);
8265 parser->in_statement = in_statement;
8266 finish_do_body (statement);
8267 /* Look for the `while' keyword. */
8268 cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
8269 /* Look for the `('. */
8270 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8271 /* Parse the expression. */
8272 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8273 /* We're done with the do-statement. */
8274 finish_do_stmt (expression, statement);
8275 /* Look for the `)'. */
8276 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8277 /* Look for the `;'. */
8278 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8280 break;
8282 case RID_FOR:
8284 tree condition = NULL_TREE;
8285 tree expression = NULL_TREE;
8287 /* Begin the for-statement. */
8288 statement = begin_for_stmt ();
8289 /* Look for the `('. */
8290 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8291 /* Parse the initialization. */
8292 cp_parser_for_init_statement (parser);
8293 finish_for_init_stmt (statement);
8295 /* If there's a condition, process it. */
8296 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8297 condition = cp_parser_condition (parser);
8298 finish_for_cond (condition, statement);
8299 /* Look for the `;'. */
8300 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8302 /* If there's an expression, process it. */
8303 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8304 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8305 finish_for_expr (expression, statement);
8306 /* Look for the `)'. */
8307 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8309 /* Parse the body of the for-statement. */
8310 parser->in_statement = IN_ITERATION_STMT;
8311 cp_parser_already_scoped_statement (parser);
8312 parser->in_statement = in_statement;
8314 /* We're done with the for-statement. */
8315 finish_for_stmt (statement);
8317 break;
8319 default:
8320 cp_parser_error (parser, "expected iteration-statement");
8321 statement = error_mark_node;
8322 break;
8325 return statement;
8328 /* Parse a for-init-statement.
8330 for-init-statement:
8331 expression-statement
8332 simple-declaration */
8334 static void
8335 cp_parser_for_init_statement (cp_parser* parser)
8337 /* If the next token is a `;', then we have an empty
8338 expression-statement. Grammatically, this is also a
8339 simple-declaration, but an invalid one, because it does not
8340 declare anything. Therefore, if we did not handle this case
8341 specially, we would issue an error message about an invalid
8342 declaration. */
8343 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8345 /* We're going to speculatively look for a declaration, falling back
8346 to an expression, if necessary. */
8347 cp_parser_parse_tentatively (parser);
8348 /* Parse the declaration. */
8349 cp_parser_simple_declaration (parser,
8350 /*function_definition_allowed_p=*/false);
8351 /* If the tentative parse failed, then we shall need to look for an
8352 expression-statement. */
8353 if (cp_parser_parse_definitely (parser))
8354 return;
8357 cp_parser_expression_statement (parser, NULL_TREE);
8360 /* Parse a jump-statement.
8362 jump-statement:
8363 break ;
8364 continue ;
8365 return expression [opt] ;
8366 return braced-init-list ;
8367 goto identifier ;
8369 GNU extension:
8371 jump-statement:
8372 goto * expression ;
8374 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
8376 static tree
8377 cp_parser_jump_statement (cp_parser* parser)
8379 tree statement = error_mark_node;
8380 cp_token *token;
8381 enum rid keyword;
8382 unsigned char in_statement;
8384 /* Peek at the next token. */
8385 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
8386 if (!token)
8387 return error_mark_node;
8389 /* See what kind of keyword it is. */
8390 keyword = token->keyword;
8391 switch (keyword)
8393 case RID_BREAK:
8394 in_statement = parser->in_statement & ~IN_IF_STMT;
8395 switch (in_statement)
8397 case 0:
8398 error_at (token->location, "break statement not within loop or switch");
8399 break;
8400 default:
8401 gcc_assert ((in_statement & IN_SWITCH_STMT)
8402 || in_statement == IN_ITERATION_STMT);
8403 statement = finish_break_stmt ();
8404 break;
8405 case IN_OMP_BLOCK:
8406 error_at (token->location, "invalid exit from OpenMP structured block");
8407 break;
8408 case IN_OMP_FOR:
8409 error_at (token->location, "break statement used with OpenMP for loop");
8410 break;
8412 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8413 break;
8415 case RID_CONTINUE:
8416 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
8418 case 0:
8419 error_at (token->location, "continue statement not within a loop");
8420 break;
8421 case IN_ITERATION_STMT:
8422 case IN_OMP_FOR:
8423 statement = finish_continue_stmt ();
8424 break;
8425 case IN_OMP_BLOCK:
8426 error_at (token->location, "invalid exit from OpenMP structured block");
8427 break;
8428 default:
8429 gcc_unreachable ();
8431 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8432 break;
8434 case RID_RETURN:
8436 tree expr;
8437 bool expr_non_constant_p;
8439 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8441 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8442 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8444 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8445 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8446 else
8447 /* If the next token is a `;', then there is no
8448 expression. */
8449 expr = NULL_TREE;
8450 /* Build the return-statement. */
8451 statement = finish_return_stmt (expr);
8452 /* Look for the final `;'. */
8453 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8455 break;
8457 case RID_GOTO:
8458 /* Create the goto-statement. */
8459 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
8461 /* Issue a warning about this use of a GNU extension. */
8462 pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
8463 /* Consume the '*' token. */
8464 cp_lexer_consume_token (parser->lexer);
8465 /* Parse the dependent expression. */
8466 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
8468 else
8469 finish_goto_stmt (cp_parser_identifier (parser));
8470 /* Look for the final `;'. */
8471 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8472 break;
8474 default:
8475 cp_parser_error (parser, "expected jump-statement");
8476 break;
8479 return statement;
8482 /* Parse a declaration-statement.
8484 declaration-statement:
8485 block-declaration */
8487 static void
8488 cp_parser_declaration_statement (cp_parser* parser)
8490 void *p;
8492 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
8493 p = obstack_alloc (&declarator_obstack, 0);
8495 /* Parse the block-declaration. */
8496 cp_parser_block_declaration (parser, /*statement_p=*/true);
8498 /* Free any declarators allocated. */
8499 obstack_free (&declarator_obstack, p);
8501 /* Finish off the statement. */
8502 finish_stmt ();
8505 /* Some dependent statements (like `if (cond) statement'), are
8506 implicitly in their own scope. In other words, if the statement is
8507 a single statement (as opposed to a compound-statement), it is
8508 none-the-less treated as if it were enclosed in braces. Any
8509 declarations appearing in the dependent statement are out of scope
8510 after control passes that point. This function parses a statement,
8511 but ensures that is in its own scope, even if it is not a
8512 compound-statement.
8514 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8515 is a (possibly labeled) if statement which is not enclosed in
8516 braces and has an else clause. This is used to implement
8517 -Wparentheses.
8519 Returns the new statement. */
8521 static tree
8522 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
8524 tree statement;
8526 if (if_p != NULL)
8527 *if_p = false;
8529 /* Mark if () ; with a special NOP_EXPR. */
8530 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8532 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8533 cp_lexer_consume_token (parser->lexer);
8534 statement = add_stmt (build_empty_stmt (loc));
8536 /* if a compound is opened, we simply parse the statement directly. */
8537 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8538 statement = cp_parser_compound_statement (parser, NULL, false);
8539 /* If the token is not a `{', then we must take special action. */
8540 else
8542 /* Create a compound-statement. */
8543 statement = begin_compound_stmt (0);
8544 /* Parse the dependent-statement. */
8545 cp_parser_statement (parser, NULL_TREE, false, if_p);
8546 /* Finish the dummy compound-statement. */
8547 finish_compound_stmt (statement);
8550 /* Return the statement. */
8551 return statement;
8554 /* For some dependent statements (like `while (cond) statement'), we
8555 have already created a scope. Therefore, even if the dependent
8556 statement is a compound-statement, we do not want to create another
8557 scope. */
8559 static void
8560 cp_parser_already_scoped_statement (cp_parser* parser)
8562 /* If the token is a `{', then we must take special action. */
8563 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8564 cp_parser_statement (parser, NULL_TREE, false, NULL);
8565 else
8567 /* Avoid calling cp_parser_compound_statement, so that we
8568 don't create a new scope. Do everything else by hand. */
8569 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
8570 /* If the next keyword is `__label__' we have a label declaration. */
8571 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8572 cp_parser_label_declaration (parser);
8573 /* Parse an (optional) statement-seq. */
8574 cp_parser_statement_seq_opt (parser, NULL_TREE);
8575 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8579 /* Declarations [gram.dcl.dcl] */
8581 /* Parse an optional declaration-sequence.
8583 declaration-seq:
8584 declaration
8585 declaration-seq declaration */
8587 static void
8588 cp_parser_declaration_seq_opt (cp_parser* parser)
8590 while (true)
8592 cp_token *token;
8594 token = cp_lexer_peek_token (parser->lexer);
8596 if (token->type == CPP_CLOSE_BRACE
8597 || token->type == CPP_EOF
8598 || token->type == CPP_PRAGMA_EOL)
8599 break;
8601 if (token->type == CPP_SEMICOLON)
8603 /* A declaration consisting of a single semicolon is
8604 invalid. Allow it unless we're being pedantic. */
8605 cp_lexer_consume_token (parser->lexer);
8606 if (!in_system_header)
8607 pedwarn (input_location, OPT_pedantic, "extra %<;%>");
8608 continue;
8611 /* If we're entering or exiting a region that's implicitly
8612 extern "C", modify the lang context appropriately. */
8613 if (!parser->implicit_extern_c && token->implicit_extern_c)
8615 push_lang_context (lang_name_c);
8616 parser->implicit_extern_c = true;
8618 else if (parser->implicit_extern_c && !token->implicit_extern_c)
8620 pop_lang_context ();
8621 parser->implicit_extern_c = false;
8624 if (token->type == CPP_PRAGMA)
8626 /* A top-level declaration can consist solely of a #pragma.
8627 A nested declaration cannot, so this is done here and not
8628 in cp_parser_declaration. (A #pragma at block scope is
8629 handled in cp_parser_statement.) */
8630 cp_parser_pragma (parser, pragma_external);
8631 continue;
8634 /* Parse the declaration itself. */
8635 cp_parser_declaration (parser);
8639 /* Parse a declaration.
8641 declaration:
8642 block-declaration
8643 function-definition
8644 template-declaration
8645 explicit-instantiation
8646 explicit-specialization
8647 linkage-specification
8648 namespace-definition
8650 GNU extension:
8652 declaration:
8653 __extension__ declaration */
8655 static void
8656 cp_parser_declaration (cp_parser* parser)
8658 cp_token token1;
8659 cp_token token2;
8660 int saved_pedantic;
8661 void *p;
8663 /* Check for the `__extension__' keyword. */
8664 if (cp_parser_extension_opt (parser, &saved_pedantic))
8666 /* Parse the qualified declaration. */
8667 cp_parser_declaration (parser);
8668 /* Restore the PEDANTIC flag. */
8669 pedantic = saved_pedantic;
8671 return;
8674 /* Try to figure out what kind of declaration is present. */
8675 token1 = *cp_lexer_peek_token (parser->lexer);
8677 if (token1.type != CPP_EOF)
8678 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
8679 else
8681 token2.type = CPP_EOF;
8682 token2.keyword = RID_MAX;
8685 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
8686 p = obstack_alloc (&declarator_obstack, 0);
8688 /* If the next token is `extern' and the following token is a string
8689 literal, then we have a linkage specification. */
8690 if (token1.keyword == RID_EXTERN
8691 && cp_parser_is_string_literal (&token2))
8692 cp_parser_linkage_specification (parser);
8693 /* If the next token is `template', then we have either a template
8694 declaration, an explicit instantiation, or an explicit
8695 specialization. */
8696 else if (token1.keyword == RID_TEMPLATE)
8698 /* `template <>' indicates a template specialization. */
8699 if (token2.type == CPP_LESS
8700 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
8701 cp_parser_explicit_specialization (parser);
8702 /* `template <' indicates a template declaration. */
8703 else if (token2.type == CPP_LESS)
8704 cp_parser_template_declaration (parser, /*member_p=*/false);
8705 /* Anything else must be an explicit instantiation. */
8706 else
8707 cp_parser_explicit_instantiation (parser);
8709 /* If the next token is `export', then we have a template
8710 declaration. */
8711 else if (token1.keyword == RID_EXPORT)
8712 cp_parser_template_declaration (parser, /*member_p=*/false);
8713 /* If the next token is `extern', 'static' or 'inline' and the one
8714 after that is `template', we have a GNU extended explicit
8715 instantiation directive. */
8716 else if (cp_parser_allow_gnu_extensions_p (parser)
8717 && (token1.keyword == RID_EXTERN
8718 || token1.keyword == RID_STATIC
8719 || token1.keyword == RID_INLINE)
8720 && token2.keyword == RID_TEMPLATE)
8721 cp_parser_explicit_instantiation (parser);
8722 /* If the next token is `namespace', check for a named or unnamed
8723 namespace definition. */
8724 else if (token1.keyword == RID_NAMESPACE
8725 && (/* A named namespace definition. */
8726 (token2.type == CPP_NAME
8727 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
8728 != CPP_EQ))
8729 /* An unnamed namespace definition. */
8730 || token2.type == CPP_OPEN_BRACE
8731 || token2.keyword == RID_ATTRIBUTE))
8732 cp_parser_namespace_definition (parser);
8733 /* An inline (associated) namespace definition. */
8734 else if (token1.keyword == RID_INLINE
8735 && token2.keyword == RID_NAMESPACE)
8736 cp_parser_namespace_definition (parser);
8737 /* Objective-C++ declaration/definition. */
8738 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
8739 cp_parser_objc_declaration (parser);
8740 /* We must have either a block declaration or a function
8741 definition. */
8742 else
8743 /* Try to parse a block-declaration, or a function-definition. */
8744 cp_parser_block_declaration (parser, /*statement_p=*/false);
8746 /* Free any declarators allocated. */
8747 obstack_free (&declarator_obstack, p);
8750 /* Parse a block-declaration.
8752 block-declaration:
8753 simple-declaration
8754 asm-definition
8755 namespace-alias-definition
8756 using-declaration
8757 using-directive
8759 GNU Extension:
8761 block-declaration:
8762 __extension__ block-declaration
8764 C++0x Extension:
8766 block-declaration:
8767 static_assert-declaration
8769 If STATEMENT_P is TRUE, then this block-declaration is occurring as
8770 part of a declaration-statement. */
8772 static void
8773 cp_parser_block_declaration (cp_parser *parser,
8774 bool statement_p)
8776 cp_token *token1;
8777 int saved_pedantic;
8779 /* Check for the `__extension__' keyword. */
8780 if (cp_parser_extension_opt (parser, &saved_pedantic))
8782 /* Parse the qualified declaration. */
8783 cp_parser_block_declaration (parser, statement_p);
8784 /* Restore the PEDANTIC flag. */
8785 pedantic = saved_pedantic;
8787 return;
8790 /* Peek at the next token to figure out which kind of declaration is
8791 present. */
8792 token1 = cp_lexer_peek_token (parser->lexer);
8794 /* If the next keyword is `asm', we have an asm-definition. */
8795 if (token1->keyword == RID_ASM)
8797 if (statement_p)
8798 cp_parser_commit_to_tentative_parse (parser);
8799 cp_parser_asm_definition (parser);
8801 /* If the next keyword is `namespace', we have a
8802 namespace-alias-definition. */
8803 else if (token1->keyword == RID_NAMESPACE)
8804 cp_parser_namespace_alias_definition (parser);
8805 /* If the next keyword is `using', we have either a
8806 using-declaration or a using-directive. */
8807 else if (token1->keyword == RID_USING)
8809 cp_token *token2;
8811 if (statement_p)
8812 cp_parser_commit_to_tentative_parse (parser);
8813 /* If the token after `using' is `namespace', then we have a
8814 using-directive. */
8815 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8816 if (token2->keyword == RID_NAMESPACE)
8817 cp_parser_using_directive (parser);
8818 /* Otherwise, it's a using-declaration. */
8819 else
8820 cp_parser_using_declaration (parser,
8821 /*access_declaration_p=*/false);
8823 /* If the next keyword is `__label__' we have a misplaced label
8824 declaration. */
8825 else if (token1->keyword == RID_LABEL)
8827 cp_lexer_consume_token (parser->lexer);
8828 error_at (token1->location, "%<__label__%> not at the beginning of a block");
8829 cp_parser_skip_to_end_of_statement (parser);
8830 /* If the next token is now a `;', consume it. */
8831 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8832 cp_lexer_consume_token (parser->lexer);
8834 /* If the next token is `static_assert' we have a static assertion. */
8835 else if (token1->keyword == RID_STATIC_ASSERT)
8836 cp_parser_static_assert (parser, /*member_p=*/false);
8837 /* Anything else must be a simple-declaration. */
8838 else
8839 cp_parser_simple_declaration (parser, !statement_p);
8842 /* Parse a simple-declaration.
8844 simple-declaration:
8845 decl-specifier-seq [opt] init-declarator-list [opt] ;
8847 init-declarator-list:
8848 init-declarator
8849 init-declarator-list , init-declarator
8851 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8852 function-definition as a simple-declaration. */
8854 static void
8855 cp_parser_simple_declaration (cp_parser* parser,
8856 bool function_definition_allowed_p)
8858 cp_decl_specifier_seq decl_specifiers;
8859 int declares_class_or_enum;
8860 bool saw_declarator;
8862 /* Defer access checks until we know what is being declared; the
8863 checks for names appearing in the decl-specifier-seq should be
8864 done as if we were in the scope of the thing being declared. */
8865 push_deferring_access_checks (dk_deferred);
8867 /* Parse the decl-specifier-seq. We have to keep track of whether
8868 or not the decl-specifier-seq declares a named class or
8869 enumeration type, since that is the only case in which the
8870 init-declarator-list is allowed to be empty.
8872 [dcl.dcl]
8874 In a simple-declaration, the optional init-declarator-list can be
8875 omitted only when declaring a class or enumeration, that is when
8876 the decl-specifier-seq contains either a class-specifier, an
8877 elaborated-type-specifier, or an enum-specifier. */
8878 cp_parser_decl_specifier_seq (parser,
8879 CP_PARSER_FLAGS_OPTIONAL,
8880 &decl_specifiers,
8881 &declares_class_or_enum);
8882 /* We no longer need to defer access checks. */
8883 stop_deferring_access_checks ();
8885 /* In a block scope, a valid declaration must always have a
8886 decl-specifier-seq. By not trying to parse declarators, we can
8887 resolve the declaration/expression ambiguity more quickly. */
8888 if (!function_definition_allowed_p
8889 && !decl_specifiers.any_specifiers_p)
8891 cp_parser_error (parser, "expected declaration");
8892 goto done;
8895 /* If the next two tokens are both identifiers, the code is
8896 erroneous. The usual cause of this situation is code like:
8898 T t;
8900 where "T" should name a type -- but does not. */
8901 if (!decl_specifiers.any_type_specifiers_p
8902 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8904 /* If parsing tentatively, we should commit; we really are
8905 looking at a declaration. */
8906 cp_parser_commit_to_tentative_parse (parser);
8907 /* Give up. */
8908 goto done;
8911 /* If we have seen at least one decl-specifier, and the next token
8912 is not a parenthesis, then we must be looking at a declaration.
8913 (After "int (" we might be looking at a functional cast.) */
8914 if (decl_specifiers.any_specifiers_p
8915 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8916 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
8917 && !cp_parser_error_occurred (parser))
8918 cp_parser_commit_to_tentative_parse (parser);
8920 /* Keep going until we hit the `;' at the end of the simple
8921 declaration. */
8922 saw_declarator = false;
8923 while (cp_lexer_next_token_is_not (parser->lexer,
8924 CPP_SEMICOLON))
8926 cp_token *token;
8927 bool function_definition_p;
8928 tree decl;
8930 if (saw_declarator)
8932 /* If we are processing next declarator, coma is expected */
8933 token = cp_lexer_peek_token (parser->lexer);
8934 gcc_assert (token->type == CPP_COMMA);
8935 cp_lexer_consume_token (parser->lexer);
8937 else
8938 saw_declarator = true;
8940 /* Parse the init-declarator. */
8941 decl = cp_parser_init_declarator (parser, &decl_specifiers,
8942 /*checks=*/NULL,
8943 function_definition_allowed_p,
8944 /*member_p=*/false,
8945 declares_class_or_enum,
8946 &function_definition_p);
8947 /* If an error occurred while parsing tentatively, exit quickly.
8948 (That usually happens when in the body of a function; each
8949 statement is treated as a declaration-statement until proven
8950 otherwise.) */
8951 if (cp_parser_error_occurred (parser))
8952 goto done;
8953 /* Handle function definitions specially. */
8954 if (function_definition_p)
8956 /* If the next token is a `,', then we are probably
8957 processing something like:
8959 void f() {}, *p;
8961 which is erroneous. */
8962 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8964 cp_token *token = cp_lexer_peek_token (parser->lexer);
8965 error_at (token->location,
8966 "mixing"
8967 " declarations and function-definitions is forbidden");
8969 /* Otherwise, we're done with the list of declarators. */
8970 else
8972 pop_deferring_access_checks ();
8973 return;
8976 /* The next token should be either a `,' or a `;'. */
8977 token = cp_lexer_peek_token (parser->lexer);
8978 /* If it's a `,', there are more declarators to come. */
8979 if (token->type == CPP_COMMA)
8980 /* will be consumed next time around */;
8981 /* If it's a `;', we are done. */
8982 else if (token->type == CPP_SEMICOLON)
8983 break;
8984 /* Anything else is an error. */
8985 else
8987 /* If we have already issued an error message we don't need
8988 to issue another one. */
8989 if (decl != error_mark_node
8990 || cp_parser_uncommitted_to_tentative_parse_p (parser))
8991 cp_parser_error (parser, "expected %<,%> or %<;%>");
8992 /* Skip tokens until we reach the end of the statement. */
8993 cp_parser_skip_to_end_of_statement (parser);
8994 /* If the next token is now a `;', consume it. */
8995 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8996 cp_lexer_consume_token (parser->lexer);
8997 goto done;
8999 /* After the first time around, a function-definition is not
9000 allowed -- even if it was OK at first. For example:
9002 int i, f() {}
9004 is not valid. */
9005 function_definition_allowed_p = false;
9008 /* Issue an error message if no declarators are present, and the
9009 decl-specifier-seq does not itself declare a class or
9010 enumeration. */
9011 if (!saw_declarator)
9013 if (cp_parser_declares_only_class_p (parser))
9014 shadow_tag (&decl_specifiers);
9015 /* Perform any deferred access checks. */
9016 perform_deferred_access_checks ();
9019 /* Consume the `;'. */
9020 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
9022 done:
9023 pop_deferring_access_checks ();
9026 /* Parse a decl-specifier-seq.
9028 decl-specifier-seq:
9029 decl-specifier-seq [opt] decl-specifier
9031 decl-specifier:
9032 storage-class-specifier
9033 type-specifier
9034 function-specifier
9035 friend
9036 typedef
9038 GNU Extension:
9040 decl-specifier:
9041 attributes
9043 Set *DECL_SPECS to a representation of the decl-specifier-seq.
9045 The parser flags FLAGS is used to control type-specifier parsing.
9047 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
9048 flags:
9050 1: one of the decl-specifiers is an elaborated-type-specifier
9051 (i.e., a type declaration)
9052 2: one of the decl-specifiers is an enum-specifier or a
9053 class-specifier (i.e., a type definition)
9057 static void
9058 cp_parser_decl_specifier_seq (cp_parser* parser,
9059 cp_parser_flags flags,
9060 cp_decl_specifier_seq *decl_specs,
9061 int* declares_class_or_enum)
9063 bool constructor_possible_p = !parser->in_declarator_p;
9064 cp_token *start_token = NULL;
9066 /* Clear DECL_SPECS. */
9067 clear_decl_specs (decl_specs);
9069 /* Assume no class or enumeration type is declared. */
9070 *declares_class_or_enum = 0;
9072 /* Keep reading specifiers until there are no more to read. */
9073 while (true)
9075 bool constructor_p;
9076 bool found_decl_spec;
9077 cp_token *token;
9079 /* Peek at the next token. */
9080 token = cp_lexer_peek_token (parser->lexer);
9082 /* Save the first token of the decl spec list for error
9083 reporting. */
9084 if (!start_token)
9085 start_token = token;
9086 /* Handle attributes. */
9087 if (token->keyword == RID_ATTRIBUTE)
9089 /* Parse the attributes. */
9090 decl_specs->attributes
9091 = chainon (decl_specs->attributes,
9092 cp_parser_attributes_opt (parser));
9093 continue;
9095 /* Assume we will find a decl-specifier keyword. */
9096 found_decl_spec = true;
9097 /* If the next token is an appropriate keyword, we can simply
9098 add it to the list. */
9099 switch (token->keyword)
9101 /* decl-specifier:
9102 friend
9103 constexpr */
9104 case RID_FRIEND:
9105 if (!at_class_scope_p ())
9107 error_at (token->location, "%<friend%> used outside of class");
9108 cp_lexer_purge_token (parser->lexer);
9110 else
9112 ++decl_specs->specs[(int) ds_friend];
9113 /* Consume the token. */
9114 cp_lexer_consume_token (parser->lexer);
9116 break;
9118 case RID_CONSTEXPR:
9119 ++decl_specs->specs[(int) ds_constexpr];
9120 cp_lexer_consume_token (parser->lexer);
9121 break;
9123 /* function-specifier:
9124 inline
9125 virtual
9126 explicit */
9127 case RID_INLINE:
9128 case RID_VIRTUAL:
9129 case RID_EXPLICIT:
9130 cp_parser_function_specifier_opt (parser, decl_specs);
9131 break;
9133 /* decl-specifier:
9134 typedef */
9135 case RID_TYPEDEF:
9136 ++decl_specs->specs[(int) ds_typedef];
9137 /* Consume the token. */
9138 cp_lexer_consume_token (parser->lexer);
9139 /* A constructor declarator cannot appear in a typedef. */
9140 constructor_possible_p = false;
9141 /* The "typedef" keyword can only occur in a declaration; we
9142 may as well commit at this point. */
9143 cp_parser_commit_to_tentative_parse (parser);
9145 if (decl_specs->storage_class != sc_none)
9146 decl_specs->conflicting_specifiers_p = true;
9147 break;
9149 /* storage-class-specifier:
9150 auto
9151 register
9152 static
9153 extern
9154 mutable
9156 GNU Extension:
9157 thread */
9158 case RID_AUTO:
9159 if (cxx_dialect == cxx98)
9161 /* Consume the token. */
9162 cp_lexer_consume_token (parser->lexer);
9164 /* Complain about `auto' as a storage specifier, if
9165 we're complaining about C++0x compatibility. */
9166 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9167 " will change meaning in C++0x; please remove it");
9169 /* Set the storage class anyway. */
9170 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9171 token->location);
9173 else
9174 /* C++0x auto type-specifier. */
9175 found_decl_spec = false;
9176 break;
9178 case RID_REGISTER:
9179 case RID_STATIC:
9180 case RID_EXTERN:
9181 case RID_MUTABLE:
9182 /* Consume the token. */
9183 cp_lexer_consume_token (parser->lexer);
9184 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9185 token->location);
9186 break;
9187 case RID_THREAD:
9188 /* Consume the token. */
9189 cp_lexer_consume_token (parser->lexer);
9190 ++decl_specs->specs[(int) ds_thread];
9191 break;
9193 default:
9194 /* We did not yet find a decl-specifier yet. */
9195 found_decl_spec = false;
9196 break;
9199 /* Constructors are a special case. The `S' in `S()' is not a
9200 decl-specifier; it is the beginning of the declarator. */
9201 constructor_p
9202 = (!found_decl_spec
9203 && constructor_possible_p
9204 && (cp_parser_constructor_declarator_p
9205 (parser, decl_specs->specs[(int) ds_friend] != 0)));
9207 /* If we don't have a DECL_SPEC yet, then we must be looking at
9208 a type-specifier. */
9209 if (!found_decl_spec && !constructor_p)
9211 int decl_spec_declares_class_or_enum;
9212 bool is_cv_qualifier;
9213 tree type_spec;
9215 type_spec
9216 = cp_parser_type_specifier (parser, flags,
9217 decl_specs,
9218 /*is_declaration=*/true,
9219 &decl_spec_declares_class_or_enum,
9220 &is_cv_qualifier);
9221 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
9223 /* If this type-specifier referenced a user-defined type
9224 (a typedef, class-name, etc.), then we can't allow any
9225 more such type-specifiers henceforth.
9227 [dcl.spec]
9229 The longest sequence of decl-specifiers that could
9230 possibly be a type name is taken as the
9231 decl-specifier-seq of a declaration. The sequence shall
9232 be self-consistent as described below.
9234 [dcl.type]
9236 As a general rule, at most one type-specifier is allowed
9237 in the complete decl-specifier-seq of a declaration. The
9238 only exceptions are the following:
9240 -- const or volatile can be combined with any other
9241 type-specifier.
9243 -- signed or unsigned can be combined with char, long,
9244 short, or int.
9246 -- ..
9248 Example:
9250 typedef char* Pc;
9251 void g (const int Pc);
9253 Here, Pc is *not* part of the decl-specifier seq; it's
9254 the declarator. Therefore, once we see a type-specifier
9255 (other than a cv-qualifier), we forbid any additional
9256 user-defined types. We *do* still allow things like `int
9257 int' to be considered a decl-specifier-seq, and issue the
9258 error message later. */
9259 if (type_spec && !is_cv_qualifier)
9260 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
9261 /* A constructor declarator cannot follow a type-specifier. */
9262 if (type_spec)
9264 constructor_possible_p = false;
9265 found_decl_spec = true;
9266 if (!is_cv_qualifier)
9267 decl_specs->any_type_specifiers_p = true;
9271 /* If we still do not have a DECL_SPEC, then there are no more
9272 decl-specifiers. */
9273 if (!found_decl_spec)
9274 break;
9276 decl_specs->any_specifiers_p = true;
9277 /* After we see one decl-specifier, further decl-specifiers are
9278 always optional. */
9279 flags |= CP_PARSER_FLAGS_OPTIONAL;
9282 cp_parser_check_decl_spec (decl_specs, start_token->location);
9284 /* Don't allow a friend specifier with a class definition. */
9285 if (decl_specs->specs[(int) ds_friend] != 0
9286 && (*declares_class_or_enum & 2))
9287 error_at (start_token->location,
9288 "class definition may not be declared a friend");
9291 /* Parse an (optional) storage-class-specifier.
9293 storage-class-specifier:
9294 auto
9295 register
9296 static
9297 extern
9298 mutable
9300 GNU Extension:
9302 storage-class-specifier:
9303 thread
9305 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
9307 static tree
9308 cp_parser_storage_class_specifier_opt (cp_parser* parser)
9310 switch (cp_lexer_peek_token (parser->lexer)->keyword)
9312 case RID_AUTO:
9313 if (cxx_dialect != cxx98)
9314 return NULL_TREE;
9315 /* Fall through for C++98. */
9317 case RID_REGISTER:
9318 case RID_STATIC:
9319 case RID_EXTERN:
9320 case RID_MUTABLE:
9321 case RID_THREAD:
9322 /* Consume the token. */
9323 return cp_lexer_consume_token (parser->lexer)->u.value;
9325 default:
9326 return NULL_TREE;
9330 /* Parse an (optional) function-specifier.
9332 function-specifier:
9333 inline
9334 virtual
9335 explicit
9337 Returns an IDENTIFIER_NODE corresponding to the keyword used.
9338 Updates DECL_SPECS, if it is non-NULL. */
9340 static tree
9341 cp_parser_function_specifier_opt (cp_parser* parser,
9342 cp_decl_specifier_seq *decl_specs)
9344 cp_token *token = cp_lexer_peek_token (parser->lexer);
9345 switch (token->keyword)
9347 case RID_INLINE:
9348 if (decl_specs)
9349 ++decl_specs->specs[(int) ds_inline];
9350 break;
9352 case RID_VIRTUAL:
9353 /* 14.5.2.3 [temp.mem]
9355 A member function template shall not be virtual. */
9356 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
9357 error_at (token->location, "templates may not be %<virtual%>");
9358 else if (decl_specs)
9359 ++decl_specs->specs[(int) ds_virtual];
9360 break;
9362 case RID_EXPLICIT:
9363 if (decl_specs)
9364 ++decl_specs->specs[(int) ds_explicit];
9365 break;
9367 default:
9368 return NULL_TREE;
9371 /* Consume the token. */
9372 return cp_lexer_consume_token (parser->lexer)->u.value;
9375 /* Parse a linkage-specification.
9377 linkage-specification:
9378 extern string-literal { declaration-seq [opt] }
9379 extern string-literal declaration */
9381 static void
9382 cp_parser_linkage_specification (cp_parser* parser)
9384 tree linkage;
9386 /* Look for the `extern' keyword. */
9387 cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
9389 /* Look for the string-literal. */
9390 linkage = cp_parser_string_literal (parser, false, false);
9392 /* Transform the literal into an identifier. If the literal is a
9393 wide-character string, or contains embedded NULs, then we can't
9394 handle it as the user wants. */
9395 if (strlen (TREE_STRING_POINTER (linkage))
9396 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
9398 cp_parser_error (parser, "invalid linkage-specification");
9399 /* Assume C++ linkage. */
9400 linkage = lang_name_cplusplus;
9402 else
9403 linkage = get_identifier (TREE_STRING_POINTER (linkage));
9405 /* We're now using the new linkage. */
9406 push_lang_context (linkage);
9408 /* If the next token is a `{', then we're using the first
9409 production. */
9410 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9412 /* Consume the `{' token. */
9413 cp_lexer_consume_token (parser->lexer);
9414 /* Parse the declarations. */
9415 cp_parser_declaration_seq_opt (parser);
9416 /* Look for the closing `}'. */
9417 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
9419 /* Otherwise, there's just one declaration. */
9420 else
9422 bool saved_in_unbraced_linkage_specification_p;
9424 saved_in_unbraced_linkage_specification_p
9425 = parser->in_unbraced_linkage_specification_p;
9426 parser->in_unbraced_linkage_specification_p = true;
9427 cp_parser_declaration (parser);
9428 parser->in_unbraced_linkage_specification_p
9429 = saved_in_unbraced_linkage_specification_p;
9432 /* We're done with the linkage-specification. */
9433 pop_lang_context ();
9436 /* Parse a static_assert-declaration.
9438 static_assert-declaration:
9439 static_assert ( constant-expression , string-literal ) ;
9441 If MEMBER_P, this static_assert is a class member. */
9443 static void
9444 cp_parser_static_assert(cp_parser *parser, bool member_p)
9446 tree condition;
9447 tree message;
9448 cp_token *token;
9449 location_t saved_loc;
9451 /* Peek at the `static_assert' token so we can keep track of exactly
9452 where the static assertion started. */
9453 token = cp_lexer_peek_token (parser->lexer);
9454 saved_loc = token->location;
9456 /* Look for the `static_assert' keyword. */
9457 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
9458 "%<static_assert%>"))
9459 return;
9461 /* We know we are in a static assertion; commit to any tentative
9462 parse. */
9463 if (cp_parser_parsing_tentatively (parser))
9464 cp_parser_commit_to_tentative_parse (parser);
9466 /* Parse the `(' starting the static assertion condition. */
9467 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
9469 /* Parse the constant-expression. */
9470 condition =
9471 cp_parser_constant_expression (parser,
9472 /*allow_non_constant_p=*/false,
9473 /*non_constant_p=*/NULL);
9475 /* Parse the separating `,'. */
9476 cp_parser_require (parser, CPP_COMMA, "%<,%>");
9478 /* Parse the string-literal message. */
9479 message = cp_parser_string_literal (parser,
9480 /*translate=*/false,
9481 /*wide_ok=*/true);
9483 /* A `)' completes the static assertion. */
9484 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
9485 cp_parser_skip_to_closing_parenthesis (parser,
9486 /*recovering=*/true,
9487 /*or_comma=*/false,
9488 /*consume_paren=*/true);
9490 /* A semicolon terminates the declaration. */
9491 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
9493 /* Complete the static assertion, which may mean either processing
9494 the static assert now or saving it for template instantiation. */
9495 finish_static_assert (condition, message, saved_loc, member_p);
9498 /* Parse a `decltype' type. Returns the type.
9500 simple-type-specifier:
9501 decltype ( expression ) */
9503 static tree
9504 cp_parser_decltype (cp_parser *parser)
9506 tree expr;
9507 bool id_expression_or_member_access_p = false;
9508 const char *saved_message;
9509 bool saved_integral_constant_expression_p;
9510 bool saved_non_integral_constant_expression_p;
9511 cp_token *id_expr_start_token;
9513 /* Look for the `decltype' token. */
9514 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
9515 return error_mark_node;
9517 /* Types cannot be defined in a `decltype' expression. Save away the
9518 old message. */
9519 saved_message = parser->type_definition_forbidden_message;
9521 /* And create the new one. */
9522 parser->type_definition_forbidden_message
9523 = G_("types may not be defined in %<decltype%> expressions");
9525 /* The restrictions on constant-expressions do not apply inside
9526 decltype expressions. */
9527 saved_integral_constant_expression_p
9528 = parser->integral_constant_expression_p;
9529 saved_non_integral_constant_expression_p
9530 = parser->non_integral_constant_expression_p;
9531 parser->integral_constant_expression_p = false;
9533 /* Do not actually evaluate the expression. */
9534 ++cp_unevaluated_operand;
9536 /* Do not warn about problems with the expression. */
9537 ++c_inhibit_evaluation_warnings;
9539 /* Parse the opening `('. */
9540 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
9541 return error_mark_node;
9543 /* First, try parsing an id-expression. */
9544 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
9545 cp_parser_parse_tentatively (parser);
9546 expr = cp_parser_id_expression (parser,
9547 /*template_keyword_p=*/false,
9548 /*check_dependency_p=*/true,
9549 /*template_p=*/NULL,
9550 /*declarator_p=*/false,
9551 /*optional_p=*/false);
9553 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
9555 bool non_integral_constant_expression_p = false;
9556 tree id_expression = expr;
9557 cp_id_kind idk;
9558 const char *error_msg;
9560 if (TREE_CODE (expr) == IDENTIFIER_NODE)
9561 /* Lookup the name we got back from the id-expression. */
9562 expr = cp_parser_lookup_name (parser, expr,
9563 none_type,
9564 /*is_template=*/false,
9565 /*is_namespace=*/false,
9566 /*check_dependency=*/true,
9567 /*ambiguous_decls=*/NULL,
9568 id_expr_start_token->location);
9570 if (expr
9571 && expr != error_mark_node
9572 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
9573 && TREE_CODE (expr) != TYPE_DECL
9574 && (TREE_CODE (expr) != BIT_NOT_EXPR
9575 || !TYPE_P (TREE_OPERAND (expr, 0)))
9576 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9578 /* Complete lookup of the id-expression. */
9579 expr = (finish_id_expression
9580 (id_expression, expr, parser->scope, &idk,
9581 /*integral_constant_expression_p=*/false,
9582 /*allow_non_integral_constant_expression_p=*/true,
9583 &non_integral_constant_expression_p,
9584 /*template_p=*/false,
9585 /*done=*/true,
9586 /*address_p=*/false,
9587 /*template_arg_p=*/false,
9588 &error_msg,
9589 id_expr_start_token->location));
9591 if (expr == error_mark_node)
9592 /* We found an id-expression, but it was something that we
9593 should not have found. This is an error, not something
9594 we can recover from, so note that we found an
9595 id-expression and we'll recover as gracefully as
9596 possible. */
9597 id_expression_or_member_access_p = true;
9600 if (expr
9601 && expr != error_mark_node
9602 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9603 /* We have an id-expression. */
9604 id_expression_or_member_access_p = true;
9607 if (!id_expression_or_member_access_p)
9609 /* Abort the id-expression parse. */
9610 cp_parser_abort_tentative_parse (parser);
9612 /* Parsing tentatively, again. */
9613 cp_parser_parse_tentatively (parser);
9615 /* Parse a class member access. */
9616 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
9617 /*cast_p=*/false,
9618 /*member_access_only_p=*/true, NULL);
9620 if (expr
9621 && expr != error_mark_node
9622 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9623 /* We have an id-expression. */
9624 id_expression_or_member_access_p = true;
9627 if (id_expression_or_member_access_p)
9628 /* We have parsed the complete id-expression or member access. */
9629 cp_parser_parse_definitely (parser);
9630 else
9632 bool saved_greater_than_is_operator_p;
9634 /* Abort our attempt to parse an id-expression or member access
9635 expression. */
9636 cp_parser_abort_tentative_parse (parser);
9638 /* Within a parenthesized expression, a `>' token is always
9639 the greater-than operator. */
9640 saved_greater_than_is_operator_p
9641 = parser->greater_than_is_operator_p;
9642 parser->greater_than_is_operator_p = true;
9644 /* Parse a full expression. */
9645 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9647 /* The `>' token might be the end of a template-id or
9648 template-parameter-list now. */
9649 parser->greater_than_is_operator_p
9650 = saved_greater_than_is_operator_p;
9653 /* Go back to evaluating expressions. */
9654 --cp_unevaluated_operand;
9655 --c_inhibit_evaluation_warnings;
9657 /* Restore the old message and the integral constant expression
9658 flags. */
9659 parser->type_definition_forbidden_message = saved_message;
9660 parser->integral_constant_expression_p
9661 = saved_integral_constant_expression_p;
9662 parser->non_integral_constant_expression_p
9663 = saved_non_integral_constant_expression_p;
9665 if (expr == error_mark_node)
9667 /* Skip everything up to the closing `)'. */
9668 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9669 /*consume_paren=*/true);
9670 return error_mark_node;
9673 /* Parse to the closing `)'. */
9674 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
9676 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9677 /*consume_paren=*/true);
9678 return error_mark_node;
9681 return finish_decltype_type (expr, id_expression_or_member_access_p);
9684 /* Special member functions [gram.special] */
9686 /* Parse a conversion-function-id.
9688 conversion-function-id:
9689 operator conversion-type-id
9691 Returns an IDENTIFIER_NODE representing the operator. */
9693 static tree
9694 cp_parser_conversion_function_id (cp_parser* parser)
9696 tree type;
9697 tree saved_scope;
9698 tree saved_qualifying_scope;
9699 tree saved_object_scope;
9700 tree pushed_scope = NULL_TREE;
9702 /* Look for the `operator' token. */
9703 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9704 return error_mark_node;
9705 /* When we parse the conversion-type-id, the current scope will be
9706 reset. However, we need that information in able to look up the
9707 conversion function later, so we save it here. */
9708 saved_scope = parser->scope;
9709 saved_qualifying_scope = parser->qualifying_scope;
9710 saved_object_scope = parser->object_scope;
9711 /* We must enter the scope of the class so that the names of
9712 entities declared within the class are available in the
9713 conversion-type-id. For example, consider:
9715 struct S {
9716 typedef int I;
9717 operator I();
9720 S::operator I() { ... }
9722 In order to see that `I' is a type-name in the definition, we
9723 must be in the scope of `S'. */
9724 if (saved_scope)
9725 pushed_scope = push_scope (saved_scope);
9726 /* Parse the conversion-type-id. */
9727 type = cp_parser_conversion_type_id (parser);
9728 /* Leave the scope of the class, if any. */
9729 if (pushed_scope)
9730 pop_scope (pushed_scope);
9731 /* Restore the saved scope. */
9732 parser->scope = saved_scope;
9733 parser->qualifying_scope = saved_qualifying_scope;
9734 parser->object_scope = saved_object_scope;
9735 /* If the TYPE is invalid, indicate failure. */
9736 if (type == error_mark_node)
9737 return error_mark_node;
9738 return mangle_conv_op_name_for_type (type);
9741 /* Parse a conversion-type-id:
9743 conversion-type-id:
9744 type-specifier-seq conversion-declarator [opt]
9746 Returns the TYPE specified. */
9748 static tree
9749 cp_parser_conversion_type_id (cp_parser* parser)
9751 tree attributes;
9752 cp_decl_specifier_seq type_specifiers;
9753 cp_declarator *declarator;
9754 tree type_specified;
9756 /* Parse the attributes. */
9757 attributes = cp_parser_attributes_opt (parser);
9758 /* Parse the type-specifiers. */
9759 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
9760 /*is_trailing_return=*/false,
9761 &type_specifiers);
9762 /* If that didn't work, stop. */
9763 if (type_specifiers.type == error_mark_node)
9764 return error_mark_node;
9765 /* Parse the conversion-declarator. */
9766 declarator = cp_parser_conversion_declarator_opt (parser);
9768 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
9769 /*initialized=*/0, &attributes);
9770 if (attributes)
9771 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
9773 /* Don't give this error when parsing tentatively. This happens to
9774 work because we always parse this definitively once. */
9775 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
9776 && type_uses_auto (type_specified))
9778 error ("invalid use of %<auto%> in conversion operator");
9779 return error_mark_node;
9782 return type_specified;
9785 /* Parse an (optional) conversion-declarator.
9787 conversion-declarator:
9788 ptr-operator conversion-declarator [opt]
9792 static cp_declarator *
9793 cp_parser_conversion_declarator_opt (cp_parser* parser)
9795 enum tree_code code;
9796 tree class_type;
9797 cp_cv_quals cv_quals;
9799 /* We don't know if there's a ptr-operator next, or not. */
9800 cp_parser_parse_tentatively (parser);
9801 /* Try the ptr-operator. */
9802 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
9803 /* If it worked, look for more conversion-declarators. */
9804 if (cp_parser_parse_definitely (parser))
9806 cp_declarator *declarator;
9808 /* Parse another optional declarator. */
9809 declarator = cp_parser_conversion_declarator_opt (parser);
9811 return cp_parser_make_indirect_declarator
9812 (code, class_type, cv_quals, declarator);
9815 return NULL;
9818 /* Parse an (optional) ctor-initializer.
9820 ctor-initializer:
9821 : mem-initializer-list
9823 Returns TRUE iff the ctor-initializer was actually present. */
9825 static bool
9826 cp_parser_ctor_initializer_opt (cp_parser* parser)
9828 /* If the next token is not a `:', then there is no
9829 ctor-initializer. */
9830 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
9832 /* Do default initialization of any bases and members. */
9833 if (DECL_CONSTRUCTOR_P (current_function_decl))
9834 finish_mem_initializers (NULL_TREE);
9836 return false;
9839 /* Consume the `:' token. */
9840 cp_lexer_consume_token (parser->lexer);
9841 /* And the mem-initializer-list. */
9842 cp_parser_mem_initializer_list (parser);
9844 return true;
9847 /* Parse a mem-initializer-list.
9849 mem-initializer-list:
9850 mem-initializer ... [opt]
9851 mem-initializer ... [opt] , mem-initializer-list */
9853 static void
9854 cp_parser_mem_initializer_list (cp_parser* parser)
9856 tree mem_initializer_list = NULL_TREE;
9857 cp_token *token = cp_lexer_peek_token (parser->lexer);
9859 /* Let the semantic analysis code know that we are starting the
9860 mem-initializer-list. */
9861 if (!DECL_CONSTRUCTOR_P (current_function_decl))
9862 error_at (token->location,
9863 "only constructors take base initializers");
9865 /* Loop through the list. */
9866 while (true)
9868 tree mem_initializer;
9870 token = cp_lexer_peek_token (parser->lexer);
9871 /* Parse the mem-initializer. */
9872 mem_initializer = cp_parser_mem_initializer (parser);
9873 /* If the next token is a `...', we're expanding member initializers. */
9874 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9876 /* Consume the `...'. */
9877 cp_lexer_consume_token (parser->lexer);
9879 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9880 can be expanded but members cannot. */
9881 if (mem_initializer != error_mark_node
9882 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9884 error_at (token->location,
9885 "cannot expand initializer for member %<%D%>",
9886 TREE_PURPOSE (mem_initializer));
9887 mem_initializer = error_mark_node;
9890 /* Construct the pack expansion type. */
9891 if (mem_initializer != error_mark_node)
9892 mem_initializer = make_pack_expansion (mem_initializer);
9894 /* Add it to the list, unless it was erroneous. */
9895 if (mem_initializer != error_mark_node)
9897 TREE_CHAIN (mem_initializer) = mem_initializer_list;
9898 mem_initializer_list = mem_initializer;
9900 /* If the next token is not a `,', we're done. */
9901 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9902 break;
9903 /* Consume the `,' token. */
9904 cp_lexer_consume_token (parser->lexer);
9907 /* Perform semantic analysis. */
9908 if (DECL_CONSTRUCTOR_P (current_function_decl))
9909 finish_mem_initializers (mem_initializer_list);
9912 /* Parse a mem-initializer.
9914 mem-initializer:
9915 mem-initializer-id ( expression-list [opt] )
9916 mem-initializer-id braced-init-list
9918 GNU extension:
9920 mem-initializer:
9921 ( expression-list [opt] )
9923 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
9924 class) or FIELD_DECL (for a non-static data member) to initialize;
9925 the TREE_VALUE is the expression-list. An empty initialization
9926 list is represented by void_list_node. */
9928 static tree
9929 cp_parser_mem_initializer (cp_parser* parser)
9931 tree mem_initializer_id;
9932 tree expression_list;
9933 tree member;
9934 cp_token *token = cp_lexer_peek_token (parser->lexer);
9936 /* Find out what is being initialized. */
9937 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9939 permerror (token->location,
9940 "anachronistic old-style base class initializer");
9941 mem_initializer_id = NULL_TREE;
9943 else
9945 mem_initializer_id = cp_parser_mem_initializer_id (parser);
9946 if (mem_initializer_id == error_mark_node)
9947 return mem_initializer_id;
9949 member = expand_member_init (mem_initializer_id);
9950 if (member && !DECL_P (member))
9951 in_base_initializer = 1;
9953 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9955 bool expr_non_constant_p;
9956 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9957 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9958 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9959 expression_list = build_tree_list (NULL_TREE, expression_list);
9961 else
9963 VEC(tree,gc)* vec;
9964 vec = cp_parser_parenthesized_expression_list (parser, false,
9965 /*cast_p=*/false,
9966 /*allow_expansion_p=*/true,
9967 /*non_constant_p=*/NULL);
9968 if (vec == NULL)
9969 return error_mark_node;
9970 expression_list = build_tree_list_vec (vec);
9971 release_tree_vector (vec);
9974 if (expression_list == error_mark_node)
9975 return error_mark_node;
9976 if (!expression_list)
9977 expression_list = void_type_node;
9979 in_base_initializer = 0;
9981 return member ? build_tree_list (member, expression_list) : error_mark_node;
9984 /* Parse a mem-initializer-id.
9986 mem-initializer-id:
9987 :: [opt] nested-name-specifier [opt] class-name
9988 identifier
9990 Returns a TYPE indicating the class to be initializer for the first
9991 production. Returns an IDENTIFIER_NODE indicating the data member
9992 to be initialized for the second production. */
9994 static tree
9995 cp_parser_mem_initializer_id (cp_parser* parser)
9997 bool global_scope_p;
9998 bool nested_name_specifier_p;
9999 bool template_p = false;
10000 tree id;
10002 cp_token *token = cp_lexer_peek_token (parser->lexer);
10004 /* `typename' is not allowed in this context ([temp.res]). */
10005 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
10007 error_at (token->location,
10008 "keyword %<typename%> not allowed in this context (a qualified "
10009 "member initializer is implicitly a type)");
10010 cp_lexer_consume_token (parser->lexer);
10012 /* Look for the optional `::' operator. */
10013 global_scope_p
10014 = (cp_parser_global_scope_opt (parser,
10015 /*current_scope_valid_p=*/false)
10016 != NULL_TREE);
10017 /* Look for the optional nested-name-specifier. The simplest way to
10018 implement:
10020 [temp.res]
10022 The keyword `typename' is not permitted in a base-specifier or
10023 mem-initializer; in these contexts a qualified name that
10024 depends on a template-parameter is implicitly assumed to be a
10025 type name.
10027 is to assume that we have seen the `typename' keyword at this
10028 point. */
10029 nested_name_specifier_p
10030 = (cp_parser_nested_name_specifier_opt (parser,
10031 /*typename_keyword_p=*/true,
10032 /*check_dependency_p=*/true,
10033 /*type_p=*/true,
10034 /*is_declaration=*/true)
10035 != NULL_TREE);
10036 if (nested_name_specifier_p)
10037 template_p = cp_parser_optional_template_keyword (parser);
10038 /* If there is a `::' operator or a nested-name-specifier, then we
10039 are definitely looking for a class-name. */
10040 if (global_scope_p || nested_name_specifier_p)
10041 return cp_parser_class_name (parser,
10042 /*typename_keyword_p=*/true,
10043 /*template_keyword_p=*/template_p,
10044 typename_type,
10045 /*check_dependency_p=*/true,
10046 /*class_head_p=*/false,
10047 /*is_declaration=*/true);
10048 /* Otherwise, we could also be looking for an ordinary identifier. */
10049 cp_parser_parse_tentatively (parser);
10050 /* Try a class-name. */
10051 id = cp_parser_class_name (parser,
10052 /*typename_keyword_p=*/true,
10053 /*template_keyword_p=*/false,
10054 none_type,
10055 /*check_dependency_p=*/true,
10056 /*class_head_p=*/false,
10057 /*is_declaration=*/true);
10058 /* If we found one, we're done. */
10059 if (cp_parser_parse_definitely (parser))
10060 return id;
10061 /* Otherwise, look for an ordinary identifier. */
10062 return cp_parser_identifier (parser);
10065 /* Overloading [gram.over] */
10067 /* Parse an operator-function-id.
10069 operator-function-id:
10070 operator operator
10072 Returns an IDENTIFIER_NODE for the operator which is a
10073 human-readable spelling of the identifier, e.g., `operator +'. */
10075 static tree
10076 cp_parser_operator_function_id (cp_parser* parser)
10078 /* Look for the `operator' keyword. */
10079 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
10080 return error_mark_node;
10081 /* And then the name of the operator itself. */
10082 return cp_parser_operator (parser);
10085 /* Parse an operator.
10087 operator:
10088 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10089 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10090 || ++ -- , ->* -> () []
10092 GNU Extensions:
10094 operator:
10095 <? >? <?= >?=
10097 Returns an IDENTIFIER_NODE for the operator which is a
10098 human-readable spelling of the identifier, e.g., `operator +'. */
10100 static tree
10101 cp_parser_operator (cp_parser* parser)
10103 tree id = NULL_TREE;
10104 cp_token *token;
10106 /* Peek at the next token. */
10107 token = cp_lexer_peek_token (parser->lexer);
10108 /* Figure out which operator we have. */
10109 switch (token->type)
10111 case CPP_KEYWORD:
10113 enum tree_code op;
10115 /* The keyword should be either `new' or `delete'. */
10116 if (token->keyword == RID_NEW)
10117 op = NEW_EXPR;
10118 else if (token->keyword == RID_DELETE)
10119 op = DELETE_EXPR;
10120 else
10121 break;
10123 /* Consume the `new' or `delete' token. */
10124 cp_lexer_consume_token (parser->lexer);
10126 /* Peek at the next token. */
10127 token = cp_lexer_peek_token (parser->lexer);
10128 /* If it's a `[' token then this is the array variant of the
10129 operator. */
10130 if (token->type == CPP_OPEN_SQUARE)
10132 /* Consume the `[' token. */
10133 cp_lexer_consume_token (parser->lexer);
10134 /* Look for the `]' token. */
10135 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
10136 id = ansi_opname (op == NEW_EXPR
10137 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10139 /* Otherwise, we have the non-array variant. */
10140 else
10141 id = ansi_opname (op);
10143 return id;
10146 case CPP_PLUS:
10147 id = ansi_opname (PLUS_EXPR);
10148 break;
10150 case CPP_MINUS:
10151 id = ansi_opname (MINUS_EXPR);
10152 break;
10154 case CPP_MULT:
10155 id = ansi_opname (MULT_EXPR);
10156 break;
10158 case CPP_DIV:
10159 id = ansi_opname (TRUNC_DIV_EXPR);
10160 break;
10162 case CPP_MOD:
10163 id = ansi_opname (TRUNC_MOD_EXPR);
10164 break;
10166 case CPP_XOR:
10167 id = ansi_opname (BIT_XOR_EXPR);
10168 break;
10170 case CPP_AND:
10171 id = ansi_opname (BIT_AND_EXPR);
10172 break;
10174 case CPP_OR:
10175 id = ansi_opname (BIT_IOR_EXPR);
10176 break;
10178 case CPP_COMPL:
10179 id = ansi_opname (BIT_NOT_EXPR);
10180 break;
10182 case CPP_NOT:
10183 id = ansi_opname (TRUTH_NOT_EXPR);
10184 break;
10186 case CPP_EQ:
10187 id = ansi_assopname (NOP_EXPR);
10188 break;
10190 case CPP_LESS:
10191 id = ansi_opname (LT_EXPR);
10192 break;
10194 case CPP_GREATER:
10195 id = ansi_opname (GT_EXPR);
10196 break;
10198 case CPP_PLUS_EQ:
10199 id = ansi_assopname (PLUS_EXPR);
10200 break;
10202 case CPP_MINUS_EQ:
10203 id = ansi_assopname (MINUS_EXPR);
10204 break;
10206 case CPP_MULT_EQ:
10207 id = ansi_assopname (MULT_EXPR);
10208 break;
10210 case CPP_DIV_EQ:
10211 id = ansi_assopname (TRUNC_DIV_EXPR);
10212 break;
10214 case CPP_MOD_EQ:
10215 id = ansi_assopname (TRUNC_MOD_EXPR);
10216 break;
10218 case CPP_XOR_EQ:
10219 id = ansi_assopname (BIT_XOR_EXPR);
10220 break;
10222 case CPP_AND_EQ:
10223 id = ansi_assopname (BIT_AND_EXPR);
10224 break;
10226 case CPP_OR_EQ:
10227 id = ansi_assopname (BIT_IOR_EXPR);
10228 break;
10230 case CPP_LSHIFT:
10231 id = ansi_opname (LSHIFT_EXPR);
10232 break;
10234 case CPP_RSHIFT:
10235 id = ansi_opname (RSHIFT_EXPR);
10236 break;
10238 case CPP_LSHIFT_EQ:
10239 id = ansi_assopname (LSHIFT_EXPR);
10240 break;
10242 case CPP_RSHIFT_EQ:
10243 id = ansi_assopname (RSHIFT_EXPR);
10244 break;
10246 case CPP_EQ_EQ:
10247 id = ansi_opname (EQ_EXPR);
10248 break;
10250 case CPP_NOT_EQ:
10251 id = ansi_opname (NE_EXPR);
10252 break;
10254 case CPP_LESS_EQ:
10255 id = ansi_opname (LE_EXPR);
10256 break;
10258 case CPP_GREATER_EQ:
10259 id = ansi_opname (GE_EXPR);
10260 break;
10262 case CPP_AND_AND:
10263 id = ansi_opname (TRUTH_ANDIF_EXPR);
10264 break;
10266 case CPP_OR_OR:
10267 id = ansi_opname (TRUTH_ORIF_EXPR);
10268 break;
10270 case CPP_PLUS_PLUS:
10271 id = ansi_opname (POSTINCREMENT_EXPR);
10272 break;
10274 case CPP_MINUS_MINUS:
10275 id = ansi_opname (PREDECREMENT_EXPR);
10276 break;
10278 case CPP_COMMA:
10279 id = ansi_opname (COMPOUND_EXPR);
10280 break;
10282 case CPP_DEREF_STAR:
10283 id = ansi_opname (MEMBER_REF);
10284 break;
10286 case CPP_DEREF:
10287 id = ansi_opname (COMPONENT_REF);
10288 break;
10290 case CPP_OPEN_PAREN:
10291 /* Consume the `('. */
10292 cp_lexer_consume_token (parser->lexer);
10293 /* Look for the matching `)'. */
10294 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
10295 return ansi_opname (CALL_EXPR);
10297 case CPP_OPEN_SQUARE:
10298 /* Consume the `['. */
10299 cp_lexer_consume_token (parser->lexer);
10300 /* Look for the matching `]'. */
10301 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
10302 return ansi_opname (ARRAY_REF);
10304 default:
10305 /* Anything else is an error. */
10306 break;
10309 /* If we have selected an identifier, we need to consume the
10310 operator token. */
10311 if (id)
10312 cp_lexer_consume_token (parser->lexer);
10313 /* Otherwise, no valid operator name was present. */
10314 else
10316 cp_parser_error (parser, "expected operator");
10317 id = error_mark_node;
10320 return id;
10323 /* Parse a template-declaration.
10325 template-declaration:
10326 export [opt] template < template-parameter-list > declaration
10328 If MEMBER_P is TRUE, this template-declaration occurs within a
10329 class-specifier.
10331 The grammar rule given by the standard isn't correct. What
10332 is really meant is:
10334 template-declaration:
10335 export [opt] template-parameter-list-seq
10336 decl-specifier-seq [opt] init-declarator [opt] ;
10337 export [opt] template-parameter-list-seq
10338 function-definition
10340 template-parameter-list-seq:
10341 template-parameter-list-seq [opt]
10342 template < template-parameter-list > */
10344 static void
10345 cp_parser_template_declaration (cp_parser* parser, bool member_p)
10347 /* Check for `export'. */
10348 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
10350 /* Consume the `export' token. */
10351 cp_lexer_consume_token (parser->lexer);
10352 /* Warn that we do not support `export'. */
10353 warning (0, "keyword %<export%> not implemented, and will be ignored");
10356 cp_parser_template_declaration_after_export (parser, member_p);
10359 /* Parse a template-parameter-list.
10361 template-parameter-list:
10362 template-parameter
10363 template-parameter-list , template-parameter
10365 Returns a TREE_LIST. Each node represents a template parameter.
10366 The nodes are connected via their TREE_CHAINs. */
10368 static tree
10369 cp_parser_template_parameter_list (cp_parser* parser)
10371 tree parameter_list = NULL_TREE;
10373 begin_template_parm_list ();
10374 while (true)
10376 tree parameter;
10377 bool is_non_type;
10378 bool is_parameter_pack;
10379 location_t parm_loc;
10381 /* Parse the template-parameter. */
10382 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
10383 parameter = cp_parser_template_parameter (parser,
10384 &is_non_type,
10385 &is_parameter_pack);
10386 /* Add it to the list. */
10387 if (parameter != error_mark_node)
10388 parameter_list = process_template_parm (parameter_list,
10389 parm_loc,
10390 parameter,
10391 is_non_type,
10392 is_parameter_pack);
10393 else
10395 tree err_parm = build_tree_list (parameter, parameter);
10396 TREE_VALUE (err_parm) = error_mark_node;
10397 parameter_list = chainon (parameter_list, err_parm);
10400 /* If the next token is not a `,', we're done. */
10401 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10402 break;
10403 /* Otherwise, consume the `,' token. */
10404 cp_lexer_consume_token (parser->lexer);
10407 return end_template_parm_list (parameter_list);
10410 /* Parse a template-parameter.
10412 template-parameter:
10413 type-parameter
10414 parameter-declaration
10416 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
10417 the parameter. The TREE_PURPOSE is the default value, if any.
10418 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
10419 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
10420 set to true iff this parameter is a parameter pack. */
10422 static tree
10423 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
10424 bool *is_parameter_pack)
10426 cp_token *token;
10427 cp_parameter_declarator *parameter_declarator;
10428 cp_declarator *id_declarator;
10429 tree parm;
10431 /* Assume it is a type parameter or a template parameter. */
10432 *is_non_type = false;
10433 /* Assume it not a parameter pack. */
10434 *is_parameter_pack = false;
10435 /* Peek at the next token. */
10436 token = cp_lexer_peek_token (parser->lexer);
10437 /* If it is `class' or `template', we have a type-parameter. */
10438 if (token->keyword == RID_TEMPLATE)
10439 return cp_parser_type_parameter (parser, is_parameter_pack);
10440 /* If it is `class' or `typename' we do not know yet whether it is a
10441 type parameter or a non-type parameter. Consider:
10443 template <typename T, typename T::X X> ...
10447 template <class C, class D*> ...
10449 Here, the first parameter is a type parameter, and the second is
10450 a non-type parameter. We can tell by looking at the token after
10451 the identifier -- if it is a `,', `=', or `>' then we have a type
10452 parameter. */
10453 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
10455 /* Peek at the token after `class' or `typename'. */
10456 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10457 /* If it's an ellipsis, we have a template type parameter
10458 pack. */
10459 if (token->type == CPP_ELLIPSIS)
10460 return cp_parser_type_parameter (parser, is_parameter_pack);
10461 /* If it's an identifier, skip it. */
10462 if (token->type == CPP_NAME)
10463 token = cp_lexer_peek_nth_token (parser->lexer, 3);
10464 /* Now, see if the token looks like the end of a template
10465 parameter. */
10466 if (token->type == CPP_COMMA
10467 || token->type == CPP_EQ
10468 || token->type == CPP_GREATER)
10469 return cp_parser_type_parameter (parser, is_parameter_pack);
10472 /* Otherwise, it is a non-type parameter.
10474 [temp.param]
10476 When parsing a default template-argument for a non-type
10477 template-parameter, the first non-nested `>' is taken as the end
10478 of the template parameter-list rather than a greater-than
10479 operator. */
10480 *is_non_type = true;
10481 parameter_declarator
10482 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
10483 /*parenthesized_p=*/NULL);
10485 /* If the parameter declaration is marked as a parameter pack, set
10486 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
10487 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
10488 grokdeclarator. */
10489 if (parameter_declarator
10490 && parameter_declarator->declarator
10491 && parameter_declarator->declarator->parameter_pack_p)
10493 *is_parameter_pack = true;
10494 parameter_declarator->declarator->parameter_pack_p = false;
10497 /* If the next token is an ellipsis, and we don't already have it
10498 marked as a parameter pack, then we have a parameter pack (that
10499 has no declarator). */
10500 if (!*is_parameter_pack
10501 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
10502 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
10504 /* Consume the `...'. */
10505 cp_lexer_consume_token (parser->lexer);
10506 maybe_warn_variadic_templates ();
10508 *is_parameter_pack = true;
10510 /* We might end up with a pack expansion as the type of the non-type
10511 template parameter, in which case this is a non-type template
10512 parameter pack. */
10513 else if (parameter_declarator
10514 && parameter_declarator->decl_specifiers.type
10515 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
10517 *is_parameter_pack = true;
10518 parameter_declarator->decl_specifiers.type =
10519 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
10522 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10524 /* Parameter packs cannot have default arguments. However, a
10525 user may try to do so, so we'll parse them and give an
10526 appropriate diagnostic here. */
10528 /* Consume the `='. */
10529 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
10530 cp_lexer_consume_token (parser->lexer);
10532 /* Find the name of the parameter pack. */
10533 id_declarator = parameter_declarator->declarator;
10534 while (id_declarator && id_declarator->kind != cdk_id)
10535 id_declarator = id_declarator->declarator;
10537 if (id_declarator && id_declarator->kind == cdk_id)
10538 error_at (start_token->location,
10539 "template parameter pack %qD cannot have a default argument",
10540 id_declarator->u.id.unqualified_name);
10541 else
10542 error_at (start_token->location,
10543 "template parameter pack cannot have a default argument");
10545 /* Parse the default argument, but throw away the result. */
10546 cp_parser_default_argument (parser, /*template_parm_p=*/true);
10549 parm = grokdeclarator (parameter_declarator->declarator,
10550 &parameter_declarator->decl_specifiers,
10551 TPARM, /*initialized=*/0,
10552 /*attrlist=*/NULL);
10553 if (parm == error_mark_node)
10554 return error_mark_node;
10556 return build_tree_list (parameter_declarator->default_argument, parm);
10559 /* Parse a type-parameter.
10561 type-parameter:
10562 class identifier [opt]
10563 class identifier [opt] = type-id
10564 typename identifier [opt]
10565 typename identifier [opt] = type-id
10566 template < template-parameter-list > class identifier [opt]
10567 template < template-parameter-list > class identifier [opt]
10568 = id-expression
10570 GNU Extension (variadic templates):
10572 type-parameter:
10573 class ... identifier [opt]
10574 typename ... identifier [opt]
10576 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
10577 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
10578 the declaration of the parameter.
10580 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
10582 static tree
10583 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
10585 cp_token *token;
10586 tree parameter;
10588 /* Look for a keyword to tell us what kind of parameter this is. */
10589 token = cp_parser_require (parser, CPP_KEYWORD,
10590 "%<class%>, %<typename%>, or %<template%>");
10591 if (!token)
10592 return error_mark_node;
10594 switch (token->keyword)
10596 case RID_CLASS:
10597 case RID_TYPENAME:
10599 tree identifier;
10600 tree default_argument;
10602 /* If the next token is an ellipsis, we have a template
10603 argument pack. */
10604 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10606 /* Consume the `...' token. */
10607 cp_lexer_consume_token (parser->lexer);
10608 maybe_warn_variadic_templates ();
10610 *is_parameter_pack = true;
10613 /* If the next token is an identifier, then it names the
10614 parameter. */
10615 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10616 identifier = cp_parser_identifier (parser);
10617 else
10618 identifier = NULL_TREE;
10620 /* Create the parameter. */
10621 parameter = finish_template_type_parm (class_type_node, identifier);
10623 /* If the next token is an `=', we have a default argument. */
10624 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10626 /* Consume the `=' token. */
10627 cp_lexer_consume_token (parser->lexer);
10628 /* Parse the default-argument. */
10629 push_deferring_access_checks (dk_no_deferred);
10630 default_argument = cp_parser_type_id (parser);
10632 /* Template parameter packs cannot have default
10633 arguments. */
10634 if (*is_parameter_pack)
10636 if (identifier)
10637 error_at (token->location,
10638 "template parameter pack %qD cannot have a "
10639 "default argument", identifier);
10640 else
10641 error_at (token->location,
10642 "template parameter packs cannot have "
10643 "default arguments");
10644 default_argument = NULL_TREE;
10646 pop_deferring_access_checks ();
10648 else
10649 default_argument = NULL_TREE;
10651 /* Create the combined representation of the parameter and the
10652 default argument. */
10653 parameter = build_tree_list (default_argument, parameter);
10655 break;
10657 case RID_TEMPLATE:
10659 tree identifier;
10660 tree default_argument;
10662 /* Look for the `<'. */
10663 cp_parser_require (parser, CPP_LESS, "%<<%>");
10664 /* Parse the template-parameter-list. */
10665 cp_parser_template_parameter_list (parser);
10666 /* Look for the `>'. */
10667 cp_parser_require (parser, CPP_GREATER, "%<>%>");
10668 /* Look for the `class' keyword. */
10669 cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
10670 /* If the next token is an ellipsis, we have a template
10671 argument pack. */
10672 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10674 /* Consume the `...' token. */
10675 cp_lexer_consume_token (parser->lexer);
10676 maybe_warn_variadic_templates ();
10678 *is_parameter_pack = true;
10680 /* If the next token is an `=', then there is a
10681 default-argument. If the next token is a `>', we are at
10682 the end of the parameter-list. If the next token is a `,',
10683 then we are at the end of this parameter. */
10684 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10685 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
10686 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10688 identifier = cp_parser_identifier (parser);
10689 /* Treat invalid names as if the parameter were nameless. */
10690 if (identifier == error_mark_node)
10691 identifier = NULL_TREE;
10693 else
10694 identifier = NULL_TREE;
10696 /* Create the template parameter. */
10697 parameter = finish_template_template_parm (class_type_node,
10698 identifier);
10700 /* If the next token is an `=', then there is a
10701 default-argument. */
10702 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10704 bool is_template;
10706 /* Consume the `='. */
10707 cp_lexer_consume_token (parser->lexer);
10708 /* Parse the id-expression. */
10709 push_deferring_access_checks (dk_no_deferred);
10710 /* save token before parsing the id-expression, for error
10711 reporting */
10712 token = cp_lexer_peek_token (parser->lexer);
10713 default_argument
10714 = cp_parser_id_expression (parser,
10715 /*template_keyword_p=*/false,
10716 /*check_dependency_p=*/true,
10717 /*template_p=*/&is_template,
10718 /*declarator_p=*/false,
10719 /*optional_p=*/false);
10720 if (TREE_CODE (default_argument) == TYPE_DECL)
10721 /* If the id-expression was a template-id that refers to
10722 a template-class, we already have the declaration here,
10723 so no further lookup is needed. */
10725 else
10726 /* Look up the name. */
10727 default_argument
10728 = cp_parser_lookup_name (parser, default_argument,
10729 none_type,
10730 /*is_template=*/is_template,
10731 /*is_namespace=*/false,
10732 /*check_dependency=*/true,
10733 /*ambiguous_decls=*/NULL,
10734 token->location);
10735 /* See if the default argument is valid. */
10736 default_argument
10737 = check_template_template_default_arg (default_argument);
10739 /* Template parameter packs cannot have default
10740 arguments. */
10741 if (*is_parameter_pack)
10743 if (identifier)
10744 error_at (token->location,
10745 "template parameter pack %qD cannot "
10746 "have a default argument",
10747 identifier);
10748 else
10749 error_at (token->location, "template parameter packs cannot "
10750 "have default arguments");
10751 default_argument = NULL_TREE;
10753 pop_deferring_access_checks ();
10755 else
10756 default_argument = NULL_TREE;
10758 /* Create the combined representation of the parameter and the
10759 default argument. */
10760 parameter = build_tree_list (default_argument, parameter);
10762 break;
10764 default:
10765 gcc_unreachable ();
10766 break;
10769 return parameter;
10772 /* Parse a template-id.
10774 template-id:
10775 template-name < template-argument-list [opt] >
10777 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
10778 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
10779 returned. Otherwise, if the template-name names a function, or set
10780 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
10781 names a class, returns a TYPE_DECL for the specialization.
10783 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
10784 uninstantiated templates. */
10786 static tree
10787 cp_parser_template_id (cp_parser *parser,
10788 bool template_keyword_p,
10789 bool check_dependency_p,
10790 bool is_declaration)
10792 int i;
10793 tree templ;
10794 tree arguments;
10795 tree template_id;
10796 cp_token_position start_of_id = 0;
10797 deferred_access_check *chk;
10798 VEC (deferred_access_check,gc) *access_check;
10799 cp_token *next_token = NULL, *next_token_2 = NULL;
10800 bool is_identifier;
10802 /* If the next token corresponds to a template-id, there is no need
10803 to reparse it. */
10804 next_token = cp_lexer_peek_token (parser->lexer);
10805 if (next_token->type == CPP_TEMPLATE_ID)
10807 struct tree_check *check_value;
10809 /* Get the stored value. */
10810 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
10811 /* Perform any access checks that were deferred. */
10812 access_check = check_value->checks;
10813 if (access_check)
10815 for (i = 0 ;
10816 VEC_iterate (deferred_access_check, access_check, i, chk) ;
10817 ++i)
10819 perform_or_defer_access_check (chk->binfo,
10820 chk->decl,
10821 chk->diag_decl);
10824 /* Return the stored value. */
10825 return check_value->value;
10828 /* Avoid performing name lookup if there is no possibility of
10829 finding a template-id. */
10830 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
10831 || (next_token->type == CPP_NAME
10832 && !cp_parser_nth_token_starts_template_argument_list_p
10833 (parser, 2)))
10835 cp_parser_error (parser, "expected template-id");
10836 return error_mark_node;
10839 /* Remember where the template-id starts. */
10840 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
10841 start_of_id = cp_lexer_token_position (parser->lexer, false);
10843 push_deferring_access_checks (dk_deferred);
10845 /* Parse the template-name. */
10846 is_identifier = false;
10847 templ = cp_parser_template_name (parser, template_keyword_p,
10848 check_dependency_p,
10849 is_declaration,
10850 &is_identifier);
10851 if (templ == error_mark_node || is_identifier)
10853 pop_deferring_access_checks ();
10854 return templ;
10857 /* If we find the sequence `[:' after a template-name, it's probably
10858 a digraph-typo for `< ::'. Substitute the tokens and check if we can
10859 parse correctly the argument list. */
10860 next_token = cp_lexer_peek_token (parser->lexer);
10861 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10862 if (next_token->type == CPP_OPEN_SQUARE
10863 && next_token->flags & DIGRAPH
10864 && next_token_2->type == CPP_COLON
10865 && !(next_token_2->flags & PREV_WHITE))
10867 cp_parser_parse_tentatively (parser);
10868 /* Change `:' into `::'. */
10869 next_token_2->type = CPP_SCOPE;
10870 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
10871 CPP_LESS. */
10872 cp_lexer_consume_token (parser->lexer);
10874 /* Parse the arguments. */
10875 arguments = cp_parser_enclosed_template_argument_list (parser);
10876 if (!cp_parser_parse_definitely (parser))
10878 /* If we couldn't parse an argument list, then we revert our changes
10879 and return simply an error. Maybe this is not a template-id
10880 after all. */
10881 next_token_2->type = CPP_COLON;
10882 cp_parser_error (parser, "expected %<<%>");
10883 pop_deferring_access_checks ();
10884 return error_mark_node;
10886 /* Otherwise, emit an error about the invalid digraph, but continue
10887 parsing because we got our argument list. */
10888 if (permerror (next_token->location,
10889 "%<<::%> cannot begin a template-argument list"))
10891 static bool hint = false;
10892 inform (next_token->location,
10893 "%<<:%> is an alternate spelling for %<[%>."
10894 " Insert whitespace between %<<%> and %<::%>");
10895 if (!hint && !flag_permissive)
10897 inform (next_token->location, "(if you use %<-fpermissive%>"
10898 " G++ will accept your code)");
10899 hint = true;
10903 else
10905 /* Look for the `<' that starts the template-argument-list. */
10906 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10908 pop_deferring_access_checks ();
10909 return error_mark_node;
10911 /* Parse the arguments. */
10912 arguments = cp_parser_enclosed_template_argument_list (parser);
10915 /* Build a representation of the specialization. */
10916 if (TREE_CODE (templ) == IDENTIFIER_NODE)
10917 template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10918 else if (DECL_CLASS_TEMPLATE_P (templ)
10919 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10921 bool entering_scope;
10922 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10923 template (rather than some instantiation thereof) only if
10924 is not nested within some other construct. For example, in
10925 "template <typename T> void f(T) { A<T>::", A<T> is just an
10926 instantiation of A. */
10927 entering_scope = (template_parm_scope_p ()
10928 && cp_lexer_next_token_is (parser->lexer,
10929 CPP_SCOPE));
10930 template_id
10931 = finish_template_type (templ, arguments, entering_scope);
10933 else
10935 /* If it's not a class-template or a template-template, it should be
10936 a function-template. */
10937 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10938 || TREE_CODE (templ) == OVERLOAD
10939 || BASELINK_P (templ)));
10941 template_id = lookup_template_function (templ, arguments);
10944 /* If parsing tentatively, replace the sequence of tokens that makes
10945 up the template-id with a CPP_TEMPLATE_ID token. That way,
10946 should we re-parse the token stream, we will not have to repeat
10947 the effort required to do the parse, nor will we issue duplicate
10948 error messages about problems during instantiation of the
10949 template. */
10950 if (start_of_id)
10952 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10954 /* Reset the contents of the START_OF_ID token. */
10955 token->type = CPP_TEMPLATE_ID;
10956 /* Retrieve any deferred checks. Do not pop this access checks yet
10957 so the memory will not be reclaimed during token replacing below. */
10958 token->u.tree_check_value = GGC_CNEW (struct tree_check);
10959 token->u.tree_check_value->value = template_id;
10960 token->u.tree_check_value->checks = get_deferred_access_checks ();
10961 token->keyword = RID_MAX;
10963 /* Purge all subsequent tokens. */
10964 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10966 /* ??? Can we actually assume that, if template_id ==
10967 error_mark_node, we will have issued a diagnostic to the
10968 user, as opposed to simply marking the tentative parse as
10969 failed? */
10970 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10971 error_at (token->location, "parse error in template argument list");
10974 pop_deferring_access_checks ();
10975 return template_id;
10978 /* Parse a template-name.
10980 template-name:
10981 identifier
10983 The standard should actually say:
10985 template-name:
10986 identifier
10987 operator-function-id
10989 A defect report has been filed about this issue.
10991 A conversion-function-id cannot be a template name because they cannot
10992 be part of a template-id. In fact, looking at this code:
10994 a.operator K<int>()
10996 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10997 It is impossible to call a templated conversion-function-id with an
10998 explicit argument list, since the only allowed template parameter is
10999 the type to which it is converting.
11001 If TEMPLATE_KEYWORD_P is true, then we have just seen the
11002 `template' keyword, in a construction like:
11004 T::template f<3>()
11006 In that case `f' is taken to be a template-name, even though there
11007 is no way of knowing for sure.
11009 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
11010 name refers to a set of overloaded functions, at least one of which
11011 is a template, or an IDENTIFIER_NODE with the name of the template,
11012 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
11013 names are looked up inside uninstantiated templates. */
11015 static tree
11016 cp_parser_template_name (cp_parser* parser,
11017 bool template_keyword_p,
11018 bool check_dependency_p,
11019 bool is_declaration,
11020 bool *is_identifier)
11022 tree identifier;
11023 tree decl;
11024 tree fns;
11025 cp_token *token = cp_lexer_peek_token (parser->lexer);
11027 /* If the next token is `operator', then we have either an
11028 operator-function-id or a conversion-function-id. */
11029 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
11031 /* We don't know whether we're looking at an
11032 operator-function-id or a conversion-function-id. */
11033 cp_parser_parse_tentatively (parser);
11034 /* Try an operator-function-id. */
11035 identifier = cp_parser_operator_function_id (parser);
11036 /* If that didn't work, try a conversion-function-id. */
11037 if (!cp_parser_parse_definitely (parser))
11039 cp_parser_error (parser, "expected template-name");
11040 return error_mark_node;
11043 /* Look for the identifier. */
11044 else
11045 identifier = cp_parser_identifier (parser);
11047 /* If we didn't find an identifier, we don't have a template-id. */
11048 if (identifier == error_mark_node)
11049 return error_mark_node;
11051 /* If the name immediately followed the `template' keyword, then it
11052 is a template-name. However, if the next token is not `<', then
11053 we do not treat it as a template-name, since it is not being used
11054 as part of a template-id. This enables us to handle constructs
11055 like:
11057 template <typename T> struct S { S(); };
11058 template <typename T> S<T>::S();
11060 correctly. We would treat `S' as a template -- if it were `S<T>'
11061 -- but we do not if there is no `<'. */
11063 if (processing_template_decl
11064 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
11066 /* In a declaration, in a dependent context, we pretend that the
11067 "template" keyword was present in order to improve error
11068 recovery. For example, given:
11070 template <typename T> void f(T::X<int>);
11072 we want to treat "X<int>" as a template-id. */
11073 if (is_declaration
11074 && !template_keyword_p
11075 && parser->scope && TYPE_P (parser->scope)
11076 && check_dependency_p
11077 && dependent_scope_p (parser->scope)
11078 /* Do not do this for dtors (or ctors), since they never
11079 need the template keyword before their name. */
11080 && !constructor_name_p (identifier, parser->scope))
11082 cp_token_position start = 0;
11084 /* Explain what went wrong. */
11085 error_at (token->location, "non-template %qD used as template",
11086 identifier);
11087 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
11088 parser->scope, identifier);
11089 /* If parsing tentatively, find the location of the "<" token. */
11090 if (cp_parser_simulate_error (parser))
11091 start = cp_lexer_token_position (parser->lexer, true);
11092 /* Parse the template arguments so that we can issue error
11093 messages about them. */
11094 cp_lexer_consume_token (parser->lexer);
11095 cp_parser_enclosed_template_argument_list (parser);
11096 /* Skip tokens until we find a good place from which to
11097 continue parsing. */
11098 cp_parser_skip_to_closing_parenthesis (parser,
11099 /*recovering=*/true,
11100 /*or_comma=*/true,
11101 /*consume_paren=*/false);
11102 /* If parsing tentatively, permanently remove the
11103 template argument list. That will prevent duplicate
11104 error messages from being issued about the missing
11105 "template" keyword. */
11106 if (start)
11107 cp_lexer_purge_tokens_after (parser->lexer, start);
11108 if (is_identifier)
11109 *is_identifier = true;
11110 return identifier;
11113 /* If the "template" keyword is present, then there is generally
11114 no point in doing name-lookup, so we just return IDENTIFIER.
11115 But, if the qualifying scope is non-dependent then we can
11116 (and must) do name-lookup normally. */
11117 if (template_keyword_p
11118 && (!parser->scope
11119 || (TYPE_P (parser->scope)
11120 && dependent_type_p (parser->scope))))
11121 return identifier;
11124 /* Look up the name. */
11125 decl = cp_parser_lookup_name (parser, identifier,
11126 none_type,
11127 /*is_template=*/true,
11128 /*is_namespace=*/false,
11129 check_dependency_p,
11130 /*ambiguous_decls=*/NULL,
11131 token->location);
11133 /* If DECL is a template, then the name was a template-name. */
11134 if (TREE_CODE (decl) == TEMPLATE_DECL)
11136 else
11138 tree fn = NULL_TREE;
11140 /* The standard does not explicitly indicate whether a name that
11141 names a set of overloaded declarations, some of which are
11142 templates, is a template-name. However, such a name should
11143 be a template-name; otherwise, there is no way to form a
11144 template-id for the overloaded templates. */
11145 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11146 if (TREE_CODE (fns) == OVERLOAD)
11147 for (fn = fns; fn; fn = OVL_NEXT (fn))
11148 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11149 break;
11151 if (!fn)
11153 /* The name does not name a template. */
11154 cp_parser_error (parser, "expected template-name");
11155 return error_mark_node;
11159 /* If DECL is dependent, and refers to a function, then just return
11160 its name; we will look it up again during template instantiation. */
11161 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11163 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
11164 if (TYPE_P (scope) && dependent_type_p (scope))
11165 return identifier;
11168 return decl;
11171 /* Parse a template-argument-list.
11173 template-argument-list:
11174 template-argument ... [opt]
11175 template-argument-list , template-argument ... [opt]
11177 Returns a TREE_VEC containing the arguments. */
11179 static tree
11180 cp_parser_template_argument_list (cp_parser* parser)
11182 tree fixed_args[10];
11183 unsigned n_args = 0;
11184 unsigned alloced = 10;
11185 tree *arg_ary = fixed_args;
11186 tree vec;
11187 bool saved_in_template_argument_list_p;
11188 bool saved_ice_p;
11189 bool saved_non_ice_p;
11191 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
11192 parser->in_template_argument_list_p = true;
11193 /* Even if the template-id appears in an integral
11194 constant-expression, the contents of the argument list do
11195 not. */
11196 saved_ice_p = parser->integral_constant_expression_p;
11197 parser->integral_constant_expression_p = false;
11198 saved_non_ice_p = parser->non_integral_constant_expression_p;
11199 parser->non_integral_constant_expression_p = false;
11200 /* Parse the arguments. */
11203 tree argument;
11205 if (n_args)
11206 /* Consume the comma. */
11207 cp_lexer_consume_token (parser->lexer);
11209 /* Parse the template-argument. */
11210 argument = cp_parser_template_argument (parser);
11212 /* If the next token is an ellipsis, we're expanding a template
11213 argument pack. */
11214 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11216 if (argument == error_mark_node)
11218 cp_token *token = cp_lexer_peek_token (parser->lexer);
11219 error_at (token->location,
11220 "expected parameter pack before %<...%>");
11222 /* Consume the `...' token. */
11223 cp_lexer_consume_token (parser->lexer);
11225 /* Make the argument into a TYPE_PACK_EXPANSION or
11226 EXPR_PACK_EXPANSION. */
11227 argument = make_pack_expansion (argument);
11230 if (n_args == alloced)
11232 alloced *= 2;
11234 if (arg_ary == fixed_args)
11236 arg_ary = XNEWVEC (tree, alloced);
11237 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
11239 else
11240 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
11242 arg_ary[n_args++] = argument;
11244 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
11246 vec = make_tree_vec (n_args);
11248 while (n_args--)
11249 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
11251 if (arg_ary != fixed_args)
11252 free (arg_ary);
11253 parser->non_integral_constant_expression_p = saved_non_ice_p;
11254 parser->integral_constant_expression_p = saved_ice_p;
11255 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
11256 #ifdef ENABLE_CHECKING
11257 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
11258 #endif
11259 return vec;
11262 /* Parse a template-argument.
11264 template-argument:
11265 assignment-expression
11266 type-id
11267 id-expression
11269 The representation is that of an assignment-expression, type-id, or
11270 id-expression -- except that the qualified id-expression is
11271 evaluated, so that the value returned is either a DECL or an
11272 OVERLOAD.
11274 Although the standard says "assignment-expression", it forbids
11275 throw-expressions or assignments in the template argument.
11276 Therefore, we use "conditional-expression" instead. */
11278 static tree
11279 cp_parser_template_argument (cp_parser* parser)
11281 tree argument;
11282 bool template_p;
11283 bool address_p;
11284 bool maybe_type_id = false;
11285 cp_token *token = NULL, *argument_start_token = NULL;
11286 cp_id_kind idk;
11288 /* There's really no way to know what we're looking at, so we just
11289 try each alternative in order.
11291 [temp.arg]
11293 In a template-argument, an ambiguity between a type-id and an
11294 expression is resolved to a type-id, regardless of the form of
11295 the corresponding template-parameter.
11297 Therefore, we try a type-id first. */
11298 cp_parser_parse_tentatively (parser);
11299 argument = cp_parser_template_type_arg (parser);
11300 /* If there was no error parsing the type-id but the next token is a
11301 '>>', our behavior depends on which dialect of C++ we're
11302 parsing. In C++98, we probably found a typo for '> >'. But there
11303 are type-id which are also valid expressions. For instance:
11305 struct X { int operator >> (int); };
11306 template <int V> struct Foo {};
11307 Foo<X () >> 5> r;
11309 Here 'X()' is a valid type-id of a function type, but the user just
11310 wanted to write the expression "X() >> 5". Thus, we remember that we
11311 found a valid type-id, but we still try to parse the argument as an
11312 expression to see what happens.
11314 In C++0x, the '>>' will be considered two separate '>'
11315 tokens. */
11316 if (!cp_parser_error_occurred (parser)
11317 && cxx_dialect == cxx98
11318 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
11320 maybe_type_id = true;
11321 cp_parser_abort_tentative_parse (parser);
11323 else
11325 /* If the next token isn't a `,' or a `>', then this argument wasn't
11326 really finished. This means that the argument is not a valid
11327 type-id. */
11328 if (!cp_parser_next_token_ends_template_argument_p (parser))
11329 cp_parser_error (parser, "expected template-argument");
11330 /* If that worked, we're done. */
11331 if (cp_parser_parse_definitely (parser))
11332 return argument;
11334 /* We're still not sure what the argument will be. */
11335 cp_parser_parse_tentatively (parser);
11336 /* Try a template. */
11337 argument_start_token = cp_lexer_peek_token (parser->lexer);
11338 argument = cp_parser_id_expression (parser,
11339 /*template_keyword_p=*/false,
11340 /*check_dependency_p=*/true,
11341 &template_p,
11342 /*declarator_p=*/false,
11343 /*optional_p=*/false);
11344 /* If the next token isn't a `,' or a `>', then this argument wasn't
11345 really finished. */
11346 if (!cp_parser_next_token_ends_template_argument_p (parser))
11347 cp_parser_error (parser, "expected template-argument");
11348 if (!cp_parser_error_occurred (parser))
11350 /* Figure out what is being referred to. If the id-expression
11351 was for a class template specialization, then we will have a
11352 TYPE_DECL at this point. There is no need to do name lookup
11353 at this point in that case. */
11354 if (TREE_CODE (argument) != TYPE_DECL)
11355 argument = cp_parser_lookup_name (parser, argument,
11356 none_type,
11357 /*is_template=*/template_p,
11358 /*is_namespace=*/false,
11359 /*check_dependency=*/true,
11360 /*ambiguous_decls=*/NULL,
11361 argument_start_token->location);
11362 if (TREE_CODE (argument) != TEMPLATE_DECL
11363 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
11364 cp_parser_error (parser, "expected template-name");
11366 if (cp_parser_parse_definitely (parser))
11367 return argument;
11368 /* It must be a non-type argument. There permitted cases are given
11369 in [temp.arg.nontype]:
11371 -- an integral constant-expression of integral or enumeration
11372 type; or
11374 -- the name of a non-type template-parameter; or
11376 -- the name of an object or function with external linkage...
11378 -- the address of an object or function with external linkage...
11380 -- a pointer to member... */
11381 /* Look for a non-type template parameter. */
11382 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11384 cp_parser_parse_tentatively (parser);
11385 argument = cp_parser_primary_expression (parser,
11386 /*address_p=*/false,
11387 /*cast_p=*/false,
11388 /*template_arg_p=*/true,
11389 &idk);
11390 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
11391 || !cp_parser_next_token_ends_template_argument_p (parser))
11392 cp_parser_simulate_error (parser);
11393 if (cp_parser_parse_definitely (parser))
11394 return argument;
11397 /* If the next token is "&", the argument must be the address of an
11398 object or function with external linkage. */
11399 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
11400 if (address_p)
11401 cp_lexer_consume_token (parser->lexer);
11402 /* See if we might have an id-expression. */
11403 token = cp_lexer_peek_token (parser->lexer);
11404 if (token->type == CPP_NAME
11405 || token->keyword == RID_OPERATOR
11406 || token->type == CPP_SCOPE
11407 || token->type == CPP_TEMPLATE_ID
11408 || token->type == CPP_NESTED_NAME_SPECIFIER)
11410 cp_parser_parse_tentatively (parser);
11411 argument = cp_parser_primary_expression (parser,
11412 address_p,
11413 /*cast_p=*/false,
11414 /*template_arg_p=*/true,
11415 &idk);
11416 if (cp_parser_error_occurred (parser)
11417 || !cp_parser_next_token_ends_template_argument_p (parser))
11418 cp_parser_abort_tentative_parse (parser);
11419 else
11421 tree probe;
11423 if (TREE_CODE (argument) == INDIRECT_REF)
11425 gcc_assert (REFERENCE_REF_P (argument));
11426 argument = TREE_OPERAND (argument, 0);
11429 /* If we're in a template, we represent a qualified-id referring
11430 to a static data member as a SCOPE_REF even if the scope isn't
11431 dependent so that we can check access control later. */
11432 probe = argument;
11433 if (TREE_CODE (probe) == SCOPE_REF)
11434 probe = TREE_OPERAND (probe, 1);
11435 if (TREE_CODE (probe) == VAR_DECL)
11437 /* A variable without external linkage might still be a
11438 valid constant-expression, so no error is issued here
11439 if the external-linkage check fails. */
11440 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
11441 cp_parser_simulate_error (parser);
11443 else if (is_overloaded_fn (argument))
11444 /* All overloaded functions are allowed; if the external
11445 linkage test does not pass, an error will be issued
11446 later. */
11448 else if (address_p
11449 && (TREE_CODE (argument) == OFFSET_REF
11450 || TREE_CODE (argument) == SCOPE_REF))
11451 /* A pointer-to-member. */
11453 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
11455 else
11456 cp_parser_simulate_error (parser);
11458 if (cp_parser_parse_definitely (parser))
11460 if (address_p)
11461 argument = build_x_unary_op (ADDR_EXPR, argument,
11462 tf_warning_or_error);
11463 return argument;
11467 /* If the argument started with "&", there are no other valid
11468 alternatives at this point. */
11469 if (address_p)
11471 cp_parser_error (parser, "invalid non-type template argument");
11472 return error_mark_node;
11475 /* If the argument wasn't successfully parsed as a type-id followed
11476 by '>>', the argument can only be a constant expression now.
11477 Otherwise, we try parsing the constant-expression tentatively,
11478 because the argument could really be a type-id. */
11479 if (maybe_type_id)
11480 cp_parser_parse_tentatively (parser);
11481 argument = cp_parser_constant_expression (parser,
11482 /*allow_non_constant_p=*/false,
11483 /*non_constant_p=*/NULL);
11484 argument = fold_non_dependent_expr (argument);
11485 if (!maybe_type_id)
11486 return argument;
11487 if (!cp_parser_next_token_ends_template_argument_p (parser))
11488 cp_parser_error (parser, "expected template-argument");
11489 if (cp_parser_parse_definitely (parser))
11490 return argument;
11491 /* We did our best to parse the argument as a non type-id, but that
11492 was the only alternative that matched (albeit with a '>' after
11493 it). We can assume it's just a typo from the user, and a
11494 diagnostic will then be issued. */
11495 return cp_parser_template_type_arg (parser);
11498 /* Parse an explicit-instantiation.
11500 explicit-instantiation:
11501 template declaration
11503 Although the standard says `declaration', what it really means is:
11505 explicit-instantiation:
11506 template decl-specifier-seq [opt] declarator [opt] ;
11508 Things like `template int S<int>::i = 5, int S<double>::j;' are not
11509 supposed to be allowed. A defect report has been filed about this
11510 issue.
11512 GNU Extension:
11514 explicit-instantiation:
11515 storage-class-specifier template
11516 decl-specifier-seq [opt] declarator [opt] ;
11517 function-specifier template
11518 decl-specifier-seq [opt] declarator [opt] ; */
11520 static void
11521 cp_parser_explicit_instantiation (cp_parser* parser)
11523 int declares_class_or_enum;
11524 cp_decl_specifier_seq decl_specifiers;
11525 tree extension_specifier = NULL_TREE;
11527 /* Look for an (optional) storage-class-specifier or
11528 function-specifier. */
11529 if (cp_parser_allow_gnu_extensions_p (parser))
11531 extension_specifier
11532 = cp_parser_storage_class_specifier_opt (parser);
11533 if (!extension_specifier)
11534 extension_specifier
11535 = cp_parser_function_specifier_opt (parser,
11536 /*decl_specs=*/NULL);
11539 /* Look for the `template' keyword. */
11540 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
11541 /* Let the front end know that we are processing an explicit
11542 instantiation. */
11543 begin_explicit_instantiation ();
11544 /* [temp.explicit] says that we are supposed to ignore access
11545 control while processing explicit instantiation directives. */
11546 push_deferring_access_checks (dk_no_check);
11547 /* Parse a decl-specifier-seq. */
11548 cp_parser_decl_specifier_seq (parser,
11549 CP_PARSER_FLAGS_OPTIONAL,
11550 &decl_specifiers,
11551 &declares_class_or_enum);
11552 /* If there was exactly one decl-specifier, and it declared a class,
11553 and there's no declarator, then we have an explicit type
11554 instantiation. */
11555 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
11557 tree type;
11559 type = check_tag_decl (&decl_specifiers);
11560 /* Turn access control back on for names used during
11561 template instantiation. */
11562 pop_deferring_access_checks ();
11563 if (type)
11564 do_type_instantiation (type, extension_specifier,
11565 /*complain=*/tf_error);
11567 else
11569 cp_declarator *declarator;
11570 tree decl;
11572 /* Parse the declarator. */
11573 declarator
11574 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11575 /*ctor_dtor_or_conv_p=*/NULL,
11576 /*parenthesized_p=*/NULL,
11577 /*member_p=*/false);
11578 if (declares_class_or_enum & 2)
11579 cp_parser_check_for_definition_in_return_type (declarator,
11580 decl_specifiers.type,
11581 decl_specifiers.type_location);
11582 if (declarator != cp_error_declarator)
11584 decl = grokdeclarator (declarator, &decl_specifiers,
11585 NORMAL, 0, &decl_specifiers.attributes);
11586 /* Turn access control back on for names used during
11587 template instantiation. */
11588 pop_deferring_access_checks ();
11589 /* Do the explicit instantiation. */
11590 do_decl_instantiation (decl, extension_specifier);
11592 else
11594 pop_deferring_access_checks ();
11595 /* Skip the body of the explicit instantiation. */
11596 cp_parser_skip_to_end_of_statement (parser);
11599 /* We're done with the instantiation. */
11600 end_explicit_instantiation ();
11602 cp_parser_consume_semicolon_at_end_of_statement (parser);
11605 /* Parse an explicit-specialization.
11607 explicit-specialization:
11608 template < > declaration
11610 Although the standard says `declaration', what it really means is:
11612 explicit-specialization:
11613 template <> decl-specifier [opt] init-declarator [opt] ;
11614 template <> function-definition
11615 template <> explicit-specialization
11616 template <> template-declaration */
11618 static void
11619 cp_parser_explicit_specialization (cp_parser* parser)
11621 bool need_lang_pop;
11622 cp_token *token = cp_lexer_peek_token (parser->lexer);
11624 /* Look for the `template' keyword. */
11625 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
11626 /* Look for the `<'. */
11627 cp_parser_require (parser, CPP_LESS, "%<<%>");
11628 /* Look for the `>'. */
11629 cp_parser_require (parser, CPP_GREATER, "%<>%>");
11630 /* We have processed another parameter list. */
11631 ++parser->num_template_parameter_lists;
11632 /* [temp]
11634 A template ... explicit specialization ... shall not have C
11635 linkage. */
11636 if (current_lang_name == lang_name_c)
11638 error_at (token->location, "template specialization with C linkage");
11639 /* Give it C++ linkage to avoid confusing other parts of the
11640 front end. */
11641 push_lang_context (lang_name_cplusplus);
11642 need_lang_pop = true;
11644 else
11645 need_lang_pop = false;
11646 /* Let the front end know that we are beginning a specialization. */
11647 if (!begin_specialization ())
11649 end_specialization ();
11650 return;
11653 /* If the next keyword is `template', we need to figure out whether
11654 or not we're looking a template-declaration. */
11655 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
11657 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
11658 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
11659 cp_parser_template_declaration_after_export (parser,
11660 /*member_p=*/false);
11661 else
11662 cp_parser_explicit_specialization (parser);
11664 else
11665 /* Parse the dependent declaration. */
11666 cp_parser_single_declaration (parser,
11667 /*checks=*/NULL,
11668 /*member_p=*/false,
11669 /*explicit_specialization_p=*/true,
11670 /*friend_p=*/NULL);
11671 /* We're done with the specialization. */
11672 end_specialization ();
11673 /* For the erroneous case of a template with C linkage, we pushed an
11674 implicit C++ linkage scope; exit that scope now. */
11675 if (need_lang_pop)
11676 pop_lang_context ();
11677 /* We're done with this parameter list. */
11678 --parser->num_template_parameter_lists;
11681 /* Parse a type-specifier.
11683 type-specifier:
11684 simple-type-specifier
11685 class-specifier
11686 enum-specifier
11687 elaborated-type-specifier
11688 cv-qualifier
11690 GNU Extension:
11692 type-specifier:
11693 __complex__
11695 Returns a representation of the type-specifier. For a
11696 class-specifier, enum-specifier, or elaborated-type-specifier, a
11697 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
11699 The parser flags FLAGS is used to control type-specifier parsing.
11701 If IS_DECLARATION is TRUE, then this type-specifier is appearing
11702 in a decl-specifier-seq.
11704 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
11705 class-specifier, enum-specifier, or elaborated-type-specifier, then
11706 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
11707 if a type is declared; 2 if it is defined. Otherwise, it is set to
11708 zero.
11710 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
11711 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
11712 is set to FALSE. */
11714 static tree
11715 cp_parser_type_specifier (cp_parser* parser,
11716 cp_parser_flags flags,
11717 cp_decl_specifier_seq *decl_specs,
11718 bool is_declaration,
11719 int* declares_class_or_enum,
11720 bool* is_cv_qualifier)
11722 tree type_spec = NULL_TREE;
11723 cp_token *token;
11724 enum rid keyword;
11725 cp_decl_spec ds = ds_last;
11727 /* Assume this type-specifier does not declare a new type. */
11728 if (declares_class_or_enum)
11729 *declares_class_or_enum = 0;
11730 /* And that it does not specify a cv-qualifier. */
11731 if (is_cv_qualifier)
11732 *is_cv_qualifier = false;
11733 /* Peek at the next token. */
11734 token = cp_lexer_peek_token (parser->lexer);
11736 /* If we're looking at a keyword, we can use that to guide the
11737 production we choose. */
11738 keyword = token->keyword;
11739 switch (keyword)
11741 case RID_ENUM:
11742 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
11743 goto elaborated_type_specifier;
11745 /* Look for the enum-specifier. */
11746 type_spec = cp_parser_enum_specifier (parser);
11747 /* If that worked, we're done. */
11748 if (type_spec)
11750 if (declares_class_or_enum)
11751 *declares_class_or_enum = 2;
11752 if (decl_specs)
11753 cp_parser_set_decl_spec_type (decl_specs,
11754 type_spec,
11755 token->location,
11756 /*user_defined_p=*/true);
11757 return type_spec;
11759 else
11760 goto elaborated_type_specifier;
11762 /* Any of these indicate either a class-specifier, or an
11763 elaborated-type-specifier. */
11764 case RID_CLASS:
11765 case RID_STRUCT:
11766 case RID_UNION:
11767 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
11768 goto elaborated_type_specifier;
11770 /* Parse tentatively so that we can back up if we don't find a
11771 class-specifier. */
11772 cp_parser_parse_tentatively (parser);
11773 /* Look for the class-specifier. */
11774 type_spec = cp_parser_class_specifier (parser);
11775 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
11776 /* If that worked, we're done. */
11777 if (cp_parser_parse_definitely (parser))
11779 if (declares_class_or_enum)
11780 *declares_class_or_enum = 2;
11781 if (decl_specs)
11782 cp_parser_set_decl_spec_type (decl_specs,
11783 type_spec,
11784 token->location,
11785 /*user_defined_p=*/true);
11786 return type_spec;
11789 /* Fall through. */
11790 elaborated_type_specifier:
11791 /* We're declaring (not defining) a class or enum. */
11792 if (declares_class_or_enum)
11793 *declares_class_or_enum = 1;
11795 /* Fall through. */
11796 case RID_TYPENAME:
11797 /* Look for an elaborated-type-specifier. */
11798 type_spec
11799 = (cp_parser_elaborated_type_specifier
11800 (parser,
11801 decl_specs && decl_specs->specs[(int) ds_friend],
11802 is_declaration));
11803 if (decl_specs)
11804 cp_parser_set_decl_spec_type (decl_specs,
11805 type_spec,
11806 token->location,
11807 /*user_defined_p=*/true);
11808 return type_spec;
11810 case RID_CONST:
11811 ds = ds_const;
11812 if (is_cv_qualifier)
11813 *is_cv_qualifier = true;
11814 break;
11816 case RID_VOLATILE:
11817 ds = ds_volatile;
11818 if (is_cv_qualifier)
11819 *is_cv_qualifier = true;
11820 break;
11822 case RID_RESTRICT:
11823 ds = ds_restrict;
11824 if (is_cv_qualifier)
11825 *is_cv_qualifier = true;
11826 break;
11828 case RID_COMPLEX:
11829 /* The `__complex__' keyword is a GNU extension. */
11830 ds = ds_complex;
11831 break;
11833 default:
11834 break;
11837 /* Handle simple keywords. */
11838 if (ds != ds_last)
11840 if (decl_specs)
11842 ++decl_specs->specs[(int)ds];
11843 decl_specs->any_specifiers_p = true;
11845 return cp_lexer_consume_token (parser->lexer)->u.value;
11848 /* If we do not already have a type-specifier, assume we are looking
11849 at a simple-type-specifier. */
11850 type_spec = cp_parser_simple_type_specifier (parser,
11851 decl_specs,
11852 flags);
11854 /* If we didn't find a type-specifier, and a type-specifier was not
11855 optional in this context, issue an error message. */
11856 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11858 cp_parser_error (parser, "expected type specifier");
11859 return error_mark_node;
11862 return type_spec;
11865 /* Parse a simple-type-specifier.
11867 simple-type-specifier:
11868 :: [opt] nested-name-specifier [opt] type-name
11869 :: [opt] nested-name-specifier template template-id
11870 char
11871 wchar_t
11872 bool
11873 short
11875 long
11876 signed
11877 unsigned
11878 float
11879 double
11880 void
11882 C++0x Extension:
11884 simple-type-specifier:
11885 auto
11886 decltype ( expression )
11887 char16_t
11888 char32_t
11890 GNU Extension:
11892 simple-type-specifier:
11893 __typeof__ unary-expression
11894 __typeof__ ( type-id )
11896 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
11897 appropriately updated. */
11899 static tree
11900 cp_parser_simple_type_specifier (cp_parser* parser,
11901 cp_decl_specifier_seq *decl_specs,
11902 cp_parser_flags flags)
11904 tree type = NULL_TREE;
11905 cp_token *token;
11907 /* Peek at the next token. */
11908 token = cp_lexer_peek_token (parser->lexer);
11910 /* If we're looking at a keyword, things are easy. */
11911 switch (token->keyword)
11913 case RID_CHAR:
11914 if (decl_specs)
11915 decl_specs->explicit_char_p = true;
11916 type = char_type_node;
11917 break;
11918 case RID_CHAR16:
11919 type = char16_type_node;
11920 break;
11921 case RID_CHAR32:
11922 type = char32_type_node;
11923 break;
11924 case RID_WCHAR:
11925 type = wchar_type_node;
11926 break;
11927 case RID_BOOL:
11928 type = boolean_type_node;
11929 break;
11930 case RID_SHORT:
11931 if (decl_specs)
11932 ++decl_specs->specs[(int) ds_short];
11933 type = short_integer_type_node;
11934 break;
11935 case RID_INT:
11936 if (decl_specs)
11937 decl_specs->explicit_int_p = true;
11938 type = integer_type_node;
11939 break;
11940 case RID_LONG:
11941 if (decl_specs)
11942 ++decl_specs->specs[(int) ds_long];
11943 type = long_integer_type_node;
11944 break;
11945 case RID_SIGNED:
11946 if (decl_specs)
11947 ++decl_specs->specs[(int) ds_signed];
11948 type = integer_type_node;
11949 break;
11950 case RID_UNSIGNED:
11951 if (decl_specs)
11952 ++decl_specs->specs[(int) ds_unsigned];
11953 type = unsigned_type_node;
11954 break;
11955 case RID_FLOAT:
11956 type = float_type_node;
11957 break;
11958 case RID_DOUBLE:
11959 type = double_type_node;
11960 break;
11961 case RID_VOID:
11962 type = void_type_node;
11963 break;
11965 case RID_AUTO:
11966 maybe_warn_cpp0x (CPP0X_AUTO);
11967 type = make_auto ();
11968 break;
11970 case RID_DECLTYPE:
11971 /* Parse the `decltype' type. */
11972 type = cp_parser_decltype (parser);
11974 if (decl_specs)
11975 cp_parser_set_decl_spec_type (decl_specs, type,
11976 token->location,
11977 /*user_defined_p=*/true);
11979 return type;
11981 case RID_TYPEOF:
11982 /* Consume the `typeof' token. */
11983 cp_lexer_consume_token (parser->lexer);
11984 /* Parse the operand to `typeof'. */
11985 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11986 /* If it is not already a TYPE, take its type. */
11987 if (!TYPE_P (type))
11988 type = finish_typeof (type);
11990 if (decl_specs)
11991 cp_parser_set_decl_spec_type (decl_specs, type,
11992 token->location,
11993 /*user_defined_p=*/true);
11995 return type;
11997 default:
11998 break;
12001 /* If the type-specifier was for a built-in type, we're done. */
12002 if (type)
12004 /* Record the type. */
12005 if (decl_specs
12006 && (token->keyword != RID_SIGNED
12007 && token->keyword != RID_UNSIGNED
12008 && token->keyword != RID_SHORT
12009 && token->keyword != RID_LONG))
12010 cp_parser_set_decl_spec_type (decl_specs,
12011 type,
12012 token->location,
12013 /*user_defined=*/false);
12014 if (decl_specs)
12015 decl_specs->any_specifiers_p = true;
12017 /* Consume the token. */
12018 cp_lexer_consume_token (parser->lexer);
12020 /* There is no valid C++ program where a non-template type is
12021 followed by a "<". That usually indicates that the user thought
12022 that the type was a template. */
12023 cp_parser_check_for_invalid_template_id (parser, type, token->location);
12025 return TYPE_NAME (type);
12028 /* The type-specifier must be a user-defined type. */
12029 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
12031 bool qualified_p;
12032 bool global_p;
12034 /* Don't gobble tokens or issue error messages if this is an
12035 optional type-specifier. */
12036 if (flags & CP_PARSER_FLAGS_OPTIONAL)
12037 cp_parser_parse_tentatively (parser);
12039 /* Look for the optional `::' operator. */
12040 global_p
12041 = (cp_parser_global_scope_opt (parser,
12042 /*current_scope_valid_p=*/false)
12043 != NULL_TREE);
12044 /* Look for the nested-name specifier. */
12045 qualified_p
12046 = (cp_parser_nested_name_specifier_opt (parser,
12047 /*typename_keyword_p=*/false,
12048 /*check_dependency_p=*/true,
12049 /*type_p=*/false,
12050 /*is_declaration=*/false)
12051 != NULL_TREE);
12052 token = cp_lexer_peek_token (parser->lexer);
12053 /* If we have seen a nested-name-specifier, and the next token
12054 is `template', then we are using the template-id production. */
12055 if (parser->scope
12056 && cp_parser_optional_template_keyword (parser))
12058 /* Look for the template-id. */
12059 type = cp_parser_template_id (parser,
12060 /*template_keyword_p=*/true,
12061 /*check_dependency_p=*/true,
12062 /*is_declaration=*/false);
12063 /* If the template-id did not name a type, we are out of
12064 luck. */
12065 if (TREE_CODE (type) != TYPE_DECL)
12067 cp_parser_error (parser, "expected template-id for type");
12068 type = NULL_TREE;
12071 /* Otherwise, look for a type-name. */
12072 else
12073 type = cp_parser_type_name (parser);
12074 /* Keep track of all name-lookups performed in class scopes. */
12075 if (type
12076 && !global_p
12077 && !qualified_p
12078 && TREE_CODE (type) == TYPE_DECL
12079 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
12080 maybe_note_name_used_in_class (DECL_NAME (type), type);
12081 /* If it didn't work out, we don't have a TYPE. */
12082 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
12083 && !cp_parser_parse_definitely (parser))
12084 type = NULL_TREE;
12085 if (type && decl_specs)
12086 cp_parser_set_decl_spec_type (decl_specs, type,
12087 token->location,
12088 /*user_defined=*/true);
12091 /* If we didn't get a type-name, issue an error message. */
12092 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12094 cp_parser_error (parser, "expected type-name");
12095 return error_mark_node;
12098 /* There is no valid C++ program where a non-template type is
12099 followed by a "<". That usually indicates that the user thought
12100 that the type was a template. */
12101 if (type && type != error_mark_node)
12103 /* As a last-ditch effort, see if TYPE is an Objective-C type.
12104 If it is, then the '<'...'>' enclose protocol names rather than
12105 template arguments, and so everything is fine. */
12106 if (c_dialect_objc ()
12107 && (objc_is_id (type) || objc_is_class_name (type)))
12109 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12110 tree qual_type = objc_get_protocol_qualified_type (type, protos);
12112 /* Clobber the "unqualified" type previously entered into
12113 DECL_SPECS with the new, improved protocol-qualified version. */
12114 if (decl_specs)
12115 decl_specs->type = qual_type;
12117 return qual_type;
12120 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12121 token->location);
12124 return type;
12127 /* Parse a type-name.
12129 type-name:
12130 class-name
12131 enum-name
12132 typedef-name
12134 enum-name:
12135 identifier
12137 typedef-name:
12138 identifier
12140 Returns a TYPE_DECL for the type. */
12142 static tree
12143 cp_parser_type_name (cp_parser* parser)
12145 tree type_decl;
12147 /* We can't know yet whether it is a class-name or not. */
12148 cp_parser_parse_tentatively (parser);
12149 /* Try a class-name. */
12150 type_decl = cp_parser_class_name (parser,
12151 /*typename_keyword_p=*/false,
12152 /*template_keyword_p=*/false,
12153 none_type,
12154 /*check_dependency_p=*/true,
12155 /*class_head_p=*/false,
12156 /*is_declaration=*/false);
12157 /* If it's not a class-name, keep looking. */
12158 if (!cp_parser_parse_definitely (parser))
12160 /* It must be a typedef-name or an enum-name. */
12161 return cp_parser_nonclass_name (parser);
12164 return type_decl;
12167 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12169 enum-name:
12170 identifier
12172 typedef-name:
12173 identifier
12175 Returns a TYPE_DECL for the type. */
12177 static tree
12178 cp_parser_nonclass_name (cp_parser* parser)
12180 tree type_decl;
12181 tree identifier;
12183 cp_token *token = cp_lexer_peek_token (parser->lexer);
12184 identifier = cp_parser_identifier (parser);
12185 if (identifier == error_mark_node)
12186 return error_mark_node;
12188 /* Look up the type-name. */
12189 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
12191 if (TREE_CODE (type_decl) != TYPE_DECL
12192 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
12194 /* See if this is an Objective-C type. */
12195 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12196 tree type = objc_get_protocol_qualified_type (identifier, protos);
12197 if (type)
12198 type_decl = TYPE_NAME (type);
12201 /* Issue an error if we did not find a type-name. */
12202 if (TREE_CODE (type_decl) != TYPE_DECL)
12204 if (!cp_parser_simulate_error (parser))
12205 cp_parser_name_lookup_error (parser, identifier, type_decl,
12206 "is not a type", token->location);
12207 return error_mark_node;
12209 /* Remember that the name was used in the definition of the
12210 current class so that we can check later to see if the
12211 meaning would have been different after the class was
12212 entirely defined. */
12213 else if (type_decl != error_mark_node
12214 && !parser->scope)
12215 maybe_note_name_used_in_class (identifier, type_decl);
12217 return type_decl;
12220 /* Parse an elaborated-type-specifier. Note that the grammar given
12221 here incorporates the resolution to DR68.
12223 elaborated-type-specifier:
12224 class-key :: [opt] nested-name-specifier [opt] identifier
12225 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
12226 enum-key :: [opt] nested-name-specifier [opt] identifier
12227 typename :: [opt] nested-name-specifier identifier
12228 typename :: [opt] nested-name-specifier template [opt]
12229 template-id
12231 GNU extension:
12233 elaborated-type-specifier:
12234 class-key attributes :: [opt] nested-name-specifier [opt] identifier
12235 class-key attributes :: [opt] nested-name-specifier [opt]
12236 template [opt] template-id
12237 enum attributes :: [opt] nested-name-specifier [opt] identifier
12239 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
12240 declared `friend'. If IS_DECLARATION is TRUE, then this
12241 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
12242 something is being declared.
12244 Returns the TYPE specified. */
12246 static tree
12247 cp_parser_elaborated_type_specifier (cp_parser* parser,
12248 bool is_friend,
12249 bool is_declaration)
12251 enum tag_types tag_type;
12252 tree identifier;
12253 tree type = NULL_TREE;
12254 tree attributes = NULL_TREE;
12255 tree globalscope;
12256 cp_token *token = NULL;
12258 /* See if we're looking at the `enum' keyword. */
12259 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
12261 /* Consume the `enum' token. */
12262 cp_lexer_consume_token (parser->lexer);
12263 /* Remember that it's an enumeration type. */
12264 tag_type = enum_type;
12265 /* Parse the optional `struct' or `class' key (for C++0x scoped
12266 enums). */
12267 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12268 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12270 if (cxx_dialect == cxx98)
12271 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
12273 /* Consume the `struct' or `class'. */
12274 cp_lexer_consume_token (parser->lexer);
12276 /* Parse the attributes. */
12277 attributes = cp_parser_attributes_opt (parser);
12279 /* Or, it might be `typename'. */
12280 else if (cp_lexer_next_token_is_keyword (parser->lexer,
12281 RID_TYPENAME))
12283 /* Consume the `typename' token. */
12284 cp_lexer_consume_token (parser->lexer);
12285 /* Remember that it's a `typename' type. */
12286 tag_type = typename_type;
12288 /* Otherwise it must be a class-key. */
12289 else
12291 tag_type = cp_parser_class_key (parser);
12292 if (tag_type == none_type)
12293 return error_mark_node;
12294 /* Parse the attributes. */
12295 attributes = cp_parser_attributes_opt (parser);
12298 /* Look for the `::' operator. */
12299 globalscope = cp_parser_global_scope_opt (parser,
12300 /*current_scope_valid_p=*/false);
12301 /* Look for the nested-name-specifier. */
12302 if (tag_type == typename_type && !globalscope)
12304 if (!cp_parser_nested_name_specifier (parser,
12305 /*typename_keyword_p=*/true,
12306 /*check_dependency_p=*/true,
12307 /*type_p=*/true,
12308 is_declaration))
12309 return error_mark_node;
12311 else
12312 /* Even though `typename' is not present, the proposed resolution
12313 to Core Issue 180 says that in `class A<T>::B', `B' should be
12314 considered a type-name, even if `A<T>' is dependent. */
12315 cp_parser_nested_name_specifier_opt (parser,
12316 /*typename_keyword_p=*/true,
12317 /*check_dependency_p=*/true,
12318 /*type_p=*/true,
12319 is_declaration);
12320 /* For everything but enumeration types, consider a template-id.
12321 For an enumeration type, consider only a plain identifier. */
12322 if (tag_type != enum_type)
12324 bool template_p = false;
12325 tree decl;
12327 /* Allow the `template' keyword. */
12328 template_p = cp_parser_optional_template_keyword (parser);
12329 /* If we didn't see `template', we don't know if there's a
12330 template-id or not. */
12331 if (!template_p)
12332 cp_parser_parse_tentatively (parser);
12333 /* Parse the template-id. */
12334 token = cp_lexer_peek_token (parser->lexer);
12335 decl = cp_parser_template_id (parser, template_p,
12336 /*check_dependency_p=*/true,
12337 is_declaration);
12338 /* If we didn't find a template-id, look for an ordinary
12339 identifier. */
12340 if (!template_p && !cp_parser_parse_definitely (parser))
12342 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
12343 in effect, then we must assume that, upon instantiation, the
12344 template will correspond to a class. */
12345 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12346 && tag_type == typename_type)
12347 type = make_typename_type (parser->scope, decl,
12348 typename_type,
12349 /*complain=*/tf_error);
12350 /* If the `typename' keyword is in effect and DECL is not a type
12351 decl. Then type is non existant. */
12352 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
12353 type = NULL_TREE;
12354 else
12355 type = TREE_TYPE (decl);
12358 if (!type)
12360 token = cp_lexer_peek_token (parser->lexer);
12361 identifier = cp_parser_identifier (parser);
12363 if (identifier == error_mark_node)
12365 parser->scope = NULL_TREE;
12366 return error_mark_node;
12369 /* For a `typename', we needn't call xref_tag. */
12370 if (tag_type == typename_type
12371 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
12372 return cp_parser_make_typename_type (parser, parser->scope,
12373 identifier,
12374 token->location);
12375 /* Look up a qualified name in the usual way. */
12376 if (parser->scope)
12378 tree decl;
12379 tree ambiguous_decls;
12381 decl = cp_parser_lookup_name (parser, identifier,
12382 tag_type,
12383 /*is_template=*/false,
12384 /*is_namespace=*/false,
12385 /*check_dependency=*/true,
12386 &ambiguous_decls,
12387 token->location);
12389 /* If the lookup was ambiguous, an error will already have been
12390 issued. */
12391 if (ambiguous_decls)
12392 return error_mark_node;
12394 /* If we are parsing friend declaration, DECL may be a
12395 TEMPLATE_DECL tree node here. However, we need to check
12396 whether this TEMPLATE_DECL results in valid code. Consider
12397 the following example:
12399 namespace N {
12400 template <class T> class C {};
12402 class X {
12403 template <class T> friend class N::C; // #1, valid code
12405 template <class T> class Y {
12406 friend class N::C; // #2, invalid code
12409 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
12410 name lookup of `N::C'. We see that friend declaration must
12411 be template for the code to be valid. Note that
12412 processing_template_decl does not work here since it is
12413 always 1 for the above two cases. */
12415 decl = (cp_parser_maybe_treat_template_as_class
12416 (decl, /*tag_name_p=*/is_friend
12417 && parser->num_template_parameter_lists));
12419 if (TREE_CODE (decl) != TYPE_DECL)
12421 cp_parser_diagnose_invalid_type_name (parser,
12422 parser->scope,
12423 identifier,
12424 token->location);
12425 return error_mark_node;
12428 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
12430 bool allow_template = (parser->num_template_parameter_lists
12431 || DECL_SELF_REFERENCE_P (decl));
12432 type = check_elaborated_type_specifier (tag_type, decl,
12433 allow_template);
12435 if (type == error_mark_node)
12436 return error_mark_node;
12439 /* Forward declarations of nested types, such as
12441 class C1::C2;
12442 class C1::C2::C3;
12444 are invalid unless all components preceding the final '::'
12445 are complete. If all enclosing types are complete, these
12446 declarations become merely pointless.
12448 Invalid forward declarations of nested types are errors
12449 caught elsewhere in parsing. Those that are pointless arrive
12450 here. */
12452 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12453 && !is_friend && !processing_explicit_instantiation)
12454 warning (0, "declaration %qD does not declare anything", decl);
12456 type = TREE_TYPE (decl);
12458 else
12460 /* An elaborated-type-specifier sometimes introduces a new type and
12461 sometimes names an existing type. Normally, the rule is that it
12462 introduces a new type only if there is not an existing type of
12463 the same name already in scope. For example, given:
12465 struct S {};
12466 void f() { struct S s; }
12468 the `struct S' in the body of `f' is the same `struct S' as in
12469 the global scope; the existing definition is used. However, if
12470 there were no global declaration, this would introduce a new
12471 local class named `S'.
12473 An exception to this rule applies to the following code:
12475 namespace N { struct S; }
12477 Here, the elaborated-type-specifier names a new type
12478 unconditionally; even if there is already an `S' in the
12479 containing scope this declaration names a new type.
12480 This exception only applies if the elaborated-type-specifier
12481 forms the complete declaration:
12483 [class.name]
12485 A declaration consisting solely of `class-key identifier ;' is
12486 either a redeclaration of the name in the current scope or a
12487 forward declaration of the identifier as a class name. It
12488 introduces the name into the current scope.
12490 We are in this situation precisely when the next token is a `;'.
12492 An exception to the exception is that a `friend' declaration does
12493 *not* name a new type; i.e., given:
12495 struct S { friend struct T; };
12497 `T' is not a new type in the scope of `S'.
12499 Also, `new struct S' or `sizeof (struct S)' never results in the
12500 definition of a new type; a new type can only be declared in a
12501 declaration context. */
12503 tag_scope ts;
12504 bool template_p;
12506 if (is_friend)
12507 /* Friends have special name lookup rules. */
12508 ts = ts_within_enclosing_non_class;
12509 else if (is_declaration
12510 && cp_lexer_next_token_is (parser->lexer,
12511 CPP_SEMICOLON))
12512 /* This is a `class-key identifier ;' */
12513 ts = ts_current;
12514 else
12515 ts = ts_global;
12517 template_p =
12518 (parser->num_template_parameter_lists
12519 && (cp_parser_next_token_starts_class_definition_p (parser)
12520 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
12521 /* An unqualified name was used to reference this type, so
12522 there were no qualifying templates. */
12523 if (!cp_parser_check_template_parameters (parser,
12524 /*num_templates=*/0,
12525 token->location,
12526 /*declarator=*/NULL))
12527 return error_mark_node;
12528 type = xref_tag (tag_type, identifier, ts, template_p);
12532 if (type == error_mark_node)
12533 return error_mark_node;
12535 /* Allow attributes on forward declarations of classes. */
12536 if (attributes)
12538 if (TREE_CODE (type) == TYPENAME_TYPE)
12539 warning (OPT_Wattributes,
12540 "attributes ignored on uninstantiated type");
12541 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
12542 && ! processing_explicit_instantiation)
12543 warning (OPT_Wattributes,
12544 "attributes ignored on template instantiation");
12545 else if (is_declaration && cp_parser_declares_only_class_p (parser))
12546 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
12547 else
12548 warning (OPT_Wattributes,
12549 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
12552 if (tag_type != enum_type)
12553 cp_parser_check_class_key (tag_type, type);
12555 /* A "<" cannot follow an elaborated type specifier. If that
12556 happens, the user was probably trying to form a template-id. */
12557 cp_parser_check_for_invalid_template_id (parser, type, token->location);
12559 return type;
12562 /* Parse an enum-specifier.
12564 enum-specifier:
12565 enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
12567 enum-key:
12568 enum
12569 enum class [C++0x]
12570 enum struct [C++0x]
12572 enum-base: [C++0x]
12573 : type-specifier-seq
12575 GNU Extensions:
12576 enum-key attributes[opt] identifier [opt] enum-base [opt]
12577 { enumerator-list [opt] }attributes[opt]
12579 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
12580 if the token stream isn't an enum-specifier after all. */
12582 static tree
12583 cp_parser_enum_specifier (cp_parser* parser)
12585 tree identifier;
12586 tree type;
12587 tree attributes;
12588 bool scoped_enum_p = false;
12589 bool has_underlying_type = false;
12590 tree underlying_type = NULL_TREE;
12592 /* Parse tentatively so that we can back up if we don't find a
12593 enum-specifier. */
12594 cp_parser_parse_tentatively (parser);
12596 /* Caller guarantees that the current token is 'enum', an identifier
12597 possibly follows, and the token after that is an opening brace.
12598 If we don't have an identifier, fabricate an anonymous name for
12599 the enumeration being defined. */
12600 cp_lexer_consume_token (parser->lexer);
12602 /* Parse the "class" or "struct", which indicates a scoped
12603 enumeration type in C++0x. */
12604 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12605 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12607 if (cxx_dialect == cxx98)
12608 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
12610 /* Consume the `struct' or `class' token. */
12611 cp_lexer_consume_token (parser->lexer);
12613 scoped_enum_p = true;
12616 attributes = cp_parser_attributes_opt (parser);
12618 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12619 identifier = cp_parser_identifier (parser);
12620 else
12621 identifier = make_anon_name ();
12623 /* Check for the `:' that denotes a specified underlying type in C++0x.
12624 Note that a ':' could also indicate a bitfield width, however. */
12625 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12627 cp_decl_specifier_seq type_specifiers;
12629 /* Consume the `:'. */
12630 cp_lexer_consume_token (parser->lexer);
12632 /* Parse the type-specifier-seq. */
12633 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12634 /*is_trailing_return=*/false,
12635 &type_specifiers);
12637 /* At this point this is surely not elaborated type specifier. */
12638 if (!cp_parser_parse_definitely (parser))
12639 return NULL_TREE;
12641 if (cxx_dialect == cxx98)
12642 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
12644 has_underlying_type = true;
12646 /* If that didn't work, stop. */
12647 if (type_specifiers.type != error_mark_node)
12649 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
12650 /*initialized=*/0, NULL);
12651 if (underlying_type == error_mark_node)
12652 underlying_type = NULL_TREE;
12656 /* Look for the `{' but don't consume it yet. */
12657 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12659 cp_parser_error (parser, "expected %<{%>");
12660 if (has_underlying_type)
12661 return NULL_TREE;
12664 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
12665 return NULL_TREE;
12667 /* Issue an error message if type-definitions are forbidden here. */
12668 if (!cp_parser_check_type_definition (parser))
12669 type = error_mark_node;
12670 else
12671 /* Create the new type. We do this before consuming the opening
12672 brace so the enum will be recorded as being on the line of its
12673 tag (or the 'enum' keyword, if there is no tag). */
12674 type = start_enum (identifier, underlying_type, scoped_enum_p);
12676 /* Consume the opening brace. */
12677 cp_lexer_consume_token (parser->lexer);
12679 if (type == error_mark_node)
12681 cp_parser_skip_to_end_of_block_or_statement (parser);
12682 return error_mark_node;
12685 /* If the next token is not '}', then there are some enumerators. */
12686 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12687 cp_parser_enumerator_list (parser, type);
12689 /* Consume the final '}'. */
12690 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12692 /* Look for trailing attributes to apply to this enumeration, and
12693 apply them if appropriate. */
12694 if (cp_parser_allow_gnu_extensions_p (parser))
12696 tree trailing_attr = cp_parser_attributes_opt (parser);
12697 trailing_attr = chainon (trailing_attr, attributes);
12698 cplus_decl_attributes (&type,
12699 trailing_attr,
12700 (int) ATTR_FLAG_TYPE_IN_PLACE);
12703 /* Finish up the enumeration. */
12704 finish_enum (type);
12706 return type;
12709 /* Parse an enumerator-list. The enumerators all have the indicated
12710 TYPE.
12712 enumerator-list:
12713 enumerator-definition
12714 enumerator-list , enumerator-definition */
12716 static void
12717 cp_parser_enumerator_list (cp_parser* parser, tree type)
12719 while (true)
12721 /* Parse an enumerator-definition. */
12722 cp_parser_enumerator_definition (parser, type);
12724 /* If the next token is not a ',', we've reached the end of
12725 the list. */
12726 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12727 break;
12728 /* Otherwise, consume the `,' and keep going. */
12729 cp_lexer_consume_token (parser->lexer);
12730 /* If the next token is a `}', there is a trailing comma. */
12731 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
12733 if (!in_system_header)
12734 pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
12735 break;
12740 /* Parse an enumerator-definition. The enumerator has the indicated
12741 TYPE.
12743 enumerator-definition:
12744 enumerator
12745 enumerator = constant-expression
12747 enumerator:
12748 identifier */
12750 static void
12751 cp_parser_enumerator_definition (cp_parser* parser, tree type)
12753 tree identifier;
12754 tree value;
12756 /* Look for the identifier. */
12757 identifier = cp_parser_identifier (parser);
12758 if (identifier == error_mark_node)
12759 return;
12761 /* If the next token is an '=', then there is an explicit value. */
12762 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12764 /* Consume the `=' token. */
12765 cp_lexer_consume_token (parser->lexer);
12766 /* Parse the value. */
12767 value = cp_parser_constant_expression (parser,
12768 /*allow_non_constant_p=*/false,
12769 NULL);
12771 else
12772 value = NULL_TREE;
12774 /* If we are processing a template, make sure the initializer of the
12775 enumerator doesn't contain any bare template parameter pack. */
12776 if (check_for_bare_parameter_packs (value))
12777 value = error_mark_node;
12779 /* Create the enumerator. */
12780 build_enumerator (identifier, value, type);
12783 /* Parse a namespace-name.
12785 namespace-name:
12786 original-namespace-name
12787 namespace-alias
12789 Returns the NAMESPACE_DECL for the namespace. */
12791 static tree
12792 cp_parser_namespace_name (cp_parser* parser)
12794 tree identifier;
12795 tree namespace_decl;
12797 cp_token *token = cp_lexer_peek_token (parser->lexer);
12799 /* Get the name of the namespace. */
12800 identifier = cp_parser_identifier (parser);
12801 if (identifier == error_mark_node)
12802 return error_mark_node;
12804 /* Look up the identifier in the currently active scope. Look only
12805 for namespaces, due to:
12807 [basic.lookup.udir]
12809 When looking up a namespace-name in a using-directive or alias
12810 definition, only namespace names are considered.
12812 And:
12814 [basic.lookup.qual]
12816 During the lookup of a name preceding the :: scope resolution
12817 operator, object, function, and enumerator names are ignored.
12819 (Note that cp_parser_qualifying_entity only calls this
12820 function if the token after the name is the scope resolution
12821 operator.) */
12822 namespace_decl = cp_parser_lookup_name (parser, identifier,
12823 none_type,
12824 /*is_template=*/false,
12825 /*is_namespace=*/true,
12826 /*check_dependency=*/true,
12827 /*ambiguous_decls=*/NULL,
12828 token->location);
12829 /* If it's not a namespace, issue an error. */
12830 if (namespace_decl == error_mark_node
12831 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
12833 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12834 error_at (token->location, "%qD is not a namespace-name", identifier);
12835 cp_parser_error (parser, "expected namespace-name");
12836 namespace_decl = error_mark_node;
12839 return namespace_decl;
12842 /* Parse a namespace-definition.
12844 namespace-definition:
12845 named-namespace-definition
12846 unnamed-namespace-definition
12848 named-namespace-definition:
12849 original-namespace-definition
12850 extension-namespace-definition
12852 original-namespace-definition:
12853 namespace identifier { namespace-body }
12855 extension-namespace-definition:
12856 namespace original-namespace-name { namespace-body }
12858 unnamed-namespace-definition:
12859 namespace { namespace-body } */
12861 static void
12862 cp_parser_namespace_definition (cp_parser* parser)
12864 tree identifier, attribs;
12865 bool has_visibility;
12866 bool is_inline;
12868 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
12870 is_inline = true;
12871 cp_lexer_consume_token (parser->lexer);
12873 else
12874 is_inline = false;
12876 /* Look for the `namespace' keyword. */
12877 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12879 /* Get the name of the namespace. We do not attempt to distinguish
12880 between an original-namespace-definition and an
12881 extension-namespace-definition at this point. The semantic
12882 analysis routines are responsible for that. */
12883 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12884 identifier = cp_parser_identifier (parser);
12885 else
12886 identifier = NULL_TREE;
12888 /* Parse any specified attributes. */
12889 attribs = cp_parser_attributes_opt (parser);
12891 /* Look for the `{' to start the namespace. */
12892 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
12893 /* Start the namespace. */
12894 push_namespace (identifier);
12896 /* "inline namespace" is equivalent to a stub namespace definition
12897 followed by a strong using directive. */
12898 if (is_inline)
12900 tree name_space = current_namespace;
12901 /* Set up namespace association. */
12902 DECL_NAMESPACE_ASSOCIATIONS (name_space)
12903 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
12904 DECL_NAMESPACE_ASSOCIATIONS (name_space));
12905 /* Import the contents of the inline namespace. */
12906 pop_namespace ();
12907 do_using_directive (name_space);
12908 push_namespace (identifier);
12911 has_visibility = handle_namespace_attrs (current_namespace, attribs);
12913 /* Parse the body of the namespace. */
12914 cp_parser_namespace_body (parser);
12916 #ifdef HANDLE_PRAGMA_VISIBILITY
12917 if (has_visibility)
12918 pop_visibility (1);
12919 #endif
12921 /* Finish the namespace. */
12922 pop_namespace ();
12923 /* Look for the final `}'. */
12924 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12927 /* Parse a namespace-body.
12929 namespace-body:
12930 declaration-seq [opt] */
12932 static void
12933 cp_parser_namespace_body (cp_parser* parser)
12935 cp_parser_declaration_seq_opt (parser);
12938 /* Parse a namespace-alias-definition.
12940 namespace-alias-definition:
12941 namespace identifier = qualified-namespace-specifier ; */
12943 static void
12944 cp_parser_namespace_alias_definition (cp_parser* parser)
12946 tree identifier;
12947 tree namespace_specifier;
12949 cp_token *token = cp_lexer_peek_token (parser->lexer);
12951 /* Look for the `namespace' keyword. */
12952 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12953 /* Look for the identifier. */
12954 identifier = cp_parser_identifier (parser);
12955 if (identifier == error_mark_node)
12956 return;
12957 /* Look for the `=' token. */
12958 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12959 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12961 error_at (token->location, "%<namespace%> definition is not allowed here");
12962 /* Skip the definition. */
12963 cp_lexer_consume_token (parser->lexer);
12964 if (cp_parser_skip_to_closing_brace (parser))
12965 cp_lexer_consume_token (parser->lexer);
12966 return;
12968 cp_parser_require (parser, CPP_EQ, "%<=%>");
12969 /* Look for the qualified-namespace-specifier. */
12970 namespace_specifier
12971 = cp_parser_qualified_namespace_specifier (parser);
12972 /* Look for the `;' token. */
12973 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12975 /* Register the alias in the symbol table. */
12976 do_namespace_alias (identifier, namespace_specifier);
12979 /* Parse a qualified-namespace-specifier.
12981 qualified-namespace-specifier:
12982 :: [opt] nested-name-specifier [opt] namespace-name
12984 Returns a NAMESPACE_DECL corresponding to the specified
12985 namespace. */
12987 static tree
12988 cp_parser_qualified_namespace_specifier (cp_parser* parser)
12990 /* Look for the optional `::'. */
12991 cp_parser_global_scope_opt (parser,
12992 /*current_scope_valid_p=*/false);
12994 /* Look for the optional nested-name-specifier. */
12995 cp_parser_nested_name_specifier_opt (parser,
12996 /*typename_keyword_p=*/false,
12997 /*check_dependency_p=*/true,
12998 /*type_p=*/false,
12999 /*is_declaration=*/true);
13001 return cp_parser_namespace_name (parser);
13004 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
13005 access declaration.
13007 using-declaration:
13008 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
13009 using :: unqualified-id ;
13011 access-declaration:
13012 qualified-id ;
13016 static bool
13017 cp_parser_using_declaration (cp_parser* parser,
13018 bool access_declaration_p)
13020 cp_token *token;
13021 bool typename_p = false;
13022 bool global_scope_p;
13023 tree decl;
13024 tree identifier;
13025 tree qscope;
13027 if (access_declaration_p)
13028 cp_parser_parse_tentatively (parser);
13029 else
13031 /* Look for the `using' keyword. */
13032 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
13034 /* Peek at the next token. */
13035 token = cp_lexer_peek_token (parser->lexer);
13036 /* See if it's `typename'. */
13037 if (token->keyword == RID_TYPENAME)
13039 /* Remember that we've seen it. */
13040 typename_p = true;
13041 /* Consume the `typename' token. */
13042 cp_lexer_consume_token (parser->lexer);
13046 /* Look for the optional global scope qualification. */
13047 global_scope_p
13048 = (cp_parser_global_scope_opt (parser,
13049 /*current_scope_valid_p=*/false)
13050 != NULL_TREE);
13052 /* If we saw `typename', or didn't see `::', then there must be a
13053 nested-name-specifier present. */
13054 if (typename_p || !global_scope_p)
13055 qscope = cp_parser_nested_name_specifier (parser, typename_p,
13056 /*check_dependency_p=*/true,
13057 /*type_p=*/false,
13058 /*is_declaration=*/true);
13059 /* Otherwise, we could be in either of the two productions. In that
13060 case, treat the nested-name-specifier as optional. */
13061 else
13062 qscope = cp_parser_nested_name_specifier_opt (parser,
13063 /*typename_keyword_p=*/false,
13064 /*check_dependency_p=*/true,
13065 /*type_p=*/false,
13066 /*is_declaration=*/true);
13067 if (!qscope)
13068 qscope = global_namespace;
13070 if (access_declaration_p && cp_parser_error_occurred (parser))
13071 /* Something has already gone wrong; there's no need to parse
13072 further. Since an error has occurred, the return value of
13073 cp_parser_parse_definitely will be false, as required. */
13074 return cp_parser_parse_definitely (parser);
13076 token = cp_lexer_peek_token (parser->lexer);
13077 /* Parse the unqualified-id. */
13078 identifier = cp_parser_unqualified_id (parser,
13079 /*template_keyword_p=*/false,
13080 /*check_dependency_p=*/true,
13081 /*declarator_p=*/true,
13082 /*optional_p=*/false);
13084 if (access_declaration_p)
13086 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13087 cp_parser_simulate_error (parser);
13088 if (!cp_parser_parse_definitely (parser))
13089 return false;
13092 /* The function we call to handle a using-declaration is different
13093 depending on what scope we are in. */
13094 if (qscope == error_mark_node || identifier == error_mark_node)
13096 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
13097 && TREE_CODE (identifier) != BIT_NOT_EXPR)
13098 /* [namespace.udecl]
13100 A using declaration shall not name a template-id. */
13101 error_at (token->location,
13102 "a template-id may not appear in a using-declaration");
13103 else
13105 if (at_class_scope_p ())
13107 /* Create the USING_DECL. */
13108 decl = do_class_using_decl (parser->scope, identifier);
13110 if (check_for_bare_parameter_packs (decl))
13111 return false;
13112 else
13113 /* Add it to the list of members in this class. */
13114 finish_member_declaration (decl);
13116 else
13118 decl = cp_parser_lookup_name_simple (parser,
13119 identifier,
13120 token->location);
13121 if (decl == error_mark_node)
13122 cp_parser_name_lookup_error (parser, identifier,
13123 decl, NULL,
13124 token->location);
13125 else if (check_for_bare_parameter_packs (decl))
13126 return false;
13127 else if (!at_namespace_scope_p ())
13128 do_local_using_decl (decl, qscope, identifier);
13129 else
13130 do_toplevel_using_decl (decl, qscope, identifier);
13134 /* Look for the final `;'. */
13135 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13137 return true;
13140 /* Parse a using-directive.
13142 using-directive:
13143 using namespace :: [opt] nested-name-specifier [opt]
13144 namespace-name ; */
13146 static void
13147 cp_parser_using_directive (cp_parser* parser)
13149 tree namespace_decl;
13150 tree attribs;
13152 /* Look for the `using' keyword. */
13153 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
13154 /* And the `namespace' keyword. */
13155 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
13156 /* Look for the optional `::' operator. */
13157 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13158 /* And the optional nested-name-specifier. */
13159 cp_parser_nested_name_specifier_opt (parser,
13160 /*typename_keyword_p=*/false,
13161 /*check_dependency_p=*/true,
13162 /*type_p=*/false,
13163 /*is_declaration=*/true);
13164 /* Get the namespace being used. */
13165 namespace_decl = cp_parser_namespace_name (parser);
13166 /* And any specified attributes. */
13167 attribs = cp_parser_attributes_opt (parser);
13168 /* Update the symbol table. */
13169 parse_using_directive (namespace_decl, attribs);
13170 /* Look for the final `;'. */
13171 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13174 /* Parse an asm-definition.
13176 asm-definition:
13177 asm ( string-literal ) ;
13179 GNU Extension:
13181 asm-definition:
13182 asm volatile [opt] ( string-literal ) ;
13183 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
13184 asm volatile [opt] ( string-literal : asm-operand-list [opt]
13185 : asm-operand-list [opt] ) ;
13186 asm volatile [opt] ( string-literal : asm-operand-list [opt]
13187 : asm-operand-list [opt]
13188 : asm-clobber-list [opt] ) ;
13189 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
13190 : asm-clobber-list [opt]
13191 : asm-goto-list ) ; */
13193 static void
13194 cp_parser_asm_definition (cp_parser* parser)
13196 tree string;
13197 tree outputs = NULL_TREE;
13198 tree inputs = NULL_TREE;
13199 tree clobbers = NULL_TREE;
13200 tree labels = NULL_TREE;
13201 tree asm_stmt;
13202 bool volatile_p = false;
13203 bool extended_p = false;
13204 bool invalid_inputs_p = false;
13205 bool invalid_outputs_p = false;
13206 bool goto_p = false;
13207 const char *missing = NULL;
13209 /* Look for the `asm' keyword. */
13210 cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
13211 /* See if the next token is `volatile'. */
13212 if (cp_parser_allow_gnu_extensions_p (parser)
13213 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
13215 /* Remember that we saw the `volatile' keyword. */
13216 volatile_p = true;
13217 /* Consume the token. */
13218 cp_lexer_consume_token (parser->lexer);
13220 if (cp_parser_allow_gnu_extensions_p (parser)
13221 && parser->in_function_body
13222 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
13224 /* Remember that we saw the `goto' keyword. */
13225 goto_p = true;
13226 /* Consume the token. */
13227 cp_lexer_consume_token (parser->lexer);
13229 /* Look for the opening `('. */
13230 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
13231 return;
13232 /* Look for the string. */
13233 string = cp_parser_string_literal (parser, false, false);
13234 if (string == error_mark_node)
13236 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13237 /*consume_paren=*/true);
13238 return;
13241 /* If we're allowing GNU extensions, check for the extended assembly
13242 syntax. Unfortunately, the `:' tokens need not be separated by
13243 a space in C, and so, for compatibility, we tolerate that here
13244 too. Doing that means that we have to treat the `::' operator as
13245 two `:' tokens. */
13246 if (cp_parser_allow_gnu_extensions_p (parser)
13247 && parser->in_function_body
13248 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
13249 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
13251 bool inputs_p = false;
13252 bool clobbers_p = false;
13253 bool labels_p = false;
13255 /* The extended syntax was used. */
13256 extended_p = true;
13258 /* Look for outputs. */
13259 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13261 /* Consume the `:'. */
13262 cp_lexer_consume_token (parser->lexer);
13263 /* Parse the output-operands. */
13264 if (cp_lexer_next_token_is_not (parser->lexer,
13265 CPP_COLON)
13266 && cp_lexer_next_token_is_not (parser->lexer,
13267 CPP_SCOPE)
13268 && cp_lexer_next_token_is_not (parser->lexer,
13269 CPP_CLOSE_PAREN)
13270 && !goto_p)
13271 outputs = cp_parser_asm_operand_list (parser);
13273 if (outputs == error_mark_node)
13274 invalid_outputs_p = true;
13276 /* If the next token is `::', there are no outputs, and the
13277 next token is the beginning of the inputs. */
13278 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13279 /* The inputs are coming next. */
13280 inputs_p = true;
13282 /* Look for inputs. */
13283 if (inputs_p
13284 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13286 /* Consume the `:' or `::'. */
13287 cp_lexer_consume_token (parser->lexer);
13288 /* Parse the output-operands. */
13289 if (cp_lexer_next_token_is_not (parser->lexer,
13290 CPP_COLON)
13291 && cp_lexer_next_token_is_not (parser->lexer,
13292 CPP_SCOPE)
13293 && cp_lexer_next_token_is_not (parser->lexer,
13294 CPP_CLOSE_PAREN))
13295 inputs = cp_parser_asm_operand_list (parser);
13297 if (inputs == error_mark_node)
13298 invalid_inputs_p = true;
13300 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13301 /* The clobbers are coming next. */
13302 clobbers_p = true;
13304 /* Look for clobbers. */
13305 if (clobbers_p
13306 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13308 clobbers_p = true;
13309 /* Consume the `:' or `::'. */
13310 cp_lexer_consume_token (parser->lexer);
13311 /* Parse the clobbers. */
13312 if (cp_lexer_next_token_is_not (parser->lexer,
13313 CPP_COLON)
13314 && cp_lexer_next_token_is_not (parser->lexer,
13315 CPP_CLOSE_PAREN))
13316 clobbers = cp_parser_asm_clobber_list (parser);
13318 else if (goto_p
13319 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13320 /* The labels are coming next. */
13321 labels_p = true;
13323 /* Look for labels. */
13324 if (labels_p
13325 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
13327 labels_p = true;
13328 /* Consume the `:' or `::'. */
13329 cp_lexer_consume_token (parser->lexer);
13330 /* Parse the labels. */
13331 labels = cp_parser_asm_label_list (parser);
13334 if (goto_p && !labels_p)
13335 missing = clobbers_p ? "%<:%>" : "%<:%> or %<::%>";
13337 else if (goto_p)
13338 missing = "%<:%> or %<::%>";
13340 /* Look for the closing `)'. */
13341 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
13342 missing ? missing : "%<)%>"))
13343 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13344 /*consume_paren=*/true);
13345 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13347 if (!invalid_inputs_p && !invalid_outputs_p)
13349 /* Create the ASM_EXPR. */
13350 if (parser->in_function_body)
13352 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
13353 inputs, clobbers, labels);
13354 /* If the extended syntax was not used, mark the ASM_EXPR. */
13355 if (!extended_p)
13357 tree temp = asm_stmt;
13358 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
13359 temp = TREE_OPERAND (temp, 0);
13361 ASM_INPUT_P (temp) = 1;
13364 else
13365 cgraph_add_asm_node (string);
13369 /* Declarators [gram.dcl.decl] */
13371 /* Parse an init-declarator.
13373 init-declarator:
13374 declarator initializer [opt]
13376 GNU Extension:
13378 init-declarator:
13379 declarator asm-specification [opt] attributes [opt] initializer [opt]
13381 function-definition:
13382 decl-specifier-seq [opt] declarator ctor-initializer [opt]
13383 function-body
13384 decl-specifier-seq [opt] declarator function-try-block
13386 GNU Extension:
13388 function-definition:
13389 __extension__ function-definition
13391 The DECL_SPECIFIERS apply to this declarator. Returns a
13392 representation of the entity declared. If MEMBER_P is TRUE, then
13393 this declarator appears in a class scope. The new DECL created by
13394 this declarator is returned.
13396 The CHECKS are access checks that should be performed once we know
13397 what entity is being declared (and, therefore, what classes have
13398 befriended it).
13400 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
13401 for a function-definition here as well. If the declarator is a
13402 declarator for a function-definition, *FUNCTION_DEFINITION_P will
13403 be TRUE upon return. By that point, the function-definition will
13404 have been completely parsed.
13406 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
13407 is FALSE. */
13409 static tree
13410 cp_parser_init_declarator (cp_parser* parser,
13411 cp_decl_specifier_seq *decl_specifiers,
13412 VEC (deferred_access_check,gc)* checks,
13413 bool function_definition_allowed_p,
13414 bool member_p,
13415 int declares_class_or_enum,
13416 bool* function_definition_p)
13418 cp_token *token = NULL, *asm_spec_start_token = NULL,
13419 *attributes_start_token = NULL;
13420 cp_declarator *declarator;
13421 tree prefix_attributes;
13422 tree attributes;
13423 tree asm_specification;
13424 tree initializer;
13425 tree decl = NULL_TREE;
13426 tree scope;
13427 int is_initialized;
13428 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
13429 initialized with "= ..", CPP_OPEN_PAREN if initialized with
13430 "(...)". */
13431 enum cpp_ttype initialization_kind;
13432 bool is_direct_init = false;
13433 bool is_non_constant_init;
13434 int ctor_dtor_or_conv_p;
13435 bool friend_p;
13436 tree pushed_scope = NULL;
13438 /* Gather the attributes that were provided with the
13439 decl-specifiers. */
13440 prefix_attributes = decl_specifiers->attributes;
13442 /* Assume that this is not the declarator for a function
13443 definition. */
13444 if (function_definition_p)
13445 *function_definition_p = false;
13447 /* Defer access checks while parsing the declarator; we cannot know
13448 what names are accessible until we know what is being
13449 declared. */
13450 resume_deferring_access_checks ();
13452 /* Parse the declarator. */
13453 token = cp_lexer_peek_token (parser->lexer);
13454 declarator
13455 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13456 &ctor_dtor_or_conv_p,
13457 /*parenthesized_p=*/NULL,
13458 /*member_p=*/false);
13459 /* Gather up the deferred checks. */
13460 stop_deferring_access_checks ();
13462 /* If the DECLARATOR was erroneous, there's no need to go
13463 further. */
13464 if (declarator == cp_error_declarator)
13465 return error_mark_node;
13467 /* Check that the number of template-parameter-lists is OK. */
13468 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
13469 token->location))
13470 return error_mark_node;
13472 if (declares_class_or_enum & 2)
13473 cp_parser_check_for_definition_in_return_type (declarator,
13474 decl_specifiers->type,
13475 decl_specifiers->type_location);
13477 /* Figure out what scope the entity declared by the DECLARATOR is
13478 located in. `grokdeclarator' sometimes changes the scope, so
13479 we compute it now. */
13480 scope = get_scope_of_declarator (declarator);
13482 /* Perform any lookups in the declared type which were thought to be
13483 dependent, but are not in the scope of the declarator. */
13484 decl_specifiers->type
13485 = maybe_update_decl_type (decl_specifiers->type, scope);
13487 /* If we're allowing GNU extensions, look for an asm-specification
13488 and attributes. */
13489 if (cp_parser_allow_gnu_extensions_p (parser))
13491 /* Look for an asm-specification. */
13492 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
13493 asm_specification = cp_parser_asm_specification_opt (parser);
13494 /* And attributes. */
13495 attributes_start_token = cp_lexer_peek_token (parser->lexer);
13496 attributes = cp_parser_attributes_opt (parser);
13498 else
13500 asm_specification = NULL_TREE;
13501 attributes = NULL_TREE;
13504 /* Peek at the next token. */
13505 token = cp_lexer_peek_token (parser->lexer);
13506 /* Check to see if the token indicates the start of a
13507 function-definition. */
13508 if (function_declarator_p (declarator)
13509 && cp_parser_token_starts_function_definition_p (token))
13511 if (!function_definition_allowed_p)
13513 /* If a function-definition should not appear here, issue an
13514 error message. */
13515 cp_parser_error (parser,
13516 "a function-definition is not allowed here");
13517 return error_mark_node;
13519 else
13521 location_t func_brace_location
13522 = cp_lexer_peek_token (parser->lexer)->location;
13524 /* Neither attributes nor an asm-specification are allowed
13525 on a function-definition. */
13526 if (asm_specification)
13527 error_at (asm_spec_start_token->location,
13528 "an asm-specification is not allowed "
13529 "on a function-definition");
13530 if (attributes)
13531 error_at (attributes_start_token->location,
13532 "attributes are not allowed on a function-definition");
13533 /* This is a function-definition. */
13534 *function_definition_p = true;
13536 /* Parse the function definition. */
13537 if (member_p)
13538 decl = cp_parser_save_member_function_body (parser,
13539 decl_specifiers,
13540 declarator,
13541 prefix_attributes);
13542 else
13543 decl
13544 = (cp_parser_function_definition_from_specifiers_and_declarator
13545 (parser, decl_specifiers, prefix_attributes, declarator));
13547 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
13549 /* This is where the prologue starts... */
13550 DECL_STRUCT_FUNCTION (decl)->function_start_locus
13551 = func_brace_location;
13554 return decl;
13558 /* [dcl.dcl]
13560 Only in function declarations for constructors, destructors, and
13561 type conversions can the decl-specifier-seq be omitted.
13563 We explicitly postpone this check past the point where we handle
13564 function-definitions because we tolerate function-definitions
13565 that are missing their return types in some modes. */
13566 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
13568 cp_parser_error (parser,
13569 "expected constructor, destructor, or type conversion");
13570 return error_mark_node;
13573 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
13574 if (token->type == CPP_EQ
13575 || token->type == CPP_OPEN_PAREN
13576 || token->type == CPP_OPEN_BRACE)
13578 is_initialized = SD_INITIALIZED;
13579 initialization_kind = token->type;
13581 if (token->type == CPP_EQ
13582 && function_declarator_p (declarator))
13584 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13585 if (t2->keyword == RID_DEFAULT)
13586 is_initialized = SD_DEFAULTED;
13587 else if (t2->keyword == RID_DELETE)
13588 is_initialized = SD_DELETED;
13591 else
13593 /* If the init-declarator isn't initialized and isn't followed by a
13594 `,' or `;', it's not a valid init-declarator. */
13595 if (token->type != CPP_COMMA
13596 && token->type != CPP_SEMICOLON)
13598 cp_parser_error (parser, "expected initializer");
13599 return error_mark_node;
13601 is_initialized = SD_UNINITIALIZED;
13602 initialization_kind = CPP_EOF;
13605 /* Because start_decl has side-effects, we should only call it if we
13606 know we're going ahead. By this point, we know that we cannot
13607 possibly be looking at any other construct. */
13608 cp_parser_commit_to_tentative_parse (parser);
13610 /* If the decl specifiers were bad, issue an error now that we're
13611 sure this was intended to be a declarator. Then continue
13612 declaring the variable(s), as int, to try to cut down on further
13613 errors. */
13614 if (decl_specifiers->any_specifiers_p
13615 && decl_specifiers->type == error_mark_node)
13617 cp_parser_error (parser, "invalid type in declaration");
13618 decl_specifiers->type = integer_type_node;
13621 /* Check to see whether or not this declaration is a friend. */
13622 friend_p = cp_parser_friend_p (decl_specifiers);
13624 /* Enter the newly declared entry in the symbol table. If we're
13625 processing a declaration in a class-specifier, we wait until
13626 after processing the initializer. */
13627 if (!member_p)
13629 if (parser->in_unbraced_linkage_specification_p)
13630 decl_specifiers->storage_class = sc_extern;
13631 decl = start_decl (declarator, decl_specifiers,
13632 is_initialized, attributes, prefix_attributes,
13633 &pushed_scope);
13635 else if (scope)
13636 /* Enter the SCOPE. That way unqualified names appearing in the
13637 initializer will be looked up in SCOPE. */
13638 pushed_scope = push_scope (scope);
13640 /* Perform deferred access control checks, now that we know in which
13641 SCOPE the declared entity resides. */
13642 if (!member_p && decl)
13644 tree saved_current_function_decl = NULL_TREE;
13646 /* If the entity being declared is a function, pretend that we
13647 are in its scope. If it is a `friend', it may have access to
13648 things that would not otherwise be accessible. */
13649 if (TREE_CODE (decl) == FUNCTION_DECL)
13651 saved_current_function_decl = current_function_decl;
13652 current_function_decl = decl;
13655 /* Perform access checks for template parameters. */
13656 cp_parser_perform_template_parameter_access_checks (checks);
13658 /* Perform the access control checks for the declarator and the
13659 decl-specifiers. */
13660 perform_deferred_access_checks ();
13662 /* Restore the saved value. */
13663 if (TREE_CODE (decl) == FUNCTION_DECL)
13664 current_function_decl = saved_current_function_decl;
13667 /* Parse the initializer. */
13668 initializer = NULL_TREE;
13669 is_direct_init = false;
13670 is_non_constant_init = true;
13671 if (is_initialized)
13673 if (function_declarator_p (declarator))
13675 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
13676 if (initialization_kind == CPP_EQ)
13677 initializer = cp_parser_pure_specifier (parser);
13678 else
13680 /* If the declaration was erroneous, we don't really
13681 know what the user intended, so just silently
13682 consume the initializer. */
13683 if (decl != error_mark_node)
13684 error_at (initializer_start_token->location,
13685 "initializer provided for function");
13686 cp_parser_skip_to_closing_parenthesis (parser,
13687 /*recovering=*/true,
13688 /*or_comma=*/false,
13689 /*consume_paren=*/true);
13692 else
13694 /* We want to record the extra mangling scope for in-class
13695 initializers of class members and initializers of static data
13696 member templates. The former is a C++0x feature which isn't
13697 implemented yet, and I expect it will involve deferring
13698 parsing of the initializer until end of class as with default
13699 arguments. So right here we only handle the latter. */
13700 if (!member_p && processing_template_decl)
13701 start_lambda_scope (decl);
13702 initializer = cp_parser_initializer (parser,
13703 &is_direct_init,
13704 &is_non_constant_init);
13705 if (!member_p && processing_template_decl)
13706 finish_lambda_scope ();
13710 /* The old parser allows attributes to appear after a parenthesized
13711 initializer. Mark Mitchell proposed removing this functionality
13712 on the GCC mailing lists on 2002-08-13. This parser accepts the
13713 attributes -- but ignores them. */
13714 if (cp_parser_allow_gnu_extensions_p (parser)
13715 && initialization_kind == CPP_OPEN_PAREN)
13716 if (cp_parser_attributes_opt (parser))
13717 warning (OPT_Wattributes,
13718 "attributes after parenthesized initializer ignored");
13720 /* For an in-class declaration, use `grokfield' to create the
13721 declaration. */
13722 if (member_p)
13724 if (pushed_scope)
13726 pop_scope (pushed_scope);
13727 pushed_scope = false;
13729 decl = grokfield (declarator, decl_specifiers,
13730 initializer, !is_non_constant_init,
13731 /*asmspec=*/NULL_TREE,
13732 prefix_attributes);
13733 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
13734 cp_parser_save_default_args (parser, decl);
13737 /* Finish processing the declaration. But, skip friend
13738 declarations. */
13739 if (!friend_p && decl && decl != error_mark_node)
13741 cp_finish_decl (decl,
13742 initializer, !is_non_constant_init,
13743 asm_specification,
13744 /* If the initializer is in parentheses, then this is
13745 a direct-initialization, which means that an
13746 `explicit' constructor is OK. Otherwise, an
13747 `explicit' constructor cannot be used. */
13748 ((is_direct_init || !is_initialized)
13749 ? 0 : LOOKUP_ONLYCONVERTING));
13751 else if ((cxx_dialect != cxx98) && friend_p
13752 && decl && TREE_CODE (decl) == FUNCTION_DECL)
13753 /* Core issue #226 (C++0x only): A default template-argument
13754 shall not be specified in a friend class template
13755 declaration. */
13756 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
13757 /*is_partial=*/0, /*is_friend_decl=*/1);
13759 if (!friend_p && pushed_scope)
13760 pop_scope (pushed_scope);
13762 return decl;
13765 /* Parse a declarator.
13767 declarator:
13768 direct-declarator
13769 ptr-operator declarator
13771 abstract-declarator:
13772 ptr-operator abstract-declarator [opt]
13773 direct-abstract-declarator
13775 GNU Extensions:
13777 declarator:
13778 attributes [opt] direct-declarator
13779 attributes [opt] ptr-operator declarator
13781 abstract-declarator:
13782 attributes [opt] ptr-operator abstract-declarator [opt]
13783 attributes [opt] direct-abstract-declarator
13785 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
13786 detect constructor, destructor or conversion operators. It is set
13787 to -1 if the declarator is a name, and +1 if it is a
13788 function. Otherwise it is set to zero. Usually you just want to
13789 test for >0, but internally the negative value is used.
13791 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
13792 a decl-specifier-seq unless it declares a constructor, destructor,
13793 or conversion. It might seem that we could check this condition in
13794 semantic analysis, rather than parsing, but that makes it difficult
13795 to handle something like `f()'. We want to notice that there are
13796 no decl-specifiers, and therefore realize that this is an
13797 expression, not a declaration.)
13799 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
13800 the declarator is a direct-declarator of the form "(...)".
13802 MEMBER_P is true iff this declarator is a member-declarator. */
13804 static cp_declarator *
13805 cp_parser_declarator (cp_parser* parser,
13806 cp_parser_declarator_kind dcl_kind,
13807 int* ctor_dtor_or_conv_p,
13808 bool* parenthesized_p,
13809 bool member_p)
13811 cp_declarator *declarator;
13812 enum tree_code code;
13813 cp_cv_quals cv_quals;
13814 tree class_type;
13815 tree attributes = NULL_TREE;
13817 /* Assume this is not a constructor, destructor, or type-conversion
13818 operator. */
13819 if (ctor_dtor_or_conv_p)
13820 *ctor_dtor_or_conv_p = 0;
13822 if (cp_parser_allow_gnu_extensions_p (parser))
13823 attributes = cp_parser_attributes_opt (parser);
13825 /* Check for the ptr-operator production. */
13826 cp_parser_parse_tentatively (parser);
13827 /* Parse the ptr-operator. */
13828 code = cp_parser_ptr_operator (parser,
13829 &class_type,
13830 &cv_quals);
13831 /* If that worked, then we have a ptr-operator. */
13832 if (cp_parser_parse_definitely (parser))
13834 /* If a ptr-operator was found, then this declarator was not
13835 parenthesized. */
13836 if (parenthesized_p)
13837 *parenthesized_p = true;
13838 /* The dependent declarator is optional if we are parsing an
13839 abstract-declarator. */
13840 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13841 cp_parser_parse_tentatively (parser);
13843 /* Parse the dependent declarator. */
13844 declarator = cp_parser_declarator (parser, dcl_kind,
13845 /*ctor_dtor_or_conv_p=*/NULL,
13846 /*parenthesized_p=*/NULL,
13847 /*member_p=*/false);
13849 /* If we are parsing an abstract-declarator, we must handle the
13850 case where the dependent declarator is absent. */
13851 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
13852 && !cp_parser_parse_definitely (parser))
13853 declarator = NULL;
13855 declarator = cp_parser_make_indirect_declarator
13856 (code, class_type, cv_quals, declarator);
13858 /* Everything else is a direct-declarator. */
13859 else
13861 if (parenthesized_p)
13862 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
13863 CPP_OPEN_PAREN);
13864 declarator = cp_parser_direct_declarator (parser, dcl_kind,
13865 ctor_dtor_or_conv_p,
13866 member_p);
13869 if (attributes && declarator && declarator != cp_error_declarator)
13870 declarator->attributes = attributes;
13872 return declarator;
13875 /* Parse a direct-declarator or direct-abstract-declarator.
13877 direct-declarator:
13878 declarator-id
13879 direct-declarator ( parameter-declaration-clause )
13880 cv-qualifier-seq [opt]
13881 exception-specification [opt]
13882 direct-declarator [ constant-expression [opt] ]
13883 ( declarator )
13885 direct-abstract-declarator:
13886 direct-abstract-declarator [opt]
13887 ( parameter-declaration-clause )
13888 cv-qualifier-seq [opt]
13889 exception-specification [opt]
13890 direct-abstract-declarator [opt] [ constant-expression [opt] ]
13891 ( abstract-declarator )
13893 Returns a representation of the declarator. DCL_KIND is
13894 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
13895 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
13896 we are parsing a direct-declarator. It is
13897 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
13898 of ambiguity we prefer an abstract declarator, as per
13899 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
13900 cp_parser_declarator. */
13902 static cp_declarator *
13903 cp_parser_direct_declarator (cp_parser* parser,
13904 cp_parser_declarator_kind dcl_kind,
13905 int* ctor_dtor_or_conv_p,
13906 bool member_p)
13908 cp_token *token;
13909 cp_declarator *declarator = NULL;
13910 tree scope = NULL_TREE;
13911 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13912 bool saved_in_declarator_p = parser->in_declarator_p;
13913 bool first = true;
13914 tree pushed_scope = NULL_TREE;
13916 while (true)
13918 /* Peek at the next token. */
13919 token = cp_lexer_peek_token (parser->lexer);
13920 if (token->type == CPP_OPEN_PAREN)
13922 /* This is either a parameter-declaration-clause, or a
13923 parenthesized declarator. When we know we are parsing a
13924 named declarator, it must be a parenthesized declarator
13925 if FIRST is true. For instance, `(int)' is a
13926 parameter-declaration-clause, with an omitted
13927 direct-abstract-declarator. But `((*))', is a
13928 parenthesized abstract declarator. Finally, when T is a
13929 template parameter `(T)' is a
13930 parameter-declaration-clause, and not a parenthesized
13931 named declarator.
13933 We first try and parse a parameter-declaration-clause,
13934 and then try a nested declarator (if FIRST is true).
13936 It is not an error for it not to be a
13937 parameter-declaration-clause, even when FIRST is
13938 false. Consider,
13940 int i (int);
13941 int i (3);
13943 The first is the declaration of a function while the
13944 second is the definition of a variable, including its
13945 initializer.
13947 Having seen only the parenthesis, we cannot know which of
13948 these two alternatives should be selected. Even more
13949 complex are examples like:
13951 int i (int (a));
13952 int i (int (3));
13954 The former is a function-declaration; the latter is a
13955 variable initialization.
13957 Thus again, we try a parameter-declaration-clause, and if
13958 that fails, we back out and return. */
13960 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13962 tree params;
13963 unsigned saved_num_template_parameter_lists;
13964 bool is_declarator = false;
13965 tree t;
13967 /* In a member-declarator, the only valid interpretation
13968 of a parenthesis is the start of a
13969 parameter-declaration-clause. (It is invalid to
13970 initialize a static data member with a parenthesized
13971 initializer; only the "=" form of initialization is
13972 permitted.) */
13973 if (!member_p)
13974 cp_parser_parse_tentatively (parser);
13976 /* Consume the `('. */
13977 cp_lexer_consume_token (parser->lexer);
13978 if (first)
13980 /* If this is going to be an abstract declarator, we're
13981 in a declarator and we can't have default args. */
13982 parser->default_arg_ok_p = false;
13983 parser->in_declarator_p = true;
13986 /* Inside the function parameter list, surrounding
13987 template-parameter-lists do not apply. */
13988 saved_num_template_parameter_lists
13989 = parser->num_template_parameter_lists;
13990 parser->num_template_parameter_lists = 0;
13992 begin_scope (sk_function_parms, NULL_TREE);
13994 /* Parse the parameter-declaration-clause. */
13995 params = cp_parser_parameter_declaration_clause (parser);
13997 parser->num_template_parameter_lists
13998 = saved_num_template_parameter_lists;
14000 /* If all went well, parse the cv-qualifier-seq and the
14001 exception-specification. */
14002 if (member_p || cp_parser_parse_definitely (parser))
14004 cp_cv_quals cv_quals;
14005 tree exception_specification;
14006 tree late_return;
14008 is_declarator = true;
14010 if (ctor_dtor_or_conv_p)
14011 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
14012 first = false;
14013 /* Consume the `)'. */
14014 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
14016 /* Parse the cv-qualifier-seq. */
14017 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14018 /* And the exception-specification. */
14019 exception_specification
14020 = cp_parser_exception_specification_opt (parser);
14022 late_return
14023 = cp_parser_late_return_type_opt (parser);
14025 /* Create the function-declarator. */
14026 declarator = make_call_declarator (declarator,
14027 params,
14028 cv_quals,
14029 exception_specification,
14030 late_return);
14031 /* Any subsequent parameter lists are to do with
14032 return type, so are not those of the declared
14033 function. */
14034 parser->default_arg_ok_p = false;
14037 /* Remove the function parms from scope. */
14038 for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
14039 pop_binding (DECL_NAME (t), t);
14040 leave_scope();
14042 if (is_declarator)
14043 /* Repeat the main loop. */
14044 continue;
14047 /* If this is the first, we can try a parenthesized
14048 declarator. */
14049 if (first)
14051 bool saved_in_type_id_in_expr_p;
14053 parser->default_arg_ok_p = saved_default_arg_ok_p;
14054 parser->in_declarator_p = saved_in_declarator_p;
14056 /* Consume the `('. */
14057 cp_lexer_consume_token (parser->lexer);
14058 /* Parse the nested declarator. */
14059 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14060 parser->in_type_id_in_expr_p = true;
14061 declarator
14062 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
14063 /*parenthesized_p=*/NULL,
14064 member_p);
14065 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14066 first = false;
14067 /* Expect a `)'. */
14068 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
14069 declarator = cp_error_declarator;
14070 if (declarator == cp_error_declarator)
14071 break;
14073 goto handle_declarator;
14075 /* Otherwise, we must be done. */
14076 else
14077 break;
14079 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14080 && token->type == CPP_OPEN_SQUARE)
14082 /* Parse an array-declarator. */
14083 tree bounds;
14085 if (ctor_dtor_or_conv_p)
14086 *ctor_dtor_or_conv_p = 0;
14088 first = false;
14089 parser->default_arg_ok_p = false;
14090 parser->in_declarator_p = true;
14091 /* Consume the `['. */
14092 cp_lexer_consume_token (parser->lexer);
14093 /* Peek at the next token. */
14094 token = cp_lexer_peek_token (parser->lexer);
14095 /* If the next token is `]', then there is no
14096 constant-expression. */
14097 if (token->type != CPP_CLOSE_SQUARE)
14099 bool non_constant_p;
14101 bounds
14102 = cp_parser_constant_expression (parser,
14103 /*allow_non_constant=*/true,
14104 &non_constant_p);
14105 if (!non_constant_p)
14106 bounds = fold_non_dependent_expr (bounds);
14107 /* Normally, the array bound must be an integral constant
14108 expression. However, as an extension, we allow VLAs
14109 in function scopes as long as they aren't part of a
14110 parameter declaration. */
14111 else if (!parser->in_function_body
14112 || current_binding_level->kind == sk_function_parms)
14114 cp_parser_error (parser,
14115 "array bound is not an integer constant");
14116 bounds = error_mark_node;
14118 else if (processing_template_decl && !error_operand_p (bounds))
14120 /* Remember this wasn't a constant-expression. */
14121 bounds = build_nop (TREE_TYPE (bounds), bounds);
14122 TREE_SIDE_EFFECTS (bounds) = 1;
14125 else
14126 bounds = NULL_TREE;
14127 /* Look for the closing `]'. */
14128 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
14130 declarator = cp_error_declarator;
14131 break;
14134 declarator = make_array_declarator (declarator, bounds);
14136 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
14139 tree qualifying_scope;
14140 tree unqualified_name;
14141 special_function_kind sfk;
14142 bool abstract_ok;
14143 bool pack_expansion_p = false;
14144 cp_token *declarator_id_start_token;
14146 /* Parse a declarator-id */
14147 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
14148 if (abstract_ok)
14150 cp_parser_parse_tentatively (parser);
14152 /* If we see an ellipsis, we should be looking at a
14153 parameter pack. */
14154 if (token->type == CPP_ELLIPSIS)
14156 /* Consume the `...' */
14157 cp_lexer_consume_token (parser->lexer);
14159 pack_expansion_p = true;
14163 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
14164 unqualified_name
14165 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
14166 qualifying_scope = parser->scope;
14167 if (abstract_ok)
14169 bool okay = false;
14171 if (!unqualified_name && pack_expansion_p)
14173 /* Check whether an error occurred. */
14174 okay = !cp_parser_error_occurred (parser);
14176 /* We already consumed the ellipsis to mark a
14177 parameter pack, but we have no way to report it,
14178 so abort the tentative parse. We will be exiting
14179 immediately anyway. */
14180 cp_parser_abort_tentative_parse (parser);
14182 else
14183 okay = cp_parser_parse_definitely (parser);
14185 if (!okay)
14186 unqualified_name = error_mark_node;
14187 else if (unqualified_name
14188 && (qualifying_scope
14189 || (TREE_CODE (unqualified_name)
14190 != IDENTIFIER_NODE)))
14192 cp_parser_error (parser, "expected unqualified-id");
14193 unqualified_name = error_mark_node;
14197 if (!unqualified_name)
14198 return NULL;
14199 if (unqualified_name == error_mark_node)
14201 declarator = cp_error_declarator;
14202 pack_expansion_p = false;
14203 declarator->parameter_pack_p = false;
14204 break;
14207 if (qualifying_scope && at_namespace_scope_p ()
14208 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
14210 /* In the declaration of a member of a template class
14211 outside of the class itself, the SCOPE will sometimes
14212 be a TYPENAME_TYPE. For example, given:
14214 template <typename T>
14215 int S<T>::R::i = 3;
14217 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
14218 this context, we must resolve S<T>::R to an ordinary
14219 type, rather than a typename type.
14221 The reason we normally avoid resolving TYPENAME_TYPEs
14222 is that a specialization of `S' might render
14223 `S<T>::R' not a type. However, if `S' is
14224 specialized, then this `i' will not be used, so there
14225 is no harm in resolving the types here. */
14226 tree type;
14228 /* Resolve the TYPENAME_TYPE. */
14229 type = resolve_typename_type (qualifying_scope,
14230 /*only_current_p=*/false);
14231 /* If that failed, the declarator is invalid. */
14232 if (TREE_CODE (type) == TYPENAME_TYPE)
14234 if (typedef_variant_p (type))
14235 error_at (declarator_id_start_token->location,
14236 "cannot define member of dependent typedef "
14237 "%qT", type);
14238 else
14239 error_at (declarator_id_start_token->location,
14240 "%<%T::%E%> is not a type",
14241 TYPE_CONTEXT (qualifying_scope),
14242 TYPE_IDENTIFIER (qualifying_scope));
14244 qualifying_scope = type;
14247 sfk = sfk_none;
14249 if (unqualified_name)
14251 tree class_type;
14253 if (qualifying_scope
14254 && CLASS_TYPE_P (qualifying_scope))
14255 class_type = qualifying_scope;
14256 else
14257 class_type = current_class_type;
14259 if (TREE_CODE (unqualified_name) == TYPE_DECL)
14261 tree name_type = TREE_TYPE (unqualified_name);
14262 if (class_type && same_type_p (name_type, class_type))
14264 if (qualifying_scope
14265 && CLASSTYPE_USE_TEMPLATE (name_type))
14267 error_at (declarator_id_start_token->location,
14268 "invalid use of constructor as a template");
14269 inform (declarator_id_start_token->location,
14270 "use %<%T::%D%> instead of %<%T::%D%> to "
14271 "name the constructor in a qualified name",
14272 class_type,
14273 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
14274 class_type, name_type);
14275 declarator = cp_error_declarator;
14276 break;
14278 else
14279 unqualified_name = constructor_name (class_type);
14281 else
14283 /* We do not attempt to print the declarator
14284 here because we do not have enough
14285 information about its original syntactic
14286 form. */
14287 cp_parser_error (parser, "invalid declarator");
14288 declarator = cp_error_declarator;
14289 break;
14293 if (class_type)
14295 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
14296 sfk = sfk_destructor;
14297 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
14298 sfk = sfk_conversion;
14299 else if (/* There's no way to declare a constructor
14300 for an anonymous type, even if the type
14301 got a name for linkage purposes. */
14302 !TYPE_WAS_ANONYMOUS (class_type)
14303 && constructor_name_p (unqualified_name,
14304 class_type))
14306 unqualified_name = constructor_name (class_type);
14307 sfk = sfk_constructor;
14309 else if (is_overloaded_fn (unqualified_name)
14310 && DECL_CONSTRUCTOR_P (get_first_fn
14311 (unqualified_name)))
14312 sfk = sfk_constructor;
14314 if (ctor_dtor_or_conv_p && sfk != sfk_none)
14315 *ctor_dtor_or_conv_p = -1;
14318 declarator = make_id_declarator (qualifying_scope,
14319 unqualified_name,
14320 sfk);
14321 declarator->id_loc = token->location;
14322 declarator->parameter_pack_p = pack_expansion_p;
14324 if (pack_expansion_p)
14325 maybe_warn_variadic_templates ();
14328 handle_declarator:;
14329 scope = get_scope_of_declarator (declarator);
14330 if (scope)
14331 /* Any names that appear after the declarator-id for a
14332 member are looked up in the containing scope. */
14333 pushed_scope = push_scope (scope);
14334 parser->in_declarator_p = true;
14335 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
14336 || (declarator && declarator->kind == cdk_id))
14337 /* Default args are only allowed on function
14338 declarations. */
14339 parser->default_arg_ok_p = saved_default_arg_ok_p;
14340 else
14341 parser->default_arg_ok_p = false;
14343 first = false;
14345 /* We're done. */
14346 else
14347 break;
14350 /* For an abstract declarator, we might wind up with nothing at this
14351 point. That's an error; the declarator is not optional. */
14352 if (!declarator)
14353 cp_parser_error (parser, "expected declarator");
14355 /* If we entered a scope, we must exit it now. */
14356 if (pushed_scope)
14357 pop_scope (pushed_scope);
14359 parser->default_arg_ok_p = saved_default_arg_ok_p;
14360 parser->in_declarator_p = saved_in_declarator_p;
14362 return declarator;
14365 /* Parse a ptr-operator.
14367 ptr-operator:
14368 * cv-qualifier-seq [opt]
14370 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
14372 GNU Extension:
14374 ptr-operator:
14375 & cv-qualifier-seq [opt]
14377 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
14378 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
14379 an rvalue reference. In the case of a pointer-to-member, *TYPE is
14380 filled in with the TYPE containing the member. *CV_QUALS is
14381 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
14382 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
14383 Note that the tree codes returned by this function have nothing
14384 to do with the types of trees that will be eventually be created
14385 to represent the pointer or reference type being parsed. They are
14386 just constants with suggestive names. */
14387 static enum tree_code
14388 cp_parser_ptr_operator (cp_parser* parser,
14389 tree* type,
14390 cp_cv_quals *cv_quals)
14392 enum tree_code code = ERROR_MARK;
14393 cp_token *token;
14395 /* Assume that it's not a pointer-to-member. */
14396 *type = NULL_TREE;
14397 /* And that there are no cv-qualifiers. */
14398 *cv_quals = TYPE_UNQUALIFIED;
14400 /* Peek at the next token. */
14401 token = cp_lexer_peek_token (parser->lexer);
14403 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
14404 if (token->type == CPP_MULT)
14405 code = INDIRECT_REF;
14406 else if (token->type == CPP_AND)
14407 code = ADDR_EXPR;
14408 else if ((cxx_dialect != cxx98) &&
14409 token->type == CPP_AND_AND) /* C++0x only */
14410 code = NON_LVALUE_EXPR;
14412 if (code != ERROR_MARK)
14414 /* Consume the `*', `&' or `&&'. */
14415 cp_lexer_consume_token (parser->lexer);
14417 /* A `*' can be followed by a cv-qualifier-seq, and so can a
14418 `&', if we are allowing GNU extensions. (The only qualifier
14419 that can legally appear after `&' is `restrict', but that is
14420 enforced during semantic analysis. */
14421 if (code == INDIRECT_REF
14422 || cp_parser_allow_gnu_extensions_p (parser))
14423 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14425 else
14427 /* Try the pointer-to-member case. */
14428 cp_parser_parse_tentatively (parser);
14429 /* Look for the optional `::' operator. */
14430 cp_parser_global_scope_opt (parser,
14431 /*current_scope_valid_p=*/false);
14432 /* Look for the nested-name specifier. */
14433 token = cp_lexer_peek_token (parser->lexer);
14434 cp_parser_nested_name_specifier (parser,
14435 /*typename_keyword_p=*/false,
14436 /*check_dependency_p=*/true,
14437 /*type_p=*/false,
14438 /*is_declaration=*/false);
14439 /* If we found it, and the next token is a `*', then we are
14440 indeed looking at a pointer-to-member operator. */
14441 if (!cp_parser_error_occurred (parser)
14442 && cp_parser_require (parser, CPP_MULT, "%<*%>"))
14444 /* Indicate that the `*' operator was used. */
14445 code = INDIRECT_REF;
14447 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
14448 error_at (token->location, "%qD is a namespace", parser->scope);
14449 else
14451 /* The type of which the member is a member is given by the
14452 current SCOPE. */
14453 *type = parser->scope;
14454 /* The next name will not be qualified. */
14455 parser->scope = NULL_TREE;
14456 parser->qualifying_scope = NULL_TREE;
14457 parser->object_scope = NULL_TREE;
14458 /* Look for the optional cv-qualifier-seq. */
14459 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14462 /* If that didn't work we don't have a ptr-operator. */
14463 if (!cp_parser_parse_definitely (parser))
14464 cp_parser_error (parser, "expected ptr-operator");
14467 return code;
14470 /* Parse an (optional) cv-qualifier-seq.
14472 cv-qualifier-seq:
14473 cv-qualifier cv-qualifier-seq [opt]
14475 cv-qualifier:
14476 const
14477 volatile
14479 GNU Extension:
14481 cv-qualifier:
14482 __restrict__
14484 Returns a bitmask representing the cv-qualifiers. */
14486 static cp_cv_quals
14487 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
14489 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
14491 while (true)
14493 cp_token *token;
14494 cp_cv_quals cv_qualifier;
14496 /* Peek at the next token. */
14497 token = cp_lexer_peek_token (parser->lexer);
14498 /* See if it's a cv-qualifier. */
14499 switch (token->keyword)
14501 case RID_CONST:
14502 cv_qualifier = TYPE_QUAL_CONST;
14503 break;
14505 case RID_VOLATILE:
14506 cv_qualifier = TYPE_QUAL_VOLATILE;
14507 break;
14509 case RID_RESTRICT:
14510 cv_qualifier = TYPE_QUAL_RESTRICT;
14511 break;
14513 default:
14514 cv_qualifier = TYPE_UNQUALIFIED;
14515 break;
14518 if (!cv_qualifier)
14519 break;
14521 if (cv_quals & cv_qualifier)
14523 error_at (token->location, "duplicate cv-qualifier");
14524 cp_lexer_purge_token (parser->lexer);
14526 else
14528 cp_lexer_consume_token (parser->lexer);
14529 cv_quals |= cv_qualifier;
14533 return cv_quals;
14536 /* Parse a late-specified return type, if any. This is not a separate
14537 non-terminal, but part of a function declarator, which looks like
14539 -> trailing-type-specifier-seq abstract-declarator(opt)
14541 Returns the type indicated by the type-id. */
14543 static tree
14544 cp_parser_late_return_type_opt (cp_parser* parser)
14546 cp_token *token;
14548 /* Peek at the next token. */
14549 token = cp_lexer_peek_token (parser->lexer);
14550 /* A late-specified return type is indicated by an initial '->'. */
14551 if (token->type != CPP_DEREF)
14552 return NULL_TREE;
14554 /* Consume the ->. */
14555 cp_lexer_consume_token (parser->lexer);
14557 return cp_parser_trailing_type_id (parser);
14560 /* Parse a declarator-id.
14562 declarator-id:
14563 id-expression
14564 :: [opt] nested-name-specifier [opt] type-name
14566 In the `id-expression' case, the value returned is as for
14567 cp_parser_id_expression if the id-expression was an unqualified-id.
14568 If the id-expression was a qualified-id, then a SCOPE_REF is
14569 returned. The first operand is the scope (either a NAMESPACE_DECL
14570 or TREE_TYPE), but the second is still just a representation of an
14571 unqualified-id. */
14573 static tree
14574 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
14576 tree id;
14577 /* The expression must be an id-expression. Assume that qualified
14578 names are the names of types so that:
14580 template <class T>
14581 int S<T>::R::i = 3;
14583 will work; we must treat `S<T>::R' as the name of a type.
14584 Similarly, assume that qualified names are templates, where
14585 required, so that:
14587 template <class T>
14588 int S<T>::R<T>::i = 3;
14590 will work, too. */
14591 id = cp_parser_id_expression (parser,
14592 /*template_keyword_p=*/false,
14593 /*check_dependency_p=*/false,
14594 /*template_p=*/NULL,
14595 /*declarator_p=*/true,
14596 optional_p);
14597 if (id && BASELINK_P (id))
14598 id = BASELINK_FUNCTIONS (id);
14599 return id;
14602 /* Parse a type-id.
14604 type-id:
14605 type-specifier-seq abstract-declarator [opt]
14607 Returns the TYPE specified. */
14609 static tree
14610 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
14611 bool is_trailing_return)
14613 cp_decl_specifier_seq type_specifier_seq;
14614 cp_declarator *abstract_declarator;
14616 /* Parse the type-specifier-seq. */
14617 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14618 is_trailing_return,
14619 &type_specifier_seq);
14620 if (type_specifier_seq.type == error_mark_node)
14621 return error_mark_node;
14623 /* There might or might not be an abstract declarator. */
14624 cp_parser_parse_tentatively (parser);
14625 /* Look for the declarator. */
14626 abstract_declarator
14627 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
14628 /*parenthesized_p=*/NULL,
14629 /*member_p=*/false);
14630 /* Check to see if there really was a declarator. */
14631 if (!cp_parser_parse_definitely (parser))
14632 abstract_declarator = NULL;
14634 if (type_specifier_seq.type
14635 && type_uses_auto (type_specifier_seq.type))
14637 /* A type-id with type 'auto' is only ok if the abstract declarator
14638 is a function declarator with a late-specified return type. */
14639 if (abstract_declarator
14640 && abstract_declarator->kind == cdk_function
14641 && abstract_declarator->u.function.late_return_type)
14642 /* OK */;
14643 else
14645 error ("invalid use of %<auto%>");
14646 return error_mark_node;
14650 return groktypename (&type_specifier_seq, abstract_declarator,
14651 is_template_arg);
14654 static tree cp_parser_type_id (cp_parser *parser)
14656 return cp_parser_type_id_1 (parser, false, false);
14659 static tree cp_parser_template_type_arg (cp_parser *parser)
14661 return cp_parser_type_id_1 (parser, true, false);
14664 static tree cp_parser_trailing_type_id (cp_parser *parser)
14666 return cp_parser_type_id_1 (parser, false, true);
14669 /* Parse a type-specifier-seq.
14671 type-specifier-seq:
14672 type-specifier type-specifier-seq [opt]
14674 GNU extension:
14676 type-specifier-seq:
14677 attributes type-specifier-seq [opt]
14679 If IS_DECLARATION is true, we are at the start of a "condition" or
14680 exception-declaration, so we might be followed by a declarator-id.
14682 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
14683 i.e. we've just seen "->".
14685 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
14687 static void
14688 cp_parser_type_specifier_seq (cp_parser* parser,
14689 bool is_declaration,
14690 bool is_trailing_return,
14691 cp_decl_specifier_seq *type_specifier_seq)
14693 bool seen_type_specifier = false;
14694 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
14695 cp_token *start_token = NULL;
14697 /* Clear the TYPE_SPECIFIER_SEQ. */
14698 clear_decl_specs (type_specifier_seq);
14700 /* In the context of a trailing return type, enum E { } is an
14701 elaborated-type-specifier followed by a function-body, not an
14702 enum-specifier. */
14703 if (is_trailing_return)
14704 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
14706 /* Parse the type-specifiers and attributes. */
14707 while (true)
14709 tree type_specifier;
14710 bool is_cv_qualifier;
14712 /* Check for attributes first. */
14713 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
14715 type_specifier_seq->attributes =
14716 chainon (type_specifier_seq->attributes,
14717 cp_parser_attributes_opt (parser));
14718 continue;
14721 /* record the token of the beginning of the type specifier seq,
14722 for error reporting purposes*/
14723 if (!start_token)
14724 start_token = cp_lexer_peek_token (parser->lexer);
14726 /* Look for the type-specifier. */
14727 type_specifier = cp_parser_type_specifier (parser,
14728 flags,
14729 type_specifier_seq,
14730 /*is_declaration=*/false,
14731 NULL,
14732 &is_cv_qualifier);
14733 if (!type_specifier)
14735 /* If the first type-specifier could not be found, this is not a
14736 type-specifier-seq at all. */
14737 if (!seen_type_specifier)
14739 cp_parser_error (parser, "expected type-specifier");
14740 type_specifier_seq->type = error_mark_node;
14741 return;
14743 /* If subsequent type-specifiers could not be found, the
14744 type-specifier-seq is complete. */
14745 break;
14748 seen_type_specifier = true;
14749 /* The standard says that a condition can be:
14751 type-specifier-seq declarator = assignment-expression
14753 However, given:
14755 struct S {};
14756 if (int S = ...)
14758 we should treat the "S" as a declarator, not as a
14759 type-specifier. The standard doesn't say that explicitly for
14760 type-specifier-seq, but it does say that for
14761 decl-specifier-seq in an ordinary declaration. Perhaps it
14762 would be clearer just to allow a decl-specifier-seq here, and
14763 then add a semantic restriction that if any decl-specifiers
14764 that are not type-specifiers appear, the program is invalid. */
14765 if (is_declaration && !is_cv_qualifier)
14766 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
14769 cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
14772 /* Parse a parameter-declaration-clause.
14774 parameter-declaration-clause:
14775 parameter-declaration-list [opt] ... [opt]
14776 parameter-declaration-list , ...
14778 Returns a representation for the parameter declarations. A return
14779 value of NULL indicates a parameter-declaration-clause consisting
14780 only of an ellipsis. */
14782 static tree
14783 cp_parser_parameter_declaration_clause (cp_parser* parser)
14785 tree parameters;
14786 cp_token *token;
14787 bool ellipsis_p;
14788 bool is_error;
14790 /* Peek at the next token. */
14791 token = cp_lexer_peek_token (parser->lexer);
14792 /* Check for trivial parameter-declaration-clauses. */
14793 if (token->type == CPP_ELLIPSIS)
14795 /* Consume the `...' token. */
14796 cp_lexer_consume_token (parser->lexer);
14797 return NULL_TREE;
14799 else if (token->type == CPP_CLOSE_PAREN)
14800 /* There are no parameters. */
14802 #ifndef NO_IMPLICIT_EXTERN_C
14803 if (in_system_header && current_class_type == NULL
14804 && current_lang_name == lang_name_c)
14805 return NULL_TREE;
14806 else
14807 #endif
14808 return void_list_node;
14810 /* Check for `(void)', too, which is a special case. */
14811 else if (token->keyword == RID_VOID
14812 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
14813 == CPP_CLOSE_PAREN))
14815 /* Consume the `void' token. */
14816 cp_lexer_consume_token (parser->lexer);
14817 /* There are no parameters. */
14818 return void_list_node;
14821 /* Parse the parameter-declaration-list. */
14822 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
14823 /* If a parse error occurred while parsing the
14824 parameter-declaration-list, then the entire
14825 parameter-declaration-clause is erroneous. */
14826 if (is_error)
14827 return NULL;
14829 /* Peek at the next token. */
14830 token = cp_lexer_peek_token (parser->lexer);
14831 /* If it's a `,', the clause should terminate with an ellipsis. */
14832 if (token->type == CPP_COMMA)
14834 /* Consume the `,'. */
14835 cp_lexer_consume_token (parser->lexer);
14836 /* Expect an ellipsis. */
14837 ellipsis_p
14838 = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
14840 /* It might also be `...' if the optional trailing `,' was
14841 omitted. */
14842 else if (token->type == CPP_ELLIPSIS)
14844 /* Consume the `...' token. */
14845 cp_lexer_consume_token (parser->lexer);
14846 /* And remember that we saw it. */
14847 ellipsis_p = true;
14849 else
14850 ellipsis_p = false;
14852 /* Finish the parameter list. */
14853 if (!ellipsis_p)
14854 parameters = chainon (parameters, void_list_node);
14856 return parameters;
14859 /* Parse a parameter-declaration-list.
14861 parameter-declaration-list:
14862 parameter-declaration
14863 parameter-declaration-list , parameter-declaration
14865 Returns a representation of the parameter-declaration-list, as for
14866 cp_parser_parameter_declaration_clause. However, the
14867 `void_list_node' is never appended to the list. Upon return,
14868 *IS_ERROR will be true iff an error occurred. */
14870 static tree
14871 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
14873 tree parameters = NULL_TREE;
14874 tree *tail = &parameters;
14875 bool saved_in_unbraced_linkage_specification_p;
14876 int index = 0;
14878 /* Assume all will go well. */
14879 *is_error = false;
14880 /* The special considerations that apply to a function within an
14881 unbraced linkage specifications do not apply to the parameters
14882 to the function. */
14883 saved_in_unbraced_linkage_specification_p
14884 = parser->in_unbraced_linkage_specification_p;
14885 parser->in_unbraced_linkage_specification_p = false;
14887 /* Look for more parameters. */
14888 while (true)
14890 cp_parameter_declarator *parameter;
14891 tree decl = error_mark_node;
14892 bool parenthesized_p;
14893 /* Parse the parameter. */
14894 parameter
14895 = cp_parser_parameter_declaration (parser,
14896 /*template_parm_p=*/false,
14897 &parenthesized_p);
14899 /* We don't know yet if the enclosing context is deprecated, so wait
14900 and warn in grokparms if appropriate. */
14901 deprecated_state = DEPRECATED_SUPPRESS;
14903 if (parameter)
14904 decl = grokdeclarator (parameter->declarator,
14905 &parameter->decl_specifiers,
14906 PARM,
14907 parameter->default_argument != NULL_TREE,
14908 &parameter->decl_specifiers.attributes);
14910 deprecated_state = DEPRECATED_NORMAL;
14912 /* If a parse error occurred parsing the parameter declaration,
14913 then the entire parameter-declaration-list is erroneous. */
14914 if (decl == error_mark_node)
14916 *is_error = true;
14917 parameters = error_mark_node;
14918 break;
14921 if (parameter->decl_specifiers.attributes)
14922 cplus_decl_attributes (&decl,
14923 parameter->decl_specifiers.attributes,
14925 if (DECL_NAME (decl))
14926 decl = pushdecl (decl);
14928 if (decl != error_mark_node)
14930 retrofit_lang_decl (decl);
14931 DECL_PARM_INDEX (decl) = ++index;
14934 /* Add the new parameter to the list. */
14935 *tail = build_tree_list (parameter->default_argument, decl);
14936 tail = &TREE_CHAIN (*tail);
14938 /* Peek at the next token. */
14939 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
14940 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
14941 /* These are for Objective-C++ */
14942 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14943 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14944 /* The parameter-declaration-list is complete. */
14945 break;
14946 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14948 cp_token *token;
14950 /* Peek at the next token. */
14951 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14952 /* If it's an ellipsis, then the list is complete. */
14953 if (token->type == CPP_ELLIPSIS)
14954 break;
14955 /* Otherwise, there must be more parameters. Consume the
14956 `,'. */
14957 cp_lexer_consume_token (parser->lexer);
14958 /* When parsing something like:
14960 int i(float f, double d)
14962 we can tell after seeing the declaration for "f" that we
14963 are not looking at an initialization of a variable "i",
14964 but rather at the declaration of a function "i".
14966 Due to the fact that the parsing of template arguments
14967 (as specified to a template-id) requires backtracking we
14968 cannot use this technique when inside a template argument
14969 list. */
14970 if (!parser->in_template_argument_list_p
14971 && !parser->in_type_id_in_expr_p
14972 && cp_parser_uncommitted_to_tentative_parse_p (parser)
14973 /* However, a parameter-declaration of the form
14974 "foat(f)" (which is a valid declaration of a
14975 parameter "f") can also be interpreted as an
14976 expression (the conversion of "f" to "float"). */
14977 && !parenthesized_p)
14978 cp_parser_commit_to_tentative_parse (parser);
14980 else
14982 cp_parser_error (parser, "expected %<,%> or %<...%>");
14983 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14984 cp_parser_skip_to_closing_parenthesis (parser,
14985 /*recovering=*/true,
14986 /*or_comma=*/false,
14987 /*consume_paren=*/false);
14988 break;
14992 parser->in_unbraced_linkage_specification_p
14993 = saved_in_unbraced_linkage_specification_p;
14995 return parameters;
14998 /* Parse a parameter declaration.
15000 parameter-declaration:
15001 decl-specifier-seq ... [opt] declarator
15002 decl-specifier-seq declarator = assignment-expression
15003 decl-specifier-seq ... [opt] abstract-declarator [opt]
15004 decl-specifier-seq abstract-declarator [opt] = assignment-expression
15006 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
15007 declares a template parameter. (In that case, a non-nested `>'
15008 token encountered during the parsing of the assignment-expression
15009 is not interpreted as a greater-than operator.)
15011 Returns a representation of the parameter, or NULL if an error
15012 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
15013 true iff the declarator is of the form "(p)". */
15015 static cp_parameter_declarator *
15016 cp_parser_parameter_declaration (cp_parser *parser,
15017 bool template_parm_p,
15018 bool *parenthesized_p)
15020 int declares_class_or_enum;
15021 cp_decl_specifier_seq decl_specifiers;
15022 cp_declarator *declarator;
15023 tree default_argument;
15024 cp_token *token = NULL, *declarator_token_start = NULL;
15025 const char *saved_message;
15027 /* In a template parameter, `>' is not an operator.
15029 [temp.param]
15031 When parsing a default template-argument for a non-type
15032 template-parameter, the first non-nested `>' is taken as the end
15033 of the template parameter-list rather than a greater-than
15034 operator. */
15036 /* Type definitions may not appear in parameter types. */
15037 saved_message = parser->type_definition_forbidden_message;
15038 parser->type_definition_forbidden_message
15039 = G_("types may not be defined in parameter types");
15041 /* Parse the declaration-specifiers. */
15042 cp_parser_decl_specifier_seq (parser,
15043 CP_PARSER_FLAGS_NONE,
15044 &decl_specifiers,
15045 &declares_class_or_enum);
15047 /* Complain about missing 'typename' or other invalid type names. */
15048 if (!decl_specifiers.any_type_specifiers_p)
15049 cp_parser_parse_and_diagnose_invalid_type_name (parser);
15051 /* If an error occurred, there's no reason to attempt to parse the
15052 rest of the declaration. */
15053 if (cp_parser_error_occurred (parser))
15055 parser->type_definition_forbidden_message = saved_message;
15056 return NULL;
15059 /* Peek at the next token. */
15060 token = cp_lexer_peek_token (parser->lexer);
15062 /* If the next token is a `)', `,', `=', `>', or `...', then there
15063 is no declarator. However, when variadic templates are enabled,
15064 there may be a declarator following `...'. */
15065 if (token->type == CPP_CLOSE_PAREN
15066 || token->type == CPP_COMMA
15067 || token->type == CPP_EQ
15068 || token->type == CPP_GREATER)
15070 declarator = NULL;
15071 if (parenthesized_p)
15072 *parenthesized_p = false;
15074 /* Otherwise, there should be a declarator. */
15075 else
15077 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
15078 parser->default_arg_ok_p = false;
15080 /* After seeing a decl-specifier-seq, if the next token is not a
15081 "(", there is no possibility that the code is a valid
15082 expression. Therefore, if parsing tentatively, we commit at
15083 this point. */
15084 if (!parser->in_template_argument_list_p
15085 /* In an expression context, having seen:
15087 (int((char ...
15089 we cannot be sure whether we are looking at a
15090 function-type (taking a "char" as a parameter) or a cast
15091 of some object of type "char" to "int". */
15092 && !parser->in_type_id_in_expr_p
15093 && cp_parser_uncommitted_to_tentative_parse_p (parser)
15094 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
15095 cp_parser_commit_to_tentative_parse (parser);
15096 /* Parse the declarator. */
15097 declarator_token_start = token;
15098 declarator = cp_parser_declarator (parser,
15099 CP_PARSER_DECLARATOR_EITHER,
15100 /*ctor_dtor_or_conv_p=*/NULL,
15101 parenthesized_p,
15102 /*member_p=*/false);
15103 parser->default_arg_ok_p = saved_default_arg_ok_p;
15104 /* After the declarator, allow more attributes. */
15105 decl_specifiers.attributes
15106 = chainon (decl_specifiers.attributes,
15107 cp_parser_attributes_opt (parser));
15110 /* If the next token is an ellipsis, and we have not seen a
15111 declarator name, and the type of the declarator contains parameter
15112 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
15113 a parameter pack expansion expression. Otherwise, leave the
15114 ellipsis for a C-style variadic function. */
15115 token = cp_lexer_peek_token (parser->lexer);
15116 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15118 tree type = decl_specifiers.type;
15120 if (type && DECL_P (type))
15121 type = TREE_TYPE (type);
15123 if (type
15124 && TREE_CODE (type) != TYPE_PACK_EXPANSION
15125 && declarator_can_be_parameter_pack (declarator)
15126 && (!declarator || !declarator->parameter_pack_p)
15127 && uses_parameter_packs (type))
15129 /* Consume the `...'. */
15130 cp_lexer_consume_token (parser->lexer);
15131 maybe_warn_variadic_templates ();
15133 /* Build a pack expansion type */
15134 if (declarator)
15135 declarator->parameter_pack_p = true;
15136 else
15137 decl_specifiers.type = make_pack_expansion (type);
15141 /* The restriction on defining new types applies only to the type
15142 of the parameter, not to the default argument. */
15143 parser->type_definition_forbidden_message = saved_message;
15145 /* If the next token is `=', then process a default argument. */
15146 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15148 /* Consume the `='. */
15149 cp_lexer_consume_token (parser->lexer);
15151 /* If we are defining a class, then the tokens that make up the
15152 default argument must be saved and processed later. */
15153 if (!template_parm_p && at_class_scope_p ()
15154 && TYPE_BEING_DEFINED (current_class_type)
15155 && !LAMBDA_TYPE_P (current_class_type))
15157 unsigned depth = 0;
15158 int maybe_template_id = 0;
15159 cp_token *first_token;
15160 cp_token *token;
15162 /* Add tokens until we have processed the entire default
15163 argument. We add the range [first_token, token). */
15164 first_token = cp_lexer_peek_token (parser->lexer);
15165 while (true)
15167 bool done = false;
15169 /* Peek at the next token. */
15170 token = cp_lexer_peek_token (parser->lexer);
15171 /* What we do depends on what token we have. */
15172 switch (token->type)
15174 /* In valid code, a default argument must be
15175 immediately followed by a `,' `)', or `...'. */
15176 case CPP_COMMA:
15177 if (depth == 0 && maybe_template_id)
15179 /* If we've seen a '<', we might be in a
15180 template-argument-list. Until Core issue 325 is
15181 resolved, we don't know how this situation ought
15182 to be handled, so try to DTRT. We check whether
15183 what comes after the comma is a valid parameter
15184 declaration list. If it is, then the comma ends
15185 the default argument; otherwise the default
15186 argument continues. */
15187 bool error = false;
15189 /* Set ITALP so cp_parser_parameter_declaration_list
15190 doesn't decide to commit to this parse. */
15191 bool saved_italp = parser->in_template_argument_list_p;
15192 parser->in_template_argument_list_p = true;
15194 cp_parser_parse_tentatively (parser);
15195 cp_lexer_consume_token (parser->lexer);
15196 cp_parser_parameter_declaration_list (parser, &error);
15197 if (!cp_parser_error_occurred (parser) && !error)
15198 done = true;
15199 cp_parser_abort_tentative_parse (parser);
15201 parser->in_template_argument_list_p = saved_italp;
15202 break;
15204 case CPP_CLOSE_PAREN:
15205 case CPP_ELLIPSIS:
15206 /* If we run into a non-nested `;', `}', or `]',
15207 then the code is invalid -- but the default
15208 argument is certainly over. */
15209 case CPP_SEMICOLON:
15210 case CPP_CLOSE_BRACE:
15211 case CPP_CLOSE_SQUARE:
15212 if (depth == 0)
15213 done = true;
15214 /* Update DEPTH, if necessary. */
15215 else if (token->type == CPP_CLOSE_PAREN
15216 || token->type == CPP_CLOSE_BRACE
15217 || token->type == CPP_CLOSE_SQUARE)
15218 --depth;
15219 break;
15221 case CPP_OPEN_PAREN:
15222 case CPP_OPEN_SQUARE:
15223 case CPP_OPEN_BRACE:
15224 ++depth;
15225 break;
15227 case CPP_LESS:
15228 if (depth == 0)
15229 /* This might be the comparison operator, or it might
15230 start a template argument list. */
15231 ++maybe_template_id;
15232 break;
15234 case CPP_RSHIFT:
15235 if (cxx_dialect == cxx98)
15236 break;
15237 /* Fall through for C++0x, which treats the `>>'
15238 operator like two `>' tokens in certain
15239 cases. */
15241 case CPP_GREATER:
15242 if (depth == 0)
15244 /* This might be an operator, or it might close a
15245 template argument list. But if a previous '<'
15246 started a template argument list, this will have
15247 closed it, so we can't be in one anymore. */
15248 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
15249 if (maybe_template_id < 0)
15250 maybe_template_id = 0;
15252 break;
15254 /* If we run out of tokens, issue an error message. */
15255 case CPP_EOF:
15256 case CPP_PRAGMA_EOL:
15257 error_at (token->location, "file ends in default argument");
15258 done = true;
15259 break;
15261 case CPP_NAME:
15262 case CPP_SCOPE:
15263 /* In these cases, we should look for template-ids.
15264 For example, if the default argument is
15265 `X<int, double>()', we need to do name lookup to
15266 figure out whether or not `X' is a template; if
15267 so, the `,' does not end the default argument.
15269 That is not yet done. */
15270 break;
15272 default:
15273 break;
15276 /* If we've reached the end, stop. */
15277 if (done)
15278 break;
15280 /* Add the token to the token block. */
15281 token = cp_lexer_consume_token (parser->lexer);
15284 /* Create a DEFAULT_ARG to represent the unparsed default
15285 argument. */
15286 default_argument = make_node (DEFAULT_ARG);
15287 DEFARG_TOKENS (default_argument)
15288 = cp_token_cache_new (first_token, token);
15289 DEFARG_INSTANTIATIONS (default_argument) = NULL;
15291 /* Outside of a class definition, we can just parse the
15292 assignment-expression. */
15293 else
15295 token = cp_lexer_peek_token (parser->lexer);
15296 default_argument
15297 = cp_parser_default_argument (parser, template_parm_p);
15300 if (!parser->default_arg_ok_p)
15302 if (flag_permissive)
15303 warning (0, "deprecated use of default argument for parameter of non-function");
15304 else
15306 error_at (token->location,
15307 "default arguments are only "
15308 "permitted for function parameters");
15309 default_argument = NULL_TREE;
15312 else if ((declarator && declarator->parameter_pack_p)
15313 || (decl_specifiers.type
15314 && PACK_EXPANSION_P (decl_specifiers.type)))
15316 /* Find the name of the parameter pack. */
15317 cp_declarator *id_declarator = declarator;
15318 while (id_declarator && id_declarator->kind != cdk_id)
15319 id_declarator = id_declarator->declarator;
15321 if (id_declarator && id_declarator->kind == cdk_id)
15322 error_at (declarator_token_start->location,
15323 template_parm_p
15324 ? "template parameter pack %qD"
15325 " cannot have a default argument"
15326 : "parameter pack %qD cannot have a default argument",
15327 id_declarator->u.id.unqualified_name);
15328 else
15329 error_at (declarator_token_start->location,
15330 template_parm_p
15331 ? "template parameter pack cannot have a default argument"
15332 : "parameter pack cannot have a default argument");
15334 default_argument = NULL_TREE;
15337 else
15338 default_argument = NULL_TREE;
15340 return make_parameter_declarator (&decl_specifiers,
15341 declarator,
15342 default_argument);
15345 /* Parse a default argument and return it.
15347 TEMPLATE_PARM_P is true if this is a default argument for a
15348 non-type template parameter. */
15349 static tree
15350 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
15352 tree default_argument = NULL_TREE;
15353 bool saved_greater_than_is_operator_p;
15354 bool saved_local_variables_forbidden_p;
15356 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
15357 set correctly. */
15358 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
15359 parser->greater_than_is_operator_p = !template_parm_p;
15360 /* Local variable names (and the `this' keyword) may not
15361 appear in a default argument. */
15362 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15363 parser->local_variables_forbidden_p = true;
15364 /* Parse the assignment-expression. */
15365 if (template_parm_p)
15366 push_deferring_access_checks (dk_no_deferred);
15367 default_argument
15368 = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
15369 if (template_parm_p)
15370 pop_deferring_access_checks ();
15371 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
15372 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15374 return default_argument;
15377 /* Parse a function-body.
15379 function-body:
15380 compound_statement */
15382 static void
15383 cp_parser_function_body (cp_parser *parser)
15385 cp_parser_compound_statement (parser, NULL, false);
15388 /* Parse a ctor-initializer-opt followed by a function-body. Return
15389 true if a ctor-initializer was present. */
15391 static bool
15392 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
15394 tree body;
15395 bool ctor_initializer_p;
15397 /* Begin the function body. */
15398 body = begin_function_body ();
15399 /* Parse the optional ctor-initializer. */
15400 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
15401 /* Parse the function-body. */
15402 cp_parser_function_body (parser);
15403 /* Finish the function body. */
15404 finish_function_body (body);
15406 return ctor_initializer_p;
15409 /* Parse an initializer.
15411 initializer:
15412 = initializer-clause
15413 ( expression-list )
15415 Returns an expression representing the initializer. If no
15416 initializer is present, NULL_TREE is returned.
15418 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
15419 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
15420 set to TRUE if there is no initializer present. If there is an
15421 initializer, and it is not a constant-expression, *NON_CONSTANT_P
15422 is set to true; otherwise it is set to false. */
15424 static tree
15425 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
15426 bool* non_constant_p)
15428 cp_token *token;
15429 tree init;
15431 /* Peek at the next token. */
15432 token = cp_lexer_peek_token (parser->lexer);
15434 /* Let our caller know whether or not this initializer was
15435 parenthesized. */
15436 *is_direct_init = (token->type != CPP_EQ);
15437 /* Assume that the initializer is constant. */
15438 *non_constant_p = false;
15440 if (token->type == CPP_EQ)
15442 /* Consume the `='. */
15443 cp_lexer_consume_token (parser->lexer);
15444 /* Parse the initializer-clause. */
15445 init = cp_parser_initializer_clause (parser, non_constant_p);
15447 else if (token->type == CPP_OPEN_PAREN)
15449 VEC(tree,gc) *vec;
15450 vec = cp_parser_parenthesized_expression_list (parser, false,
15451 /*cast_p=*/false,
15452 /*allow_expansion_p=*/true,
15453 non_constant_p);
15454 if (vec == NULL)
15455 return error_mark_node;
15456 init = build_tree_list_vec (vec);
15457 release_tree_vector (vec);
15459 else if (token->type == CPP_OPEN_BRACE)
15461 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
15462 init = cp_parser_braced_list (parser, non_constant_p);
15463 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
15465 else
15467 /* Anything else is an error. */
15468 cp_parser_error (parser, "expected initializer");
15469 init = error_mark_node;
15472 return init;
15475 /* Parse an initializer-clause.
15477 initializer-clause:
15478 assignment-expression
15479 braced-init-list
15481 Returns an expression representing the initializer.
15483 If the `assignment-expression' production is used the value
15484 returned is simply a representation for the expression.
15486 Otherwise, calls cp_parser_braced_list. */
15488 static tree
15489 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
15491 tree initializer;
15493 /* Assume the expression is constant. */
15494 *non_constant_p = false;
15496 /* If it is not a `{', then we are looking at an
15497 assignment-expression. */
15498 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
15500 initializer
15501 = cp_parser_constant_expression (parser,
15502 /*allow_non_constant_p=*/true,
15503 non_constant_p);
15504 if (!*non_constant_p)
15505 initializer = fold_non_dependent_expr (initializer);
15507 else
15508 initializer = cp_parser_braced_list (parser, non_constant_p);
15510 return initializer;
15513 /* Parse a brace-enclosed initializer list.
15515 braced-init-list:
15516 { initializer-list , [opt] }
15519 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
15520 the elements of the initializer-list (or NULL, if the last
15521 production is used). The TREE_TYPE for the CONSTRUCTOR will be
15522 NULL_TREE. There is no way to detect whether or not the optional
15523 trailing `,' was provided. NON_CONSTANT_P is as for
15524 cp_parser_initializer. */
15526 static tree
15527 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
15529 tree initializer;
15531 /* Consume the `{' token. */
15532 cp_lexer_consume_token (parser->lexer);
15533 /* Create a CONSTRUCTOR to represent the braced-initializer. */
15534 initializer = make_node (CONSTRUCTOR);
15535 /* If it's not a `}', then there is a non-trivial initializer. */
15536 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
15538 /* Parse the initializer list. */
15539 CONSTRUCTOR_ELTS (initializer)
15540 = cp_parser_initializer_list (parser, non_constant_p);
15541 /* A trailing `,' token is allowed. */
15542 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15543 cp_lexer_consume_token (parser->lexer);
15545 /* Now, there should be a trailing `}'. */
15546 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15547 TREE_TYPE (initializer) = init_list_type_node;
15548 return initializer;
15551 /* Parse an initializer-list.
15553 initializer-list:
15554 initializer-clause ... [opt]
15555 initializer-list , initializer-clause ... [opt]
15557 GNU Extension:
15559 initializer-list:
15560 identifier : initializer-clause
15561 initializer-list, identifier : initializer-clause
15563 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
15564 for the initializer. If the INDEX of the elt is non-NULL, it is the
15565 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
15566 as for cp_parser_initializer. */
15568 static VEC(constructor_elt,gc) *
15569 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
15571 VEC(constructor_elt,gc) *v = NULL;
15573 /* Assume all of the expressions are constant. */
15574 *non_constant_p = false;
15576 /* Parse the rest of the list. */
15577 while (true)
15579 cp_token *token;
15580 tree identifier;
15581 tree initializer;
15582 bool clause_non_constant_p;
15584 /* If the next token is an identifier and the following one is a
15585 colon, we are looking at the GNU designated-initializer
15586 syntax. */
15587 if (cp_parser_allow_gnu_extensions_p (parser)
15588 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
15589 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
15591 /* Warn the user that they are using an extension. */
15592 pedwarn (input_location, OPT_pedantic,
15593 "ISO C++ does not allow designated initializers");
15594 /* Consume the identifier. */
15595 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
15596 /* Consume the `:'. */
15597 cp_lexer_consume_token (parser->lexer);
15599 else
15600 identifier = NULL_TREE;
15602 /* Parse the initializer. */
15603 initializer = cp_parser_initializer_clause (parser,
15604 &clause_non_constant_p);
15605 /* If any clause is non-constant, so is the entire initializer. */
15606 if (clause_non_constant_p)
15607 *non_constant_p = true;
15609 /* If we have an ellipsis, this is an initializer pack
15610 expansion. */
15611 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15613 /* Consume the `...'. */
15614 cp_lexer_consume_token (parser->lexer);
15616 /* Turn the initializer into an initializer expansion. */
15617 initializer = make_pack_expansion (initializer);
15620 /* Add it to the vector. */
15621 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
15623 /* If the next token is not a comma, we have reached the end of
15624 the list. */
15625 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15626 break;
15628 /* Peek at the next token. */
15629 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15630 /* If the next token is a `}', then we're still done. An
15631 initializer-clause can have a trailing `,' after the
15632 initializer-list and before the closing `}'. */
15633 if (token->type == CPP_CLOSE_BRACE)
15634 break;
15636 /* Consume the `,' token. */
15637 cp_lexer_consume_token (parser->lexer);
15640 return v;
15643 /* Classes [gram.class] */
15645 /* Parse a class-name.
15647 class-name:
15648 identifier
15649 template-id
15651 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
15652 to indicate that names looked up in dependent types should be
15653 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
15654 keyword has been used to indicate that the name that appears next
15655 is a template. TAG_TYPE indicates the explicit tag given before
15656 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
15657 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
15658 is the class being defined in a class-head.
15660 Returns the TYPE_DECL representing the class. */
15662 static tree
15663 cp_parser_class_name (cp_parser *parser,
15664 bool typename_keyword_p,
15665 bool template_keyword_p,
15666 enum tag_types tag_type,
15667 bool check_dependency_p,
15668 bool class_head_p,
15669 bool is_declaration)
15671 tree decl;
15672 tree scope;
15673 bool typename_p;
15674 cp_token *token;
15675 tree identifier = NULL_TREE;
15677 /* All class-names start with an identifier. */
15678 token = cp_lexer_peek_token (parser->lexer);
15679 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
15681 cp_parser_error (parser, "expected class-name");
15682 return error_mark_node;
15685 /* PARSER->SCOPE can be cleared when parsing the template-arguments
15686 to a template-id, so we save it here. */
15687 scope = parser->scope;
15688 if (scope == error_mark_node)
15689 return error_mark_node;
15691 /* Any name names a type if we're following the `typename' keyword
15692 in a qualified name where the enclosing scope is type-dependent. */
15693 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
15694 && dependent_type_p (scope));
15695 /* Handle the common case (an identifier, but not a template-id)
15696 efficiently. */
15697 if (token->type == CPP_NAME
15698 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
15700 cp_token *identifier_token;
15701 bool ambiguous_p;
15703 /* Look for the identifier. */
15704 identifier_token = cp_lexer_peek_token (parser->lexer);
15705 ambiguous_p = identifier_token->ambiguous_p;
15706 identifier = cp_parser_identifier (parser);
15707 /* If the next token isn't an identifier, we are certainly not
15708 looking at a class-name. */
15709 if (identifier == error_mark_node)
15710 decl = error_mark_node;
15711 /* If we know this is a type-name, there's no need to look it
15712 up. */
15713 else if (typename_p)
15714 decl = identifier;
15715 else
15717 tree ambiguous_decls;
15718 /* If we already know that this lookup is ambiguous, then
15719 we've already issued an error message; there's no reason
15720 to check again. */
15721 if (ambiguous_p)
15723 cp_parser_simulate_error (parser);
15724 return error_mark_node;
15726 /* If the next token is a `::', then the name must be a type
15727 name.
15729 [basic.lookup.qual]
15731 During the lookup for a name preceding the :: scope
15732 resolution operator, object, function, and enumerator
15733 names are ignored. */
15734 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15735 tag_type = typename_type;
15736 /* Look up the name. */
15737 decl = cp_parser_lookup_name (parser, identifier,
15738 tag_type,
15739 /*is_template=*/false,
15740 /*is_namespace=*/false,
15741 check_dependency_p,
15742 &ambiguous_decls,
15743 identifier_token->location);
15744 if (ambiguous_decls)
15746 if (cp_parser_parsing_tentatively (parser))
15747 cp_parser_simulate_error (parser);
15748 return error_mark_node;
15752 else
15754 /* Try a template-id. */
15755 decl = cp_parser_template_id (parser, template_keyword_p,
15756 check_dependency_p,
15757 is_declaration);
15758 if (decl == error_mark_node)
15759 return error_mark_node;
15762 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
15764 /* If this is a typename, create a TYPENAME_TYPE. */
15765 if (typename_p && decl != error_mark_node)
15767 decl = make_typename_type (scope, decl, typename_type,
15768 /*complain=*/tf_error);
15769 if (decl != error_mark_node)
15770 decl = TYPE_NAME (decl);
15773 /* Check to see that it is really the name of a class. */
15774 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15775 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
15776 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15777 /* Situations like this:
15779 template <typename T> struct A {
15780 typename T::template X<int>::I i;
15783 are problematic. Is `T::template X<int>' a class-name? The
15784 standard does not seem to be definitive, but there is no other
15785 valid interpretation of the following `::'. Therefore, those
15786 names are considered class-names. */
15788 decl = make_typename_type (scope, decl, tag_type, tf_error);
15789 if (decl != error_mark_node)
15790 decl = TYPE_NAME (decl);
15792 else if (TREE_CODE (decl) != TYPE_DECL
15793 || TREE_TYPE (decl) == error_mark_node
15794 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
15795 decl = error_mark_node;
15797 if (decl == error_mark_node)
15798 cp_parser_error (parser, "expected class-name");
15799 else if (identifier && !parser->scope)
15800 maybe_note_name_used_in_class (identifier, decl);
15802 return decl;
15805 /* Parse a class-specifier.
15807 class-specifier:
15808 class-head { member-specification [opt] }
15810 Returns the TREE_TYPE representing the class. */
15812 static tree
15813 cp_parser_class_specifier (cp_parser* parser)
15815 tree type;
15816 tree attributes = NULL_TREE;
15817 bool nested_name_specifier_p;
15818 unsigned saved_num_template_parameter_lists;
15819 bool saved_in_function_body;
15820 bool saved_in_unbraced_linkage_specification_p;
15821 tree old_scope = NULL_TREE;
15822 tree scope = NULL_TREE;
15823 tree bases;
15825 push_deferring_access_checks (dk_no_deferred);
15827 /* Parse the class-head. */
15828 type = cp_parser_class_head (parser,
15829 &nested_name_specifier_p,
15830 &attributes,
15831 &bases);
15832 /* If the class-head was a semantic disaster, skip the entire body
15833 of the class. */
15834 if (!type)
15836 cp_parser_skip_to_end_of_block_or_statement (parser);
15837 pop_deferring_access_checks ();
15838 return error_mark_node;
15841 /* Look for the `{'. */
15842 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
15844 pop_deferring_access_checks ();
15845 return error_mark_node;
15848 /* Process the base classes. If they're invalid, skip the
15849 entire class body. */
15850 if (!xref_basetypes (type, bases))
15852 /* Consuming the closing brace yields better error messages
15853 later on. */
15854 if (cp_parser_skip_to_closing_brace (parser))
15855 cp_lexer_consume_token (parser->lexer);
15856 pop_deferring_access_checks ();
15857 return error_mark_node;
15860 /* Issue an error message if type-definitions are forbidden here. */
15861 cp_parser_check_type_definition (parser);
15862 /* Remember that we are defining one more class. */
15863 ++parser->num_classes_being_defined;
15864 /* Inside the class, surrounding template-parameter-lists do not
15865 apply. */
15866 saved_num_template_parameter_lists
15867 = parser->num_template_parameter_lists;
15868 parser->num_template_parameter_lists = 0;
15869 /* We are not in a function body. */
15870 saved_in_function_body = parser->in_function_body;
15871 parser->in_function_body = false;
15872 /* We are not immediately inside an extern "lang" block. */
15873 saved_in_unbraced_linkage_specification_p
15874 = parser->in_unbraced_linkage_specification_p;
15875 parser->in_unbraced_linkage_specification_p = false;
15877 /* Start the class. */
15878 if (nested_name_specifier_p)
15880 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
15881 old_scope = push_inner_scope (scope);
15883 type = begin_class_definition (type, attributes);
15885 if (type == error_mark_node)
15886 /* If the type is erroneous, skip the entire body of the class. */
15887 cp_parser_skip_to_closing_brace (parser);
15888 else
15889 /* Parse the member-specification. */
15890 cp_parser_member_specification_opt (parser);
15892 /* Look for the trailing `}'. */
15893 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15894 /* Look for trailing attributes to apply to this class. */
15895 if (cp_parser_allow_gnu_extensions_p (parser))
15896 attributes = cp_parser_attributes_opt (parser);
15897 if (type != error_mark_node)
15898 type = finish_struct (type, attributes);
15899 if (nested_name_specifier_p)
15900 pop_inner_scope (old_scope, scope);
15901 /* If this class is not itself within the scope of another class,
15902 then we need to parse the bodies of all of the queued function
15903 definitions. Note that the queued functions defined in a class
15904 are not always processed immediately following the
15905 class-specifier for that class. Consider:
15907 struct A {
15908 struct B { void f() { sizeof (A); } };
15911 If `f' were processed before the processing of `A' were
15912 completed, there would be no way to compute the size of `A'.
15913 Note that the nesting we are interested in here is lexical --
15914 not the semantic nesting given by TYPE_CONTEXT. In particular,
15915 for:
15917 struct A { struct B; };
15918 struct A::B { void f() { } };
15920 there is no need to delay the parsing of `A::B::f'. */
15921 if (--parser->num_classes_being_defined == 0)
15923 tree queue_entry;
15924 tree fn;
15925 tree class_type = NULL_TREE;
15926 tree pushed_scope = NULL_TREE;
15928 /* In a first pass, parse default arguments to the functions.
15929 Then, in a second pass, parse the bodies of the functions.
15930 This two-phased approach handles cases like:
15932 struct S {
15933 void f() { g(); }
15934 void g(int i = 3);
15938 for (TREE_PURPOSE (parser->unparsed_functions_queues)
15939 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
15940 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
15941 TREE_PURPOSE (parser->unparsed_functions_queues)
15942 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
15944 fn = TREE_VALUE (queue_entry);
15945 /* If there are default arguments that have not yet been processed,
15946 take care of them now. */
15947 if (class_type != TREE_PURPOSE (queue_entry))
15949 if (pushed_scope)
15950 pop_scope (pushed_scope);
15951 class_type = TREE_PURPOSE (queue_entry);
15952 pushed_scope = push_scope (class_type);
15954 /* Make sure that any template parameters are in scope. */
15955 maybe_begin_member_template_processing (fn);
15956 /* Parse the default argument expressions. */
15957 cp_parser_late_parsing_default_args (parser, fn);
15958 /* Remove any template parameters from the symbol table. */
15959 maybe_end_member_template_processing ();
15961 if (pushed_scope)
15962 pop_scope (pushed_scope);
15963 /* Now parse the body of the functions. */
15964 for (TREE_VALUE (parser->unparsed_functions_queues)
15965 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
15966 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
15967 TREE_VALUE (parser->unparsed_functions_queues)
15968 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
15970 /* Figure out which function we need to process. */
15971 fn = TREE_VALUE (queue_entry);
15972 /* Parse the function. */
15973 cp_parser_late_parsing_for_member (parser, fn);
15977 /* Put back any saved access checks. */
15978 pop_deferring_access_checks ();
15980 /* Restore saved state. */
15981 parser->in_function_body = saved_in_function_body;
15982 parser->num_template_parameter_lists
15983 = saved_num_template_parameter_lists;
15984 parser->in_unbraced_linkage_specification_p
15985 = saved_in_unbraced_linkage_specification_p;
15987 return type;
15990 /* Parse a class-head.
15992 class-head:
15993 class-key identifier [opt] base-clause [opt]
15994 class-key nested-name-specifier identifier base-clause [opt]
15995 class-key nested-name-specifier [opt] template-id
15996 base-clause [opt]
15998 GNU Extensions:
15999 class-key attributes identifier [opt] base-clause [opt]
16000 class-key attributes nested-name-specifier identifier base-clause [opt]
16001 class-key attributes nested-name-specifier [opt] template-id
16002 base-clause [opt]
16004 Upon return BASES is initialized to the list of base classes (or
16005 NULL, if there are none) in the same form returned by
16006 cp_parser_base_clause.
16008 Returns the TYPE of the indicated class. Sets
16009 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
16010 involving a nested-name-specifier was used, and FALSE otherwise.
16012 Returns error_mark_node if this is not a class-head.
16014 Returns NULL_TREE if the class-head is syntactically valid, but
16015 semantically invalid in a way that means we should skip the entire
16016 body of the class. */
16018 static tree
16019 cp_parser_class_head (cp_parser* parser,
16020 bool* nested_name_specifier_p,
16021 tree *attributes_p,
16022 tree *bases)
16024 tree nested_name_specifier;
16025 enum tag_types class_key;
16026 tree id = NULL_TREE;
16027 tree type = NULL_TREE;
16028 tree attributes;
16029 bool template_id_p = false;
16030 bool qualified_p = false;
16031 bool invalid_nested_name_p = false;
16032 bool invalid_explicit_specialization_p = false;
16033 tree pushed_scope = NULL_TREE;
16034 unsigned num_templates;
16035 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
16036 /* Assume no nested-name-specifier will be present. */
16037 *nested_name_specifier_p = false;
16038 /* Assume no template parameter lists will be used in defining the
16039 type. */
16040 num_templates = 0;
16042 *bases = NULL_TREE;
16044 /* Look for the class-key. */
16045 class_key = cp_parser_class_key (parser);
16046 if (class_key == none_type)
16047 return error_mark_node;
16049 /* Parse the attributes. */
16050 attributes = cp_parser_attributes_opt (parser);
16052 /* If the next token is `::', that is invalid -- but sometimes
16053 people do try to write:
16055 struct ::S {};
16057 Handle this gracefully by accepting the extra qualifier, and then
16058 issuing an error about it later if this really is a
16059 class-head. If it turns out just to be an elaborated type
16060 specifier, remain silent. */
16061 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
16062 qualified_p = true;
16064 push_deferring_access_checks (dk_no_check);
16066 /* Determine the name of the class. Begin by looking for an
16067 optional nested-name-specifier. */
16068 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
16069 nested_name_specifier
16070 = cp_parser_nested_name_specifier_opt (parser,
16071 /*typename_keyword_p=*/false,
16072 /*check_dependency_p=*/false,
16073 /*type_p=*/false,
16074 /*is_declaration=*/false);
16075 /* If there was a nested-name-specifier, then there *must* be an
16076 identifier. */
16077 if (nested_name_specifier)
16079 type_start_token = cp_lexer_peek_token (parser->lexer);
16080 /* Although the grammar says `identifier', it really means
16081 `class-name' or `template-name'. You are only allowed to
16082 define a class that has already been declared with this
16083 syntax.
16085 The proposed resolution for Core Issue 180 says that wherever
16086 you see `class T::X' you should treat `X' as a type-name.
16088 It is OK to define an inaccessible class; for example:
16090 class A { class B; };
16091 class A::B {};
16093 We do not know if we will see a class-name, or a
16094 template-name. We look for a class-name first, in case the
16095 class-name is a template-id; if we looked for the
16096 template-name first we would stop after the template-name. */
16097 cp_parser_parse_tentatively (parser);
16098 type = cp_parser_class_name (parser,
16099 /*typename_keyword_p=*/false,
16100 /*template_keyword_p=*/false,
16101 class_type,
16102 /*check_dependency_p=*/false,
16103 /*class_head_p=*/true,
16104 /*is_declaration=*/false);
16105 /* If that didn't work, ignore the nested-name-specifier. */
16106 if (!cp_parser_parse_definitely (parser))
16108 invalid_nested_name_p = true;
16109 type_start_token = cp_lexer_peek_token (parser->lexer);
16110 id = cp_parser_identifier (parser);
16111 if (id == error_mark_node)
16112 id = NULL_TREE;
16114 /* If we could not find a corresponding TYPE, treat this
16115 declaration like an unqualified declaration. */
16116 if (type == error_mark_node)
16117 nested_name_specifier = NULL_TREE;
16118 /* Otherwise, count the number of templates used in TYPE and its
16119 containing scopes. */
16120 else
16122 tree scope;
16124 for (scope = TREE_TYPE (type);
16125 scope && TREE_CODE (scope) != NAMESPACE_DECL;
16126 scope = (TYPE_P (scope)
16127 ? TYPE_CONTEXT (scope)
16128 : DECL_CONTEXT (scope)))
16129 if (TYPE_P (scope)
16130 && CLASS_TYPE_P (scope)
16131 && CLASSTYPE_TEMPLATE_INFO (scope)
16132 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
16133 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
16134 ++num_templates;
16137 /* Otherwise, the identifier is optional. */
16138 else
16140 /* We don't know whether what comes next is a template-id,
16141 an identifier, or nothing at all. */
16142 cp_parser_parse_tentatively (parser);
16143 /* Check for a template-id. */
16144 type_start_token = cp_lexer_peek_token (parser->lexer);
16145 id = cp_parser_template_id (parser,
16146 /*template_keyword_p=*/false,
16147 /*check_dependency_p=*/true,
16148 /*is_declaration=*/true);
16149 /* If that didn't work, it could still be an identifier. */
16150 if (!cp_parser_parse_definitely (parser))
16152 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16154 type_start_token = cp_lexer_peek_token (parser->lexer);
16155 id = cp_parser_identifier (parser);
16157 else
16158 id = NULL_TREE;
16160 else
16162 template_id_p = true;
16163 ++num_templates;
16167 pop_deferring_access_checks ();
16169 if (id)
16170 cp_parser_check_for_invalid_template_id (parser, id,
16171 type_start_token->location);
16173 /* If it's not a `:' or a `{' then we can't really be looking at a
16174 class-head, since a class-head only appears as part of a
16175 class-specifier. We have to detect this situation before calling
16176 xref_tag, since that has irreversible side-effects. */
16177 if (!cp_parser_next_token_starts_class_definition_p (parser))
16179 cp_parser_error (parser, "expected %<{%> or %<:%>");
16180 return error_mark_node;
16183 /* At this point, we're going ahead with the class-specifier, even
16184 if some other problem occurs. */
16185 cp_parser_commit_to_tentative_parse (parser);
16186 /* Issue the error about the overly-qualified name now. */
16187 if (qualified_p)
16189 cp_parser_error (parser,
16190 "global qualification of class name is invalid");
16191 return error_mark_node;
16193 else if (invalid_nested_name_p)
16195 cp_parser_error (parser,
16196 "qualified name does not name a class");
16197 return error_mark_node;
16199 else if (nested_name_specifier)
16201 tree scope;
16203 /* Reject typedef-names in class heads. */
16204 if (!DECL_IMPLICIT_TYPEDEF_P (type))
16206 error_at (type_start_token->location,
16207 "invalid class name in declaration of %qD",
16208 type);
16209 type = NULL_TREE;
16210 goto done;
16213 /* Figure out in what scope the declaration is being placed. */
16214 scope = current_scope ();
16215 /* If that scope does not contain the scope in which the
16216 class was originally declared, the program is invalid. */
16217 if (scope && !is_ancestor (scope, nested_name_specifier))
16219 if (at_namespace_scope_p ())
16220 error_at (type_start_token->location,
16221 "declaration of %qD in namespace %qD which does not "
16222 "enclose %qD",
16223 type, scope, nested_name_specifier);
16224 else
16225 error_at (type_start_token->location,
16226 "declaration of %qD in %qD which does not enclose %qD",
16227 type, scope, nested_name_specifier);
16228 type = NULL_TREE;
16229 goto done;
16231 /* [dcl.meaning]
16233 A declarator-id shall not be qualified except for the
16234 definition of a ... nested class outside of its class
16235 ... [or] the definition or explicit instantiation of a
16236 class member of a namespace outside of its namespace. */
16237 if (scope == nested_name_specifier)
16239 permerror (nested_name_specifier_token_start->location,
16240 "extra qualification not allowed");
16241 nested_name_specifier = NULL_TREE;
16242 num_templates = 0;
16245 /* An explicit-specialization must be preceded by "template <>". If
16246 it is not, try to recover gracefully. */
16247 if (at_namespace_scope_p ()
16248 && parser->num_template_parameter_lists == 0
16249 && template_id_p)
16251 error_at (type_start_token->location,
16252 "an explicit specialization must be preceded by %<template <>%>");
16253 invalid_explicit_specialization_p = true;
16254 /* Take the same action that would have been taken by
16255 cp_parser_explicit_specialization. */
16256 ++parser->num_template_parameter_lists;
16257 begin_specialization ();
16259 /* There must be no "return" statements between this point and the
16260 end of this function; set "type "to the correct return value and
16261 use "goto done;" to return. */
16262 /* Make sure that the right number of template parameters were
16263 present. */
16264 if (!cp_parser_check_template_parameters (parser, num_templates,
16265 type_start_token->location,
16266 /*declarator=*/NULL))
16268 /* If something went wrong, there is no point in even trying to
16269 process the class-definition. */
16270 type = NULL_TREE;
16271 goto done;
16274 /* Look up the type. */
16275 if (template_id_p)
16277 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
16278 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
16279 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
16281 error_at (type_start_token->location,
16282 "function template %qD redeclared as a class template", id);
16283 type = error_mark_node;
16285 else
16287 type = TREE_TYPE (id);
16288 type = maybe_process_partial_specialization (type);
16290 if (nested_name_specifier)
16291 pushed_scope = push_scope (nested_name_specifier);
16293 else if (nested_name_specifier)
16295 tree class_type;
16297 /* Given:
16299 template <typename T> struct S { struct T };
16300 template <typename T> struct S<T>::T { };
16302 we will get a TYPENAME_TYPE when processing the definition of
16303 `S::T'. We need to resolve it to the actual type before we
16304 try to define it. */
16305 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
16307 class_type = resolve_typename_type (TREE_TYPE (type),
16308 /*only_current_p=*/false);
16309 if (TREE_CODE (class_type) != TYPENAME_TYPE)
16310 type = TYPE_NAME (class_type);
16311 else
16313 cp_parser_error (parser, "could not resolve typename type");
16314 type = error_mark_node;
16318 if (maybe_process_partial_specialization (TREE_TYPE (type))
16319 == error_mark_node)
16321 type = NULL_TREE;
16322 goto done;
16325 class_type = current_class_type;
16326 /* Enter the scope indicated by the nested-name-specifier. */
16327 pushed_scope = push_scope (nested_name_specifier);
16328 /* Get the canonical version of this type. */
16329 type = TYPE_MAIN_DECL (TREE_TYPE (type));
16330 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
16331 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
16333 type = push_template_decl (type);
16334 if (type == error_mark_node)
16336 type = NULL_TREE;
16337 goto done;
16341 type = TREE_TYPE (type);
16342 *nested_name_specifier_p = true;
16344 else /* The name is not a nested name. */
16346 /* If the class was unnamed, create a dummy name. */
16347 if (!id)
16348 id = make_anon_name ();
16349 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
16350 parser->num_template_parameter_lists);
16353 /* Indicate whether this class was declared as a `class' or as a
16354 `struct'. */
16355 if (TREE_CODE (type) == RECORD_TYPE)
16356 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
16357 cp_parser_check_class_key (class_key, type);
16359 /* If this type was already complete, and we see another definition,
16360 that's an error. */
16361 if (type != error_mark_node && COMPLETE_TYPE_P (type))
16363 error_at (type_start_token->location, "redefinition of %q#T",
16364 type);
16365 error_at (type_start_token->location, "previous definition of %q+#T",
16366 type);
16367 type = NULL_TREE;
16368 goto done;
16370 else if (type == error_mark_node)
16371 type = NULL_TREE;
16373 /* We will have entered the scope containing the class; the names of
16374 base classes should be looked up in that context. For example:
16376 struct A { struct B {}; struct C; };
16377 struct A::C : B {};
16379 is valid. */
16381 /* Get the list of base-classes, if there is one. */
16382 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16383 *bases = cp_parser_base_clause (parser);
16385 done:
16386 /* Leave the scope given by the nested-name-specifier. We will
16387 enter the class scope itself while processing the members. */
16388 if (pushed_scope)
16389 pop_scope (pushed_scope);
16391 if (invalid_explicit_specialization_p)
16393 end_specialization ();
16394 --parser->num_template_parameter_lists;
16396 *attributes_p = attributes;
16397 return type;
16400 /* Parse a class-key.
16402 class-key:
16403 class
16404 struct
16405 union
16407 Returns the kind of class-key specified, or none_type to indicate
16408 error. */
16410 static enum tag_types
16411 cp_parser_class_key (cp_parser* parser)
16413 cp_token *token;
16414 enum tag_types tag_type;
16416 /* Look for the class-key. */
16417 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
16418 if (!token)
16419 return none_type;
16421 /* Check to see if the TOKEN is a class-key. */
16422 tag_type = cp_parser_token_is_class_key (token);
16423 if (!tag_type)
16424 cp_parser_error (parser, "expected class-key");
16425 return tag_type;
16428 /* Parse an (optional) member-specification.
16430 member-specification:
16431 member-declaration member-specification [opt]
16432 access-specifier : member-specification [opt] */
16434 static void
16435 cp_parser_member_specification_opt (cp_parser* parser)
16437 while (true)
16439 cp_token *token;
16440 enum rid keyword;
16442 /* Peek at the next token. */
16443 token = cp_lexer_peek_token (parser->lexer);
16444 /* If it's a `}', or EOF then we've seen all the members. */
16445 if (token->type == CPP_CLOSE_BRACE
16446 || token->type == CPP_EOF
16447 || token->type == CPP_PRAGMA_EOL)
16448 break;
16450 /* See if this token is a keyword. */
16451 keyword = token->keyword;
16452 switch (keyword)
16454 case RID_PUBLIC:
16455 case RID_PROTECTED:
16456 case RID_PRIVATE:
16457 /* Consume the access-specifier. */
16458 cp_lexer_consume_token (parser->lexer);
16459 /* Remember which access-specifier is active. */
16460 current_access_specifier = token->u.value;
16461 /* Look for the `:'. */
16462 cp_parser_require (parser, CPP_COLON, "%<:%>");
16463 break;
16465 default:
16466 /* Accept #pragmas at class scope. */
16467 if (token->type == CPP_PRAGMA)
16469 cp_parser_pragma (parser, pragma_external);
16470 break;
16473 /* Otherwise, the next construction must be a
16474 member-declaration. */
16475 cp_parser_member_declaration (parser);
16480 /* Parse a member-declaration.
16482 member-declaration:
16483 decl-specifier-seq [opt] member-declarator-list [opt] ;
16484 function-definition ; [opt]
16485 :: [opt] nested-name-specifier template [opt] unqualified-id ;
16486 using-declaration
16487 template-declaration
16489 member-declarator-list:
16490 member-declarator
16491 member-declarator-list , member-declarator
16493 member-declarator:
16494 declarator pure-specifier [opt]
16495 declarator constant-initializer [opt]
16496 identifier [opt] : constant-expression
16498 GNU Extensions:
16500 member-declaration:
16501 __extension__ member-declaration
16503 member-declarator:
16504 declarator attributes [opt] pure-specifier [opt]
16505 declarator attributes [opt] constant-initializer [opt]
16506 identifier [opt] attributes [opt] : constant-expression
16508 C++0x Extensions:
16510 member-declaration:
16511 static_assert-declaration */
16513 static void
16514 cp_parser_member_declaration (cp_parser* parser)
16516 cp_decl_specifier_seq decl_specifiers;
16517 tree prefix_attributes;
16518 tree decl;
16519 int declares_class_or_enum;
16520 bool friend_p;
16521 cp_token *token = NULL;
16522 cp_token *decl_spec_token_start = NULL;
16523 cp_token *initializer_token_start = NULL;
16524 int saved_pedantic;
16526 /* Check for the `__extension__' keyword. */
16527 if (cp_parser_extension_opt (parser, &saved_pedantic))
16529 /* Recurse. */
16530 cp_parser_member_declaration (parser);
16531 /* Restore the old value of the PEDANTIC flag. */
16532 pedantic = saved_pedantic;
16534 return;
16537 /* Check for a template-declaration. */
16538 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16540 /* An explicit specialization here is an error condition, and we
16541 expect the specialization handler to detect and report this. */
16542 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16543 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
16544 cp_parser_explicit_specialization (parser);
16545 else
16546 cp_parser_template_declaration (parser, /*member_p=*/true);
16548 return;
16551 /* Check for a using-declaration. */
16552 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
16554 /* Parse the using-declaration. */
16555 cp_parser_using_declaration (parser,
16556 /*access_declaration_p=*/false);
16557 return;
16560 /* Check for @defs. */
16561 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
16563 tree ivar, member;
16564 tree ivar_chains = cp_parser_objc_defs_expression (parser);
16565 ivar = ivar_chains;
16566 while (ivar)
16568 member = ivar;
16569 ivar = TREE_CHAIN (member);
16570 TREE_CHAIN (member) = NULL_TREE;
16571 finish_member_declaration (member);
16573 return;
16576 /* If the next token is `static_assert' we have a static assertion. */
16577 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
16579 cp_parser_static_assert (parser, /*member_p=*/true);
16580 return;
16583 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
16584 return;
16586 /* Parse the decl-specifier-seq. */
16587 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
16588 cp_parser_decl_specifier_seq (parser,
16589 CP_PARSER_FLAGS_OPTIONAL,
16590 &decl_specifiers,
16591 &declares_class_or_enum);
16592 prefix_attributes = decl_specifiers.attributes;
16593 decl_specifiers.attributes = NULL_TREE;
16594 /* Check for an invalid type-name. */
16595 if (!decl_specifiers.any_type_specifiers_p
16596 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
16597 return;
16598 /* If there is no declarator, then the decl-specifier-seq should
16599 specify a type. */
16600 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16602 /* If there was no decl-specifier-seq, and the next token is a
16603 `;', then we have something like:
16605 struct S { ; };
16607 [class.mem]
16609 Each member-declaration shall declare at least one member
16610 name of the class. */
16611 if (!decl_specifiers.any_specifiers_p)
16613 cp_token *token = cp_lexer_peek_token (parser->lexer);
16614 if (!in_system_header_at (token->location))
16615 pedwarn (token->location, OPT_pedantic, "extra %<;%>");
16617 else
16619 tree type;
16621 /* See if this declaration is a friend. */
16622 friend_p = cp_parser_friend_p (&decl_specifiers);
16623 /* If there were decl-specifiers, check to see if there was
16624 a class-declaration. */
16625 type = check_tag_decl (&decl_specifiers);
16626 /* Nested classes have already been added to the class, but
16627 a `friend' needs to be explicitly registered. */
16628 if (friend_p)
16630 /* If the `friend' keyword was present, the friend must
16631 be introduced with a class-key. */
16632 if (!declares_class_or_enum)
16633 error_at (decl_spec_token_start->location,
16634 "a class-key must be used when declaring a friend");
16635 /* In this case:
16637 template <typename T> struct A {
16638 friend struct A<T>::B;
16641 A<T>::B will be represented by a TYPENAME_TYPE, and
16642 therefore not recognized by check_tag_decl. */
16643 if (!type
16644 && decl_specifiers.type
16645 && TYPE_P (decl_specifiers.type))
16646 type = decl_specifiers.type;
16647 if (!type || !TYPE_P (type))
16648 error_at (decl_spec_token_start->location,
16649 "friend declaration does not name a class or "
16650 "function");
16651 else
16652 make_friend_class (current_class_type, type,
16653 /*complain=*/true);
16655 /* If there is no TYPE, an error message will already have
16656 been issued. */
16657 else if (!type || type == error_mark_node)
16659 /* An anonymous aggregate has to be handled specially; such
16660 a declaration really declares a data member (with a
16661 particular type), as opposed to a nested class. */
16662 else if (ANON_AGGR_TYPE_P (type))
16664 /* Remove constructors and such from TYPE, now that we
16665 know it is an anonymous aggregate. */
16666 fixup_anonymous_aggr (type);
16667 /* And make the corresponding data member. */
16668 decl = build_decl (decl_spec_token_start->location,
16669 FIELD_DECL, NULL_TREE, type);
16670 /* Add it to the class. */
16671 finish_member_declaration (decl);
16673 else
16674 cp_parser_check_access_in_redeclaration
16675 (TYPE_NAME (type),
16676 decl_spec_token_start->location);
16679 else
16681 /* See if these declarations will be friends. */
16682 friend_p = cp_parser_friend_p (&decl_specifiers);
16684 /* Keep going until we hit the `;' at the end of the
16685 declaration. */
16686 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16688 tree attributes = NULL_TREE;
16689 tree first_attribute;
16691 /* Peek at the next token. */
16692 token = cp_lexer_peek_token (parser->lexer);
16694 /* Check for a bitfield declaration. */
16695 if (token->type == CPP_COLON
16696 || (token->type == CPP_NAME
16697 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
16698 == CPP_COLON))
16700 tree identifier;
16701 tree width;
16703 /* Get the name of the bitfield. Note that we cannot just
16704 check TOKEN here because it may have been invalidated by
16705 the call to cp_lexer_peek_nth_token above. */
16706 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
16707 identifier = cp_parser_identifier (parser);
16708 else
16709 identifier = NULL_TREE;
16711 /* Consume the `:' token. */
16712 cp_lexer_consume_token (parser->lexer);
16713 /* Get the width of the bitfield. */
16714 width
16715 = cp_parser_constant_expression (parser,
16716 /*allow_non_constant=*/false,
16717 NULL);
16719 /* Look for attributes that apply to the bitfield. */
16720 attributes = cp_parser_attributes_opt (parser);
16721 /* Remember which attributes are prefix attributes and
16722 which are not. */
16723 first_attribute = attributes;
16724 /* Combine the attributes. */
16725 attributes = chainon (prefix_attributes, attributes);
16727 /* Create the bitfield declaration. */
16728 decl = grokbitfield (identifier
16729 ? make_id_declarator (NULL_TREE,
16730 identifier,
16731 sfk_none)
16732 : NULL,
16733 &decl_specifiers,
16734 width,
16735 attributes);
16737 else
16739 cp_declarator *declarator;
16740 tree initializer;
16741 tree asm_specification;
16742 int ctor_dtor_or_conv_p;
16744 /* Parse the declarator. */
16745 declarator
16746 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16747 &ctor_dtor_or_conv_p,
16748 /*parenthesized_p=*/NULL,
16749 /*member_p=*/true);
16751 /* If something went wrong parsing the declarator, make sure
16752 that we at least consume some tokens. */
16753 if (declarator == cp_error_declarator)
16755 /* Skip to the end of the statement. */
16756 cp_parser_skip_to_end_of_statement (parser);
16757 /* If the next token is not a semicolon, that is
16758 probably because we just skipped over the body of
16759 a function. So, we consume a semicolon if
16760 present, but do not issue an error message if it
16761 is not present. */
16762 if (cp_lexer_next_token_is (parser->lexer,
16763 CPP_SEMICOLON))
16764 cp_lexer_consume_token (parser->lexer);
16765 return;
16768 if (declares_class_or_enum & 2)
16769 cp_parser_check_for_definition_in_return_type
16770 (declarator, decl_specifiers.type,
16771 decl_specifiers.type_location);
16773 /* Look for an asm-specification. */
16774 asm_specification = cp_parser_asm_specification_opt (parser);
16775 /* Look for attributes that apply to the declaration. */
16776 attributes = cp_parser_attributes_opt (parser);
16777 /* Remember which attributes are prefix attributes and
16778 which are not. */
16779 first_attribute = attributes;
16780 /* Combine the attributes. */
16781 attributes = chainon (prefix_attributes, attributes);
16783 /* If it's an `=', then we have a constant-initializer or a
16784 pure-specifier. It is not correct to parse the
16785 initializer before registering the member declaration
16786 since the member declaration should be in scope while
16787 its initializer is processed. However, the rest of the
16788 front end does not yet provide an interface that allows
16789 us to handle this correctly. */
16790 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16792 /* In [class.mem]:
16794 A pure-specifier shall be used only in the declaration of
16795 a virtual function.
16797 A member-declarator can contain a constant-initializer
16798 only if it declares a static member of integral or
16799 enumeration type.
16801 Therefore, if the DECLARATOR is for a function, we look
16802 for a pure-specifier; otherwise, we look for a
16803 constant-initializer. When we call `grokfield', it will
16804 perform more stringent semantics checks. */
16805 initializer_token_start = cp_lexer_peek_token (parser->lexer);
16806 if (function_declarator_p (declarator))
16807 initializer = cp_parser_pure_specifier (parser);
16808 else
16809 /* Parse the initializer. */
16810 initializer = cp_parser_constant_initializer (parser);
16812 /* Otherwise, there is no initializer. */
16813 else
16814 initializer = NULL_TREE;
16816 /* See if we are probably looking at a function
16817 definition. We are certainly not looking at a
16818 member-declarator. Calling `grokfield' has
16819 side-effects, so we must not do it unless we are sure
16820 that we are looking at a member-declarator. */
16821 if (cp_parser_token_starts_function_definition_p
16822 (cp_lexer_peek_token (parser->lexer)))
16824 /* The grammar does not allow a pure-specifier to be
16825 used when a member function is defined. (It is
16826 possible that this fact is an oversight in the
16827 standard, since a pure function may be defined
16828 outside of the class-specifier. */
16829 if (initializer)
16830 error_at (initializer_token_start->location,
16831 "pure-specifier on function-definition");
16832 decl = cp_parser_save_member_function_body (parser,
16833 &decl_specifiers,
16834 declarator,
16835 attributes);
16836 /* If the member was not a friend, declare it here. */
16837 if (!friend_p)
16838 finish_member_declaration (decl);
16839 /* Peek at the next token. */
16840 token = cp_lexer_peek_token (parser->lexer);
16841 /* If the next token is a semicolon, consume it. */
16842 if (token->type == CPP_SEMICOLON)
16843 cp_lexer_consume_token (parser->lexer);
16844 return;
16846 else
16847 if (declarator->kind == cdk_function)
16848 declarator->id_loc = token->location;
16849 /* Create the declaration. */
16850 decl = grokfield (declarator, &decl_specifiers,
16851 initializer, /*init_const_expr_p=*/true,
16852 asm_specification,
16853 attributes);
16856 /* Reset PREFIX_ATTRIBUTES. */
16857 while (attributes && TREE_CHAIN (attributes) != first_attribute)
16858 attributes = TREE_CHAIN (attributes);
16859 if (attributes)
16860 TREE_CHAIN (attributes) = NULL_TREE;
16862 /* If there is any qualification still in effect, clear it
16863 now; we will be starting fresh with the next declarator. */
16864 parser->scope = NULL_TREE;
16865 parser->qualifying_scope = NULL_TREE;
16866 parser->object_scope = NULL_TREE;
16867 /* If it's a `,', then there are more declarators. */
16868 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16869 cp_lexer_consume_token (parser->lexer);
16870 /* If the next token isn't a `;', then we have a parse error. */
16871 else if (cp_lexer_next_token_is_not (parser->lexer,
16872 CPP_SEMICOLON))
16874 cp_parser_error (parser, "expected %<;%>");
16875 /* Skip tokens until we find a `;'. */
16876 cp_parser_skip_to_end_of_statement (parser);
16878 break;
16881 if (decl)
16883 /* Add DECL to the list of members. */
16884 if (!friend_p)
16885 finish_member_declaration (decl);
16887 if (TREE_CODE (decl) == FUNCTION_DECL)
16888 cp_parser_save_default_args (parser, decl);
16893 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16896 /* Parse a pure-specifier.
16898 pure-specifier:
16901 Returns INTEGER_ZERO_NODE if a pure specifier is found.
16902 Otherwise, ERROR_MARK_NODE is returned. */
16904 static tree
16905 cp_parser_pure_specifier (cp_parser* parser)
16907 cp_token *token;
16909 /* Look for the `=' token. */
16910 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16911 return error_mark_node;
16912 /* Look for the `0' token. */
16913 token = cp_lexer_peek_token (parser->lexer);
16915 if (token->type == CPP_EOF
16916 || token->type == CPP_PRAGMA_EOL)
16917 return error_mark_node;
16919 cp_lexer_consume_token (parser->lexer);
16921 /* Accept = default or = delete in c++0x mode. */
16922 if (token->keyword == RID_DEFAULT
16923 || token->keyword == RID_DELETE)
16925 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
16926 return token->u.value;
16929 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
16930 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
16932 cp_parser_error (parser,
16933 "invalid pure specifier (only %<= 0%> is allowed)");
16934 cp_parser_skip_to_end_of_statement (parser);
16935 return error_mark_node;
16937 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
16939 error_at (token->location, "templates may not be %<virtual%>");
16940 return error_mark_node;
16943 return integer_zero_node;
16946 /* Parse a constant-initializer.
16948 constant-initializer:
16949 = constant-expression
16951 Returns a representation of the constant-expression. */
16953 static tree
16954 cp_parser_constant_initializer (cp_parser* parser)
16956 /* Look for the `=' token. */
16957 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16958 return error_mark_node;
16960 /* It is invalid to write:
16962 struct S { static const int i = { 7 }; };
16965 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16967 cp_parser_error (parser,
16968 "a brace-enclosed initializer is not allowed here");
16969 /* Consume the opening brace. */
16970 cp_lexer_consume_token (parser->lexer);
16971 /* Skip the initializer. */
16972 cp_parser_skip_to_closing_brace (parser);
16973 /* Look for the trailing `}'. */
16974 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
16976 return error_mark_node;
16979 return cp_parser_constant_expression (parser,
16980 /*allow_non_constant=*/false,
16981 NULL);
16984 /* Derived classes [gram.class.derived] */
16986 /* Parse a base-clause.
16988 base-clause:
16989 : base-specifier-list
16991 base-specifier-list:
16992 base-specifier ... [opt]
16993 base-specifier-list , base-specifier ... [opt]
16995 Returns a TREE_LIST representing the base-classes, in the order in
16996 which they were declared. The representation of each node is as
16997 described by cp_parser_base_specifier.
16999 In the case that no bases are specified, this function will return
17000 NULL_TREE, not ERROR_MARK_NODE. */
17002 static tree
17003 cp_parser_base_clause (cp_parser* parser)
17005 tree bases = NULL_TREE;
17007 /* Look for the `:' that begins the list. */
17008 cp_parser_require (parser, CPP_COLON, "%<:%>");
17010 /* Scan the base-specifier-list. */
17011 while (true)
17013 cp_token *token;
17014 tree base;
17015 bool pack_expansion_p = false;
17017 /* Look for the base-specifier. */
17018 base = cp_parser_base_specifier (parser);
17019 /* Look for the (optional) ellipsis. */
17020 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17022 /* Consume the `...'. */
17023 cp_lexer_consume_token (parser->lexer);
17025 pack_expansion_p = true;
17028 /* Add BASE to the front of the list. */
17029 if (base != error_mark_node)
17031 if (pack_expansion_p)
17032 /* Make this a pack expansion type. */
17033 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
17036 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
17038 TREE_CHAIN (base) = bases;
17039 bases = base;
17042 /* Peek at the next token. */
17043 token = cp_lexer_peek_token (parser->lexer);
17044 /* If it's not a comma, then the list is complete. */
17045 if (token->type != CPP_COMMA)
17046 break;
17047 /* Consume the `,'. */
17048 cp_lexer_consume_token (parser->lexer);
17051 /* PARSER->SCOPE may still be non-NULL at this point, if the last
17052 base class had a qualified name. However, the next name that
17053 appears is certainly not qualified. */
17054 parser->scope = NULL_TREE;
17055 parser->qualifying_scope = NULL_TREE;
17056 parser->object_scope = NULL_TREE;
17058 return nreverse (bases);
17061 /* Parse a base-specifier.
17063 base-specifier:
17064 :: [opt] nested-name-specifier [opt] class-name
17065 virtual access-specifier [opt] :: [opt] nested-name-specifier
17066 [opt] class-name
17067 access-specifier virtual [opt] :: [opt] nested-name-specifier
17068 [opt] class-name
17070 Returns a TREE_LIST. The TREE_PURPOSE will be one of
17071 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
17072 indicate the specifiers provided. The TREE_VALUE will be a TYPE
17073 (or the ERROR_MARK_NODE) indicating the type that was specified. */
17075 static tree
17076 cp_parser_base_specifier (cp_parser* parser)
17078 cp_token *token;
17079 bool done = false;
17080 bool virtual_p = false;
17081 bool duplicate_virtual_error_issued_p = false;
17082 bool duplicate_access_error_issued_p = false;
17083 bool class_scope_p, template_p;
17084 tree access = access_default_node;
17085 tree type;
17087 /* Process the optional `virtual' and `access-specifier'. */
17088 while (!done)
17090 /* Peek at the next token. */
17091 token = cp_lexer_peek_token (parser->lexer);
17092 /* Process `virtual'. */
17093 switch (token->keyword)
17095 case RID_VIRTUAL:
17096 /* If `virtual' appears more than once, issue an error. */
17097 if (virtual_p && !duplicate_virtual_error_issued_p)
17099 cp_parser_error (parser,
17100 "%<virtual%> specified more than once in base-specified");
17101 duplicate_virtual_error_issued_p = true;
17104 virtual_p = true;
17106 /* Consume the `virtual' token. */
17107 cp_lexer_consume_token (parser->lexer);
17109 break;
17111 case RID_PUBLIC:
17112 case RID_PROTECTED:
17113 case RID_PRIVATE:
17114 /* If more than one access specifier appears, issue an
17115 error. */
17116 if (access != access_default_node
17117 && !duplicate_access_error_issued_p)
17119 cp_parser_error (parser,
17120 "more than one access specifier in base-specified");
17121 duplicate_access_error_issued_p = true;
17124 access = ridpointers[(int) token->keyword];
17126 /* Consume the access-specifier. */
17127 cp_lexer_consume_token (parser->lexer);
17129 break;
17131 default:
17132 done = true;
17133 break;
17136 /* It is not uncommon to see programs mechanically, erroneously, use
17137 the 'typename' keyword to denote (dependent) qualified types
17138 as base classes. */
17139 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
17141 token = cp_lexer_peek_token (parser->lexer);
17142 if (!processing_template_decl)
17143 error_at (token->location,
17144 "keyword %<typename%> not allowed outside of templates");
17145 else
17146 error_at (token->location,
17147 "keyword %<typename%> not allowed in this context "
17148 "(the base class is implicitly a type)");
17149 cp_lexer_consume_token (parser->lexer);
17152 /* Look for the optional `::' operator. */
17153 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
17154 /* Look for the nested-name-specifier. The simplest way to
17155 implement:
17157 [temp.res]
17159 The keyword `typename' is not permitted in a base-specifier or
17160 mem-initializer; in these contexts a qualified name that
17161 depends on a template-parameter is implicitly assumed to be a
17162 type name.
17164 is to pretend that we have seen the `typename' keyword at this
17165 point. */
17166 cp_parser_nested_name_specifier_opt (parser,
17167 /*typename_keyword_p=*/true,
17168 /*check_dependency_p=*/true,
17169 typename_type,
17170 /*is_declaration=*/true);
17171 /* If the base class is given by a qualified name, assume that names
17172 we see are type names or templates, as appropriate. */
17173 class_scope_p = (parser->scope && TYPE_P (parser->scope));
17174 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
17176 /* Finally, look for the class-name. */
17177 type = cp_parser_class_name (parser,
17178 class_scope_p,
17179 template_p,
17180 typename_type,
17181 /*check_dependency_p=*/true,
17182 /*class_head_p=*/false,
17183 /*is_declaration=*/true);
17185 if (type == error_mark_node)
17186 return error_mark_node;
17188 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
17191 /* Exception handling [gram.exception] */
17193 /* Parse an (optional) exception-specification.
17195 exception-specification:
17196 throw ( type-id-list [opt] )
17198 Returns a TREE_LIST representing the exception-specification. The
17199 TREE_VALUE of each node is a type. */
17201 static tree
17202 cp_parser_exception_specification_opt (cp_parser* parser)
17204 cp_token *token;
17205 tree type_id_list;
17207 /* Peek at the next token. */
17208 token = cp_lexer_peek_token (parser->lexer);
17209 /* If it's not `throw', then there's no exception-specification. */
17210 if (!cp_parser_is_keyword (token, RID_THROW))
17211 return NULL_TREE;
17213 /* Consume the `throw'. */
17214 cp_lexer_consume_token (parser->lexer);
17216 /* Look for the `('. */
17217 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17219 /* Peek at the next token. */
17220 token = cp_lexer_peek_token (parser->lexer);
17221 /* If it's not a `)', then there is a type-id-list. */
17222 if (token->type != CPP_CLOSE_PAREN)
17224 const char *saved_message;
17226 /* Types may not be defined in an exception-specification. */
17227 saved_message = parser->type_definition_forbidden_message;
17228 parser->type_definition_forbidden_message
17229 = G_("types may not be defined in an exception-specification");
17230 /* Parse the type-id-list. */
17231 type_id_list = cp_parser_type_id_list (parser);
17232 /* Restore the saved message. */
17233 parser->type_definition_forbidden_message = saved_message;
17235 else
17236 type_id_list = empty_except_spec;
17238 /* Look for the `)'. */
17239 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17241 return type_id_list;
17244 /* Parse an (optional) type-id-list.
17246 type-id-list:
17247 type-id ... [opt]
17248 type-id-list , type-id ... [opt]
17250 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
17251 in the order that the types were presented. */
17253 static tree
17254 cp_parser_type_id_list (cp_parser* parser)
17256 tree types = NULL_TREE;
17258 while (true)
17260 cp_token *token;
17261 tree type;
17263 /* Get the next type-id. */
17264 type = cp_parser_type_id (parser);
17265 /* Parse the optional ellipsis. */
17266 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17268 /* Consume the `...'. */
17269 cp_lexer_consume_token (parser->lexer);
17271 /* Turn the type into a pack expansion expression. */
17272 type = make_pack_expansion (type);
17274 /* Add it to the list. */
17275 types = add_exception_specifier (types, type, /*complain=*/1);
17276 /* Peek at the next token. */
17277 token = cp_lexer_peek_token (parser->lexer);
17278 /* If it is not a `,', we are done. */
17279 if (token->type != CPP_COMMA)
17280 break;
17281 /* Consume the `,'. */
17282 cp_lexer_consume_token (parser->lexer);
17285 return nreverse (types);
17288 /* Parse a try-block.
17290 try-block:
17291 try compound-statement handler-seq */
17293 static tree
17294 cp_parser_try_block (cp_parser* parser)
17296 tree try_block;
17298 cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
17299 try_block = begin_try_block ();
17300 cp_parser_compound_statement (parser, NULL, true);
17301 finish_try_block (try_block);
17302 cp_parser_handler_seq (parser);
17303 finish_handler_sequence (try_block);
17305 return try_block;
17308 /* Parse a function-try-block.
17310 function-try-block:
17311 try ctor-initializer [opt] function-body handler-seq */
17313 static bool
17314 cp_parser_function_try_block (cp_parser* parser)
17316 tree compound_stmt;
17317 tree try_block;
17318 bool ctor_initializer_p;
17320 /* Look for the `try' keyword. */
17321 if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
17322 return false;
17323 /* Let the rest of the front end know where we are. */
17324 try_block = begin_function_try_block (&compound_stmt);
17325 /* Parse the function-body. */
17326 ctor_initializer_p
17327 = cp_parser_ctor_initializer_opt_and_function_body (parser);
17328 /* We're done with the `try' part. */
17329 finish_function_try_block (try_block);
17330 /* Parse the handlers. */
17331 cp_parser_handler_seq (parser);
17332 /* We're done with the handlers. */
17333 finish_function_handler_sequence (try_block, compound_stmt);
17335 return ctor_initializer_p;
17338 /* Parse a handler-seq.
17340 handler-seq:
17341 handler handler-seq [opt] */
17343 static void
17344 cp_parser_handler_seq (cp_parser* parser)
17346 while (true)
17348 cp_token *token;
17350 /* Parse the handler. */
17351 cp_parser_handler (parser);
17352 /* Peek at the next token. */
17353 token = cp_lexer_peek_token (parser->lexer);
17354 /* If it's not `catch' then there are no more handlers. */
17355 if (!cp_parser_is_keyword (token, RID_CATCH))
17356 break;
17360 /* Parse a handler.
17362 handler:
17363 catch ( exception-declaration ) compound-statement */
17365 static void
17366 cp_parser_handler (cp_parser* parser)
17368 tree handler;
17369 tree declaration;
17371 cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
17372 handler = begin_handler ();
17373 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17374 declaration = cp_parser_exception_declaration (parser);
17375 finish_handler_parms (declaration, handler);
17376 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17377 cp_parser_compound_statement (parser, NULL, false);
17378 finish_handler (handler);
17381 /* Parse an exception-declaration.
17383 exception-declaration:
17384 type-specifier-seq declarator
17385 type-specifier-seq abstract-declarator
17386 type-specifier-seq
17389 Returns a VAR_DECL for the declaration, or NULL_TREE if the
17390 ellipsis variant is used. */
17392 static tree
17393 cp_parser_exception_declaration (cp_parser* parser)
17395 cp_decl_specifier_seq type_specifiers;
17396 cp_declarator *declarator;
17397 const char *saved_message;
17399 /* If it's an ellipsis, it's easy to handle. */
17400 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17402 /* Consume the `...' token. */
17403 cp_lexer_consume_token (parser->lexer);
17404 return NULL_TREE;
17407 /* Types may not be defined in exception-declarations. */
17408 saved_message = parser->type_definition_forbidden_message;
17409 parser->type_definition_forbidden_message
17410 = G_("types may not be defined in exception-declarations");
17412 /* Parse the type-specifier-seq. */
17413 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
17414 /*is_trailing_return=*/false,
17415 &type_specifiers);
17416 /* If it's a `)', then there is no declarator. */
17417 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
17418 declarator = NULL;
17419 else
17420 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
17421 /*ctor_dtor_or_conv_p=*/NULL,
17422 /*parenthesized_p=*/NULL,
17423 /*member_p=*/false);
17425 /* Restore the saved message. */
17426 parser->type_definition_forbidden_message = saved_message;
17428 if (!type_specifiers.any_specifiers_p)
17429 return error_mark_node;
17431 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
17434 /* Parse a throw-expression.
17436 throw-expression:
17437 throw assignment-expression [opt]
17439 Returns a THROW_EXPR representing the throw-expression. */
17441 static tree
17442 cp_parser_throw_expression (cp_parser* parser)
17444 tree expression;
17445 cp_token* token;
17447 cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
17448 token = cp_lexer_peek_token (parser->lexer);
17449 /* Figure out whether or not there is an assignment-expression
17450 following the "throw" keyword. */
17451 if (token->type == CPP_COMMA
17452 || token->type == CPP_SEMICOLON
17453 || token->type == CPP_CLOSE_PAREN
17454 || token->type == CPP_CLOSE_SQUARE
17455 || token->type == CPP_CLOSE_BRACE
17456 || token->type == CPP_COLON)
17457 expression = NULL_TREE;
17458 else
17459 expression = cp_parser_assignment_expression (parser,
17460 /*cast_p=*/false, NULL);
17462 return build_throw (expression);
17465 /* GNU Extensions */
17467 /* Parse an (optional) asm-specification.
17469 asm-specification:
17470 asm ( string-literal )
17472 If the asm-specification is present, returns a STRING_CST
17473 corresponding to the string-literal. Otherwise, returns
17474 NULL_TREE. */
17476 static tree
17477 cp_parser_asm_specification_opt (cp_parser* parser)
17479 cp_token *token;
17480 tree asm_specification;
17482 /* Peek at the next token. */
17483 token = cp_lexer_peek_token (parser->lexer);
17484 /* If the next token isn't the `asm' keyword, then there's no
17485 asm-specification. */
17486 if (!cp_parser_is_keyword (token, RID_ASM))
17487 return NULL_TREE;
17489 /* Consume the `asm' token. */
17490 cp_lexer_consume_token (parser->lexer);
17491 /* Look for the `('. */
17492 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17494 /* Look for the string-literal. */
17495 asm_specification = cp_parser_string_literal (parser, false, false);
17497 /* Look for the `)'. */
17498 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17500 return asm_specification;
17503 /* Parse an asm-operand-list.
17505 asm-operand-list:
17506 asm-operand
17507 asm-operand-list , asm-operand
17509 asm-operand:
17510 string-literal ( expression )
17511 [ string-literal ] string-literal ( expression )
17513 Returns a TREE_LIST representing the operands. The TREE_VALUE of
17514 each node is the expression. The TREE_PURPOSE is itself a
17515 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
17516 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
17517 is a STRING_CST for the string literal before the parenthesis. Returns
17518 ERROR_MARK_NODE if any of the operands are invalid. */
17520 static tree
17521 cp_parser_asm_operand_list (cp_parser* parser)
17523 tree asm_operands = NULL_TREE;
17524 bool invalid_operands = false;
17526 while (true)
17528 tree string_literal;
17529 tree expression;
17530 tree name;
17532 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
17534 /* Consume the `[' token. */
17535 cp_lexer_consume_token (parser->lexer);
17536 /* Read the operand name. */
17537 name = cp_parser_identifier (parser);
17538 if (name != error_mark_node)
17539 name = build_string (IDENTIFIER_LENGTH (name),
17540 IDENTIFIER_POINTER (name));
17541 /* Look for the closing `]'. */
17542 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
17544 else
17545 name = NULL_TREE;
17546 /* Look for the string-literal. */
17547 string_literal = cp_parser_string_literal (parser, false, false);
17549 /* Look for the `('. */
17550 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17551 /* Parse the expression. */
17552 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
17553 /* Look for the `)'. */
17554 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17556 if (name == error_mark_node
17557 || string_literal == error_mark_node
17558 || expression == error_mark_node)
17559 invalid_operands = true;
17561 /* Add this operand to the list. */
17562 asm_operands = tree_cons (build_tree_list (name, string_literal),
17563 expression,
17564 asm_operands);
17565 /* If the next token is not a `,', there are no more
17566 operands. */
17567 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17568 break;
17569 /* Consume the `,'. */
17570 cp_lexer_consume_token (parser->lexer);
17573 return invalid_operands ? error_mark_node : nreverse (asm_operands);
17576 /* Parse an asm-clobber-list.
17578 asm-clobber-list:
17579 string-literal
17580 asm-clobber-list , string-literal
17582 Returns a TREE_LIST, indicating the clobbers in the order that they
17583 appeared. The TREE_VALUE of each node is a STRING_CST. */
17585 static tree
17586 cp_parser_asm_clobber_list (cp_parser* parser)
17588 tree clobbers = NULL_TREE;
17590 while (true)
17592 tree string_literal;
17594 /* Look for the string literal. */
17595 string_literal = cp_parser_string_literal (parser, false, false);
17596 /* Add it to the list. */
17597 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
17598 /* If the next token is not a `,', then the list is
17599 complete. */
17600 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17601 break;
17602 /* Consume the `,' token. */
17603 cp_lexer_consume_token (parser->lexer);
17606 return clobbers;
17609 /* Parse an asm-label-list.
17611 asm-label-list:
17612 identifier
17613 asm-label-list , identifier
17615 Returns a TREE_LIST, indicating the labels in the order that they
17616 appeared. The TREE_VALUE of each node is a label. */
17618 static tree
17619 cp_parser_asm_label_list (cp_parser* parser)
17621 tree labels = NULL_TREE;
17623 while (true)
17625 tree identifier, label, name;
17627 /* Look for the identifier. */
17628 identifier = cp_parser_identifier (parser);
17629 if (!error_operand_p (identifier))
17631 label = lookup_label (identifier);
17632 if (TREE_CODE (label) == LABEL_DECL)
17634 TREE_USED (label) = 1;
17635 check_goto (label);
17636 name = build_string (IDENTIFIER_LENGTH (identifier),
17637 IDENTIFIER_POINTER (identifier));
17638 labels = tree_cons (name, label, labels);
17641 /* If the next token is not a `,', then the list is
17642 complete. */
17643 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17644 break;
17645 /* Consume the `,' token. */
17646 cp_lexer_consume_token (parser->lexer);
17649 return nreverse (labels);
17652 /* Parse an (optional) series of attributes.
17654 attributes:
17655 attributes attribute
17657 attribute:
17658 __attribute__ (( attribute-list [opt] ))
17660 The return value is as for cp_parser_attribute_list. */
17662 static tree
17663 cp_parser_attributes_opt (cp_parser* parser)
17665 tree attributes = NULL_TREE;
17667 while (true)
17669 cp_token *token;
17670 tree attribute_list;
17672 /* Peek at the next token. */
17673 token = cp_lexer_peek_token (parser->lexer);
17674 /* If it's not `__attribute__', then we're done. */
17675 if (token->keyword != RID_ATTRIBUTE)
17676 break;
17678 /* Consume the `__attribute__' keyword. */
17679 cp_lexer_consume_token (parser->lexer);
17680 /* Look for the two `(' tokens. */
17681 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17682 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17684 /* Peek at the next token. */
17685 token = cp_lexer_peek_token (parser->lexer);
17686 if (token->type != CPP_CLOSE_PAREN)
17687 /* Parse the attribute-list. */
17688 attribute_list = cp_parser_attribute_list (parser);
17689 else
17690 /* If the next token is a `)', then there is no attribute
17691 list. */
17692 attribute_list = NULL;
17694 /* Look for the two `)' tokens. */
17695 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17696 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17698 /* Add these new attributes to the list. */
17699 attributes = chainon (attributes, attribute_list);
17702 return attributes;
17705 /* Parse an attribute-list.
17707 attribute-list:
17708 attribute
17709 attribute-list , attribute
17711 attribute:
17712 identifier
17713 identifier ( identifier )
17714 identifier ( identifier , expression-list )
17715 identifier ( expression-list )
17717 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
17718 to an attribute. The TREE_PURPOSE of each node is the identifier
17719 indicating which attribute is in use. The TREE_VALUE represents
17720 the arguments, if any. */
17722 static tree
17723 cp_parser_attribute_list (cp_parser* parser)
17725 tree attribute_list = NULL_TREE;
17726 bool save_translate_strings_p = parser->translate_strings_p;
17728 parser->translate_strings_p = false;
17729 while (true)
17731 cp_token *token;
17732 tree identifier;
17733 tree attribute;
17735 /* Look for the identifier. We also allow keywords here; for
17736 example `__attribute__ ((const))' is legal. */
17737 token = cp_lexer_peek_token (parser->lexer);
17738 if (token->type == CPP_NAME
17739 || token->type == CPP_KEYWORD)
17741 tree arguments = NULL_TREE;
17743 /* Consume the token. */
17744 token = cp_lexer_consume_token (parser->lexer);
17746 /* Save away the identifier that indicates which attribute
17747 this is. */
17748 identifier = (token->type == CPP_KEYWORD)
17749 /* For keywords, use the canonical spelling, not the
17750 parsed identifier. */
17751 ? ridpointers[(int) token->keyword]
17752 : token->u.value;
17754 attribute = build_tree_list (identifier, NULL_TREE);
17756 /* Peek at the next token. */
17757 token = cp_lexer_peek_token (parser->lexer);
17758 /* If it's an `(', then parse the attribute arguments. */
17759 if (token->type == CPP_OPEN_PAREN)
17761 VEC(tree,gc) *vec;
17762 vec = cp_parser_parenthesized_expression_list
17763 (parser, true, /*cast_p=*/false,
17764 /*allow_expansion_p=*/false,
17765 /*non_constant_p=*/NULL);
17766 if (vec == NULL)
17767 arguments = error_mark_node;
17768 else
17770 arguments = build_tree_list_vec (vec);
17771 release_tree_vector (vec);
17773 /* Save the arguments away. */
17774 TREE_VALUE (attribute) = arguments;
17777 if (arguments != error_mark_node)
17779 /* Add this attribute to the list. */
17780 TREE_CHAIN (attribute) = attribute_list;
17781 attribute_list = attribute;
17784 token = cp_lexer_peek_token (parser->lexer);
17786 /* Now, look for more attributes. If the next token isn't a
17787 `,', we're done. */
17788 if (token->type != CPP_COMMA)
17789 break;
17791 /* Consume the comma and keep going. */
17792 cp_lexer_consume_token (parser->lexer);
17794 parser->translate_strings_p = save_translate_strings_p;
17796 /* We built up the list in reverse order. */
17797 return nreverse (attribute_list);
17800 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
17801 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
17802 current value of the PEDANTIC flag, regardless of whether or not
17803 the `__extension__' keyword is present. The caller is responsible
17804 for restoring the value of the PEDANTIC flag. */
17806 static bool
17807 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
17809 /* Save the old value of the PEDANTIC flag. */
17810 *saved_pedantic = pedantic;
17812 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
17814 /* Consume the `__extension__' token. */
17815 cp_lexer_consume_token (parser->lexer);
17816 /* We're not being pedantic while the `__extension__' keyword is
17817 in effect. */
17818 pedantic = 0;
17820 return true;
17823 return false;
17826 /* Parse a label declaration.
17828 label-declaration:
17829 __label__ label-declarator-seq ;
17831 label-declarator-seq:
17832 identifier , label-declarator-seq
17833 identifier */
17835 static void
17836 cp_parser_label_declaration (cp_parser* parser)
17838 /* Look for the `__label__' keyword. */
17839 cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
17841 while (true)
17843 tree identifier;
17845 /* Look for an identifier. */
17846 identifier = cp_parser_identifier (parser);
17847 /* If we failed, stop. */
17848 if (identifier == error_mark_node)
17849 break;
17850 /* Declare it as a label. */
17851 finish_label_decl (identifier);
17852 /* If the next token is a `;', stop. */
17853 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17854 break;
17855 /* Look for the `,' separating the label declarations. */
17856 cp_parser_require (parser, CPP_COMMA, "%<,%>");
17859 /* Look for the final `;'. */
17860 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
17863 /* Support Functions */
17865 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
17866 NAME should have one of the representations used for an
17867 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
17868 is returned. If PARSER->SCOPE is a dependent type, then a
17869 SCOPE_REF is returned.
17871 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
17872 returned; the name was already resolved when the TEMPLATE_ID_EXPR
17873 was formed. Abstractly, such entities should not be passed to this
17874 function, because they do not need to be looked up, but it is
17875 simpler to check for this special case here, rather than at the
17876 call-sites.
17878 In cases not explicitly covered above, this function returns a
17879 DECL, OVERLOAD, or baselink representing the result of the lookup.
17880 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
17881 is returned.
17883 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
17884 (e.g., "struct") that was used. In that case bindings that do not
17885 refer to types are ignored.
17887 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
17888 ignored.
17890 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
17891 are ignored.
17893 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
17894 types.
17896 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
17897 TREE_LIST of candidates if name-lookup results in an ambiguity, and
17898 NULL_TREE otherwise. */
17900 static tree
17901 cp_parser_lookup_name (cp_parser *parser, tree name,
17902 enum tag_types tag_type,
17903 bool is_template,
17904 bool is_namespace,
17905 bool check_dependency,
17906 tree *ambiguous_decls,
17907 location_t name_location)
17909 int flags = 0;
17910 tree decl;
17911 tree object_type = parser->context->object_type;
17913 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17914 flags |= LOOKUP_COMPLAIN;
17916 /* Assume that the lookup will be unambiguous. */
17917 if (ambiguous_decls)
17918 *ambiguous_decls = NULL_TREE;
17920 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
17921 no longer valid. Note that if we are parsing tentatively, and
17922 the parse fails, OBJECT_TYPE will be automatically restored. */
17923 parser->context->object_type = NULL_TREE;
17925 if (name == error_mark_node)
17926 return error_mark_node;
17928 /* A template-id has already been resolved; there is no lookup to
17929 do. */
17930 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
17931 return name;
17932 if (BASELINK_P (name))
17934 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
17935 == TEMPLATE_ID_EXPR);
17936 return name;
17939 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
17940 it should already have been checked to make sure that the name
17941 used matches the type being destroyed. */
17942 if (TREE_CODE (name) == BIT_NOT_EXPR)
17944 tree type;
17946 /* Figure out to which type this destructor applies. */
17947 if (parser->scope)
17948 type = parser->scope;
17949 else if (object_type)
17950 type = object_type;
17951 else
17952 type = current_class_type;
17953 /* If that's not a class type, there is no destructor. */
17954 if (!type || !CLASS_TYPE_P (type))
17955 return error_mark_node;
17956 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
17957 lazily_declare_fn (sfk_destructor, type);
17958 if (!CLASSTYPE_DESTRUCTORS (type))
17959 return error_mark_node;
17960 /* If it was a class type, return the destructor. */
17961 return CLASSTYPE_DESTRUCTORS (type);
17964 /* By this point, the NAME should be an ordinary identifier. If
17965 the id-expression was a qualified name, the qualifying scope is
17966 stored in PARSER->SCOPE at this point. */
17967 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
17969 /* Perform the lookup. */
17970 if (parser->scope)
17972 bool dependent_p;
17974 if (parser->scope == error_mark_node)
17975 return error_mark_node;
17977 /* If the SCOPE is dependent, the lookup must be deferred until
17978 the template is instantiated -- unless we are explicitly
17979 looking up names in uninstantiated templates. Even then, we
17980 cannot look up the name if the scope is not a class type; it
17981 might, for example, be a template type parameter. */
17982 dependent_p = (TYPE_P (parser->scope)
17983 && dependent_scope_p (parser->scope));
17984 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
17985 && dependent_p)
17986 /* Defer lookup. */
17987 decl = error_mark_node;
17988 else
17990 tree pushed_scope = NULL_TREE;
17992 /* If PARSER->SCOPE is a dependent type, then it must be a
17993 class type, and we must not be checking dependencies;
17994 otherwise, we would have processed this lookup above. So
17995 that PARSER->SCOPE is not considered a dependent base by
17996 lookup_member, we must enter the scope here. */
17997 if (dependent_p)
17998 pushed_scope = push_scope (parser->scope);
18000 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
18001 lookup result and the nested-name-specifier nominates a class C:
18002 * if the name specified after the nested-name-specifier, when
18003 looked up in C, is the injected-class-name of C (Clause 9), or
18004 * if the name specified after the nested-name-specifier is the
18005 same as the identifier or the simple-template-id's template-
18006 name in the last component of the nested-name-specifier,
18007 the name is instead considered to name the constructor of
18008 class C. [ Note: for example, the constructor is not an
18009 acceptable lookup result in an elaborated-type-specifier so
18010 the constructor would not be used in place of the
18011 injected-class-name. --end note ] Such a constructor name
18012 shall be used only in the declarator-id of a declaration that
18013 names a constructor or in a using-declaration. */
18014 if (tag_type == none_type
18015 && CLASS_TYPE_P (parser->scope)
18016 && constructor_name_p (name, parser->scope))
18017 name = ctor_identifier;
18019 /* If the PARSER->SCOPE is a template specialization, it
18020 may be instantiated during name lookup. In that case,
18021 errors may be issued. Even if we rollback the current
18022 tentative parse, those errors are valid. */
18023 decl = lookup_qualified_name (parser->scope, name,
18024 tag_type != none_type,
18025 /*complain=*/true);
18027 /* If we have a single function from a using decl, pull it out. */
18028 if (TREE_CODE (decl) == OVERLOAD
18029 && !really_overloaded_fn (decl))
18030 decl = OVL_FUNCTION (decl);
18032 if (pushed_scope)
18033 pop_scope (pushed_scope);
18036 /* If the scope is a dependent type and either we deferred lookup or
18037 we did lookup but didn't find the name, rememeber the name. */
18038 if (decl == error_mark_node && TYPE_P (parser->scope)
18039 && dependent_type_p (parser->scope))
18041 if (tag_type)
18043 tree type;
18045 /* The resolution to Core Issue 180 says that `struct
18046 A::B' should be considered a type-name, even if `A'
18047 is dependent. */
18048 type = make_typename_type (parser->scope, name, tag_type,
18049 /*complain=*/tf_error);
18050 decl = TYPE_NAME (type);
18052 else if (is_template
18053 && (cp_parser_next_token_ends_template_argument_p (parser)
18054 || cp_lexer_next_token_is (parser->lexer,
18055 CPP_CLOSE_PAREN)))
18056 decl = make_unbound_class_template (parser->scope,
18057 name, NULL_TREE,
18058 /*complain=*/tf_error);
18059 else
18060 decl = build_qualified_name (/*type=*/NULL_TREE,
18061 parser->scope, name,
18062 is_template);
18064 parser->qualifying_scope = parser->scope;
18065 parser->object_scope = NULL_TREE;
18067 else if (object_type)
18069 tree object_decl = NULL_TREE;
18070 /* Look up the name in the scope of the OBJECT_TYPE, unless the
18071 OBJECT_TYPE is not a class. */
18072 if (CLASS_TYPE_P (object_type))
18073 /* If the OBJECT_TYPE is a template specialization, it may
18074 be instantiated during name lookup. In that case, errors
18075 may be issued. Even if we rollback the current tentative
18076 parse, those errors are valid. */
18077 object_decl = lookup_member (object_type,
18078 name,
18079 /*protect=*/0,
18080 tag_type != none_type);
18081 /* Look it up in the enclosing context, too. */
18082 decl = lookup_name_real (name, tag_type != none_type,
18083 /*nonclass=*/0,
18084 /*block_p=*/true, is_namespace, flags);
18085 parser->object_scope = object_type;
18086 parser->qualifying_scope = NULL_TREE;
18087 if (object_decl)
18088 decl = object_decl;
18090 else
18092 decl = lookup_name_real (name, tag_type != none_type,
18093 /*nonclass=*/0,
18094 /*block_p=*/true, is_namespace, flags);
18095 parser->qualifying_scope = NULL_TREE;
18096 parser->object_scope = NULL_TREE;
18099 /* If the lookup failed, let our caller know. */
18100 if (!decl || decl == error_mark_node)
18101 return error_mark_node;
18103 /* Pull out the template from an injected-class-name (or multiple). */
18104 if (is_template)
18105 decl = maybe_get_template_decl_from_type_decl (decl);
18107 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
18108 if (TREE_CODE (decl) == TREE_LIST)
18110 if (ambiguous_decls)
18111 *ambiguous_decls = decl;
18112 /* The error message we have to print is too complicated for
18113 cp_parser_error, so we incorporate its actions directly. */
18114 if (!cp_parser_simulate_error (parser))
18116 error_at (name_location, "reference to %qD is ambiguous",
18117 name);
18118 print_candidates (decl);
18120 return error_mark_node;
18123 gcc_assert (DECL_P (decl)
18124 || TREE_CODE (decl) == OVERLOAD
18125 || TREE_CODE (decl) == SCOPE_REF
18126 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
18127 || BASELINK_P (decl));
18129 /* If we have resolved the name of a member declaration, check to
18130 see if the declaration is accessible. When the name resolves to
18131 set of overloaded functions, accessibility is checked when
18132 overload resolution is done.
18134 During an explicit instantiation, access is not checked at all,
18135 as per [temp.explicit]. */
18136 if (DECL_P (decl))
18137 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
18139 return decl;
18142 /* Like cp_parser_lookup_name, but for use in the typical case where
18143 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
18144 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
18146 static tree
18147 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
18149 return cp_parser_lookup_name (parser, name,
18150 none_type,
18151 /*is_template=*/false,
18152 /*is_namespace=*/false,
18153 /*check_dependency=*/true,
18154 /*ambiguous_decls=*/NULL,
18155 location);
18158 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
18159 the current context, return the TYPE_DECL. If TAG_NAME_P is
18160 true, the DECL indicates the class being defined in a class-head,
18161 or declared in an elaborated-type-specifier.
18163 Otherwise, return DECL. */
18165 static tree
18166 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
18168 /* If the TEMPLATE_DECL is being declared as part of a class-head,
18169 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
18171 struct A {
18172 template <typename T> struct B;
18175 template <typename T> struct A::B {};
18177 Similarly, in an elaborated-type-specifier:
18179 namespace N { struct X{}; }
18181 struct A {
18182 template <typename T> friend struct N::X;
18185 However, if the DECL refers to a class type, and we are in
18186 the scope of the class, then the name lookup automatically
18187 finds the TYPE_DECL created by build_self_reference rather
18188 than a TEMPLATE_DECL. For example, in:
18190 template <class T> struct S {
18191 S s;
18194 there is no need to handle such case. */
18196 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
18197 return DECL_TEMPLATE_RESULT (decl);
18199 return decl;
18202 /* If too many, or too few, template-parameter lists apply to the
18203 declarator, issue an error message. Returns TRUE if all went well,
18204 and FALSE otherwise. */
18206 static bool
18207 cp_parser_check_declarator_template_parameters (cp_parser* parser,
18208 cp_declarator *declarator,
18209 location_t declarator_location)
18211 unsigned num_templates;
18213 /* We haven't seen any classes that involve template parameters yet. */
18214 num_templates = 0;
18216 switch (declarator->kind)
18218 case cdk_id:
18219 if (declarator->u.id.qualifying_scope)
18221 tree scope;
18223 scope = declarator->u.id.qualifying_scope;
18225 while (scope && CLASS_TYPE_P (scope))
18227 /* You're supposed to have one `template <...>'
18228 for every template class, but you don't need one
18229 for a full specialization. For example:
18231 template <class T> struct S{};
18232 template <> struct S<int> { void f(); };
18233 void S<int>::f () {}
18235 is correct; there shouldn't be a `template <>' for
18236 the definition of `S<int>::f'. */
18237 if (!CLASSTYPE_TEMPLATE_INFO (scope))
18238 /* If SCOPE does not have template information of any
18239 kind, then it is not a template, nor is it nested
18240 within a template. */
18241 break;
18242 if (explicit_class_specialization_p (scope))
18243 break;
18244 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
18245 ++num_templates;
18247 scope = TYPE_CONTEXT (scope);
18250 else if (TREE_CODE (declarator->u.id.unqualified_name)
18251 == TEMPLATE_ID_EXPR)
18252 /* If the DECLARATOR has the form `X<y>' then it uses one
18253 additional level of template parameters. */
18254 ++num_templates;
18256 return cp_parser_check_template_parameters
18257 (parser, num_templates, declarator_location, declarator);
18260 case cdk_function:
18261 case cdk_array:
18262 case cdk_pointer:
18263 case cdk_reference:
18264 case cdk_ptrmem:
18265 return (cp_parser_check_declarator_template_parameters
18266 (parser, declarator->declarator, declarator_location));
18268 case cdk_error:
18269 return true;
18271 default:
18272 gcc_unreachable ();
18274 return false;
18277 /* NUM_TEMPLATES were used in the current declaration. If that is
18278 invalid, return FALSE and issue an error messages. Otherwise,
18279 return TRUE. If DECLARATOR is non-NULL, then we are checking a
18280 declarator and we can print more accurate diagnostics. */
18282 static bool
18283 cp_parser_check_template_parameters (cp_parser* parser,
18284 unsigned num_templates,
18285 location_t location,
18286 cp_declarator *declarator)
18288 /* If there are the same number of template classes and parameter
18289 lists, that's OK. */
18290 if (parser->num_template_parameter_lists == num_templates)
18291 return true;
18292 /* If there are more, but only one more, then we are referring to a
18293 member template. That's OK too. */
18294 if (parser->num_template_parameter_lists == num_templates + 1)
18295 return true;
18296 /* If there are more template classes than parameter lists, we have
18297 something like:
18299 template <class T> void S<T>::R<T>::f (); */
18300 if (parser->num_template_parameter_lists < num_templates)
18302 if (declarator && !current_function_decl)
18303 error_at (location, "specializing member %<%T::%E%> "
18304 "requires %<template<>%> syntax",
18305 declarator->u.id.qualifying_scope,
18306 declarator->u.id.unqualified_name);
18307 else if (declarator)
18308 error_at (location, "invalid declaration of %<%T::%E%>",
18309 declarator->u.id.qualifying_scope,
18310 declarator->u.id.unqualified_name);
18311 else
18312 error_at (location, "too few template-parameter-lists");
18313 return false;
18315 /* Otherwise, there are too many template parameter lists. We have
18316 something like:
18318 template <class T> template <class U> void S::f(); */
18319 error_at (location, "too many template-parameter-lists");
18320 return false;
18323 /* Parse an optional `::' token indicating that the following name is
18324 from the global namespace. If so, PARSER->SCOPE is set to the
18325 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
18326 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
18327 Returns the new value of PARSER->SCOPE, if the `::' token is
18328 present, and NULL_TREE otherwise. */
18330 static tree
18331 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
18333 cp_token *token;
18335 /* Peek at the next token. */
18336 token = cp_lexer_peek_token (parser->lexer);
18337 /* If we're looking at a `::' token then we're starting from the
18338 global namespace, not our current location. */
18339 if (token->type == CPP_SCOPE)
18341 /* Consume the `::' token. */
18342 cp_lexer_consume_token (parser->lexer);
18343 /* Set the SCOPE so that we know where to start the lookup. */
18344 parser->scope = global_namespace;
18345 parser->qualifying_scope = global_namespace;
18346 parser->object_scope = NULL_TREE;
18348 return parser->scope;
18350 else if (!current_scope_valid_p)
18352 parser->scope = NULL_TREE;
18353 parser->qualifying_scope = NULL_TREE;
18354 parser->object_scope = NULL_TREE;
18357 return NULL_TREE;
18360 /* Returns TRUE if the upcoming token sequence is the start of a
18361 constructor declarator. If FRIEND_P is true, the declarator is
18362 preceded by the `friend' specifier. */
18364 static bool
18365 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
18367 bool constructor_p;
18368 tree nested_name_specifier;
18369 cp_token *next_token;
18371 /* The common case is that this is not a constructor declarator, so
18372 try to avoid doing lots of work if at all possible. It's not
18373 valid declare a constructor at function scope. */
18374 if (parser->in_function_body)
18375 return false;
18376 /* And only certain tokens can begin a constructor declarator. */
18377 next_token = cp_lexer_peek_token (parser->lexer);
18378 if (next_token->type != CPP_NAME
18379 && next_token->type != CPP_SCOPE
18380 && next_token->type != CPP_NESTED_NAME_SPECIFIER
18381 && next_token->type != CPP_TEMPLATE_ID)
18382 return false;
18384 /* Parse tentatively; we are going to roll back all of the tokens
18385 consumed here. */
18386 cp_parser_parse_tentatively (parser);
18387 /* Assume that we are looking at a constructor declarator. */
18388 constructor_p = true;
18390 /* Look for the optional `::' operator. */
18391 cp_parser_global_scope_opt (parser,
18392 /*current_scope_valid_p=*/false);
18393 /* Look for the nested-name-specifier. */
18394 nested_name_specifier
18395 = (cp_parser_nested_name_specifier_opt (parser,
18396 /*typename_keyword_p=*/false,
18397 /*check_dependency_p=*/false,
18398 /*type_p=*/false,
18399 /*is_declaration=*/false));
18400 /* Outside of a class-specifier, there must be a
18401 nested-name-specifier. */
18402 if (!nested_name_specifier &&
18403 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
18404 || friend_p))
18405 constructor_p = false;
18406 else if (nested_name_specifier == error_mark_node)
18407 constructor_p = false;
18409 /* If we have a class scope, this is easy; DR 147 says that S::S always
18410 names the constructor, and no other qualified name could. */
18411 if (constructor_p && nested_name_specifier
18412 && TYPE_P (nested_name_specifier))
18414 tree id = cp_parser_unqualified_id (parser,
18415 /*template_keyword_p=*/false,
18416 /*check_dependency_p=*/false,
18417 /*declarator_p=*/true,
18418 /*optional_p=*/false);
18419 if (is_overloaded_fn (id))
18420 id = DECL_NAME (get_first_fn (id));
18421 if (!constructor_name_p (id, nested_name_specifier))
18422 constructor_p = false;
18424 /* If we still think that this might be a constructor-declarator,
18425 look for a class-name. */
18426 else if (constructor_p)
18428 /* If we have:
18430 template <typename T> struct S {
18431 S();
18434 we must recognize that the nested `S' names a class. */
18435 tree type_decl;
18436 type_decl = cp_parser_class_name (parser,
18437 /*typename_keyword_p=*/false,
18438 /*template_keyword_p=*/false,
18439 none_type,
18440 /*check_dependency_p=*/false,
18441 /*class_head_p=*/false,
18442 /*is_declaration=*/false);
18443 /* If there was no class-name, then this is not a constructor. */
18444 constructor_p = !cp_parser_error_occurred (parser);
18446 /* If we're still considering a constructor, we have to see a `(',
18447 to begin the parameter-declaration-clause, followed by either a
18448 `)', an `...', or a decl-specifier. We need to check for a
18449 type-specifier to avoid being fooled into thinking that:
18451 S (f) (int);
18453 is a constructor. (It is actually a function named `f' that
18454 takes one parameter (of type `int') and returns a value of type
18455 `S'. */
18456 if (constructor_p
18457 && !cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
18458 constructor_p = false;
18460 if (constructor_p
18461 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
18462 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
18463 /* A parameter declaration begins with a decl-specifier,
18464 which is either the "attribute" keyword, a storage class
18465 specifier, or (usually) a type-specifier. */
18466 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
18468 tree type;
18469 tree pushed_scope = NULL_TREE;
18470 unsigned saved_num_template_parameter_lists;
18472 /* Names appearing in the type-specifier should be looked up
18473 in the scope of the class. */
18474 if (current_class_type)
18475 type = NULL_TREE;
18476 else
18478 type = TREE_TYPE (type_decl);
18479 if (TREE_CODE (type) == TYPENAME_TYPE)
18481 type = resolve_typename_type (type,
18482 /*only_current_p=*/false);
18483 if (TREE_CODE (type) == TYPENAME_TYPE)
18485 cp_parser_abort_tentative_parse (parser);
18486 return false;
18489 pushed_scope = push_scope (type);
18492 /* Inside the constructor parameter list, surrounding
18493 template-parameter-lists do not apply. */
18494 saved_num_template_parameter_lists
18495 = parser->num_template_parameter_lists;
18496 parser->num_template_parameter_lists = 0;
18498 /* Look for the type-specifier. */
18499 cp_parser_type_specifier (parser,
18500 CP_PARSER_FLAGS_NONE,
18501 /*decl_specs=*/NULL,
18502 /*is_declarator=*/true,
18503 /*declares_class_or_enum=*/NULL,
18504 /*is_cv_qualifier=*/NULL);
18506 parser->num_template_parameter_lists
18507 = saved_num_template_parameter_lists;
18509 /* Leave the scope of the class. */
18510 if (pushed_scope)
18511 pop_scope (pushed_scope);
18513 constructor_p = !cp_parser_error_occurred (parser);
18517 /* We did not really want to consume any tokens. */
18518 cp_parser_abort_tentative_parse (parser);
18520 return constructor_p;
18523 /* Parse the definition of the function given by the DECL_SPECIFIERS,
18524 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
18525 they must be performed once we are in the scope of the function.
18527 Returns the function defined. */
18529 static tree
18530 cp_parser_function_definition_from_specifiers_and_declarator
18531 (cp_parser* parser,
18532 cp_decl_specifier_seq *decl_specifiers,
18533 tree attributes,
18534 const cp_declarator *declarator)
18536 tree fn;
18537 bool success_p;
18539 /* Begin the function-definition. */
18540 success_p = start_function (decl_specifiers, declarator, attributes);
18542 /* The things we're about to see are not directly qualified by any
18543 template headers we've seen thus far. */
18544 reset_specialization ();
18546 /* If there were names looked up in the decl-specifier-seq that we
18547 did not check, check them now. We must wait until we are in the
18548 scope of the function to perform the checks, since the function
18549 might be a friend. */
18550 perform_deferred_access_checks ();
18552 if (!success_p)
18554 /* Skip the entire function. */
18555 cp_parser_skip_to_end_of_block_or_statement (parser);
18556 fn = error_mark_node;
18558 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
18560 /* Seen already, skip it. An error message has already been output. */
18561 cp_parser_skip_to_end_of_block_or_statement (parser);
18562 fn = current_function_decl;
18563 current_function_decl = NULL_TREE;
18564 /* If this is a function from a class, pop the nested class. */
18565 if (current_class_name)
18566 pop_nested_class ();
18568 else
18569 fn = cp_parser_function_definition_after_declarator (parser,
18570 /*inline_p=*/false);
18572 return fn;
18575 /* Parse the part of a function-definition that follows the
18576 declarator. INLINE_P is TRUE iff this function is an inline
18577 function defined within a class-specifier.
18579 Returns the function defined. */
18581 static tree
18582 cp_parser_function_definition_after_declarator (cp_parser* parser,
18583 bool inline_p)
18585 tree fn;
18586 bool ctor_initializer_p = false;
18587 bool saved_in_unbraced_linkage_specification_p;
18588 bool saved_in_function_body;
18589 unsigned saved_num_template_parameter_lists;
18590 cp_token *token;
18592 saved_in_function_body = parser->in_function_body;
18593 parser->in_function_body = true;
18594 /* If the next token is `return', then the code may be trying to
18595 make use of the "named return value" extension that G++ used to
18596 support. */
18597 token = cp_lexer_peek_token (parser->lexer);
18598 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
18600 /* Consume the `return' keyword. */
18601 cp_lexer_consume_token (parser->lexer);
18602 /* Look for the identifier that indicates what value is to be
18603 returned. */
18604 cp_parser_identifier (parser);
18605 /* Issue an error message. */
18606 error_at (token->location,
18607 "named return values are no longer supported");
18608 /* Skip tokens until we reach the start of the function body. */
18609 while (true)
18611 cp_token *token = cp_lexer_peek_token (parser->lexer);
18612 if (token->type == CPP_OPEN_BRACE
18613 || token->type == CPP_EOF
18614 || token->type == CPP_PRAGMA_EOL)
18615 break;
18616 cp_lexer_consume_token (parser->lexer);
18619 /* The `extern' in `extern "C" void f () { ... }' does not apply to
18620 anything declared inside `f'. */
18621 saved_in_unbraced_linkage_specification_p
18622 = parser->in_unbraced_linkage_specification_p;
18623 parser->in_unbraced_linkage_specification_p = false;
18624 /* Inside the function, surrounding template-parameter-lists do not
18625 apply. */
18626 saved_num_template_parameter_lists
18627 = parser->num_template_parameter_lists;
18628 parser->num_template_parameter_lists = 0;
18630 start_lambda_scope (current_function_decl);
18632 /* If the next token is `try', then we are looking at a
18633 function-try-block. */
18634 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
18635 ctor_initializer_p = cp_parser_function_try_block (parser);
18636 /* A function-try-block includes the function-body, so we only do
18637 this next part if we're not processing a function-try-block. */
18638 else
18639 ctor_initializer_p
18640 = cp_parser_ctor_initializer_opt_and_function_body (parser);
18642 finish_lambda_scope ();
18644 /* Finish the function. */
18645 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
18646 (inline_p ? 2 : 0));
18647 /* Generate code for it, if necessary. */
18648 expand_or_defer_fn (fn);
18649 /* Restore the saved values. */
18650 parser->in_unbraced_linkage_specification_p
18651 = saved_in_unbraced_linkage_specification_p;
18652 parser->num_template_parameter_lists
18653 = saved_num_template_parameter_lists;
18654 parser->in_function_body = saved_in_function_body;
18656 return fn;
18659 /* Parse a template-declaration, assuming that the `export' (and
18660 `extern') keywords, if present, has already been scanned. MEMBER_P
18661 is as for cp_parser_template_declaration. */
18663 static void
18664 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
18666 tree decl = NULL_TREE;
18667 VEC (deferred_access_check,gc) *checks;
18668 tree parameter_list;
18669 bool friend_p = false;
18670 bool need_lang_pop;
18671 cp_token *token;
18673 /* Look for the `template' keyword. */
18674 token = cp_lexer_peek_token (parser->lexer);
18675 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
18676 return;
18678 /* And the `<'. */
18679 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
18680 return;
18681 if (at_class_scope_p () && current_function_decl)
18683 /* 14.5.2.2 [temp.mem]
18685 A local class shall not have member templates. */
18686 error_at (token->location,
18687 "invalid declaration of member template in local class");
18688 cp_parser_skip_to_end_of_block_or_statement (parser);
18689 return;
18691 /* [temp]
18693 A template ... shall not have C linkage. */
18694 if (current_lang_name == lang_name_c)
18696 error_at (token->location, "template with C linkage");
18697 /* Give it C++ linkage to avoid confusing other parts of the
18698 front end. */
18699 push_lang_context (lang_name_cplusplus);
18700 need_lang_pop = true;
18702 else
18703 need_lang_pop = false;
18705 /* We cannot perform access checks on the template parameter
18706 declarations until we know what is being declared, just as we
18707 cannot check the decl-specifier list. */
18708 push_deferring_access_checks (dk_deferred);
18710 /* If the next token is `>', then we have an invalid
18711 specialization. Rather than complain about an invalid template
18712 parameter, issue an error message here. */
18713 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
18715 cp_parser_error (parser, "invalid explicit specialization");
18716 begin_specialization ();
18717 parameter_list = NULL_TREE;
18719 else
18720 /* Parse the template parameters. */
18721 parameter_list = cp_parser_template_parameter_list (parser);
18723 /* Get the deferred access checks from the parameter list. These
18724 will be checked once we know what is being declared, as for a
18725 member template the checks must be performed in the scope of the
18726 class containing the member. */
18727 checks = get_deferred_access_checks ();
18729 /* Look for the `>'. */
18730 cp_parser_skip_to_end_of_template_parameter_list (parser);
18731 /* We just processed one more parameter list. */
18732 ++parser->num_template_parameter_lists;
18733 /* If the next token is `template', there are more template
18734 parameters. */
18735 if (cp_lexer_next_token_is_keyword (parser->lexer,
18736 RID_TEMPLATE))
18737 cp_parser_template_declaration_after_export (parser, member_p);
18738 else
18740 /* There are no access checks when parsing a template, as we do not
18741 know if a specialization will be a friend. */
18742 push_deferring_access_checks (dk_no_check);
18743 token = cp_lexer_peek_token (parser->lexer);
18744 decl = cp_parser_single_declaration (parser,
18745 checks,
18746 member_p,
18747 /*explicit_specialization_p=*/false,
18748 &friend_p);
18749 pop_deferring_access_checks ();
18751 /* If this is a member template declaration, let the front
18752 end know. */
18753 if (member_p && !friend_p && decl)
18755 if (TREE_CODE (decl) == TYPE_DECL)
18756 cp_parser_check_access_in_redeclaration (decl, token->location);
18758 decl = finish_member_template_decl (decl);
18760 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
18761 make_friend_class (current_class_type, TREE_TYPE (decl),
18762 /*complain=*/true);
18764 /* We are done with the current parameter list. */
18765 --parser->num_template_parameter_lists;
18767 pop_deferring_access_checks ();
18769 /* Finish up. */
18770 finish_template_decl (parameter_list);
18772 /* Register member declarations. */
18773 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
18774 finish_member_declaration (decl);
18775 /* For the erroneous case of a template with C linkage, we pushed an
18776 implicit C++ linkage scope; exit that scope now. */
18777 if (need_lang_pop)
18778 pop_lang_context ();
18779 /* If DECL is a function template, we must return to parse it later.
18780 (Even though there is no definition, there might be default
18781 arguments that need handling.) */
18782 if (member_p && decl
18783 && (TREE_CODE (decl) == FUNCTION_DECL
18784 || DECL_FUNCTION_TEMPLATE_P (decl)))
18785 TREE_VALUE (parser->unparsed_functions_queues)
18786 = tree_cons (NULL_TREE, decl,
18787 TREE_VALUE (parser->unparsed_functions_queues));
18790 /* Perform the deferred access checks from a template-parameter-list.
18791 CHECKS is a TREE_LIST of access checks, as returned by
18792 get_deferred_access_checks. */
18794 static void
18795 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
18797 ++processing_template_parmlist;
18798 perform_access_checks (checks);
18799 --processing_template_parmlist;
18802 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
18803 `function-definition' sequence. MEMBER_P is true, this declaration
18804 appears in a class scope.
18806 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
18807 *FRIEND_P is set to TRUE iff the declaration is a friend. */
18809 static tree
18810 cp_parser_single_declaration (cp_parser* parser,
18811 VEC (deferred_access_check,gc)* checks,
18812 bool member_p,
18813 bool explicit_specialization_p,
18814 bool* friend_p)
18816 int declares_class_or_enum;
18817 tree decl = NULL_TREE;
18818 cp_decl_specifier_seq decl_specifiers;
18819 bool function_definition_p = false;
18820 cp_token *decl_spec_token_start;
18822 /* This function is only used when processing a template
18823 declaration. */
18824 gcc_assert (innermost_scope_kind () == sk_template_parms
18825 || innermost_scope_kind () == sk_template_spec);
18827 /* Defer access checks until we know what is being declared. */
18828 push_deferring_access_checks (dk_deferred);
18830 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
18831 alternative. */
18832 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
18833 cp_parser_decl_specifier_seq (parser,
18834 CP_PARSER_FLAGS_OPTIONAL,
18835 &decl_specifiers,
18836 &declares_class_or_enum);
18837 if (friend_p)
18838 *friend_p = cp_parser_friend_p (&decl_specifiers);
18840 /* There are no template typedefs. */
18841 if (decl_specifiers.specs[(int) ds_typedef])
18843 error_at (decl_spec_token_start->location,
18844 "template declaration of %<typedef%>");
18845 decl = error_mark_node;
18848 /* Gather up the access checks that occurred the
18849 decl-specifier-seq. */
18850 stop_deferring_access_checks ();
18852 /* Check for the declaration of a template class. */
18853 if (declares_class_or_enum)
18855 if (cp_parser_declares_only_class_p (parser))
18857 decl = shadow_tag (&decl_specifiers);
18859 /* In this case:
18861 struct C {
18862 friend template <typename T> struct A<T>::B;
18865 A<T>::B will be represented by a TYPENAME_TYPE, and
18866 therefore not recognized by shadow_tag. */
18867 if (friend_p && *friend_p
18868 && !decl
18869 && decl_specifiers.type
18870 && TYPE_P (decl_specifiers.type))
18871 decl = decl_specifiers.type;
18873 if (decl && decl != error_mark_node)
18874 decl = TYPE_NAME (decl);
18875 else
18876 decl = error_mark_node;
18878 /* Perform access checks for template parameters. */
18879 cp_parser_perform_template_parameter_access_checks (checks);
18883 /* Complain about missing 'typename' or other invalid type names. */
18884 if (!decl_specifiers.any_type_specifiers_p)
18885 cp_parser_parse_and_diagnose_invalid_type_name (parser);
18887 /* If it's not a template class, try for a template function. If
18888 the next token is a `;', then this declaration does not declare
18889 anything. But, if there were errors in the decl-specifiers, then
18890 the error might well have come from an attempted class-specifier.
18891 In that case, there's no need to warn about a missing declarator. */
18892 if (!decl
18893 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
18894 || decl_specifiers.type != error_mark_node))
18896 decl = cp_parser_init_declarator (parser,
18897 &decl_specifiers,
18898 checks,
18899 /*function_definition_allowed_p=*/true,
18900 member_p,
18901 declares_class_or_enum,
18902 &function_definition_p);
18904 /* 7.1.1-1 [dcl.stc]
18906 A storage-class-specifier shall not be specified in an explicit
18907 specialization... */
18908 if (decl
18909 && explicit_specialization_p
18910 && decl_specifiers.storage_class != sc_none)
18912 error_at (decl_spec_token_start->location,
18913 "explicit template specialization cannot have a storage class");
18914 decl = error_mark_node;
18918 pop_deferring_access_checks ();
18920 /* Clear any current qualification; whatever comes next is the start
18921 of something new. */
18922 parser->scope = NULL_TREE;
18923 parser->qualifying_scope = NULL_TREE;
18924 parser->object_scope = NULL_TREE;
18925 /* Look for a trailing `;' after the declaration. */
18926 if (!function_definition_p
18927 && (decl == error_mark_node
18928 || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
18929 cp_parser_skip_to_end_of_block_or_statement (parser);
18931 return decl;
18934 /* Parse a cast-expression that is not the operand of a unary "&". */
18936 static tree
18937 cp_parser_simple_cast_expression (cp_parser *parser)
18939 return cp_parser_cast_expression (parser, /*address_p=*/false,
18940 /*cast_p=*/false, NULL);
18943 /* Parse a functional cast to TYPE. Returns an expression
18944 representing the cast. */
18946 static tree
18947 cp_parser_functional_cast (cp_parser* parser, tree type)
18949 VEC(tree,gc) *vec;
18950 tree expression_list;
18951 tree cast;
18952 bool nonconst_p;
18954 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18956 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
18957 expression_list = cp_parser_braced_list (parser, &nonconst_p);
18958 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
18959 if (TREE_CODE (type) == TYPE_DECL)
18960 type = TREE_TYPE (type);
18961 return finish_compound_literal (type, expression_list);
18965 vec = cp_parser_parenthesized_expression_list (parser, false,
18966 /*cast_p=*/true,
18967 /*allow_expansion_p=*/true,
18968 /*non_constant_p=*/NULL);
18969 if (vec == NULL)
18970 expression_list = error_mark_node;
18971 else
18973 expression_list = build_tree_list_vec (vec);
18974 release_tree_vector (vec);
18977 cast = build_functional_cast (type, expression_list,
18978 tf_warning_or_error);
18979 /* [expr.const]/1: In an integral constant expression "only type
18980 conversions to integral or enumeration type can be used". */
18981 if (TREE_CODE (type) == TYPE_DECL)
18982 type = TREE_TYPE (type);
18983 if (cast != error_mark_node
18984 && !cast_valid_in_integral_constant_expression_p (type)
18985 && (cp_parser_non_integral_constant_expression
18986 (parser, "a call to a constructor")))
18987 return error_mark_node;
18988 return cast;
18991 /* Save the tokens that make up the body of a member function defined
18992 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
18993 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
18994 specifiers applied to the declaration. Returns the FUNCTION_DECL
18995 for the member function. */
18997 static tree
18998 cp_parser_save_member_function_body (cp_parser* parser,
18999 cp_decl_specifier_seq *decl_specifiers,
19000 cp_declarator *declarator,
19001 tree attributes)
19003 cp_token *first;
19004 cp_token *last;
19005 tree fn;
19007 /* Create the FUNCTION_DECL. */
19008 fn = grokmethod (decl_specifiers, declarator, attributes);
19009 /* If something went badly wrong, bail out now. */
19010 if (fn == error_mark_node)
19012 /* If there's a function-body, skip it. */
19013 if (cp_parser_token_starts_function_definition_p
19014 (cp_lexer_peek_token (parser->lexer)))
19015 cp_parser_skip_to_end_of_block_or_statement (parser);
19016 return error_mark_node;
19019 /* Remember it, if there default args to post process. */
19020 cp_parser_save_default_args (parser, fn);
19022 /* Save away the tokens that make up the body of the
19023 function. */
19024 first = parser->lexer->next_token;
19025 /* We can have braced-init-list mem-initializers before the fn body. */
19026 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19028 cp_lexer_consume_token (parser->lexer);
19029 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
19030 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
19032 /* cache_group will stop after an un-nested { } pair, too. */
19033 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
19034 break;
19036 /* variadic mem-inits have ... after the ')'. */
19037 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19038 cp_lexer_consume_token (parser->lexer);
19041 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
19042 /* Handle function try blocks. */
19043 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
19044 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
19045 last = parser->lexer->next_token;
19047 /* Save away the inline definition; we will process it when the
19048 class is complete. */
19049 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
19050 DECL_PENDING_INLINE_P (fn) = 1;
19052 /* We need to know that this was defined in the class, so that
19053 friend templates are handled correctly. */
19054 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
19056 /* Add FN to the queue of functions to be parsed later. */
19057 TREE_VALUE (parser->unparsed_functions_queues)
19058 = tree_cons (NULL_TREE, fn,
19059 TREE_VALUE (parser->unparsed_functions_queues));
19061 return fn;
19064 /* Parse a template-argument-list, as well as the trailing ">" (but
19065 not the opening ">"). See cp_parser_template_argument_list for the
19066 return value. */
19068 static tree
19069 cp_parser_enclosed_template_argument_list (cp_parser* parser)
19071 tree arguments;
19072 tree saved_scope;
19073 tree saved_qualifying_scope;
19074 tree saved_object_scope;
19075 bool saved_greater_than_is_operator_p;
19076 int saved_unevaluated_operand;
19077 int saved_inhibit_evaluation_warnings;
19079 /* [temp.names]
19081 When parsing a template-id, the first non-nested `>' is taken as
19082 the end of the template-argument-list rather than a greater-than
19083 operator. */
19084 saved_greater_than_is_operator_p
19085 = parser->greater_than_is_operator_p;
19086 parser->greater_than_is_operator_p = false;
19087 /* Parsing the argument list may modify SCOPE, so we save it
19088 here. */
19089 saved_scope = parser->scope;
19090 saved_qualifying_scope = parser->qualifying_scope;
19091 saved_object_scope = parser->object_scope;
19092 /* We need to evaluate the template arguments, even though this
19093 template-id may be nested within a "sizeof". */
19094 saved_unevaluated_operand = cp_unevaluated_operand;
19095 cp_unevaluated_operand = 0;
19096 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
19097 c_inhibit_evaluation_warnings = 0;
19098 /* Parse the template-argument-list itself. */
19099 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
19100 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
19101 arguments = NULL_TREE;
19102 else
19103 arguments = cp_parser_template_argument_list (parser);
19104 /* Look for the `>' that ends the template-argument-list. If we find
19105 a '>>' instead, it's probably just a typo. */
19106 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
19108 if (cxx_dialect != cxx98)
19110 /* In C++0x, a `>>' in a template argument list or cast
19111 expression is considered to be two separate `>'
19112 tokens. So, change the current token to a `>', but don't
19113 consume it: it will be consumed later when the outer
19114 template argument list (or cast expression) is parsed.
19115 Note that this replacement of `>' for `>>' is necessary
19116 even if we are parsing tentatively: in the tentative
19117 case, after calling
19118 cp_parser_enclosed_template_argument_list we will always
19119 throw away all of the template arguments and the first
19120 closing `>', either because the template argument list
19121 was erroneous or because we are replacing those tokens
19122 with a CPP_TEMPLATE_ID token. The second `>' (which will
19123 not have been thrown away) is needed either to close an
19124 outer template argument list or to complete a new-style
19125 cast. */
19126 cp_token *token = cp_lexer_peek_token (parser->lexer);
19127 token->type = CPP_GREATER;
19129 else if (!saved_greater_than_is_operator_p)
19131 /* If we're in a nested template argument list, the '>>' has
19132 to be a typo for '> >'. We emit the error message, but we
19133 continue parsing and we push a '>' as next token, so that
19134 the argument list will be parsed correctly. Note that the
19135 global source location is still on the token before the
19136 '>>', so we need to say explicitly where we want it. */
19137 cp_token *token = cp_lexer_peek_token (parser->lexer);
19138 error_at (token->location, "%<>>%> should be %<> >%> "
19139 "within a nested template argument list");
19141 token->type = CPP_GREATER;
19143 else
19145 /* If this is not a nested template argument list, the '>>'
19146 is a typo for '>'. Emit an error message and continue.
19147 Same deal about the token location, but here we can get it
19148 right by consuming the '>>' before issuing the diagnostic. */
19149 cp_token *token = cp_lexer_consume_token (parser->lexer);
19150 error_at (token->location,
19151 "spurious %<>>%>, use %<>%> to terminate "
19152 "a template argument list");
19155 else
19156 cp_parser_skip_to_end_of_template_parameter_list (parser);
19157 /* The `>' token might be a greater-than operator again now. */
19158 parser->greater_than_is_operator_p
19159 = saved_greater_than_is_operator_p;
19160 /* Restore the SAVED_SCOPE. */
19161 parser->scope = saved_scope;
19162 parser->qualifying_scope = saved_qualifying_scope;
19163 parser->object_scope = saved_object_scope;
19164 cp_unevaluated_operand = saved_unevaluated_operand;
19165 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
19167 return arguments;
19170 /* MEMBER_FUNCTION is a member function, or a friend. If default
19171 arguments, or the body of the function have not yet been parsed,
19172 parse them now. */
19174 static void
19175 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
19177 /* If this member is a template, get the underlying
19178 FUNCTION_DECL. */
19179 if (DECL_FUNCTION_TEMPLATE_P (member_function))
19180 member_function = DECL_TEMPLATE_RESULT (member_function);
19182 /* There should not be any class definitions in progress at this
19183 point; the bodies of members are only parsed outside of all class
19184 definitions. */
19185 gcc_assert (parser->num_classes_being_defined == 0);
19186 /* While we're parsing the member functions we might encounter more
19187 classes. We want to handle them right away, but we don't want
19188 them getting mixed up with functions that are currently in the
19189 queue. */
19190 parser->unparsed_functions_queues
19191 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
19193 /* Make sure that any template parameters are in scope. */
19194 maybe_begin_member_template_processing (member_function);
19196 /* If the body of the function has not yet been parsed, parse it
19197 now. */
19198 if (DECL_PENDING_INLINE_P (member_function))
19200 tree function_scope;
19201 cp_token_cache *tokens;
19203 /* The function is no longer pending; we are processing it. */
19204 tokens = DECL_PENDING_INLINE_INFO (member_function);
19205 DECL_PENDING_INLINE_INFO (member_function) = NULL;
19206 DECL_PENDING_INLINE_P (member_function) = 0;
19208 /* If this is a local class, enter the scope of the containing
19209 function. */
19210 function_scope = current_function_decl;
19211 if (function_scope)
19212 push_function_context ();
19214 /* Push the body of the function onto the lexer stack. */
19215 cp_parser_push_lexer_for_tokens (parser, tokens);
19217 /* Let the front end know that we going to be defining this
19218 function. */
19219 start_preparsed_function (member_function, NULL_TREE,
19220 SF_PRE_PARSED | SF_INCLASS_INLINE);
19222 /* Don't do access checking if it is a templated function. */
19223 if (processing_template_decl)
19224 push_deferring_access_checks (dk_no_check);
19226 /* Now, parse the body of the function. */
19227 cp_parser_function_definition_after_declarator (parser,
19228 /*inline_p=*/true);
19230 if (processing_template_decl)
19231 pop_deferring_access_checks ();
19233 /* Leave the scope of the containing function. */
19234 if (function_scope)
19235 pop_function_context ();
19236 cp_parser_pop_lexer (parser);
19239 /* Remove any template parameters from the symbol table. */
19240 maybe_end_member_template_processing ();
19242 /* Restore the queue. */
19243 parser->unparsed_functions_queues
19244 = TREE_CHAIN (parser->unparsed_functions_queues);
19247 /* If DECL contains any default args, remember it on the unparsed
19248 functions queue. */
19250 static void
19251 cp_parser_save_default_args (cp_parser* parser, tree decl)
19253 tree probe;
19255 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
19256 probe;
19257 probe = TREE_CHAIN (probe))
19258 if (TREE_PURPOSE (probe))
19260 TREE_PURPOSE (parser->unparsed_functions_queues)
19261 = tree_cons (current_class_type, decl,
19262 TREE_PURPOSE (parser->unparsed_functions_queues));
19263 break;
19267 /* FN is a FUNCTION_DECL which may contains a parameter with an
19268 unparsed DEFAULT_ARG. Parse the default args now. This function
19269 assumes that the current scope is the scope in which the default
19270 argument should be processed. */
19272 static void
19273 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
19275 bool saved_local_variables_forbidden_p;
19276 tree parm, parmdecl;
19278 /* While we're parsing the default args, we might (due to the
19279 statement expression extension) encounter more classes. We want
19280 to handle them right away, but we don't want them getting mixed
19281 up with default args that are currently in the queue. */
19282 parser->unparsed_functions_queues
19283 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
19285 /* Local variable names (and the `this' keyword) may not appear
19286 in a default argument. */
19287 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
19288 parser->local_variables_forbidden_p = true;
19290 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
19291 parmdecl = DECL_ARGUMENTS (fn);
19292 parm && parm != void_list_node;
19293 parm = TREE_CHAIN (parm),
19294 parmdecl = TREE_CHAIN (parmdecl))
19296 cp_token_cache *tokens;
19297 tree default_arg = TREE_PURPOSE (parm);
19298 tree parsed_arg;
19299 VEC(tree,gc) *insts;
19300 tree copy;
19301 unsigned ix;
19303 if (!default_arg)
19304 continue;
19306 if (TREE_CODE (default_arg) != DEFAULT_ARG)
19307 /* This can happen for a friend declaration for a function
19308 already declared with default arguments. */
19309 continue;
19311 /* Push the saved tokens for the default argument onto the parser's
19312 lexer stack. */
19313 tokens = DEFARG_TOKENS (default_arg);
19314 cp_parser_push_lexer_for_tokens (parser, tokens);
19316 start_lambda_scope (parmdecl);
19318 /* Parse the assignment-expression. */
19319 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
19320 if (parsed_arg == error_mark_node)
19322 cp_parser_pop_lexer (parser);
19323 continue;
19326 if (!processing_template_decl)
19327 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
19329 TREE_PURPOSE (parm) = parsed_arg;
19331 /* Update any instantiations we've already created. */
19332 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
19333 VEC_iterate (tree, insts, ix, copy); ix++)
19334 TREE_PURPOSE (copy) = parsed_arg;
19336 finish_lambda_scope ();
19338 /* If the token stream has not been completely used up, then
19339 there was extra junk after the end of the default
19340 argument. */
19341 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
19342 cp_parser_error (parser, "expected %<,%>");
19344 /* Revert to the main lexer. */
19345 cp_parser_pop_lexer (parser);
19348 /* Make sure no default arg is missing. */
19349 check_default_args (fn);
19351 /* Restore the state of local_variables_forbidden_p. */
19352 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
19354 /* Restore the queue. */
19355 parser->unparsed_functions_queues
19356 = TREE_CHAIN (parser->unparsed_functions_queues);
19359 /* Parse the operand of `sizeof' (or a similar operator). Returns
19360 either a TYPE or an expression, depending on the form of the
19361 input. The KEYWORD indicates which kind of expression we have
19362 encountered. */
19364 static tree
19365 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
19367 tree expr = NULL_TREE;
19368 const char *saved_message;
19369 char *tmp;
19370 bool saved_integral_constant_expression_p;
19371 bool saved_non_integral_constant_expression_p;
19372 bool pack_expansion_p = false;
19374 /* Types cannot be defined in a `sizeof' expression. Save away the
19375 old message. */
19376 saved_message = parser->type_definition_forbidden_message;
19377 /* And create the new one. */
19378 tmp = concat ("types may not be defined in %<",
19379 IDENTIFIER_POINTER (ridpointers[keyword]),
19380 "%> expressions", NULL);
19381 parser->type_definition_forbidden_message = tmp;
19383 /* The restrictions on constant-expressions do not apply inside
19384 sizeof expressions. */
19385 saved_integral_constant_expression_p
19386 = parser->integral_constant_expression_p;
19387 saved_non_integral_constant_expression_p
19388 = parser->non_integral_constant_expression_p;
19389 parser->integral_constant_expression_p = false;
19391 /* If it's a `...', then we are computing the length of a parameter
19392 pack. */
19393 if (keyword == RID_SIZEOF
19394 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19396 /* Consume the `...'. */
19397 cp_lexer_consume_token (parser->lexer);
19398 maybe_warn_variadic_templates ();
19400 /* Note that this is an expansion. */
19401 pack_expansion_p = true;
19404 /* Do not actually evaluate the expression. */
19405 ++cp_unevaluated_operand;
19406 ++c_inhibit_evaluation_warnings;
19407 /* If it's a `(', then we might be looking at the type-id
19408 construction. */
19409 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19411 tree type;
19412 bool saved_in_type_id_in_expr_p;
19414 /* We can't be sure yet whether we're looking at a type-id or an
19415 expression. */
19416 cp_parser_parse_tentatively (parser);
19417 /* Consume the `('. */
19418 cp_lexer_consume_token (parser->lexer);
19419 /* Parse the type-id. */
19420 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
19421 parser->in_type_id_in_expr_p = true;
19422 type = cp_parser_type_id (parser);
19423 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
19424 /* Now, look for the trailing `)'. */
19425 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19426 /* If all went well, then we're done. */
19427 if (cp_parser_parse_definitely (parser))
19429 cp_decl_specifier_seq decl_specs;
19431 /* Build a trivial decl-specifier-seq. */
19432 clear_decl_specs (&decl_specs);
19433 decl_specs.type = type;
19435 /* Call grokdeclarator to figure out what type this is. */
19436 expr = grokdeclarator (NULL,
19437 &decl_specs,
19438 TYPENAME,
19439 /*initialized=*/0,
19440 /*attrlist=*/NULL);
19444 /* If the type-id production did not work out, then we must be
19445 looking at the unary-expression production. */
19446 if (!expr)
19447 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
19448 /*cast_p=*/false, NULL);
19450 if (pack_expansion_p)
19451 /* Build a pack expansion. */
19452 expr = make_pack_expansion (expr);
19454 /* Go back to evaluating expressions. */
19455 --cp_unevaluated_operand;
19456 --c_inhibit_evaluation_warnings;
19458 /* Free the message we created. */
19459 free (tmp);
19460 /* And restore the old one. */
19461 parser->type_definition_forbidden_message = saved_message;
19462 parser->integral_constant_expression_p
19463 = saved_integral_constant_expression_p;
19464 parser->non_integral_constant_expression_p
19465 = saved_non_integral_constant_expression_p;
19467 return expr;
19470 /* If the current declaration has no declarator, return true. */
19472 static bool
19473 cp_parser_declares_only_class_p (cp_parser *parser)
19475 /* If the next token is a `;' or a `,' then there is no
19476 declarator. */
19477 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
19478 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
19481 /* Update the DECL_SPECS to reflect the storage class indicated by
19482 KEYWORD. */
19484 static void
19485 cp_parser_set_storage_class (cp_parser *parser,
19486 cp_decl_specifier_seq *decl_specs,
19487 enum rid keyword,
19488 location_t location)
19490 cp_storage_class storage_class;
19492 if (parser->in_unbraced_linkage_specification_p)
19494 error_at (location, "invalid use of %qD in linkage specification",
19495 ridpointers[keyword]);
19496 return;
19498 else if (decl_specs->storage_class != sc_none)
19500 decl_specs->conflicting_specifiers_p = true;
19501 return;
19504 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
19505 && decl_specs->specs[(int) ds_thread])
19507 error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
19508 decl_specs->specs[(int) ds_thread] = 0;
19511 switch (keyword)
19513 case RID_AUTO:
19514 storage_class = sc_auto;
19515 break;
19516 case RID_REGISTER:
19517 storage_class = sc_register;
19518 break;
19519 case RID_STATIC:
19520 storage_class = sc_static;
19521 break;
19522 case RID_EXTERN:
19523 storage_class = sc_extern;
19524 break;
19525 case RID_MUTABLE:
19526 storage_class = sc_mutable;
19527 break;
19528 default:
19529 gcc_unreachable ();
19531 decl_specs->storage_class = storage_class;
19533 /* A storage class specifier cannot be applied alongside a typedef
19534 specifier. If there is a typedef specifier present then set
19535 conflicting_specifiers_p which will trigger an error later
19536 on in grokdeclarator. */
19537 if (decl_specs->specs[(int)ds_typedef])
19538 decl_specs->conflicting_specifiers_p = true;
19541 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
19542 is true, the type is a user-defined type; otherwise it is a
19543 built-in type specified by a keyword. */
19545 static void
19546 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
19547 tree type_spec,
19548 location_t location,
19549 bool user_defined_p)
19551 decl_specs->any_specifiers_p = true;
19553 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
19554 (with, for example, in "typedef int wchar_t;") we remember that
19555 this is what happened. In system headers, we ignore these
19556 declarations so that G++ can work with system headers that are not
19557 C++-safe. */
19558 if (decl_specs->specs[(int) ds_typedef]
19559 && !user_defined_p
19560 && (type_spec == boolean_type_node
19561 || type_spec == char16_type_node
19562 || type_spec == char32_type_node
19563 || type_spec == wchar_type_node)
19564 && (decl_specs->type
19565 || decl_specs->specs[(int) ds_long]
19566 || decl_specs->specs[(int) ds_short]
19567 || decl_specs->specs[(int) ds_unsigned]
19568 || decl_specs->specs[(int) ds_signed]))
19570 decl_specs->redefined_builtin_type = type_spec;
19571 if (!decl_specs->type)
19573 decl_specs->type = type_spec;
19574 decl_specs->user_defined_type_p = false;
19575 decl_specs->type_location = location;
19578 else if (decl_specs->type)
19579 decl_specs->multiple_types_p = true;
19580 else
19582 decl_specs->type = type_spec;
19583 decl_specs->user_defined_type_p = user_defined_p;
19584 decl_specs->redefined_builtin_type = NULL_TREE;
19585 decl_specs->type_location = location;
19589 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
19590 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
19592 static bool
19593 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
19595 return decl_specifiers->specs[(int) ds_friend] != 0;
19598 /* If the next token is of the indicated TYPE, consume it. Otherwise,
19599 issue an error message indicating that TOKEN_DESC was expected.
19601 Returns the token consumed, if the token had the appropriate type.
19602 Otherwise, returns NULL. */
19604 static cp_token *
19605 cp_parser_require (cp_parser* parser,
19606 enum cpp_ttype type,
19607 const char* token_desc)
19609 if (cp_lexer_next_token_is (parser->lexer, type))
19610 return cp_lexer_consume_token (parser->lexer);
19611 else
19613 /* Output the MESSAGE -- unless we're parsing tentatively. */
19614 if (!cp_parser_simulate_error (parser))
19616 char *message = concat ("expected ", token_desc, NULL);
19617 cp_parser_error (parser, message);
19618 free (message);
19620 return NULL;
19624 /* An error message is produced if the next token is not '>'.
19625 All further tokens are skipped until the desired token is
19626 found or '{', '}', ';' or an unbalanced ')' or ']'. */
19628 static void
19629 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
19631 /* Current level of '< ... >'. */
19632 unsigned level = 0;
19633 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
19634 unsigned nesting_depth = 0;
19636 /* Are we ready, yet? If not, issue error message. */
19637 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
19638 return;
19640 /* Skip tokens until the desired token is found. */
19641 while (true)
19643 /* Peek at the next token. */
19644 switch (cp_lexer_peek_token (parser->lexer)->type)
19646 case CPP_LESS:
19647 if (!nesting_depth)
19648 ++level;
19649 break;
19651 case CPP_RSHIFT:
19652 if (cxx_dialect == cxx98)
19653 /* C++0x views the `>>' operator as two `>' tokens, but
19654 C++98 does not. */
19655 break;
19656 else if (!nesting_depth && level-- == 0)
19658 /* We've hit a `>>' where the first `>' closes the
19659 template argument list, and the second `>' is
19660 spurious. Just consume the `>>' and stop; we've
19661 already produced at least one error. */
19662 cp_lexer_consume_token (parser->lexer);
19663 return;
19665 /* Fall through for C++0x, so we handle the second `>' in
19666 the `>>'. */
19668 case CPP_GREATER:
19669 if (!nesting_depth && level-- == 0)
19671 /* We've reached the token we want, consume it and stop. */
19672 cp_lexer_consume_token (parser->lexer);
19673 return;
19675 break;
19677 case CPP_OPEN_PAREN:
19678 case CPP_OPEN_SQUARE:
19679 ++nesting_depth;
19680 break;
19682 case CPP_CLOSE_PAREN:
19683 case CPP_CLOSE_SQUARE:
19684 if (nesting_depth-- == 0)
19685 return;
19686 break;
19688 case CPP_EOF:
19689 case CPP_PRAGMA_EOL:
19690 case CPP_SEMICOLON:
19691 case CPP_OPEN_BRACE:
19692 case CPP_CLOSE_BRACE:
19693 /* The '>' was probably forgotten, don't look further. */
19694 return;
19696 default:
19697 break;
19700 /* Consume this token. */
19701 cp_lexer_consume_token (parser->lexer);
19705 /* If the next token is the indicated keyword, consume it. Otherwise,
19706 issue an error message indicating that TOKEN_DESC was expected.
19708 Returns the token consumed, if the token had the appropriate type.
19709 Otherwise, returns NULL. */
19711 static cp_token *
19712 cp_parser_require_keyword (cp_parser* parser,
19713 enum rid keyword,
19714 const char* token_desc)
19716 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
19718 if (token && token->keyword != keyword)
19720 dyn_string_t error_msg;
19722 /* Format the error message. */
19723 error_msg = dyn_string_new (0);
19724 dyn_string_append_cstr (error_msg, "expected ");
19725 dyn_string_append_cstr (error_msg, token_desc);
19726 cp_parser_error (parser, error_msg->s);
19727 dyn_string_delete (error_msg);
19728 return NULL;
19731 return token;
19734 /* Returns TRUE iff TOKEN is a token that can begin the body of a
19735 function-definition. */
19737 static bool
19738 cp_parser_token_starts_function_definition_p (cp_token* token)
19740 return (/* An ordinary function-body begins with an `{'. */
19741 token->type == CPP_OPEN_BRACE
19742 /* A ctor-initializer begins with a `:'. */
19743 || token->type == CPP_COLON
19744 /* A function-try-block begins with `try'. */
19745 || token->keyword == RID_TRY
19746 /* The named return value extension begins with `return'. */
19747 || token->keyword == RID_RETURN);
19750 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
19751 definition. */
19753 static bool
19754 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
19756 cp_token *token;
19758 token = cp_lexer_peek_token (parser->lexer);
19759 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
19762 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
19763 C++0x) ending a template-argument. */
19765 static bool
19766 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
19768 cp_token *token;
19770 token = cp_lexer_peek_token (parser->lexer);
19771 return (token->type == CPP_COMMA
19772 || token->type == CPP_GREATER
19773 || token->type == CPP_ELLIPSIS
19774 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
19777 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
19778 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
19780 static bool
19781 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
19782 size_t n)
19784 cp_token *token;
19786 token = cp_lexer_peek_nth_token (parser->lexer, n);
19787 if (token->type == CPP_LESS)
19788 return true;
19789 /* Check for the sequence `<::' in the original code. It would be lexed as
19790 `[:', where `[' is a digraph, and there is no whitespace before
19791 `:'. */
19792 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
19794 cp_token *token2;
19795 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
19796 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
19797 return true;
19799 return false;
19802 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
19803 or none_type otherwise. */
19805 static enum tag_types
19806 cp_parser_token_is_class_key (cp_token* token)
19808 switch (token->keyword)
19810 case RID_CLASS:
19811 return class_type;
19812 case RID_STRUCT:
19813 return record_type;
19814 case RID_UNION:
19815 return union_type;
19817 default:
19818 return none_type;
19822 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
19824 static void
19825 cp_parser_check_class_key (enum tag_types class_key, tree type)
19827 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
19828 permerror (input_location, "%qs tag used in naming %q#T",
19829 class_key == union_type ? "union"
19830 : class_key == record_type ? "struct" : "class",
19831 type);
19834 /* Issue an error message if DECL is redeclared with different
19835 access than its original declaration [class.access.spec/3].
19836 This applies to nested classes and nested class templates.
19837 [class.mem/1]. */
19839 static void
19840 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
19842 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
19843 return;
19845 if ((TREE_PRIVATE (decl)
19846 != (current_access_specifier == access_private_node))
19847 || (TREE_PROTECTED (decl)
19848 != (current_access_specifier == access_protected_node)))
19849 error_at (location, "%qD redeclared with different access", decl);
19852 /* Look for the `template' keyword, as a syntactic disambiguator.
19853 Return TRUE iff it is present, in which case it will be
19854 consumed. */
19856 static bool
19857 cp_parser_optional_template_keyword (cp_parser *parser)
19859 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
19861 /* The `template' keyword can only be used within templates;
19862 outside templates the parser can always figure out what is a
19863 template and what is not. */
19864 if (!processing_template_decl)
19866 cp_token *token = cp_lexer_peek_token (parser->lexer);
19867 error_at (token->location,
19868 "%<template%> (as a disambiguator) is only allowed "
19869 "within templates");
19870 /* If this part of the token stream is rescanned, the same
19871 error message would be generated. So, we purge the token
19872 from the stream. */
19873 cp_lexer_purge_token (parser->lexer);
19874 return false;
19876 else
19878 /* Consume the `template' keyword. */
19879 cp_lexer_consume_token (parser->lexer);
19880 return true;
19884 return false;
19887 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
19888 set PARSER->SCOPE, and perform other related actions. */
19890 static void
19891 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
19893 int i;
19894 struct tree_check *check_value;
19895 deferred_access_check *chk;
19896 VEC (deferred_access_check,gc) *checks;
19898 /* Get the stored value. */
19899 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
19900 /* Perform any access checks that were deferred. */
19901 checks = check_value->checks;
19902 if (checks)
19904 for (i = 0 ;
19905 VEC_iterate (deferred_access_check, checks, i, chk) ;
19906 ++i)
19908 perform_or_defer_access_check (chk->binfo,
19909 chk->decl,
19910 chk->diag_decl);
19913 /* Set the scope from the stored value. */
19914 parser->scope = check_value->value;
19915 parser->qualifying_scope = check_value->qualifying_scope;
19916 parser->object_scope = NULL_TREE;
19919 /* Consume tokens up through a non-nested END token. Returns TRUE if we
19920 encounter the end of a block before what we were looking for. */
19922 static bool
19923 cp_parser_cache_group (cp_parser *parser,
19924 enum cpp_ttype end,
19925 unsigned depth)
19927 while (true)
19929 cp_token *token = cp_lexer_peek_token (parser->lexer);
19931 /* Abort a parenthesized expression if we encounter a semicolon. */
19932 if ((end == CPP_CLOSE_PAREN || depth == 0)
19933 && token->type == CPP_SEMICOLON)
19934 return true;
19935 /* If we've reached the end of the file, stop. */
19936 if (token->type == CPP_EOF
19937 || (end != CPP_PRAGMA_EOL
19938 && token->type == CPP_PRAGMA_EOL))
19939 return true;
19940 if (token->type == CPP_CLOSE_BRACE && depth == 0)
19941 /* We've hit the end of an enclosing block, so there's been some
19942 kind of syntax error. */
19943 return true;
19945 /* Consume the token. */
19946 cp_lexer_consume_token (parser->lexer);
19947 /* See if it starts a new group. */
19948 if (token->type == CPP_OPEN_BRACE)
19950 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
19951 /* In theory this should probably check end == '}', but
19952 cp_parser_save_member_function_body needs it to exit
19953 after either '}' or ')' when called with ')'. */
19954 if (depth == 0)
19955 return false;
19957 else if (token->type == CPP_OPEN_PAREN)
19959 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
19960 if (depth == 0 && end == CPP_CLOSE_PAREN)
19961 return false;
19963 else if (token->type == CPP_PRAGMA)
19964 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
19965 else if (token->type == end)
19966 return false;
19970 /* Begin parsing tentatively. We always save tokens while parsing
19971 tentatively so that if the tentative parsing fails we can restore the
19972 tokens. */
19974 static void
19975 cp_parser_parse_tentatively (cp_parser* parser)
19977 /* Enter a new parsing context. */
19978 parser->context = cp_parser_context_new (parser->context);
19979 /* Begin saving tokens. */
19980 cp_lexer_save_tokens (parser->lexer);
19981 /* In order to avoid repetitive access control error messages,
19982 access checks are queued up until we are no longer parsing
19983 tentatively. */
19984 push_deferring_access_checks (dk_deferred);
19987 /* Commit to the currently active tentative parse. */
19989 static void
19990 cp_parser_commit_to_tentative_parse (cp_parser* parser)
19992 cp_parser_context *context;
19993 cp_lexer *lexer;
19995 /* Mark all of the levels as committed. */
19996 lexer = parser->lexer;
19997 for (context = parser->context; context->next; context = context->next)
19999 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
20000 break;
20001 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
20002 while (!cp_lexer_saving_tokens (lexer))
20003 lexer = lexer->next;
20004 cp_lexer_commit_tokens (lexer);
20008 /* Abort the currently active tentative parse. All consumed tokens
20009 will be rolled back, and no diagnostics will be issued. */
20011 static void
20012 cp_parser_abort_tentative_parse (cp_parser* parser)
20014 cp_parser_simulate_error (parser);
20015 /* Now, pretend that we want to see if the construct was
20016 successfully parsed. */
20017 cp_parser_parse_definitely (parser);
20020 /* Stop parsing tentatively. If a parse error has occurred, restore the
20021 token stream. Otherwise, commit to the tokens we have consumed.
20022 Returns true if no error occurred; false otherwise. */
20024 static bool
20025 cp_parser_parse_definitely (cp_parser* parser)
20027 bool error_occurred;
20028 cp_parser_context *context;
20030 /* Remember whether or not an error occurred, since we are about to
20031 destroy that information. */
20032 error_occurred = cp_parser_error_occurred (parser);
20033 /* Remove the topmost context from the stack. */
20034 context = parser->context;
20035 parser->context = context->next;
20036 /* If no parse errors occurred, commit to the tentative parse. */
20037 if (!error_occurred)
20039 /* Commit to the tokens read tentatively, unless that was
20040 already done. */
20041 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
20042 cp_lexer_commit_tokens (parser->lexer);
20044 pop_to_parent_deferring_access_checks ();
20046 /* Otherwise, if errors occurred, roll back our state so that things
20047 are just as they were before we began the tentative parse. */
20048 else
20050 cp_lexer_rollback_tokens (parser->lexer);
20051 pop_deferring_access_checks ();
20053 /* Add the context to the front of the free list. */
20054 context->next = cp_parser_context_free_list;
20055 cp_parser_context_free_list = context;
20057 return !error_occurred;
20060 /* Returns true if we are parsing tentatively and are not committed to
20061 this tentative parse. */
20063 static bool
20064 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
20066 return (cp_parser_parsing_tentatively (parser)
20067 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
20070 /* Returns nonzero iff an error has occurred during the most recent
20071 tentative parse. */
20073 static bool
20074 cp_parser_error_occurred (cp_parser* parser)
20076 return (cp_parser_parsing_tentatively (parser)
20077 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
20080 /* Returns nonzero if GNU extensions are allowed. */
20082 static bool
20083 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
20085 return parser->allow_gnu_extensions_p;
20088 /* Objective-C++ Productions */
20091 /* Parse an Objective-C expression, which feeds into a primary-expression
20092 above.
20094 objc-expression:
20095 objc-message-expression
20096 objc-string-literal
20097 objc-encode-expression
20098 objc-protocol-expression
20099 objc-selector-expression
20101 Returns a tree representation of the expression. */
20103 static tree
20104 cp_parser_objc_expression (cp_parser* parser)
20106 /* Try to figure out what kind of declaration is present. */
20107 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20109 switch (kwd->type)
20111 case CPP_OPEN_SQUARE:
20112 return cp_parser_objc_message_expression (parser);
20114 case CPP_OBJC_STRING:
20115 kwd = cp_lexer_consume_token (parser->lexer);
20116 return objc_build_string_object (kwd->u.value);
20118 case CPP_KEYWORD:
20119 switch (kwd->keyword)
20121 case RID_AT_ENCODE:
20122 return cp_parser_objc_encode_expression (parser);
20124 case RID_AT_PROTOCOL:
20125 return cp_parser_objc_protocol_expression (parser);
20127 case RID_AT_SELECTOR:
20128 return cp_parser_objc_selector_expression (parser);
20130 default:
20131 break;
20133 default:
20134 error_at (kwd->location,
20135 "misplaced %<@%D%> Objective-C++ construct",
20136 kwd->u.value);
20137 cp_parser_skip_to_end_of_block_or_statement (parser);
20140 return error_mark_node;
20143 /* Parse an Objective-C message expression.
20145 objc-message-expression:
20146 [ objc-message-receiver objc-message-args ]
20148 Returns a representation of an Objective-C message. */
20150 static tree
20151 cp_parser_objc_message_expression (cp_parser* parser)
20153 tree receiver, messageargs;
20155 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
20156 receiver = cp_parser_objc_message_receiver (parser);
20157 messageargs = cp_parser_objc_message_args (parser);
20158 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
20160 return objc_build_message_expr (build_tree_list (receiver, messageargs));
20163 /* Parse an objc-message-receiver.
20165 objc-message-receiver:
20166 expression
20167 simple-type-specifier
20169 Returns a representation of the type or expression. */
20171 static tree
20172 cp_parser_objc_message_receiver (cp_parser* parser)
20174 tree rcv;
20176 /* An Objective-C message receiver may be either (1) a type
20177 or (2) an expression. */
20178 cp_parser_parse_tentatively (parser);
20179 rcv = cp_parser_expression (parser, false, NULL);
20181 if (cp_parser_parse_definitely (parser))
20182 return rcv;
20184 rcv = cp_parser_simple_type_specifier (parser,
20185 /*decl_specs=*/NULL,
20186 CP_PARSER_FLAGS_NONE);
20188 return objc_get_class_reference (rcv);
20191 /* Parse the arguments and selectors comprising an Objective-C message.
20193 objc-message-args:
20194 objc-selector
20195 objc-selector-args
20196 objc-selector-args , objc-comma-args
20198 objc-selector-args:
20199 objc-selector [opt] : assignment-expression
20200 objc-selector-args objc-selector [opt] : assignment-expression
20202 objc-comma-args:
20203 assignment-expression
20204 objc-comma-args , assignment-expression
20206 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
20207 selector arguments and TREE_VALUE containing a list of comma
20208 arguments. */
20210 static tree
20211 cp_parser_objc_message_args (cp_parser* parser)
20213 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
20214 bool maybe_unary_selector_p = true;
20215 cp_token *token = cp_lexer_peek_token (parser->lexer);
20217 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
20219 tree selector = NULL_TREE, arg;
20221 if (token->type != CPP_COLON)
20222 selector = cp_parser_objc_selector (parser);
20224 /* Detect if we have a unary selector. */
20225 if (maybe_unary_selector_p
20226 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
20227 return build_tree_list (selector, NULL_TREE);
20229 maybe_unary_selector_p = false;
20230 cp_parser_require (parser, CPP_COLON, "%<:%>");
20231 arg = cp_parser_assignment_expression (parser, false, NULL);
20233 sel_args
20234 = chainon (sel_args,
20235 build_tree_list (selector, arg));
20237 token = cp_lexer_peek_token (parser->lexer);
20240 /* Handle non-selector arguments, if any. */
20241 while (token->type == CPP_COMMA)
20243 tree arg;
20245 cp_lexer_consume_token (parser->lexer);
20246 arg = cp_parser_assignment_expression (parser, false, NULL);
20248 addl_args
20249 = chainon (addl_args,
20250 build_tree_list (NULL_TREE, arg));
20252 token = cp_lexer_peek_token (parser->lexer);
20255 return build_tree_list (sel_args, addl_args);
20258 /* Parse an Objective-C encode expression.
20260 objc-encode-expression:
20261 @encode objc-typename
20263 Returns an encoded representation of the type argument. */
20265 static tree
20266 cp_parser_objc_encode_expression (cp_parser* parser)
20268 tree type;
20269 cp_token *token;
20271 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
20272 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20273 token = cp_lexer_peek_token (parser->lexer);
20274 type = complete_type (cp_parser_type_id (parser));
20275 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20277 if (!type)
20279 error_at (token->location,
20280 "%<@encode%> must specify a type as an argument");
20281 return error_mark_node;
20284 return objc_build_encode_expr (type);
20287 /* Parse an Objective-C @defs expression. */
20289 static tree
20290 cp_parser_objc_defs_expression (cp_parser *parser)
20292 tree name;
20294 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
20295 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20296 name = cp_parser_identifier (parser);
20297 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20299 return objc_get_class_ivars (name);
20302 /* Parse an Objective-C protocol expression.
20304 objc-protocol-expression:
20305 @protocol ( identifier )
20307 Returns a representation of the protocol expression. */
20309 static tree
20310 cp_parser_objc_protocol_expression (cp_parser* parser)
20312 tree proto;
20314 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
20315 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20316 proto = cp_parser_identifier (parser);
20317 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20319 return objc_build_protocol_expr (proto);
20322 /* Parse an Objective-C selector expression.
20324 objc-selector-expression:
20325 @selector ( objc-method-signature )
20327 objc-method-signature:
20328 objc-selector
20329 objc-selector-seq
20331 objc-selector-seq:
20332 objc-selector :
20333 objc-selector-seq objc-selector :
20335 Returns a representation of the method selector. */
20337 static tree
20338 cp_parser_objc_selector_expression (cp_parser* parser)
20340 tree sel_seq = NULL_TREE;
20341 bool maybe_unary_selector_p = true;
20342 cp_token *token;
20343 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
20345 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
20346 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20347 token = cp_lexer_peek_token (parser->lexer);
20349 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
20350 || token->type == CPP_SCOPE)
20352 tree selector = NULL_TREE;
20354 if (token->type != CPP_COLON
20355 || token->type == CPP_SCOPE)
20356 selector = cp_parser_objc_selector (parser);
20358 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
20359 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
20361 /* Detect if we have a unary selector. */
20362 if (maybe_unary_selector_p)
20364 sel_seq = selector;
20365 goto finish_selector;
20367 else
20369 cp_parser_error (parser, "expected %<:%>");
20372 maybe_unary_selector_p = false;
20373 token = cp_lexer_consume_token (parser->lexer);
20375 if (token->type == CPP_SCOPE)
20377 sel_seq
20378 = chainon (sel_seq,
20379 build_tree_list (selector, NULL_TREE));
20380 sel_seq
20381 = chainon (sel_seq,
20382 build_tree_list (NULL_TREE, NULL_TREE));
20384 else
20385 sel_seq
20386 = chainon (sel_seq,
20387 build_tree_list (selector, NULL_TREE));
20389 token = cp_lexer_peek_token (parser->lexer);
20392 finish_selector:
20393 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20395 return objc_build_selector_expr (loc, sel_seq);
20398 /* Parse a list of identifiers.
20400 objc-identifier-list:
20401 identifier
20402 objc-identifier-list , identifier
20404 Returns a TREE_LIST of identifier nodes. */
20406 static tree
20407 cp_parser_objc_identifier_list (cp_parser* parser)
20409 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
20410 cp_token *sep = cp_lexer_peek_token (parser->lexer);
20412 while (sep->type == CPP_COMMA)
20414 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
20415 list = chainon (list,
20416 build_tree_list (NULL_TREE,
20417 cp_parser_identifier (parser)));
20418 sep = cp_lexer_peek_token (parser->lexer);
20421 return list;
20424 /* Parse an Objective-C alias declaration.
20426 objc-alias-declaration:
20427 @compatibility_alias identifier identifier ;
20429 This function registers the alias mapping with the Objective-C front end.
20430 It returns nothing. */
20432 static void
20433 cp_parser_objc_alias_declaration (cp_parser* parser)
20435 tree alias, orig;
20437 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
20438 alias = cp_parser_identifier (parser);
20439 orig = cp_parser_identifier (parser);
20440 objc_declare_alias (alias, orig);
20441 cp_parser_consume_semicolon_at_end_of_statement (parser);
20444 /* Parse an Objective-C class forward-declaration.
20446 objc-class-declaration:
20447 @class objc-identifier-list ;
20449 The function registers the forward declarations with the Objective-C
20450 front end. It returns nothing. */
20452 static void
20453 cp_parser_objc_class_declaration (cp_parser* parser)
20455 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
20456 objc_declare_class (cp_parser_objc_identifier_list (parser));
20457 cp_parser_consume_semicolon_at_end_of_statement (parser);
20460 /* Parse a list of Objective-C protocol references.
20462 objc-protocol-refs-opt:
20463 objc-protocol-refs [opt]
20465 objc-protocol-refs:
20466 < objc-identifier-list >
20468 Returns a TREE_LIST of identifiers, if any. */
20470 static tree
20471 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
20473 tree protorefs = NULL_TREE;
20475 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
20477 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
20478 protorefs = cp_parser_objc_identifier_list (parser);
20479 cp_parser_require (parser, CPP_GREATER, "%<>%>");
20482 return protorefs;
20485 /* Parse a Objective-C visibility specification. */
20487 static void
20488 cp_parser_objc_visibility_spec (cp_parser* parser)
20490 cp_token *vis = cp_lexer_peek_token (parser->lexer);
20492 switch (vis->keyword)
20494 case RID_AT_PRIVATE:
20495 objc_set_visibility (2);
20496 break;
20497 case RID_AT_PROTECTED:
20498 objc_set_visibility (0);
20499 break;
20500 case RID_AT_PUBLIC:
20501 objc_set_visibility (1);
20502 break;
20503 default:
20504 return;
20507 /* Eat '@private'/'@protected'/'@public'. */
20508 cp_lexer_consume_token (parser->lexer);
20511 /* Parse an Objective-C method type. */
20513 static void
20514 cp_parser_objc_method_type (cp_parser* parser)
20516 objc_set_method_type
20517 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
20518 ? PLUS_EXPR
20519 : MINUS_EXPR);
20522 /* Parse an Objective-C protocol qualifier. */
20524 static tree
20525 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
20527 tree quals = NULL_TREE, node;
20528 cp_token *token = cp_lexer_peek_token (parser->lexer);
20530 node = token->u.value;
20532 while (node && TREE_CODE (node) == IDENTIFIER_NODE
20533 && (node == ridpointers [(int) RID_IN]
20534 || node == ridpointers [(int) RID_OUT]
20535 || node == ridpointers [(int) RID_INOUT]
20536 || node == ridpointers [(int) RID_BYCOPY]
20537 || node == ridpointers [(int) RID_BYREF]
20538 || node == ridpointers [(int) RID_ONEWAY]))
20540 quals = tree_cons (NULL_TREE, node, quals);
20541 cp_lexer_consume_token (parser->lexer);
20542 token = cp_lexer_peek_token (parser->lexer);
20543 node = token->u.value;
20546 return quals;
20549 /* Parse an Objective-C typename. */
20551 static tree
20552 cp_parser_objc_typename (cp_parser* parser)
20554 tree type_name = NULL_TREE;
20556 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20558 tree proto_quals, cp_type = NULL_TREE;
20560 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
20561 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
20563 /* An ObjC type name may consist of just protocol qualifiers, in which
20564 case the type shall default to 'id'. */
20565 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
20566 cp_type = cp_parser_type_id (parser);
20568 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20569 type_name = build_tree_list (proto_quals, cp_type);
20572 return type_name;
20575 /* Check to see if TYPE refers to an Objective-C selector name. */
20577 static bool
20578 cp_parser_objc_selector_p (enum cpp_ttype type)
20580 return (type == CPP_NAME || type == CPP_KEYWORD
20581 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
20582 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
20583 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
20584 || type == CPP_XOR || type == CPP_XOR_EQ);
20587 /* Parse an Objective-C selector. */
20589 static tree
20590 cp_parser_objc_selector (cp_parser* parser)
20592 cp_token *token = cp_lexer_consume_token (parser->lexer);
20594 if (!cp_parser_objc_selector_p (token->type))
20596 error_at (token->location, "invalid Objective-C++ selector name");
20597 return error_mark_node;
20600 /* C++ operator names are allowed to appear in ObjC selectors. */
20601 switch (token->type)
20603 case CPP_AND_AND: return get_identifier ("and");
20604 case CPP_AND_EQ: return get_identifier ("and_eq");
20605 case CPP_AND: return get_identifier ("bitand");
20606 case CPP_OR: return get_identifier ("bitor");
20607 case CPP_COMPL: return get_identifier ("compl");
20608 case CPP_NOT: return get_identifier ("not");
20609 case CPP_NOT_EQ: return get_identifier ("not_eq");
20610 case CPP_OR_OR: return get_identifier ("or");
20611 case CPP_OR_EQ: return get_identifier ("or_eq");
20612 case CPP_XOR: return get_identifier ("xor");
20613 case CPP_XOR_EQ: return get_identifier ("xor_eq");
20614 default: return token->u.value;
20618 /* Parse an Objective-C params list. */
20620 static tree
20621 cp_parser_objc_method_keyword_params (cp_parser* parser)
20623 tree params = NULL_TREE;
20624 bool maybe_unary_selector_p = true;
20625 cp_token *token = cp_lexer_peek_token (parser->lexer);
20627 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
20629 tree selector = NULL_TREE, type_name, identifier;
20631 if (token->type != CPP_COLON)
20632 selector = cp_parser_objc_selector (parser);
20634 /* Detect if we have a unary selector. */
20635 if (maybe_unary_selector_p
20636 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
20637 return selector;
20639 maybe_unary_selector_p = false;
20640 cp_parser_require (parser, CPP_COLON, "%<:%>");
20641 type_name = cp_parser_objc_typename (parser);
20642 identifier = cp_parser_identifier (parser);
20644 params
20645 = chainon (params,
20646 objc_build_keyword_decl (selector,
20647 type_name,
20648 identifier));
20650 token = cp_lexer_peek_token (parser->lexer);
20653 return params;
20656 /* Parse the non-keyword Objective-C params. */
20658 static tree
20659 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
20661 tree params = make_node (TREE_LIST);
20662 cp_token *token = cp_lexer_peek_token (parser->lexer);
20663 *ellipsisp = false; /* Initially, assume no ellipsis. */
20665 while (token->type == CPP_COMMA)
20667 cp_parameter_declarator *parmdecl;
20668 tree parm;
20670 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
20671 token = cp_lexer_peek_token (parser->lexer);
20673 if (token->type == CPP_ELLIPSIS)
20675 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
20676 *ellipsisp = true;
20677 break;
20680 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20681 parm = grokdeclarator (parmdecl->declarator,
20682 &parmdecl->decl_specifiers,
20683 PARM, /*initialized=*/0,
20684 /*attrlist=*/NULL);
20686 chainon (params, build_tree_list (NULL_TREE, parm));
20687 token = cp_lexer_peek_token (parser->lexer);
20690 return params;
20693 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
20695 static void
20696 cp_parser_objc_interstitial_code (cp_parser* parser)
20698 cp_token *token = cp_lexer_peek_token (parser->lexer);
20700 /* If the next token is `extern' and the following token is a string
20701 literal, then we have a linkage specification. */
20702 if (token->keyword == RID_EXTERN
20703 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
20704 cp_parser_linkage_specification (parser);
20705 /* Handle #pragma, if any. */
20706 else if (token->type == CPP_PRAGMA)
20707 cp_parser_pragma (parser, pragma_external);
20708 /* Allow stray semicolons. */
20709 else if (token->type == CPP_SEMICOLON)
20710 cp_lexer_consume_token (parser->lexer);
20711 /* Finally, try to parse a block-declaration, or a function-definition. */
20712 else
20713 cp_parser_block_declaration (parser, /*statement_p=*/false);
20716 /* Parse a method signature. */
20718 static tree
20719 cp_parser_objc_method_signature (cp_parser* parser)
20721 tree rettype, kwdparms, optparms;
20722 bool ellipsis = false;
20724 cp_parser_objc_method_type (parser);
20725 rettype = cp_parser_objc_typename (parser);
20726 kwdparms = cp_parser_objc_method_keyword_params (parser);
20727 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
20729 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
20732 /* Pars an Objective-C method prototype list. */
20734 static void
20735 cp_parser_objc_method_prototype_list (cp_parser* parser)
20737 cp_token *token = cp_lexer_peek_token (parser->lexer);
20739 while (token->keyword != RID_AT_END)
20741 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
20743 objc_add_method_declaration
20744 (cp_parser_objc_method_signature (parser));
20745 cp_parser_consume_semicolon_at_end_of_statement (parser);
20747 else
20748 /* Allow for interspersed non-ObjC++ code. */
20749 cp_parser_objc_interstitial_code (parser);
20751 token = cp_lexer_peek_token (parser->lexer);
20754 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
20755 objc_finish_interface ();
20758 /* Parse an Objective-C method definition list. */
20760 static void
20761 cp_parser_objc_method_definition_list (cp_parser* parser)
20763 cp_token *token = cp_lexer_peek_token (parser->lexer);
20765 while (token->keyword != RID_AT_END)
20767 tree meth;
20769 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
20771 push_deferring_access_checks (dk_deferred);
20772 objc_start_method_definition
20773 (cp_parser_objc_method_signature (parser));
20775 /* For historical reasons, we accept an optional semicolon. */
20776 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20777 cp_lexer_consume_token (parser->lexer);
20779 perform_deferred_access_checks ();
20780 stop_deferring_access_checks ();
20781 meth = cp_parser_function_definition_after_declarator (parser,
20782 false);
20783 pop_deferring_access_checks ();
20784 objc_finish_method_definition (meth);
20786 else
20787 /* Allow for interspersed non-ObjC++ code. */
20788 cp_parser_objc_interstitial_code (parser);
20790 token = cp_lexer_peek_token (parser->lexer);
20793 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
20794 objc_finish_implementation ();
20797 /* Parse Objective-C ivars. */
20799 static void
20800 cp_parser_objc_class_ivars (cp_parser* parser)
20802 cp_token *token = cp_lexer_peek_token (parser->lexer);
20804 if (token->type != CPP_OPEN_BRACE)
20805 return; /* No ivars specified. */
20807 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
20808 token = cp_lexer_peek_token (parser->lexer);
20810 while (token->type != CPP_CLOSE_BRACE)
20812 cp_decl_specifier_seq declspecs;
20813 int decl_class_or_enum_p;
20814 tree prefix_attributes;
20816 cp_parser_objc_visibility_spec (parser);
20818 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
20819 break;
20821 cp_parser_decl_specifier_seq (parser,
20822 CP_PARSER_FLAGS_OPTIONAL,
20823 &declspecs,
20824 &decl_class_or_enum_p);
20825 prefix_attributes = declspecs.attributes;
20826 declspecs.attributes = NULL_TREE;
20828 /* Keep going until we hit the `;' at the end of the
20829 declaration. */
20830 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20832 tree width = NULL_TREE, attributes, first_attribute, decl;
20833 cp_declarator *declarator = NULL;
20834 int ctor_dtor_or_conv_p;
20836 /* Check for a (possibly unnamed) bitfield declaration. */
20837 token = cp_lexer_peek_token (parser->lexer);
20838 if (token->type == CPP_COLON)
20839 goto eat_colon;
20841 if (token->type == CPP_NAME
20842 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
20843 == CPP_COLON))
20845 /* Get the name of the bitfield. */
20846 declarator = make_id_declarator (NULL_TREE,
20847 cp_parser_identifier (parser),
20848 sfk_none);
20850 eat_colon:
20851 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
20852 /* Get the width of the bitfield. */
20853 width
20854 = cp_parser_constant_expression (parser,
20855 /*allow_non_constant=*/false,
20856 NULL);
20858 else
20860 /* Parse the declarator. */
20861 declarator
20862 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20863 &ctor_dtor_or_conv_p,
20864 /*parenthesized_p=*/NULL,
20865 /*member_p=*/false);
20868 /* Look for attributes that apply to the ivar. */
20869 attributes = cp_parser_attributes_opt (parser);
20870 /* Remember which attributes are prefix attributes and
20871 which are not. */
20872 first_attribute = attributes;
20873 /* Combine the attributes. */
20874 attributes = chainon (prefix_attributes, attributes);
20876 if (width)
20877 /* Create the bitfield declaration. */
20878 decl = grokbitfield (declarator, &declspecs,
20879 width,
20880 attributes);
20881 else
20882 decl = grokfield (declarator, &declspecs,
20883 NULL_TREE, /*init_const_expr_p=*/false,
20884 NULL_TREE, attributes);
20886 /* Add the instance variable. */
20887 objc_add_instance_variable (decl);
20889 /* Reset PREFIX_ATTRIBUTES. */
20890 while (attributes && TREE_CHAIN (attributes) != first_attribute)
20891 attributes = TREE_CHAIN (attributes);
20892 if (attributes)
20893 TREE_CHAIN (attributes) = NULL_TREE;
20895 token = cp_lexer_peek_token (parser->lexer);
20897 if (token->type == CPP_COMMA)
20899 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
20900 continue;
20902 break;
20905 cp_parser_consume_semicolon_at_end_of_statement (parser);
20906 token = cp_lexer_peek_token (parser->lexer);
20909 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
20910 /* For historical reasons, we accept an optional semicolon. */
20911 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20912 cp_lexer_consume_token (parser->lexer);
20915 /* Parse an Objective-C protocol declaration. */
20917 static void
20918 cp_parser_objc_protocol_declaration (cp_parser* parser)
20920 tree proto, protorefs;
20921 cp_token *tok;
20923 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
20924 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
20926 tok = cp_lexer_peek_token (parser->lexer);
20927 error_at (tok->location, "identifier expected after %<@protocol%>");
20928 goto finish;
20931 /* See if we have a forward declaration or a definition. */
20932 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
20934 /* Try a forward declaration first. */
20935 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
20937 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
20938 finish:
20939 cp_parser_consume_semicolon_at_end_of_statement (parser);
20942 /* Ok, we got a full-fledged definition (or at least should). */
20943 else
20945 proto = cp_parser_identifier (parser);
20946 protorefs = cp_parser_objc_protocol_refs_opt (parser);
20947 objc_start_protocol (proto, protorefs);
20948 cp_parser_objc_method_prototype_list (parser);
20952 /* Parse an Objective-C superclass or category. */
20954 static void
20955 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
20956 tree *categ)
20958 cp_token *next = cp_lexer_peek_token (parser->lexer);
20960 *super = *categ = NULL_TREE;
20961 if (next->type == CPP_COLON)
20963 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
20964 *super = cp_parser_identifier (parser);
20966 else if (next->type == CPP_OPEN_PAREN)
20968 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
20969 *categ = cp_parser_identifier (parser);
20970 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20974 /* Parse an Objective-C class interface. */
20976 static void
20977 cp_parser_objc_class_interface (cp_parser* parser)
20979 tree name, super, categ, protos;
20981 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
20982 name = cp_parser_identifier (parser);
20983 cp_parser_objc_superclass_or_category (parser, &super, &categ);
20984 protos = cp_parser_objc_protocol_refs_opt (parser);
20986 /* We have either a class or a category on our hands. */
20987 if (categ)
20988 objc_start_category_interface (name, categ, protos);
20989 else
20991 objc_start_class_interface (name, super, protos);
20992 /* Handle instance variable declarations, if any. */
20993 cp_parser_objc_class_ivars (parser);
20994 objc_continue_interface ();
20997 cp_parser_objc_method_prototype_list (parser);
21000 /* Parse an Objective-C class implementation. */
21002 static void
21003 cp_parser_objc_class_implementation (cp_parser* parser)
21005 tree name, super, categ;
21007 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
21008 name = cp_parser_identifier (parser);
21009 cp_parser_objc_superclass_or_category (parser, &super, &categ);
21011 /* We have either a class or a category on our hands. */
21012 if (categ)
21013 objc_start_category_implementation (name, categ);
21014 else
21016 objc_start_class_implementation (name, super);
21017 /* Handle instance variable declarations, if any. */
21018 cp_parser_objc_class_ivars (parser);
21019 objc_continue_implementation ();
21022 cp_parser_objc_method_definition_list (parser);
21025 /* Consume the @end token and finish off the implementation. */
21027 static void
21028 cp_parser_objc_end_implementation (cp_parser* parser)
21030 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
21031 objc_finish_implementation ();
21034 /* Parse an Objective-C declaration. */
21036 static void
21037 cp_parser_objc_declaration (cp_parser* parser)
21039 /* Try to figure out what kind of declaration is present. */
21040 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21042 switch (kwd->keyword)
21044 case RID_AT_ALIAS:
21045 cp_parser_objc_alias_declaration (parser);
21046 break;
21047 case RID_AT_CLASS:
21048 cp_parser_objc_class_declaration (parser);
21049 break;
21050 case RID_AT_PROTOCOL:
21051 cp_parser_objc_protocol_declaration (parser);
21052 break;
21053 case RID_AT_INTERFACE:
21054 cp_parser_objc_class_interface (parser);
21055 break;
21056 case RID_AT_IMPLEMENTATION:
21057 cp_parser_objc_class_implementation (parser);
21058 break;
21059 case RID_AT_END:
21060 cp_parser_objc_end_implementation (parser);
21061 break;
21062 default:
21063 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
21064 kwd->u.value);
21065 cp_parser_skip_to_end_of_block_or_statement (parser);
21069 /* Parse an Objective-C try-catch-finally statement.
21071 objc-try-catch-finally-stmt:
21072 @try compound-statement objc-catch-clause-seq [opt]
21073 objc-finally-clause [opt]
21075 objc-catch-clause-seq:
21076 objc-catch-clause objc-catch-clause-seq [opt]
21078 objc-catch-clause:
21079 @catch ( exception-declaration ) compound-statement
21081 objc-finally-clause
21082 @finally compound-statement
21084 Returns NULL_TREE. */
21086 static tree
21087 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
21088 location_t location;
21089 tree stmt;
21091 cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
21092 location = cp_lexer_peek_token (parser->lexer)->location;
21093 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
21094 node, lest it get absorbed into the surrounding block. */
21095 stmt = push_stmt_list ();
21096 cp_parser_compound_statement (parser, NULL, false);
21097 objc_begin_try_stmt (location, pop_stmt_list (stmt));
21099 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
21101 cp_parameter_declarator *parmdecl;
21102 tree parm;
21104 cp_lexer_consume_token (parser->lexer);
21105 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
21106 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
21107 parm = grokdeclarator (parmdecl->declarator,
21108 &parmdecl->decl_specifiers,
21109 PARM, /*initialized=*/0,
21110 /*attrlist=*/NULL);
21111 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
21112 objc_begin_catch_clause (parm);
21113 cp_parser_compound_statement (parser, NULL, false);
21114 objc_finish_catch_clause ();
21117 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
21119 cp_lexer_consume_token (parser->lexer);
21120 location = cp_lexer_peek_token (parser->lexer)->location;
21121 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
21122 node, lest it get absorbed into the surrounding block. */
21123 stmt = push_stmt_list ();
21124 cp_parser_compound_statement (parser, NULL, false);
21125 objc_build_finally_clause (location, pop_stmt_list (stmt));
21128 return objc_finish_try_stmt ();
21131 /* Parse an Objective-C synchronized statement.
21133 objc-synchronized-stmt:
21134 @synchronized ( expression ) compound-statement
21136 Returns NULL_TREE. */
21138 static tree
21139 cp_parser_objc_synchronized_statement (cp_parser *parser) {
21140 location_t location;
21141 tree lock, stmt;
21143 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
21145 location = cp_lexer_peek_token (parser->lexer)->location;
21146 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
21147 lock = cp_parser_expression (parser, false, NULL);
21148 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
21150 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
21151 node, lest it get absorbed into the surrounding block. */
21152 stmt = push_stmt_list ();
21153 cp_parser_compound_statement (parser, NULL, false);
21155 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
21158 /* Parse an Objective-C throw statement.
21160 objc-throw-stmt:
21161 @throw assignment-expression [opt] ;
21163 Returns a constructed '@throw' statement. */
21165 static tree
21166 cp_parser_objc_throw_statement (cp_parser *parser) {
21167 tree expr = NULL_TREE;
21168 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21170 cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
21172 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21173 expr = cp_parser_assignment_expression (parser, false, NULL);
21175 cp_parser_consume_semicolon_at_end_of_statement (parser);
21177 return objc_build_throw_stmt (loc, expr);
21180 /* Parse an Objective-C statement. */
21182 static tree
21183 cp_parser_objc_statement (cp_parser * parser) {
21184 /* Try to figure out what kind of declaration is present. */
21185 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21187 switch (kwd->keyword)
21189 case RID_AT_TRY:
21190 return cp_parser_objc_try_catch_finally_statement (parser);
21191 case RID_AT_SYNCHRONIZED:
21192 return cp_parser_objc_synchronized_statement (parser);
21193 case RID_AT_THROW:
21194 return cp_parser_objc_throw_statement (parser);
21195 default:
21196 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
21197 kwd->u.value);
21198 cp_parser_skip_to_end_of_block_or_statement (parser);
21201 return error_mark_node;
21204 /* OpenMP 2.5 parsing routines. */
21206 /* Returns name of the next clause.
21207 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
21208 the token is not consumed. Otherwise appropriate pragma_omp_clause is
21209 returned and the token is consumed. */
21211 static pragma_omp_clause
21212 cp_parser_omp_clause_name (cp_parser *parser)
21214 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
21216 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
21217 result = PRAGMA_OMP_CLAUSE_IF;
21218 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
21219 result = PRAGMA_OMP_CLAUSE_DEFAULT;
21220 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
21221 result = PRAGMA_OMP_CLAUSE_PRIVATE;
21222 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21224 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21225 const char *p = IDENTIFIER_POINTER (id);
21227 switch (p[0])
21229 case 'c':
21230 if (!strcmp ("collapse", p))
21231 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
21232 else if (!strcmp ("copyin", p))
21233 result = PRAGMA_OMP_CLAUSE_COPYIN;
21234 else if (!strcmp ("copyprivate", p))
21235 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
21236 break;
21237 case 'f':
21238 if (!strcmp ("firstprivate", p))
21239 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
21240 break;
21241 case 'l':
21242 if (!strcmp ("lastprivate", p))
21243 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
21244 break;
21245 case 'n':
21246 if (!strcmp ("nowait", p))
21247 result = PRAGMA_OMP_CLAUSE_NOWAIT;
21248 else if (!strcmp ("num_threads", p))
21249 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
21250 break;
21251 case 'o':
21252 if (!strcmp ("ordered", p))
21253 result = PRAGMA_OMP_CLAUSE_ORDERED;
21254 break;
21255 case 'r':
21256 if (!strcmp ("reduction", p))
21257 result = PRAGMA_OMP_CLAUSE_REDUCTION;
21258 break;
21259 case 's':
21260 if (!strcmp ("schedule", p))
21261 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
21262 else if (!strcmp ("shared", p))
21263 result = PRAGMA_OMP_CLAUSE_SHARED;
21264 break;
21265 case 'u':
21266 if (!strcmp ("untied", p))
21267 result = PRAGMA_OMP_CLAUSE_UNTIED;
21268 break;
21272 if (result != PRAGMA_OMP_CLAUSE_NONE)
21273 cp_lexer_consume_token (parser->lexer);
21275 return result;
21278 /* Validate that a clause of the given type does not already exist. */
21280 static void
21281 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
21282 const char *name, location_t location)
21284 tree c;
21286 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21287 if (OMP_CLAUSE_CODE (c) == code)
21289 error_at (location, "too many %qs clauses", name);
21290 break;
21294 /* OpenMP 2.5:
21295 variable-list:
21296 identifier
21297 variable-list , identifier
21299 In addition, we match a closing parenthesis. An opening parenthesis
21300 will have been consumed by the caller.
21302 If KIND is nonzero, create the appropriate node and install the decl
21303 in OMP_CLAUSE_DECL and add the node to the head of the list.
21305 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
21306 return the list created. */
21308 static tree
21309 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
21310 tree list)
21312 cp_token *token;
21313 while (1)
21315 tree name, decl;
21317 token = cp_lexer_peek_token (parser->lexer);
21318 name = cp_parser_id_expression (parser, /*template_p=*/false,
21319 /*check_dependency_p=*/true,
21320 /*template_p=*/NULL,
21321 /*declarator_p=*/false,
21322 /*optional_p=*/false);
21323 if (name == error_mark_node)
21324 goto skip_comma;
21326 decl = cp_parser_lookup_name_simple (parser, name, token->location);
21327 if (decl == error_mark_node)
21328 cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
21329 else if (kind != 0)
21331 tree u = build_omp_clause (token->location, kind);
21332 OMP_CLAUSE_DECL (u) = decl;
21333 OMP_CLAUSE_CHAIN (u) = list;
21334 list = u;
21336 else
21337 list = tree_cons (decl, NULL_TREE, list);
21339 get_comma:
21340 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21341 break;
21342 cp_lexer_consume_token (parser->lexer);
21345 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21347 int ending;
21349 /* Try to resync to an unnested comma. Copied from
21350 cp_parser_parenthesized_expression_list. */
21351 skip_comma:
21352 ending = cp_parser_skip_to_closing_parenthesis (parser,
21353 /*recovering=*/true,
21354 /*or_comma=*/true,
21355 /*consume_paren=*/true);
21356 if (ending < 0)
21357 goto get_comma;
21360 return list;
21363 /* Similarly, but expect leading and trailing parenthesis. This is a very
21364 common case for omp clauses. */
21366 static tree
21367 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
21369 if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21370 return cp_parser_omp_var_list_no_open (parser, kind, list);
21371 return list;
21374 /* OpenMP 3.0:
21375 collapse ( constant-expression ) */
21377 static tree
21378 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
21380 tree c, num;
21381 location_t loc;
21382 HOST_WIDE_INT n;
21384 loc = cp_lexer_peek_token (parser->lexer)->location;
21385 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21386 return list;
21388 num = cp_parser_constant_expression (parser, false, NULL);
21390 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21391 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21392 /*or_comma=*/false,
21393 /*consume_paren=*/true);
21395 if (num == error_mark_node)
21396 return list;
21397 num = fold_non_dependent_expr (num);
21398 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
21399 || !host_integerp (num, 0)
21400 || (n = tree_low_cst (num, 0)) <= 0
21401 || (int) n != n)
21403 error_at (loc, "collapse argument needs positive constant integer expression");
21404 return list;
21407 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
21408 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
21409 OMP_CLAUSE_CHAIN (c) = list;
21410 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
21412 return c;
21415 /* OpenMP 2.5:
21416 default ( shared | none ) */
21418 static tree
21419 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
21421 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
21422 tree c;
21424 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21425 return list;
21426 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21428 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21429 const char *p = IDENTIFIER_POINTER (id);
21431 switch (p[0])
21433 case 'n':
21434 if (strcmp ("none", p) != 0)
21435 goto invalid_kind;
21436 kind = OMP_CLAUSE_DEFAULT_NONE;
21437 break;
21439 case 's':
21440 if (strcmp ("shared", p) != 0)
21441 goto invalid_kind;
21442 kind = OMP_CLAUSE_DEFAULT_SHARED;
21443 break;
21445 default:
21446 goto invalid_kind;
21449 cp_lexer_consume_token (parser->lexer);
21451 else
21453 invalid_kind:
21454 cp_parser_error (parser, "expected %<none%> or %<shared%>");
21457 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21458 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21459 /*or_comma=*/false,
21460 /*consume_paren=*/true);
21462 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
21463 return list;
21465 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
21466 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
21467 OMP_CLAUSE_CHAIN (c) = list;
21468 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
21470 return c;
21473 /* OpenMP 2.5:
21474 if ( expression ) */
21476 static tree
21477 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
21479 tree t, c;
21481 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21482 return list;
21484 t = cp_parser_condition (parser);
21486 if (t == error_mark_node
21487 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21488 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21489 /*or_comma=*/false,
21490 /*consume_paren=*/true);
21492 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
21494 c = build_omp_clause (location, OMP_CLAUSE_IF);
21495 OMP_CLAUSE_IF_EXPR (c) = t;
21496 OMP_CLAUSE_CHAIN (c) = list;
21498 return c;
21501 /* OpenMP 2.5:
21502 nowait */
21504 static tree
21505 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
21506 tree list, location_t location)
21508 tree c;
21510 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
21512 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
21513 OMP_CLAUSE_CHAIN (c) = list;
21514 return c;
21517 /* OpenMP 2.5:
21518 num_threads ( expression ) */
21520 static tree
21521 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
21522 location_t location)
21524 tree t, c;
21526 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21527 return list;
21529 t = cp_parser_expression (parser, false, NULL);
21531 if (t == error_mark_node
21532 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21533 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21534 /*or_comma=*/false,
21535 /*consume_paren=*/true);
21537 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
21538 "num_threads", location);
21540 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
21541 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
21542 OMP_CLAUSE_CHAIN (c) = list;
21544 return c;
21547 /* OpenMP 2.5:
21548 ordered */
21550 static tree
21551 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
21552 tree list, location_t location)
21554 tree c;
21556 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
21557 "ordered", location);
21559 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
21560 OMP_CLAUSE_CHAIN (c) = list;
21561 return c;
21564 /* OpenMP 2.5:
21565 reduction ( reduction-operator : variable-list )
21567 reduction-operator:
21568 One of: + * - & ^ | && || */
21570 static tree
21571 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
21573 enum tree_code code;
21574 tree nlist, c;
21576 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21577 return list;
21579 switch (cp_lexer_peek_token (parser->lexer)->type)
21581 case CPP_PLUS:
21582 code = PLUS_EXPR;
21583 break;
21584 case CPP_MULT:
21585 code = MULT_EXPR;
21586 break;
21587 case CPP_MINUS:
21588 code = MINUS_EXPR;
21589 break;
21590 case CPP_AND:
21591 code = BIT_AND_EXPR;
21592 break;
21593 case CPP_XOR:
21594 code = BIT_XOR_EXPR;
21595 break;
21596 case CPP_OR:
21597 code = BIT_IOR_EXPR;
21598 break;
21599 case CPP_AND_AND:
21600 code = TRUTH_ANDIF_EXPR;
21601 break;
21602 case CPP_OR_OR:
21603 code = TRUTH_ORIF_EXPR;
21604 break;
21605 default:
21606 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
21607 "%<|%>, %<&&%>, or %<||%>");
21608 resync_fail:
21609 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21610 /*or_comma=*/false,
21611 /*consume_paren=*/true);
21612 return list;
21614 cp_lexer_consume_token (parser->lexer);
21616 if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
21617 goto resync_fail;
21619 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
21620 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
21621 OMP_CLAUSE_REDUCTION_CODE (c) = code;
21623 return nlist;
21626 /* OpenMP 2.5:
21627 schedule ( schedule-kind )
21628 schedule ( schedule-kind , expression )
21630 schedule-kind:
21631 static | dynamic | guided | runtime | auto */
21633 static tree
21634 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
21636 tree c, t;
21638 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21639 return list;
21641 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
21643 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21645 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21646 const char *p = IDENTIFIER_POINTER (id);
21648 switch (p[0])
21650 case 'd':
21651 if (strcmp ("dynamic", p) != 0)
21652 goto invalid_kind;
21653 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
21654 break;
21656 case 'g':
21657 if (strcmp ("guided", p) != 0)
21658 goto invalid_kind;
21659 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
21660 break;
21662 case 'r':
21663 if (strcmp ("runtime", p) != 0)
21664 goto invalid_kind;
21665 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
21666 break;
21668 default:
21669 goto invalid_kind;
21672 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
21673 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
21674 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
21675 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
21676 else
21677 goto invalid_kind;
21678 cp_lexer_consume_token (parser->lexer);
21680 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21682 cp_token *token;
21683 cp_lexer_consume_token (parser->lexer);
21685 token = cp_lexer_peek_token (parser->lexer);
21686 t = cp_parser_assignment_expression (parser, false, NULL);
21688 if (t == error_mark_node)
21689 goto resync_fail;
21690 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
21691 error_at (token->location, "schedule %<runtime%> does not take "
21692 "a %<chunk_size%> parameter");
21693 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
21694 error_at (token->location, "schedule %<auto%> does not take "
21695 "a %<chunk_size%> parameter");
21696 else
21697 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
21699 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21700 goto resync_fail;
21702 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
21703 goto resync_fail;
21705 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
21706 OMP_CLAUSE_CHAIN (c) = list;
21707 return c;
21709 invalid_kind:
21710 cp_parser_error (parser, "invalid schedule kind");
21711 resync_fail:
21712 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21713 /*or_comma=*/false,
21714 /*consume_paren=*/true);
21715 return list;
21718 /* OpenMP 3.0:
21719 untied */
21721 static tree
21722 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
21723 tree list, location_t location)
21725 tree c;
21727 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
21729 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
21730 OMP_CLAUSE_CHAIN (c) = list;
21731 return c;
21734 /* Parse all OpenMP clauses. The set clauses allowed by the directive
21735 is a bitmask in MASK. Return the list of clauses found; the result
21736 of clause default goes in *pdefault. */
21738 static tree
21739 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
21740 const char *where, cp_token *pragma_tok)
21742 tree clauses = NULL;
21743 bool first = true;
21744 cp_token *token = NULL;
21746 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
21748 pragma_omp_clause c_kind;
21749 const char *c_name;
21750 tree prev = clauses;
21752 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21753 cp_lexer_consume_token (parser->lexer);
21755 token = cp_lexer_peek_token (parser->lexer);
21756 c_kind = cp_parser_omp_clause_name (parser);
21757 first = false;
21759 switch (c_kind)
21761 case PRAGMA_OMP_CLAUSE_COLLAPSE:
21762 clauses = cp_parser_omp_clause_collapse (parser, clauses,
21763 token->location);
21764 c_name = "collapse";
21765 break;
21766 case PRAGMA_OMP_CLAUSE_COPYIN:
21767 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
21768 c_name = "copyin";
21769 break;
21770 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
21771 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
21772 clauses);
21773 c_name = "copyprivate";
21774 break;
21775 case PRAGMA_OMP_CLAUSE_DEFAULT:
21776 clauses = cp_parser_omp_clause_default (parser, clauses,
21777 token->location);
21778 c_name = "default";
21779 break;
21780 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
21781 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
21782 clauses);
21783 c_name = "firstprivate";
21784 break;
21785 case PRAGMA_OMP_CLAUSE_IF:
21786 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
21787 c_name = "if";
21788 break;
21789 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
21790 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
21791 clauses);
21792 c_name = "lastprivate";
21793 break;
21794 case PRAGMA_OMP_CLAUSE_NOWAIT:
21795 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
21796 c_name = "nowait";
21797 break;
21798 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
21799 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
21800 token->location);
21801 c_name = "num_threads";
21802 break;
21803 case PRAGMA_OMP_CLAUSE_ORDERED:
21804 clauses = cp_parser_omp_clause_ordered (parser, clauses,
21805 token->location);
21806 c_name = "ordered";
21807 break;
21808 case PRAGMA_OMP_CLAUSE_PRIVATE:
21809 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
21810 clauses);
21811 c_name = "private";
21812 break;
21813 case PRAGMA_OMP_CLAUSE_REDUCTION:
21814 clauses = cp_parser_omp_clause_reduction (parser, clauses);
21815 c_name = "reduction";
21816 break;
21817 case PRAGMA_OMP_CLAUSE_SCHEDULE:
21818 clauses = cp_parser_omp_clause_schedule (parser, clauses,
21819 token->location);
21820 c_name = "schedule";
21821 break;
21822 case PRAGMA_OMP_CLAUSE_SHARED:
21823 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
21824 clauses);
21825 c_name = "shared";
21826 break;
21827 case PRAGMA_OMP_CLAUSE_UNTIED:
21828 clauses = cp_parser_omp_clause_untied (parser, clauses,
21829 token->location);
21830 c_name = "nowait";
21831 break;
21832 default:
21833 cp_parser_error (parser, "expected %<#pragma omp%> clause");
21834 goto saw_error;
21837 if (((mask >> c_kind) & 1) == 0)
21839 /* Remove the invalid clause(s) from the list to avoid
21840 confusing the rest of the compiler. */
21841 clauses = prev;
21842 error_at (token->location, "%qs is not valid for %qs", c_name, where);
21845 saw_error:
21846 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
21847 return finish_omp_clauses (clauses);
21850 /* OpenMP 2.5:
21851 structured-block:
21852 statement
21854 In practice, we're also interested in adding the statement to an
21855 outer node. So it is convenient if we work around the fact that
21856 cp_parser_statement calls add_stmt. */
21858 static unsigned
21859 cp_parser_begin_omp_structured_block (cp_parser *parser)
21861 unsigned save = parser->in_statement;
21863 /* Only move the values to IN_OMP_BLOCK if they weren't false.
21864 This preserves the "not within loop or switch" style error messages
21865 for nonsense cases like
21866 void foo() {
21867 #pragma omp single
21868 break;
21871 if (parser->in_statement)
21872 parser->in_statement = IN_OMP_BLOCK;
21874 return save;
21877 static void
21878 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
21880 parser->in_statement = save;
21883 static tree
21884 cp_parser_omp_structured_block (cp_parser *parser)
21886 tree stmt = begin_omp_structured_block ();
21887 unsigned int save = cp_parser_begin_omp_structured_block (parser);
21889 cp_parser_statement (parser, NULL_TREE, false, NULL);
21891 cp_parser_end_omp_structured_block (parser, save);
21892 return finish_omp_structured_block (stmt);
21895 /* OpenMP 2.5:
21896 # pragma omp atomic new-line
21897 expression-stmt
21899 expression-stmt:
21900 x binop= expr | x++ | ++x | x-- | --x
21901 binop:
21902 +, *, -, /, &, ^, |, <<, >>
21904 where x is an lvalue expression with scalar type. */
21906 static void
21907 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
21909 tree lhs, rhs;
21910 enum tree_code code;
21912 cp_parser_require_pragma_eol (parser, pragma_tok);
21914 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
21915 /*cast_p=*/false, NULL);
21916 switch (TREE_CODE (lhs))
21918 case ERROR_MARK:
21919 goto saw_error;
21921 case PREINCREMENT_EXPR:
21922 case POSTINCREMENT_EXPR:
21923 lhs = TREE_OPERAND (lhs, 0);
21924 code = PLUS_EXPR;
21925 rhs = integer_one_node;
21926 break;
21928 case PREDECREMENT_EXPR:
21929 case POSTDECREMENT_EXPR:
21930 lhs = TREE_OPERAND (lhs, 0);
21931 code = MINUS_EXPR;
21932 rhs = integer_one_node;
21933 break;
21935 default:
21936 switch (cp_lexer_peek_token (parser->lexer)->type)
21938 case CPP_MULT_EQ:
21939 code = MULT_EXPR;
21940 break;
21941 case CPP_DIV_EQ:
21942 code = TRUNC_DIV_EXPR;
21943 break;
21944 case CPP_PLUS_EQ:
21945 code = PLUS_EXPR;
21946 break;
21947 case CPP_MINUS_EQ:
21948 code = MINUS_EXPR;
21949 break;
21950 case CPP_LSHIFT_EQ:
21951 code = LSHIFT_EXPR;
21952 break;
21953 case CPP_RSHIFT_EQ:
21954 code = RSHIFT_EXPR;
21955 break;
21956 case CPP_AND_EQ:
21957 code = BIT_AND_EXPR;
21958 break;
21959 case CPP_OR_EQ:
21960 code = BIT_IOR_EXPR;
21961 break;
21962 case CPP_XOR_EQ:
21963 code = BIT_XOR_EXPR;
21964 break;
21965 default:
21966 cp_parser_error (parser,
21967 "invalid operator for %<#pragma omp atomic%>");
21968 goto saw_error;
21970 cp_lexer_consume_token (parser->lexer);
21972 rhs = cp_parser_expression (parser, false, NULL);
21973 if (rhs == error_mark_node)
21974 goto saw_error;
21975 break;
21977 finish_omp_atomic (code, lhs, rhs);
21978 cp_parser_consume_semicolon_at_end_of_statement (parser);
21979 return;
21981 saw_error:
21982 cp_parser_skip_to_end_of_block_or_statement (parser);
21986 /* OpenMP 2.5:
21987 # pragma omp barrier new-line */
21989 static void
21990 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
21992 cp_parser_require_pragma_eol (parser, pragma_tok);
21993 finish_omp_barrier ();
21996 /* OpenMP 2.5:
21997 # pragma omp critical [(name)] new-line
21998 structured-block */
22000 static tree
22001 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
22003 tree stmt, name = NULL;
22005 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22007 cp_lexer_consume_token (parser->lexer);
22009 name = cp_parser_identifier (parser);
22011 if (name == error_mark_node
22012 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
22013 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22014 /*or_comma=*/false,
22015 /*consume_paren=*/true);
22016 if (name == error_mark_node)
22017 name = NULL;
22019 cp_parser_require_pragma_eol (parser, pragma_tok);
22021 stmt = cp_parser_omp_structured_block (parser);
22022 return c_finish_omp_critical (input_location, stmt, name);
22025 /* OpenMP 2.5:
22026 # pragma omp flush flush-vars[opt] new-line
22028 flush-vars:
22029 ( variable-list ) */
22031 static void
22032 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
22034 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22035 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
22036 cp_parser_require_pragma_eol (parser, pragma_tok);
22038 finish_omp_flush ();
22041 /* Helper function, to parse omp for increment expression. */
22043 static tree
22044 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
22046 tree cond = cp_parser_binary_expression (parser, false, true,
22047 PREC_NOT_OPERATOR, NULL);
22048 bool overloaded_p;
22050 if (cond == error_mark_node
22051 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22053 cp_parser_skip_to_end_of_statement (parser);
22054 return error_mark_node;
22057 switch (TREE_CODE (cond))
22059 case GT_EXPR:
22060 case GE_EXPR:
22061 case LT_EXPR:
22062 case LE_EXPR:
22063 break;
22064 default:
22065 return error_mark_node;
22068 /* If decl is an iterator, preserve LHS and RHS of the relational
22069 expr until finish_omp_for. */
22070 if (decl
22071 && (type_dependent_expression_p (decl)
22072 || CLASS_TYPE_P (TREE_TYPE (decl))))
22073 return cond;
22075 return build_x_binary_op (TREE_CODE (cond),
22076 TREE_OPERAND (cond, 0), ERROR_MARK,
22077 TREE_OPERAND (cond, 1), ERROR_MARK,
22078 &overloaded_p, tf_warning_or_error);
22081 /* Helper function, to parse omp for increment expression. */
22083 static tree
22084 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
22086 cp_token *token = cp_lexer_peek_token (parser->lexer);
22087 enum tree_code op;
22088 tree lhs, rhs;
22089 cp_id_kind idk;
22090 bool decl_first;
22092 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
22094 op = (token->type == CPP_PLUS_PLUS
22095 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
22096 cp_lexer_consume_token (parser->lexer);
22097 lhs = cp_parser_cast_expression (parser, false, false, NULL);
22098 if (lhs != decl)
22099 return error_mark_node;
22100 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
22103 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
22104 if (lhs != decl)
22105 return error_mark_node;
22107 token = cp_lexer_peek_token (parser->lexer);
22108 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
22110 op = (token->type == CPP_PLUS_PLUS
22111 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
22112 cp_lexer_consume_token (parser->lexer);
22113 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
22116 op = cp_parser_assignment_operator_opt (parser);
22117 if (op == ERROR_MARK)
22118 return error_mark_node;
22120 if (op != NOP_EXPR)
22122 rhs = cp_parser_assignment_expression (parser, false, NULL);
22123 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
22124 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
22127 lhs = cp_parser_binary_expression (parser, false, false,
22128 PREC_ADDITIVE_EXPRESSION, NULL);
22129 token = cp_lexer_peek_token (parser->lexer);
22130 decl_first = lhs == decl;
22131 if (decl_first)
22132 lhs = NULL_TREE;
22133 if (token->type != CPP_PLUS
22134 && token->type != CPP_MINUS)
22135 return error_mark_node;
22139 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
22140 cp_lexer_consume_token (parser->lexer);
22141 rhs = cp_parser_binary_expression (parser, false, false,
22142 PREC_ADDITIVE_EXPRESSION, NULL);
22143 token = cp_lexer_peek_token (parser->lexer);
22144 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
22146 if (lhs == NULL_TREE)
22148 if (op == PLUS_EXPR)
22149 lhs = rhs;
22150 else
22151 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
22153 else
22154 lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
22155 NULL, tf_warning_or_error);
22158 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
22160 if (!decl_first)
22162 if (rhs != decl || op == MINUS_EXPR)
22163 return error_mark_node;
22164 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
22166 else
22167 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
22169 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
22172 /* Parse the restricted form of the for statement allowed by OpenMP. */
22174 static tree
22175 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
22177 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
22178 tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
22179 tree this_pre_body, cl;
22180 location_t loc_first;
22181 bool collapse_err = false;
22182 int i, collapse = 1, nbraces = 0;
22184 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
22185 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
22186 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
22188 gcc_assert (collapse >= 1);
22190 declv = make_tree_vec (collapse);
22191 initv = make_tree_vec (collapse);
22192 condv = make_tree_vec (collapse);
22193 incrv = make_tree_vec (collapse);
22195 loc_first = cp_lexer_peek_token (parser->lexer)->location;
22197 for (i = 0; i < collapse; i++)
22199 int bracecount = 0;
22200 bool add_private_clause = false;
22201 location_t loc;
22203 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22205 cp_parser_error (parser, "for statement expected");
22206 return NULL;
22208 loc = cp_lexer_consume_token (parser->lexer)->location;
22210 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
22211 return NULL;
22213 init = decl = real_decl = NULL;
22214 this_pre_body = push_stmt_list ();
22215 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22217 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
22219 init-expr:
22220 var = lb
22221 integer-type var = lb
22222 random-access-iterator-type var = lb
22223 pointer-type var = lb
22225 cp_decl_specifier_seq type_specifiers;
22227 /* First, try to parse as an initialized declaration. See
22228 cp_parser_condition, from whence the bulk of this is copied. */
22230 cp_parser_parse_tentatively (parser);
22231 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
22232 /*is_trailing_return=*/false,
22233 &type_specifiers);
22234 if (cp_parser_parse_definitely (parser))
22236 /* If parsing a type specifier seq succeeded, then this
22237 MUST be a initialized declaration. */
22238 tree asm_specification, attributes;
22239 cp_declarator *declarator;
22241 declarator = cp_parser_declarator (parser,
22242 CP_PARSER_DECLARATOR_NAMED,
22243 /*ctor_dtor_or_conv_p=*/NULL,
22244 /*parenthesized_p=*/NULL,
22245 /*member_p=*/false);
22246 attributes = cp_parser_attributes_opt (parser);
22247 asm_specification = cp_parser_asm_specification_opt (parser);
22249 if (declarator == cp_error_declarator)
22250 cp_parser_skip_to_end_of_statement (parser);
22252 else
22254 tree pushed_scope, auto_node;
22256 decl = start_decl (declarator, &type_specifiers,
22257 SD_INITIALIZED, attributes,
22258 /*prefix_attributes=*/NULL_TREE,
22259 &pushed_scope);
22261 auto_node = type_uses_auto (TREE_TYPE (decl));
22262 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
22264 if (cp_lexer_next_token_is (parser->lexer,
22265 CPP_OPEN_PAREN))
22266 error ("parenthesized initialization is not allowed in "
22267 "OpenMP %<for%> loop");
22268 else
22269 /* Trigger an error. */
22270 cp_parser_require (parser, CPP_EQ, "%<=%>");
22272 init = error_mark_node;
22273 cp_parser_skip_to_end_of_statement (parser);
22275 else if (CLASS_TYPE_P (TREE_TYPE (decl))
22276 || type_dependent_expression_p (decl)
22277 || auto_node)
22279 bool is_direct_init, is_non_constant_init;
22281 init = cp_parser_initializer (parser,
22282 &is_direct_init,
22283 &is_non_constant_init);
22285 if (auto_node && describable_type (init))
22287 TREE_TYPE (decl)
22288 = do_auto_deduction (TREE_TYPE (decl), init,
22289 auto_node);
22291 if (!CLASS_TYPE_P (TREE_TYPE (decl))
22292 && !type_dependent_expression_p (decl))
22293 goto non_class;
22296 cp_finish_decl (decl, init, !is_non_constant_init,
22297 asm_specification,
22298 LOOKUP_ONLYCONVERTING);
22299 if (CLASS_TYPE_P (TREE_TYPE (decl)))
22301 for_block
22302 = tree_cons (NULL, this_pre_body, for_block);
22303 init = NULL_TREE;
22305 else
22306 init = pop_stmt_list (this_pre_body);
22307 this_pre_body = NULL_TREE;
22309 else
22311 /* Consume '='. */
22312 cp_lexer_consume_token (parser->lexer);
22313 init = cp_parser_assignment_expression (parser, false, NULL);
22315 non_class:
22316 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
22317 init = error_mark_node;
22318 else
22319 cp_finish_decl (decl, NULL_TREE,
22320 /*init_const_expr_p=*/false,
22321 asm_specification,
22322 LOOKUP_ONLYCONVERTING);
22325 if (pushed_scope)
22326 pop_scope (pushed_scope);
22329 else
22331 cp_id_kind idk;
22332 /* If parsing a type specifier sequence failed, then
22333 this MUST be a simple expression. */
22334 cp_parser_parse_tentatively (parser);
22335 decl = cp_parser_primary_expression (parser, false, false,
22336 false, &idk);
22337 if (!cp_parser_error_occurred (parser)
22338 && decl
22339 && DECL_P (decl)
22340 && CLASS_TYPE_P (TREE_TYPE (decl)))
22342 tree rhs;
22344 cp_parser_parse_definitely (parser);
22345 cp_parser_require (parser, CPP_EQ, "%<=%>");
22346 rhs = cp_parser_assignment_expression (parser, false, NULL);
22347 finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
22348 rhs,
22349 tf_warning_or_error));
22350 add_private_clause = true;
22352 else
22354 decl = NULL;
22355 cp_parser_abort_tentative_parse (parser);
22356 init = cp_parser_expression (parser, false, NULL);
22357 if (init)
22359 if (TREE_CODE (init) == MODIFY_EXPR
22360 || TREE_CODE (init) == MODOP_EXPR)
22361 real_decl = TREE_OPERAND (init, 0);
22366 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
22367 if (this_pre_body)
22369 this_pre_body = pop_stmt_list (this_pre_body);
22370 if (pre_body)
22372 tree t = pre_body;
22373 pre_body = push_stmt_list ();
22374 add_stmt (t);
22375 add_stmt (this_pre_body);
22376 pre_body = pop_stmt_list (pre_body);
22378 else
22379 pre_body = this_pre_body;
22382 if (decl)
22383 real_decl = decl;
22384 if (par_clauses != NULL && real_decl != NULL_TREE)
22386 tree *c;
22387 for (c = par_clauses; *c ; )
22388 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
22389 && OMP_CLAUSE_DECL (*c) == real_decl)
22391 error_at (loc, "iteration variable %qD"
22392 " should not be firstprivate", real_decl);
22393 *c = OMP_CLAUSE_CHAIN (*c);
22395 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
22396 && OMP_CLAUSE_DECL (*c) == real_decl)
22398 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
22399 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
22400 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
22401 OMP_CLAUSE_DECL (l) = real_decl;
22402 OMP_CLAUSE_CHAIN (l) = clauses;
22403 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
22404 clauses = l;
22405 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
22406 CP_OMP_CLAUSE_INFO (*c) = NULL;
22407 add_private_clause = false;
22409 else
22411 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
22412 && OMP_CLAUSE_DECL (*c) == real_decl)
22413 add_private_clause = false;
22414 c = &OMP_CLAUSE_CHAIN (*c);
22418 if (add_private_clause)
22420 tree c;
22421 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
22423 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
22424 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
22425 && OMP_CLAUSE_DECL (c) == decl)
22426 break;
22427 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
22428 && OMP_CLAUSE_DECL (c) == decl)
22429 error_at (loc, "iteration variable %qD "
22430 "should not be firstprivate",
22431 decl);
22432 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
22433 && OMP_CLAUSE_DECL (c) == decl)
22434 error_at (loc, "iteration variable %qD should not be reduction",
22435 decl);
22437 if (c == NULL)
22439 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
22440 OMP_CLAUSE_DECL (c) = decl;
22441 c = finish_omp_clauses (c);
22442 if (c)
22444 OMP_CLAUSE_CHAIN (c) = clauses;
22445 clauses = c;
22450 cond = NULL;
22451 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22452 cond = cp_parser_omp_for_cond (parser, decl);
22453 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
22455 incr = NULL;
22456 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
22458 /* If decl is an iterator, preserve the operator on decl
22459 until finish_omp_for. */
22460 if (decl
22461 && (type_dependent_expression_p (decl)
22462 || CLASS_TYPE_P (TREE_TYPE (decl))))
22463 incr = cp_parser_omp_for_incr (parser, decl);
22464 else
22465 incr = cp_parser_expression (parser, false, NULL);
22468 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
22469 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22470 /*or_comma=*/false,
22471 /*consume_paren=*/true);
22473 TREE_VEC_ELT (declv, i) = decl;
22474 TREE_VEC_ELT (initv, i) = init;
22475 TREE_VEC_ELT (condv, i) = cond;
22476 TREE_VEC_ELT (incrv, i) = incr;
22478 if (i == collapse - 1)
22479 break;
22481 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
22482 in between the collapsed for loops to be still considered perfectly
22483 nested. Hopefully the final version clarifies this.
22484 For now handle (multiple) {'s and empty statements. */
22485 cp_parser_parse_tentatively (parser);
22488 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22489 break;
22490 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22492 cp_lexer_consume_token (parser->lexer);
22493 bracecount++;
22495 else if (bracecount
22496 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22497 cp_lexer_consume_token (parser->lexer);
22498 else
22500 loc = cp_lexer_peek_token (parser->lexer)->location;
22501 error_at (loc, "not enough collapsed for loops");
22502 collapse_err = true;
22503 cp_parser_abort_tentative_parse (parser);
22504 declv = NULL_TREE;
22505 break;
22508 while (1);
22510 if (declv)
22512 cp_parser_parse_definitely (parser);
22513 nbraces += bracecount;
22517 /* Note that we saved the original contents of this flag when we entered
22518 the structured block, and so we don't need to re-save it here. */
22519 parser->in_statement = IN_OMP_FOR;
22521 /* Note that the grammar doesn't call for a structured block here,
22522 though the loop as a whole is a structured block. */
22523 body = push_stmt_list ();
22524 cp_parser_statement (parser, NULL_TREE, false, NULL);
22525 body = pop_stmt_list (body);
22527 if (declv == NULL_TREE)
22528 ret = NULL_TREE;
22529 else
22530 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
22531 pre_body, clauses);
22533 while (nbraces)
22535 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
22537 cp_lexer_consume_token (parser->lexer);
22538 nbraces--;
22540 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22541 cp_lexer_consume_token (parser->lexer);
22542 else
22544 if (!collapse_err)
22546 error_at (cp_lexer_peek_token (parser->lexer)->location,
22547 "collapsed loops not perfectly nested");
22549 collapse_err = true;
22550 cp_parser_statement_seq_opt (parser, NULL);
22551 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
22552 break;
22556 while (for_block)
22558 add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
22559 for_block = TREE_CHAIN (for_block);
22562 return ret;
22565 /* OpenMP 2.5:
22566 #pragma omp for for-clause[optseq] new-line
22567 for-loop */
22569 #define OMP_FOR_CLAUSE_MASK \
22570 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22571 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22572 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
22573 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
22574 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
22575 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
22576 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
22577 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
22579 static tree
22580 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
22582 tree clauses, sb, ret;
22583 unsigned int save;
22585 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
22586 "#pragma omp for", pragma_tok);
22588 sb = begin_omp_structured_block ();
22589 save = cp_parser_begin_omp_structured_block (parser);
22591 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
22593 cp_parser_end_omp_structured_block (parser, save);
22594 add_stmt (finish_omp_structured_block (sb));
22596 return ret;
22599 /* OpenMP 2.5:
22600 # pragma omp master new-line
22601 structured-block */
22603 static tree
22604 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
22606 cp_parser_require_pragma_eol (parser, pragma_tok);
22607 return c_finish_omp_master (input_location,
22608 cp_parser_omp_structured_block (parser));
22611 /* OpenMP 2.5:
22612 # pragma omp ordered new-line
22613 structured-block */
22615 static tree
22616 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
22618 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22619 cp_parser_require_pragma_eol (parser, pragma_tok);
22620 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
22623 /* OpenMP 2.5:
22625 section-scope:
22626 { section-sequence }
22628 section-sequence:
22629 section-directive[opt] structured-block
22630 section-sequence section-directive structured-block */
22632 static tree
22633 cp_parser_omp_sections_scope (cp_parser *parser)
22635 tree stmt, substmt;
22636 bool error_suppress = false;
22637 cp_token *tok;
22639 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
22640 return NULL_TREE;
22642 stmt = push_stmt_list ();
22644 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
22646 unsigned save;
22648 substmt = begin_omp_structured_block ();
22649 save = cp_parser_begin_omp_structured_block (parser);
22651 while (1)
22653 cp_parser_statement (parser, NULL_TREE, false, NULL);
22655 tok = cp_lexer_peek_token (parser->lexer);
22656 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
22657 break;
22658 if (tok->type == CPP_CLOSE_BRACE)
22659 break;
22660 if (tok->type == CPP_EOF)
22661 break;
22664 cp_parser_end_omp_structured_block (parser, save);
22665 substmt = finish_omp_structured_block (substmt);
22666 substmt = build1 (OMP_SECTION, void_type_node, substmt);
22667 add_stmt (substmt);
22670 while (1)
22672 tok = cp_lexer_peek_token (parser->lexer);
22673 if (tok->type == CPP_CLOSE_BRACE)
22674 break;
22675 if (tok->type == CPP_EOF)
22676 break;
22678 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
22680 cp_lexer_consume_token (parser->lexer);
22681 cp_parser_require_pragma_eol (parser, tok);
22682 error_suppress = false;
22684 else if (!error_suppress)
22686 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
22687 error_suppress = true;
22690 substmt = cp_parser_omp_structured_block (parser);
22691 substmt = build1 (OMP_SECTION, void_type_node, substmt);
22692 add_stmt (substmt);
22694 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
22696 substmt = pop_stmt_list (stmt);
22698 stmt = make_node (OMP_SECTIONS);
22699 TREE_TYPE (stmt) = void_type_node;
22700 OMP_SECTIONS_BODY (stmt) = substmt;
22702 add_stmt (stmt);
22703 return stmt;
22706 /* OpenMP 2.5:
22707 # pragma omp sections sections-clause[optseq] newline
22708 sections-scope */
22710 #define OMP_SECTIONS_CLAUSE_MASK \
22711 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22712 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22713 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
22714 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
22715 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
22717 static tree
22718 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
22720 tree clauses, ret;
22722 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
22723 "#pragma omp sections", pragma_tok);
22725 ret = cp_parser_omp_sections_scope (parser);
22726 if (ret)
22727 OMP_SECTIONS_CLAUSES (ret) = clauses;
22729 return ret;
22732 /* OpenMP 2.5:
22733 # pragma parallel parallel-clause new-line
22734 # pragma parallel for parallel-for-clause new-line
22735 # pragma parallel sections parallel-sections-clause new-line */
22737 #define OMP_PARALLEL_CLAUSE_MASK \
22738 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
22739 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22740 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22741 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
22742 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
22743 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
22744 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
22745 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
22747 static tree
22748 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
22750 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
22751 const char *p_name = "#pragma omp parallel";
22752 tree stmt, clauses, par_clause, ws_clause, block;
22753 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
22754 unsigned int save;
22755 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22757 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22759 cp_lexer_consume_token (parser->lexer);
22760 p_kind = PRAGMA_OMP_PARALLEL_FOR;
22761 p_name = "#pragma omp parallel for";
22762 mask |= OMP_FOR_CLAUSE_MASK;
22763 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
22765 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22767 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
22768 const char *p = IDENTIFIER_POINTER (id);
22769 if (strcmp (p, "sections") == 0)
22771 cp_lexer_consume_token (parser->lexer);
22772 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
22773 p_name = "#pragma omp parallel sections";
22774 mask |= OMP_SECTIONS_CLAUSE_MASK;
22775 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
22779 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
22780 block = begin_omp_parallel ();
22781 save = cp_parser_begin_omp_structured_block (parser);
22783 switch (p_kind)
22785 case PRAGMA_OMP_PARALLEL:
22786 cp_parser_statement (parser, NULL_TREE, false, NULL);
22787 par_clause = clauses;
22788 break;
22790 case PRAGMA_OMP_PARALLEL_FOR:
22791 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
22792 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
22793 break;
22795 case PRAGMA_OMP_PARALLEL_SECTIONS:
22796 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
22797 stmt = cp_parser_omp_sections_scope (parser);
22798 if (stmt)
22799 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
22800 break;
22802 default:
22803 gcc_unreachable ();
22806 cp_parser_end_omp_structured_block (parser, save);
22807 stmt = finish_omp_parallel (par_clause, block);
22808 if (p_kind != PRAGMA_OMP_PARALLEL)
22809 OMP_PARALLEL_COMBINED (stmt) = 1;
22810 return stmt;
22813 /* OpenMP 2.5:
22814 # pragma omp single single-clause[optseq] new-line
22815 structured-block */
22817 #define OMP_SINGLE_CLAUSE_MASK \
22818 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22819 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22820 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
22821 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
22823 static tree
22824 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
22826 tree stmt = make_node (OMP_SINGLE);
22827 TREE_TYPE (stmt) = void_type_node;
22829 OMP_SINGLE_CLAUSES (stmt)
22830 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
22831 "#pragma omp single", pragma_tok);
22832 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
22834 return add_stmt (stmt);
22837 /* OpenMP 3.0:
22838 # pragma omp task task-clause[optseq] new-line
22839 structured-block */
22841 #define OMP_TASK_CLAUSE_MASK \
22842 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
22843 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
22844 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
22845 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22846 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22847 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
22849 static tree
22850 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
22852 tree clauses, block;
22853 unsigned int save;
22855 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
22856 "#pragma omp task", pragma_tok);
22857 block = begin_omp_task ();
22858 save = cp_parser_begin_omp_structured_block (parser);
22859 cp_parser_statement (parser, NULL_TREE, false, NULL);
22860 cp_parser_end_omp_structured_block (parser, save);
22861 return finish_omp_task (clauses, block);
22864 /* OpenMP 3.0:
22865 # pragma omp taskwait new-line */
22867 static void
22868 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
22870 cp_parser_require_pragma_eol (parser, pragma_tok);
22871 finish_omp_taskwait ();
22874 /* OpenMP 2.5:
22875 # pragma omp threadprivate (variable-list) */
22877 static void
22878 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
22880 tree vars;
22882 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
22883 cp_parser_require_pragma_eol (parser, pragma_tok);
22885 finish_omp_threadprivate (vars);
22888 /* Main entry point to OpenMP statement pragmas. */
22890 static void
22891 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
22893 tree stmt;
22895 switch (pragma_tok->pragma_kind)
22897 case PRAGMA_OMP_ATOMIC:
22898 cp_parser_omp_atomic (parser, pragma_tok);
22899 return;
22900 case PRAGMA_OMP_CRITICAL:
22901 stmt = cp_parser_omp_critical (parser, pragma_tok);
22902 break;
22903 case PRAGMA_OMP_FOR:
22904 stmt = cp_parser_omp_for (parser, pragma_tok);
22905 break;
22906 case PRAGMA_OMP_MASTER:
22907 stmt = cp_parser_omp_master (parser, pragma_tok);
22908 break;
22909 case PRAGMA_OMP_ORDERED:
22910 stmt = cp_parser_omp_ordered (parser, pragma_tok);
22911 break;
22912 case PRAGMA_OMP_PARALLEL:
22913 stmt = cp_parser_omp_parallel (parser, pragma_tok);
22914 break;
22915 case PRAGMA_OMP_SECTIONS:
22916 stmt = cp_parser_omp_sections (parser, pragma_tok);
22917 break;
22918 case PRAGMA_OMP_SINGLE:
22919 stmt = cp_parser_omp_single (parser, pragma_tok);
22920 break;
22921 case PRAGMA_OMP_TASK:
22922 stmt = cp_parser_omp_task (parser, pragma_tok);
22923 break;
22924 default:
22925 gcc_unreachable ();
22928 if (stmt)
22929 SET_EXPR_LOCATION (stmt, pragma_tok->location);
22932 /* The parser. */
22934 static GTY (()) cp_parser *the_parser;
22937 /* Special handling for the first token or line in the file. The first
22938 thing in the file might be #pragma GCC pch_preprocess, which loads a
22939 PCH file, which is a GC collection point. So we need to handle this
22940 first pragma without benefit of an existing lexer structure.
22942 Always returns one token to the caller in *FIRST_TOKEN. This is
22943 either the true first token of the file, or the first token after
22944 the initial pragma. */
22946 static void
22947 cp_parser_initial_pragma (cp_token *first_token)
22949 tree name = NULL;
22951 cp_lexer_get_preprocessor_token (NULL, first_token);
22952 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
22953 return;
22955 cp_lexer_get_preprocessor_token (NULL, first_token);
22956 if (first_token->type == CPP_STRING)
22958 name = first_token->u.value;
22960 cp_lexer_get_preprocessor_token (NULL, first_token);
22961 if (first_token->type != CPP_PRAGMA_EOL)
22962 error_at (first_token->location,
22963 "junk at end of %<#pragma GCC pch_preprocess%>");
22965 else
22966 error_at (first_token->location, "expected string literal");
22968 /* Skip to the end of the pragma. */
22969 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
22970 cp_lexer_get_preprocessor_token (NULL, first_token);
22972 /* Now actually load the PCH file. */
22973 if (name)
22974 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
22976 /* Read one more token to return to our caller. We have to do this
22977 after reading the PCH file in, since its pointers have to be
22978 live. */
22979 cp_lexer_get_preprocessor_token (NULL, first_token);
22982 /* Normal parsing of a pragma token. Here we can (and must) use the
22983 regular lexer. */
22985 static bool
22986 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
22988 cp_token *pragma_tok;
22989 unsigned int id;
22991 pragma_tok = cp_lexer_consume_token (parser->lexer);
22992 gcc_assert (pragma_tok->type == CPP_PRAGMA);
22993 parser->lexer->in_pragma = true;
22995 id = pragma_tok->pragma_kind;
22996 switch (id)
22998 case PRAGMA_GCC_PCH_PREPROCESS:
22999 error_at (pragma_tok->location,
23000 "%<#pragma GCC pch_preprocess%> must be first");
23001 break;
23003 case PRAGMA_OMP_BARRIER:
23004 switch (context)
23006 case pragma_compound:
23007 cp_parser_omp_barrier (parser, pragma_tok);
23008 return false;
23009 case pragma_stmt:
23010 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
23011 "used in compound statements");
23012 break;
23013 default:
23014 goto bad_stmt;
23016 break;
23018 case PRAGMA_OMP_FLUSH:
23019 switch (context)
23021 case pragma_compound:
23022 cp_parser_omp_flush (parser, pragma_tok);
23023 return false;
23024 case pragma_stmt:
23025 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
23026 "used in compound statements");
23027 break;
23028 default:
23029 goto bad_stmt;
23031 break;
23033 case PRAGMA_OMP_TASKWAIT:
23034 switch (context)
23036 case pragma_compound:
23037 cp_parser_omp_taskwait (parser, pragma_tok);
23038 return false;
23039 case pragma_stmt:
23040 error_at (pragma_tok->location,
23041 "%<#pragma omp taskwait%> may only be "
23042 "used in compound statements");
23043 break;
23044 default:
23045 goto bad_stmt;
23047 break;
23049 case PRAGMA_OMP_THREADPRIVATE:
23050 cp_parser_omp_threadprivate (parser, pragma_tok);
23051 return false;
23053 case PRAGMA_OMP_ATOMIC:
23054 case PRAGMA_OMP_CRITICAL:
23055 case PRAGMA_OMP_FOR:
23056 case PRAGMA_OMP_MASTER:
23057 case PRAGMA_OMP_ORDERED:
23058 case PRAGMA_OMP_PARALLEL:
23059 case PRAGMA_OMP_SECTIONS:
23060 case PRAGMA_OMP_SINGLE:
23061 case PRAGMA_OMP_TASK:
23062 if (context == pragma_external)
23063 goto bad_stmt;
23064 cp_parser_omp_construct (parser, pragma_tok);
23065 return true;
23067 case PRAGMA_OMP_SECTION:
23068 error_at (pragma_tok->location,
23069 "%<#pragma omp section%> may only be used in "
23070 "%<#pragma omp sections%> construct");
23071 break;
23073 default:
23074 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
23075 c_invoke_pragma_handler (id);
23076 break;
23078 bad_stmt:
23079 cp_parser_error (parser, "expected declaration specifiers");
23080 break;
23083 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
23084 return false;
23087 /* The interface the pragma parsers have to the lexer. */
23089 enum cpp_ttype
23090 pragma_lex (tree *value)
23092 cp_token *tok;
23093 enum cpp_ttype ret;
23095 tok = cp_lexer_peek_token (the_parser->lexer);
23097 ret = tok->type;
23098 *value = tok->u.value;
23100 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
23101 ret = CPP_EOF;
23102 else if (ret == CPP_STRING)
23103 *value = cp_parser_string_literal (the_parser, false, false);
23104 else
23106 cp_lexer_consume_token (the_parser->lexer);
23107 if (ret == CPP_KEYWORD)
23108 ret = CPP_NAME;
23111 return ret;
23115 /* External interface. */
23117 /* Parse one entire translation unit. */
23119 void
23120 c_parse_file (void)
23122 static bool already_called = false;
23124 if (already_called)
23126 sorry ("inter-module optimizations not implemented for C++");
23127 return;
23129 already_called = true;
23131 the_parser = cp_parser_new ();
23132 push_deferring_access_checks (flag_access_control
23133 ? dk_no_deferred : dk_no_check);
23134 cp_parser_translation_unit (the_parser);
23135 the_parser = NULL;
23138 #include "gt-cp-parser.h"