Merged r157428 through r157652 into branch.
[official-gcc.git] / gcc / cp / parser.c
blobd27d12254ca0f68106b1f9b102384fe7a07b43d7
1 /* C++ Parser.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005, 2007, 2008, 2009 Free Software Foundation, Inc.
4 Written by Mark Mitchell <mark@codesourcery.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "dyn-string.h"
27 #include "varray.h"
28 #include "cpplib.h"
29 #include "tree.h"
30 #include "cp-tree.h"
31 #include "intl.h"
32 #include "c-pragma.h"
33 #include "decl.h"
34 #include "flags.h"
35 #include "diagnostic.h"
36 #include "toplev.h"
37 #include "output.h"
38 #include "target.h"
39 #include "cgraph.h"
40 #include "c-common.h"
41 #include "plugin.h"
44 /* The lexer. */
46 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
47 and c-lex.c) and the C++ parser. */
49 /* A token's value and its associated deferred access checks and
50 qualifying scope. */
52 struct GTY(()) tree_check {
53 /* The value associated with the token. */
54 tree value;
55 /* The checks that have been associated with value. */
56 VEC (deferred_access_check, gc)* checks;
57 /* The token's qualifying scope (used when it is a
58 CPP_NESTED_NAME_SPECIFIER). */
59 tree qualifying_scope;
62 /* A C++ token. */
64 typedef struct GTY (()) cp_token {
65 /* The kind of token. */
66 ENUM_BITFIELD (cpp_ttype) type : 8;
67 /* If this token is a keyword, this value indicates which keyword.
68 Otherwise, this value is RID_MAX. */
69 ENUM_BITFIELD (rid) keyword : 8;
70 /* Token flags. */
71 unsigned char flags;
72 /* Identifier for the pragma. */
73 ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
74 /* True if this token is from a context where it is implicitly extern "C" */
75 BOOL_BITFIELD implicit_extern_c : 1;
76 /* True for a CPP_NAME token that is not a keyword (i.e., for which
77 KEYWORD is RID_MAX) iff this name was looked up and found to be
78 ambiguous. An error has already been reported. */
79 BOOL_BITFIELD ambiguous_p : 1;
80 /* The location at which this token was found. */
81 location_t location;
82 /* The value associated with this token, if any. */
83 union cp_token_value {
84 /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID. */
85 struct tree_check* GTY((tag ("1"))) tree_check_value;
86 /* Use for all other tokens. */
87 tree GTY((tag ("0"))) value;
88 } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
89 } cp_token;
91 /* We use a stack of token pointer for saving token sets. */
92 typedef struct cp_token *cp_token_position;
93 DEF_VEC_P (cp_token_position);
94 DEF_VEC_ALLOC_P (cp_token_position,heap);
96 static cp_token eof_token =
98 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, 0, 0, { NULL }
101 /* The cp_lexer structure represents the C++ lexer. It is responsible
102 for managing the token stream from the preprocessor and supplying
103 it to the parser. Tokens are never added to the cp_lexer after
104 it is created. */
106 typedef struct GTY (()) cp_lexer {
107 /* The memory allocated for the buffer. NULL if this lexer does not
108 own the token buffer. */
109 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
110 /* If the lexer owns the buffer, this is the number of tokens in the
111 buffer. */
112 size_t buffer_length;
114 /* A pointer just past the last available token. The tokens
115 in this lexer are [buffer, last_token). */
116 cp_token_position GTY ((skip)) last_token;
118 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
119 no more available tokens. */
120 cp_token_position GTY ((skip)) next_token;
122 /* A stack indicating positions at which cp_lexer_save_tokens was
123 called. The top entry is the most recent position at which we
124 began saving tokens. If the stack is non-empty, we are saving
125 tokens. */
126 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
128 /* The next lexer in a linked list of lexers. */
129 struct cp_lexer *next;
131 /* True if we should output debugging information. */
132 bool debugging_p;
134 /* True if we're in the context of parsing a pragma, and should not
135 increment past the end-of-line marker. */
136 bool in_pragma;
137 } cp_lexer;
139 /* cp_token_cache is a range of tokens. There is no need to represent
140 allocate heap memory for it, since tokens are never removed from the
141 lexer's array. There is also no need for the GC to walk through
142 a cp_token_cache, since everything in here is referenced through
143 a lexer. */
145 typedef struct GTY(()) cp_token_cache {
146 /* The beginning of the token range. */
147 cp_token * GTY((skip)) first;
149 /* Points immediately after the last token in the range. */
150 cp_token * GTY ((skip)) last;
151 } cp_token_cache;
153 /* Prototypes. */
155 static cp_lexer *cp_lexer_new_main
156 (void);
157 static cp_lexer *cp_lexer_new_from_tokens
158 (cp_token_cache *tokens);
159 static void cp_lexer_destroy
160 (cp_lexer *);
161 static int cp_lexer_saving_tokens
162 (const cp_lexer *);
163 static cp_token_position cp_lexer_token_position
164 (cp_lexer *, bool);
165 static cp_token *cp_lexer_token_at
166 (cp_lexer *, cp_token_position);
167 static void cp_lexer_get_preprocessor_token
168 (cp_lexer *, cp_token *);
169 static inline cp_token *cp_lexer_peek_token
170 (cp_lexer *);
171 static cp_token *cp_lexer_peek_nth_token
172 (cp_lexer *, size_t);
173 static inline bool cp_lexer_next_token_is
174 (cp_lexer *, enum cpp_ttype);
175 static bool cp_lexer_next_token_is_not
176 (cp_lexer *, enum cpp_ttype);
177 static bool cp_lexer_next_token_is_keyword
178 (cp_lexer *, enum rid);
179 static cp_token *cp_lexer_consume_token
180 (cp_lexer *);
181 static void cp_lexer_purge_token
182 (cp_lexer *);
183 static void cp_lexer_purge_tokens_after
184 (cp_lexer *, cp_token_position);
185 static void cp_lexer_save_tokens
186 (cp_lexer *);
187 static void cp_lexer_commit_tokens
188 (cp_lexer *);
189 static void cp_lexer_rollback_tokens
190 (cp_lexer *);
191 #ifdef ENABLE_CHECKING
192 static void cp_lexer_print_token
193 (FILE *, cp_token *);
194 static inline bool cp_lexer_debugging_p
195 (cp_lexer *);
196 static void cp_lexer_start_debugging
197 (cp_lexer *) ATTRIBUTE_UNUSED;
198 static void cp_lexer_stop_debugging
199 (cp_lexer *) ATTRIBUTE_UNUSED;
200 #else
201 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
202 about passing NULL to functions that require non-NULL arguments
203 (fputs, fprintf). It will never be used, so all we need is a value
204 of the right type that's guaranteed not to be NULL. */
205 #define cp_lexer_debug_stream stdout
206 #define cp_lexer_print_token(str, tok) (void) 0
207 #define cp_lexer_debugging_p(lexer) 0
208 #endif /* ENABLE_CHECKING */
210 static cp_token_cache *cp_token_cache_new
211 (cp_token *, cp_token *);
213 static void cp_parser_initial_pragma
214 (cp_token *);
216 /* Manifest constants. */
217 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
218 #define CP_SAVED_TOKEN_STACK 5
220 /* A token type for keywords, as opposed to ordinary identifiers. */
221 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
223 /* A token type for template-ids. If a template-id is processed while
224 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
225 the value of the CPP_TEMPLATE_ID is whatever was returned by
226 cp_parser_template_id. */
227 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
229 /* A token type for nested-name-specifiers. If a
230 nested-name-specifier is processed while parsing tentatively, it is
231 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
232 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
233 cp_parser_nested_name_specifier_opt. */
234 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
236 /* A token type for tokens that are not tokens at all; these are used
237 to represent slots in the array where there used to be a token
238 that has now been deleted. */
239 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
241 /* The number of token types, including C++-specific ones. */
242 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
244 /* Variables. */
246 #ifdef ENABLE_CHECKING
247 /* The stream to which debugging output should be written. */
248 static FILE *cp_lexer_debug_stream;
249 #endif /* ENABLE_CHECKING */
251 /* Nonzero if we are parsing an unevaluated operand: an operand to
252 sizeof, typeof, or alignof. */
253 int cp_unevaluated_operand;
255 /* Create a new main C++ lexer, the lexer that gets tokens from the
256 preprocessor. */
258 static cp_lexer *
259 cp_lexer_new_main (void)
261 cp_token first_token;
262 cp_lexer *lexer;
263 cp_token *pos;
264 size_t alloc;
265 size_t space;
266 cp_token *buffer;
268 /* It's possible that parsing the first pragma will load a PCH file,
269 which is a GC collection point. So we have to do that before
270 allocating any memory. */
271 cp_parser_initial_pragma (&first_token);
273 c_common_no_more_pch ();
275 /* Allocate the memory. */
276 lexer = GGC_CNEW (cp_lexer);
278 #ifdef ENABLE_CHECKING
279 /* Initially we are not debugging. */
280 lexer->debugging_p = false;
281 #endif /* ENABLE_CHECKING */
282 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
283 CP_SAVED_TOKEN_STACK);
285 /* Create the buffer. */
286 alloc = CP_LEXER_BUFFER_SIZE;
287 buffer = GGC_NEWVEC (cp_token, alloc);
289 /* Put the first token in the buffer. */
290 space = alloc;
291 pos = buffer;
292 *pos = first_token;
294 /* Get the remaining tokens from the preprocessor. */
295 while (pos->type != CPP_EOF)
297 pos++;
298 if (!--space)
300 space = alloc;
301 alloc *= 2;
302 buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
303 pos = buffer + space;
305 cp_lexer_get_preprocessor_token (lexer, pos);
307 lexer->buffer = buffer;
308 lexer->buffer_length = alloc - space;
309 lexer->last_token = pos;
310 lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
312 /* Subsequent preprocessor diagnostics should use compiler
313 diagnostic functions to get the compiler source location. */
314 done_lexing = true;
316 gcc_assert (lexer->next_token->type != CPP_PURGED);
317 return lexer;
320 /* Create a new lexer whose token stream is primed with the tokens in
321 CACHE. When these tokens are exhausted, no new tokens will be read. */
323 static cp_lexer *
324 cp_lexer_new_from_tokens (cp_token_cache *cache)
326 cp_token *first = cache->first;
327 cp_token *last = cache->last;
328 cp_lexer *lexer = GGC_CNEW (cp_lexer);
330 /* We do not own the buffer. */
331 lexer->buffer = NULL;
332 lexer->buffer_length = 0;
333 lexer->next_token = first == last ? &eof_token : first;
334 lexer->last_token = last;
336 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
337 CP_SAVED_TOKEN_STACK);
339 #ifdef ENABLE_CHECKING
340 /* Initially we are not debugging. */
341 lexer->debugging_p = false;
342 #endif
344 gcc_assert (lexer->next_token->type != CPP_PURGED);
345 return lexer;
348 /* Frees all resources associated with LEXER. */
350 static void
351 cp_lexer_destroy (cp_lexer *lexer)
353 if (lexer->buffer)
354 ggc_free (lexer->buffer);
355 VEC_free (cp_token_position, heap, lexer->saved_tokens);
356 ggc_free (lexer);
359 /* Returns nonzero if debugging information should be output. */
361 #ifdef ENABLE_CHECKING
363 static inline bool
364 cp_lexer_debugging_p (cp_lexer *lexer)
366 return lexer->debugging_p;
369 #endif /* ENABLE_CHECKING */
371 static inline cp_token_position
372 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
374 gcc_assert (!previous_p || lexer->next_token != &eof_token);
376 return lexer->next_token - previous_p;
379 static inline cp_token *
380 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
382 return pos;
385 /* nonzero if we are presently saving tokens. */
387 static inline int
388 cp_lexer_saving_tokens (const cp_lexer* lexer)
390 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
393 /* Store the next token from the preprocessor in *TOKEN. Return true
394 if we reach EOF. If LEXER is NULL, assume we are handling an
395 initial #pragma pch_preprocess, and thus want the lexer to return
396 processed strings. */
398 static void
399 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
401 static int is_extern_c = 0;
403 /* Get a new token from the preprocessor. */
404 token->type
405 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
406 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
407 token->keyword = RID_MAX;
408 token->pragma_kind = PRAGMA_NONE;
410 /* On some systems, some header files are surrounded by an
411 implicit extern "C" block. Set a flag in the token if it
412 comes from such a header. */
413 is_extern_c += pending_lang_change;
414 pending_lang_change = 0;
415 token->implicit_extern_c = is_extern_c > 0;
417 /* Check to see if this token is a keyword. */
418 if (token->type == CPP_NAME)
420 if (C_IS_RESERVED_WORD (token->u.value))
422 /* Mark this token as a keyword. */
423 token->type = CPP_KEYWORD;
424 /* Record which keyword. */
425 token->keyword = C_RID_CODE (token->u.value);
427 else
429 if (warn_cxx0x_compat
430 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
431 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
433 /* Warn about the C++0x keyword (but still treat it as
434 an identifier). */
435 warning (OPT_Wc__0x_compat,
436 "identifier %qE will become a keyword in C++0x",
437 token->u.value);
439 /* Clear out the C_RID_CODE so we don't warn about this
440 particular identifier-turned-keyword again. */
441 C_SET_RID_CODE (token->u.value, RID_MAX);
444 token->ambiguous_p = false;
445 token->keyword = RID_MAX;
448 /* Handle Objective-C++ keywords. */
449 else if (token->type == CPP_AT_NAME)
451 token->type = CPP_KEYWORD;
452 switch (C_RID_CODE (token->u.value))
454 /* Map 'class' to '@class', 'private' to '@private', etc. */
455 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
456 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
457 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
458 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
459 case RID_THROW: token->keyword = RID_AT_THROW; break;
460 case RID_TRY: token->keyword = RID_AT_TRY; break;
461 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
462 default: token->keyword = C_RID_CODE (token->u.value);
465 else if (token->type == CPP_PRAGMA)
467 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
468 token->pragma_kind = ((enum pragma_kind)
469 TREE_INT_CST_LOW (token->u.value));
470 token->u.value = NULL_TREE;
474 /* Update the globals input_location and the input file stack from TOKEN. */
475 static inline void
476 cp_lexer_set_source_position_from_token (cp_token *token)
478 if (token->type != CPP_EOF)
480 input_location = token->location;
484 /* Return a pointer to the next token in the token stream, but do not
485 consume it. */
487 static inline cp_token *
488 cp_lexer_peek_token (cp_lexer *lexer)
490 if (cp_lexer_debugging_p (lexer))
492 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
493 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
494 putc ('\n', cp_lexer_debug_stream);
496 return lexer->next_token;
499 /* Return true if the next token has the indicated TYPE. */
501 static inline bool
502 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
504 return cp_lexer_peek_token (lexer)->type == type;
507 /* Return true if the next token does not have the indicated TYPE. */
509 static inline bool
510 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
512 return !cp_lexer_next_token_is (lexer, type);
515 /* Return true if the next token is the indicated KEYWORD. */
517 static inline bool
518 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
520 return cp_lexer_peek_token (lexer)->keyword == keyword;
523 /* Return true if the next token is not the indicated KEYWORD. */
525 static inline bool
526 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
528 return cp_lexer_peek_token (lexer)->keyword != keyword;
531 /* Return true if the next token is a keyword for a decl-specifier. */
533 static bool
534 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
536 cp_token *token;
538 token = cp_lexer_peek_token (lexer);
539 switch (token->keyword)
541 /* auto specifier: storage-class-specifier in C++,
542 simple-type-specifier in C++0x. */
543 case RID_AUTO:
544 /* Storage classes. */
545 case RID_REGISTER:
546 case RID_STATIC:
547 case RID_EXTERN:
548 case RID_MUTABLE:
549 case RID_THREAD:
550 /* Elaborated type specifiers. */
551 case RID_ENUM:
552 case RID_CLASS:
553 case RID_STRUCT:
554 case RID_UNION:
555 case RID_TYPENAME:
556 /* Simple type specifiers. */
557 case RID_CHAR:
558 case RID_CHAR16:
559 case RID_CHAR32:
560 case RID_WCHAR:
561 case RID_BOOL:
562 case RID_SHORT:
563 case RID_INT:
564 case RID_LONG:
565 case RID_SIGNED:
566 case RID_UNSIGNED:
567 case RID_FLOAT:
568 case RID_DOUBLE:
569 case RID_VOID:
570 /* GNU extensions. */
571 case RID_ATTRIBUTE:
572 case RID_TYPEOF:
573 /* C++0x extensions. */
574 case RID_DECLTYPE:
575 return true;
577 default:
578 return false;
582 /* Return a pointer to the Nth token in the token stream. If N is 1,
583 then this is precisely equivalent to cp_lexer_peek_token (except
584 that it is not inline). One would like to disallow that case, but
585 there is one case (cp_parser_nth_token_starts_template_id) where
586 the caller passes a variable for N and it might be 1. */
588 static cp_token *
589 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
591 cp_token *token;
593 /* N is 1-based, not zero-based. */
594 gcc_assert (n > 0);
596 if (cp_lexer_debugging_p (lexer))
597 fprintf (cp_lexer_debug_stream,
598 "cp_lexer: peeking ahead %ld at token: ", (long)n);
600 --n;
601 token = lexer->next_token;
602 gcc_assert (!n || token != &eof_token);
603 while (n != 0)
605 ++token;
606 if (token == lexer->last_token)
608 token = &eof_token;
609 break;
612 if (token->type != CPP_PURGED)
613 --n;
616 if (cp_lexer_debugging_p (lexer))
618 cp_lexer_print_token (cp_lexer_debug_stream, token);
619 putc ('\n', cp_lexer_debug_stream);
622 return token;
625 /* Return the next token, and advance the lexer's next_token pointer
626 to point to the next non-purged token. */
628 static cp_token *
629 cp_lexer_consume_token (cp_lexer* lexer)
631 cp_token *token = lexer->next_token;
633 gcc_assert (token != &eof_token);
634 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
638 lexer->next_token++;
639 if (lexer->next_token == lexer->last_token)
641 lexer->next_token = &eof_token;
642 break;
646 while (lexer->next_token->type == CPP_PURGED);
648 cp_lexer_set_source_position_from_token (token);
650 /* Provide debugging output. */
651 if (cp_lexer_debugging_p (lexer))
653 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
654 cp_lexer_print_token (cp_lexer_debug_stream, token);
655 putc ('\n', cp_lexer_debug_stream);
658 return token;
661 /* Permanently remove the next token from the token stream, and
662 advance the next_token pointer to refer to the next non-purged
663 token. */
665 static void
666 cp_lexer_purge_token (cp_lexer *lexer)
668 cp_token *tok = lexer->next_token;
670 gcc_assert (tok != &eof_token);
671 tok->type = CPP_PURGED;
672 tok->location = UNKNOWN_LOCATION;
673 tok->u.value = NULL_TREE;
674 tok->keyword = RID_MAX;
678 tok++;
679 if (tok == lexer->last_token)
681 tok = &eof_token;
682 break;
685 while (tok->type == CPP_PURGED);
686 lexer->next_token = tok;
689 /* Permanently remove all tokens after TOK, up to, but not
690 including, the token that will be returned next by
691 cp_lexer_peek_token. */
693 static void
694 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
696 cp_token *peek = lexer->next_token;
698 if (peek == &eof_token)
699 peek = lexer->last_token;
701 gcc_assert (tok < peek);
703 for ( tok += 1; tok != peek; tok += 1)
705 tok->type = CPP_PURGED;
706 tok->location = UNKNOWN_LOCATION;
707 tok->u.value = NULL_TREE;
708 tok->keyword = RID_MAX;
712 /* Begin saving tokens. All tokens consumed after this point will be
713 preserved. */
715 static void
716 cp_lexer_save_tokens (cp_lexer* lexer)
718 /* Provide debugging output. */
719 if (cp_lexer_debugging_p (lexer))
720 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
722 VEC_safe_push (cp_token_position, heap,
723 lexer->saved_tokens, lexer->next_token);
726 /* Commit to the portion of the token stream most recently saved. */
728 static void
729 cp_lexer_commit_tokens (cp_lexer* lexer)
731 /* Provide debugging output. */
732 if (cp_lexer_debugging_p (lexer))
733 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
735 VEC_pop (cp_token_position, lexer->saved_tokens);
738 /* Return all tokens saved since the last call to cp_lexer_save_tokens
739 to the token stream. Stop saving tokens. */
741 static void
742 cp_lexer_rollback_tokens (cp_lexer* lexer)
744 /* Provide debugging output. */
745 if (cp_lexer_debugging_p (lexer))
746 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
748 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
751 /* Print a representation of the TOKEN on the STREAM. */
753 #ifdef ENABLE_CHECKING
755 static void
756 cp_lexer_print_token (FILE * stream, cp_token *token)
758 /* We don't use cpp_type2name here because the parser defines
759 a few tokens of its own. */
760 static const char *const token_names[] = {
761 /* cpplib-defined token types */
762 #define OP(e, s) #e,
763 #define TK(e, s) #e,
764 TTYPE_TABLE
765 #undef OP
766 #undef TK
767 /* C++ parser token types - see "Manifest constants", above. */
768 "KEYWORD",
769 "TEMPLATE_ID",
770 "NESTED_NAME_SPECIFIER",
771 "PURGED"
774 /* If we have a name for the token, print it out. Otherwise, we
775 simply give the numeric code. */
776 gcc_assert (token->type < ARRAY_SIZE(token_names));
777 fputs (token_names[token->type], stream);
779 /* For some tokens, print the associated data. */
780 switch (token->type)
782 case CPP_KEYWORD:
783 /* Some keywords have a value that is not an IDENTIFIER_NODE.
784 For example, `struct' is mapped to an INTEGER_CST. */
785 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
786 break;
787 /* else fall through */
788 case CPP_NAME:
789 fputs (IDENTIFIER_POINTER (token->u.value), stream);
790 break;
792 case CPP_STRING:
793 case CPP_STRING16:
794 case CPP_STRING32:
795 case CPP_WSTRING:
796 case CPP_UTF8STRING:
797 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
798 break;
800 default:
801 break;
805 /* Start emitting debugging information. */
807 static void
808 cp_lexer_start_debugging (cp_lexer* lexer)
810 lexer->debugging_p = true;
813 /* Stop emitting debugging information. */
815 static void
816 cp_lexer_stop_debugging (cp_lexer* lexer)
818 lexer->debugging_p = false;
821 #endif /* ENABLE_CHECKING */
823 /* Create a new cp_token_cache, representing a range of tokens. */
825 static cp_token_cache *
826 cp_token_cache_new (cp_token *first, cp_token *last)
828 cp_token_cache *cache = GGC_NEW (cp_token_cache);
829 cache->first = first;
830 cache->last = last;
831 return cache;
835 /* Decl-specifiers. */
837 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
839 static void
840 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
842 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
845 /* Declarators. */
847 /* Nothing other than the parser should be creating declarators;
848 declarators are a semi-syntactic representation of C++ entities.
849 Other parts of the front end that need to create entities (like
850 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
852 static cp_declarator *make_call_declarator
853 (cp_declarator *, tree, cp_cv_quals, tree, tree);
854 static cp_declarator *make_array_declarator
855 (cp_declarator *, tree);
856 static cp_declarator *make_pointer_declarator
857 (cp_cv_quals, cp_declarator *);
858 static cp_declarator *make_reference_declarator
859 (cp_cv_quals, cp_declarator *, bool);
860 static cp_parameter_declarator *make_parameter_declarator
861 (cp_decl_specifier_seq *, cp_declarator *, tree);
862 static cp_declarator *make_ptrmem_declarator
863 (cp_cv_quals, tree, cp_declarator *);
865 /* An erroneous declarator. */
866 static cp_declarator *cp_error_declarator;
868 /* The obstack on which declarators and related data structures are
869 allocated. */
870 static struct obstack declarator_obstack;
872 /* Alloc BYTES from the declarator memory pool. */
874 static inline void *
875 alloc_declarator (size_t bytes)
877 return obstack_alloc (&declarator_obstack, bytes);
880 /* Allocate a declarator of the indicated KIND. Clear fields that are
881 common to all declarators. */
883 static cp_declarator *
884 make_declarator (cp_declarator_kind kind)
886 cp_declarator *declarator;
888 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
889 declarator->kind = kind;
890 declarator->attributes = NULL_TREE;
891 declarator->declarator = NULL;
892 declarator->parameter_pack_p = false;
894 return declarator;
897 /* Make a declarator for a generalized identifier. If
898 QUALIFYING_SCOPE is non-NULL, the identifier is
899 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
900 UNQUALIFIED_NAME. SFK indicates the kind of special function this
901 is, if any. */
903 static cp_declarator *
904 make_id_declarator (tree qualifying_scope, tree unqualified_name,
905 special_function_kind sfk)
907 cp_declarator *declarator;
909 /* It is valid to write:
911 class C { void f(); };
912 typedef C D;
913 void D::f();
915 The standard is not clear about whether `typedef const C D' is
916 legal; as of 2002-09-15 the committee is considering that
917 question. EDG 3.0 allows that syntax. Therefore, we do as
918 well. */
919 if (qualifying_scope && TYPE_P (qualifying_scope))
920 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
922 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
923 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
924 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
926 declarator = make_declarator (cdk_id);
927 declarator->u.id.qualifying_scope = qualifying_scope;
928 declarator->u.id.unqualified_name = unqualified_name;
929 declarator->u.id.sfk = sfk;
931 return declarator;
934 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
935 of modifiers such as const or volatile to apply to the pointer
936 type, represented as identifiers. */
938 cp_declarator *
939 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
941 cp_declarator *declarator;
943 declarator = make_declarator (cdk_pointer);
944 declarator->declarator = target;
945 declarator->u.pointer.qualifiers = cv_qualifiers;
946 declarator->u.pointer.class_type = NULL_TREE;
947 if (target)
949 declarator->parameter_pack_p = target->parameter_pack_p;
950 target->parameter_pack_p = false;
952 else
953 declarator->parameter_pack_p = false;
955 return declarator;
958 /* Like make_pointer_declarator -- but for references. */
960 cp_declarator *
961 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
962 bool rvalue_ref)
964 cp_declarator *declarator;
966 declarator = make_declarator (cdk_reference);
967 declarator->declarator = target;
968 declarator->u.reference.qualifiers = cv_qualifiers;
969 declarator->u.reference.rvalue_ref = rvalue_ref;
970 if (target)
972 declarator->parameter_pack_p = target->parameter_pack_p;
973 target->parameter_pack_p = false;
975 else
976 declarator->parameter_pack_p = false;
978 return declarator;
981 /* Like make_pointer_declarator -- but for a pointer to a non-static
982 member of CLASS_TYPE. */
984 cp_declarator *
985 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
986 cp_declarator *pointee)
988 cp_declarator *declarator;
990 declarator = make_declarator (cdk_ptrmem);
991 declarator->declarator = pointee;
992 declarator->u.pointer.qualifiers = cv_qualifiers;
993 declarator->u.pointer.class_type = class_type;
995 if (pointee)
997 declarator->parameter_pack_p = pointee->parameter_pack_p;
998 pointee->parameter_pack_p = false;
1000 else
1001 declarator->parameter_pack_p = false;
1003 return declarator;
1006 /* Make a declarator for the function given by TARGET, with the
1007 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1008 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1009 indicates what exceptions can be thrown. */
1011 cp_declarator *
1012 make_call_declarator (cp_declarator *target,
1013 tree parms,
1014 cp_cv_quals cv_qualifiers,
1015 tree exception_specification,
1016 tree late_return_type)
1018 cp_declarator *declarator;
1020 declarator = make_declarator (cdk_function);
1021 declarator->declarator = target;
1022 declarator->u.function.parameters = parms;
1023 declarator->u.function.qualifiers = cv_qualifiers;
1024 declarator->u.function.exception_specification = exception_specification;
1025 declarator->u.function.late_return_type = late_return_type;
1026 if (target)
1028 declarator->parameter_pack_p = target->parameter_pack_p;
1029 target->parameter_pack_p = false;
1031 else
1032 declarator->parameter_pack_p = false;
1034 return declarator;
1037 /* Make a declarator for an array of BOUNDS elements, each of which is
1038 defined by ELEMENT. */
1040 cp_declarator *
1041 make_array_declarator (cp_declarator *element, tree bounds)
1043 cp_declarator *declarator;
1045 declarator = make_declarator (cdk_array);
1046 declarator->declarator = element;
1047 declarator->u.array.bounds = bounds;
1048 if (element)
1050 declarator->parameter_pack_p = element->parameter_pack_p;
1051 element->parameter_pack_p = false;
1053 else
1054 declarator->parameter_pack_p = false;
1056 return declarator;
1059 /* Determine whether the declarator we've seen so far can be a
1060 parameter pack, when followed by an ellipsis. */
1061 static bool
1062 declarator_can_be_parameter_pack (cp_declarator *declarator)
1064 /* Search for a declarator name, or any other declarator that goes
1065 after the point where the ellipsis could appear in a parameter
1066 pack. If we find any of these, then this declarator can not be
1067 made into a parameter pack. */
1068 bool found = false;
1069 while (declarator && !found)
1071 switch ((int)declarator->kind)
1073 case cdk_id:
1074 case cdk_array:
1075 found = true;
1076 break;
1078 case cdk_error:
1079 return true;
1081 default:
1082 declarator = declarator->declarator;
1083 break;
1087 return !found;
1090 cp_parameter_declarator *no_parameters;
1092 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1093 DECLARATOR and DEFAULT_ARGUMENT. */
1095 cp_parameter_declarator *
1096 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1097 cp_declarator *declarator,
1098 tree default_argument)
1100 cp_parameter_declarator *parameter;
1102 parameter = ((cp_parameter_declarator *)
1103 alloc_declarator (sizeof (cp_parameter_declarator)));
1104 parameter->next = NULL;
1105 if (decl_specifiers)
1106 parameter->decl_specifiers = *decl_specifiers;
1107 else
1108 clear_decl_specs (&parameter->decl_specifiers);
1109 parameter->declarator = declarator;
1110 parameter->default_argument = default_argument;
1111 parameter->ellipsis_p = false;
1113 return parameter;
1116 /* Returns true iff DECLARATOR is a declaration for a function. */
1118 static bool
1119 function_declarator_p (const cp_declarator *declarator)
1121 while (declarator)
1123 if (declarator->kind == cdk_function
1124 && declarator->declarator->kind == cdk_id)
1125 return true;
1126 if (declarator->kind == cdk_id
1127 || declarator->kind == cdk_error)
1128 return false;
1129 declarator = declarator->declarator;
1131 return false;
1134 /* The parser. */
1136 /* Overview
1137 --------
1139 A cp_parser parses the token stream as specified by the C++
1140 grammar. Its job is purely parsing, not semantic analysis. For
1141 example, the parser breaks the token stream into declarators,
1142 expressions, statements, and other similar syntactic constructs.
1143 It does not check that the types of the expressions on either side
1144 of an assignment-statement are compatible, or that a function is
1145 not declared with a parameter of type `void'.
1147 The parser invokes routines elsewhere in the compiler to perform
1148 semantic analysis and to build up the abstract syntax tree for the
1149 code processed.
1151 The parser (and the template instantiation code, which is, in a
1152 way, a close relative of parsing) are the only parts of the
1153 compiler that should be calling push_scope and pop_scope, or
1154 related functions. The parser (and template instantiation code)
1155 keeps track of what scope is presently active; everything else
1156 should simply honor that. (The code that generates static
1157 initializers may also need to set the scope, in order to check
1158 access control correctly when emitting the initializers.)
1160 Methodology
1161 -----------
1163 The parser is of the standard recursive-descent variety. Upcoming
1164 tokens in the token stream are examined in order to determine which
1165 production to use when parsing a non-terminal. Some C++ constructs
1166 require arbitrary look ahead to disambiguate. For example, it is
1167 impossible, in the general case, to tell whether a statement is an
1168 expression or declaration without scanning the entire statement.
1169 Therefore, the parser is capable of "parsing tentatively." When the
1170 parser is not sure what construct comes next, it enters this mode.
1171 Then, while we attempt to parse the construct, the parser queues up
1172 error messages, rather than issuing them immediately, and saves the
1173 tokens it consumes. If the construct is parsed successfully, the
1174 parser "commits", i.e., it issues any queued error messages and
1175 the tokens that were being preserved are permanently discarded.
1176 If, however, the construct is not parsed successfully, the parser
1177 rolls back its state completely so that it can resume parsing using
1178 a different alternative.
1180 Future Improvements
1181 -------------------
1183 The performance of the parser could probably be improved substantially.
1184 We could often eliminate the need to parse tentatively by looking ahead
1185 a little bit. In some places, this approach might not entirely eliminate
1186 the need to parse tentatively, but it might still speed up the average
1187 case. */
1189 /* Flags that are passed to some parsing functions. These values can
1190 be bitwise-ored together. */
1192 enum
1194 /* No flags. */
1195 CP_PARSER_FLAGS_NONE = 0x0,
1196 /* The construct is optional. If it is not present, then no error
1197 should be issued. */
1198 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1199 /* When parsing a type-specifier, treat user-defined type-names
1200 as non-type identifiers. */
1201 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1202 /* When parsing a type-specifier, do not try to parse a class-specifier
1203 or enum-specifier. */
1204 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4
1207 /* This type is used for parameters and variables which hold
1208 combinations of the above flags. */
1209 typedef int cp_parser_flags;
1211 /* The different kinds of declarators we want to parse. */
1213 typedef enum cp_parser_declarator_kind
1215 /* We want an abstract declarator. */
1216 CP_PARSER_DECLARATOR_ABSTRACT,
1217 /* We want a named declarator. */
1218 CP_PARSER_DECLARATOR_NAMED,
1219 /* We don't mind, but the name must be an unqualified-id. */
1220 CP_PARSER_DECLARATOR_EITHER
1221 } cp_parser_declarator_kind;
1223 /* The precedence values used to parse binary expressions. The minimum value
1224 of PREC must be 1, because zero is reserved to quickly discriminate
1225 binary operators from other tokens. */
1227 enum cp_parser_prec
1229 PREC_NOT_OPERATOR,
1230 PREC_LOGICAL_OR_EXPRESSION,
1231 PREC_LOGICAL_AND_EXPRESSION,
1232 PREC_INCLUSIVE_OR_EXPRESSION,
1233 PREC_EXCLUSIVE_OR_EXPRESSION,
1234 PREC_AND_EXPRESSION,
1235 PREC_EQUALITY_EXPRESSION,
1236 PREC_RELATIONAL_EXPRESSION,
1237 PREC_SHIFT_EXPRESSION,
1238 PREC_ADDITIVE_EXPRESSION,
1239 PREC_MULTIPLICATIVE_EXPRESSION,
1240 PREC_PM_EXPRESSION,
1241 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1244 /* A mapping from a token type to a corresponding tree node type, with a
1245 precedence value. */
1247 typedef struct cp_parser_binary_operations_map_node
1249 /* The token type. */
1250 enum cpp_ttype token_type;
1251 /* The corresponding tree code. */
1252 enum tree_code tree_type;
1253 /* The precedence of this operator. */
1254 enum cp_parser_prec prec;
1255 } cp_parser_binary_operations_map_node;
1257 /* The status of a tentative parse. */
1259 typedef enum cp_parser_status_kind
1261 /* No errors have occurred. */
1262 CP_PARSER_STATUS_KIND_NO_ERROR,
1263 /* An error has occurred. */
1264 CP_PARSER_STATUS_KIND_ERROR,
1265 /* We are committed to this tentative parse, whether or not an error
1266 has occurred. */
1267 CP_PARSER_STATUS_KIND_COMMITTED
1268 } cp_parser_status_kind;
1270 typedef struct cp_parser_expression_stack_entry
1272 /* Left hand side of the binary operation we are currently
1273 parsing. */
1274 tree lhs;
1275 /* Original tree code for left hand side, if it was a binary
1276 expression itself (used for -Wparentheses). */
1277 enum tree_code lhs_type;
1278 /* Tree code for the binary operation we are parsing. */
1279 enum tree_code tree_type;
1280 /* Precedence of the binary operation we are parsing. */
1281 enum cp_parser_prec prec;
1282 } cp_parser_expression_stack_entry;
1284 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1285 entries because precedence levels on the stack are monotonically
1286 increasing. */
1287 typedef struct cp_parser_expression_stack_entry
1288 cp_parser_expression_stack[NUM_PREC_VALUES];
1290 /* Context that is saved and restored when parsing tentatively. */
1291 typedef struct GTY (()) cp_parser_context {
1292 /* If this is a tentative parsing context, the status of the
1293 tentative parse. */
1294 enum cp_parser_status_kind status;
1295 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1296 that are looked up in this context must be looked up both in the
1297 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1298 the context of the containing expression. */
1299 tree object_type;
1301 /* The next parsing context in the stack. */
1302 struct cp_parser_context *next;
1303 } cp_parser_context;
1305 /* Prototypes. */
1307 /* Constructors and destructors. */
1309 static cp_parser_context *cp_parser_context_new
1310 (cp_parser_context *);
1312 /* Class variables. */
1314 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1316 /* The operator-precedence table used by cp_parser_binary_expression.
1317 Transformed into an associative array (binops_by_token) by
1318 cp_parser_new. */
1320 static const cp_parser_binary_operations_map_node binops[] = {
1321 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1322 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1324 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1325 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1326 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1328 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1329 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1331 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1332 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1334 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1335 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1336 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1337 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1339 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1340 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1342 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1344 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1346 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1348 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1350 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1353 /* The same as binops, but initialized by cp_parser_new so that
1354 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1355 for speed. */
1356 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1358 /* Constructors and destructors. */
1360 /* Construct a new context. The context below this one on the stack
1361 is given by NEXT. */
1363 static cp_parser_context *
1364 cp_parser_context_new (cp_parser_context* next)
1366 cp_parser_context *context;
1368 /* Allocate the storage. */
1369 if (cp_parser_context_free_list != NULL)
1371 /* Pull the first entry from the free list. */
1372 context = cp_parser_context_free_list;
1373 cp_parser_context_free_list = context->next;
1374 memset (context, 0, sizeof (*context));
1376 else
1377 context = GGC_CNEW (cp_parser_context);
1379 /* No errors have occurred yet in this context. */
1380 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1381 /* If this is not the bottommost context, copy information that we
1382 need from the previous context. */
1383 if (next)
1385 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1386 expression, then we are parsing one in this context, too. */
1387 context->object_type = next->object_type;
1388 /* Thread the stack. */
1389 context->next = next;
1392 return context;
1395 /* The cp_parser structure represents the C++ parser. */
1397 typedef struct GTY(()) cp_parser {
1398 /* The lexer from which we are obtaining tokens. */
1399 cp_lexer *lexer;
1401 /* The scope in which names should be looked up. If NULL_TREE, then
1402 we look up names in the scope that is currently open in the
1403 source program. If non-NULL, this is either a TYPE or
1404 NAMESPACE_DECL for the scope in which we should look. It can
1405 also be ERROR_MARK, when we've parsed a bogus scope.
1407 This value is not cleared automatically after a name is looked
1408 up, so we must be careful to clear it before starting a new look
1409 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1410 will look up `Z' in the scope of `X', rather than the current
1411 scope.) Unfortunately, it is difficult to tell when name lookup
1412 is complete, because we sometimes peek at a token, look it up,
1413 and then decide not to consume it. */
1414 tree scope;
1416 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1417 last lookup took place. OBJECT_SCOPE is used if an expression
1418 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1419 respectively. QUALIFYING_SCOPE is used for an expression of the
1420 form "X::Y"; it refers to X. */
1421 tree object_scope;
1422 tree qualifying_scope;
1424 /* A stack of parsing contexts. All but the bottom entry on the
1425 stack will be tentative contexts.
1427 We parse tentatively in order to determine which construct is in
1428 use in some situations. For example, in order to determine
1429 whether a statement is an expression-statement or a
1430 declaration-statement we parse it tentatively as a
1431 declaration-statement. If that fails, we then reparse the same
1432 token stream as an expression-statement. */
1433 cp_parser_context *context;
1435 /* True if we are parsing GNU C++. If this flag is not set, then
1436 GNU extensions are not recognized. */
1437 bool allow_gnu_extensions_p;
1439 /* TRUE if the `>' token should be interpreted as the greater-than
1440 operator. FALSE if it is the end of a template-id or
1441 template-parameter-list. In C++0x mode, this flag also applies to
1442 `>>' tokens, which are viewed as two consecutive `>' tokens when
1443 this flag is FALSE. */
1444 bool greater_than_is_operator_p;
1446 /* TRUE if default arguments are allowed within a parameter list
1447 that starts at this point. FALSE if only a gnu extension makes
1448 them permissible. */
1449 bool default_arg_ok_p;
1451 /* TRUE if we are parsing an integral constant-expression. See
1452 [expr.const] for a precise definition. */
1453 bool integral_constant_expression_p;
1455 /* TRUE if we are parsing an integral constant-expression -- but a
1456 non-constant expression should be permitted as well. This flag
1457 is used when parsing an array bound so that GNU variable-length
1458 arrays are tolerated. */
1459 bool allow_non_integral_constant_expression_p;
1461 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1462 been seen that makes the expression non-constant. */
1463 bool non_integral_constant_expression_p;
1465 /* TRUE if local variable names and `this' are forbidden in the
1466 current context. */
1467 bool local_variables_forbidden_p;
1469 /* TRUE if the declaration we are parsing is part of a
1470 linkage-specification of the form `extern string-literal
1471 declaration'. */
1472 bool in_unbraced_linkage_specification_p;
1474 /* TRUE if we are presently parsing a declarator, after the
1475 direct-declarator. */
1476 bool in_declarator_p;
1478 /* TRUE if we are presently parsing a template-argument-list. */
1479 bool in_template_argument_list_p;
1481 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1482 to IN_OMP_BLOCK if parsing OpenMP structured block and
1483 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1484 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1485 iteration-statement, OpenMP block or loop within that switch. */
1486 #define IN_SWITCH_STMT 1
1487 #define IN_ITERATION_STMT 2
1488 #define IN_OMP_BLOCK 4
1489 #define IN_OMP_FOR 8
1490 #define IN_IF_STMT 16
1491 unsigned char in_statement;
1493 /* TRUE if we are presently parsing the body of a switch statement.
1494 Note that this doesn't quite overlap with in_statement above.
1495 The difference relates to giving the right sets of error messages:
1496 "case not in switch" vs "break statement used with OpenMP...". */
1497 bool in_switch_statement_p;
1499 /* TRUE if we are parsing a type-id in an expression context. In
1500 such a situation, both "type (expr)" and "type (type)" are valid
1501 alternatives. */
1502 bool in_type_id_in_expr_p;
1504 /* TRUE if we are currently in a header file where declarations are
1505 implicitly extern "C". */
1506 bool implicit_extern_c;
1508 /* TRUE if strings in expressions should be translated to the execution
1509 character set. */
1510 bool translate_strings_p;
1512 /* TRUE if we are presently parsing the body of a function, but not
1513 a local class. */
1514 bool in_function_body;
1516 /* If non-NULL, then we are parsing a construct where new type
1517 definitions are not permitted. The string stored here will be
1518 issued as an error message if a type is defined. */
1519 const char *type_definition_forbidden_message;
1521 /* A list of lists. The outer list is a stack, used for member
1522 functions of local classes. At each level there are two sub-list,
1523 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1524 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1525 TREE_VALUE's. The functions are chained in reverse declaration
1526 order.
1528 The TREE_PURPOSE sublist contains those functions with default
1529 arguments that need post processing, and the TREE_VALUE sublist
1530 contains those functions with definitions that need post
1531 processing.
1533 These lists can only be processed once the outermost class being
1534 defined is complete. */
1535 tree unparsed_functions_queues;
1537 /* The number of classes whose definitions are currently in
1538 progress. */
1539 unsigned num_classes_being_defined;
1541 /* The number of template parameter lists that apply directly to the
1542 current declaration. */
1543 unsigned num_template_parameter_lists;
1544 } cp_parser;
1546 /* Prototypes. */
1548 /* Constructors and destructors. */
1550 static cp_parser *cp_parser_new
1551 (void);
1553 /* Routines to parse various constructs.
1555 Those that return `tree' will return the error_mark_node (rather
1556 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1557 Sometimes, they will return an ordinary node if error-recovery was
1558 attempted, even though a parse error occurred. So, to check
1559 whether or not a parse error occurred, you should always use
1560 cp_parser_error_occurred. If the construct is optional (indicated
1561 either by an `_opt' in the name of the function that does the
1562 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1563 the construct is not present. */
1565 /* Lexical conventions [gram.lex] */
1567 static tree cp_parser_identifier
1568 (cp_parser *);
1569 static tree cp_parser_string_literal
1570 (cp_parser *, bool, bool);
1572 /* Basic concepts [gram.basic] */
1574 static bool cp_parser_translation_unit
1575 (cp_parser *);
1577 /* Expressions [gram.expr] */
1579 static tree cp_parser_primary_expression
1580 (cp_parser *, bool, bool, bool, cp_id_kind *);
1581 static tree cp_parser_id_expression
1582 (cp_parser *, bool, bool, bool *, bool, bool);
1583 static tree cp_parser_unqualified_id
1584 (cp_parser *, bool, bool, bool, bool);
1585 static tree cp_parser_nested_name_specifier_opt
1586 (cp_parser *, bool, bool, bool, bool);
1587 static tree cp_parser_nested_name_specifier
1588 (cp_parser *, bool, bool, bool, bool);
1589 static tree cp_parser_qualifying_entity
1590 (cp_parser *, bool, bool, bool, bool, bool);
1591 static tree cp_parser_postfix_expression
1592 (cp_parser *, bool, bool, bool, cp_id_kind *);
1593 static tree cp_parser_postfix_open_square_expression
1594 (cp_parser *, tree, bool);
1595 static tree cp_parser_postfix_dot_deref_expression
1596 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1597 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1598 (cp_parser *, bool, bool, bool, bool *);
1599 static void cp_parser_pseudo_destructor_name
1600 (cp_parser *, tree *, tree *);
1601 static tree cp_parser_unary_expression
1602 (cp_parser *, bool, bool, cp_id_kind *);
1603 static enum tree_code cp_parser_unary_operator
1604 (cp_token *);
1605 static tree cp_parser_new_expression
1606 (cp_parser *);
1607 static VEC(tree,gc) *cp_parser_new_placement
1608 (cp_parser *);
1609 static tree cp_parser_new_type_id
1610 (cp_parser *, tree *);
1611 static cp_declarator *cp_parser_new_declarator_opt
1612 (cp_parser *);
1613 static cp_declarator *cp_parser_direct_new_declarator
1614 (cp_parser *);
1615 static VEC(tree,gc) *cp_parser_new_initializer
1616 (cp_parser *);
1617 static tree cp_parser_delete_expression
1618 (cp_parser *);
1619 static tree cp_parser_cast_expression
1620 (cp_parser *, bool, bool, cp_id_kind *);
1621 static tree cp_parser_binary_expression
1622 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1623 static tree cp_parser_question_colon_clause
1624 (cp_parser *, tree);
1625 static tree cp_parser_assignment_expression
1626 (cp_parser *, bool, cp_id_kind *);
1627 static enum tree_code cp_parser_assignment_operator_opt
1628 (cp_parser *);
1629 static tree cp_parser_expression
1630 (cp_parser *, bool, cp_id_kind *);
1631 static tree cp_parser_constant_expression
1632 (cp_parser *, bool, bool *);
1633 static tree cp_parser_builtin_offsetof
1634 (cp_parser *);
1635 static tree cp_parser_lambda_expression
1636 (cp_parser *);
1637 static void cp_parser_lambda_introducer
1638 (cp_parser *, tree);
1639 static void cp_parser_lambda_declarator_opt
1640 (cp_parser *, tree);
1641 static void cp_parser_lambda_body
1642 (cp_parser *, tree);
1644 /* Statements [gram.stmt.stmt] */
1646 static void cp_parser_statement
1647 (cp_parser *, tree, bool, bool *);
1648 static void cp_parser_label_for_labeled_statement
1649 (cp_parser *);
1650 static tree cp_parser_expression_statement
1651 (cp_parser *, tree);
1652 static tree cp_parser_compound_statement
1653 (cp_parser *, tree, bool);
1654 static void cp_parser_statement_seq_opt
1655 (cp_parser *, tree);
1656 static tree cp_parser_selection_statement
1657 (cp_parser *, bool *);
1658 static tree cp_parser_condition
1659 (cp_parser *);
1660 static tree cp_parser_iteration_statement
1661 (cp_parser *);
1662 static void cp_parser_for_init_statement
1663 (cp_parser *);
1664 static tree cp_parser_jump_statement
1665 (cp_parser *);
1666 static void cp_parser_declaration_statement
1667 (cp_parser *);
1669 static tree cp_parser_implicitly_scoped_statement
1670 (cp_parser *, bool *);
1671 static void cp_parser_already_scoped_statement
1672 (cp_parser *);
1674 /* Declarations [gram.dcl.dcl] */
1676 static void cp_parser_declaration_seq_opt
1677 (cp_parser *);
1678 static void cp_parser_declaration
1679 (cp_parser *);
1680 static void cp_parser_block_declaration
1681 (cp_parser *, bool);
1682 static void cp_parser_simple_declaration
1683 (cp_parser *, bool);
1684 static void cp_parser_decl_specifier_seq
1685 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1686 static tree cp_parser_storage_class_specifier_opt
1687 (cp_parser *);
1688 static tree cp_parser_function_specifier_opt
1689 (cp_parser *, cp_decl_specifier_seq *);
1690 static tree cp_parser_type_specifier
1691 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1692 int *, bool *);
1693 static tree cp_parser_simple_type_specifier
1694 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1695 static tree cp_parser_type_name
1696 (cp_parser *);
1697 static tree cp_parser_nonclass_name
1698 (cp_parser* parser);
1699 static tree cp_parser_elaborated_type_specifier
1700 (cp_parser *, bool, bool);
1701 static tree cp_parser_enum_specifier
1702 (cp_parser *);
1703 static void cp_parser_enumerator_list
1704 (cp_parser *, tree);
1705 static void cp_parser_enumerator_definition
1706 (cp_parser *, tree);
1707 static tree cp_parser_namespace_name
1708 (cp_parser *);
1709 static void cp_parser_namespace_definition
1710 (cp_parser *);
1711 static void cp_parser_namespace_body
1712 (cp_parser *);
1713 static tree cp_parser_qualified_namespace_specifier
1714 (cp_parser *);
1715 static void cp_parser_namespace_alias_definition
1716 (cp_parser *);
1717 static bool cp_parser_using_declaration
1718 (cp_parser *, bool);
1719 static void cp_parser_using_directive
1720 (cp_parser *);
1721 static void cp_parser_asm_definition
1722 (cp_parser *);
1723 static void cp_parser_linkage_specification
1724 (cp_parser *);
1725 static void cp_parser_static_assert
1726 (cp_parser *, bool);
1727 static tree cp_parser_decltype
1728 (cp_parser *);
1730 /* Declarators [gram.dcl.decl] */
1732 static tree cp_parser_init_declarator
1733 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1734 static cp_declarator *cp_parser_declarator
1735 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1736 static cp_declarator *cp_parser_direct_declarator
1737 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1738 static enum tree_code cp_parser_ptr_operator
1739 (cp_parser *, tree *, cp_cv_quals *);
1740 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1741 (cp_parser *);
1742 static tree cp_parser_late_return_type_opt
1743 (cp_parser *);
1744 static tree cp_parser_declarator_id
1745 (cp_parser *, bool);
1746 static tree cp_parser_type_id
1747 (cp_parser *);
1748 static tree cp_parser_template_type_arg
1749 (cp_parser *);
1750 static tree cp_parser_trailing_type_id (cp_parser *);
1751 static tree cp_parser_type_id_1
1752 (cp_parser *, bool, bool);
1753 static void cp_parser_type_specifier_seq
1754 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1755 static tree cp_parser_parameter_declaration_clause
1756 (cp_parser *);
1757 static tree cp_parser_parameter_declaration_list
1758 (cp_parser *, bool *);
1759 static cp_parameter_declarator *cp_parser_parameter_declaration
1760 (cp_parser *, bool, bool *);
1761 static tree cp_parser_default_argument
1762 (cp_parser *, bool);
1763 static void cp_parser_function_body
1764 (cp_parser *);
1765 static tree cp_parser_initializer
1766 (cp_parser *, bool *, bool *);
1767 static tree cp_parser_initializer_clause
1768 (cp_parser *, bool *);
1769 static tree cp_parser_braced_list
1770 (cp_parser*, bool*);
1771 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1772 (cp_parser *, bool *);
1774 static bool cp_parser_ctor_initializer_opt_and_function_body
1775 (cp_parser *);
1777 /* Classes [gram.class] */
1779 static tree cp_parser_class_name
1780 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1781 static tree cp_parser_class_specifier
1782 (cp_parser *);
1783 static tree cp_parser_class_head
1784 (cp_parser *, bool *, tree *, tree *);
1785 static enum tag_types cp_parser_class_key
1786 (cp_parser *);
1787 static void cp_parser_member_specification_opt
1788 (cp_parser *);
1789 static void cp_parser_member_declaration
1790 (cp_parser *);
1791 static tree cp_parser_pure_specifier
1792 (cp_parser *);
1793 static tree cp_parser_constant_initializer
1794 (cp_parser *);
1796 /* Derived classes [gram.class.derived] */
1798 static tree cp_parser_base_clause
1799 (cp_parser *);
1800 static tree cp_parser_base_specifier
1801 (cp_parser *);
1803 /* Special member functions [gram.special] */
1805 static tree cp_parser_conversion_function_id
1806 (cp_parser *);
1807 static tree cp_parser_conversion_type_id
1808 (cp_parser *);
1809 static cp_declarator *cp_parser_conversion_declarator_opt
1810 (cp_parser *);
1811 static bool cp_parser_ctor_initializer_opt
1812 (cp_parser *);
1813 static void cp_parser_mem_initializer_list
1814 (cp_parser *);
1815 static tree cp_parser_mem_initializer
1816 (cp_parser *);
1817 static tree cp_parser_mem_initializer_id
1818 (cp_parser *);
1820 /* Overloading [gram.over] */
1822 static tree cp_parser_operator_function_id
1823 (cp_parser *);
1824 static tree cp_parser_operator
1825 (cp_parser *);
1827 /* Templates [gram.temp] */
1829 static void cp_parser_template_declaration
1830 (cp_parser *, bool);
1831 static tree cp_parser_template_parameter_list
1832 (cp_parser *);
1833 static tree cp_parser_template_parameter
1834 (cp_parser *, bool *, bool *);
1835 static tree cp_parser_type_parameter
1836 (cp_parser *, bool *);
1837 static tree cp_parser_template_id
1838 (cp_parser *, bool, bool, bool);
1839 static tree cp_parser_template_name
1840 (cp_parser *, bool, bool, bool, bool *);
1841 static tree cp_parser_template_argument_list
1842 (cp_parser *);
1843 static tree cp_parser_template_argument
1844 (cp_parser *);
1845 static void cp_parser_explicit_instantiation
1846 (cp_parser *);
1847 static void cp_parser_explicit_specialization
1848 (cp_parser *);
1850 /* Exception handling [gram.exception] */
1852 static tree cp_parser_try_block
1853 (cp_parser *);
1854 static bool cp_parser_function_try_block
1855 (cp_parser *);
1856 static void cp_parser_handler_seq
1857 (cp_parser *);
1858 static void cp_parser_handler
1859 (cp_parser *);
1860 static tree cp_parser_exception_declaration
1861 (cp_parser *);
1862 static tree cp_parser_throw_expression
1863 (cp_parser *);
1864 static tree cp_parser_exception_specification_opt
1865 (cp_parser *);
1866 static tree cp_parser_type_id_list
1867 (cp_parser *);
1869 /* GNU Extensions */
1871 static tree cp_parser_asm_specification_opt
1872 (cp_parser *);
1873 static tree cp_parser_asm_operand_list
1874 (cp_parser *);
1875 static tree cp_parser_asm_clobber_list
1876 (cp_parser *);
1877 static tree cp_parser_asm_label_list
1878 (cp_parser *);
1879 static tree cp_parser_attributes_opt
1880 (cp_parser *);
1881 static tree cp_parser_attribute_list
1882 (cp_parser *);
1883 static bool cp_parser_extension_opt
1884 (cp_parser *, int *);
1885 static void cp_parser_label_declaration
1886 (cp_parser *);
1888 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1889 static bool cp_parser_pragma
1890 (cp_parser *, enum pragma_context);
1892 /* Objective-C++ Productions */
1894 static tree cp_parser_objc_message_receiver
1895 (cp_parser *);
1896 static tree cp_parser_objc_message_args
1897 (cp_parser *);
1898 static tree cp_parser_objc_message_expression
1899 (cp_parser *);
1900 static tree cp_parser_objc_encode_expression
1901 (cp_parser *);
1902 static tree cp_parser_objc_defs_expression
1903 (cp_parser *);
1904 static tree cp_parser_objc_protocol_expression
1905 (cp_parser *);
1906 static tree cp_parser_objc_selector_expression
1907 (cp_parser *);
1908 static tree cp_parser_objc_expression
1909 (cp_parser *);
1910 static bool cp_parser_objc_selector_p
1911 (enum cpp_ttype);
1912 static tree cp_parser_objc_selector
1913 (cp_parser *);
1914 static tree cp_parser_objc_protocol_refs_opt
1915 (cp_parser *);
1916 static void cp_parser_objc_declaration
1917 (cp_parser *);
1918 static tree cp_parser_objc_statement
1919 (cp_parser *);
1921 /* Utility Routines */
1923 static tree cp_parser_lookup_name
1924 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1925 static tree cp_parser_lookup_name_simple
1926 (cp_parser *, tree, location_t);
1927 static tree cp_parser_maybe_treat_template_as_class
1928 (tree, bool);
1929 static bool cp_parser_check_declarator_template_parameters
1930 (cp_parser *, cp_declarator *, location_t);
1931 static bool cp_parser_check_template_parameters
1932 (cp_parser *, unsigned, location_t, cp_declarator *);
1933 static tree cp_parser_simple_cast_expression
1934 (cp_parser *);
1935 static tree cp_parser_global_scope_opt
1936 (cp_parser *, bool);
1937 static bool cp_parser_constructor_declarator_p
1938 (cp_parser *, bool);
1939 static tree cp_parser_function_definition_from_specifiers_and_declarator
1940 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1941 static tree cp_parser_function_definition_after_declarator
1942 (cp_parser *, bool);
1943 static void cp_parser_template_declaration_after_export
1944 (cp_parser *, bool);
1945 static void cp_parser_perform_template_parameter_access_checks
1946 (VEC (deferred_access_check,gc)*);
1947 static tree cp_parser_single_declaration
1948 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1949 static tree cp_parser_functional_cast
1950 (cp_parser *, tree);
1951 static tree cp_parser_save_member_function_body
1952 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1953 static tree cp_parser_enclosed_template_argument_list
1954 (cp_parser *);
1955 static void cp_parser_save_default_args
1956 (cp_parser *, tree);
1957 static void cp_parser_late_parsing_for_member
1958 (cp_parser *, tree);
1959 static void cp_parser_late_parsing_default_args
1960 (cp_parser *, tree);
1961 static tree cp_parser_sizeof_operand
1962 (cp_parser *, enum rid);
1963 static tree cp_parser_trait_expr
1964 (cp_parser *, enum rid);
1965 static bool cp_parser_declares_only_class_p
1966 (cp_parser *);
1967 static void cp_parser_set_storage_class
1968 (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1969 static void cp_parser_set_decl_spec_type
1970 (cp_decl_specifier_seq *, tree, location_t, bool);
1971 static bool cp_parser_friend_p
1972 (const cp_decl_specifier_seq *);
1973 static cp_token *cp_parser_require
1974 (cp_parser *, enum cpp_ttype, const char *);
1975 static cp_token *cp_parser_require_keyword
1976 (cp_parser *, enum rid, const char *);
1977 static bool cp_parser_token_starts_function_definition_p
1978 (cp_token *);
1979 static bool cp_parser_next_token_starts_class_definition_p
1980 (cp_parser *);
1981 static bool cp_parser_next_token_ends_template_argument_p
1982 (cp_parser *);
1983 static bool cp_parser_nth_token_starts_template_argument_list_p
1984 (cp_parser *, size_t);
1985 static enum tag_types cp_parser_token_is_class_key
1986 (cp_token *);
1987 static void cp_parser_check_class_key
1988 (enum tag_types, tree type);
1989 static void cp_parser_check_access_in_redeclaration
1990 (tree type, location_t location);
1991 static bool cp_parser_optional_template_keyword
1992 (cp_parser *);
1993 static void cp_parser_pre_parsed_nested_name_specifier
1994 (cp_parser *);
1995 static bool cp_parser_cache_group
1996 (cp_parser *, enum cpp_ttype, unsigned);
1997 static void cp_parser_parse_tentatively
1998 (cp_parser *);
1999 static void cp_parser_commit_to_tentative_parse
2000 (cp_parser *);
2001 static void cp_parser_abort_tentative_parse
2002 (cp_parser *);
2003 static bool cp_parser_parse_definitely
2004 (cp_parser *);
2005 static inline bool cp_parser_parsing_tentatively
2006 (cp_parser *);
2007 static bool cp_parser_uncommitted_to_tentative_parse_p
2008 (cp_parser *);
2009 static void cp_parser_error
2010 (cp_parser *, const char *);
2011 static void cp_parser_name_lookup_error
2012 (cp_parser *, tree, tree, const char *, location_t);
2013 static bool cp_parser_simulate_error
2014 (cp_parser *);
2015 static bool cp_parser_check_type_definition
2016 (cp_parser *);
2017 static void cp_parser_check_for_definition_in_return_type
2018 (cp_declarator *, tree, location_t type_location);
2019 static void cp_parser_check_for_invalid_template_id
2020 (cp_parser *, tree, location_t location);
2021 static bool cp_parser_non_integral_constant_expression
2022 (cp_parser *, const char *);
2023 static void cp_parser_diagnose_invalid_type_name
2024 (cp_parser *, tree, tree, location_t);
2025 static bool cp_parser_parse_and_diagnose_invalid_type_name
2026 (cp_parser *);
2027 static int cp_parser_skip_to_closing_parenthesis
2028 (cp_parser *, bool, bool, bool);
2029 static void cp_parser_skip_to_end_of_statement
2030 (cp_parser *);
2031 static void cp_parser_consume_semicolon_at_end_of_statement
2032 (cp_parser *);
2033 static void cp_parser_skip_to_end_of_block_or_statement
2034 (cp_parser *);
2035 static bool cp_parser_skip_to_closing_brace
2036 (cp_parser *);
2037 static void cp_parser_skip_to_end_of_template_parameter_list
2038 (cp_parser *);
2039 static void cp_parser_skip_to_pragma_eol
2040 (cp_parser*, cp_token *);
2041 static bool cp_parser_error_occurred
2042 (cp_parser *);
2043 static bool cp_parser_allow_gnu_extensions_p
2044 (cp_parser *);
2045 static bool cp_parser_is_string_literal
2046 (cp_token *);
2047 static bool cp_parser_is_keyword
2048 (cp_token *, enum rid);
2049 static tree cp_parser_make_typename_type
2050 (cp_parser *, tree, tree, location_t location);
2051 static cp_declarator * cp_parser_make_indirect_declarator
2052 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2054 /* Returns nonzero if we are parsing tentatively. */
2056 static inline bool
2057 cp_parser_parsing_tentatively (cp_parser* parser)
2059 return parser->context->next != NULL;
2062 /* Returns nonzero if TOKEN is a string literal. */
2064 static bool
2065 cp_parser_is_string_literal (cp_token* token)
2067 return (token->type == CPP_STRING ||
2068 token->type == CPP_STRING16 ||
2069 token->type == CPP_STRING32 ||
2070 token->type == CPP_WSTRING ||
2071 token->type == CPP_UTF8STRING);
2074 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2076 static bool
2077 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2079 return token->keyword == keyword;
2082 /* If not parsing tentatively, issue a diagnostic of the form
2083 FILE:LINE: MESSAGE before TOKEN
2084 where TOKEN is the next token in the input stream. MESSAGE
2085 (specified by the caller) is usually of the form "expected
2086 OTHER-TOKEN". */
2088 static void
2089 cp_parser_error (cp_parser* parser, const char* message)
2091 if (!cp_parser_simulate_error (parser))
2093 cp_token *token = cp_lexer_peek_token (parser->lexer);
2094 /* This diagnostic makes more sense if it is tagged to the line
2095 of the token we just peeked at. */
2096 cp_lexer_set_source_position_from_token (token);
2098 if (token->type == CPP_PRAGMA)
2100 error_at (token->location,
2101 "%<#pragma%> is not allowed here");
2102 cp_parser_skip_to_pragma_eol (parser, token);
2103 return;
2106 c_parse_error (message,
2107 /* Because c_parser_error does not understand
2108 CPP_KEYWORD, keywords are treated like
2109 identifiers. */
2110 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2111 token->u.value, token->flags);
2115 /* Issue an error about name-lookup failing. NAME is the
2116 IDENTIFIER_NODE DECL is the result of
2117 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2118 the thing that we hoped to find. */
2120 static void
2121 cp_parser_name_lookup_error (cp_parser* parser,
2122 tree name,
2123 tree decl,
2124 const char* desired,
2125 location_t location)
2127 /* If name lookup completely failed, tell the user that NAME was not
2128 declared. */
2129 if (decl == error_mark_node)
2131 if (parser->scope && parser->scope != global_namespace)
2132 error_at (location, "%<%E::%E%> has not been declared",
2133 parser->scope, name);
2134 else if (parser->scope == global_namespace)
2135 error_at (location, "%<::%E%> has not been declared", name);
2136 else if (parser->object_scope
2137 && !CLASS_TYPE_P (parser->object_scope))
2138 error_at (location, "request for member %qE in non-class type %qT",
2139 name, parser->object_scope);
2140 else if (parser->object_scope)
2141 error_at (location, "%<%T::%E%> has not been declared",
2142 parser->object_scope, name);
2143 else
2144 error_at (location, "%qE has not been declared", name);
2146 else if (parser->scope && parser->scope != global_namespace)
2147 error_at (location, "%<%E::%E%> %s", parser->scope, name, desired);
2148 else if (parser->scope == global_namespace)
2149 error_at (location, "%<::%E%> %s", name, desired);
2150 else
2151 error_at (location, "%qE %s", name, desired);
2154 /* If we are parsing tentatively, remember that an error has occurred
2155 during this tentative parse. Returns true if the error was
2156 simulated; false if a message should be issued by the caller. */
2158 static bool
2159 cp_parser_simulate_error (cp_parser* parser)
2161 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2163 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2164 return true;
2166 return false;
2169 /* Check for repeated decl-specifiers. */
2171 static void
2172 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2173 location_t location)
2175 int ds;
2177 for (ds = ds_first; ds != ds_last; ++ds)
2179 unsigned count = decl_specs->specs[ds];
2180 if (count < 2)
2181 continue;
2182 /* The "long" specifier is a special case because of "long long". */
2183 if (ds == ds_long)
2185 if (count > 2)
2186 error_at (location, "%<long long long%> is too long for GCC");
2187 else
2188 pedwarn_cxx98 (location, OPT_Wlong_long,
2189 "ISO C++ 1998 does not support %<long long%>");
2191 else if (count > 1)
2193 static const char *const decl_spec_names[] = {
2194 "signed",
2195 "unsigned",
2196 "short",
2197 "long",
2198 "const",
2199 "volatile",
2200 "restrict",
2201 "inline",
2202 "virtual",
2203 "explicit",
2204 "friend",
2205 "typedef",
2206 "constexpr",
2207 "__complex",
2208 "__thread"
2210 error_at (location, "duplicate %qs", decl_spec_names[ds]);
2215 /* This function is called when a type is defined. If type
2216 definitions are forbidden at this point, an error message is
2217 issued. */
2219 static bool
2220 cp_parser_check_type_definition (cp_parser* parser)
2222 /* If types are forbidden here, issue a message. */
2223 if (parser->type_definition_forbidden_message)
2225 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2226 in the message need to be interpreted. */
2227 error (parser->type_definition_forbidden_message);
2228 return false;
2230 return true;
2233 /* This function is called when the DECLARATOR is processed. The TYPE
2234 was a type defined in the decl-specifiers. If it is invalid to
2235 define a type in the decl-specifiers for DECLARATOR, an error is
2236 issued. TYPE_LOCATION is the location of TYPE and is used
2237 for error reporting. */
2239 static void
2240 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2241 tree type, location_t type_location)
2243 /* [dcl.fct] forbids type definitions in return types.
2244 Unfortunately, it's not easy to know whether or not we are
2245 processing a return type until after the fact. */
2246 while (declarator
2247 && (declarator->kind == cdk_pointer
2248 || declarator->kind == cdk_reference
2249 || declarator->kind == cdk_ptrmem))
2250 declarator = declarator->declarator;
2251 if (declarator
2252 && declarator->kind == cdk_function)
2254 error_at (type_location,
2255 "new types may not be defined in a return type");
2256 inform (type_location,
2257 "(perhaps a semicolon is missing after the definition of %qT)",
2258 type);
2262 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2263 "<" in any valid C++ program. If the next token is indeed "<",
2264 issue a message warning the user about what appears to be an
2265 invalid attempt to form a template-id. LOCATION is the location
2266 of the type-specifier (TYPE) */
2268 static void
2269 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2270 tree type, location_t location)
2272 cp_token_position start = 0;
2274 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2276 if (TYPE_P (type))
2277 error_at (location, "%qT is not a template", type);
2278 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2279 error_at (location, "%qE is not a template", type);
2280 else
2281 error_at (location, "invalid template-id");
2282 /* Remember the location of the invalid "<". */
2283 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2284 start = cp_lexer_token_position (parser->lexer, true);
2285 /* Consume the "<". */
2286 cp_lexer_consume_token (parser->lexer);
2287 /* Parse the template arguments. */
2288 cp_parser_enclosed_template_argument_list (parser);
2289 /* Permanently remove the invalid template arguments so that
2290 this error message is not issued again. */
2291 if (start)
2292 cp_lexer_purge_tokens_after (parser->lexer, start);
2296 /* If parsing an integral constant-expression, issue an error message
2297 about the fact that THING appeared and return true. Otherwise,
2298 return false. In either case, set
2299 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2301 static bool
2302 cp_parser_non_integral_constant_expression (cp_parser *parser,
2303 const char *thing)
2305 parser->non_integral_constant_expression_p = true;
2306 if (parser->integral_constant_expression_p)
2308 if (!parser->allow_non_integral_constant_expression_p)
2310 /* Don't use `%s' to print THING, because quotations (`%<', `%>')
2311 in the message need to be interpreted. */
2312 char *message = concat (thing,
2313 " cannot appear in a constant-expression",
2314 NULL);
2315 error (message);
2316 free (message);
2317 return true;
2320 return false;
2323 /* Emit a diagnostic for an invalid type name. SCOPE is the
2324 qualifying scope (or NULL, if none) for ID. This function commits
2325 to the current active tentative parse, if any. (Otherwise, the
2326 problematic construct might be encountered again later, resulting
2327 in duplicate error messages.) LOCATION is the location of ID. */
2329 static void
2330 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2331 tree scope, tree id,
2332 location_t location)
2334 tree decl, old_scope;
2335 /* Try to lookup the identifier. */
2336 old_scope = parser->scope;
2337 parser->scope = scope;
2338 decl = cp_parser_lookup_name_simple (parser, id, location);
2339 parser->scope = old_scope;
2340 /* If the lookup found a template-name, it means that the user forgot
2341 to specify an argument list. Emit a useful error message. */
2342 if (TREE_CODE (decl) == TEMPLATE_DECL)
2343 error_at (location,
2344 "invalid use of template-name %qE without an argument list",
2345 decl);
2346 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2347 error_at (location, "invalid use of destructor %qD as a type", id);
2348 else if (TREE_CODE (decl) == TYPE_DECL)
2349 /* Something like 'unsigned A a;' */
2350 error_at (location, "invalid combination of multiple type-specifiers");
2351 else if (!parser->scope)
2353 /* Issue an error message. */
2354 error_at (location, "%qE does not name a type", id);
2355 /* If we're in a template class, it's possible that the user was
2356 referring to a type from a base class. For example:
2358 template <typename T> struct A { typedef T X; };
2359 template <typename T> struct B : public A<T> { X x; };
2361 The user should have said "typename A<T>::X". */
2362 if (processing_template_decl && current_class_type
2363 && TYPE_BINFO (current_class_type))
2365 tree b;
2367 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2369 b = TREE_CHAIN (b))
2371 tree base_type = BINFO_TYPE (b);
2372 if (CLASS_TYPE_P (base_type)
2373 && dependent_type_p (base_type))
2375 tree field;
2376 /* Go from a particular instantiation of the
2377 template (which will have an empty TYPE_FIELDs),
2378 to the main version. */
2379 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2380 for (field = TYPE_FIELDS (base_type);
2381 field;
2382 field = TREE_CHAIN (field))
2383 if (TREE_CODE (field) == TYPE_DECL
2384 && DECL_NAME (field) == id)
2386 inform (location,
2387 "(perhaps %<typename %T::%E%> was intended)",
2388 BINFO_TYPE (b), id);
2389 break;
2391 if (field)
2392 break;
2397 /* Here we diagnose qualified-ids where the scope is actually correct,
2398 but the identifier does not resolve to a valid type name. */
2399 else if (parser->scope != error_mark_node)
2401 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2402 error_at (location, "%qE in namespace %qE does not name a type",
2403 id, parser->scope);
2404 else if (CLASS_TYPE_P (parser->scope)
2405 && constructor_name_p (id, parser->scope))
2407 /* A<T>::A<T>() */
2408 error_at (location, "%<%T::%E%> names the constructor, not"
2409 " the type", parser->scope, id);
2410 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2411 error_at (location, "and %qT has no template constructors",
2412 parser->scope);
2414 else if (TYPE_P (parser->scope)
2415 && dependent_scope_p (parser->scope))
2416 error_at (location, "need %<typename%> before %<%T::%E%> because "
2417 "%qT is a dependent scope",
2418 parser->scope, id, parser->scope);
2419 else if (TYPE_P (parser->scope))
2420 error_at (location, "%qE in class %qT does not name a type",
2421 id, parser->scope);
2422 else
2423 gcc_unreachable ();
2425 cp_parser_commit_to_tentative_parse (parser);
2428 /* Check for a common situation where a type-name should be present,
2429 but is not, and issue a sensible error message. Returns true if an
2430 invalid type-name was detected.
2432 The situation handled by this function are variable declarations of the
2433 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2434 Usually, `ID' should name a type, but if we got here it means that it
2435 does not. We try to emit the best possible error message depending on
2436 how exactly the id-expression looks like. */
2438 static bool
2439 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2441 tree id;
2442 cp_token *token = cp_lexer_peek_token (parser->lexer);
2444 /* Avoid duplicate error about ambiguous lookup. */
2445 if (token->type == CPP_NESTED_NAME_SPECIFIER)
2447 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2448 if (next->type == CPP_NAME && next->ambiguous_p)
2449 goto out;
2452 cp_parser_parse_tentatively (parser);
2453 id = cp_parser_id_expression (parser,
2454 /*template_keyword_p=*/false,
2455 /*check_dependency_p=*/true,
2456 /*template_p=*/NULL,
2457 /*declarator_p=*/true,
2458 /*optional_p=*/false);
2459 /* If the next token is a (, this is a function with no explicit return
2460 type, i.e. constructor, destructor or conversion op. */
2461 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2462 || TREE_CODE (id) == TYPE_DECL)
2464 cp_parser_abort_tentative_parse (parser);
2465 return false;
2467 if (!cp_parser_parse_definitely (parser))
2468 return false;
2470 /* Emit a diagnostic for the invalid type. */
2471 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2472 id, token->location);
2473 out:
2474 /* If we aren't in the middle of a declarator (i.e. in a
2475 parameter-declaration-clause), skip to the end of the declaration;
2476 there's no point in trying to process it. */
2477 if (!parser->in_declarator_p)
2478 cp_parser_skip_to_end_of_block_or_statement (parser);
2479 return true;
2482 /* Consume tokens up to, and including, the next non-nested closing `)'.
2483 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2484 are doing error recovery. Returns -1 if OR_COMMA is true and we
2485 found an unnested comma. */
2487 static int
2488 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2489 bool recovering,
2490 bool or_comma,
2491 bool consume_paren)
2493 unsigned paren_depth = 0;
2494 unsigned brace_depth = 0;
2495 unsigned square_depth = 0;
2497 if (recovering && !or_comma
2498 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2499 return 0;
2501 while (true)
2503 cp_token * token = cp_lexer_peek_token (parser->lexer);
2505 switch (token->type)
2507 case CPP_EOF:
2508 case CPP_PRAGMA_EOL:
2509 /* If we've run out of tokens, then there is no closing `)'. */
2510 return 0;
2512 /* This is good for lambda expression capture-lists. */
2513 case CPP_OPEN_SQUARE:
2514 ++square_depth;
2515 break;
2516 case CPP_CLOSE_SQUARE:
2517 if (!square_depth--)
2518 return 0;
2519 break;
2521 case CPP_SEMICOLON:
2522 /* This matches the processing in skip_to_end_of_statement. */
2523 if (!brace_depth)
2524 return 0;
2525 break;
2527 case CPP_OPEN_BRACE:
2528 ++brace_depth;
2529 break;
2530 case CPP_CLOSE_BRACE:
2531 if (!brace_depth--)
2532 return 0;
2533 break;
2535 case CPP_COMMA:
2536 if (recovering && or_comma && !brace_depth && !paren_depth
2537 && !square_depth)
2538 return -1;
2539 break;
2541 case CPP_OPEN_PAREN:
2542 if (!brace_depth)
2543 ++paren_depth;
2544 break;
2546 case CPP_CLOSE_PAREN:
2547 if (!brace_depth && !paren_depth--)
2549 if (consume_paren)
2550 cp_lexer_consume_token (parser->lexer);
2551 return 1;
2553 break;
2555 default:
2556 break;
2559 /* Consume the token. */
2560 cp_lexer_consume_token (parser->lexer);
2564 /* Consume tokens until we reach the end of the current statement.
2565 Normally, that will be just before consuming a `;'. However, if a
2566 non-nested `}' comes first, then we stop before consuming that. */
2568 static void
2569 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2571 unsigned nesting_depth = 0;
2573 while (true)
2575 cp_token *token = cp_lexer_peek_token (parser->lexer);
2577 switch (token->type)
2579 case CPP_EOF:
2580 case CPP_PRAGMA_EOL:
2581 /* If we've run out of tokens, stop. */
2582 return;
2584 case CPP_SEMICOLON:
2585 /* If the next token is a `;', we have reached the end of the
2586 statement. */
2587 if (!nesting_depth)
2588 return;
2589 break;
2591 case CPP_CLOSE_BRACE:
2592 /* If this is a non-nested '}', stop before consuming it.
2593 That way, when confronted with something like:
2595 { 3 + }
2597 we stop before consuming the closing '}', even though we
2598 have not yet reached a `;'. */
2599 if (nesting_depth == 0)
2600 return;
2602 /* If it is the closing '}' for a block that we have
2603 scanned, stop -- but only after consuming the token.
2604 That way given:
2606 void f g () { ... }
2607 typedef int I;
2609 we will stop after the body of the erroneously declared
2610 function, but before consuming the following `typedef'
2611 declaration. */
2612 if (--nesting_depth == 0)
2614 cp_lexer_consume_token (parser->lexer);
2615 return;
2618 case CPP_OPEN_BRACE:
2619 ++nesting_depth;
2620 break;
2622 default:
2623 break;
2626 /* Consume the token. */
2627 cp_lexer_consume_token (parser->lexer);
2631 /* This function is called at the end of a statement or declaration.
2632 If the next token is a semicolon, it is consumed; otherwise, error
2633 recovery is attempted. */
2635 static void
2636 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2638 /* Look for the trailing `;'. */
2639 if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2641 /* If there is additional (erroneous) input, skip to the end of
2642 the statement. */
2643 cp_parser_skip_to_end_of_statement (parser);
2644 /* If the next token is now a `;', consume it. */
2645 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2646 cp_lexer_consume_token (parser->lexer);
2650 /* Skip tokens until we have consumed an entire block, or until we
2651 have consumed a non-nested `;'. */
2653 static void
2654 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2656 int nesting_depth = 0;
2658 while (nesting_depth >= 0)
2660 cp_token *token = cp_lexer_peek_token (parser->lexer);
2662 switch (token->type)
2664 case CPP_EOF:
2665 case CPP_PRAGMA_EOL:
2666 /* If we've run out of tokens, stop. */
2667 return;
2669 case CPP_SEMICOLON:
2670 /* Stop if this is an unnested ';'. */
2671 if (!nesting_depth)
2672 nesting_depth = -1;
2673 break;
2675 case CPP_CLOSE_BRACE:
2676 /* Stop if this is an unnested '}', or closes the outermost
2677 nesting level. */
2678 nesting_depth--;
2679 if (nesting_depth < 0)
2680 return;
2681 if (!nesting_depth)
2682 nesting_depth = -1;
2683 break;
2685 case CPP_OPEN_BRACE:
2686 /* Nest. */
2687 nesting_depth++;
2688 break;
2690 default:
2691 break;
2694 /* Consume the token. */
2695 cp_lexer_consume_token (parser->lexer);
2699 /* Skip tokens until a non-nested closing curly brace is the next
2700 token, or there are no more tokens. Return true in the first case,
2701 false otherwise. */
2703 static bool
2704 cp_parser_skip_to_closing_brace (cp_parser *parser)
2706 unsigned nesting_depth = 0;
2708 while (true)
2710 cp_token *token = cp_lexer_peek_token (parser->lexer);
2712 switch (token->type)
2714 case CPP_EOF:
2715 case CPP_PRAGMA_EOL:
2716 /* If we've run out of tokens, stop. */
2717 return false;
2719 case CPP_CLOSE_BRACE:
2720 /* If the next token is a non-nested `}', then we have reached
2721 the end of the current block. */
2722 if (nesting_depth-- == 0)
2723 return true;
2724 break;
2726 case CPP_OPEN_BRACE:
2727 /* If it the next token is a `{', then we are entering a new
2728 block. Consume the entire block. */
2729 ++nesting_depth;
2730 break;
2732 default:
2733 break;
2736 /* Consume the token. */
2737 cp_lexer_consume_token (parser->lexer);
2741 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2742 parameter is the PRAGMA token, allowing us to purge the entire pragma
2743 sequence. */
2745 static void
2746 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2748 cp_token *token;
2750 parser->lexer->in_pragma = false;
2753 token = cp_lexer_consume_token (parser->lexer);
2754 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2756 /* Ensure that the pragma is not parsed again. */
2757 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2760 /* Require pragma end of line, resyncing with it as necessary. The
2761 arguments are as for cp_parser_skip_to_pragma_eol. */
2763 static void
2764 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2766 parser->lexer->in_pragma = false;
2767 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2768 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2771 /* This is a simple wrapper around make_typename_type. When the id is
2772 an unresolved identifier node, we can provide a superior diagnostic
2773 using cp_parser_diagnose_invalid_type_name. */
2775 static tree
2776 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2777 tree id, location_t id_location)
2779 tree result;
2780 if (TREE_CODE (id) == IDENTIFIER_NODE)
2782 result = make_typename_type (scope, id, typename_type,
2783 /*complain=*/tf_none);
2784 if (result == error_mark_node)
2785 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2786 return result;
2788 return make_typename_type (scope, id, typename_type, tf_error);
2791 /* This is a wrapper around the
2792 make_{pointer,ptrmem,reference}_declarator functions that decides
2793 which one to call based on the CODE and CLASS_TYPE arguments. The
2794 CODE argument should be one of the values returned by
2795 cp_parser_ptr_operator. */
2796 static cp_declarator *
2797 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2798 cp_cv_quals cv_qualifiers,
2799 cp_declarator *target)
2801 if (code == ERROR_MARK)
2802 return cp_error_declarator;
2804 if (code == INDIRECT_REF)
2805 if (class_type == NULL_TREE)
2806 return make_pointer_declarator (cv_qualifiers, target);
2807 else
2808 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2809 else if (code == ADDR_EXPR && class_type == NULL_TREE)
2810 return make_reference_declarator (cv_qualifiers, target, false);
2811 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2812 return make_reference_declarator (cv_qualifiers, target, true);
2813 gcc_unreachable ();
2816 /* Create a new C++ parser. */
2818 static cp_parser *
2819 cp_parser_new (void)
2821 cp_parser *parser;
2822 cp_lexer *lexer;
2823 unsigned i;
2825 /* cp_lexer_new_main is called before calling ggc_alloc because
2826 cp_lexer_new_main might load a PCH file. */
2827 lexer = cp_lexer_new_main ();
2829 /* Initialize the binops_by_token so that we can get the tree
2830 directly from the token. */
2831 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2832 binops_by_token[binops[i].token_type] = binops[i];
2834 parser = GGC_CNEW (cp_parser);
2835 parser->lexer = lexer;
2836 parser->context = cp_parser_context_new (NULL);
2838 /* For now, we always accept GNU extensions. */
2839 parser->allow_gnu_extensions_p = 1;
2841 /* The `>' token is a greater-than operator, not the end of a
2842 template-id. */
2843 parser->greater_than_is_operator_p = true;
2845 parser->default_arg_ok_p = true;
2847 /* We are not parsing a constant-expression. */
2848 parser->integral_constant_expression_p = false;
2849 parser->allow_non_integral_constant_expression_p = false;
2850 parser->non_integral_constant_expression_p = false;
2852 /* Local variable names are not forbidden. */
2853 parser->local_variables_forbidden_p = false;
2855 /* We are not processing an `extern "C"' declaration. */
2856 parser->in_unbraced_linkage_specification_p = false;
2858 /* We are not processing a declarator. */
2859 parser->in_declarator_p = false;
2861 /* We are not processing a template-argument-list. */
2862 parser->in_template_argument_list_p = false;
2864 /* We are not in an iteration statement. */
2865 parser->in_statement = 0;
2867 /* We are not in a switch statement. */
2868 parser->in_switch_statement_p = false;
2870 /* We are not parsing a type-id inside an expression. */
2871 parser->in_type_id_in_expr_p = false;
2873 /* Declarations aren't implicitly extern "C". */
2874 parser->implicit_extern_c = false;
2876 /* String literals should be translated to the execution character set. */
2877 parser->translate_strings_p = true;
2879 /* We are not parsing a function body. */
2880 parser->in_function_body = false;
2882 /* The unparsed function queue is empty. */
2883 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2885 /* There are no classes being defined. */
2886 parser->num_classes_being_defined = 0;
2888 /* No template parameters apply. */
2889 parser->num_template_parameter_lists = 0;
2891 return parser;
2894 /* Create a cp_lexer structure which will emit the tokens in CACHE
2895 and push it onto the parser's lexer stack. This is used for delayed
2896 parsing of in-class method bodies and default arguments, and should
2897 not be confused with tentative parsing. */
2898 static void
2899 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2901 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2902 lexer->next = parser->lexer;
2903 parser->lexer = lexer;
2905 /* Move the current source position to that of the first token in the
2906 new lexer. */
2907 cp_lexer_set_source_position_from_token (lexer->next_token);
2910 /* Pop the top lexer off the parser stack. This is never used for the
2911 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2912 static void
2913 cp_parser_pop_lexer (cp_parser *parser)
2915 cp_lexer *lexer = parser->lexer;
2916 parser->lexer = lexer->next;
2917 cp_lexer_destroy (lexer);
2919 /* Put the current source position back where it was before this
2920 lexer was pushed. */
2921 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2924 /* Lexical conventions [gram.lex] */
2926 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2927 identifier. */
2929 static tree
2930 cp_parser_identifier (cp_parser* parser)
2932 cp_token *token;
2934 /* Look for the identifier. */
2935 token = cp_parser_require (parser, CPP_NAME, "identifier");
2936 /* Return the value. */
2937 return token ? token->u.value : error_mark_node;
2940 /* Parse a sequence of adjacent string constants. Returns a
2941 TREE_STRING representing the combined, nul-terminated string
2942 constant. If TRANSLATE is true, translate the string to the
2943 execution character set. If WIDE_OK is true, a wide string is
2944 invalid here.
2946 C++98 [lex.string] says that if a narrow string literal token is
2947 adjacent to a wide string literal token, the behavior is undefined.
2948 However, C99 6.4.5p4 says that this results in a wide string literal.
2949 We follow C99 here, for consistency with the C front end.
2951 This code is largely lifted from lex_string() in c-lex.c.
2953 FUTURE: ObjC++ will need to handle @-strings here. */
2954 static tree
2955 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2957 tree value;
2958 size_t count;
2959 struct obstack str_ob;
2960 cpp_string str, istr, *strs;
2961 cp_token *tok;
2962 enum cpp_ttype type;
2964 tok = cp_lexer_peek_token (parser->lexer);
2965 if (!cp_parser_is_string_literal (tok))
2967 cp_parser_error (parser, "expected string-literal");
2968 return error_mark_node;
2971 type = tok->type;
2973 /* Try to avoid the overhead of creating and destroying an obstack
2974 for the common case of just one string. */
2975 if (!cp_parser_is_string_literal
2976 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2978 cp_lexer_consume_token (parser->lexer);
2980 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2981 str.len = TREE_STRING_LENGTH (tok->u.value);
2982 count = 1;
2984 strs = &str;
2986 else
2988 gcc_obstack_init (&str_ob);
2989 count = 0;
2993 cp_lexer_consume_token (parser->lexer);
2994 count++;
2995 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2996 str.len = TREE_STRING_LENGTH (tok->u.value);
2998 if (type != tok->type)
3000 if (type == CPP_STRING)
3001 type = tok->type;
3002 else if (tok->type != CPP_STRING)
3003 error_at (tok->location,
3004 "unsupported non-standard concatenation "
3005 "of string literals");
3008 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3010 tok = cp_lexer_peek_token (parser->lexer);
3012 while (cp_parser_is_string_literal (tok));
3014 strs = (cpp_string *) obstack_finish (&str_ob);
3017 if (type != CPP_STRING && !wide_ok)
3019 cp_parser_error (parser, "a wide string is invalid in this context");
3020 type = CPP_STRING;
3023 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3024 (parse_in, strs, count, &istr, type))
3026 value = build_string (istr.len, (const char *)istr.text);
3027 free (CONST_CAST (unsigned char *, istr.text));
3029 switch (type)
3031 default:
3032 case CPP_STRING:
3033 case CPP_UTF8STRING:
3034 TREE_TYPE (value) = char_array_type_node;
3035 break;
3036 case CPP_STRING16:
3037 TREE_TYPE (value) = char16_array_type_node;
3038 break;
3039 case CPP_STRING32:
3040 TREE_TYPE (value) = char32_array_type_node;
3041 break;
3042 case CPP_WSTRING:
3043 TREE_TYPE (value) = wchar_array_type_node;
3044 break;
3047 value = fix_string_type (value);
3049 else
3050 /* cpp_interpret_string has issued an error. */
3051 value = error_mark_node;
3053 if (count > 1)
3054 obstack_free (&str_ob, 0);
3056 return value;
3060 /* Basic concepts [gram.basic] */
3062 /* Parse a translation-unit.
3064 translation-unit:
3065 declaration-seq [opt]
3067 Returns TRUE if all went well. */
3069 static bool
3070 cp_parser_translation_unit (cp_parser* parser)
3072 /* The address of the first non-permanent object on the declarator
3073 obstack. */
3074 static void *declarator_obstack_base;
3076 bool success;
3078 /* Create the declarator obstack, if necessary. */
3079 if (!cp_error_declarator)
3081 gcc_obstack_init (&declarator_obstack);
3082 /* Create the error declarator. */
3083 cp_error_declarator = make_declarator (cdk_error);
3084 /* Create the empty parameter list. */
3085 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3086 /* Remember where the base of the declarator obstack lies. */
3087 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3090 cp_parser_declaration_seq_opt (parser);
3092 /* If there are no tokens left then all went well. */
3093 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3095 /* Get rid of the token array; we don't need it any more. */
3096 cp_lexer_destroy (parser->lexer);
3097 parser->lexer = NULL;
3099 /* This file might have been a context that's implicitly extern
3100 "C". If so, pop the lang context. (Only relevant for PCH.) */
3101 if (parser->implicit_extern_c)
3103 pop_lang_context ();
3104 parser->implicit_extern_c = false;
3107 /* Finish up. */
3108 finish_translation_unit ();
3110 success = true;
3112 else
3114 cp_parser_error (parser, "expected declaration");
3115 success = false;
3118 /* Make sure the declarator obstack was fully cleaned up. */
3119 gcc_assert (obstack_next_free (&declarator_obstack)
3120 == declarator_obstack_base);
3122 /* All went well. */
3123 return success;
3126 /* Expressions [gram.expr] */
3128 /* Parse a primary-expression.
3130 primary-expression:
3131 literal
3132 this
3133 ( expression )
3134 id-expression
3136 GNU Extensions:
3138 primary-expression:
3139 ( compound-statement )
3140 __builtin_va_arg ( assignment-expression , type-id )
3141 __builtin_offsetof ( type-id , offsetof-expression )
3143 C++ Extensions:
3144 __has_nothrow_assign ( type-id )
3145 __has_nothrow_constructor ( type-id )
3146 __has_nothrow_copy ( type-id )
3147 __has_trivial_assign ( type-id )
3148 __has_trivial_constructor ( type-id )
3149 __has_trivial_copy ( type-id )
3150 __has_trivial_destructor ( type-id )
3151 __has_virtual_destructor ( type-id )
3152 __is_abstract ( type-id )
3153 __is_base_of ( type-id , type-id )
3154 __is_class ( type-id )
3155 __is_convertible_to ( type-id , type-id )
3156 __is_empty ( type-id )
3157 __is_enum ( type-id )
3158 __is_pod ( type-id )
3159 __is_polymorphic ( type-id )
3160 __is_union ( type-id )
3162 Objective-C++ Extension:
3164 primary-expression:
3165 objc-expression
3167 literal:
3168 __null
3170 ADDRESS_P is true iff this expression was immediately preceded by
3171 "&" and therefore might denote a pointer-to-member. CAST_P is true
3172 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3173 true iff this expression is a template argument.
3175 Returns a representation of the expression. Upon return, *IDK
3176 indicates what kind of id-expression (if any) was present. */
3178 static tree
3179 cp_parser_primary_expression (cp_parser *parser,
3180 bool address_p,
3181 bool cast_p,
3182 bool template_arg_p,
3183 cp_id_kind *idk)
3185 cp_token *token = NULL;
3187 /* Assume the primary expression is not an id-expression. */
3188 *idk = CP_ID_KIND_NONE;
3190 /* Peek at the next token. */
3191 token = cp_lexer_peek_token (parser->lexer);
3192 switch (token->type)
3194 /* literal:
3195 integer-literal
3196 character-literal
3197 floating-literal
3198 string-literal
3199 boolean-literal */
3200 case CPP_CHAR:
3201 case CPP_CHAR16:
3202 case CPP_CHAR32:
3203 case CPP_WCHAR:
3204 case CPP_NUMBER:
3205 token = cp_lexer_consume_token (parser->lexer);
3206 if (TREE_CODE (token->u.value) == FIXED_CST)
3208 error_at (token->location,
3209 "fixed-point types not supported in C++");
3210 return error_mark_node;
3212 /* Floating-point literals are only allowed in an integral
3213 constant expression if they are cast to an integral or
3214 enumeration type. */
3215 if (TREE_CODE (token->u.value) == REAL_CST
3216 && parser->integral_constant_expression_p
3217 && pedantic)
3219 /* CAST_P will be set even in invalid code like "int(2.7 +
3220 ...)". Therefore, we have to check that the next token
3221 is sure to end the cast. */
3222 if (cast_p)
3224 cp_token *next_token;
3226 next_token = cp_lexer_peek_token (parser->lexer);
3227 if (/* The comma at the end of an
3228 enumerator-definition. */
3229 next_token->type != CPP_COMMA
3230 /* The curly brace at the end of an enum-specifier. */
3231 && next_token->type != CPP_CLOSE_BRACE
3232 /* The end of a statement. */
3233 && next_token->type != CPP_SEMICOLON
3234 /* The end of the cast-expression. */
3235 && next_token->type != CPP_CLOSE_PAREN
3236 /* The end of an array bound. */
3237 && next_token->type != CPP_CLOSE_SQUARE
3238 /* The closing ">" in a template-argument-list. */
3239 && (next_token->type != CPP_GREATER
3240 || parser->greater_than_is_operator_p)
3241 /* C++0x only: A ">>" treated like two ">" tokens,
3242 in a template-argument-list. */
3243 && (next_token->type != CPP_RSHIFT
3244 || (cxx_dialect == cxx98)
3245 || parser->greater_than_is_operator_p))
3246 cast_p = false;
3249 /* If we are within a cast, then the constraint that the
3250 cast is to an integral or enumeration type will be
3251 checked at that point. If we are not within a cast, then
3252 this code is invalid. */
3253 if (!cast_p)
3254 cp_parser_non_integral_constant_expression
3255 (parser, "floating-point literal");
3257 return token->u.value;
3259 case CPP_STRING:
3260 case CPP_STRING16:
3261 case CPP_STRING32:
3262 case CPP_WSTRING:
3263 case CPP_UTF8STRING:
3264 /* ??? Should wide strings be allowed when parser->translate_strings_p
3265 is false (i.e. in attributes)? If not, we can kill the third
3266 argument to cp_parser_string_literal. */
3267 return cp_parser_string_literal (parser,
3268 parser->translate_strings_p,
3269 true);
3271 case CPP_OPEN_PAREN:
3273 tree expr;
3274 bool saved_greater_than_is_operator_p;
3276 /* Consume the `('. */
3277 cp_lexer_consume_token (parser->lexer);
3278 /* Within a parenthesized expression, a `>' token is always
3279 the greater-than operator. */
3280 saved_greater_than_is_operator_p
3281 = parser->greater_than_is_operator_p;
3282 parser->greater_than_is_operator_p = true;
3283 /* If we see `( { ' then we are looking at the beginning of
3284 a GNU statement-expression. */
3285 if (cp_parser_allow_gnu_extensions_p (parser)
3286 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3288 /* Statement-expressions are not allowed by the standard. */
3289 pedwarn (token->location, OPT_pedantic,
3290 "ISO C++ forbids braced-groups within expressions");
3292 /* And they're not allowed outside of a function-body; you
3293 cannot, for example, write:
3295 int i = ({ int j = 3; j + 1; });
3297 at class or namespace scope. */
3298 if (!parser->in_function_body
3299 || parser->in_template_argument_list_p)
3301 error_at (token->location,
3302 "statement-expressions are not allowed outside "
3303 "functions nor in template-argument lists");
3304 cp_parser_skip_to_end_of_block_or_statement (parser);
3305 expr = error_mark_node;
3307 else
3309 /* Start the statement-expression. */
3310 expr = begin_stmt_expr ();
3311 /* Parse the compound-statement. */
3312 cp_parser_compound_statement (parser, expr, false);
3313 /* Finish up. */
3314 expr = finish_stmt_expr (expr, false);
3317 else
3319 /* Parse the parenthesized expression. */
3320 expr = cp_parser_expression (parser, cast_p, idk);
3321 /* Let the front end know that this expression was
3322 enclosed in parentheses. This matters in case, for
3323 example, the expression is of the form `A::B', since
3324 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3325 not. */
3326 finish_parenthesized_expr (expr);
3328 /* The `>' token might be the end of a template-id or
3329 template-parameter-list now. */
3330 parser->greater_than_is_operator_p
3331 = saved_greater_than_is_operator_p;
3332 /* Consume the `)'. */
3333 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3334 cp_parser_skip_to_end_of_statement (parser);
3336 return expr;
3339 case CPP_OPEN_SQUARE:
3340 if (c_dialect_objc ())
3341 /* We have an Objective-C++ message. */
3342 return cp_parser_objc_expression (parser);
3343 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
3344 return cp_parser_lambda_expression (parser);
3346 case CPP_OBJC_STRING:
3347 if (c_dialect_objc ())
3348 /* We have an Objective-C++ string literal. */
3349 return cp_parser_objc_expression (parser);
3350 cp_parser_error (parser, "expected primary-expression");
3351 return error_mark_node;
3353 case CPP_KEYWORD:
3354 switch (token->keyword)
3356 /* These two are the boolean literals. */
3357 case RID_TRUE:
3358 cp_lexer_consume_token (parser->lexer);
3359 return boolean_true_node;
3360 case RID_FALSE:
3361 cp_lexer_consume_token (parser->lexer);
3362 return boolean_false_node;
3364 /* The `__null' literal. */
3365 case RID_NULL:
3366 cp_lexer_consume_token (parser->lexer);
3367 return null_node;
3369 /* Recognize the `this' keyword. */
3370 case RID_THIS:
3371 cp_lexer_consume_token (parser->lexer);
3372 if (parser->local_variables_forbidden_p)
3374 error_at (token->location,
3375 "%<this%> may not be used in this context");
3376 return error_mark_node;
3378 /* Pointers cannot appear in constant-expressions. */
3379 if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3380 return error_mark_node;
3381 return finish_this_expr ();
3383 /* The `operator' keyword can be the beginning of an
3384 id-expression. */
3385 case RID_OPERATOR:
3386 goto id_expression;
3388 case RID_FUNCTION_NAME:
3389 case RID_PRETTY_FUNCTION_NAME:
3390 case RID_C99_FUNCTION_NAME:
3392 const char *name;
3394 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3395 __func__ are the names of variables -- but they are
3396 treated specially. Therefore, they are handled here,
3397 rather than relying on the generic id-expression logic
3398 below. Grammatically, these names are id-expressions.
3400 Consume the token. */
3401 token = cp_lexer_consume_token (parser->lexer);
3403 switch (token->keyword)
3405 case RID_FUNCTION_NAME:
3406 name = "%<__FUNCTION__%>";
3407 break;
3408 case RID_PRETTY_FUNCTION_NAME:
3409 name = "%<__PRETTY_FUNCTION__%>";
3410 break;
3411 case RID_C99_FUNCTION_NAME:
3412 name = "%<__func__%>";
3413 break;
3414 default:
3415 gcc_unreachable ();
3418 if (cp_parser_non_integral_constant_expression (parser, name))
3419 return error_mark_node;
3421 /* Look up the name. */
3422 return finish_fname (token->u.value);
3425 case RID_VA_ARG:
3427 tree expression;
3428 tree type;
3430 /* The `__builtin_va_arg' construct is used to handle
3431 `va_arg'. Consume the `__builtin_va_arg' token. */
3432 cp_lexer_consume_token (parser->lexer);
3433 /* Look for the opening `('. */
3434 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3435 /* Now, parse the assignment-expression. */
3436 expression = cp_parser_assignment_expression (parser,
3437 /*cast_p=*/false, NULL);
3438 /* Look for the `,'. */
3439 cp_parser_require (parser, CPP_COMMA, "%<,%>");
3440 /* Parse the type-id. */
3441 type = cp_parser_type_id (parser);
3442 /* Look for the closing `)'. */
3443 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3444 /* Using `va_arg' in a constant-expression is not
3445 allowed. */
3446 if (cp_parser_non_integral_constant_expression (parser,
3447 "%<va_arg%>"))
3448 return error_mark_node;
3449 return build_x_va_arg (expression, type);
3452 case RID_OFFSETOF:
3453 return cp_parser_builtin_offsetof (parser);
3455 case RID_HAS_NOTHROW_ASSIGN:
3456 case RID_HAS_NOTHROW_CONSTRUCTOR:
3457 case RID_HAS_NOTHROW_COPY:
3458 case RID_HAS_TRIVIAL_ASSIGN:
3459 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3460 case RID_HAS_TRIVIAL_COPY:
3461 case RID_HAS_TRIVIAL_DESTRUCTOR:
3462 case RID_HAS_VIRTUAL_DESTRUCTOR:
3463 case RID_IS_ABSTRACT:
3464 case RID_IS_BASE_OF:
3465 case RID_IS_CLASS:
3466 case RID_IS_CONVERTIBLE_TO:
3467 case RID_IS_EMPTY:
3468 case RID_IS_ENUM:
3469 case RID_IS_POD:
3470 case RID_IS_POLYMORPHIC:
3471 case RID_IS_STD_LAYOUT:
3472 case RID_IS_TRIVIAL:
3473 case RID_IS_UNION:
3474 return cp_parser_trait_expr (parser, token->keyword);
3476 /* Objective-C++ expressions. */
3477 case RID_AT_ENCODE:
3478 case RID_AT_PROTOCOL:
3479 case RID_AT_SELECTOR:
3480 return cp_parser_objc_expression (parser);
3482 default:
3483 cp_parser_error (parser, "expected primary-expression");
3484 return error_mark_node;
3487 /* An id-expression can start with either an identifier, a
3488 `::' as the beginning of a qualified-id, or the "operator"
3489 keyword. */
3490 case CPP_NAME:
3491 case CPP_SCOPE:
3492 case CPP_TEMPLATE_ID:
3493 case CPP_NESTED_NAME_SPECIFIER:
3495 tree id_expression;
3496 tree decl;
3497 const char *error_msg;
3498 bool template_p;
3499 bool done;
3500 cp_token *id_expr_token;
3502 id_expression:
3503 /* Parse the id-expression. */
3504 id_expression
3505 = cp_parser_id_expression (parser,
3506 /*template_keyword_p=*/false,
3507 /*check_dependency_p=*/true,
3508 &template_p,
3509 /*declarator_p=*/false,
3510 /*optional_p=*/false);
3511 if (id_expression == error_mark_node)
3512 return error_mark_node;
3513 id_expr_token = token;
3514 token = cp_lexer_peek_token (parser->lexer);
3515 done = (token->type != CPP_OPEN_SQUARE
3516 && token->type != CPP_OPEN_PAREN
3517 && token->type != CPP_DOT
3518 && token->type != CPP_DEREF
3519 && token->type != CPP_PLUS_PLUS
3520 && token->type != CPP_MINUS_MINUS);
3521 /* If we have a template-id, then no further lookup is
3522 required. If the template-id was for a template-class, we
3523 will sometimes have a TYPE_DECL at this point. */
3524 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3525 || TREE_CODE (id_expression) == TYPE_DECL)
3526 decl = id_expression;
3527 /* Look up the name. */
3528 else
3530 tree ambiguous_decls;
3532 /* If we already know that this lookup is ambiguous, then
3533 we've already issued an error message; there's no reason
3534 to check again. */
3535 if (id_expr_token->type == CPP_NAME
3536 && id_expr_token->ambiguous_p)
3538 cp_parser_simulate_error (parser);
3539 return error_mark_node;
3542 decl = cp_parser_lookup_name (parser, id_expression,
3543 none_type,
3544 template_p,
3545 /*is_namespace=*/false,
3546 /*check_dependency=*/true,
3547 &ambiguous_decls,
3548 id_expr_token->location);
3549 /* If the lookup was ambiguous, an error will already have
3550 been issued. */
3551 if (ambiguous_decls)
3552 return error_mark_node;
3554 /* In Objective-C++, an instance variable (ivar) may be preferred
3555 to whatever cp_parser_lookup_name() found. */
3556 decl = objc_lookup_ivar (decl, id_expression);
3558 /* If name lookup gives us a SCOPE_REF, then the
3559 qualifying scope was dependent. */
3560 if (TREE_CODE (decl) == SCOPE_REF)
3562 /* At this point, we do not know if DECL is a valid
3563 integral constant expression. We assume that it is
3564 in fact such an expression, so that code like:
3566 template <int N> struct A {
3567 int a[B<N>::i];
3570 is accepted. At template-instantiation time, we
3571 will check that B<N>::i is actually a constant. */
3572 return decl;
3574 /* Check to see if DECL is a local variable in a context
3575 where that is forbidden. */
3576 if (parser->local_variables_forbidden_p
3577 && local_variable_p (decl))
3579 /* It might be that we only found DECL because we are
3580 trying to be generous with pre-ISO scoping rules.
3581 For example, consider:
3583 int i;
3584 void g() {
3585 for (int i = 0; i < 10; ++i) {}
3586 extern void f(int j = i);
3589 Here, name look up will originally find the out
3590 of scope `i'. We need to issue a warning message,
3591 but then use the global `i'. */
3592 decl = check_for_out_of_scope_variable (decl);
3593 if (local_variable_p (decl))
3595 error_at (id_expr_token->location,
3596 "local variable %qD may not appear in this context",
3597 decl);
3598 return error_mark_node;
3603 decl = (finish_id_expression
3604 (id_expression, decl, parser->scope,
3605 idk,
3606 parser->integral_constant_expression_p,
3607 parser->allow_non_integral_constant_expression_p,
3608 &parser->non_integral_constant_expression_p,
3609 template_p, done, address_p,
3610 template_arg_p,
3611 &error_msg,
3612 id_expr_token->location));
3613 if (error_msg)
3614 cp_parser_error (parser, error_msg);
3615 return decl;
3618 /* Anything else is an error. */
3619 default:
3620 cp_parser_error (parser, "expected primary-expression");
3621 return error_mark_node;
3625 /* Parse an id-expression.
3627 id-expression:
3628 unqualified-id
3629 qualified-id
3631 qualified-id:
3632 :: [opt] nested-name-specifier template [opt] unqualified-id
3633 :: identifier
3634 :: operator-function-id
3635 :: template-id
3637 Return a representation of the unqualified portion of the
3638 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3639 a `::' or nested-name-specifier.
3641 Often, if the id-expression was a qualified-id, the caller will
3642 want to make a SCOPE_REF to represent the qualified-id. This
3643 function does not do this in order to avoid wastefully creating
3644 SCOPE_REFs when they are not required.
3646 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3647 `template' keyword.
3649 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3650 uninstantiated templates.
3652 If *TEMPLATE_P is non-NULL, it is set to true iff the
3653 `template' keyword is used to explicitly indicate that the entity
3654 named is a template.
3656 If DECLARATOR_P is true, the id-expression is appearing as part of
3657 a declarator, rather than as part of an expression. */
3659 static tree
3660 cp_parser_id_expression (cp_parser *parser,
3661 bool template_keyword_p,
3662 bool check_dependency_p,
3663 bool *template_p,
3664 bool declarator_p,
3665 bool optional_p)
3667 bool global_scope_p;
3668 bool nested_name_specifier_p;
3670 /* Assume the `template' keyword was not used. */
3671 if (template_p)
3672 *template_p = template_keyword_p;
3674 /* Look for the optional `::' operator. */
3675 global_scope_p
3676 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3677 != NULL_TREE);
3678 /* Look for the optional nested-name-specifier. */
3679 nested_name_specifier_p
3680 = (cp_parser_nested_name_specifier_opt (parser,
3681 /*typename_keyword_p=*/false,
3682 check_dependency_p,
3683 /*type_p=*/false,
3684 declarator_p)
3685 != NULL_TREE);
3686 /* If there is a nested-name-specifier, then we are looking at
3687 the first qualified-id production. */
3688 if (nested_name_specifier_p)
3690 tree saved_scope;
3691 tree saved_object_scope;
3692 tree saved_qualifying_scope;
3693 tree unqualified_id;
3694 bool is_template;
3696 /* See if the next token is the `template' keyword. */
3697 if (!template_p)
3698 template_p = &is_template;
3699 *template_p = cp_parser_optional_template_keyword (parser);
3700 /* Name lookup we do during the processing of the
3701 unqualified-id might obliterate SCOPE. */
3702 saved_scope = parser->scope;
3703 saved_object_scope = parser->object_scope;
3704 saved_qualifying_scope = parser->qualifying_scope;
3705 /* Process the final unqualified-id. */
3706 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3707 check_dependency_p,
3708 declarator_p,
3709 /*optional_p=*/false);
3710 /* Restore the SAVED_SCOPE for our caller. */
3711 parser->scope = saved_scope;
3712 parser->object_scope = saved_object_scope;
3713 parser->qualifying_scope = saved_qualifying_scope;
3715 return unqualified_id;
3717 /* Otherwise, if we are in global scope, then we are looking at one
3718 of the other qualified-id productions. */
3719 else if (global_scope_p)
3721 cp_token *token;
3722 tree id;
3724 /* Peek at the next token. */
3725 token = cp_lexer_peek_token (parser->lexer);
3727 /* If it's an identifier, and the next token is not a "<", then
3728 we can avoid the template-id case. This is an optimization
3729 for this common case. */
3730 if (token->type == CPP_NAME
3731 && !cp_parser_nth_token_starts_template_argument_list_p
3732 (parser, 2))
3733 return cp_parser_identifier (parser);
3735 cp_parser_parse_tentatively (parser);
3736 /* Try a template-id. */
3737 id = cp_parser_template_id (parser,
3738 /*template_keyword_p=*/false,
3739 /*check_dependency_p=*/true,
3740 declarator_p);
3741 /* If that worked, we're done. */
3742 if (cp_parser_parse_definitely (parser))
3743 return id;
3745 /* Peek at the next token. (Changes in the token buffer may
3746 have invalidated the pointer obtained above.) */
3747 token = cp_lexer_peek_token (parser->lexer);
3749 switch (token->type)
3751 case CPP_NAME:
3752 return cp_parser_identifier (parser);
3754 case CPP_KEYWORD:
3755 if (token->keyword == RID_OPERATOR)
3756 return cp_parser_operator_function_id (parser);
3757 /* Fall through. */
3759 default:
3760 cp_parser_error (parser, "expected id-expression");
3761 return error_mark_node;
3764 else
3765 return cp_parser_unqualified_id (parser, template_keyword_p,
3766 /*check_dependency_p=*/true,
3767 declarator_p,
3768 optional_p);
3771 /* Parse an unqualified-id.
3773 unqualified-id:
3774 identifier
3775 operator-function-id
3776 conversion-function-id
3777 ~ class-name
3778 template-id
3780 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3781 keyword, in a construct like `A::template ...'.
3783 Returns a representation of unqualified-id. For the `identifier'
3784 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3785 production a BIT_NOT_EXPR is returned; the operand of the
3786 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3787 other productions, see the documentation accompanying the
3788 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3789 names are looked up in uninstantiated templates. If DECLARATOR_P
3790 is true, the unqualified-id is appearing as part of a declarator,
3791 rather than as part of an expression. */
3793 static tree
3794 cp_parser_unqualified_id (cp_parser* parser,
3795 bool template_keyword_p,
3796 bool check_dependency_p,
3797 bool declarator_p,
3798 bool optional_p)
3800 cp_token *token;
3802 /* Peek at the next token. */
3803 token = cp_lexer_peek_token (parser->lexer);
3805 switch (token->type)
3807 case CPP_NAME:
3809 tree id;
3811 /* We don't know yet whether or not this will be a
3812 template-id. */
3813 cp_parser_parse_tentatively (parser);
3814 /* Try a template-id. */
3815 id = cp_parser_template_id (parser, template_keyword_p,
3816 check_dependency_p,
3817 declarator_p);
3818 /* If it worked, we're done. */
3819 if (cp_parser_parse_definitely (parser))
3820 return id;
3821 /* Otherwise, it's an ordinary identifier. */
3822 return cp_parser_identifier (parser);
3825 case CPP_TEMPLATE_ID:
3826 return cp_parser_template_id (parser, template_keyword_p,
3827 check_dependency_p,
3828 declarator_p);
3830 case CPP_COMPL:
3832 tree type_decl;
3833 tree qualifying_scope;
3834 tree object_scope;
3835 tree scope;
3836 bool done;
3838 /* Consume the `~' token. */
3839 cp_lexer_consume_token (parser->lexer);
3840 /* Parse the class-name. The standard, as written, seems to
3841 say that:
3843 template <typename T> struct S { ~S (); };
3844 template <typename T> S<T>::~S() {}
3846 is invalid, since `~' must be followed by a class-name, but
3847 `S<T>' is dependent, and so not known to be a class.
3848 That's not right; we need to look in uninstantiated
3849 templates. A further complication arises from:
3851 template <typename T> void f(T t) {
3852 t.T::~T();
3855 Here, it is not possible to look up `T' in the scope of `T'
3856 itself. We must look in both the current scope, and the
3857 scope of the containing complete expression.
3859 Yet another issue is:
3861 struct S {
3862 int S;
3863 ~S();
3866 S::~S() {}
3868 The standard does not seem to say that the `S' in `~S'
3869 should refer to the type `S' and not the data member
3870 `S::S'. */
3872 /* DR 244 says that we look up the name after the "~" in the
3873 same scope as we looked up the qualifying name. That idea
3874 isn't fully worked out; it's more complicated than that. */
3875 scope = parser->scope;
3876 object_scope = parser->object_scope;
3877 qualifying_scope = parser->qualifying_scope;
3879 /* Check for invalid scopes. */
3880 if (scope == error_mark_node)
3882 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3883 cp_lexer_consume_token (parser->lexer);
3884 return error_mark_node;
3886 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3888 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3889 error_at (token->location,
3890 "scope %qT before %<~%> is not a class-name",
3891 scope);
3892 cp_parser_simulate_error (parser);
3893 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3894 cp_lexer_consume_token (parser->lexer);
3895 return error_mark_node;
3897 gcc_assert (!scope || TYPE_P (scope));
3899 /* If the name is of the form "X::~X" it's OK. */
3900 token = cp_lexer_peek_token (parser->lexer);
3901 if (scope
3902 && token->type == CPP_NAME
3903 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3904 != CPP_LESS)
3905 && constructor_name_p (token->u.value, scope))
3907 cp_lexer_consume_token (parser->lexer);
3908 return build_nt (BIT_NOT_EXPR, scope);
3911 /* If there was an explicit qualification (S::~T), first look
3912 in the scope given by the qualification (i.e., S).
3914 Note: in the calls to cp_parser_class_name below we pass
3915 typename_type so that lookup finds the injected-class-name
3916 rather than the constructor. */
3917 done = false;
3918 type_decl = NULL_TREE;
3919 if (scope)
3921 cp_parser_parse_tentatively (parser);
3922 type_decl = cp_parser_class_name (parser,
3923 /*typename_keyword_p=*/false,
3924 /*template_keyword_p=*/false,
3925 typename_type,
3926 /*check_dependency=*/false,
3927 /*class_head_p=*/false,
3928 declarator_p);
3929 if (cp_parser_parse_definitely (parser))
3930 done = true;
3932 /* In "N::S::~S", look in "N" as well. */
3933 if (!done && scope && qualifying_scope)
3935 cp_parser_parse_tentatively (parser);
3936 parser->scope = qualifying_scope;
3937 parser->object_scope = NULL_TREE;
3938 parser->qualifying_scope = NULL_TREE;
3939 type_decl
3940 = cp_parser_class_name (parser,
3941 /*typename_keyword_p=*/false,
3942 /*template_keyword_p=*/false,
3943 typename_type,
3944 /*check_dependency=*/false,
3945 /*class_head_p=*/false,
3946 declarator_p);
3947 if (cp_parser_parse_definitely (parser))
3948 done = true;
3950 /* In "p->S::~T", look in the scope given by "*p" as well. */
3951 else if (!done && object_scope)
3953 cp_parser_parse_tentatively (parser);
3954 parser->scope = object_scope;
3955 parser->object_scope = NULL_TREE;
3956 parser->qualifying_scope = NULL_TREE;
3957 type_decl
3958 = cp_parser_class_name (parser,
3959 /*typename_keyword_p=*/false,
3960 /*template_keyword_p=*/false,
3961 typename_type,
3962 /*check_dependency=*/false,
3963 /*class_head_p=*/false,
3964 declarator_p);
3965 if (cp_parser_parse_definitely (parser))
3966 done = true;
3968 /* Look in the surrounding context. */
3969 if (!done)
3971 parser->scope = NULL_TREE;
3972 parser->object_scope = NULL_TREE;
3973 parser->qualifying_scope = NULL_TREE;
3974 if (processing_template_decl)
3975 cp_parser_parse_tentatively (parser);
3976 type_decl
3977 = cp_parser_class_name (parser,
3978 /*typename_keyword_p=*/false,
3979 /*template_keyword_p=*/false,
3980 typename_type,
3981 /*check_dependency=*/false,
3982 /*class_head_p=*/false,
3983 declarator_p);
3984 if (processing_template_decl
3985 && ! cp_parser_parse_definitely (parser))
3987 /* We couldn't find a type with this name, so just accept
3988 it and check for a match at instantiation time. */
3989 type_decl = cp_parser_identifier (parser);
3990 if (type_decl != error_mark_node)
3991 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
3992 return type_decl;
3995 /* If an error occurred, assume that the name of the
3996 destructor is the same as the name of the qualifying
3997 class. That allows us to keep parsing after running
3998 into ill-formed destructor names. */
3999 if (type_decl == error_mark_node && scope)
4000 return build_nt (BIT_NOT_EXPR, scope);
4001 else if (type_decl == error_mark_node)
4002 return error_mark_node;
4004 /* Check that destructor name and scope match. */
4005 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4007 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4008 error_at (token->location,
4009 "declaration of %<~%T%> as member of %qT",
4010 type_decl, scope);
4011 cp_parser_simulate_error (parser);
4012 return error_mark_node;
4015 /* [class.dtor]
4017 A typedef-name that names a class shall not be used as the
4018 identifier in the declarator for a destructor declaration. */
4019 if (declarator_p
4020 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4021 && !DECL_SELF_REFERENCE_P (type_decl)
4022 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4023 error_at (token->location,
4024 "typedef-name %qD used as destructor declarator",
4025 type_decl);
4027 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4030 case CPP_KEYWORD:
4031 if (token->keyword == RID_OPERATOR)
4033 tree id;
4035 /* This could be a template-id, so we try that first. */
4036 cp_parser_parse_tentatively (parser);
4037 /* Try a template-id. */
4038 id = cp_parser_template_id (parser, template_keyword_p,
4039 /*check_dependency_p=*/true,
4040 declarator_p);
4041 /* If that worked, we're done. */
4042 if (cp_parser_parse_definitely (parser))
4043 return id;
4044 /* We still don't know whether we're looking at an
4045 operator-function-id or a conversion-function-id. */
4046 cp_parser_parse_tentatively (parser);
4047 /* Try an operator-function-id. */
4048 id = cp_parser_operator_function_id (parser);
4049 /* If that didn't work, try a conversion-function-id. */
4050 if (!cp_parser_parse_definitely (parser))
4051 id = cp_parser_conversion_function_id (parser);
4053 return id;
4055 /* Fall through. */
4057 default:
4058 if (optional_p)
4059 return NULL_TREE;
4060 cp_parser_error (parser, "expected unqualified-id");
4061 return error_mark_node;
4065 /* Parse an (optional) nested-name-specifier.
4067 nested-name-specifier: [C++98]
4068 class-or-namespace-name :: nested-name-specifier [opt]
4069 class-or-namespace-name :: template nested-name-specifier [opt]
4071 nested-name-specifier: [C++0x]
4072 type-name ::
4073 namespace-name ::
4074 nested-name-specifier identifier ::
4075 nested-name-specifier template [opt] simple-template-id ::
4077 PARSER->SCOPE should be set appropriately before this function is
4078 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4079 effect. TYPE_P is TRUE if we non-type bindings should be ignored
4080 in name lookups.
4082 Sets PARSER->SCOPE to the class (TYPE) or namespace
4083 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4084 it unchanged if there is no nested-name-specifier. Returns the new
4085 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4087 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4088 part of a declaration and/or decl-specifier. */
4090 static tree
4091 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4092 bool typename_keyword_p,
4093 bool check_dependency_p,
4094 bool type_p,
4095 bool is_declaration)
4097 bool success = false;
4098 cp_token_position start = 0;
4099 cp_token *token;
4101 /* Remember where the nested-name-specifier starts. */
4102 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4104 start = cp_lexer_token_position (parser->lexer, false);
4105 push_deferring_access_checks (dk_deferred);
4108 while (true)
4110 tree new_scope;
4111 tree old_scope;
4112 tree saved_qualifying_scope;
4113 bool template_keyword_p;
4115 /* Spot cases that cannot be the beginning of a
4116 nested-name-specifier. */
4117 token = cp_lexer_peek_token (parser->lexer);
4119 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4120 the already parsed nested-name-specifier. */
4121 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4123 /* Grab the nested-name-specifier and continue the loop. */
4124 cp_parser_pre_parsed_nested_name_specifier (parser);
4125 /* If we originally encountered this nested-name-specifier
4126 with IS_DECLARATION set to false, we will not have
4127 resolved TYPENAME_TYPEs, so we must do so here. */
4128 if (is_declaration
4129 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4131 new_scope = resolve_typename_type (parser->scope,
4132 /*only_current_p=*/false);
4133 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4134 parser->scope = new_scope;
4136 success = true;
4137 continue;
4140 /* Spot cases that cannot be the beginning of a
4141 nested-name-specifier. On the second and subsequent times
4142 through the loop, we look for the `template' keyword. */
4143 if (success && token->keyword == RID_TEMPLATE)
4145 /* A template-id can start a nested-name-specifier. */
4146 else if (token->type == CPP_TEMPLATE_ID)
4148 else
4150 /* If the next token is not an identifier, then it is
4151 definitely not a type-name or namespace-name. */
4152 if (token->type != CPP_NAME)
4153 break;
4154 /* If the following token is neither a `<' (to begin a
4155 template-id), nor a `::', then we are not looking at a
4156 nested-name-specifier. */
4157 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4158 if (token->type != CPP_SCOPE
4159 && !cp_parser_nth_token_starts_template_argument_list_p
4160 (parser, 2))
4161 break;
4164 /* The nested-name-specifier is optional, so we parse
4165 tentatively. */
4166 cp_parser_parse_tentatively (parser);
4168 /* Look for the optional `template' keyword, if this isn't the
4169 first time through the loop. */
4170 if (success)
4171 template_keyword_p = cp_parser_optional_template_keyword (parser);
4172 else
4173 template_keyword_p = false;
4175 /* Save the old scope since the name lookup we are about to do
4176 might destroy it. */
4177 old_scope = parser->scope;
4178 saved_qualifying_scope = parser->qualifying_scope;
4179 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4180 look up names in "X<T>::I" in order to determine that "Y" is
4181 a template. So, if we have a typename at this point, we make
4182 an effort to look through it. */
4183 if (is_declaration
4184 && !typename_keyword_p
4185 && parser->scope
4186 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4187 parser->scope = resolve_typename_type (parser->scope,
4188 /*only_current_p=*/false);
4189 /* Parse the qualifying entity. */
4190 new_scope
4191 = cp_parser_qualifying_entity (parser,
4192 typename_keyword_p,
4193 template_keyword_p,
4194 check_dependency_p,
4195 type_p,
4196 is_declaration);
4197 /* Look for the `::' token. */
4198 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4200 /* If we found what we wanted, we keep going; otherwise, we're
4201 done. */
4202 if (!cp_parser_parse_definitely (parser))
4204 bool error_p = false;
4206 /* Restore the OLD_SCOPE since it was valid before the
4207 failed attempt at finding the last
4208 class-or-namespace-name. */
4209 parser->scope = old_scope;
4210 parser->qualifying_scope = saved_qualifying_scope;
4211 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4212 break;
4213 /* If the next token is an identifier, and the one after
4214 that is a `::', then any valid interpretation would have
4215 found a class-or-namespace-name. */
4216 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4217 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4218 == CPP_SCOPE)
4219 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4220 != CPP_COMPL))
4222 token = cp_lexer_consume_token (parser->lexer);
4223 if (!error_p)
4225 if (!token->ambiguous_p)
4227 tree decl;
4228 tree ambiguous_decls;
4230 decl = cp_parser_lookup_name (parser, token->u.value,
4231 none_type,
4232 /*is_template=*/false,
4233 /*is_namespace=*/false,
4234 /*check_dependency=*/true,
4235 &ambiguous_decls,
4236 token->location);
4237 if (TREE_CODE (decl) == TEMPLATE_DECL)
4238 error_at (token->location,
4239 "%qD used without template parameters",
4240 decl);
4241 else if (ambiguous_decls)
4243 error_at (token->location,
4244 "reference to %qD is ambiguous",
4245 token->u.value);
4246 print_candidates (ambiguous_decls);
4247 decl = error_mark_node;
4249 else
4251 const char* msg = "is not a class or namespace";
4252 if (cxx_dialect != cxx98)
4253 msg = "is not a class, namespace, or enumeration";
4254 cp_parser_name_lookup_error
4255 (parser, token->u.value, decl, msg,
4256 token->location);
4259 parser->scope = error_mark_node;
4260 error_p = true;
4261 /* Treat this as a successful nested-name-specifier
4262 due to:
4264 [basic.lookup.qual]
4266 If the name found is not a class-name (clause
4267 _class_) or namespace-name (_namespace.def_), the
4268 program is ill-formed. */
4269 success = true;
4271 cp_lexer_consume_token (parser->lexer);
4273 break;
4275 /* We've found one valid nested-name-specifier. */
4276 success = true;
4277 /* Name lookup always gives us a DECL. */
4278 if (TREE_CODE (new_scope) == TYPE_DECL)
4279 new_scope = TREE_TYPE (new_scope);
4280 /* Uses of "template" must be followed by actual templates. */
4281 if (template_keyword_p
4282 && !(CLASS_TYPE_P (new_scope)
4283 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4284 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4285 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4286 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4287 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4288 == TEMPLATE_ID_EXPR)))
4289 permerror (input_location, TYPE_P (new_scope)
4290 ? "%qT is not a template"
4291 : "%qD is not a template",
4292 new_scope);
4293 /* If it is a class scope, try to complete it; we are about to
4294 be looking up names inside the class. */
4295 if (TYPE_P (new_scope)
4296 /* Since checking types for dependency can be expensive,
4297 avoid doing it if the type is already complete. */
4298 && !COMPLETE_TYPE_P (new_scope)
4299 /* Do not try to complete dependent types. */
4300 && !dependent_type_p (new_scope))
4302 new_scope = complete_type (new_scope);
4303 /* If it is a typedef to current class, use the current
4304 class instead, as the typedef won't have any names inside
4305 it yet. */
4306 if (!COMPLETE_TYPE_P (new_scope)
4307 && currently_open_class (new_scope))
4308 new_scope = TYPE_MAIN_VARIANT (new_scope);
4310 /* Make sure we look in the right scope the next time through
4311 the loop. */
4312 parser->scope = new_scope;
4315 /* If parsing tentatively, replace the sequence of tokens that makes
4316 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4317 token. That way, should we re-parse the token stream, we will
4318 not have to repeat the effort required to do the parse, nor will
4319 we issue duplicate error messages. */
4320 if (success && start)
4322 cp_token *token;
4324 token = cp_lexer_token_at (parser->lexer, start);
4325 /* Reset the contents of the START token. */
4326 token->type = CPP_NESTED_NAME_SPECIFIER;
4327 /* Retrieve any deferred checks. Do not pop this access checks yet
4328 so the memory will not be reclaimed during token replacing below. */
4329 token->u.tree_check_value = GGC_CNEW (struct tree_check);
4330 token->u.tree_check_value->value = parser->scope;
4331 token->u.tree_check_value->checks = get_deferred_access_checks ();
4332 token->u.tree_check_value->qualifying_scope =
4333 parser->qualifying_scope;
4334 token->keyword = RID_MAX;
4336 /* Purge all subsequent tokens. */
4337 cp_lexer_purge_tokens_after (parser->lexer, start);
4340 if (start)
4341 pop_to_parent_deferring_access_checks ();
4343 return success ? parser->scope : NULL_TREE;
4346 /* Parse a nested-name-specifier. See
4347 cp_parser_nested_name_specifier_opt for details. This function
4348 behaves identically, except that it will an issue an error if no
4349 nested-name-specifier is present. */
4351 static tree
4352 cp_parser_nested_name_specifier (cp_parser *parser,
4353 bool typename_keyword_p,
4354 bool check_dependency_p,
4355 bool type_p,
4356 bool is_declaration)
4358 tree scope;
4360 /* Look for the nested-name-specifier. */
4361 scope = cp_parser_nested_name_specifier_opt (parser,
4362 typename_keyword_p,
4363 check_dependency_p,
4364 type_p,
4365 is_declaration);
4366 /* If it was not present, issue an error message. */
4367 if (!scope)
4369 cp_parser_error (parser, "expected nested-name-specifier");
4370 parser->scope = NULL_TREE;
4373 return scope;
4376 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4377 this is either a class-name or a namespace-name (which corresponds
4378 to the class-or-namespace-name production in the grammar). For
4379 C++0x, it can also be a type-name that refers to an enumeration
4380 type.
4382 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4383 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4384 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4385 TYPE_P is TRUE iff the next name should be taken as a class-name,
4386 even the same name is declared to be another entity in the same
4387 scope.
4389 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4390 specified by the class-or-namespace-name. If neither is found the
4391 ERROR_MARK_NODE is returned. */
4393 static tree
4394 cp_parser_qualifying_entity (cp_parser *parser,
4395 bool typename_keyword_p,
4396 bool template_keyword_p,
4397 bool check_dependency_p,
4398 bool type_p,
4399 bool is_declaration)
4401 tree saved_scope;
4402 tree saved_qualifying_scope;
4403 tree saved_object_scope;
4404 tree scope;
4405 bool only_class_p;
4406 bool successful_parse_p;
4408 /* Before we try to parse the class-name, we must save away the
4409 current PARSER->SCOPE since cp_parser_class_name will destroy
4410 it. */
4411 saved_scope = parser->scope;
4412 saved_qualifying_scope = parser->qualifying_scope;
4413 saved_object_scope = parser->object_scope;
4414 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4415 there is no need to look for a namespace-name. */
4416 only_class_p = template_keyword_p
4417 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4418 if (!only_class_p)
4419 cp_parser_parse_tentatively (parser);
4420 scope = cp_parser_class_name (parser,
4421 typename_keyword_p,
4422 template_keyword_p,
4423 type_p ? class_type : none_type,
4424 check_dependency_p,
4425 /*class_head_p=*/false,
4426 is_declaration);
4427 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4428 /* If that didn't work and we're in C++0x mode, try for a type-name. */
4429 if (!only_class_p
4430 && cxx_dialect != cxx98
4431 && !successful_parse_p)
4433 /* Restore the saved scope. */
4434 parser->scope = saved_scope;
4435 parser->qualifying_scope = saved_qualifying_scope;
4436 parser->object_scope = saved_object_scope;
4438 /* Parse tentatively. */
4439 cp_parser_parse_tentatively (parser);
4441 /* Parse a typedef-name or enum-name. */
4442 scope = cp_parser_nonclass_name (parser);
4443 successful_parse_p = cp_parser_parse_definitely (parser);
4445 /* If that didn't work, try for a namespace-name. */
4446 if (!only_class_p && !successful_parse_p)
4448 /* Restore the saved scope. */
4449 parser->scope = saved_scope;
4450 parser->qualifying_scope = saved_qualifying_scope;
4451 parser->object_scope = saved_object_scope;
4452 /* If we are not looking at an identifier followed by the scope
4453 resolution operator, then this is not part of a
4454 nested-name-specifier. (Note that this function is only used
4455 to parse the components of a nested-name-specifier.) */
4456 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4457 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4458 return error_mark_node;
4459 scope = cp_parser_namespace_name (parser);
4462 return scope;
4465 /* Parse a postfix-expression.
4467 postfix-expression:
4468 primary-expression
4469 postfix-expression [ expression ]
4470 postfix-expression ( expression-list [opt] )
4471 simple-type-specifier ( expression-list [opt] )
4472 typename :: [opt] nested-name-specifier identifier
4473 ( expression-list [opt] )
4474 typename :: [opt] nested-name-specifier template [opt] template-id
4475 ( expression-list [opt] )
4476 postfix-expression . template [opt] id-expression
4477 postfix-expression -> template [opt] id-expression
4478 postfix-expression . pseudo-destructor-name
4479 postfix-expression -> pseudo-destructor-name
4480 postfix-expression ++
4481 postfix-expression --
4482 dynamic_cast < type-id > ( expression )
4483 static_cast < type-id > ( expression )
4484 reinterpret_cast < type-id > ( expression )
4485 const_cast < type-id > ( expression )
4486 typeid ( expression )
4487 typeid ( type-id )
4489 GNU Extension:
4491 postfix-expression:
4492 ( type-id ) { initializer-list , [opt] }
4494 This extension is a GNU version of the C99 compound-literal
4495 construct. (The C99 grammar uses `type-name' instead of `type-id',
4496 but they are essentially the same concept.)
4498 If ADDRESS_P is true, the postfix expression is the operand of the
4499 `&' operator. CAST_P is true if this expression is the target of a
4500 cast.
4502 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4503 class member access expressions [expr.ref].
4505 Returns a representation of the expression. */
4507 static tree
4508 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4509 bool member_access_only_p,
4510 cp_id_kind * pidk_return)
4512 cp_token *token;
4513 enum rid keyword;
4514 cp_id_kind idk = CP_ID_KIND_NONE;
4515 tree postfix_expression = NULL_TREE;
4516 bool is_member_access = false;
4518 /* Peek at the next token. */
4519 token = cp_lexer_peek_token (parser->lexer);
4520 /* Some of the productions are determined by keywords. */
4521 keyword = token->keyword;
4522 switch (keyword)
4524 case RID_DYNCAST:
4525 case RID_STATCAST:
4526 case RID_REINTCAST:
4527 case RID_CONSTCAST:
4529 tree type;
4530 tree expression;
4531 const char *saved_message;
4533 /* All of these can be handled in the same way from the point
4534 of view of parsing. Begin by consuming the token
4535 identifying the cast. */
4536 cp_lexer_consume_token (parser->lexer);
4538 /* New types cannot be defined in the cast. */
4539 saved_message = parser->type_definition_forbidden_message;
4540 parser->type_definition_forbidden_message
4541 = G_("types may not be defined in casts");
4543 /* Look for the opening `<'. */
4544 cp_parser_require (parser, CPP_LESS, "%<<%>");
4545 /* Parse the type to which we are casting. */
4546 type = cp_parser_type_id (parser);
4547 /* Look for the closing `>'. */
4548 cp_parser_require (parser, CPP_GREATER, "%<>%>");
4549 /* Restore the old message. */
4550 parser->type_definition_forbidden_message = saved_message;
4552 /* And the expression which is being cast. */
4553 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4554 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4555 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4557 /* Only type conversions to integral or enumeration types
4558 can be used in constant-expressions. */
4559 if (!cast_valid_in_integral_constant_expression_p (type)
4560 && (cp_parser_non_integral_constant_expression
4561 (parser,
4562 "a cast to a type other than an integral or "
4563 "enumeration type")))
4564 return error_mark_node;
4566 switch (keyword)
4568 case RID_DYNCAST:
4569 postfix_expression
4570 = build_dynamic_cast (type, expression, tf_warning_or_error);
4571 break;
4572 case RID_STATCAST:
4573 postfix_expression
4574 = build_static_cast (type, expression, tf_warning_or_error);
4575 break;
4576 case RID_REINTCAST:
4577 postfix_expression
4578 = build_reinterpret_cast (type, expression,
4579 tf_warning_or_error);
4580 break;
4581 case RID_CONSTCAST:
4582 postfix_expression
4583 = build_const_cast (type, expression, tf_warning_or_error);
4584 break;
4585 default:
4586 gcc_unreachable ();
4589 break;
4591 case RID_TYPEID:
4593 tree type;
4594 const char *saved_message;
4595 bool saved_in_type_id_in_expr_p;
4597 /* Consume the `typeid' token. */
4598 cp_lexer_consume_token (parser->lexer);
4599 /* Look for the `(' token. */
4600 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4601 /* Types cannot be defined in a `typeid' expression. */
4602 saved_message = parser->type_definition_forbidden_message;
4603 parser->type_definition_forbidden_message
4604 = G_("types may not be defined in a %<typeid%> expression");
4605 /* We can't be sure yet whether we're looking at a type-id or an
4606 expression. */
4607 cp_parser_parse_tentatively (parser);
4608 /* Try a type-id first. */
4609 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4610 parser->in_type_id_in_expr_p = true;
4611 type = cp_parser_type_id (parser);
4612 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4613 /* Look for the `)' token. Otherwise, we can't be sure that
4614 we're not looking at an expression: consider `typeid (int
4615 (3))', for example. */
4616 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4617 /* If all went well, simply lookup the type-id. */
4618 if (cp_parser_parse_definitely (parser))
4619 postfix_expression = get_typeid (type);
4620 /* Otherwise, fall back to the expression variant. */
4621 else
4623 tree expression;
4625 /* Look for an expression. */
4626 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4627 /* Compute its typeid. */
4628 postfix_expression = build_typeid (expression);
4629 /* Look for the `)' token. */
4630 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4632 /* Restore the saved message. */
4633 parser->type_definition_forbidden_message = saved_message;
4634 /* `typeid' may not appear in an integral constant expression. */
4635 if (cp_parser_non_integral_constant_expression(parser,
4636 "%<typeid%> operator"))
4637 return error_mark_node;
4639 break;
4641 case RID_TYPENAME:
4643 tree type;
4644 /* The syntax permitted here is the same permitted for an
4645 elaborated-type-specifier. */
4646 type = cp_parser_elaborated_type_specifier (parser,
4647 /*is_friend=*/false,
4648 /*is_declaration=*/false);
4649 postfix_expression = cp_parser_functional_cast (parser, type);
4651 break;
4653 default:
4655 tree type;
4657 /* If the next thing is a simple-type-specifier, we may be
4658 looking at a functional cast. We could also be looking at
4659 an id-expression. So, we try the functional cast, and if
4660 that doesn't work we fall back to the primary-expression. */
4661 cp_parser_parse_tentatively (parser);
4662 /* Look for the simple-type-specifier. */
4663 type = cp_parser_simple_type_specifier (parser,
4664 /*decl_specs=*/NULL,
4665 CP_PARSER_FLAGS_NONE);
4666 /* Parse the cast itself. */
4667 if (!cp_parser_error_occurred (parser))
4668 postfix_expression
4669 = cp_parser_functional_cast (parser, type);
4670 /* If that worked, we're done. */
4671 if (cp_parser_parse_definitely (parser))
4672 break;
4674 /* If the functional-cast didn't work out, try a
4675 compound-literal. */
4676 if (cp_parser_allow_gnu_extensions_p (parser)
4677 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4679 VEC(constructor_elt,gc) *initializer_list = NULL;
4680 bool saved_in_type_id_in_expr_p;
4682 cp_parser_parse_tentatively (parser);
4683 /* Consume the `('. */
4684 cp_lexer_consume_token (parser->lexer);
4685 /* Parse the type. */
4686 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4687 parser->in_type_id_in_expr_p = true;
4688 type = cp_parser_type_id (parser);
4689 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4690 /* Look for the `)'. */
4691 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4692 /* Look for the `{'. */
4693 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4694 /* If things aren't going well, there's no need to
4695 keep going. */
4696 if (!cp_parser_error_occurred (parser))
4698 bool non_constant_p;
4699 /* Parse the initializer-list. */
4700 initializer_list
4701 = cp_parser_initializer_list (parser, &non_constant_p);
4702 /* Allow a trailing `,'. */
4703 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4704 cp_lexer_consume_token (parser->lexer);
4705 /* Look for the final `}'. */
4706 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4708 /* If that worked, we're definitely looking at a
4709 compound-literal expression. */
4710 if (cp_parser_parse_definitely (parser))
4712 /* Warn the user that a compound literal is not
4713 allowed in standard C++. */
4714 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4715 /* For simplicity, we disallow compound literals in
4716 constant-expressions. We could
4717 allow compound literals of integer type, whose
4718 initializer was a constant, in constant
4719 expressions. Permitting that usage, as a further
4720 extension, would not change the meaning of any
4721 currently accepted programs. (Of course, as
4722 compound literals are not part of ISO C++, the
4723 standard has nothing to say.) */
4724 if (cp_parser_non_integral_constant_expression
4725 (parser, "non-constant compound literals"))
4727 postfix_expression = error_mark_node;
4728 break;
4730 /* Form the representation of the compound-literal. */
4731 postfix_expression
4732 = (finish_compound_literal
4733 (type, build_constructor (init_list_type_node,
4734 initializer_list)));
4735 break;
4739 /* It must be a primary-expression. */
4740 postfix_expression
4741 = cp_parser_primary_expression (parser, address_p, cast_p,
4742 /*template_arg_p=*/false,
4743 &idk);
4745 break;
4748 /* Keep looping until the postfix-expression is complete. */
4749 while (true)
4751 if (idk == CP_ID_KIND_UNQUALIFIED
4752 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4753 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4754 /* It is not a Koenig lookup function call. */
4755 postfix_expression
4756 = unqualified_name_lookup_error (postfix_expression);
4758 /* Peek at the next token. */
4759 token = cp_lexer_peek_token (parser->lexer);
4761 switch (token->type)
4763 case CPP_OPEN_SQUARE:
4764 postfix_expression
4765 = cp_parser_postfix_open_square_expression (parser,
4766 postfix_expression,
4767 false);
4768 idk = CP_ID_KIND_NONE;
4769 is_member_access = false;
4770 break;
4772 case CPP_OPEN_PAREN:
4773 /* postfix-expression ( expression-list [opt] ) */
4775 bool koenig_p;
4776 bool is_builtin_constant_p;
4777 bool saved_integral_constant_expression_p = false;
4778 bool saved_non_integral_constant_expression_p = false;
4779 VEC(tree,gc) *args;
4781 is_member_access = false;
4783 is_builtin_constant_p
4784 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4785 if (is_builtin_constant_p)
4787 /* The whole point of __builtin_constant_p is to allow
4788 non-constant expressions to appear as arguments. */
4789 saved_integral_constant_expression_p
4790 = parser->integral_constant_expression_p;
4791 saved_non_integral_constant_expression_p
4792 = parser->non_integral_constant_expression_p;
4793 parser->integral_constant_expression_p = false;
4795 args = (cp_parser_parenthesized_expression_list
4796 (parser, /*is_attribute_list=*/false,
4797 /*cast_p=*/false, /*allow_expansion_p=*/true,
4798 /*non_constant_p=*/NULL));
4799 if (is_builtin_constant_p)
4801 parser->integral_constant_expression_p
4802 = saved_integral_constant_expression_p;
4803 parser->non_integral_constant_expression_p
4804 = saved_non_integral_constant_expression_p;
4807 if (args == NULL)
4809 postfix_expression = error_mark_node;
4810 break;
4813 /* Function calls are not permitted in
4814 constant-expressions. */
4815 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4816 && cp_parser_non_integral_constant_expression (parser,
4817 "a function call"))
4819 postfix_expression = error_mark_node;
4820 release_tree_vector (args);
4821 break;
4824 koenig_p = false;
4825 if (idk == CP_ID_KIND_UNQUALIFIED
4826 || idk == CP_ID_KIND_TEMPLATE_ID)
4828 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4830 if (!VEC_empty (tree, args))
4832 koenig_p = true;
4833 if (!any_type_dependent_arguments_p (args))
4834 postfix_expression
4835 = perform_koenig_lookup (postfix_expression, args);
4837 else
4838 postfix_expression
4839 = unqualified_fn_lookup_error (postfix_expression);
4841 /* We do not perform argument-dependent lookup if
4842 normal lookup finds a non-function, in accordance
4843 with the expected resolution of DR 218. */
4844 else if (!VEC_empty (tree, args)
4845 && is_overloaded_fn (postfix_expression))
4847 tree fn = get_first_fn (postfix_expression);
4848 fn = STRIP_TEMPLATE (fn);
4850 /* Do not do argument dependent lookup if regular
4851 lookup finds a member function or a block-scope
4852 function declaration. [basic.lookup.argdep]/3 */
4853 if (!DECL_FUNCTION_MEMBER_P (fn)
4854 && !DECL_LOCAL_FUNCTION_P (fn))
4856 koenig_p = true;
4857 if (!any_type_dependent_arguments_p (args))
4858 postfix_expression
4859 = perform_koenig_lookup (postfix_expression, args);
4864 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4866 tree instance = TREE_OPERAND (postfix_expression, 0);
4867 tree fn = TREE_OPERAND (postfix_expression, 1);
4869 if (processing_template_decl
4870 && (type_dependent_expression_p (instance)
4871 || (!BASELINK_P (fn)
4872 && TREE_CODE (fn) != FIELD_DECL)
4873 || type_dependent_expression_p (fn)
4874 || any_type_dependent_arguments_p (args)))
4876 postfix_expression
4877 = build_nt_call_vec (postfix_expression, args);
4878 release_tree_vector (args);
4879 break;
4882 if (BASELINK_P (fn))
4884 postfix_expression
4885 = (build_new_method_call
4886 (instance, fn, &args, NULL_TREE,
4887 (idk == CP_ID_KIND_QUALIFIED
4888 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4889 /*fn_p=*/NULL,
4890 tf_warning_or_error));
4892 else
4893 postfix_expression
4894 = finish_call_expr (postfix_expression, &args,
4895 /*disallow_virtual=*/false,
4896 /*koenig_p=*/false,
4897 tf_warning_or_error);
4899 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4900 || TREE_CODE (postfix_expression) == MEMBER_REF
4901 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4902 postfix_expression = (build_offset_ref_call_from_tree
4903 (postfix_expression, &args));
4904 else if (idk == CP_ID_KIND_QUALIFIED)
4905 /* A call to a static class member, or a namespace-scope
4906 function. */
4907 postfix_expression
4908 = finish_call_expr (postfix_expression, &args,
4909 /*disallow_virtual=*/true,
4910 koenig_p,
4911 tf_warning_or_error);
4912 else
4913 /* All other function calls. */
4914 postfix_expression
4915 = finish_call_expr (postfix_expression, &args,
4916 /*disallow_virtual=*/false,
4917 koenig_p,
4918 tf_warning_or_error);
4920 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4921 idk = CP_ID_KIND_NONE;
4923 release_tree_vector (args);
4925 break;
4927 case CPP_DOT:
4928 case CPP_DEREF:
4929 /* postfix-expression . template [opt] id-expression
4930 postfix-expression . pseudo-destructor-name
4931 postfix-expression -> template [opt] id-expression
4932 postfix-expression -> pseudo-destructor-name */
4934 /* Consume the `.' or `->' operator. */
4935 cp_lexer_consume_token (parser->lexer);
4937 postfix_expression
4938 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4939 postfix_expression,
4940 false, &idk,
4941 token->location);
4943 is_member_access = true;
4944 break;
4946 case CPP_PLUS_PLUS:
4947 /* postfix-expression ++ */
4948 /* Consume the `++' token. */
4949 cp_lexer_consume_token (parser->lexer);
4950 /* Generate a representation for the complete expression. */
4951 postfix_expression
4952 = finish_increment_expr (postfix_expression,
4953 POSTINCREMENT_EXPR);
4954 /* Increments may not appear in constant-expressions. */
4955 if (cp_parser_non_integral_constant_expression (parser,
4956 "an increment"))
4957 postfix_expression = error_mark_node;
4958 idk = CP_ID_KIND_NONE;
4959 is_member_access = false;
4960 break;
4962 case CPP_MINUS_MINUS:
4963 /* postfix-expression -- */
4964 /* Consume the `--' token. */
4965 cp_lexer_consume_token (parser->lexer);
4966 /* Generate a representation for the complete expression. */
4967 postfix_expression
4968 = finish_increment_expr (postfix_expression,
4969 POSTDECREMENT_EXPR);
4970 /* Decrements may not appear in constant-expressions. */
4971 if (cp_parser_non_integral_constant_expression (parser,
4972 "a decrement"))
4973 postfix_expression = error_mark_node;
4974 idk = CP_ID_KIND_NONE;
4975 is_member_access = false;
4976 break;
4978 default:
4979 if (pidk_return != NULL)
4980 * pidk_return = idk;
4981 if (member_access_only_p)
4982 return is_member_access? postfix_expression : error_mark_node;
4983 else
4984 return postfix_expression;
4988 /* We should never get here. */
4989 gcc_unreachable ();
4990 return error_mark_node;
4993 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4994 by cp_parser_builtin_offsetof. We're looking for
4996 postfix-expression [ expression ]
4998 FOR_OFFSETOF is set if we're being called in that context, which
4999 changes how we deal with integer constant expressions. */
5001 static tree
5002 cp_parser_postfix_open_square_expression (cp_parser *parser,
5003 tree postfix_expression,
5004 bool for_offsetof)
5006 tree index;
5008 /* Consume the `[' token. */
5009 cp_lexer_consume_token (parser->lexer);
5011 /* Parse the index expression. */
5012 /* ??? For offsetof, there is a question of what to allow here. If
5013 offsetof is not being used in an integral constant expression context,
5014 then we *could* get the right answer by computing the value at runtime.
5015 If we are in an integral constant expression context, then we might
5016 could accept any constant expression; hard to say without analysis.
5017 Rather than open the barn door too wide right away, allow only integer
5018 constant expressions here. */
5019 if (for_offsetof)
5020 index = cp_parser_constant_expression (parser, false, NULL);
5021 else
5022 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5024 /* Look for the closing `]'. */
5025 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5027 /* Build the ARRAY_REF. */
5028 postfix_expression = grok_array_decl (postfix_expression, index);
5030 /* When not doing offsetof, array references are not permitted in
5031 constant-expressions. */
5032 if (!for_offsetof
5033 && (cp_parser_non_integral_constant_expression
5034 (parser, "an array reference")))
5035 postfix_expression = error_mark_node;
5037 return postfix_expression;
5040 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5041 by cp_parser_builtin_offsetof. We're looking for
5043 postfix-expression . template [opt] id-expression
5044 postfix-expression . pseudo-destructor-name
5045 postfix-expression -> template [opt] id-expression
5046 postfix-expression -> pseudo-destructor-name
5048 FOR_OFFSETOF is set if we're being called in that context. That sorta
5049 limits what of the above we'll actually accept, but nevermind.
5050 TOKEN_TYPE is the "." or "->" token, which will already have been
5051 removed from the stream. */
5053 static tree
5054 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5055 enum cpp_ttype token_type,
5056 tree postfix_expression,
5057 bool for_offsetof, cp_id_kind *idk,
5058 location_t location)
5060 tree name;
5061 bool dependent_p;
5062 bool pseudo_destructor_p;
5063 tree scope = NULL_TREE;
5065 /* If this is a `->' operator, dereference the pointer. */
5066 if (token_type == CPP_DEREF)
5067 postfix_expression = build_x_arrow (postfix_expression);
5068 /* Check to see whether or not the expression is type-dependent. */
5069 dependent_p = type_dependent_expression_p (postfix_expression);
5070 /* The identifier following the `->' or `.' is not qualified. */
5071 parser->scope = NULL_TREE;
5072 parser->qualifying_scope = NULL_TREE;
5073 parser->object_scope = NULL_TREE;
5074 *idk = CP_ID_KIND_NONE;
5076 /* Enter the scope corresponding to the type of the object
5077 given by the POSTFIX_EXPRESSION. */
5078 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5080 scope = TREE_TYPE (postfix_expression);
5081 /* According to the standard, no expression should ever have
5082 reference type. Unfortunately, we do not currently match
5083 the standard in this respect in that our internal representation
5084 of an expression may have reference type even when the standard
5085 says it does not. Therefore, we have to manually obtain the
5086 underlying type here. */
5087 scope = non_reference (scope);
5088 /* The type of the POSTFIX_EXPRESSION must be complete. */
5089 if (scope == unknown_type_node)
5091 error_at (location, "%qE does not have class type",
5092 postfix_expression);
5093 scope = NULL_TREE;
5095 else
5096 scope = complete_type_or_else (scope, NULL_TREE);
5097 /* Let the name lookup machinery know that we are processing a
5098 class member access expression. */
5099 parser->context->object_type = scope;
5100 /* If something went wrong, we want to be able to discern that case,
5101 as opposed to the case where there was no SCOPE due to the type
5102 of expression being dependent. */
5103 if (!scope)
5104 scope = error_mark_node;
5105 /* If the SCOPE was erroneous, make the various semantic analysis
5106 functions exit quickly -- and without issuing additional error
5107 messages. */
5108 if (scope == error_mark_node)
5109 postfix_expression = error_mark_node;
5112 /* Assume this expression is not a pseudo-destructor access. */
5113 pseudo_destructor_p = false;
5115 /* If the SCOPE is a scalar type, then, if this is a valid program,
5116 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5117 is type dependent, it can be pseudo-destructor-name or something else.
5118 Try to parse it as pseudo-destructor-name first. */
5119 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5121 tree s;
5122 tree type;
5124 cp_parser_parse_tentatively (parser);
5125 /* Parse the pseudo-destructor-name. */
5126 s = NULL_TREE;
5127 cp_parser_pseudo_destructor_name (parser, &s, &type);
5128 if (dependent_p
5129 && (cp_parser_error_occurred (parser)
5130 || TREE_CODE (type) != TYPE_DECL
5131 || !SCALAR_TYPE_P (TREE_TYPE (type))))
5132 cp_parser_abort_tentative_parse (parser);
5133 else if (cp_parser_parse_definitely (parser))
5135 pseudo_destructor_p = true;
5136 postfix_expression
5137 = finish_pseudo_destructor_expr (postfix_expression,
5138 s, TREE_TYPE (type));
5142 if (!pseudo_destructor_p)
5144 /* If the SCOPE is not a scalar type, we are looking at an
5145 ordinary class member access expression, rather than a
5146 pseudo-destructor-name. */
5147 bool template_p;
5148 cp_token *token = cp_lexer_peek_token (parser->lexer);
5149 /* Parse the id-expression. */
5150 name = (cp_parser_id_expression
5151 (parser,
5152 cp_parser_optional_template_keyword (parser),
5153 /*check_dependency_p=*/true,
5154 &template_p,
5155 /*declarator_p=*/false,
5156 /*optional_p=*/false));
5157 /* In general, build a SCOPE_REF if the member name is qualified.
5158 However, if the name was not dependent and has already been
5159 resolved; there is no need to build the SCOPE_REF. For example;
5161 struct X { void f(); };
5162 template <typename T> void f(T* t) { t->X::f(); }
5164 Even though "t" is dependent, "X::f" is not and has been resolved
5165 to a BASELINK; there is no need to include scope information. */
5167 /* But we do need to remember that there was an explicit scope for
5168 virtual function calls. */
5169 if (parser->scope)
5170 *idk = CP_ID_KIND_QUALIFIED;
5172 /* If the name is a template-id that names a type, we will get a
5173 TYPE_DECL here. That is invalid code. */
5174 if (TREE_CODE (name) == TYPE_DECL)
5176 error_at (token->location, "invalid use of %qD", name);
5177 postfix_expression = error_mark_node;
5179 else
5181 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5183 name = build_qualified_name (/*type=*/NULL_TREE,
5184 parser->scope,
5185 name,
5186 template_p);
5187 parser->scope = NULL_TREE;
5188 parser->qualifying_scope = NULL_TREE;
5189 parser->object_scope = NULL_TREE;
5191 if (scope && name && BASELINK_P (name))
5192 adjust_result_of_qualified_name_lookup
5193 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5194 postfix_expression
5195 = finish_class_member_access_expr (postfix_expression, name,
5196 template_p,
5197 tf_warning_or_error);
5201 /* We no longer need to look up names in the scope of the object on
5202 the left-hand side of the `.' or `->' operator. */
5203 parser->context->object_type = NULL_TREE;
5205 /* Outside of offsetof, these operators may not appear in
5206 constant-expressions. */
5207 if (!for_offsetof
5208 && (cp_parser_non_integral_constant_expression
5209 (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5210 postfix_expression = error_mark_node;
5212 return postfix_expression;
5215 /* Parse a parenthesized expression-list.
5217 expression-list:
5218 assignment-expression
5219 expression-list, assignment-expression
5221 attribute-list:
5222 expression-list
5223 identifier
5224 identifier, expression-list
5226 CAST_P is true if this expression is the target of a cast.
5228 ALLOW_EXPANSION_P is true if this expression allows expansion of an
5229 argument pack.
5231 Returns a vector of trees. Each element is a representation of an
5232 assignment-expression. NULL is returned if the ( and or ) are
5233 missing. An empty, but allocated, vector is returned on no
5234 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is true
5235 if this is really an attribute list being parsed. If
5236 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5237 not all of the expressions in the list were constant. */
5239 static VEC(tree,gc) *
5240 cp_parser_parenthesized_expression_list (cp_parser* parser,
5241 bool is_attribute_list,
5242 bool cast_p,
5243 bool allow_expansion_p,
5244 bool *non_constant_p)
5246 VEC(tree,gc) *expression_list;
5247 bool fold_expr_p = is_attribute_list;
5248 tree identifier = NULL_TREE;
5249 bool saved_greater_than_is_operator_p;
5251 /* Assume all the expressions will be constant. */
5252 if (non_constant_p)
5253 *non_constant_p = false;
5255 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5256 return NULL;
5258 expression_list = make_tree_vector ();
5260 /* Within a parenthesized expression, a `>' token is always
5261 the greater-than operator. */
5262 saved_greater_than_is_operator_p
5263 = parser->greater_than_is_operator_p;
5264 parser->greater_than_is_operator_p = true;
5266 /* Consume expressions until there are no more. */
5267 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5268 while (true)
5270 tree expr;
5272 /* At the beginning of attribute lists, check to see if the
5273 next token is an identifier. */
5274 if (is_attribute_list
5275 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5277 cp_token *token;
5279 /* Consume the identifier. */
5280 token = cp_lexer_consume_token (parser->lexer);
5281 /* Save the identifier. */
5282 identifier = token->u.value;
5284 else
5286 bool expr_non_constant_p;
5288 /* Parse the next assignment-expression. */
5289 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5291 /* A braced-init-list. */
5292 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5293 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5294 if (non_constant_p && expr_non_constant_p)
5295 *non_constant_p = true;
5297 else if (non_constant_p)
5299 expr = (cp_parser_constant_expression
5300 (parser, /*allow_non_constant_p=*/true,
5301 &expr_non_constant_p));
5302 if (expr_non_constant_p)
5303 *non_constant_p = true;
5305 else
5306 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5308 if (fold_expr_p)
5309 expr = fold_non_dependent_expr (expr);
5311 /* If we have an ellipsis, then this is an expression
5312 expansion. */
5313 if (allow_expansion_p
5314 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5316 /* Consume the `...'. */
5317 cp_lexer_consume_token (parser->lexer);
5319 /* Build the argument pack. */
5320 expr = make_pack_expansion (expr);
5323 /* Add it to the list. We add error_mark_node
5324 expressions to the list, so that we can still tell if
5325 the correct form for a parenthesized expression-list
5326 is found. That gives better errors. */
5327 VEC_safe_push (tree, gc, expression_list, expr);
5329 if (expr == error_mark_node)
5330 goto skip_comma;
5333 /* After the first item, attribute lists look the same as
5334 expression lists. */
5335 is_attribute_list = false;
5337 get_comma:;
5338 /* If the next token isn't a `,', then we are done. */
5339 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5340 break;
5342 /* Otherwise, consume the `,' and keep going. */
5343 cp_lexer_consume_token (parser->lexer);
5346 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5348 int ending;
5350 skip_comma:;
5351 /* We try and resync to an unnested comma, as that will give the
5352 user better diagnostics. */
5353 ending = cp_parser_skip_to_closing_parenthesis (parser,
5354 /*recovering=*/true,
5355 /*or_comma=*/true,
5356 /*consume_paren=*/true);
5357 if (ending < 0)
5358 goto get_comma;
5359 if (!ending)
5361 parser->greater_than_is_operator_p
5362 = saved_greater_than_is_operator_p;
5363 return NULL;
5367 parser->greater_than_is_operator_p
5368 = saved_greater_than_is_operator_p;
5370 if (identifier)
5371 VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5373 return expression_list;
5376 /* Parse a pseudo-destructor-name.
5378 pseudo-destructor-name:
5379 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5380 :: [opt] nested-name-specifier template template-id :: ~ type-name
5381 :: [opt] nested-name-specifier [opt] ~ type-name
5383 If either of the first two productions is used, sets *SCOPE to the
5384 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5385 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5386 or ERROR_MARK_NODE if the parse fails. */
5388 static void
5389 cp_parser_pseudo_destructor_name (cp_parser* parser,
5390 tree* scope,
5391 tree* type)
5393 bool nested_name_specifier_p;
5395 /* Assume that things will not work out. */
5396 *type = error_mark_node;
5398 /* Look for the optional `::' operator. */
5399 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5400 /* Look for the optional nested-name-specifier. */
5401 nested_name_specifier_p
5402 = (cp_parser_nested_name_specifier_opt (parser,
5403 /*typename_keyword_p=*/false,
5404 /*check_dependency_p=*/true,
5405 /*type_p=*/false,
5406 /*is_declaration=*/false)
5407 != NULL_TREE);
5408 /* Now, if we saw a nested-name-specifier, we might be doing the
5409 second production. */
5410 if (nested_name_specifier_p
5411 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5413 /* Consume the `template' keyword. */
5414 cp_lexer_consume_token (parser->lexer);
5415 /* Parse the template-id. */
5416 cp_parser_template_id (parser,
5417 /*template_keyword_p=*/true,
5418 /*check_dependency_p=*/false,
5419 /*is_declaration=*/true);
5420 /* Look for the `::' token. */
5421 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5423 /* If the next token is not a `~', then there might be some
5424 additional qualification. */
5425 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5427 /* At this point, we're looking for "type-name :: ~". The type-name
5428 must not be a class-name, since this is a pseudo-destructor. So,
5429 it must be either an enum-name, or a typedef-name -- both of which
5430 are just identifiers. So, we peek ahead to check that the "::"
5431 and "~" tokens are present; if they are not, then we can avoid
5432 calling type_name. */
5433 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5434 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5435 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5437 cp_parser_error (parser, "non-scalar type");
5438 return;
5441 /* Look for the type-name. */
5442 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5443 if (*scope == error_mark_node)
5444 return;
5446 /* Look for the `::' token. */
5447 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5449 else
5450 *scope = NULL_TREE;
5452 /* Look for the `~'. */
5453 cp_parser_require (parser, CPP_COMPL, "%<~%>");
5454 /* Look for the type-name again. We are not responsible for
5455 checking that it matches the first type-name. */
5456 *type = cp_parser_nonclass_name (parser);
5459 /* Parse a unary-expression.
5461 unary-expression:
5462 postfix-expression
5463 ++ cast-expression
5464 -- cast-expression
5465 unary-operator cast-expression
5466 sizeof unary-expression
5467 sizeof ( type-id )
5468 new-expression
5469 delete-expression
5471 GNU Extensions:
5473 unary-expression:
5474 __extension__ cast-expression
5475 __alignof__ unary-expression
5476 __alignof__ ( type-id )
5477 __real__ cast-expression
5478 __imag__ cast-expression
5479 && identifier
5481 ADDRESS_P is true iff the unary-expression is appearing as the
5482 operand of the `&' operator. CAST_P is true if this expression is
5483 the target of a cast.
5485 Returns a representation of the expression. */
5487 static tree
5488 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5489 cp_id_kind * pidk)
5491 cp_token *token;
5492 enum tree_code unary_operator;
5494 /* Peek at the next token. */
5495 token = cp_lexer_peek_token (parser->lexer);
5496 /* Some keywords give away the kind of expression. */
5497 if (token->type == CPP_KEYWORD)
5499 enum rid keyword = token->keyword;
5501 switch (keyword)
5503 case RID_ALIGNOF:
5504 case RID_SIZEOF:
5506 tree operand;
5507 enum tree_code op;
5509 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5510 /* Consume the token. */
5511 cp_lexer_consume_token (parser->lexer);
5512 /* Parse the operand. */
5513 operand = cp_parser_sizeof_operand (parser, keyword);
5515 if (TYPE_P (operand))
5516 return cxx_sizeof_or_alignof_type (operand, op, true);
5517 else
5518 return cxx_sizeof_or_alignof_expr (operand, op, true);
5521 case RID_NEW:
5522 return cp_parser_new_expression (parser);
5524 case RID_DELETE:
5525 return cp_parser_delete_expression (parser);
5527 case RID_EXTENSION:
5529 /* The saved value of the PEDANTIC flag. */
5530 int saved_pedantic;
5531 tree expr;
5533 /* Save away the PEDANTIC flag. */
5534 cp_parser_extension_opt (parser, &saved_pedantic);
5535 /* Parse the cast-expression. */
5536 expr = cp_parser_simple_cast_expression (parser);
5537 /* Restore the PEDANTIC flag. */
5538 pedantic = saved_pedantic;
5540 return expr;
5543 case RID_REALPART:
5544 case RID_IMAGPART:
5546 tree expression;
5548 /* Consume the `__real__' or `__imag__' token. */
5549 cp_lexer_consume_token (parser->lexer);
5550 /* Parse the cast-expression. */
5551 expression = cp_parser_simple_cast_expression (parser);
5552 /* Create the complete representation. */
5553 return build_x_unary_op ((keyword == RID_REALPART
5554 ? REALPART_EXPR : IMAGPART_EXPR),
5555 expression,
5556 tf_warning_or_error);
5558 break;
5560 default:
5561 break;
5565 /* Look for the `:: new' and `:: delete', which also signal the
5566 beginning of a new-expression, or delete-expression,
5567 respectively. If the next token is `::', then it might be one of
5568 these. */
5569 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5571 enum rid keyword;
5573 /* See if the token after the `::' is one of the keywords in
5574 which we're interested. */
5575 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5576 /* If it's `new', we have a new-expression. */
5577 if (keyword == RID_NEW)
5578 return cp_parser_new_expression (parser);
5579 /* Similarly, for `delete'. */
5580 else if (keyword == RID_DELETE)
5581 return cp_parser_delete_expression (parser);
5584 /* Look for a unary operator. */
5585 unary_operator = cp_parser_unary_operator (token);
5586 /* The `++' and `--' operators can be handled similarly, even though
5587 they are not technically unary-operators in the grammar. */
5588 if (unary_operator == ERROR_MARK)
5590 if (token->type == CPP_PLUS_PLUS)
5591 unary_operator = PREINCREMENT_EXPR;
5592 else if (token->type == CPP_MINUS_MINUS)
5593 unary_operator = PREDECREMENT_EXPR;
5594 /* Handle the GNU address-of-label extension. */
5595 else if (cp_parser_allow_gnu_extensions_p (parser)
5596 && token->type == CPP_AND_AND)
5598 tree identifier;
5599 tree expression;
5600 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5602 /* Consume the '&&' token. */
5603 cp_lexer_consume_token (parser->lexer);
5604 /* Look for the identifier. */
5605 identifier = cp_parser_identifier (parser);
5606 /* Create an expression representing the address. */
5607 expression = finish_label_address_expr (identifier, loc);
5608 if (cp_parser_non_integral_constant_expression (parser,
5609 "the address of a label"))
5610 expression = error_mark_node;
5611 return expression;
5614 if (unary_operator != ERROR_MARK)
5616 tree cast_expression;
5617 tree expression = error_mark_node;
5618 const char *non_constant_p = NULL;
5620 /* Consume the operator token. */
5621 token = cp_lexer_consume_token (parser->lexer);
5622 /* Parse the cast-expression. */
5623 cast_expression
5624 = cp_parser_cast_expression (parser,
5625 unary_operator == ADDR_EXPR,
5626 /*cast_p=*/false, pidk);
5627 /* Now, build an appropriate representation. */
5628 switch (unary_operator)
5630 case INDIRECT_REF:
5631 non_constant_p = "%<*%>";
5632 expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR,
5633 tf_warning_or_error);
5634 break;
5636 case ADDR_EXPR:
5637 non_constant_p = "%<&%>";
5638 /* Fall through. */
5639 case BIT_NOT_EXPR:
5640 expression = build_x_unary_op (unary_operator, cast_expression,
5641 tf_warning_or_error);
5642 break;
5644 case PREINCREMENT_EXPR:
5645 case PREDECREMENT_EXPR:
5646 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5647 ? "%<++%>" : "%<--%>");
5648 /* Fall through. */
5649 case UNARY_PLUS_EXPR:
5650 case NEGATE_EXPR:
5651 case TRUTH_NOT_EXPR:
5652 expression = finish_unary_op_expr (unary_operator, cast_expression);
5653 break;
5655 default:
5656 gcc_unreachable ();
5659 if (non_constant_p
5660 && cp_parser_non_integral_constant_expression (parser,
5661 non_constant_p))
5662 expression = error_mark_node;
5664 return expression;
5667 return cp_parser_postfix_expression (parser, address_p, cast_p,
5668 /*member_access_only_p=*/false,
5669 pidk);
5672 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5673 unary-operator, the corresponding tree code is returned. */
5675 static enum tree_code
5676 cp_parser_unary_operator (cp_token* token)
5678 switch (token->type)
5680 case CPP_MULT:
5681 return INDIRECT_REF;
5683 case CPP_AND:
5684 return ADDR_EXPR;
5686 case CPP_PLUS:
5687 return UNARY_PLUS_EXPR;
5689 case CPP_MINUS:
5690 return NEGATE_EXPR;
5692 case CPP_NOT:
5693 return TRUTH_NOT_EXPR;
5695 case CPP_COMPL:
5696 return BIT_NOT_EXPR;
5698 default:
5699 return ERROR_MARK;
5703 /* Parse a new-expression.
5705 new-expression:
5706 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5707 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5709 Returns a representation of the expression. */
5711 static tree
5712 cp_parser_new_expression (cp_parser* parser)
5714 bool global_scope_p;
5715 VEC(tree,gc) *placement;
5716 tree type;
5717 VEC(tree,gc) *initializer;
5718 tree nelts;
5719 tree ret;
5721 /* Look for the optional `::' operator. */
5722 global_scope_p
5723 = (cp_parser_global_scope_opt (parser,
5724 /*current_scope_valid_p=*/false)
5725 != NULL_TREE);
5726 /* Look for the `new' operator. */
5727 cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5728 /* There's no easy way to tell a new-placement from the
5729 `( type-id )' construct. */
5730 cp_parser_parse_tentatively (parser);
5731 /* Look for a new-placement. */
5732 placement = cp_parser_new_placement (parser);
5733 /* If that didn't work out, there's no new-placement. */
5734 if (!cp_parser_parse_definitely (parser))
5736 if (placement != NULL)
5737 release_tree_vector (placement);
5738 placement = NULL;
5741 /* If the next token is a `(', then we have a parenthesized
5742 type-id. */
5743 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5745 cp_token *token;
5746 /* Consume the `('. */
5747 cp_lexer_consume_token (parser->lexer);
5748 /* Parse the type-id. */
5749 type = cp_parser_type_id (parser);
5750 /* Look for the closing `)'. */
5751 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5752 token = cp_lexer_peek_token (parser->lexer);
5753 /* There should not be a direct-new-declarator in this production,
5754 but GCC used to allowed this, so we check and emit a sensible error
5755 message for this case. */
5756 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5758 error_at (token->location,
5759 "array bound forbidden after parenthesized type-id");
5760 inform (token->location,
5761 "try removing the parentheses around the type-id");
5762 cp_parser_direct_new_declarator (parser);
5764 nelts = NULL_TREE;
5766 /* Otherwise, there must be a new-type-id. */
5767 else
5768 type = cp_parser_new_type_id (parser, &nelts);
5770 /* If the next token is a `(' or '{', then we have a new-initializer. */
5771 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5772 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5773 initializer = cp_parser_new_initializer (parser);
5774 else
5775 initializer = NULL;
5777 /* A new-expression may not appear in an integral constant
5778 expression. */
5779 if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5780 ret = error_mark_node;
5781 else
5783 /* Create a representation of the new-expression. */
5784 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
5785 tf_warning_or_error);
5788 if (placement != NULL)
5789 release_tree_vector (placement);
5790 if (initializer != NULL)
5791 release_tree_vector (initializer);
5793 return ret;
5796 /* Parse a new-placement.
5798 new-placement:
5799 ( expression-list )
5801 Returns the same representation as for an expression-list. */
5803 static VEC(tree,gc) *
5804 cp_parser_new_placement (cp_parser* parser)
5806 VEC(tree,gc) *expression_list;
5808 /* Parse the expression-list. */
5809 expression_list = (cp_parser_parenthesized_expression_list
5810 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5811 /*non_constant_p=*/NULL));
5813 return expression_list;
5816 /* Parse a new-type-id.
5818 new-type-id:
5819 type-specifier-seq new-declarator [opt]
5821 Returns the TYPE allocated. If the new-type-id indicates an array
5822 type, *NELTS is set to the number of elements in the last array
5823 bound; the TYPE will not include the last array bound. */
5825 static tree
5826 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5828 cp_decl_specifier_seq type_specifier_seq;
5829 cp_declarator *new_declarator;
5830 cp_declarator *declarator;
5831 cp_declarator *outer_declarator;
5832 const char *saved_message;
5833 tree type;
5835 /* The type-specifier sequence must not contain type definitions.
5836 (It cannot contain declarations of new types either, but if they
5837 are not definitions we will catch that because they are not
5838 complete.) */
5839 saved_message = parser->type_definition_forbidden_message;
5840 parser->type_definition_forbidden_message
5841 = G_("types may not be defined in a new-type-id");
5842 /* Parse the type-specifier-seq. */
5843 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
5844 /*is_trailing_return=*/false,
5845 &type_specifier_seq);
5846 /* Restore the old message. */
5847 parser->type_definition_forbidden_message = saved_message;
5848 /* Parse the new-declarator. */
5849 new_declarator = cp_parser_new_declarator_opt (parser);
5851 /* Determine the number of elements in the last array dimension, if
5852 any. */
5853 *nelts = NULL_TREE;
5854 /* Skip down to the last array dimension. */
5855 declarator = new_declarator;
5856 outer_declarator = NULL;
5857 while (declarator && (declarator->kind == cdk_pointer
5858 || declarator->kind == cdk_ptrmem))
5860 outer_declarator = declarator;
5861 declarator = declarator->declarator;
5863 while (declarator
5864 && declarator->kind == cdk_array
5865 && declarator->declarator
5866 && declarator->declarator->kind == cdk_array)
5868 outer_declarator = declarator;
5869 declarator = declarator->declarator;
5872 if (declarator && declarator->kind == cdk_array)
5874 *nelts = declarator->u.array.bounds;
5875 if (*nelts == error_mark_node)
5876 *nelts = integer_one_node;
5878 if (outer_declarator)
5879 outer_declarator->declarator = declarator->declarator;
5880 else
5881 new_declarator = NULL;
5884 type = groktypename (&type_specifier_seq, new_declarator, false);
5885 return type;
5888 /* Parse an (optional) new-declarator.
5890 new-declarator:
5891 ptr-operator new-declarator [opt]
5892 direct-new-declarator
5894 Returns the declarator. */
5896 static cp_declarator *
5897 cp_parser_new_declarator_opt (cp_parser* parser)
5899 enum tree_code code;
5900 tree type;
5901 cp_cv_quals cv_quals;
5903 /* We don't know if there's a ptr-operator next, or not. */
5904 cp_parser_parse_tentatively (parser);
5905 /* Look for a ptr-operator. */
5906 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5907 /* If that worked, look for more new-declarators. */
5908 if (cp_parser_parse_definitely (parser))
5910 cp_declarator *declarator;
5912 /* Parse another optional declarator. */
5913 declarator = cp_parser_new_declarator_opt (parser);
5915 return cp_parser_make_indirect_declarator
5916 (code, type, cv_quals, declarator);
5919 /* If the next token is a `[', there is a direct-new-declarator. */
5920 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5921 return cp_parser_direct_new_declarator (parser);
5923 return NULL;
5926 /* Parse a direct-new-declarator.
5928 direct-new-declarator:
5929 [ expression ]
5930 direct-new-declarator [constant-expression]
5934 static cp_declarator *
5935 cp_parser_direct_new_declarator (cp_parser* parser)
5937 cp_declarator *declarator = NULL;
5939 while (true)
5941 tree expression;
5943 /* Look for the opening `['. */
5944 cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5945 /* The first expression is not required to be constant. */
5946 if (!declarator)
5948 cp_token *token = cp_lexer_peek_token (parser->lexer);
5949 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5950 /* The standard requires that the expression have integral
5951 type. DR 74 adds enumeration types. We believe that the
5952 real intent is that these expressions be handled like the
5953 expression in a `switch' condition, which also allows
5954 classes with a single conversion to integral or
5955 enumeration type. */
5956 if (!processing_template_decl)
5958 expression
5959 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5960 expression,
5961 /*complain=*/true);
5962 if (!expression)
5964 error_at (token->location,
5965 "expression in new-declarator must have integral "
5966 "or enumeration type");
5967 expression = error_mark_node;
5971 /* But all the other expressions must be. */
5972 else
5973 expression
5974 = cp_parser_constant_expression (parser,
5975 /*allow_non_constant=*/false,
5976 NULL);
5977 /* Look for the closing `]'. */
5978 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5980 /* Add this bound to the declarator. */
5981 declarator = make_array_declarator (declarator, expression);
5983 /* If the next token is not a `[', then there are no more
5984 bounds. */
5985 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5986 break;
5989 return declarator;
5992 /* Parse a new-initializer.
5994 new-initializer:
5995 ( expression-list [opt] )
5996 braced-init-list
5998 Returns a representation of the expression-list. */
6000 static VEC(tree,gc) *
6001 cp_parser_new_initializer (cp_parser* parser)
6003 VEC(tree,gc) *expression_list;
6005 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6007 tree t;
6008 bool expr_non_constant_p;
6009 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6010 t = cp_parser_braced_list (parser, &expr_non_constant_p);
6011 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6012 expression_list = make_tree_vector_single (t);
6014 else
6015 expression_list = (cp_parser_parenthesized_expression_list
6016 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
6017 /*non_constant_p=*/NULL));
6019 return expression_list;
6022 /* Parse a delete-expression.
6024 delete-expression:
6025 :: [opt] delete cast-expression
6026 :: [opt] delete [ ] cast-expression
6028 Returns a representation of the expression. */
6030 static tree
6031 cp_parser_delete_expression (cp_parser* parser)
6033 bool global_scope_p;
6034 bool array_p;
6035 tree expression;
6037 /* Look for the optional `::' operator. */
6038 global_scope_p
6039 = (cp_parser_global_scope_opt (parser,
6040 /*current_scope_valid_p=*/false)
6041 != NULL_TREE);
6042 /* Look for the `delete' keyword. */
6043 cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
6044 /* See if the array syntax is in use. */
6045 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6047 /* Consume the `[' token. */
6048 cp_lexer_consume_token (parser->lexer);
6049 /* Look for the `]' token. */
6050 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
6051 /* Remember that this is the `[]' construct. */
6052 array_p = true;
6054 else
6055 array_p = false;
6057 /* Parse the cast-expression. */
6058 expression = cp_parser_simple_cast_expression (parser);
6060 /* A delete-expression may not appear in an integral constant
6061 expression. */
6062 if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
6063 return error_mark_node;
6065 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
6068 /* Returns true if TOKEN may start a cast-expression and false
6069 otherwise. */
6071 static bool
6072 cp_parser_token_starts_cast_expression (cp_token *token)
6074 switch (token->type)
6076 case CPP_COMMA:
6077 case CPP_SEMICOLON:
6078 case CPP_QUERY:
6079 case CPP_COLON:
6080 case CPP_CLOSE_SQUARE:
6081 case CPP_CLOSE_PAREN:
6082 case CPP_CLOSE_BRACE:
6083 case CPP_DOT:
6084 case CPP_DOT_STAR:
6085 case CPP_DEREF:
6086 case CPP_DEREF_STAR:
6087 case CPP_DIV:
6088 case CPP_MOD:
6089 case CPP_LSHIFT:
6090 case CPP_RSHIFT:
6091 case CPP_LESS:
6092 case CPP_GREATER:
6093 case CPP_LESS_EQ:
6094 case CPP_GREATER_EQ:
6095 case CPP_EQ_EQ:
6096 case CPP_NOT_EQ:
6097 case CPP_EQ:
6098 case CPP_MULT_EQ:
6099 case CPP_DIV_EQ:
6100 case CPP_MOD_EQ:
6101 case CPP_PLUS_EQ:
6102 case CPP_MINUS_EQ:
6103 case CPP_RSHIFT_EQ:
6104 case CPP_LSHIFT_EQ:
6105 case CPP_AND_EQ:
6106 case CPP_XOR_EQ:
6107 case CPP_OR_EQ:
6108 case CPP_XOR:
6109 case CPP_OR:
6110 case CPP_OR_OR:
6111 case CPP_EOF:
6112 return false;
6114 /* '[' may start a primary-expression in obj-c++. */
6115 case CPP_OPEN_SQUARE:
6116 return c_dialect_objc ();
6118 default:
6119 return true;
6123 /* Parse a cast-expression.
6125 cast-expression:
6126 unary-expression
6127 ( type-id ) cast-expression
6129 ADDRESS_P is true iff the unary-expression is appearing as the
6130 operand of the `&' operator. CAST_P is true if this expression is
6131 the target of a cast.
6133 Returns a representation of the expression. */
6135 static tree
6136 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6137 cp_id_kind * pidk)
6139 /* If it's a `(', then we might be looking at a cast. */
6140 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6142 tree type = NULL_TREE;
6143 tree expr = NULL_TREE;
6144 bool compound_literal_p;
6145 const char *saved_message;
6147 /* There's no way to know yet whether or not this is a cast.
6148 For example, `(int (3))' is a unary-expression, while `(int)
6149 3' is a cast. So, we resort to parsing tentatively. */
6150 cp_parser_parse_tentatively (parser);
6151 /* Types may not be defined in a cast. */
6152 saved_message = parser->type_definition_forbidden_message;
6153 parser->type_definition_forbidden_message
6154 = G_("types may not be defined in casts");
6155 /* Consume the `('. */
6156 cp_lexer_consume_token (parser->lexer);
6157 /* A very tricky bit is that `(struct S) { 3 }' is a
6158 compound-literal (which we permit in C++ as an extension).
6159 But, that construct is not a cast-expression -- it is a
6160 postfix-expression. (The reason is that `(struct S) { 3 }.i'
6161 is legal; if the compound-literal were a cast-expression,
6162 you'd need an extra set of parentheses.) But, if we parse
6163 the type-id, and it happens to be a class-specifier, then we
6164 will commit to the parse at that point, because we cannot
6165 undo the action that is done when creating a new class. So,
6166 then we cannot back up and do a postfix-expression.
6168 Therefore, we scan ahead to the closing `)', and check to see
6169 if the token after the `)' is a `{'. If so, we are not
6170 looking at a cast-expression.
6172 Save tokens so that we can put them back. */
6173 cp_lexer_save_tokens (parser->lexer);
6174 /* Skip tokens until the next token is a closing parenthesis.
6175 If we find the closing `)', and the next token is a `{', then
6176 we are looking at a compound-literal. */
6177 compound_literal_p
6178 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6179 /*consume_paren=*/true)
6180 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6181 /* Roll back the tokens we skipped. */
6182 cp_lexer_rollback_tokens (parser->lexer);
6183 /* If we were looking at a compound-literal, simulate an error
6184 so that the call to cp_parser_parse_definitely below will
6185 fail. */
6186 if (compound_literal_p)
6187 cp_parser_simulate_error (parser);
6188 else
6190 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6191 parser->in_type_id_in_expr_p = true;
6192 /* Look for the type-id. */
6193 type = cp_parser_type_id (parser);
6194 /* Look for the closing `)'. */
6195 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6196 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6199 /* Restore the saved message. */
6200 parser->type_definition_forbidden_message = saved_message;
6202 /* At this point this can only be either a cast or a
6203 parenthesized ctor such as `(T ())' that looks like a cast to
6204 function returning T. */
6205 if (!cp_parser_error_occurred (parser)
6206 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6207 (parser->lexer)))
6209 cp_parser_parse_definitely (parser);
6210 expr = cp_parser_cast_expression (parser,
6211 /*address_p=*/false,
6212 /*cast_p=*/true, pidk);
6214 /* Warn about old-style casts, if so requested. */
6215 if (warn_old_style_cast
6216 && !in_system_header
6217 && !VOID_TYPE_P (type)
6218 && current_lang_name != lang_name_c)
6219 warning (OPT_Wold_style_cast, "use of old-style cast");
6221 /* Only type conversions to integral or enumeration types
6222 can be used in constant-expressions. */
6223 if (!cast_valid_in_integral_constant_expression_p (type)
6224 && (cp_parser_non_integral_constant_expression
6225 (parser,
6226 "a cast to a type other than an integral or "
6227 "enumeration type")))
6228 return error_mark_node;
6230 /* Perform the cast. */
6231 expr = build_c_cast (input_location, type, expr);
6232 return expr;
6234 else
6235 cp_parser_abort_tentative_parse (parser);
6238 /* If we get here, then it's not a cast, so it must be a
6239 unary-expression. */
6240 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6243 /* Parse a binary expression of the general form:
6245 pm-expression:
6246 cast-expression
6247 pm-expression .* cast-expression
6248 pm-expression ->* cast-expression
6250 multiplicative-expression:
6251 pm-expression
6252 multiplicative-expression * pm-expression
6253 multiplicative-expression / pm-expression
6254 multiplicative-expression % pm-expression
6256 additive-expression:
6257 multiplicative-expression
6258 additive-expression + multiplicative-expression
6259 additive-expression - multiplicative-expression
6261 shift-expression:
6262 additive-expression
6263 shift-expression << additive-expression
6264 shift-expression >> additive-expression
6266 relational-expression:
6267 shift-expression
6268 relational-expression < shift-expression
6269 relational-expression > shift-expression
6270 relational-expression <= shift-expression
6271 relational-expression >= shift-expression
6273 GNU Extension:
6275 relational-expression:
6276 relational-expression <? shift-expression
6277 relational-expression >? shift-expression
6279 equality-expression:
6280 relational-expression
6281 equality-expression == relational-expression
6282 equality-expression != relational-expression
6284 and-expression:
6285 equality-expression
6286 and-expression & equality-expression
6288 exclusive-or-expression:
6289 and-expression
6290 exclusive-or-expression ^ and-expression
6292 inclusive-or-expression:
6293 exclusive-or-expression
6294 inclusive-or-expression | exclusive-or-expression
6296 logical-and-expression:
6297 inclusive-or-expression
6298 logical-and-expression && inclusive-or-expression
6300 logical-or-expression:
6301 logical-and-expression
6302 logical-or-expression || logical-and-expression
6304 All these are implemented with a single function like:
6306 binary-expression:
6307 simple-cast-expression
6308 binary-expression <token> binary-expression
6310 CAST_P is true if this expression is the target of a cast.
6312 The binops_by_token map is used to get the tree codes for each <token> type.
6313 binary-expressions are associated according to a precedence table. */
6315 #define TOKEN_PRECEDENCE(token) \
6316 (((token->type == CPP_GREATER \
6317 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6318 && !parser->greater_than_is_operator_p) \
6319 ? PREC_NOT_OPERATOR \
6320 : binops_by_token[token->type].prec)
6322 static tree
6323 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6324 bool no_toplevel_fold_p,
6325 enum cp_parser_prec prec,
6326 cp_id_kind * pidk)
6328 cp_parser_expression_stack stack;
6329 cp_parser_expression_stack_entry *sp = &stack[0];
6330 tree lhs, rhs;
6331 cp_token *token;
6332 enum tree_code tree_type, lhs_type, rhs_type;
6333 enum cp_parser_prec new_prec, lookahead_prec;
6334 bool overloaded_p;
6336 /* Parse the first expression. */
6337 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6338 lhs_type = ERROR_MARK;
6340 for (;;)
6342 /* Get an operator token. */
6343 token = cp_lexer_peek_token (parser->lexer);
6345 if (warn_cxx0x_compat
6346 && token->type == CPP_RSHIFT
6347 && !parser->greater_than_is_operator_p)
6349 if (warning_at (token->location, OPT_Wc__0x_compat,
6350 "%<>>%> operator will be treated as"
6351 " two right angle brackets in C++0x"))
6352 inform (token->location,
6353 "suggest parentheses around %<>>%> expression");
6356 new_prec = TOKEN_PRECEDENCE (token);
6358 /* Popping an entry off the stack means we completed a subexpression:
6359 - either we found a token which is not an operator (`>' where it is not
6360 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6361 will happen repeatedly;
6362 - or, we found an operator which has lower priority. This is the case
6363 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6364 parsing `3 * 4'. */
6365 if (new_prec <= prec)
6367 if (sp == stack)
6368 break;
6369 else
6370 goto pop;
6373 get_rhs:
6374 tree_type = binops_by_token[token->type].tree_type;
6376 /* We used the operator token. */
6377 cp_lexer_consume_token (parser->lexer);
6379 /* For "false && x" or "true || x", x will never be executed;
6380 disable warnings while evaluating it. */
6381 if (tree_type == TRUTH_ANDIF_EXPR)
6382 c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6383 else if (tree_type == TRUTH_ORIF_EXPR)
6384 c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6386 /* Extract another operand. It may be the RHS of this expression
6387 or the LHS of a new, higher priority expression. */
6388 rhs = cp_parser_simple_cast_expression (parser);
6389 rhs_type = ERROR_MARK;
6391 /* Get another operator token. Look up its precedence to avoid
6392 building a useless (immediately popped) stack entry for common
6393 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6394 token = cp_lexer_peek_token (parser->lexer);
6395 lookahead_prec = TOKEN_PRECEDENCE (token);
6396 if (lookahead_prec > new_prec)
6398 /* ... and prepare to parse the RHS of the new, higher priority
6399 expression. Since precedence levels on the stack are
6400 monotonically increasing, we do not have to care about
6401 stack overflows. */
6402 sp->prec = prec;
6403 sp->tree_type = tree_type;
6404 sp->lhs = lhs;
6405 sp->lhs_type = lhs_type;
6406 sp++;
6407 lhs = rhs;
6408 lhs_type = rhs_type;
6409 prec = new_prec;
6410 new_prec = lookahead_prec;
6411 goto get_rhs;
6413 pop:
6414 lookahead_prec = new_prec;
6415 /* If the stack is not empty, we have parsed into LHS the right side
6416 (`4' in the example above) of an expression we had suspended.
6417 We can use the information on the stack to recover the LHS (`3')
6418 from the stack together with the tree code (`MULT_EXPR'), and
6419 the precedence of the higher level subexpression
6420 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6421 which will be used to actually build the additive expression. */
6422 --sp;
6423 prec = sp->prec;
6424 tree_type = sp->tree_type;
6425 rhs = lhs;
6426 rhs_type = lhs_type;
6427 lhs = sp->lhs;
6428 lhs_type = sp->lhs_type;
6431 /* Undo the disabling of warnings done above. */
6432 if (tree_type == TRUTH_ANDIF_EXPR)
6433 c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6434 else if (tree_type == TRUTH_ORIF_EXPR)
6435 c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6437 overloaded_p = false;
6438 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6439 ERROR_MARK for everything that is not a binary expression.
6440 This makes warn_about_parentheses miss some warnings that
6441 involve unary operators. For unary expressions we should
6442 pass the correct tree_code unless the unary expression was
6443 surrounded by parentheses.
6445 if (no_toplevel_fold_p
6446 && lookahead_prec <= prec
6447 && sp == stack
6448 && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6449 lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6450 else
6451 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6452 &overloaded_p, tf_warning_or_error);
6453 lhs_type = tree_type;
6455 /* If the binary operator required the use of an overloaded operator,
6456 then this expression cannot be an integral constant-expression.
6457 An overloaded operator can be used even if both operands are
6458 otherwise permissible in an integral constant-expression if at
6459 least one of the operands is of enumeration type. */
6461 if (overloaded_p
6462 && (cp_parser_non_integral_constant_expression
6463 (parser, "calls to overloaded operators")))
6464 return error_mark_node;
6467 return lhs;
6471 /* Parse the `? expression : assignment-expression' part of a
6472 conditional-expression. The LOGICAL_OR_EXPR is the
6473 logical-or-expression that started the conditional-expression.
6474 Returns a representation of the entire conditional-expression.
6476 This routine is used by cp_parser_assignment_expression.
6478 ? expression : assignment-expression
6480 GNU Extensions:
6482 ? : assignment-expression */
6484 static tree
6485 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6487 tree expr;
6488 tree assignment_expr;
6490 /* Consume the `?' token. */
6491 cp_lexer_consume_token (parser->lexer);
6492 if (cp_parser_allow_gnu_extensions_p (parser)
6493 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6495 /* Implicit true clause. */
6496 expr = NULL_TREE;
6497 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6499 else
6501 /* Parse the expression. */
6502 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6503 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6504 c_inhibit_evaluation_warnings +=
6505 ((logical_or_expr == truthvalue_true_node)
6506 - (logical_or_expr == truthvalue_false_node));
6509 /* The next token should be a `:'. */
6510 cp_parser_require (parser, CPP_COLON, "%<:%>");
6511 /* Parse the assignment-expression. */
6512 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6513 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6515 /* Build the conditional-expression. */
6516 return build_x_conditional_expr (logical_or_expr,
6517 expr,
6518 assignment_expr,
6519 tf_warning_or_error);
6522 /* Parse an assignment-expression.
6524 assignment-expression:
6525 conditional-expression
6526 logical-or-expression assignment-operator assignment_expression
6527 throw-expression
6529 CAST_P is true if this expression is the target of a cast.
6531 Returns a representation for the expression. */
6533 static tree
6534 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6535 cp_id_kind * pidk)
6537 tree expr;
6539 /* If the next token is the `throw' keyword, then we're looking at
6540 a throw-expression. */
6541 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6542 expr = cp_parser_throw_expression (parser);
6543 /* Otherwise, it must be that we are looking at a
6544 logical-or-expression. */
6545 else
6547 /* Parse the binary expressions (logical-or-expression). */
6548 expr = cp_parser_binary_expression (parser, cast_p, false,
6549 PREC_NOT_OPERATOR, pidk);
6550 /* If the next token is a `?' then we're actually looking at a
6551 conditional-expression. */
6552 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6553 return cp_parser_question_colon_clause (parser, expr);
6554 else
6556 enum tree_code assignment_operator;
6558 /* If it's an assignment-operator, we're using the second
6559 production. */
6560 assignment_operator
6561 = cp_parser_assignment_operator_opt (parser);
6562 if (assignment_operator != ERROR_MARK)
6564 bool non_constant_p;
6566 /* Parse the right-hand side of the assignment. */
6567 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6569 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6570 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6572 /* An assignment may not appear in a
6573 constant-expression. */
6574 if (cp_parser_non_integral_constant_expression (parser,
6575 "an assignment"))
6576 return error_mark_node;
6577 /* Build the assignment expression. */
6578 expr = build_x_modify_expr (expr,
6579 assignment_operator,
6580 rhs,
6581 tf_warning_or_error);
6586 return expr;
6589 /* Parse an (optional) assignment-operator.
6591 assignment-operator: one of
6592 = *= /= %= += -= >>= <<= &= ^= |=
6594 GNU Extension:
6596 assignment-operator: one of
6597 <?= >?=
6599 If the next token is an assignment operator, the corresponding tree
6600 code is returned, and the token is consumed. For example, for
6601 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6602 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6603 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6604 operator, ERROR_MARK is returned. */
6606 static enum tree_code
6607 cp_parser_assignment_operator_opt (cp_parser* parser)
6609 enum tree_code op;
6610 cp_token *token;
6612 /* Peek at the next token. */
6613 token = cp_lexer_peek_token (parser->lexer);
6615 switch (token->type)
6617 case CPP_EQ:
6618 op = NOP_EXPR;
6619 break;
6621 case CPP_MULT_EQ:
6622 op = MULT_EXPR;
6623 break;
6625 case CPP_DIV_EQ:
6626 op = TRUNC_DIV_EXPR;
6627 break;
6629 case CPP_MOD_EQ:
6630 op = TRUNC_MOD_EXPR;
6631 break;
6633 case CPP_PLUS_EQ:
6634 op = PLUS_EXPR;
6635 break;
6637 case CPP_MINUS_EQ:
6638 op = MINUS_EXPR;
6639 break;
6641 case CPP_RSHIFT_EQ:
6642 op = RSHIFT_EXPR;
6643 break;
6645 case CPP_LSHIFT_EQ:
6646 op = LSHIFT_EXPR;
6647 break;
6649 case CPP_AND_EQ:
6650 op = BIT_AND_EXPR;
6651 break;
6653 case CPP_XOR_EQ:
6654 op = BIT_XOR_EXPR;
6655 break;
6657 case CPP_OR_EQ:
6658 op = BIT_IOR_EXPR;
6659 break;
6661 default:
6662 /* Nothing else is an assignment operator. */
6663 op = ERROR_MARK;
6666 /* If it was an assignment operator, consume it. */
6667 if (op != ERROR_MARK)
6668 cp_lexer_consume_token (parser->lexer);
6670 return op;
6673 /* Parse an expression.
6675 expression:
6676 assignment-expression
6677 expression , assignment-expression
6679 CAST_P is true if this expression is the target of a cast.
6681 Returns a representation of the expression. */
6683 static tree
6684 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6686 tree expression = NULL_TREE;
6688 while (true)
6690 tree assignment_expression;
6692 /* Parse the next assignment-expression. */
6693 assignment_expression
6694 = cp_parser_assignment_expression (parser, cast_p, pidk);
6695 /* If this is the first assignment-expression, we can just
6696 save it away. */
6697 if (!expression)
6698 expression = assignment_expression;
6699 else
6700 expression = build_x_compound_expr (expression,
6701 assignment_expression,
6702 tf_warning_or_error);
6703 /* If the next token is not a comma, then we are done with the
6704 expression. */
6705 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6706 break;
6707 /* Consume the `,'. */
6708 cp_lexer_consume_token (parser->lexer);
6709 /* A comma operator cannot appear in a constant-expression. */
6710 if (cp_parser_non_integral_constant_expression (parser,
6711 "a comma operator"))
6712 expression = error_mark_node;
6715 return expression;
6718 /* Parse a constant-expression.
6720 constant-expression:
6721 conditional-expression
6723 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6724 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6725 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6726 is false, NON_CONSTANT_P should be NULL. */
6728 static tree
6729 cp_parser_constant_expression (cp_parser* parser,
6730 bool allow_non_constant_p,
6731 bool *non_constant_p)
6733 bool saved_integral_constant_expression_p;
6734 bool saved_allow_non_integral_constant_expression_p;
6735 bool saved_non_integral_constant_expression_p;
6736 tree expression;
6738 /* It might seem that we could simply parse the
6739 conditional-expression, and then check to see if it were
6740 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6741 one that the compiler can figure out is constant, possibly after
6742 doing some simplifications or optimizations. The standard has a
6743 precise definition of constant-expression, and we must honor
6744 that, even though it is somewhat more restrictive.
6746 For example:
6748 int i[(2, 3)];
6750 is not a legal declaration, because `(2, 3)' is not a
6751 constant-expression. The `,' operator is forbidden in a
6752 constant-expression. However, GCC's constant-folding machinery
6753 will fold this operation to an INTEGER_CST for `3'. */
6755 /* Save the old settings. */
6756 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6757 saved_allow_non_integral_constant_expression_p
6758 = parser->allow_non_integral_constant_expression_p;
6759 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6760 /* We are now parsing a constant-expression. */
6761 parser->integral_constant_expression_p = true;
6762 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6763 parser->non_integral_constant_expression_p = false;
6764 /* Although the grammar says "conditional-expression", we parse an
6765 "assignment-expression", which also permits "throw-expression"
6766 and the use of assignment operators. In the case that
6767 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6768 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6769 actually essential that we look for an assignment-expression.
6770 For example, cp_parser_initializer_clauses uses this function to
6771 determine whether a particular assignment-expression is in fact
6772 constant. */
6773 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6774 /* Restore the old settings. */
6775 parser->integral_constant_expression_p
6776 = saved_integral_constant_expression_p;
6777 parser->allow_non_integral_constant_expression_p
6778 = saved_allow_non_integral_constant_expression_p;
6779 if (allow_non_constant_p)
6780 *non_constant_p = parser->non_integral_constant_expression_p;
6781 else if (parser->non_integral_constant_expression_p)
6782 expression = error_mark_node;
6783 parser->non_integral_constant_expression_p
6784 = saved_non_integral_constant_expression_p;
6786 return expression;
6789 /* Parse __builtin_offsetof.
6791 offsetof-expression:
6792 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6794 offsetof-member-designator:
6795 id-expression
6796 | offsetof-member-designator "." id-expression
6797 | offsetof-member-designator "[" expression "]"
6798 | offsetof-member-designator "->" id-expression */
6800 static tree
6801 cp_parser_builtin_offsetof (cp_parser *parser)
6803 int save_ice_p, save_non_ice_p;
6804 tree type, expr;
6805 cp_id_kind dummy;
6806 cp_token *token;
6808 /* We're about to accept non-integral-constant things, but will
6809 definitely yield an integral constant expression. Save and
6810 restore these values around our local parsing. */
6811 save_ice_p = parser->integral_constant_expression_p;
6812 save_non_ice_p = parser->non_integral_constant_expression_p;
6814 /* Consume the "__builtin_offsetof" token. */
6815 cp_lexer_consume_token (parser->lexer);
6816 /* Consume the opening `('. */
6817 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6818 /* Parse the type-id. */
6819 type = cp_parser_type_id (parser);
6820 /* Look for the `,'. */
6821 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6822 token = cp_lexer_peek_token (parser->lexer);
6824 /* Build the (type *)null that begins the traditional offsetof macro. */
6825 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6826 tf_warning_or_error);
6828 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6829 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6830 true, &dummy, token->location);
6831 while (true)
6833 token = cp_lexer_peek_token (parser->lexer);
6834 switch (token->type)
6836 case CPP_OPEN_SQUARE:
6837 /* offsetof-member-designator "[" expression "]" */
6838 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6839 break;
6841 case CPP_DEREF:
6842 /* offsetof-member-designator "->" identifier */
6843 expr = grok_array_decl (expr, integer_zero_node);
6844 /* FALLTHRU */
6846 case CPP_DOT:
6847 /* offsetof-member-designator "." identifier */
6848 cp_lexer_consume_token (parser->lexer);
6849 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
6850 expr, true, &dummy,
6851 token->location);
6852 break;
6854 case CPP_CLOSE_PAREN:
6855 /* Consume the ")" token. */
6856 cp_lexer_consume_token (parser->lexer);
6857 goto success;
6859 default:
6860 /* Error. We know the following require will fail, but
6861 that gives the proper error message. */
6862 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6863 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6864 expr = error_mark_node;
6865 goto failure;
6869 success:
6870 /* If we're processing a template, we can't finish the semantics yet.
6871 Otherwise we can fold the entire expression now. */
6872 if (processing_template_decl)
6873 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6874 else
6875 expr = finish_offsetof (expr);
6877 failure:
6878 parser->integral_constant_expression_p = save_ice_p;
6879 parser->non_integral_constant_expression_p = save_non_ice_p;
6881 return expr;
6884 /* Parse a trait expression. */
6886 static tree
6887 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6889 cp_trait_kind kind;
6890 tree type1, type2 = NULL_TREE;
6891 bool binary = false;
6892 cp_decl_specifier_seq decl_specs;
6894 switch (keyword)
6896 case RID_HAS_NOTHROW_ASSIGN:
6897 kind = CPTK_HAS_NOTHROW_ASSIGN;
6898 break;
6899 case RID_HAS_NOTHROW_CONSTRUCTOR:
6900 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6901 break;
6902 case RID_HAS_NOTHROW_COPY:
6903 kind = CPTK_HAS_NOTHROW_COPY;
6904 break;
6905 case RID_HAS_TRIVIAL_ASSIGN:
6906 kind = CPTK_HAS_TRIVIAL_ASSIGN;
6907 break;
6908 case RID_HAS_TRIVIAL_CONSTRUCTOR:
6909 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6910 break;
6911 case RID_HAS_TRIVIAL_COPY:
6912 kind = CPTK_HAS_TRIVIAL_COPY;
6913 break;
6914 case RID_HAS_TRIVIAL_DESTRUCTOR:
6915 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6916 break;
6917 case RID_HAS_VIRTUAL_DESTRUCTOR:
6918 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6919 break;
6920 case RID_IS_ABSTRACT:
6921 kind = CPTK_IS_ABSTRACT;
6922 break;
6923 case RID_IS_BASE_OF:
6924 kind = CPTK_IS_BASE_OF;
6925 binary = true;
6926 break;
6927 case RID_IS_CLASS:
6928 kind = CPTK_IS_CLASS;
6929 break;
6930 case RID_IS_CONVERTIBLE_TO:
6931 kind = CPTK_IS_CONVERTIBLE_TO;
6932 binary = true;
6933 break;
6934 case RID_IS_EMPTY:
6935 kind = CPTK_IS_EMPTY;
6936 break;
6937 case RID_IS_ENUM:
6938 kind = CPTK_IS_ENUM;
6939 break;
6940 case RID_IS_POD:
6941 kind = CPTK_IS_POD;
6942 break;
6943 case RID_IS_POLYMORPHIC:
6944 kind = CPTK_IS_POLYMORPHIC;
6945 break;
6946 case RID_IS_STD_LAYOUT:
6947 kind = CPTK_IS_STD_LAYOUT;
6948 break;
6949 case RID_IS_TRIVIAL:
6950 kind = CPTK_IS_TRIVIAL;
6951 break;
6952 case RID_IS_UNION:
6953 kind = CPTK_IS_UNION;
6954 break;
6955 default:
6956 gcc_unreachable ();
6959 /* Consume the token. */
6960 cp_lexer_consume_token (parser->lexer);
6962 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6964 type1 = cp_parser_type_id (parser);
6966 if (type1 == error_mark_node)
6967 return error_mark_node;
6969 /* Build a trivial decl-specifier-seq. */
6970 clear_decl_specs (&decl_specs);
6971 decl_specs.type = type1;
6973 /* Call grokdeclarator to figure out what type this is. */
6974 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6975 /*initialized=*/0, /*attrlist=*/NULL);
6977 if (binary)
6979 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6981 type2 = cp_parser_type_id (parser);
6983 if (type2 == error_mark_node)
6984 return error_mark_node;
6986 /* Build a trivial decl-specifier-seq. */
6987 clear_decl_specs (&decl_specs);
6988 decl_specs.type = type2;
6990 /* Call grokdeclarator to figure out what type this is. */
6991 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6992 /*initialized=*/0, /*attrlist=*/NULL);
6995 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6997 /* Complete the trait expression, which may mean either processing
6998 the trait expr now or saving it for template instantiation. */
6999 return finish_trait_expr (kind, type1, type2);
7002 /* Lambdas that appear in variable initializer or default argument scope
7003 get that in their mangling, so we need to record it. We might as well
7004 use the count for function and namespace scopes as well. */
7005 static GTY(()) tree lambda_scope;
7006 static GTY(()) int lambda_count;
7007 typedef struct GTY(()) tree_int
7009 tree t;
7010 int i;
7011 } tree_int;
7012 DEF_VEC_O(tree_int);
7013 DEF_VEC_ALLOC_O(tree_int,gc);
7014 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7016 static void
7017 start_lambda_scope (tree decl)
7019 tree_int ti;
7020 gcc_assert (decl);
7021 /* Once we're inside a function, we ignore other scopes and just push
7022 the function again so that popping works properly. */
7023 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7024 decl = current_function_decl;
7025 ti.t = lambda_scope;
7026 ti.i = lambda_count;
7027 VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7028 if (lambda_scope != decl)
7030 /* Don't reset the count if we're still in the same function. */
7031 lambda_scope = decl;
7032 lambda_count = 0;
7036 static void
7037 record_lambda_scope (tree lambda)
7039 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7040 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7043 static void
7044 finish_lambda_scope (void)
7046 tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7047 if (lambda_scope != p->t)
7049 lambda_scope = p->t;
7050 lambda_count = p->i;
7052 VEC_pop (tree_int, lambda_scope_stack);
7055 /* Parse a lambda expression.
7057 lambda-expression:
7058 lambda-introducer lambda-declarator [opt] compound-statement
7060 Returns a representation of the expression. */
7062 static tree
7063 cp_parser_lambda_expression (cp_parser* parser)
7065 tree lambda_expr = build_lambda_expr ();
7066 tree type;
7068 LAMBDA_EXPR_LOCATION (lambda_expr)
7069 = cp_lexer_peek_token (parser->lexer)->location;
7071 /* We may be in the middle of deferred access check. Disable
7072 it now. */
7073 push_deferring_access_checks (dk_no_deferred);
7075 cp_parser_lambda_introducer (parser, lambda_expr);
7077 type = begin_lambda_type (lambda_expr);
7079 record_lambda_scope (lambda_expr);
7081 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
7082 determine_visibility (TYPE_NAME (type));
7084 /* Now that we've started the type, add the capture fields for any
7085 explicit captures. */
7086 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
7089 /* Inside the class, surrounding template-parameter-lists do not apply. */
7090 unsigned int saved_num_template_parameter_lists
7091 = parser->num_template_parameter_lists;
7093 parser->num_template_parameter_lists = 0;
7095 /* By virtue of defining a local class, a lambda expression has access to
7096 the private variables of enclosing classes. */
7098 cp_parser_lambda_declarator_opt (parser, lambda_expr);
7100 cp_parser_lambda_body (parser, lambda_expr);
7102 /* The capture list was built up in reverse order; fix that now. */
7104 tree newlist = NULL_TREE;
7105 tree elt, next;
7107 for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7108 elt; elt = next)
7110 tree field = TREE_PURPOSE (elt);
7111 char *buf;
7113 next = TREE_CHAIN (elt);
7114 TREE_CHAIN (elt) = newlist;
7115 newlist = elt;
7117 /* Also add __ to the beginning of the field name so that code
7118 outside the lambda body can't see the captured name. We could
7119 just remove the name entirely, but this is more useful for
7120 debugging. */
7121 if (field == LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
7122 /* The 'this' capture already starts with __. */
7123 continue;
7125 buf = (char *) alloca (IDENTIFIER_LENGTH (DECL_NAME (field)) + 3);
7126 buf[1] = buf[0] = '_';
7127 memcpy (buf + 2, IDENTIFIER_POINTER (DECL_NAME (field)),
7128 IDENTIFIER_LENGTH (DECL_NAME (field)) + 1);
7129 DECL_NAME (field) = get_identifier (buf);
7131 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7134 maybe_add_lambda_conv_op (type);
7136 type = finish_struct (type, /*attributes=*/NULL_TREE);
7138 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7141 pop_deferring_access_checks ();
7143 return build_lambda_object (lambda_expr);
7146 /* Parse the beginning of a lambda expression.
7148 lambda-introducer:
7149 [ lambda-capture [opt] ]
7151 LAMBDA_EXPR is the current representation of the lambda expression. */
7153 static void
7154 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7156 /* Need commas after the first capture. */
7157 bool first = true;
7159 /* Eat the leading `['. */
7160 cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
7162 /* Record default capture mode. "[&" "[=" "[&," "[=," */
7163 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7164 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7165 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7166 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7167 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7169 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7171 cp_lexer_consume_token (parser->lexer);
7172 first = false;
7175 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7177 cp_token* capture_token;
7178 tree capture_id;
7179 tree capture_init_expr;
7180 cp_id_kind idk = CP_ID_KIND_NONE;
7181 bool explicit_init_p = false;
7183 enum capture_kind_type
7185 BY_COPY,
7186 BY_REFERENCE
7188 enum capture_kind_type capture_kind = BY_COPY;
7190 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7192 error ("expected end of capture-list");
7193 return;
7196 if (first)
7197 first = false;
7198 else
7199 cp_parser_require (parser, CPP_COMMA, "%<,%>");
7201 /* Possibly capture `this'. */
7202 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7204 cp_lexer_consume_token (parser->lexer);
7205 add_capture (lambda_expr,
7206 /*id=*/get_identifier ("__this"),
7207 /*initializer=*/finish_this_expr(),
7208 /*by_reference_p=*/false,
7209 explicit_init_p);
7210 continue;
7213 /* Remember whether we want to capture as a reference or not. */
7214 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7216 capture_kind = BY_REFERENCE;
7217 cp_lexer_consume_token (parser->lexer);
7220 /* Get the identifier. */
7221 capture_token = cp_lexer_peek_token (parser->lexer);
7222 capture_id = cp_parser_identifier (parser);
7224 if (capture_id == error_mark_node)
7225 /* Would be nice to have a cp_parser_skip_to_closing_x for general
7226 delimiters, but I modified this to stop on unnested ']' as well. It
7227 was already changed to stop on unnested '}', so the
7228 "closing_parenthesis" name is no more misleading with my change. */
7230 cp_parser_skip_to_closing_parenthesis (parser,
7231 /*recovering=*/true,
7232 /*or_comma=*/true,
7233 /*consume_paren=*/true);
7234 break;
7237 /* Find the initializer for this capture. */
7238 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7240 /* An explicit expression exists. */
7241 cp_lexer_consume_token (parser->lexer);
7242 pedwarn (input_location, OPT_pedantic,
7243 "ISO C++ does not allow initializers "
7244 "in lambda expression capture lists");
7245 capture_init_expr = cp_parser_assignment_expression (parser,
7246 /*cast_p=*/true,
7247 &idk);
7248 explicit_init_p = true;
7250 else
7252 const char* error_msg;
7254 /* Turn the identifier into an id-expression. */
7255 capture_init_expr
7256 = cp_parser_lookup_name
7257 (parser,
7258 capture_id,
7259 none_type,
7260 /*is_template=*/false,
7261 /*is_namespace=*/false,
7262 /*check_dependency=*/true,
7263 /*ambiguous_decls=*/NULL,
7264 capture_token->location);
7266 capture_init_expr
7267 = finish_id_expression
7268 (capture_id,
7269 capture_init_expr,
7270 parser->scope,
7271 &idk,
7272 /*integral_constant_expression_p=*/false,
7273 /*allow_non_integral_constant_expression_p=*/false,
7274 /*non_integral_constant_expression_p=*/NULL,
7275 /*template_p=*/false,
7276 /*done=*/true,
7277 /*address_p=*/false,
7278 /*template_arg_p=*/false,
7279 &error_msg,
7280 capture_token->location);
7283 if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7284 capture_init_expr
7285 = unqualified_name_lookup_error (capture_init_expr);
7287 add_capture (lambda_expr,
7288 capture_id,
7289 capture_init_expr,
7290 /*by_reference_p=*/capture_kind == BY_REFERENCE,
7291 explicit_init_p);
7294 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
7297 /* Parse the (optional) middle of a lambda expression.
7299 lambda-declarator:
7300 ( parameter-declaration-clause [opt] )
7301 attribute-specifier [opt]
7302 mutable [opt]
7303 exception-specification [opt]
7304 lambda-return-type-clause [opt]
7306 LAMBDA_EXPR is the current representation of the lambda expression. */
7308 static void
7309 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7311 /* 5.1.1.4 of the standard says:
7312 If a lambda-expression does not include a lambda-declarator, it is as if
7313 the lambda-declarator were ().
7314 This means an empty parameter list, no attributes, and no exception
7315 specification. */
7316 tree param_list = void_list_node;
7317 tree attributes = NULL_TREE;
7318 tree exception_spec = NULL_TREE;
7319 tree t;
7321 /* The lambda-declarator is optional, but must begin with an opening
7322 parenthesis if present. */
7323 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7325 cp_lexer_consume_token (parser->lexer);
7327 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7329 /* Parse parameters. */
7330 param_list = cp_parser_parameter_declaration_clause (parser);
7332 /* Default arguments shall not be specified in the
7333 parameter-declaration-clause of a lambda-declarator. */
7334 for (t = param_list; t; t = TREE_CHAIN (t))
7335 if (TREE_PURPOSE (t))
7336 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7337 "default argument specified for lambda parameter");
7339 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7341 attributes = cp_parser_attributes_opt (parser);
7343 /* Parse optional `mutable' keyword. */
7344 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7346 cp_lexer_consume_token (parser->lexer);
7347 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7350 /* Parse optional exception specification. */
7351 exception_spec = cp_parser_exception_specification_opt (parser);
7353 /* Parse optional trailing return type. */
7354 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7356 cp_lexer_consume_token (parser->lexer);
7357 LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7360 /* The function parameters must be in scope all the way until after the
7361 trailing-return-type in case of decltype. */
7362 for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
7363 pop_binding (DECL_NAME (t), t);
7365 leave_scope ();
7368 /* Create the function call operator.
7370 Messing with declarators like this is no uglier than building up the
7371 FUNCTION_DECL by hand, and this is less likely to get out of sync with
7372 other code. */
7374 cp_decl_specifier_seq return_type_specs;
7375 cp_declarator* declarator;
7376 tree fco;
7377 int quals;
7378 void *p;
7380 clear_decl_specs (&return_type_specs);
7381 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7382 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7383 else
7384 /* Maybe we will deduce the return type later, but we can use void
7385 as a placeholder return type anyways. */
7386 return_type_specs.type = void_type_node;
7388 p = obstack_alloc (&declarator_obstack, 0);
7390 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7391 sfk_none);
7393 quals = TYPE_UNQUALIFIED;
7394 if (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) == NULL_TREE
7395 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_NONE)
7397 /* A lambda with no captures has a static op() and a conversion op
7398 to function type. */
7399 if (LAMBDA_EXPR_MUTABLE_P (lambda_expr))
7400 error ("lambda expression with no captures declared mutable");
7401 return_type_specs.storage_class = sc_static;
7403 else if (!LAMBDA_EXPR_MUTABLE_P (lambda_expr))
7404 quals = TYPE_QUAL_CONST;
7405 declarator = make_call_declarator (declarator, param_list, quals,
7406 exception_spec,
7407 /*late_return_type=*/NULL_TREE);
7409 fco = grokmethod (&return_type_specs,
7410 declarator,
7411 attributes);
7412 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7413 DECL_ARTIFICIAL (fco) = 1;
7415 finish_member_declaration (fco);
7417 obstack_free (&declarator_obstack, p);
7421 /* Parse the body of a lambda expression, which is simply
7423 compound-statement
7425 but which requires special handling.
7426 LAMBDA_EXPR is the current representation of the lambda expression. */
7428 static void
7429 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7431 bool nested = (current_function_decl != NULL_TREE);
7432 if (nested)
7433 push_function_context ();
7435 /* Finish the function call operator
7436 - class_specifier
7437 + late_parsing_for_member
7438 + function_definition_after_declarator
7439 + ctor_initializer_opt_and_function_body */
7441 tree fco = lambda_function (lambda_expr);
7442 tree body;
7443 bool done = false;
7445 /* Let the front end know that we are going to be defining this
7446 function. */
7447 start_preparsed_function (fco,
7448 NULL_TREE,
7449 SF_PRE_PARSED | SF_INCLASS_INLINE);
7451 start_lambda_scope (fco);
7452 body = begin_function_body ();
7454 /* 5.1.1.4 of the standard says:
7455 If a lambda-expression does not include a trailing-return-type, it
7456 is as if the trailing-return-type denotes the following type:
7457 * if the compound-statement is of the form
7458 { return attribute-specifier [opt] expression ; }
7459 the type of the returned expression after lvalue-to-rvalue
7460 conversion (_conv.lval_ 4.1), array-to-pointer conversion
7461 (_conv.array_ 4.2), and function-to-pointer conversion
7462 (_conv.func_ 4.3);
7463 * otherwise, void. */
7465 /* In a lambda that has neither a lambda-return-type-clause
7466 nor a deducible form, errors should be reported for return statements
7467 in the body. Since we used void as the placeholder return type, parsing
7468 the body as usual will give such desired behavior. */
7469 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7470 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
7471 && cp_lexer_peek_nth_token (parser->lexer, 2)->keyword == RID_RETURN
7472 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_SEMICOLON)
7474 tree compound_stmt;
7475 tree expr = NULL_TREE;
7476 cp_id_kind idk = CP_ID_KIND_NONE;
7478 /* Parse tentatively in case there's more after the initial return
7479 statement. */
7480 cp_parser_parse_tentatively (parser);
7482 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7483 cp_parser_require_keyword (parser, RID_RETURN, "%<return%>");
7485 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7487 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7488 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7490 if (cp_parser_parse_definitely (parser))
7492 apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7494 compound_stmt = begin_compound_stmt (0);
7495 /* Will get error here if type not deduced yet. */
7496 finish_return_stmt (expr);
7497 finish_compound_stmt (compound_stmt);
7499 done = true;
7503 if (!done)
7505 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7506 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
7507 /* TODO: does begin_compound_stmt want BCS_FN_BODY?
7508 cp_parser_compound_stmt does not pass it. */
7509 cp_parser_function_body (parser);
7510 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
7513 finish_function_body (body);
7514 finish_lambda_scope ();
7516 /* Finish the function and generate code for it if necessary. */
7517 expand_or_defer_fn (finish_function (/*inline*/2));
7520 if (nested)
7521 pop_function_context();
7524 /* Statements [gram.stmt.stmt] */
7526 /* Parse a statement.
7528 statement:
7529 labeled-statement
7530 expression-statement
7531 compound-statement
7532 selection-statement
7533 iteration-statement
7534 jump-statement
7535 declaration-statement
7536 try-block
7538 IN_COMPOUND is true when the statement is nested inside a
7539 cp_parser_compound_statement; this matters for certain pragmas.
7541 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7542 is a (possibly labeled) if statement which is not enclosed in braces
7543 and has an else clause. This is used to implement -Wparentheses. */
7545 static void
7546 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
7547 bool in_compound, bool *if_p)
7549 tree statement;
7550 cp_token *token;
7551 location_t statement_location;
7553 restart:
7554 if (if_p != NULL)
7555 *if_p = false;
7556 /* There is no statement yet. */
7557 statement = NULL_TREE;
7558 /* Peek at the next token. */
7559 token = cp_lexer_peek_token (parser->lexer);
7560 /* Remember the location of the first token in the statement. */
7561 statement_location = token->location;
7562 /* If this is a keyword, then that will often determine what kind of
7563 statement we have. */
7564 if (token->type == CPP_KEYWORD)
7566 enum rid keyword = token->keyword;
7568 switch (keyword)
7570 case RID_CASE:
7571 case RID_DEFAULT:
7572 /* Looks like a labeled-statement with a case label.
7573 Parse the label, and then use tail recursion to parse
7574 the statement. */
7575 cp_parser_label_for_labeled_statement (parser);
7576 goto restart;
7578 case RID_IF:
7579 case RID_SWITCH:
7580 statement = cp_parser_selection_statement (parser, if_p);
7581 break;
7583 case RID_WHILE:
7584 case RID_DO:
7585 case RID_FOR:
7586 statement = cp_parser_iteration_statement (parser);
7587 break;
7589 case RID_BREAK:
7590 case RID_CONTINUE:
7591 case RID_RETURN:
7592 case RID_GOTO:
7593 statement = cp_parser_jump_statement (parser);
7594 break;
7596 /* Objective-C++ exception-handling constructs. */
7597 case RID_AT_TRY:
7598 case RID_AT_CATCH:
7599 case RID_AT_FINALLY:
7600 case RID_AT_SYNCHRONIZED:
7601 case RID_AT_THROW:
7602 statement = cp_parser_objc_statement (parser);
7603 break;
7605 case RID_TRY:
7606 statement = cp_parser_try_block (parser);
7607 break;
7609 case RID_NAMESPACE:
7610 /* This must be a namespace alias definition. */
7611 cp_parser_declaration_statement (parser);
7612 return;
7614 default:
7615 /* It might be a keyword like `int' that can start a
7616 declaration-statement. */
7617 break;
7620 else if (token->type == CPP_NAME)
7622 /* If the next token is a `:', then we are looking at a
7623 labeled-statement. */
7624 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7625 if (token->type == CPP_COLON)
7627 /* Looks like a labeled-statement with an ordinary label.
7628 Parse the label, and then use tail recursion to parse
7629 the statement. */
7630 cp_parser_label_for_labeled_statement (parser);
7631 goto restart;
7634 /* Anything that starts with a `{' must be a compound-statement. */
7635 else if (token->type == CPP_OPEN_BRACE)
7636 statement = cp_parser_compound_statement (parser, NULL, false);
7637 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
7638 a statement all its own. */
7639 else if (token->type == CPP_PRAGMA)
7641 /* Only certain OpenMP pragmas are attached to statements, and thus
7642 are considered statements themselves. All others are not. In
7643 the context of a compound, accept the pragma as a "statement" and
7644 return so that we can check for a close brace. Otherwise we
7645 require a real statement and must go back and read one. */
7646 if (in_compound)
7647 cp_parser_pragma (parser, pragma_compound);
7648 else if (!cp_parser_pragma (parser, pragma_stmt))
7649 goto restart;
7650 return;
7652 else if (token->type == CPP_EOF)
7654 cp_parser_error (parser, "expected statement");
7655 return;
7658 /* Everything else must be a declaration-statement or an
7659 expression-statement. Try for the declaration-statement
7660 first, unless we are looking at a `;', in which case we know that
7661 we have an expression-statement. */
7662 if (!statement)
7664 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7666 cp_parser_parse_tentatively (parser);
7667 /* Try to parse the declaration-statement. */
7668 cp_parser_declaration_statement (parser);
7669 /* If that worked, we're done. */
7670 if (cp_parser_parse_definitely (parser))
7671 return;
7673 /* Look for an expression-statement instead. */
7674 statement = cp_parser_expression_statement (parser, in_statement_expr);
7677 /* Set the line number for the statement. */
7678 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7679 SET_EXPR_LOCATION (statement, statement_location);
7682 /* Parse the label for a labeled-statement, i.e.
7684 identifier :
7685 case constant-expression :
7686 default :
7688 GNU Extension:
7689 case constant-expression ... constant-expression : statement
7691 When a label is parsed without errors, the label is added to the
7692 parse tree by the finish_* functions, so this function doesn't
7693 have to return the label. */
7695 static void
7696 cp_parser_label_for_labeled_statement (cp_parser* parser)
7698 cp_token *token;
7699 tree label = NULL_TREE;
7701 /* The next token should be an identifier. */
7702 token = cp_lexer_peek_token (parser->lexer);
7703 if (token->type != CPP_NAME
7704 && token->type != CPP_KEYWORD)
7706 cp_parser_error (parser, "expected labeled-statement");
7707 return;
7710 switch (token->keyword)
7712 case RID_CASE:
7714 tree expr, expr_hi;
7715 cp_token *ellipsis;
7717 /* Consume the `case' token. */
7718 cp_lexer_consume_token (parser->lexer);
7719 /* Parse the constant-expression. */
7720 expr = cp_parser_constant_expression (parser,
7721 /*allow_non_constant_p=*/false,
7722 NULL);
7724 ellipsis = cp_lexer_peek_token (parser->lexer);
7725 if (ellipsis->type == CPP_ELLIPSIS)
7727 /* Consume the `...' token. */
7728 cp_lexer_consume_token (parser->lexer);
7729 expr_hi =
7730 cp_parser_constant_expression (parser,
7731 /*allow_non_constant_p=*/false,
7732 NULL);
7733 /* We don't need to emit warnings here, as the common code
7734 will do this for us. */
7736 else
7737 expr_hi = NULL_TREE;
7739 if (parser->in_switch_statement_p)
7740 finish_case_label (token->location, expr, expr_hi);
7741 else
7742 error_at (token->location,
7743 "case label %qE not within a switch statement",
7744 expr);
7746 break;
7748 case RID_DEFAULT:
7749 /* Consume the `default' token. */
7750 cp_lexer_consume_token (parser->lexer);
7752 if (parser->in_switch_statement_p)
7753 finish_case_label (token->location, NULL_TREE, NULL_TREE);
7754 else
7755 error_at (token->location, "case label not within a switch statement");
7756 break;
7758 default:
7759 /* Anything else must be an ordinary label. */
7760 label = finish_label_stmt (cp_parser_identifier (parser));
7761 break;
7764 /* Require the `:' token. */
7765 cp_parser_require (parser, CPP_COLON, "%<:%>");
7767 /* An ordinary label may optionally be followed by attributes.
7768 However, this is only permitted if the attributes are then
7769 followed by a semicolon. This is because, for backward
7770 compatibility, when parsing
7771 lab: __attribute__ ((unused)) int i;
7772 we want the attribute to attach to "i", not "lab". */
7773 if (label != NULL_TREE
7774 && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
7776 tree attrs;
7778 cp_parser_parse_tentatively (parser);
7779 attrs = cp_parser_attributes_opt (parser);
7780 if (attrs == NULL_TREE
7781 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7782 cp_parser_abort_tentative_parse (parser);
7783 else if (!cp_parser_parse_definitely (parser))
7785 else
7786 cplus_decl_attributes (&label, attrs, 0);
7790 /* Parse an expression-statement.
7792 expression-statement:
7793 expression [opt] ;
7795 Returns the new EXPR_STMT -- or NULL_TREE if the expression
7796 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
7797 indicates whether this expression-statement is part of an
7798 expression statement. */
7800 static tree
7801 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
7803 tree statement = NULL_TREE;
7804 cp_token *token = cp_lexer_peek_token (parser->lexer);
7806 /* If the next token is a ';', then there is no expression
7807 statement. */
7808 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7809 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7811 /* Give a helpful message for "A<T>::type t;" and the like. */
7812 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
7813 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
7815 if (TREE_CODE (statement) == SCOPE_REF)
7816 error_at (token->location, "need %<typename%> before %qE because "
7817 "%qT is a dependent scope",
7818 statement, TREE_OPERAND (statement, 0));
7819 else if (is_overloaded_fn (statement)
7820 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
7822 /* A::A a; */
7823 tree fn = get_first_fn (statement);
7824 error_at (token->location,
7825 "%<%T::%D%> names the constructor, not the type",
7826 DECL_CONTEXT (fn), DECL_NAME (fn));
7830 /* Consume the final `;'. */
7831 cp_parser_consume_semicolon_at_end_of_statement (parser);
7833 if (in_statement_expr
7834 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
7835 /* This is the final expression statement of a statement
7836 expression. */
7837 statement = finish_stmt_expr_expr (statement, in_statement_expr);
7838 else if (statement)
7839 statement = finish_expr_stmt (statement);
7840 else
7841 finish_stmt ();
7843 return statement;
7846 /* Parse a compound-statement.
7848 compound-statement:
7849 { statement-seq [opt] }
7851 GNU extension:
7853 compound-statement:
7854 { label-declaration-seq [opt] statement-seq [opt] }
7856 label-declaration-seq:
7857 label-declaration
7858 label-declaration-seq label-declaration
7860 Returns a tree representing the statement. */
7862 static tree
7863 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7864 bool in_try)
7866 tree compound_stmt;
7868 /* Consume the `{'. */
7869 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7870 return error_mark_node;
7871 /* Begin the compound-statement. */
7872 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7873 /* If the next keyword is `__label__' we have a label declaration. */
7874 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7875 cp_parser_label_declaration (parser);
7876 /* Parse an (optional) statement-seq. */
7877 cp_parser_statement_seq_opt (parser, in_statement_expr);
7878 /* Finish the compound-statement. */
7879 finish_compound_stmt (compound_stmt);
7880 /* Consume the `}'. */
7881 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7883 return compound_stmt;
7886 /* Parse an (optional) statement-seq.
7888 statement-seq:
7889 statement
7890 statement-seq [opt] statement */
7892 static void
7893 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7895 /* Scan statements until there aren't any more. */
7896 while (true)
7898 cp_token *token = cp_lexer_peek_token (parser->lexer);
7900 /* If we're looking at a `}', then we've run out of statements. */
7901 if (token->type == CPP_CLOSE_BRACE
7902 || token->type == CPP_EOF
7903 || token->type == CPP_PRAGMA_EOL)
7904 break;
7906 /* If we are in a compound statement and find 'else' then
7907 something went wrong. */
7908 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7910 if (parser->in_statement & IN_IF_STMT)
7911 break;
7912 else
7914 token = cp_lexer_consume_token (parser->lexer);
7915 error_at (token->location, "%<else%> without a previous %<if%>");
7919 /* Parse the statement. */
7920 cp_parser_statement (parser, in_statement_expr, true, NULL);
7924 /* Parse a selection-statement.
7926 selection-statement:
7927 if ( condition ) statement
7928 if ( condition ) statement else statement
7929 switch ( condition ) statement
7931 Returns the new IF_STMT or SWITCH_STMT.
7933 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7934 is a (possibly labeled) if statement which is not enclosed in
7935 braces and has an else clause. This is used to implement
7936 -Wparentheses. */
7938 static tree
7939 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7941 cp_token *token;
7942 enum rid keyword;
7944 if (if_p != NULL)
7945 *if_p = false;
7947 /* Peek at the next token. */
7948 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7950 /* See what kind of keyword it is. */
7951 keyword = token->keyword;
7952 switch (keyword)
7954 case RID_IF:
7955 case RID_SWITCH:
7957 tree statement;
7958 tree condition;
7960 /* Look for the `('. */
7961 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7963 cp_parser_skip_to_end_of_statement (parser);
7964 return error_mark_node;
7967 /* Begin the selection-statement. */
7968 if (keyword == RID_IF)
7969 statement = begin_if_stmt ();
7970 else
7971 statement = begin_switch_stmt ();
7973 /* Parse the condition. */
7974 condition = cp_parser_condition (parser);
7975 /* Look for the `)'. */
7976 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7977 cp_parser_skip_to_closing_parenthesis (parser, true, false,
7978 /*consume_paren=*/true);
7980 if (keyword == RID_IF)
7982 bool nested_if;
7983 unsigned char in_statement;
7985 /* Add the condition. */
7986 finish_if_stmt_cond (condition, statement);
7988 /* Parse the then-clause. */
7989 in_statement = parser->in_statement;
7990 parser->in_statement |= IN_IF_STMT;
7991 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7993 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7994 add_stmt (build_empty_stmt (loc));
7995 cp_lexer_consume_token (parser->lexer);
7996 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
7997 warning_at (loc, OPT_Wempty_body, "suggest braces around "
7998 "empty body in an %<if%> statement");
7999 nested_if = false;
8001 else
8002 cp_parser_implicitly_scoped_statement (parser, &nested_if);
8003 parser->in_statement = in_statement;
8005 finish_then_clause (statement);
8007 /* If the next token is `else', parse the else-clause. */
8008 if (cp_lexer_next_token_is_keyword (parser->lexer,
8009 RID_ELSE))
8011 /* Consume the `else' keyword. */
8012 cp_lexer_consume_token (parser->lexer);
8013 begin_else_clause (statement);
8014 /* Parse the else-clause. */
8015 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8017 location_t loc;
8018 loc = cp_lexer_peek_token (parser->lexer)->location;
8019 warning_at (loc,
8020 OPT_Wempty_body, "suggest braces around "
8021 "empty body in an %<else%> statement");
8022 add_stmt (build_empty_stmt (loc));
8023 cp_lexer_consume_token (parser->lexer);
8025 else
8026 cp_parser_implicitly_scoped_statement (parser, NULL);
8028 finish_else_clause (statement);
8030 /* If we are currently parsing a then-clause, then
8031 IF_P will not be NULL. We set it to true to
8032 indicate that this if statement has an else clause.
8033 This may trigger the Wparentheses warning below
8034 when we get back up to the parent if statement. */
8035 if (if_p != NULL)
8036 *if_p = true;
8038 else
8040 /* This if statement does not have an else clause. If
8041 NESTED_IF is true, then the then-clause is an if
8042 statement which does have an else clause. We warn
8043 about the potential ambiguity. */
8044 if (nested_if)
8045 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
8046 "suggest explicit braces to avoid ambiguous"
8047 " %<else%>");
8050 /* Now we're all done with the if-statement. */
8051 finish_if_stmt (statement);
8053 else
8055 bool in_switch_statement_p;
8056 unsigned char in_statement;
8058 /* Add the condition. */
8059 finish_switch_cond (condition, statement);
8061 /* Parse the body of the switch-statement. */
8062 in_switch_statement_p = parser->in_switch_statement_p;
8063 in_statement = parser->in_statement;
8064 parser->in_switch_statement_p = true;
8065 parser->in_statement |= IN_SWITCH_STMT;
8066 cp_parser_implicitly_scoped_statement (parser, NULL);
8067 parser->in_switch_statement_p = in_switch_statement_p;
8068 parser->in_statement = in_statement;
8070 /* Now we're all done with the switch-statement. */
8071 finish_switch_stmt (statement);
8074 return statement;
8076 break;
8078 default:
8079 cp_parser_error (parser, "expected selection-statement");
8080 return error_mark_node;
8084 /* Parse a condition.
8086 condition:
8087 expression
8088 type-specifier-seq declarator = initializer-clause
8089 type-specifier-seq declarator braced-init-list
8091 GNU Extension:
8093 condition:
8094 type-specifier-seq declarator asm-specification [opt]
8095 attributes [opt] = assignment-expression
8097 Returns the expression that should be tested. */
8099 static tree
8100 cp_parser_condition (cp_parser* parser)
8102 cp_decl_specifier_seq type_specifiers;
8103 const char *saved_message;
8105 /* Try the declaration first. */
8106 cp_parser_parse_tentatively (parser);
8107 /* New types are not allowed in the type-specifier-seq for a
8108 condition. */
8109 saved_message = parser->type_definition_forbidden_message;
8110 parser->type_definition_forbidden_message
8111 = G_("types may not be defined in conditions");
8112 /* Parse the type-specifier-seq. */
8113 cp_parser_type_specifier_seq (parser, /*is_declaration==*/true,
8114 /*is_trailing_return=*/false,
8115 &type_specifiers);
8116 /* Restore the saved message. */
8117 parser->type_definition_forbidden_message = saved_message;
8118 /* If all is well, we might be looking at a declaration. */
8119 if (!cp_parser_error_occurred (parser))
8121 tree decl;
8122 tree asm_specification;
8123 tree attributes;
8124 cp_declarator *declarator;
8125 tree initializer = NULL_TREE;
8127 /* Parse the declarator. */
8128 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8129 /*ctor_dtor_or_conv_p=*/NULL,
8130 /*parenthesized_p=*/NULL,
8131 /*member_p=*/false);
8132 /* Parse the attributes. */
8133 attributes = cp_parser_attributes_opt (parser);
8134 /* Parse the asm-specification. */
8135 asm_specification = cp_parser_asm_specification_opt (parser);
8136 /* If the next token is not an `=' or '{', then we might still be
8137 looking at an expression. For example:
8139 if (A(a).x)
8141 looks like a decl-specifier-seq and a declarator -- but then
8142 there is no `=', so this is an expression. */
8143 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8144 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8145 cp_parser_simulate_error (parser);
8147 /* If we did see an `=' or '{', then we are looking at a declaration
8148 for sure. */
8149 if (cp_parser_parse_definitely (parser))
8151 tree pushed_scope;
8152 bool non_constant_p;
8153 bool flags = LOOKUP_ONLYCONVERTING;
8155 /* Create the declaration. */
8156 decl = start_decl (declarator, &type_specifiers,
8157 /*initialized_p=*/true,
8158 attributes, /*prefix_attributes=*/NULL_TREE,
8159 &pushed_scope);
8161 /* Parse the initializer. */
8162 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8164 initializer = cp_parser_braced_list (parser, &non_constant_p);
8165 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8166 flags = 0;
8168 else
8170 /* Consume the `='. */
8171 cp_parser_require (parser, CPP_EQ, "%<=%>");
8172 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8174 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
8175 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8177 if (!non_constant_p)
8178 initializer = fold_non_dependent_expr (initializer);
8180 /* Process the initializer. */
8181 cp_finish_decl (decl,
8182 initializer, !non_constant_p,
8183 asm_specification,
8184 flags);
8186 if (pushed_scope)
8187 pop_scope (pushed_scope);
8189 return convert_from_reference (decl);
8192 /* If we didn't even get past the declarator successfully, we are
8193 definitely not looking at a declaration. */
8194 else
8195 cp_parser_abort_tentative_parse (parser);
8197 /* Otherwise, we are looking at an expression. */
8198 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
8201 /* Parse an iteration-statement.
8203 iteration-statement:
8204 while ( condition ) statement
8205 do statement while ( expression ) ;
8206 for ( for-init-statement condition [opt] ; expression [opt] )
8207 statement
8209 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
8211 static tree
8212 cp_parser_iteration_statement (cp_parser* parser)
8214 cp_token *token;
8215 enum rid keyword;
8216 tree statement;
8217 unsigned char in_statement;
8219 /* Peek at the next token. */
8220 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
8221 if (!token)
8222 return error_mark_node;
8224 /* Remember whether or not we are already within an iteration
8225 statement. */
8226 in_statement = parser->in_statement;
8228 /* See what kind of keyword it is. */
8229 keyword = token->keyword;
8230 switch (keyword)
8232 case RID_WHILE:
8234 tree condition;
8236 /* Begin the while-statement. */
8237 statement = begin_while_stmt ();
8238 /* Look for the `('. */
8239 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8240 /* Parse the condition. */
8241 condition = cp_parser_condition (parser);
8242 finish_while_stmt_cond (condition, statement);
8243 /* Look for the `)'. */
8244 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8245 /* Parse the dependent statement. */
8246 parser->in_statement = IN_ITERATION_STMT;
8247 cp_parser_already_scoped_statement (parser);
8248 parser->in_statement = in_statement;
8249 /* We're done with the while-statement. */
8250 finish_while_stmt (statement);
8252 break;
8254 case RID_DO:
8256 tree expression;
8258 /* Begin the do-statement. */
8259 statement = begin_do_stmt ();
8260 /* Parse the body of the do-statement. */
8261 parser->in_statement = IN_ITERATION_STMT;
8262 cp_parser_implicitly_scoped_statement (parser, NULL);
8263 parser->in_statement = in_statement;
8264 finish_do_body (statement);
8265 /* Look for the `while' keyword. */
8266 cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
8267 /* Look for the `('. */
8268 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8269 /* Parse the expression. */
8270 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8271 /* We're done with the do-statement. */
8272 finish_do_stmt (expression, statement);
8273 /* Look for the `)'. */
8274 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8275 /* Look for the `;'. */
8276 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8278 break;
8280 case RID_FOR:
8282 tree condition = NULL_TREE;
8283 tree expression = NULL_TREE;
8285 /* Begin the for-statement. */
8286 statement = begin_for_stmt ();
8287 /* Look for the `('. */
8288 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8289 /* Parse the initialization. */
8290 cp_parser_for_init_statement (parser);
8291 finish_for_init_stmt (statement);
8293 /* If there's a condition, process it. */
8294 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8295 condition = cp_parser_condition (parser);
8296 finish_for_cond (condition, statement);
8297 /* Look for the `;'. */
8298 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8300 /* If there's an expression, process it. */
8301 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8302 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8303 finish_for_expr (expression, statement);
8304 /* Look for the `)'. */
8305 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8307 /* Parse the body of the for-statement. */
8308 parser->in_statement = IN_ITERATION_STMT;
8309 cp_parser_already_scoped_statement (parser);
8310 parser->in_statement = in_statement;
8312 /* We're done with the for-statement. */
8313 finish_for_stmt (statement);
8315 break;
8317 default:
8318 cp_parser_error (parser, "expected iteration-statement");
8319 statement = error_mark_node;
8320 break;
8323 return statement;
8326 /* Parse a for-init-statement.
8328 for-init-statement:
8329 expression-statement
8330 simple-declaration */
8332 static void
8333 cp_parser_for_init_statement (cp_parser* parser)
8335 /* If the next token is a `;', then we have an empty
8336 expression-statement. Grammatically, this is also a
8337 simple-declaration, but an invalid one, because it does not
8338 declare anything. Therefore, if we did not handle this case
8339 specially, we would issue an error message about an invalid
8340 declaration. */
8341 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8343 /* We're going to speculatively look for a declaration, falling back
8344 to an expression, if necessary. */
8345 cp_parser_parse_tentatively (parser);
8346 /* Parse the declaration. */
8347 cp_parser_simple_declaration (parser,
8348 /*function_definition_allowed_p=*/false);
8349 /* If the tentative parse failed, then we shall need to look for an
8350 expression-statement. */
8351 if (cp_parser_parse_definitely (parser))
8352 return;
8355 cp_parser_expression_statement (parser, NULL_TREE);
8358 /* Parse a jump-statement.
8360 jump-statement:
8361 break ;
8362 continue ;
8363 return expression [opt] ;
8364 return braced-init-list ;
8365 goto identifier ;
8367 GNU extension:
8369 jump-statement:
8370 goto * expression ;
8372 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
8374 static tree
8375 cp_parser_jump_statement (cp_parser* parser)
8377 tree statement = error_mark_node;
8378 cp_token *token;
8379 enum rid keyword;
8380 unsigned char in_statement;
8382 /* Peek at the next token. */
8383 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
8384 if (!token)
8385 return error_mark_node;
8387 /* See what kind of keyword it is. */
8388 keyword = token->keyword;
8389 switch (keyword)
8391 case RID_BREAK:
8392 in_statement = parser->in_statement & ~IN_IF_STMT;
8393 switch (in_statement)
8395 case 0:
8396 error_at (token->location, "break statement not within loop or switch");
8397 break;
8398 default:
8399 gcc_assert ((in_statement & IN_SWITCH_STMT)
8400 || in_statement == IN_ITERATION_STMT);
8401 statement = finish_break_stmt ();
8402 break;
8403 case IN_OMP_BLOCK:
8404 error_at (token->location, "invalid exit from OpenMP structured block");
8405 break;
8406 case IN_OMP_FOR:
8407 error_at (token->location, "break statement used with OpenMP for loop");
8408 break;
8410 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8411 break;
8413 case RID_CONTINUE:
8414 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
8416 case 0:
8417 error_at (token->location, "continue statement not within a loop");
8418 break;
8419 case IN_ITERATION_STMT:
8420 case IN_OMP_FOR:
8421 statement = finish_continue_stmt ();
8422 break;
8423 case IN_OMP_BLOCK:
8424 error_at (token->location, "invalid exit from OpenMP structured block");
8425 break;
8426 default:
8427 gcc_unreachable ();
8429 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8430 break;
8432 case RID_RETURN:
8434 tree expr;
8435 bool expr_non_constant_p;
8437 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8439 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8440 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8442 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8443 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8444 else
8445 /* If the next token is a `;', then there is no
8446 expression. */
8447 expr = NULL_TREE;
8448 /* Build the return-statement. */
8449 statement = finish_return_stmt (expr);
8450 /* Look for the final `;'. */
8451 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8453 break;
8455 case RID_GOTO:
8456 /* Create the goto-statement. */
8457 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
8459 /* Issue a warning about this use of a GNU extension. */
8460 pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
8461 /* Consume the '*' token. */
8462 cp_lexer_consume_token (parser->lexer);
8463 /* Parse the dependent expression. */
8464 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
8466 else
8467 finish_goto_stmt (cp_parser_identifier (parser));
8468 /* Look for the final `;'. */
8469 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8470 break;
8472 default:
8473 cp_parser_error (parser, "expected jump-statement");
8474 break;
8477 return statement;
8480 /* Parse a declaration-statement.
8482 declaration-statement:
8483 block-declaration */
8485 static void
8486 cp_parser_declaration_statement (cp_parser* parser)
8488 void *p;
8490 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
8491 p = obstack_alloc (&declarator_obstack, 0);
8493 /* Parse the block-declaration. */
8494 cp_parser_block_declaration (parser, /*statement_p=*/true);
8496 /* Free any declarators allocated. */
8497 obstack_free (&declarator_obstack, p);
8499 /* Finish off the statement. */
8500 finish_stmt ();
8503 /* Some dependent statements (like `if (cond) statement'), are
8504 implicitly in their own scope. In other words, if the statement is
8505 a single statement (as opposed to a compound-statement), it is
8506 none-the-less treated as if it were enclosed in braces. Any
8507 declarations appearing in the dependent statement are out of scope
8508 after control passes that point. This function parses a statement,
8509 but ensures that is in its own scope, even if it is not a
8510 compound-statement.
8512 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8513 is a (possibly labeled) if statement which is not enclosed in
8514 braces and has an else clause. This is used to implement
8515 -Wparentheses.
8517 Returns the new statement. */
8519 static tree
8520 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
8522 tree statement;
8524 if (if_p != NULL)
8525 *if_p = false;
8527 /* Mark if () ; with a special NOP_EXPR. */
8528 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8530 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8531 cp_lexer_consume_token (parser->lexer);
8532 statement = add_stmt (build_empty_stmt (loc));
8534 /* if a compound is opened, we simply parse the statement directly. */
8535 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8536 statement = cp_parser_compound_statement (parser, NULL, false);
8537 /* If the token is not a `{', then we must take special action. */
8538 else
8540 /* Create a compound-statement. */
8541 statement = begin_compound_stmt (0);
8542 /* Parse the dependent-statement. */
8543 cp_parser_statement (parser, NULL_TREE, false, if_p);
8544 /* Finish the dummy compound-statement. */
8545 finish_compound_stmt (statement);
8548 /* Return the statement. */
8549 return statement;
8552 /* For some dependent statements (like `while (cond) statement'), we
8553 have already created a scope. Therefore, even if the dependent
8554 statement is a compound-statement, we do not want to create another
8555 scope. */
8557 static void
8558 cp_parser_already_scoped_statement (cp_parser* parser)
8560 /* If the token is a `{', then we must take special action. */
8561 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8562 cp_parser_statement (parser, NULL_TREE, false, NULL);
8563 else
8565 /* Avoid calling cp_parser_compound_statement, so that we
8566 don't create a new scope. Do everything else by hand. */
8567 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
8568 /* If the next keyword is `__label__' we have a label declaration. */
8569 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8570 cp_parser_label_declaration (parser);
8571 /* Parse an (optional) statement-seq. */
8572 cp_parser_statement_seq_opt (parser, NULL_TREE);
8573 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8577 /* Declarations [gram.dcl.dcl] */
8579 /* Parse an optional declaration-sequence.
8581 declaration-seq:
8582 declaration
8583 declaration-seq declaration */
8585 static void
8586 cp_parser_declaration_seq_opt (cp_parser* parser)
8588 while (true)
8590 cp_token *token;
8592 token = cp_lexer_peek_token (parser->lexer);
8594 if (token->type == CPP_CLOSE_BRACE
8595 || token->type == CPP_EOF
8596 || token->type == CPP_PRAGMA_EOL)
8597 break;
8599 if (token->type == CPP_SEMICOLON)
8601 /* A declaration consisting of a single semicolon is
8602 invalid. Allow it unless we're being pedantic. */
8603 cp_lexer_consume_token (parser->lexer);
8604 if (!in_system_header)
8605 pedwarn (input_location, OPT_pedantic, "extra %<;%>");
8606 continue;
8609 /* If we're entering or exiting a region that's implicitly
8610 extern "C", modify the lang context appropriately. */
8611 if (!parser->implicit_extern_c && token->implicit_extern_c)
8613 push_lang_context (lang_name_c);
8614 parser->implicit_extern_c = true;
8616 else if (parser->implicit_extern_c && !token->implicit_extern_c)
8618 pop_lang_context ();
8619 parser->implicit_extern_c = false;
8622 if (token->type == CPP_PRAGMA)
8624 /* A top-level declaration can consist solely of a #pragma.
8625 A nested declaration cannot, so this is done here and not
8626 in cp_parser_declaration. (A #pragma at block scope is
8627 handled in cp_parser_statement.) */
8628 cp_parser_pragma (parser, pragma_external);
8629 continue;
8632 /* Parse the declaration itself. */
8633 cp_parser_declaration (parser);
8637 /* Parse a declaration.
8639 declaration:
8640 block-declaration
8641 function-definition
8642 template-declaration
8643 explicit-instantiation
8644 explicit-specialization
8645 linkage-specification
8646 namespace-definition
8648 GNU extension:
8650 declaration:
8651 __extension__ declaration */
8653 static void
8654 cp_parser_declaration (cp_parser* parser)
8656 cp_token token1;
8657 cp_token token2;
8658 int saved_pedantic;
8659 void *p;
8661 /* Check for the `__extension__' keyword. */
8662 if (cp_parser_extension_opt (parser, &saved_pedantic))
8664 /* Parse the qualified declaration. */
8665 cp_parser_declaration (parser);
8666 /* Restore the PEDANTIC flag. */
8667 pedantic = saved_pedantic;
8669 return;
8672 /* Try to figure out what kind of declaration is present. */
8673 token1 = *cp_lexer_peek_token (parser->lexer);
8675 if (token1.type != CPP_EOF)
8676 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
8677 else
8679 token2.type = CPP_EOF;
8680 token2.keyword = RID_MAX;
8683 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
8684 p = obstack_alloc (&declarator_obstack, 0);
8686 /* If the next token is `extern' and the following token is a string
8687 literal, then we have a linkage specification. */
8688 if (token1.keyword == RID_EXTERN
8689 && cp_parser_is_string_literal (&token2))
8690 cp_parser_linkage_specification (parser);
8691 /* If the next token is `template', then we have either a template
8692 declaration, an explicit instantiation, or an explicit
8693 specialization. */
8694 else if (token1.keyword == RID_TEMPLATE)
8696 /* `template <>' indicates a template specialization. */
8697 if (token2.type == CPP_LESS
8698 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
8699 cp_parser_explicit_specialization (parser);
8700 /* `template <' indicates a template declaration. */
8701 else if (token2.type == CPP_LESS)
8702 cp_parser_template_declaration (parser, /*member_p=*/false);
8703 /* Anything else must be an explicit instantiation. */
8704 else
8705 cp_parser_explicit_instantiation (parser);
8707 /* If the next token is `export', then we have a template
8708 declaration. */
8709 else if (token1.keyword == RID_EXPORT)
8710 cp_parser_template_declaration (parser, /*member_p=*/false);
8711 /* If the next token is `extern', 'static' or 'inline' and the one
8712 after that is `template', we have a GNU extended explicit
8713 instantiation directive. */
8714 else if (cp_parser_allow_gnu_extensions_p (parser)
8715 && (token1.keyword == RID_EXTERN
8716 || token1.keyword == RID_STATIC
8717 || token1.keyword == RID_INLINE)
8718 && token2.keyword == RID_TEMPLATE)
8719 cp_parser_explicit_instantiation (parser);
8720 /* If the next token is `namespace', check for a named or unnamed
8721 namespace definition. */
8722 else if (token1.keyword == RID_NAMESPACE
8723 && (/* A named namespace definition. */
8724 (token2.type == CPP_NAME
8725 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
8726 != CPP_EQ))
8727 /* An unnamed namespace definition. */
8728 || token2.type == CPP_OPEN_BRACE
8729 || token2.keyword == RID_ATTRIBUTE))
8730 cp_parser_namespace_definition (parser);
8731 /* An inline (associated) namespace definition. */
8732 else if (token1.keyword == RID_INLINE
8733 && token2.keyword == RID_NAMESPACE)
8734 cp_parser_namespace_definition (parser);
8735 /* Objective-C++ declaration/definition. */
8736 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
8737 cp_parser_objc_declaration (parser);
8738 /* We must have either a block declaration or a function
8739 definition. */
8740 else
8741 /* Try to parse a block-declaration, or a function-definition. */
8742 cp_parser_block_declaration (parser, /*statement_p=*/false);
8744 /* Free any declarators allocated. */
8745 obstack_free (&declarator_obstack, p);
8748 /* Parse a block-declaration.
8750 block-declaration:
8751 simple-declaration
8752 asm-definition
8753 namespace-alias-definition
8754 using-declaration
8755 using-directive
8757 GNU Extension:
8759 block-declaration:
8760 __extension__ block-declaration
8762 C++0x Extension:
8764 block-declaration:
8765 static_assert-declaration
8767 If STATEMENT_P is TRUE, then this block-declaration is occurring as
8768 part of a declaration-statement. */
8770 static void
8771 cp_parser_block_declaration (cp_parser *parser,
8772 bool statement_p)
8774 cp_token *token1;
8775 int saved_pedantic;
8777 /* Check for the `__extension__' keyword. */
8778 if (cp_parser_extension_opt (parser, &saved_pedantic))
8780 /* Parse the qualified declaration. */
8781 cp_parser_block_declaration (parser, statement_p);
8782 /* Restore the PEDANTIC flag. */
8783 pedantic = saved_pedantic;
8785 return;
8788 /* Peek at the next token to figure out which kind of declaration is
8789 present. */
8790 token1 = cp_lexer_peek_token (parser->lexer);
8792 /* If the next keyword is `asm', we have an asm-definition. */
8793 if (token1->keyword == RID_ASM)
8795 if (statement_p)
8796 cp_parser_commit_to_tentative_parse (parser);
8797 cp_parser_asm_definition (parser);
8799 /* If the next keyword is `namespace', we have a
8800 namespace-alias-definition. */
8801 else if (token1->keyword == RID_NAMESPACE)
8802 cp_parser_namespace_alias_definition (parser);
8803 /* If the next keyword is `using', we have either a
8804 using-declaration or a using-directive. */
8805 else if (token1->keyword == RID_USING)
8807 cp_token *token2;
8809 if (statement_p)
8810 cp_parser_commit_to_tentative_parse (parser);
8811 /* If the token after `using' is `namespace', then we have a
8812 using-directive. */
8813 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8814 if (token2->keyword == RID_NAMESPACE)
8815 cp_parser_using_directive (parser);
8816 /* Otherwise, it's a using-declaration. */
8817 else
8818 cp_parser_using_declaration (parser,
8819 /*access_declaration_p=*/false);
8821 /* If the next keyword is `__label__' we have a misplaced label
8822 declaration. */
8823 else if (token1->keyword == RID_LABEL)
8825 cp_lexer_consume_token (parser->lexer);
8826 error_at (token1->location, "%<__label__%> not at the beginning of a block");
8827 cp_parser_skip_to_end_of_statement (parser);
8828 /* If the next token is now a `;', consume it. */
8829 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8830 cp_lexer_consume_token (parser->lexer);
8832 /* If the next token is `static_assert' we have a static assertion. */
8833 else if (token1->keyword == RID_STATIC_ASSERT)
8834 cp_parser_static_assert (parser, /*member_p=*/false);
8835 /* Anything else must be a simple-declaration. */
8836 else
8837 cp_parser_simple_declaration (parser, !statement_p);
8840 /* Parse a simple-declaration.
8842 simple-declaration:
8843 decl-specifier-seq [opt] init-declarator-list [opt] ;
8845 init-declarator-list:
8846 init-declarator
8847 init-declarator-list , init-declarator
8849 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8850 function-definition as a simple-declaration. */
8852 static void
8853 cp_parser_simple_declaration (cp_parser* parser,
8854 bool function_definition_allowed_p)
8856 cp_decl_specifier_seq decl_specifiers;
8857 int declares_class_or_enum;
8858 bool saw_declarator;
8860 /* Defer access checks until we know what is being declared; the
8861 checks for names appearing in the decl-specifier-seq should be
8862 done as if we were in the scope of the thing being declared. */
8863 push_deferring_access_checks (dk_deferred);
8865 /* Parse the decl-specifier-seq. We have to keep track of whether
8866 or not the decl-specifier-seq declares a named class or
8867 enumeration type, since that is the only case in which the
8868 init-declarator-list is allowed to be empty.
8870 [dcl.dcl]
8872 In a simple-declaration, the optional init-declarator-list can be
8873 omitted only when declaring a class or enumeration, that is when
8874 the decl-specifier-seq contains either a class-specifier, an
8875 elaborated-type-specifier, or an enum-specifier. */
8876 cp_parser_decl_specifier_seq (parser,
8877 CP_PARSER_FLAGS_OPTIONAL,
8878 &decl_specifiers,
8879 &declares_class_or_enum);
8880 /* We no longer need to defer access checks. */
8881 stop_deferring_access_checks ();
8883 /* In a block scope, a valid declaration must always have a
8884 decl-specifier-seq. By not trying to parse declarators, we can
8885 resolve the declaration/expression ambiguity more quickly. */
8886 if (!function_definition_allowed_p
8887 && !decl_specifiers.any_specifiers_p)
8889 cp_parser_error (parser, "expected declaration");
8890 goto done;
8893 /* If the next two tokens are both identifiers, the code is
8894 erroneous. The usual cause of this situation is code like:
8896 T t;
8898 where "T" should name a type -- but does not. */
8899 if (!decl_specifiers.any_type_specifiers_p
8900 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8902 /* If parsing tentatively, we should commit; we really are
8903 looking at a declaration. */
8904 cp_parser_commit_to_tentative_parse (parser);
8905 /* Give up. */
8906 goto done;
8909 /* If we have seen at least one decl-specifier, and the next token
8910 is not a parenthesis, then we must be looking at a declaration.
8911 (After "int (" we might be looking at a functional cast.) */
8912 if (decl_specifiers.any_specifiers_p
8913 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8914 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
8915 && !cp_parser_error_occurred (parser))
8916 cp_parser_commit_to_tentative_parse (parser);
8918 /* Keep going until we hit the `;' at the end of the simple
8919 declaration. */
8920 saw_declarator = false;
8921 while (cp_lexer_next_token_is_not (parser->lexer,
8922 CPP_SEMICOLON))
8924 cp_token *token;
8925 bool function_definition_p;
8926 tree decl;
8928 if (saw_declarator)
8930 /* If we are processing next declarator, coma is expected */
8931 token = cp_lexer_peek_token (parser->lexer);
8932 gcc_assert (token->type == CPP_COMMA);
8933 cp_lexer_consume_token (parser->lexer);
8935 else
8936 saw_declarator = true;
8938 /* Parse the init-declarator. */
8939 decl = cp_parser_init_declarator (parser, &decl_specifiers,
8940 /*checks=*/NULL,
8941 function_definition_allowed_p,
8942 /*member_p=*/false,
8943 declares_class_or_enum,
8944 &function_definition_p);
8945 /* If an error occurred while parsing tentatively, exit quickly.
8946 (That usually happens when in the body of a function; each
8947 statement is treated as a declaration-statement until proven
8948 otherwise.) */
8949 if (cp_parser_error_occurred (parser))
8950 goto done;
8951 /* Handle function definitions specially. */
8952 if (function_definition_p)
8954 /* If the next token is a `,', then we are probably
8955 processing something like:
8957 void f() {}, *p;
8959 which is erroneous. */
8960 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8962 cp_token *token = cp_lexer_peek_token (parser->lexer);
8963 error_at (token->location,
8964 "mixing"
8965 " declarations and function-definitions is forbidden");
8967 /* Otherwise, we're done with the list of declarators. */
8968 else
8970 pop_deferring_access_checks ();
8971 return;
8974 /* The next token should be either a `,' or a `;'. */
8975 token = cp_lexer_peek_token (parser->lexer);
8976 /* If it's a `,', there are more declarators to come. */
8977 if (token->type == CPP_COMMA)
8978 /* will be consumed next time around */;
8979 /* If it's a `;', we are done. */
8980 else if (token->type == CPP_SEMICOLON)
8981 break;
8982 /* Anything else is an error. */
8983 else
8985 /* If we have already issued an error message we don't need
8986 to issue another one. */
8987 if (decl != error_mark_node
8988 || cp_parser_uncommitted_to_tentative_parse_p (parser))
8989 cp_parser_error (parser, "expected %<,%> or %<;%>");
8990 /* Skip tokens until we reach the end of the statement. */
8991 cp_parser_skip_to_end_of_statement (parser);
8992 /* If the next token is now a `;', consume it. */
8993 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8994 cp_lexer_consume_token (parser->lexer);
8995 goto done;
8997 /* After the first time around, a function-definition is not
8998 allowed -- even if it was OK at first. For example:
9000 int i, f() {}
9002 is not valid. */
9003 function_definition_allowed_p = false;
9006 /* Issue an error message if no declarators are present, and the
9007 decl-specifier-seq does not itself declare a class or
9008 enumeration. */
9009 if (!saw_declarator)
9011 if (cp_parser_declares_only_class_p (parser))
9012 shadow_tag (&decl_specifiers);
9013 /* Perform any deferred access checks. */
9014 perform_deferred_access_checks ();
9017 /* Consume the `;'. */
9018 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
9020 done:
9021 pop_deferring_access_checks ();
9024 /* Parse a decl-specifier-seq.
9026 decl-specifier-seq:
9027 decl-specifier-seq [opt] decl-specifier
9029 decl-specifier:
9030 storage-class-specifier
9031 type-specifier
9032 function-specifier
9033 friend
9034 typedef
9036 GNU Extension:
9038 decl-specifier:
9039 attributes
9041 Set *DECL_SPECS to a representation of the decl-specifier-seq.
9043 The parser flags FLAGS is used to control type-specifier parsing.
9045 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
9046 flags:
9048 1: one of the decl-specifiers is an elaborated-type-specifier
9049 (i.e., a type declaration)
9050 2: one of the decl-specifiers is an enum-specifier or a
9051 class-specifier (i.e., a type definition)
9055 static void
9056 cp_parser_decl_specifier_seq (cp_parser* parser,
9057 cp_parser_flags flags,
9058 cp_decl_specifier_seq *decl_specs,
9059 int* declares_class_or_enum)
9061 bool constructor_possible_p = !parser->in_declarator_p;
9062 cp_token *start_token = NULL;
9064 /* Clear DECL_SPECS. */
9065 clear_decl_specs (decl_specs);
9067 /* Assume no class or enumeration type is declared. */
9068 *declares_class_or_enum = 0;
9070 /* Keep reading specifiers until there are no more to read. */
9071 while (true)
9073 bool constructor_p;
9074 bool found_decl_spec;
9075 cp_token *token;
9077 /* Peek at the next token. */
9078 token = cp_lexer_peek_token (parser->lexer);
9080 /* Save the first token of the decl spec list for error
9081 reporting. */
9082 if (!start_token)
9083 start_token = token;
9084 /* Handle attributes. */
9085 if (token->keyword == RID_ATTRIBUTE)
9087 /* Parse the attributes. */
9088 decl_specs->attributes
9089 = chainon (decl_specs->attributes,
9090 cp_parser_attributes_opt (parser));
9091 continue;
9093 /* Assume we will find a decl-specifier keyword. */
9094 found_decl_spec = true;
9095 /* If the next token is an appropriate keyword, we can simply
9096 add it to the list. */
9097 switch (token->keyword)
9099 /* decl-specifier:
9100 friend
9101 constexpr */
9102 case RID_FRIEND:
9103 if (!at_class_scope_p ())
9105 error_at (token->location, "%<friend%> used outside of class");
9106 cp_lexer_purge_token (parser->lexer);
9108 else
9110 ++decl_specs->specs[(int) ds_friend];
9111 /* Consume the token. */
9112 cp_lexer_consume_token (parser->lexer);
9114 break;
9116 case RID_CONSTEXPR:
9117 ++decl_specs->specs[(int) ds_constexpr];
9118 cp_lexer_consume_token (parser->lexer);
9119 break;
9121 /* function-specifier:
9122 inline
9123 virtual
9124 explicit */
9125 case RID_INLINE:
9126 case RID_VIRTUAL:
9127 case RID_EXPLICIT:
9128 cp_parser_function_specifier_opt (parser, decl_specs);
9129 break;
9131 /* decl-specifier:
9132 typedef */
9133 case RID_TYPEDEF:
9134 ++decl_specs->specs[(int) ds_typedef];
9135 /* Consume the token. */
9136 cp_lexer_consume_token (parser->lexer);
9137 /* A constructor declarator cannot appear in a typedef. */
9138 constructor_possible_p = false;
9139 /* The "typedef" keyword can only occur in a declaration; we
9140 may as well commit at this point. */
9141 cp_parser_commit_to_tentative_parse (parser);
9143 if (decl_specs->storage_class != sc_none)
9144 decl_specs->conflicting_specifiers_p = true;
9145 break;
9147 /* storage-class-specifier:
9148 auto
9149 register
9150 static
9151 extern
9152 mutable
9154 GNU Extension:
9155 thread */
9156 case RID_AUTO:
9157 if (cxx_dialect == cxx98)
9159 /* Consume the token. */
9160 cp_lexer_consume_token (parser->lexer);
9162 /* Complain about `auto' as a storage specifier, if
9163 we're complaining about C++0x compatibility. */
9164 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9165 " will change meaning in C++0x; please remove it");
9167 /* Set the storage class anyway. */
9168 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9169 token->location);
9171 else
9172 /* C++0x auto type-specifier. */
9173 found_decl_spec = false;
9174 break;
9176 case RID_REGISTER:
9177 case RID_STATIC:
9178 case RID_EXTERN:
9179 case RID_MUTABLE:
9180 /* Consume the token. */
9181 cp_lexer_consume_token (parser->lexer);
9182 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9183 token->location);
9184 break;
9185 case RID_THREAD:
9186 /* Consume the token. */
9187 cp_lexer_consume_token (parser->lexer);
9188 ++decl_specs->specs[(int) ds_thread];
9189 break;
9191 default:
9192 /* We did not yet find a decl-specifier yet. */
9193 found_decl_spec = false;
9194 break;
9197 /* Constructors are a special case. The `S' in `S()' is not a
9198 decl-specifier; it is the beginning of the declarator. */
9199 constructor_p
9200 = (!found_decl_spec
9201 && constructor_possible_p
9202 && (cp_parser_constructor_declarator_p
9203 (parser, decl_specs->specs[(int) ds_friend] != 0)));
9205 /* If we don't have a DECL_SPEC yet, then we must be looking at
9206 a type-specifier. */
9207 if (!found_decl_spec && !constructor_p)
9209 int decl_spec_declares_class_or_enum;
9210 bool is_cv_qualifier;
9211 tree type_spec;
9213 type_spec
9214 = cp_parser_type_specifier (parser, flags,
9215 decl_specs,
9216 /*is_declaration=*/true,
9217 &decl_spec_declares_class_or_enum,
9218 &is_cv_qualifier);
9219 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
9221 /* If this type-specifier referenced a user-defined type
9222 (a typedef, class-name, etc.), then we can't allow any
9223 more such type-specifiers henceforth.
9225 [dcl.spec]
9227 The longest sequence of decl-specifiers that could
9228 possibly be a type name is taken as the
9229 decl-specifier-seq of a declaration. The sequence shall
9230 be self-consistent as described below.
9232 [dcl.type]
9234 As a general rule, at most one type-specifier is allowed
9235 in the complete decl-specifier-seq of a declaration. The
9236 only exceptions are the following:
9238 -- const or volatile can be combined with any other
9239 type-specifier.
9241 -- signed or unsigned can be combined with char, long,
9242 short, or int.
9244 -- ..
9246 Example:
9248 typedef char* Pc;
9249 void g (const int Pc);
9251 Here, Pc is *not* part of the decl-specifier seq; it's
9252 the declarator. Therefore, once we see a type-specifier
9253 (other than a cv-qualifier), we forbid any additional
9254 user-defined types. We *do* still allow things like `int
9255 int' to be considered a decl-specifier-seq, and issue the
9256 error message later. */
9257 if (type_spec && !is_cv_qualifier)
9258 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
9259 /* A constructor declarator cannot follow a type-specifier. */
9260 if (type_spec)
9262 constructor_possible_p = false;
9263 found_decl_spec = true;
9264 if (!is_cv_qualifier)
9265 decl_specs->any_type_specifiers_p = true;
9269 /* If we still do not have a DECL_SPEC, then there are no more
9270 decl-specifiers. */
9271 if (!found_decl_spec)
9272 break;
9274 decl_specs->any_specifiers_p = true;
9275 /* After we see one decl-specifier, further decl-specifiers are
9276 always optional. */
9277 flags |= CP_PARSER_FLAGS_OPTIONAL;
9280 cp_parser_check_decl_spec (decl_specs, start_token->location);
9282 /* Don't allow a friend specifier with a class definition. */
9283 if (decl_specs->specs[(int) ds_friend] != 0
9284 && (*declares_class_or_enum & 2))
9285 error_at (start_token->location,
9286 "class definition may not be declared a friend");
9289 /* Parse an (optional) storage-class-specifier.
9291 storage-class-specifier:
9292 auto
9293 register
9294 static
9295 extern
9296 mutable
9298 GNU Extension:
9300 storage-class-specifier:
9301 thread
9303 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
9305 static tree
9306 cp_parser_storage_class_specifier_opt (cp_parser* parser)
9308 switch (cp_lexer_peek_token (parser->lexer)->keyword)
9310 case RID_AUTO:
9311 if (cxx_dialect != cxx98)
9312 return NULL_TREE;
9313 /* Fall through for C++98. */
9315 case RID_REGISTER:
9316 case RID_STATIC:
9317 case RID_EXTERN:
9318 case RID_MUTABLE:
9319 case RID_THREAD:
9320 /* Consume the token. */
9321 return cp_lexer_consume_token (parser->lexer)->u.value;
9323 default:
9324 return NULL_TREE;
9328 /* Parse an (optional) function-specifier.
9330 function-specifier:
9331 inline
9332 virtual
9333 explicit
9335 Returns an IDENTIFIER_NODE corresponding to the keyword used.
9336 Updates DECL_SPECS, if it is non-NULL. */
9338 static tree
9339 cp_parser_function_specifier_opt (cp_parser* parser,
9340 cp_decl_specifier_seq *decl_specs)
9342 cp_token *token = cp_lexer_peek_token (parser->lexer);
9343 switch (token->keyword)
9345 case RID_INLINE:
9346 if (decl_specs)
9347 ++decl_specs->specs[(int) ds_inline];
9348 break;
9350 case RID_VIRTUAL:
9351 /* 14.5.2.3 [temp.mem]
9353 A member function template shall not be virtual. */
9354 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
9355 error_at (token->location, "templates may not be %<virtual%>");
9356 else if (decl_specs)
9357 ++decl_specs->specs[(int) ds_virtual];
9358 break;
9360 case RID_EXPLICIT:
9361 if (decl_specs)
9362 ++decl_specs->specs[(int) ds_explicit];
9363 break;
9365 default:
9366 return NULL_TREE;
9369 /* Consume the token. */
9370 return cp_lexer_consume_token (parser->lexer)->u.value;
9373 /* Parse a linkage-specification.
9375 linkage-specification:
9376 extern string-literal { declaration-seq [opt] }
9377 extern string-literal declaration */
9379 static void
9380 cp_parser_linkage_specification (cp_parser* parser)
9382 tree linkage;
9384 /* Look for the `extern' keyword. */
9385 cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
9387 /* Look for the string-literal. */
9388 linkage = cp_parser_string_literal (parser, false, false);
9390 /* Transform the literal into an identifier. If the literal is a
9391 wide-character string, or contains embedded NULs, then we can't
9392 handle it as the user wants. */
9393 if (strlen (TREE_STRING_POINTER (linkage))
9394 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
9396 cp_parser_error (parser, "invalid linkage-specification");
9397 /* Assume C++ linkage. */
9398 linkage = lang_name_cplusplus;
9400 else
9401 linkage = get_identifier (TREE_STRING_POINTER (linkage));
9403 /* We're now using the new linkage. */
9404 push_lang_context (linkage);
9406 /* If the next token is a `{', then we're using the first
9407 production. */
9408 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9410 /* Consume the `{' token. */
9411 cp_lexer_consume_token (parser->lexer);
9412 /* Parse the declarations. */
9413 cp_parser_declaration_seq_opt (parser);
9414 /* Look for the closing `}'. */
9415 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
9417 /* Otherwise, there's just one declaration. */
9418 else
9420 bool saved_in_unbraced_linkage_specification_p;
9422 saved_in_unbraced_linkage_specification_p
9423 = parser->in_unbraced_linkage_specification_p;
9424 parser->in_unbraced_linkage_specification_p = true;
9425 cp_parser_declaration (parser);
9426 parser->in_unbraced_linkage_specification_p
9427 = saved_in_unbraced_linkage_specification_p;
9430 /* We're done with the linkage-specification. */
9431 pop_lang_context ();
9434 /* Parse a static_assert-declaration.
9436 static_assert-declaration:
9437 static_assert ( constant-expression , string-literal ) ;
9439 If MEMBER_P, this static_assert is a class member. */
9441 static void
9442 cp_parser_static_assert(cp_parser *parser, bool member_p)
9444 tree condition;
9445 tree message;
9446 cp_token *token;
9447 location_t saved_loc;
9449 /* Peek at the `static_assert' token so we can keep track of exactly
9450 where the static assertion started. */
9451 token = cp_lexer_peek_token (parser->lexer);
9452 saved_loc = token->location;
9454 /* Look for the `static_assert' keyword. */
9455 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
9456 "%<static_assert%>"))
9457 return;
9459 /* We know we are in a static assertion; commit to any tentative
9460 parse. */
9461 if (cp_parser_parsing_tentatively (parser))
9462 cp_parser_commit_to_tentative_parse (parser);
9464 /* Parse the `(' starting the static assertion condition. */
9465 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
9467 /* Parse the constant-expression. */
9468 condition =
9469 cp_parser_constant_expression (parser,
9470 /*allow_non_constant_p=*/false,
9471 /*non_constant_p=*/NULL);
9473 /* Parse the separating `,'. */
9474 cp_parser_require (parser, CPP_COMMA, "%<,%>");
9476 /* Parse the string-literal message. */
9477 message = cp_parser_string_literal (parser,
9478 /*translate=*/false,
9479 /*wide_ok=*/true);
9481 /* A `)' completes the static assertion. */
9482 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
9483 cp_parser_skip_to_closing_parenthesis (parser,
9484 /*recovering=*/true,
9485 /*or_comma=*/false,
9486 /*consume_paren=*/true);
9488 /* A semicolon terminates the declaration. */
9489 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
9491 /* Complete the static assertion, which may mean either processing
9492 the static assert now or saving it for template instantiation. */
9493 finish_static_assert (condition, message, saved_loc, member_p);
9496 /* Parse a `decltype' type. Returns the type.
9498 simple-type-specifier:
9499 decltype ( expression ) */
9501 static tree
9502 cp_parser_decltype (cp_parser *parser)
9504 tree expr;
9505 bool id_expression_or_member_access_p = false;
9506 const char *saved_message;
9507 bool saved_integral_constant_expression_p;
9508 bool saved_non_integral_constant_expression_p;
9509 cp_token *id_expr_start_token;
9511 /* Look for the `decltype' token. */
9512 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
9513 return error_mark_node;
9515 /* Types cannot be defined in a `decltype' expression. Save away the
9516 old message. */
9517 saved_message = parser->type_definition_forbidden_message;
9519 /* And create the new one. */
9520 parser->type_definition_forbidden_message
9521 = G_("types may not be defined in %<decltype%> expressions");
9523 /* The restrictions on constant-expressions do not apply inside
9524 decltype expressions. */
9525 saved_integral_constant_expression_p
9526 = parser->integral_constant_expression_p;
9527 saved_non_integral_constant_expression_p
9528 = parser->non_integral_constant_expression_p;
9529 parser->integral_constant_expression_p = false;
9531 /* Do not actually evaluate the expression. */
9532 ++cp_unevaluated_operand;
9534 /* Do not warn about problems with the expression. */
9535 ++c_inhibit_evaluation_warnings;
9537 /* Parse the opening `('. */
9538 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
9539 return error_mark_node;
9541 /* First, try parsing an id-expression. */
9542 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
9543 cp_parser_parse_tentatively (parser);
9544 expr = cp_parser_id_expression (parser,
9545 /*template_keyword_p=*/false,
9546 /*check_dependency_p=*/true,
9547 /*template_p=*/NULL,
9548 /*declarator_p=*/false,
9549 /*optional_p=*/false);
9551 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
9553 bool non_integral_constant_expression_p = false;
9554 tree id_expression = expr;
9555 cp_id_kind idk;
9556 const char *error_msg;
9558 if (TREE_CODE (expr) == IDENTIFIER_NODE)
9559 /* Lookup the name we got back from the id-expression. */
9560 expr = cp_parser_lookup_name (parser, expr,
9561 none_type,
9562 /*is_template=*/false,
9563 /*is_namespace=*/false,
9564 /*check_dependency=*/true,
9565 /*ambiguous_decls=*/NULL,
9566 id_expr_start_token->location);
9568 if (expr
9569 && expr != error_mark_node
9570 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
9571 && TREE_CODE (expr) != TYPE_DECL
9572 && (TREE_CODE (expr) != BIT_NOT_EXPR
9573 || !TYPE_P (TREE_OPERAND (expr, 0)))
9574 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9576 /* Complete lookup of the id-expression. */
9577 expr = (finish_id_expression
9578 (id_expression, expr, parser->scope, &idk,
9579 /*integral_constant_expression_p=*/false,
9580 /*allow_non_integral_constant_expression_p=*/true,
9581 &non_integral_constant_expression_p,
9582 /*template_p=*/false,
9583 /*done=*/true,
9584 /*address_p=*/false,
9585 /*template_arg_p=*/false,
9586 &error_msg,
9587 id_expr_start_token->location));
9589 if (expr == error_mark_node)
9590 /* We found an id-expression, but it was something that we
9591 should not have found. This is an error, not something
9592 we can recover from, so note that we found an
9593 id-expression and we'll recover as gracefully as
9594 possible. */
9595 id_expression_or_member_access_p = true;
9598 if (expr
9599 && expr != error_mark_node
9600 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9601 /* We have an id-expression. */
9602 id_expression_or_member_access_p = true;
9605 if (!id_expression_or_member_access_p)
9607 /* Abort the id-expression parse. */
9608 cp_parser_abort_tentative_parse (parser);
9610 /* Parsing tentatively, again. */
9611 cp_parser_parse_tentatively (parser);
9613 /* Parse a class member access. */
9614 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
9615 /*cast_p=*/false,
9616 /*member_access_only_p=*/true, NULL);
9618 if (expr
9619 && expr != error_mark_node
9620 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9621 /* We have an id-expression. */
9622 id_expression_or_member_access_p = true;
9625 if (id_expression_or_member_access_p)
9626 /* We have parsed the complete id-expression or member access. */
9627 cp_parser_parse_definitely (parser);
9628 else
9630 bool saved_greater_than_is_operator_p;
9632 /* Abort our attempt to parse an id-expression or member access
9633 expression. */
9634 cp_parser_abort_tentative_parse (parser);
9636 /* Within a parenthesized expression, a `>' token is always
9637 the greater-than operator. */
9638 saved_greater_than_is_operator_p
9639 = parser->greater_than_is_operator_p;
9640 parser->greater_than_is_operator_p = true;
9642 /* Parse a full expression. */
9643 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9645 /* The `>' token might be the end of a template-id or
9646 template-parameter-list now. */
9647 parser->greater_than_is_operator_p
9648 = saved_greater_than_is_operator_p;
9651 /* Go back to evaluating expressions. */
9652 --cp_unevaluated_operand;
9653 --c_inhibit_evaluation_warnings;
9655 /* Restore the old message and the integral constant expression
9656 flags. */
9657 parser->type_definition_forbidden_message = saved_message;
9658 parser->integral_constant_expression_p
9659 = saved_integral_constant_expression_p;
9660 parser->non_integral_constant_expression_p
9661 = saved_non_integral_constant_expression_p;
9663 if (expr == error_mark_node)
9665 /* Skip everything up to the closing `)'. */
9666 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9667 /*consume_paren=*/true);
9668 return error_mark_node;
9671 /* Parse to the closing `)'. */
9672 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
9674 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9675 /*consume_paren=*/true);
9676 return error_mark_node;
9679 return finish_decltype_type (expr, id_expression_or_member_access_p);
9682 /* Special member functions [gram.special] */
9684 /* Parse a conversion-function-id.
9686 conversion-function-id:
9687 operator conversion-type-id
9689 Returns an IDENTIFIER_NODE representing the operator. */
9691 static tree
9692 cp_parser_conversion_function_id (cp_parser* parser)
9694 tree type;
9695 tree saved_scope;
9696 tree saved_qualifying_scope;
9697 tree saved_object_scope;
9698 tree pushed_scope = NULL_TREE;
9700 /* Look for the `operator' token. */
9701 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9702 return error_mark_node;
9703 /* When we parse the conversion-type-id, the current scope will be
9704 reset. However, we need that information in able to look up the
9705 conversion function later, so we save it here. */
9706 saved_scope = parser->scope;
9707 saved_qualifying_scope = parser->qualifying_scope;
9708 saved_object_scope = parser->object_scope;
9709 /* We must enter the scope of the class so that the names of
9710 entities declared within the class are available in the
9711 conversion-type-id. For example, consider:
9713 struct S {
9714 typedef int I;
9715 operator I();
9718 S::operator I() { ... }
9720 In order to see that `I' is a type-name in the definition, we
9721 must be in the scope of `S'. */
9722 if (saved_scope)
9723 pushed_scope = push_scope (saved_scope);
9724 /* Parse the conversion-type-id. */
9725 type = cp_parser_conversion_type_id (parser);
9726 /* Leave the scope of the class, if any. */
9727 if (pushed_scope)
9728 pop_scope (pushed_scope);
9729 /* Restore the saved scope. */
9730 parser->scope = saved_scope;
9731 parser->qualifying_scope = saved_qualifying_scope;
9732 parser->object_scope = saved_object_scope;
9733 /* If the TYPE is invalid, indicate failure. */
9734 if (type == error_mark_node)
9735 return error_mark_node;
9736 return mangle_conv_op_name_for_type (type);
9739 /* Parse a conversion-type-id:
9741 conversion-type-id:
9742 type-specifier-seq conversion-declarator [opt]
9744 Returns the TYPE specified. */
9746 static tree
9747 cp_parser_conversion_type_id (cp_parser* parser)
9749 tree attributes;
9750 cp_decl_specifier_seq type_specifiers;
9751 cp_declarator *declarator;
9752 tree type_specified;
9754 /* Parse the attributes. */
9755 attributes = cp_parser_attributes_opt (parser);
9756 /* Parse the type-specifiers. */
9757 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
9758 /*is_trailing_return=*/false,
9759 &type_specifiers);
9760 /* If that didn't work, stop. */
9761 if (type_specifiers.type == error_mark_node)
9762 return error_mark_node;
9763 /* Parse the conversion-declarator. */
9764 declarator = cp_parser_conversion_declarator_opt (parser);
9766 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
9767 /*initialized=*/0, &attributes);
9768 if (attributes)
9769 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
9771 /* Don't give this error when parsing tentatively. This happens to
9772 work because we always parse this definitively once. */
9773 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
9774 && type_uses_auto (type_specified))
9776 error ("invalid use of %<auto%> in conversion operator");
9777 return error_mark_node;
9780 return type_specified;
9783 /* Parse an (optional) conversion-declarator.
9785 conversion-declarator:
9786 ptr-operator conversion-declarator [opt]
9790 static cp_declarator *
9791 cp_parser_conversion_declarator_opt (cp_parser* parser)
9793 enum tree_code code;
9794 tree class_type;
9795 cp_cv_quals cv_quals;
9797 /* We don't know if there's a ptr-operator next, or not. */
9798 cp_parser_parse_tentatively (parser);
9799 /* Try the ptr-operator. */
9800 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
9801 /* If it worked, look for more conversion-declarators. */
9802 if (cp_parser_parse_definitely (parser))
9804 cp_declarator *declarator;
9806 /* Parse another optional declarator. */
9807 declarator = cp_parser_conversion_declarator_opt (parser);
9809 return cp_parser_make_indirect_declarator
9810 (code, class_type, cv_quals, declarator);
9813 return NULL;
9816 /* Parse an (optional) ctor-initializer.
9818 ctor-initializer:
9819 : mem-initializer-list
9821 Returns TRUE iff the ctor-initializer was actually present. */
9823 static bool
9824 cp_parser_ctor_initializer_opt (cp_parser* parser)
9826 /* If the next token is not a `:', then there is no
9827 ctor-initializer. */
9828 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
9830 /* Do default initialization of any bases and members. */
9831 if (DECL_CONSTRUCTOR_P (current_function_decl))
9832 finish_mem_initializers (NULL_TREE);
9834 return false;
9837 /* Consume the `:' token. */
9838 cp_lexer_consume_token (parser->lexer);
9839 /* And the mem-initializer-list. */
9840 cp_parser_mem_initializer_list (parser);
9842 return true;
9845 /* Parse a mem-initializer-list.
9847 mem-initializer-list:
9848 mem-initializer ... [opt]
9849 mem-initializer ... [opt] , mem-initializer-list */
9851 static void
9852 cp_parser_mem_initializer_list (cp_parser* parser)
9854 tree mem_initializer_list = NULL_TREE;
9855 cp_token *token = cp_lexer_peek_token (parser->lexer);
9857 /* Let the semantic analysis code know that we are starting the
9858 mem-initializer-list. */
9859 if (!DECL_CONSTRUCTOR_P (current_function_decl))
9860 error_at (token->location,
9861 "only constructors take base initializers");
9863 /* Loop through the list. */
9864 while (true)
9866 tree mem_initializer;
9868 token = cp_lexer_peek_token (parser->lexer);
9869 /* Parse the mem-initializer. */
9870 mem_initializer = cp_parser_mem_initializer (parser);
9871 /* If the next token is a `...', we're expanding member initializers. */
9872 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9874 /* Consume the `...'. */
9875 cp_lexer_consume_token (parser->lexer);
9877 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9878 can be expanded but members cannot. */
9879 if (mem_initializer != error_mark_node
9880 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9882 error_at (token->location,
9883 "cannot expand initializer for member %<%D%>",
9884 TREE_PURPOSE (mem_initializer));
9885 mem_initializer = error_mark_node;
9888 /* Construct the pack expansion type. */
9889 if (mem_initializer != error_mark_node)
9890 mem_initializer = make_pack_expansion (mem_initializer);
9892 /* Add it to the list, unless it was erroneous. */
9893 if (mem_initializer != error_mark_node)
9895 TREE_CHAIN (mem_initializer) = mem_initializer_list;
9896 mem_initializer_list = mem_initializer;
9898 /* If the next token is not a `,', we're done. */
9899 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9900 break;
9901 /* Consume the `,' token. */
9902 cp_lexer_consume_token (parser->lexer);
9905 /* Perform semantic analysis. */
9906 if (DECL_CONSTRUCTOR_P (current_function_decl))
9907 finish_mem_initializers (mem_initializer_list);
9910 /* Parse a mem-initializer.
9912 mem-initializer:
9913 mem-initializer-id ( expression-list [opt] )
9914 mem-initializer-id braced-init-list
9916 GNU extension:
9918 mem-initializer:
9919 ( expression-list [opt] )
9921 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
9922 class) or FIELD_DECL (for a non-static data member) to initialize;
9923 the TREE_VALUE is the expression-list. An empty initialization
9924 list is represented by void_list_node. */
9926 static tree
9927 cp_parser_mem_initializer (cp_parser* parser)
9929 tree mem_initializer_id;
9930 tree expression_list;
9931 tree member;
9932 cp_token *token = cp_lexer_peek_token (parser->lexer);
9934 /* Find out what is being initialized. */
9935 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9937 permerror (token->location,
9938 "anachronistic old-style base class initializer");
9939 mem_initializer_id = NULL_TREE;
9941 else
9943 mem_initializer_id = cp_parser_mem_initializer_id (parser);
9944 if (mem_initializer_id == error_mark_node)
9945 return mem_initializer_id;
9947 member = expand_member_init (mem_initializer_id);
9948 if (member && !DECL_P (member))
9949 in_base_initializer = 1;
9951 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9953 bool expr_non_constant_p;
9954 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9955 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9956 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9957 expression_list = build_tree_list (NULL_TREE, expression_list);
9959 else
9961 VEC(tree,gc)* vec;
9962 vec = cp_parser_parenthesized_expression_list (parser, false,
9963 /*cast_p=*/false,
9964 /*allow_expansion_p=*/true,
9965 /*non_constant_p=*/NULL);
9966 if (vec == NULL)
9967 return error_mark_node;
9968 expression_list = build_tree_list_vec (vec);
9969 release_tree_vector (vec);
9972 if (expression_list == error_mark_node)
9973 return error_mark_node;
9974 if (!expression_list)
9975 expression_list = void_type_node;
9977 in_base_initializer = 0;
9979 return member ? build_tree_list (member, expression_list) : error_mark_node;
9982 /* Parse a mem-initializer-id.
9984 mem-initializer-id:
9985 :: [opt] nested-name-specifier [opt] class-name
9986 identifier
9988 Returns a TYPE indicating the class to be initializer for the first
9989 production. Returns an IDENTIFIER_NODE indicating the data member
9990 to be initialized for the second production. */
9992 static tree
9993 cp_parser_mem_initializer_id (cp_parser* parser)
9995 bool global_scope_p;
9996 bool nested_name_specifier_p;
9997 bool template_p = false;
9998 tree id;
10000 cp_token *token = cp_lexer_peek_token (parser->lexer);
10002 /* `typename' is not allowed in this context ([temp.res]). */
10003 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
10005 error_at (token->location,
10006 "keyword %<typename%> not allowed in this context (a qualified "
10007 "member initializer is implicitly a type)");
10008 cp_lexer_consume_token (parser->lexer);
10010 /* Look for the optional `::' operator. */
10011 global_scope_p
10012 = (cp_parser_global_scope_opt (parser,
10013 /*current_scope_valid_p=*/false)
10014 != NULL_TREE);
10015 /* Look for the optional nested-name-specifier. The simplest way to
10016 implement:
10018 [temp.res]
10020 The keyword `typename' is not permitted in a base-specifier or
10021 mem-initializer; in these contexts a qualified name that
10022 depends on a template-parameter is implicitly assumed to be a
10023 type name.
10025 is to assume that we have seen the `typename' keyword at this
10026 point. */
10027 nested_name_specifier_p
10028 = (cp_parser_nested_name_specifier_opt (parser,
10029 /*typename_keyword_p=*/true,
10030 /*check_dependency_p=*/true,
10031 /*type_p=*/true,
10032 /*is_declaration=*/true)
10033 != NULL_TREE);
10034 if (nested_name_specifier_p)
10035 template_p = cp_parser_optional_template_keyword (parser);
10036 /* If there is a `::' operator or a nested-name-specifier, then we
10037 are definitely looking for a class-name. */
10038 if (global_scope_p || nested_name_specifier_p)
10039 return cp_parser_class_name (parser,
10040 /*typename_keyword_p=*/true,
10041 /*template_keyword_p=*/template_p,
10042 typename_type,
10043 /*check_dependency_p=*/true,
10044 /*class_head_p=*/false,
10045 /*is_declaration=*/true);
10046 /* Otherwise, we could also be looking for an ordinary identifier. */
10047 cp_parser_parse_tentatively (parser);
10048 /* Try a class-name. */
10049 id = cp_parser_class_name (parser,
10050 /*typename_keyword_p=*/true,
10051 /*template_keyword_p=*/false,
10052 none_type,
10053 /*check_dependency_p=*/true,
10054 /*class_head_p=*/false,
10055 /*is_declaration=*/true);
10056 /* If we found one, we're done. */
10057 if (cp_parser_parse_definitely (parser))
10058 return id;
10059 /* Otherwise, look for an ordinary identifier. */
10060 return cp_parser_identifier (parser);
10063 /* Overloading [gram.over] */
10065 /* Parse an operator-function-id.
10067 operator-function-id:
10068 operator operator
10070 Returns an IDENTIFIER_NODE for the operator which is a
10071 human-readable spelling of the identifier, e.g., `operator +'. */
10073 static tree
10074 cp_parser_operator_function_id (cp_parser* parser)
10076 /* Look for the `operator' keyword. */
10077 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
10078 return error_mark_node;
10079 /* And then the name of the operator itself. */
10080 return cp_parser_operator (parser);
10083 /* Parse an operator.
10085 operator:
10086 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10087 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10088 || ++ -- , ->* -> () []
10090 GNU Extensions:
10092 operator:
10093 <? >? <?= >?=
10095 Returns an IDENTIFIER_NODE for the operator which is a
10096 human-readable spelling of the identifier, e.g., `operator +'. */
10098 static tree
10099 cp_parser_operator (cp_parser* parser)
10101 tree id = NULL_TREE;
10102 cp_token *token;
10104 /* Peek at the next token. */
10105 token = cp_lexer_peek_token (parser->lexer);
10106 /* Figure out which operator we have. */
10107 switch (token->type)
10109 case CPP_KEYWORD:
10111 enum tree_code op;
10113 /* The keyword should be either `new' or `delete'. */
10114 if (token->keyword == RID_NEW)
10115 op = NEW_EXPR;
10116 else if (token->keyword == RID_DELETE)
10117 op = DELETE_EXPR;
10118 else
10119 break;
10121 /* Consume the `new' or `delete' token. */
10122 cp_lexer_consume_token (parser->lexer);
10124 /* Peek at the next token. */
10125 token = cp_lexer_peek_token (parser->lexer);
10126 /* If it's a `[' token then this is the array variant of the
10127 operator. */
10128 if (token->type == CPP_OPEN_SQUARE)
10130 /* Consume the `[' token. */
10131 cp_lexer_consume_token (parser->lexer);
10132 /* Look for the `]' token. */
10133 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
10134 id = ansi_opname (op == NEW_EXPR
10135 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10137 /* Otherwise, we have the non-array variant. */
10138 else
10139 id = ansi_opname (op);
10141 return id;
10144 case CPP_PLUS:
10145 id = ansi_opname (PLUS_EXPR);
10146 break;
10148 case CPP_MINUS:
10149 id = ansi_opname (MINUS_EXPR);
10150 break;
10152 case CPP_MULT:
10153 id = ansi_opname (MULT_EXPR);
10154 break;
10156 case CPP_DIV:
10157 id = ansi_opname (TRUNC_DIV_EXPR);
10158 break;
10160 case CPP_MOD:
10161 id = ansi_opname (TRUNC_MOD_EXPR);
10162 break;
10164 case CPP_XOR:
10165 id = ansi_opname (BIT_XOR_EXPR);
10166 break;
10168 case CPP_AND:
10169 id = ansi_opname (BIT_AND_EXPR);
10170 break;
10172 case CPP_OR:
10173 id = ansi_opname (BIT_IOR_EXPR);
10174 break;
10176 case CPP_COMPL:
10177 id = ansi_opname (BIT_NOT_EXPR);
10178 break;
10180 case CPP_NOT:
10181 id = ansi_opname (TRUTH_NOT_EXPR);
10182 break;
10184 case CPP_EQ:
10185 id = ansi_assopname (NOP_EXPR);
10186 break;
10188 case CPP_LESS:
10189 id = ansi_opname (LT_EXPR);
10190 break;
10192 case CPP_GREATER:
10193 id = ansi_opname (GT_EXPR);
10194 break;
10196 case CPP_PLUS_EQ:
10197 id = ansi_assopname (PLUS_EXPR);
10198 break;
10200 case CPP_MINUS_EQ:
10201 id = ansi_assopname (MINUS_EXPR);
10202 break;
10204 case CPP_MULT_EQ:
10205 id = ansi_assopname (MULT_EXPR);
10206 break;
10208 case CPP_DIV_EQ:
10209 id = ansi_assopname (TRUNC_DIV_EXPR);
10210 break;
10212 case CPP_MOD_EQ:
10213 id = ansi_assopname (TRUNC_MOD_EXPR);
10214 break;
10216 case CPP_XOR_EQ:
10217 id = ansi_assopname (BIT_XOR_EXPR);
10218 break;
10220 case CPP_AND_EQ:
10221 id = ansi_assopname (BIT_AND_EXPR);
10222 break;
10224 case CPP_OR_EQ:
10225 id = ansi_assopname (BIT_IOR_EXPR);
10226 break;
10228 case CPP_LSHIFT:
10229 id = ansi_opname (LSHIFT_EXPR);
10230 break;
10232 case CPP_RSHIFT:
10233 id = ansi_opname (RSHIFT_EXPR);
10234 break;
10236 case CPP_LSHIFT_EQ:
10237 id = ansi_assopname (LSHIFT_EXPR);
10238 break;
10240 case CPP_RSHIFT_EQ:
10241 id = ansi_assopname (RSHIFT_EXPR);
10242 break;
10244 case CPP_EQ_EQ:
10245 id = ansi_opname (EQ_EXPR);
10246 break;
10248 case CPP_NOT_EQ:
10249 id = ansi_opname (NE_EXPR);
10250 break;
10252 case CPP_LESS_EQ:
10253 id = ansi_opname (LE_EXPR);
10254 break;
10256 case CPP_GREATER_EQ:
10257 id = ansi_opname (GE_EXPR);
10258 break;
10260 case CPP_AND_AND:
10261 id = ansi_opname (TRUTH_ANDIF_EXPR);
10262 break;
10264 case CPP_OR_OR:
10265 id = ansi_opname (TRUTH_ORIF_EXPR);
10266 break;
10268 case CPP_PLUS_PLUS:
10269 id = ansi_opname (POSTINCREMENT_EXPR);
10270 break;
10272 case CPP_MINUS_MINUS:
10273 id = ansi_opname (PREDECREMENT_EXPR);
10274 break;
10276 case CPP_COMMA:
10277 id = ansi_opname (COMPOUND_EXPR);
10278 break;
10280 case CPP_DEREF_STAR:
10281 id = ansi_opname (MEMBER_REF);
10282 break;
10284 case CPP_DEREF:
10285 id = ansi_opname (COMPONENT_REF);
10286 break;
10288 case CPP_OPEN_PAREN:
10289 /* Consume the `('. */
10290 cp_lexer_consume_token (parser->lexer);
10291 /* Look for the matching `)'. */
10292 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
10293 return ansi_opname (CALL_EXPR);
10295 case CPP_OPEN_SQUARE:
10296 /* Consume the `['. */
10297 cp_lexer_consume_token (parser->lexer);
10298 /* Look for the matching `]'. */
10299 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
10300 return ansi_opname (ARRAY_REF);
10302 default:
10303 /* Anything else is an error. */
10304 break;
10307 /* If we have selected an identifier, we need to consume the
10308 operator token. */
10309 if (id)
10310 cp_lexer_consume_token (parser->lexer);
10311 /* Otherwise, no valid operator name was present. */
10312 else
10314 cp_parser_error (parser, "expected operator");
10315 id = error_mark_node;
10318 return id;
10321 /* Parse a template-declaration.
10323 template-declaration:
10324 export [opt] template < template-parameter-list > declaration
10326 If MEMBER_P is TRUE, this template-declaration occurs within a
10327 class-specifier.
10329 The grammar rule given by the standard isn't correct. What
10330 is really meant is:
10332 template-declaration:
10333 export [opt] template-parameter-list-seq
10334 decl-specifier-seq [opt] init-declarator [opt] ;
10335 export [opt] template-parameter-list-seq
10336 function-definition
10338 template-parameter-list-seq:
10339 template-parameter-list-seq [opt]
10340 template < template-parameter-list > */
10342 static void
10343 cp_parser_template_declaration (cp_parser* parser, bool member_p)
10345 /* Check for `export'. */
10346 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
10348 /* Consume the `export' token. */
10349 cp_lexer_consume_token (parser->lexer);
10350 /* Warn that we do not support `export'. */
10351 warning (0, "keyword %<export%> not implemented, and will be ignored");
10354 cp_parser_template_declaration_after_export (parser, member_p);
10357 /* Parse a template-parameter-list.
10359 template-parameter-list:
10360 template-parameter
10361 template-parameter-list , template-parameter
10363 Returns a TREE_LIST. Each node represents a template parameter.
10364 The nodes are connected via their TREE_CHAINs. */
10366 static tree
10367 cp_parser_template_parameter_list (cp_parser* parser)
10369 tree parameter_list = NULL_TREE;
10371 begin_template_parm_list ();
10372 while (true)
10374 tree parameter;
10375 bool is_non_type;
10376 bool is_parameter_pack;
10377 location_t parm_loc;
10379 /* Parse the template-parameter. */
10380 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
10381 parameter = cp_parser_template_parameter (parser,
10382 &is_non_type,
10383 &is_parameter_pack);
10384 /* Add it to the list. */
10385 if (parameter != error_mark_node)
10386 parameter_list = process_template_parm (parameter_list,
10387 parm_loc,
10388 parameter,
10389 is_non_type,
10390 is_parameter_pack);
10391 else
10393 tree err_parm = build_tree_list (parameter, parameter);
10394 TREE_VALUE (err_parm) = error_mark_node;
10395 parameter_list = chainon (parameter_list, err_parm);
10398 /* If the next token is not a `,', we're done. */
10399 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10400 break;
10401 /* Otherwise, consume the `,' token. */
10402 cp_lexer_consume_token (parser->lexer);
10405 return end_template_parm_list (parameter_list);
10408 /* Parse a template-parameter.
10410 template-parameter:
10411 type-parameter
10412 parameter-declaration
10414 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
10415 the parameter. The TREE_PURPOSE is the default value, if any.
10416 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
10417 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
10418 set to true iff this parameter is a parameter pack. */
10420 static tree
10421 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
10422 bool *is_parameter_pack)
10424 cp_token *token;
10425 cp_parameter_declarator *parameter_declarator;
10426 cp_declarator *id_declarator;
10427 tree parm;
10429 /* Assume it is a type parameter or a template parameter. */
10430 *is_non_type = false;
10431 /* Assume it not a parameter pack. */
10432 *is_parameter_pack = false;
10433 /* Peek at the next token. */
10434 token = cp_lexer_peek_token (parser->lexer);
10435 /* If it is `class' or `template', we have a type-parameter. */
10436 if (token->keyword == RID_TEMPLATE)
10437 return cp_parser_type_parameter (parser, is_parameter_pack);
10438 /* If it is `class' or `typename' we do not know yet whether it is a
10439 type parameter or a non-type parameter. Consider:
10441 template <typename T, typename T::X X> ...
10445 template <class C, class D*> ...
10447 Here, the first parameter is a type parameter, and the second is
10448 a non-type parameter. We can tell by looking at the token after
10449 the identifier -- if it is a `,', `=', or `>' then we have a type
10450 parameter. */
10451 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
10453 /* Peek at the token after `class' or `typename'. */
10454 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10455 /* If it's an ellipsis, we have a template type parameter
10456 pack. */
10457 if (token->type == CPP_ELLIPSIS)
10458 return cp_parser_type_parameter (parser, is_parameter_pack);
10459 /* If it's an identifier, skip it. */
10460 if (token->type == CPP_NAME)
10461 token = cp_lexer_peek_nth_token (parser->lexer, 3);
10462 /* Now, see if the token looks like the end of a template
10463 parameter. */
10464 if (token->type == CPP_COMMA
10465 || token->type == CPP_EQ
10466 || token->type == CPP_GREATER)
10467 return cp_parser_type_parameter (parser, is_parameter_pack);
10470 /* Otherwise, it is a non-type parameter.
10472 [temp.param]
10474 When parsing a default template-argument for a non-type
10475 template-parameter, the first non-nested `>' is taken as the end
10476 of the template parameter-list rather than a greater-than
10477 operator. */
10478 *is_non_type = true;
10479 parameter_declarator
10480 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
10481 /*parenthesized_p=*/NULL);
10483 /* If the parameter declaration is marked as a parameter pack, set
10484 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
10485 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
10486 grokdeclarator. */
10487 if (parameter_declarator
10488 && parameter_declarator->declarator
10489 && parameter_declarator->declarator->parameter_pack_p)
10491 *is_parameter_pack = true;
10492 parameter_declarator->declarator->parameter_pack_p = false;
10495 /* If the next token is an ellipsis, and we don't already have it
10496 marked as a parameter pack, then we have a parameter pack (that
10497 has no declarator). */
10498 if (!*is_parameter_pack
10499 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
10500 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
10502 /* Consume the `...'. */
10503 cp_lexer_consume_token (parser->lexer);
10504 maybe_warn_variadic_templates ();
10506 *is_parameter_pack = true;
10508 /* We might end up with a pack expansion as the type of the non-type
10509 template parameter, in which case this is a non-type template
10510 parameter pack. */
10511 else if (parameter_declarator
10512 && parameter_declarator->decl_specifiers.type
10513 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
10515 *is_parameter_pack = true;
10516 parameter_declarator->decl_specifiers.type =
10517 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
10520 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10522 /* Parameter packs cannot have default arguments. However, a
10523 user may try to do so, so we'll parse them and give an
10524 appropriate diagnostic here. */
10526 /* Consume the `='. */
10527 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
10528 cp_lexer_consume_token (parser->lexer);
10530 /* Find the name of the parameter pack. */
10531 id_declarator = parameter_declarator->declarator;
10532 while (id_declarator && id_declarator->kind != cdk_id)
10533 id_declarator = id_declarator->declarator;
10535 if (id_declarator && id_declarator->kind == cdk_id)
10536 error_at (start_token->location,
10537 "template parameter pack %qD cannot have a default argument",
10538 id_declarator->u.id.unqualified_name);
10539 else
10540 error_at (start_token->location,
10541 "template parameter pack cannot have a default argument");
10543 /* Parse the default argument, but throw away the result. */
10544 cp_parser_default_argument (parser, /*template_parm_p=*/true);
10547 parm = grokdeclarator (parameter_declarator->declarator,
10548 &parameter_declarator->decl_specifiers,
10549 TPARM, /*initialized=*/0,
10550 /*attrlist=*/NULL);
10551 if (parm == error_mark_node)
10552 return error_mark_node;
10554 return build_tree_list (parameter_declarator->default_argument, parm);
10557 /* Parse a type-parameter.
10559 type-parameter:
10560 class identifier [opt]
10561 class identifier [opt] = type-id
10562 typename identifier [opt]
10563 typename identifier [opt] = type-id
10564 template < template-parameter-list > class identifier [opt]
10565 template < template-parameter-list > class identifier [opt]
10566 = id-expression
10568 GNU Extension (variadic templates):
10570 type-parameter:
10571 class ... identifier [opt]
10572 typename ... identifier [opt]
10574 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
10575 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
10576 the declaration of the parameter.
10578 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
10580 static tree
10581 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
10583 cp_token *token;
10584 tree parameter;
10586 /* Look for a keyword to tell us what kind of parameter this is. */
10587 token = cp_parser_require (parser, CPP_KEYWORD,
10588 "%<class%>, %<typename%>, or %<template%>");
10589 if (!token)
10590 return error_mark_node;
10592 switch (token->keyword)
10594 case RID_CLASS:
10595 case RID_TYPENAME:
10597 tree identifier;
10598 tree default_argument;
10600 /* If the next token is an ellipsis, we have a template
10601 argument pack. */
10602 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10604 /* Consume the `...' token. */
10605 cp_lexer_consume_token (parser->lexer);
10606 maybe_warn_variadic_templates ();
10608 *is_parameter_pack = true;
10611 /* If the next token is an identifier, then it names the
10612 parameter. */
10613 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10614 identifier = cp_parser_identifier (parser);
10615 else
10616 identifier = NULL_TREE;
10618 /* Create the parameter. */
10619 parameter = finish_template_type_parm (class_type_node, identifier);
10621 /* If the next token is an `=', we have a default argument. */
10622 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10624 /* Consume the `=' token. */
10625 cp_lexer_consume_token (parser->lexer);
10626 /* Parse the default-argument. */
10627 push_deferring_access_checks (dk_no_deferred);
10628 default_argument = cp_parser_type_id (parser);
10630 /* Template parameter packs cannot have default
10631 arguments. */
10632 if (*is_parameter_pack)
10634 if (identifier)
10635 error_at (token->location,
10636 "template parameter pack %qD cannot have a "
10637 "default argument", identifier);
10638 else
10639 error_at (token->location,
10640 "template parameter packs cannot have "
10641 "default arguments");
10642 default_argument = NULL_TREE;
10644 pop_deferring_access_checks ();
10646 else
10647 default_argument = NULL_TREE;
10649 /* Create the combined representation of the parameter and the
10650 default argument. */
10651 parameter = build_tree_list (default_argument, parameter);
10653 break;
10655 case RID_TEMPLATE:
10657 tree identifier;
10658 tree default_argument;
10660 /* Look for the `<'. */
10661 cp_parser_require (parser, CPP_LESS, "%<<%>");
10662 /* Parse the template-parameter-list. */
10663 cp_parser_template_parameter_list (parser);
10664 /* Look for the `>'. */
10665 cp_parser_require (parser, CPP_GREATER, "%<>%>");
10666 /* Look for the `class' keyword. */
10667 cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
10668 /* If the next token is an ellipsis, we have a template
10669 argument pack. */
10670 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10672 /* Consume the `...' token. */
10673 cp_lexer_consume_token (parser->lexer);
10674 maybe_warn_variadic_templates ();
10676 *is_parameter_pack = true;
10678 /* If the next token is an `=', then there is a
10679 default-argument. If the next token is a `>', we are at
10680 the end of the parameter-list. If the next token is a `,',
10681 then we are at the end of this parameter. */
10682 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10683 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
10684 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10686 identifier = cp_parser_identifier (parser);
10687 /* Treat invalid names as if the parameter were nameless. */
10688 if (identifier == error_mark_node)
10689 identifier = NULL_TREE;
10691 else
10692 identifier = NULL_TREE;
10694 /* Create the template parameter. */
10695 parameter = finish_template_template_parm (class_type_node,
10696 identifier);
10698 /* If the next token is an `=', then there is a
10699 default-argument. */
10700 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10702 bool is_template;
10704 /* Consume the `='. */
10705 cp_lexer_consume_token (parser->lexer);
10706 /* Parse the id-expression. */
10707 push_deferring_access_checks (dk_no_deferred);
10708 /* save token before parsing the id-expression, for error
10709 reporting */
10710 token = cp_lexer_peek_token (parser->lexer);
10711 default_argument
10712 = cp_parser_id_expression (parser,
10713 /*template_keyword_p=*/false,
10714 /*check_dependency_p=*/true,
10715 /*template_p=*/&is_template,
10716 /*declarator_p=*/false,
10717 /*optional_p=*/false);
10718 if (TREE_CODE (default_argument) == TYPE_DECL)
10719 /* If the id-expression was a template-id that refers to
10720 a template-class, we already have the declaration here,
10721 so no further lookup is needed. */
10723 else
10724 /* Look up the name. */
10725 default_argument
10726 = cp_parser_lookup_name (parser, default_argument,
10727 none_type,
10728 /*is_template=*/is_template,
10729 /*is_namespace=*/false,
10730 /*check_dependency=*/true,
10731 /*ambiguous_decls=*/NULL,
10732 token->location);
10733 /* See if the default argument is valid. */
10734 default_argument
10735 = check_template_template_default_arg (default_argument);
10737 /* Template parameter packs cannot have default
10738 arguments. */
10739 if (*is_parameter_pack)
10741 if (identifier)
10742 error_at (token->location,
10743 "template parameter pack %qD cannot "
10744 "have a default argument",
10745 identifier);
10746 else
10747 error_at (token->location, "template parameter packs cannot "
10748 "have default arguments");
10749 default_argument = NULL_TREE;
10751 pop_deferring_access_checks ();
10753 else
10754 default_argument = NULL_TREE;
10756 /* Create the combined representation of the parameter and the
10757 default argument. */
10758 parameter = build_tree_list (default_argument, parameter);
10760 break;
10762 default:
10763 gcc_unreachable ();
10764 break;
10767 return parameter;
10770 /* Parse a template-id.
10772 template-id:
10773 template-name < template-argument-list [opt] >
10775 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
10776 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
10777 returned. Otherwise, if the template-name names a function, or set
10778 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
10779 names a class, returns a TYPE_DECL for the specialization.
10781 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
10782 uninstantiated templates. */
10784 static tree
10785 cp_parser_template_id (cp_parser *parser,
10786 bool template_keyword_p,
10787 bool check_dependency_p,
10788 bool is_declaration)
10790 int i;
10791 tree templ;
10792 tree arguments;
10793 tree template_id;
10794 cp_token_position start_of_id = 0;
10795 deferred_access_check *chk;
10796 VEC (deferred_access_check,gc) *access_check;
10797 cp_token *next_token = NULL, *next_token_2 = NULL;
10798 bool is_identifier;
10800 /* If the next token corresponds to a template-id, there is no need
10801 to reparse it. */
10802 next_token = cp_lexer_peek_token (parser->lexer);
10803 if (next_token->type == CPP_TEMPLATE_ID)
10805 struct tree_check *check_value;
10807 /* Get the stored value. */
10808 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
10809 /* Perform any access checks that were deferred. */
10810 access_check = check_value->checks;
10811 if (access_check)
10813 for (i = 0 ;
10814 VEC_iterate (deferred_access_check, access_check, i, chk) ;
10815 ++i)
10817 perform_or_defer_access_check (chk->binfo,
10818 chk->decl,
10819 chk->diag_decl);
10822 /* Return the stored value. */
10823 return check_value->value;
10826 /* Avoid performing name lookup if there is no possibility of
10827 finding a template-id. */
10828 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
10829 || (next_token->type == CPP_NAME
10830 && !cp_parser_nth_token_starts_template_argument_list_p
10831 (parser, 2)))
10833 cp_parser_error (parser, "expected template-id");
10834 return error_mark_node;
10837 /* Remember where the template-id starts. */
10838 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
10839 start_of_id = cp_lexer_token_position (parser->lexer, false);
10841 push_deferring_access_checks (dk_deferred);
10843 /* Parse the template-name. */
10844 is_identifier = false;
10845 templ = cp_parser_template_name (parser, template_keyword_p,
10846 check_dependency_p,
10847 is_declaration,
10848 &is_identifier);
10849 if (templ == error_mark_node || is_identifier)
10851 pop_deferring_access_checks ();
10852 return templ;
10855 /* If we find the sequence `[:' after a template-name, it's probably
10856 a digraph-typo for `< ::'. Substitute the tokens and check if we can
10857 parse correctly the argument list. */
10858 next_token = cp_lexer_peek_token (parser->lexer);
10859 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10860 if (next_token->type == CPP_OPEN_SQUARE
10861 && next_token->flags & DIGRAPH
10862 && next_token_2->type == CPP_COLON
10863 && !(next_token_2->flags & PREV_WHITE))
10865 cp_parser_parse_tentatively (parser);
10866 /* Change `:' into `::'. */
10867 next_token_2->type = CPP_SCOPE;
10868 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
10869 CPP_LESS. */
10870 cp_lexer_consume_token (parser->lexer);
10872 /* Parse the arguments. */
10873 arguments = cp_parser_enclosed_template_argument_list (parser);
10874 if (!cp_parser_parse_definitely (parser))
10876 /* If we couldn't parse an argument list, then we revert our changes
10877 and return simply an error. Maybe this is not a template-id
10878 after all. */
10879 next_token_2->type = CPP_COLON;
10880 cp_parser_error (parser, "expected %<<%>");
10881 pop_deferring_access_checks ();
10882 return error_mark_node;
10884 /* Otherwise, emit an error about the invalid digraph, but continue
10885 parsing because we got our argument list. */
10886 if (permerror (next_token->location,
10887 "%<<::%> cannot begin a template-argument list"))
10889 static bool hint = false;
10890 inform (next_token->location,
10891 "%<<:%> is an alternate spelling for %<[%>."
10892 " Insert whitespace between %<<%> and %<::%>");
10893 if (!hint && !flag_permissive)
10895 inform (next_token->location, "(if you use %<-fpermissive%>"
10896 " G++ will accept your code)");
10897 hint = true;
10901 else
10903 /* Look for the `<' that starts the template-argument-list. */
10904 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10906 pop_deferring_access_checks ();
10907 return error_mark_node;
10909 /* Parse the arguments. */
10910 arguments = cp_parser_enclosed_template_argument_list (parser);
10913 /* Build a representation of the specialization. */
10914 if (TREE_CODE (templ) == IDENTIFIER_NODE)
10915 template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10916 else if (DECL_CLASS_TEMPLATE_P (templ)
10917 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10919 bool entering_scope;
10920 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10921 template (rather than some instantiation thereof) only if
10922 is not nested within some other construct. For example, in
10923 "template <typename T> void f(T) { A<T>::", A<T> is just an
10924 instantiation of A. */
10925 entering_scope = (template_parm_scope_p ()
10926 && cp_lexer_next_token_is (parser->lexer,
10927 CPP_SCOPE));
10928 template_id
10929 = finish_template_type (templ, arguments, entering_scope);
10931 else
10933 /* If it's not a class-template or a template-template, it should be
10934 a function-template. */
10935 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10936 || TREE_CODE (templ) == OVERLOAD
10937 || BASELINK_P (templ)));
10939 template_id = lookup_template_function (templ, arguments);
10942 /* If parsing tentatively, replace the sequence of tokens that makes
10943 up the template-id with a CPP_TEMPLATE_ID token. That way,
10944 should we re-parse the token stream, we will not have to repeat
10945 the effort required to do the parse, nor will we issue duplicate
10946 error messages about problems during instantiation of the
10947 template. */
10948 if (start_of_id)
10950 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10952 /* Reset the contents of the START_OF_ID token. */
10953 token->type = CPP_TEMPLATE_ID;
10954 /* Retrieve any deferred checks. Do not pop this access checks yet
10955 so the memory will not be reclaimed during token replacing below. */
10956 token->u.tree_check_value = GGC_CNEW (struct tree_check);
10957 token->u.tree_check_value->value = template_id;
10958 token->u.tree_check_value->checks = get_deferred_access_checks ();
10959 token->keyword = RID_MAX;
10961 /* Purge all subsequent tokens. */
10962 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10964 /* ??? Can we actually assume that, if template_id ==
10965 error_mark_node, we will have issued a diagnostic to the
10966 user, as opposed to simply marking the tentative parse as
10967 failed? */
10968 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10969 error_at (token->location, "parse error in template argument list");
10972 pop_deferring_access_checks ();
10973 return template_id;
10976 /* Parse a template-name.
10978 template-name:
10979 identifier
10981 The standard should actually say:
10983 template-name:
10984 identifier
10985 operator-function-id
10987 A defect report has been filed about this issue.
10989 A conversion-function-id cannot be a template name because they cannot
10990 be part of a template-id. In fact, looking at this code:
10992 a.operator K<int>()
10994 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10995 It is impossible to call a templated conversion-function-id with an
10996 explicit argument list, since the only allowed template parameter is
10997 the type to which it is converting.
10999 If TEMPLATE_KEYWORD_P is true, then we have just seen the
11000 `template' keyword, in a construction like:
11002 T::template f<3>()
11004 In that case `f' is taken to be a template-name, even though there
11005 is no way of knowing for sure.
11007 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
11008 name refers to a set of overloaded functions, at least one of which
11009 is a template, or an IDENTIFIER_NODE with the name of the template,
11010 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
11011 names are looked up inside uninstantiated templates. */
11013 static tree
11014 cp_parser_template_name (cp_parser* parser,
11015 bool template_keyword_p,
11016 bool check_dependency_p,
11017 bool is_declaration,
11018 bool *is_identifier)
11020 tree identifier;
11021 tree decl;
11022 tree fns;
11023 cp_token *token = cp_lexer_peek_token (parser->lexer);
11025 /* If the next token is `operator', then we have either an
11026 operator-function-id or a conversion-function-id. */
11027 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
11029 /* We don't know whether we're looking at an
11030 operator-function-id or a conversion-function-id. */
11031 cp_parser_parse_tentatively (parser);
11032 /* Try an operator-function-id. */
11033 identifier = cp_parser_operator_function_id (parser);
11034 /* If that didn't work, try a conversion-function-id. */
11035 if (!cp_parser_parse_definitely (parser))
11037 cp_parser_error (parser, "expected template-name");
11038 return error_mark_node;
11041 /* Look for the identifier. */
11042 else
11043 identifier = cp_parser_identifier (parser);
11045 /* If we didn't find an identifier, we don't have a template-id. */
11046 if (identifier == error_mark_node)
11047 return error_mark_node;
11049 /* If the name immediately followed the `template' keyword, then it
11050 is a template-name. However, if the next token is not `<', then
11051 we do not treat it as a template-name, since it is not being used
11052 as part of a template-id. This enables us to handle constructs
11053 like:
11055 template <typename T> struct S { S(); };
11056 template <typename T> S<T>::S();
11058 correctly. We would treat `S' as a template -- if it were `S<T>'
11059 -- but we do not if there is no `<'. */
11061 if (processing_template_decl
11062 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
11064 /* In a declaration, in a dependent context, we pretend that the
11065 "template" keyword was present in order to improve error
11066 recovery. For example, given:
11068 template <typename T> void f(T::X<int>);
11070 we want to treat "X<int>" as a template-id. */
11071 if (is_declaration
11072 && !template_keyword_p
11073 && parser->scope && TYPE_P (parser->scope)
11074 && check_dependency_p
11075 && dependent_scope_p (parser->scope)
11076 /* Do not do this for dtors (or ctors), since they never
11077 need the template keyword before their name. */
11078 && !constructor_name_p (identifier, parser->scope))
11080 cp_token_position start = 0;
11082 /* Explain what went wrong. */
11083 error_at (token->location, "non-template %qD used as template",
11084 identifier);
11085 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
11086 parser->scope, identifier);
11087 /* If parsing tentatively, find the location of the "<" token. */
11088 if (cp_parser_simulate_error (parser))
11089 start = cp_lexer_token_position (parser->lexer, true);
11090 /* Parse the template arguments so that we can issue error
11091 messages about them. */
11092 cp_lexer_consume_token (parser->lexer);
11093 cp_parser_enclosed_template_argument_list (parser);
11094 /* Skip tokens until we find a good place from which to
11095 continue parsing. */
11096 cp_parser_skip_to_closing_parenthesis (parser,
11097 /*recovering=*/true,
11098 /*or_comma=*/true,
11099 /*consume_paren=*/false);
11100 /* If parsing tentatively, permanently remove the
11101 template argument list. That will prevent duplicate
11102 error messages from being issued about the missing
11103 "template" keyword. */
11104 if (start)
11105 cp_lexer_purge_tokens_after (parser->lexer, start);
11106 if (is_identifier)
11107 *is_identifier = true;
11108 return identifier;
11111 /* If the "template" keyword is present, then there is generally
11112 no point in doing name-lookup, so we just return IDENTIFIER.
11113 But, if the qualifying scope is non-dependent then we can
11114 (and must) do name-lookup normally. */
11115 if (template_keyword_p
11116 && (!parser->scope
11117 || (TYPE_P (parser->scope)
11118 && dependent_type_p (parser->scope))))
11119 return identifier;
11122 /* Look up the name. */
11123 decl = cp_parser_lookup_name (parser, identifier,
11124 none_type,
11125 /*is_template=*/true,
11126 /*is_namespace=*/false,
11127 check_dependency_p,
11128 /*ambiguous_decls=*/NULL,
11129 token->location);
11131 /* If DECL is a template, then the name was a template-name. */
11132 if (TREE_CODE (decl) == TEMPLATE_DECL)
11134 else
11136 tree fn = NULL_TREE;
11138 /* The standard does not explicitly indicate whether a name that
11139 names a set of overloaded declarations, some of which are
11140 templates, is a template-name. However, such a name should
11141 be a template-name; otherwise, there is no way to form a
11142 template-id for the overloaded templates. */
11143 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11144 if (TREE_CODE (fns) == OVERLOAD)
11145 for (fn = fns; fn; fn = OVL_NEXT (fn))
11146 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11147 break;
11149 if (!fn)
11151 /* The name does not name a template. */
11152 cp_parser_error (parser, "expected template-name");
11153 return error_mark_node;
11157 /* If DECL is dependent, and refers to a function, then just return
11158 its name; we will look it up again during template instantiation. */
11159 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11161 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
11162 if (TYPE_P (scope) && dependent_type_p (scope))
11163 return identifier;
11166 return decl;
11169 /* Parse a template-argument-list.
11171 template-argument-list:
11172 template-argument ... [opt]
11173 template-argument-list , template-argument ... [opt]
11175 Returns a TREE_VEC containing the arguments. */
11177 static tree
11178 cp_parser_template_argument_list (cp_parser* parser)
11180 tree fixed_args[10];
11181 unsigned n_args = 0;
11182 unsigned alloced = 10;
11183 tree *arg_ary = fixed_args;
11184 tree vec;
11185 bool saved_in_template_argument_list_p;
11186 bool saved_ice_p;
11187 bool saved_non_ice_p;
11189 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
11190 parser->in_template_argument_list_p = true;
11191 /* Even if the template-id appears in an integral
11192 constant-expression, the contents of the argument list do
11193 not. */
11194 saved_ice_p = parser->integral_constant_expression_p;
11195 parser->integral_constant_expression_p = false;
11196 saved_non_ice_p = parser->non_integral_constant_expression_p;
11197 parser->non_integral_constant_expression_p = false;
11198 /* Parse the arguments. */
11201 tree argument;
11203 if (n_args)
11204 /* Consume the comma. */
11205 cp_lexer_consume_token (parser->lexer);
11207 /* Parse the template-argument. */
11208 argument = cp_parser_template_argument (parser);
11210 /* If the next token is an ellipsis, we're expanding a template
11211 argument pack. */
11212 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11214 if (argument == error_mark_node)
11216 cp_token *token = cp_lexer_peek_token (parser->lexer);
11217 error_at (token->location,
11218 "expected parameter pack before %<...%>");
11220 /* Consume the `...' token. */
11221 cp_lexer_consume_token (parser->lexer);
11223 /* Make the argument into a TYPE_PACK_EXPANSION or
11224 EXPR_PACK_EXPANSION. */
11225 argument = make_pack_expansion (argument);
11228 if (n_args == alloced)
11230 alloced *= 2;
11232 if (arg_ary == fixed_args)
11234 arg_ary = XNEWVEC (tree, alloced);
11235 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
11237 else
11238 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
11240 arg_ary[n_args++] = argument;
11242 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
11244 vec = make_tree_vec (n_args);
11246 while (n_args--)
11247 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
11249 if (arg_ary != fixed_args)
11250 free (arg_ary);
11251 parser->non_integral_constant_expression_p = saved_non_ice_p;
11252 parser->integral_constant_expression_p = saved_ice_p;
11253 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
11254 #ifdef ENABLE_CHECKING
11255 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
11256 #endif
11257 return vec;
11260 /* Parse a template-argument.
11262 template-argument:
11263 assignment-expression
11264 type-id
11265 id-expression
11267 The representation is that of an assignment-expression, type-id, or
11268 id-expression -- except that the qualified id-expression is
11269 evaluated, so that the value returned is either a DECL or an
11270 OVERLOAD.
11272 Although the standard says "assignment-expression", it forbids
11273 throw-expressions or assignments in the template argument.
11274 Therefore, we use "conditional-expression" instead. */
11276 static tree
11277 cp_parser_template_argument (cp_parser* parser)
11279 tree argument;
11280 bool template_p;
11281 bool address_p;
11282 bool maybe_type_id = false;
11283 cp_token *token = NULL, *argument_start_token = NULL;
11284 cp_id_kind idk;
11286 /* There's really no way to know what we're looking at, so we just
11287 try each alternative in order.
11289 [temp.arg]
11291 In a template-argument, an ambiguity between a type-id and an
11292 expression is resolved to a type-id, regardless of the form of
11293 the corresponding template-parameter.
11295 Therefore, we try a type-id first. */
11296 cp_parser_parse_tentatively (parser);
11297 argument = cp_parser_template_type_arg (parser);
11298 /* If there was no error parsing the type-id but the next token is a
11299 '>>', our behavior depends on which dialect of C++ we're
11300 parsing. In C++98, we probably found a typo for '> >'. But there
11301 are type-id which are also valid expressions. For instance:
11303 struct X { int operator >> (int); };
11304 template <int V> struct Foo {};
11305 Foo<X () >> 5> r;
11307 Here 'X()' is a valid type-id of a function type, but the user just
11308 wanted to write the expression "X() >> 5". Thus, we remember that we
11309 found a valid type-id, but we still try to parse the argument as an
11310 expression to see what happens.
11312 In C++0x, the '>>' will be considered two separate '>'
11313 tokens. */
11314 if (!cp_parser_error_occurred (parser)
11315 && cxx_dialect == cxx98
11316 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
11318 maybe_type_id = true;
11319 cp_parser_abort_tentative_parse (parser);
11321 else
11323 /* If the next token isn't a `,' or a `>', then this argument wasn't
11324 really finished. This means that the argument is not a valid
11325 type-id. */
11326 if (!cp_parser_next_token_ends_template_argument_p (parser))
11327 cp_parser_error (parser, "expected template-argument");
11328 /* If that worked, we're done. */
11329 if (cp_parser_parse_definitely (parser))
11330 return argument;
11332 /* We're still not sure what the argument will be. */
11333 cp_parser_parse_tentatively (parser);
11334 /* Try a template. */
11335 argument_start_token = cp_lexer_peek_token (parser->lexer);
11336 argument = cp_parser_id_expression (parser,
11337 /*template_keyword_p=*/false,
11338 /*check_dependency_p=*/true,
11339 &template_p,
11340 /*declarator_p=*/false,
11341 /*optional_p=*/false);
11342 /* If the next token isn't a `,' or a `>', then this argument wasn't
11343 really finished. */
11344 if (!cp_parser_next_token_ends_template_argument_p (parser))
11345 cp_parser_error (parser, "expected template-argument");
11346 if (!cp_parser_error_occurred (parser))
11348 /* Figure out what is being referred to. If the id-expression
11349 was for a class template specialization, then we will have a
11350 TYPE_DECL at this point. There is no need to do name lookup
11351 at this point in that case. */
11352 if (TREE_CODE (argument) != TYPE_DECL)
11353 argument = cp_parser_lookup_name (parser, argument,
11354 none_type,
11355 /*is_template=*/template_p,
11356 /*is_namespace=*/false,
11357 /*check_dependency=*/true,
11358 /*ambiguous_decls=*/NULL,
11359 argument_start_token->location);
11360 if (TREE_CODE (argument) != TEMPLATE_DECL
11361 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
11362 cp_parser_error (parser, "expected template-name");
11364 if (cp_parser_parse_definitely (parser))
11365 return argument;
11366 /* It must be a non-type argument. There permitted cases are given
11367 in [temp.arg.nontype]:
11369 -- an integral constant-expression of integral or enumeration
11370 type; or
11372 -- the name of a non-type template-parameter; or
11374 -- the name of an object or function with external linkage...
11376 -- the address of an object or function with external linkage...
11378 -- a pointer to member... */
11379 /* Look for a non-type template parameter. */
11380 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11382 cp_parser_parse_tentatively (parser);
11383 argument = cp_parser_primary_expression (parser,
11384 /*address_p=*/false,
11385 /*cast_p=*/false,
11386 /*template_arg_p=*/true,
11387 &idk);
11388 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
11389 || !cp_parser_next_token_ends_template_argument_p (parser))
11390 cp_parser_simulate_error (parser);
11391 if (cp_parser_parse_definitely (parser))
11392 return argument;
11395 /* If the next token is "&", the argument must be the address of an
11396 object or function with external linkage. */
11397 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
11398 if (address_p)
11399 cp_lexer_consume_token (parser->lexer);
11400 /* See if we might have an id-expression. */
11401 token = cp_lexer_peek_token (parser->lexer);
11402 if (token->type == CPP_NAME
11403 || token->keyword == RID_OPERATOR
11404 || token->type == CPP_SCOPE
11405 || token->type == CPP_TEMPLATE_ID
11406 || token->type == CPP_NESTED_NAME_SPECIFIER)
11408 cp_parser_parse_tentatively (parser);
11409 argument = cp_parser_primary_expression (parser,
11410 address_p,
11411 /*cast_p=*/false,
11412 /*template_arg_p=*/true,
11413 &idk);
11414 if (cp_parser_error_occurred (parser)
11415 || !cp_parser_next_token_ends_template_argument_p (parser))
11416 cp_parser_abort_tentative_parse (parser);
11417 else
11419 tree probe;
11421 if (TREE_CODE (argument) == INDIRECT_REF)
11423 gcc_assert (REFERENCE_REF_P (argument));
11424 argument = TREE_OPERAND (argument, 0);
11427 /* If we're in a template, we represent a qualified-id referring
11428 to a static data member as a SCOPE_REF even if the scope isn't
11429 dependent so that we can check access control later. */
11430 probe = argument;
11431 if (TREE_CODE (probe) == SCOPE_REF)
11432 probe = TREE_OPERAND (probe, 1);
11433 if (TREE_CODE (probe) == VAR_DECL)
11435 /* A variable without external linkage might still be a
11436 valid constant-expression, so no error is issued here
11437 if the external-linkage check fails. */
11438 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
11439 cp_parser_simulate_error (parser);
11441 else if (is_overloaded_fn (argument))
11442 /* All overloaded functions are allowed; if the external
11443 linkage test does not pass, an error will be issued
11444 later. */
11446 else if (address_p
11447 && (TREE_CODE (argument) == OFFSET_REF
11448 || TREE_CODE (argument) == SCOPE_REF))
11449 /* A pointer-to-member. */
11451 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
11453 else
11454 cp_parser_simulate_error (parser);
11456 if (cp_parser_parse_definitely (parser))
11458 if (address_p)
11459 argument = build_x_unary_op (ADDR_EXPR, argument,
11460 tf_warning_or_error);
11461 return argument;
11465 /* If the argument started with "&", there are no other valid
11466 alternatives at this point. */
11467 if (address_p)
11469 cp_parser_error (parser, "invalid non-type template argument");
11470 return error_mark_node;
11473 /* If the argument wasn't successfully parsed as a type-id followed
11474 by '>>', the argument can only be a constant expression now.
11475 Otherwise, we try parsing the constant-expression tentatively,
11476 because the argument could really be a type-id. */
11477 if (maybe_type_id)
11478 cp_parser_parse_tentatively (parser);
11479 argument = cp_parser_constant_expression (parser,
11480 /*allow_non_constant_p=*/false,
11481 /*non_constant_p=*/NULL);
11482 argument = fold_non_dependent_expr (argument);
11483 if (!maybe_type_id)
11484 return argument;
11485 if (!cp_parser_next_token_ends_template_argument_p (parser))
11486 cp_parser_error (parser, "expected template-argument");
11487 if (cp_parser_parse_definitely (parser))
11488 return argument;
11489 /* We did our best to parse the argument as a non type-id, but that
11490 was the only alternative that matched (albeit with a '>' after
11491 it). We can assume it's just a typo from the user, and a
11492 diagnostic will then be issued. */
11493 return cp_parser_template_type_arg (parser);
11496 /* Parse an explicit-instantiation.
11498 explicit-instantiation:
11499 template declaration
11501 Although the standard says `declaration', what it really means is:
11503 explicit-instantiation:
11504 template decl-specifier-seq [opt] declarator [opt] ;
11506 Things like `template int S<int>::i = 5, int S<double>::j;' are not
11507 supposed to be allowed. A defect report has been filed about this
11508 issue.
11510 GNU Extension:
11512 explicit-instantiation:
11513 storage-class-specifier template
11514 decl-specifier-seq [opt] declarator [opt] ;
11515 function-specifier template
11516 decl-specifier-seq [opt] declarator [opt] ; */
11518 static void
11519 cp_parser_explicit_instantiation (cp_parser* parser)
11521 int declares_class_or_enum;
11522 cp_decl_specifier_seq decl_specifiers;
11523 tree extension_specifier = NULL_TREE;
11525 /* Look for an (optional) storage-class-specifier or
11526 function-specifier. */
11527 if (cp_parser_allow_gnu_extensions_p (parser))
11529 extension_specifier
11530 = cp_parser_storage_class_specifier_opt (parser);
11531 if (!extension_specifier)
11532 extension_specifier
11533 = cp_parser_function_specifier_opt (parser,
11534 /*decl_specs=*/NULL);
11537 /* Look for the `template' keyword. */
11538 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
11539 /* Let the front end know that we are processing an explicit
11540 instantiation. */
11541 begin_explicit_instantiation ();
11542 /* [temp.explicit] says that we are supposed to ignore access
11543 control while processing explicit instantiation directives. */
11544 push_deferring_access_checks (dk_no_check);
11545 /* Parse a decl-specifier-seq. */
11546 cp_parser_decl_specifier_seq (parser,
11547 CP_PARSER_FLAGS_OPTIONAL,
11548 &decl_specifiers,
11549 &declares_class_or_enum);
11550 /* If there was exactly one decl-specifier, and it declared a class,
11551 and there's no declarator, then we have an explicit type
11552 instantiation. */
11553 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
11555 tree type;
11557 type = check_tag_decl (&decl_specifiers);
11558 /* Turn access control back on for names used during
11559 template instantiation. */
11560 pop_deferring_access_checks ();
11561 if (type)
11562 do_type_instantiation (type, extension_specifier,
11563 /*complain=*/tf_error);
11565 else
11567 cp_declarator *declarator;
11568 tree decl;
11570 /* Parse the declarator. */
11571 declarator
11572 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11573 /*ctor_dtor_or_conv_p=*/NULL,
11574 /*parenthesized_p=*/NULL,
11575 /*member_p=*/false);
11576 if (declares_class_or_enum & 2)
11577 cp_parser_check_for_definition_in_return_type (declarator,
11578 decl_specifiers.type,
11579 decl_specifiers.type_location);
11580 if (declarator != cp_error_declarator)
11582 decl = grokdeclarator (declarator, &decl_specifiers,
11583 NORMAL, 0, &decl_specifiers.attributes);
11584 /* Turn access control back on for names used during
11585 template instantiation. */
11586 pop_deferring_access_checks ();
11587 /* Do the explicit instantiation. */
11588 do_decl_instantiation (decl, extension_specifier);
11590 else
11592 pop_deferring_access_checks ();
11593 /* Skip the body of the explicit instantiation. */
11594 cp_parser_skip_to_end_of_statement (parser);
11597 /* We're done with the instantiation. */
11598 end_explicit_instantiation ();
11600 cp_parser_consume_semicolon_at_end_of_statement (parser);
11603 /* Parse an explicit-specialization.
11605 explicit-specialization:
11606 template < > declaration
11608 Although the standard says `declaration', what it really means is:
11610 explicit-specialization:
11611 template <> decl-specifier [opt] init-declarator [opt] ;
11612 template <> function-definition
11613 template <> explicit-specialization
11614 template <> template-declaration */
11616 static void
11617 cp_parser_explicit_specialization (cp_parser* parser)
11619 bool need_lang_pop;
11620 cp_token *token = cp_lexer_peek_token (parser->lexer);
11622 /* Look for the `template' keyword. */
11623 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
11624 /* Look for the `<'. */
11625 cp_parser_require (parser, CPP_LESS, "%<<%>");
11626 /* Look for the `>'. */
11627 cp_parser_require (parser, CPP_GREATER, "%<>%>");
11628 /* We have processed another parameter list. */
11629 ++parser->num_template_parameter_lists;
11630 /* [temp]
11632 A template ... explicit specialization ... shall not have C
11633 linkage. */
11634 if (current_lang_name == lang_name_c)
11636 error_at (token->location, "template specialization with C linkage");
11637 /* Give it C++ linkage to avoid confusing other parts of the
11638 front end. */
11639 push_lang_context (lang_name_cplusplus);
11640 need_lang_pop = true;
11642 else
11643 need_lang_pop = false;
11644 /* Let the front end know that we are beginning a specialization. */
11645 if (!begin_specialization ())
11647 end_specialization ();
11648 return;
11651 /* If the next keyword is `template', we need to figure out whether
11652 or not we're looking a template-declaration. */
11653 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
11655 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
11656 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
11657 cp_parser_template_declaration_after_export (parser,
11658 /*member_p=*/false);
11659 else
11660 cp_parser_explicit_specialization (parser);
11662 else
11663 /* Parse the dependent declaration. */
11664 cp_parser_single_declaration (parser,
11665 /*checks=*/NULL,
11666 /*member_p=*/false,
11667 /*explicit_specialization_p=*/true,
11668 /*friend_p=*/NULL);
11669 /* We're done with the specialization. */
11670 end_specialization ();
11671 /* For the erroneous case of a template with C linkage, we pushed an
11672 implicit C++ linkage scope; exit that scope now. */
11673 if (need_lang_pop)
11674 pop_lang_context ();
11675 /* We're done with this parameter list. */
11676 --parser->num_template_parameter_lists;
11679 /* Parse a type-specifier.
11681 type-specifier:
11682 simple-type-specifier
11683 class-specifier
11684 enum-specifier
11685 elaborated-type-specifier
11686 cv-qualifier
11688 GNU Extension:
11690 type-specifier:
11691 __complex__
11693 Returns a representation of the type-specifier. For a
11694 class-specifier, enum-specifier, or elaborated-type-specifier, a
11695 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
11697 The parser flags FLAGS is used to control type-specifier parsing.
11699 If IS_DECLARATION is TRUE, then this type-specifier is appearing
11700 in a decl-specifier-seq.
11702 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
11703 class-specifier, enum-specifier, or elaborated-type-specifier, then
11704 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
11705 if a type is declared; 2 if it is defined. Otherwise, it is set to
11706 zero.
11708 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
11709 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
11710 is set to FALSE. */
11712 static tree
11713 cp_parser_type_specifier (cp_parser* parser,
11714 cp_parser_flags flags,
11715 cp_decl_specifier_seq *decl_specs,
11716 bool is_declaration,
11717 int* declares_class_or_enum,
11718 bool* is_cv_qualifier)
11720 tree type_spec = NULL_TREE;
11721 cp_token *token;
11722 enum rid keyword;
11723 cp_decl_spec ds = ds_last;
11725 /* Assume this type-specifier does not declare a new type. */
11726 if (declares_class_or_enum)
11727 *declares_class_or_enum = 0;
11728 /* And that it does not specify a cv-qualifier. */
11729 if (is_cv_qualifier)
11730 *is_cv_qualifier = false;
11731 /* Peek at the next token. */
11732 token = cp_lexer_peek_token (parser->lexer);
11734 /* If we're looking at a keyword, we can use that to guide the
11735 production we choose. */
11736 keyword = token->keyword;
11737 switch (keyword)
11739 case RID_ENUM:
11740 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
11741 goto elaborated_type_specifier;
11743 /* Look for the enum-specifier. */
11744 type_spec = cp_parser_enum_specifier (parser);
11745 /* If that worked, we're done. */
11746 if (type_spec)
11748 if (declares_class_or_enum)
11749 *declares_class_or_enum = 2;
11750 if (decl_specs)
11751 cp_parser_set_decl_spec_type (decl_specs,
11752 type_spec,
11753 token->location,
11754 /*user_defined_p=*/true);
11755 return type_spec;
11757 else
11758 goto elaborated_type_specifier;
11760 /* Any of these indicate either a class-specifier, or an
11761 elaborated-type-specifier. */
11762 case RID_CLASS:
11763 case RID_STRUCT:
11764 case RID_UNION:
11765 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
11766 goto elaborated_type_specifier;
11768 /* Parse tentatively so that we can back up if we don't find a
11769 class-specifier. */
11770 cp_parser_parse_tentatively (parser);
11771 /* Look for the class-specifier. */
11772 type_spec = cp_parser_class_specifier (parser);
11773 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
11774 /* If that worked, we're done. */
11775 if (cp_parser_parse_definitely (parser))
11777 if (declares_class_or_enum)
11778 *declares_class_or_enum = 2;
11779 if (decl_specs)
11780 cp_parser_set_decl_spec_type (decl_specs,
11781 type_spec,
11782 token->location,
11783 /*user_defined_p=*/true);
11784 return type_spec;
11787 /* Fall through. */
11788 elaborated_type_specifier:
11789 /* We're declaring (not defining) a class or enum. */
11790 if (declares_class_or_enum)
11791 *declares_class_or_enum = 1;
11793 /* Fall through. */
11794 case RID_TYPENAME:
11795 /* Look for an elaborated-type-specifier. */
11796 type_spec
11797 = (cp_parser_elaborated_type_specifier
11798 (parser,
11799 decl_specs && decl_specs->specs[(int) ds_friend],
11800 is_declaration));
11801 if (decl_specs)
11802 cp_parser_set_decl_spec_type (decl_specs,
11803 type_spec,
11804 token->location,
11805 /*user_defined_p=*/true);
11806 return type_spec;
11808 case RID_CONST:
11809 ds = ds_const;
11810 if (is_cv_qualifier)
11811 *is_cv_qualifier = true;
11812 break;
11814 case RID_VOLATILE:
11815 ds = ds_volatile;
11816 if (is_cv_qualifier)
11817 *is_cv_qualifier = true;
11818 break;
11820 case RID_RESTRICT:
11821 ds = ds_restrict;
11822 if (is_cv_qualifier)
11823 *is_cv_qualifier = true;
11824 break;
11826 case RID_COMPLEX:
11827 /* The `__complex__' keyword is a GNU extension. */
11828 ds = ds_complex;
11829 break;
11831 default:
11832 break;
11835 /* Handle simple keywords. */
11836 if (ds != ds_last)
11838 if (decl_specs)
11840 ++decl_specs->specs[(int)ds];
11841 decl_specs->any_specifiers_p = true;
11843 return cp_lexer_consume_token (parser->lexer)->u.value;
11846 /* If we do not already have a type-specifier, assume we are looking
11847 at a simple-type-specifier. */
11848 type_spec = cp_parser_simple_type_specifier (parser,
11849 decl_specs,
11850 flags);
11852 /* If we didn't find a type-specifier, and a type-specifier was not
11853 optional in this context, issue an error message. */
11854 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11856 cp_parser_error (parser, "expected type specifier");
11857 return error_mark_node;
11860 return type_spec;
11863 /* Parse a simple-type-specifier.
11865 simple-type-specifier:
11866 :: [opt] nested-name-specifier [opt] type-name
11867 :: [opt] nested-name-specifier template template-id
11868 char
11869 wchar_t
11870 bool
11871 short
11873 long
11874 signed
11875 unsigned
11876 float
11877 double
11878 void
11880 C++0x Extension:
11882 simple-type-specifier:
11883 auto
11884 decltype ( expression )
11885 char16_t
11886 char32_t
11888 GNU Extension:
11890 simple-type-specifier:
11891 __typeof__ unary-expression
11892 __typeof__ ( type-id )
11894 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
11895 appropriately updated. */
11897 static tree
11898 cp_parser_simple_type_specifier (cp_parser* parser,
11899 cp_decl_specifier_seq *decl_specs,
11900 cp_parser_flags flags)
11902 tree type = NULL_TREE;
11903 cp_token *token;
11905 /* Peek at the next token. */
11906 token = cp_lexer_peek_token (parser->lexer);
11908 /* If we're looking at a keyword, things are easy. */
11909 switch (token->keyword)
11911 case RID_CHAR:
11912 if (decl_specs)
11913 decl_specs->explicit_char_p = true;
11914 type = char_type_node;
11915 break;
11916 case RID_CHAR16:
11917 type = char16_type_node;
11918 break;
11919 case RID_CHAR32:
11920 type = char32_type_node;
11921 break;
11922 case RID_WCHAR:
11923 type = wchar_type_node;
11924 break;
11925 case RID_BOOL:
11926 type = boolean_type_node;
11927 break;
11928 case RID_SHORT:
11929 if (decl_specs)
11930 ++decl_specs->specs[(int) ds_short];
11931 type = short_integer_type_node;
11932 break;
11933 case RID_INT:
11934 if (decl_specs)
11935 decl_specs->explicit_int_p = true;
11936 type = integer_type_node;
11937 break;
11938 case RID_LONG:
11939 if (decl_specs)
11940 ++decl_specs->specs[(int) ds_long];
11941 type = long_integer_type_node;
11942 break;
11943 case RID_SIGNED:
11944 if (decl_specs)
11945 ++decl_specs->specs[(int) ds_signed];
11946 type = integer_type_node;
11947 break;
11948 case RID_UNSIGNED:
11949 if (decl_specs)
11950 ++decl_specs->specs[(int) ds_unsigned];
11951 type = unsigned_type_node;
11952 break;
11953 case RID_FLOAT:
11954 type = float_type_node;
11955 break;
11956 case RID_DOUBLE:
11957 type = double_type_node;
11958 break;
11959 case RID_VOID:
11960 type = void_type_node;
11961 break;
11963 case RID_AUTO:
11964 maybe_warn_cpp0x (CPP0X_AUTO);
11965 type = make_auto ();
11966 break;
11968 case RID_DECLTYPE:
11969 /* Parse the `decltype' type. */
11970 type = cp_parser_decltype (parser);
11972 if (decl_specs)
11973 cp_parser_set_decl_spec_type (decl_specs, type,
11974 token->location,
11975 /*user_defined_p=*/true);
11977 return type;
11979 case RID_TYPEOF:
11980 /* Consume the `typeof' token. */
11981 cp_lexer_consume_token (parser->lexer);
11982 /* Parse the operand to `typeof'. */
11983 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11984 /* If it is not already a TYPE, take its type. */
11985 if (!TYPE_P (type))
11986 type = finish_typeof (type);
11988 if (decl_specs)
11989 cp_parser_set_decl_spec_type (decl_specs, type,
11990 token->location,
11991 /*user_defined_p=*/true);
11993 return type;
11995 default:
11996 break;
11999 /* If the type-specifier was for a built-in type, we're done. */
12000 if (type)
12002 /* Record the type. */
12003 if (decl_specs
12004 && (token->keyword != RID_SIGNED
12005 && token->keyword != RID_UNSIGNED
12006 && token->keyword != RID_SHORT
12007 && token->keyword != RID_LONG))
12008 cp_parser_set_decl_spec_type (decl_specs,
12009 type,
12010 token->location,
12011 /*user_defined=*/false);
12012 if (decl_specs)
12013 decl_specs->any_specifiers_p = true;
12015 /* Consume the token. */
12016 cp_lexer_consume_token (parser->lexer);
12018 /* There is no valid C++ program where a non-template type is
12019 followed by a "<". That usually indicates that the user thought
12020 that the type was a template. */
12021 cp_parser_check_for_invalid_template_id (parser, type, token->location);
12023 return TYPE_NAME (type);
12026 /* The type-specifier must be a user-defined type. */
12027 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
12029 bool qualified_p;
12030 bool global_p;
12032 /* Don't gobble tokens or issue error messages if this is an
12033 optional type-specifier. */
12034 if (flags & CP_PARSER_FLAGS_OPTIONAL)
12035 cp_parser_parse_tentatively (parser);
12037 /* Look for the optional `::' operator. */
12038 global_p
12039 = (cp_parser_global_scope_opt (parser,
12040 /*current_scope_valid_p=*/false)
12041 != NULL_TREE);
12042 /* Look for the nested-name specifier. */
12043 qualified_p
12044 = (cp_parser_nested_name_specifier_opt (parser,
12045 /*typename_keyword_p=*/false,
12046 /*check_dependency_p=*/true,
12047 /*type_p=*/false,
12048 /*is_declaration=*/false)
12049 != NULL_TREE);
12050 token = cp_lexer_peek_token (parser->lexer);
12051 /* If we have seen a nested-name-specifier, and the next token
12052 is `template', then we are using the template-id production. */
12053 if (parser->scope
12054 && cp_parser_optional_template_keyword (parser))
12056 /* Look for the template-id. */
12057 type = cp_parser_template_id (parser,
12058 /*template_keyword_p=*/true,
12059 /*check_dependency_p=*/true,
12060 /*is_declaration=*/false);
12061 /* If the template-id did not name a type, we are out of
12062 luck. */
12063 if (TREE_CODE (type) != TYPE_DECL)
12065 cp_parser_error (parser, "expected template-id for type");
12066 type = NULL_TREE;
12069 /* Otherwise, look for a type-name. */
12070 else
12071 type = cp_parser_type_name (parser);
12072 /* Keep track of all name-lookups performed in class scopes. */
12073 if (type
12074 && !global_p
12075 && !qualified_p
12076 && TREE_CODE (type) == TYPE_DECL
12077 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
12078 maybe_note_name_used_in_class (DECL_NAME (type), type);
12079 /* If it didn't work out, we don't have a TYPE. */
12080 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
12081 && !cp_parser_parse_definitely (parser))
12082 type = NULL_TREE;
12083 if (type && decl_specs)
12084 cp_parser_set_decl_spec_type (decl_specs, type,
12085 token->location,
12086 /*user_defined=*/true);
12089 /* If we didn't get a type-name, issue an error message. */
12090 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12092 cp_parser_error (parser, "expected type-name");
12093 return error_mark_node;
12096 /* There is no valid C++ program where a non-template type is
12097 followed by a "<". That usually indicates that the user thought
12098 that the type was a template. */
12099 if (type && type != error_mark_node)
12101 /* As a last-ditch effort, see if TYPE is an Objective-C type.
12102 If it is, then the '<'...'>' enclose protocol names rather than
12103 template arguments, and so everything is fine. */
12104 if (c_dialect_objc ()
12105 && (objc_is_id (type) || objc_is_class_name (type)))
12107 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12108 tree qual_type = objc_get_protocol_qualified_type (type, protos);
12110 /* Clobber the "unqualified" type previously entered into
12111 DECL_SPECS with the new, improved protocol-qualified version. */
12112 if (decl_specs)
12113 decl_specs->type = qual_type;
12115 return qual_type;
12118 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12119 token->location);
12122 return type;
12125 /* Parse a type-name.
12127 type-name:
12128 class-name
12129 enum-name
12130 typedef-name
12132 enum-name:
12133 identifier
12135 typedef-name:
12136 identifier
12138 Returns a TYPE_DECL for the type. */
12140 static tree
12141 cp_parser_type_name (cp_parser* parser)
12143 tree type_decl;
12145 /* We can't know yet whether it is a class-name or not. */
12146 cp_parser_parse_tentatively (parser);
12147 /* Try a class-name. */
12148 type_decl = cp_parser_class_name (parser,
12149 /*typename_keyword_p=*/false,
12150 /*template_keyword_p=*/false,
12151 none_type,
12152 /*check_dependency_p=*/true,
12153 /*class_head_p=*/false,
12154 /*is_declaration=*/false);
12155 /* If it's not a class-name, keep looking. */
12156 if (!cp_parser_parse_definitely (parser))
12158 /* It must be a typedef-name or an enum-name. */
12159 return cp_parser_nonclass_name (parser);
12162 return type_decl;
12165 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12167 enum-name:
12168 identifier
12170 typedef-name:
12171 identifier
12173 Returns a TYPE_DECL for the type. */
12175 static tree
12176 cp_parser_nonclass_name (cp_parser* parser)
12178 tree type_decl;
12179 tree identifier;
12181 cp_token *token = cp_lexer_peek_token (parser->lexer);
12182 identifier = cp_parser_identifier (parser);
12183 if (identifier == error_mark_node)
12184 return error_mark_node;
12186 /* Look up the type-name. */
12187 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
12189 if (TREE_CODE (type_decl) != TYPE_DECL
12190 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
12192 /* See if this is an Objective-C type. */
12193 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12194 tree type = objc_get_protocol_qualified_type (identifier, protos);
12195 if (type)
12196 type_decl = TYPE_NAME (type);
12199 /* Issue an error if we did not find a type-name. */
12200 if (TREE_CODE (type_decl) != TYPE_DECL)
12202 if (!cp_parser_simulate_error (parser))
12203 cp_parser_name_lookup_error (parser, identifier, type_decl,
12204 "is not a type", token->location);
12205 return error_mark_node;
12207 /* Remember that the name was used in the definition of the
12208 current class so that we can check later to see if the
12209 meaning would have been different after the class was
12210 entirely defined. */
12211 else if (type_decl != error_mark_node
12212 && !parser->scope)
12213 maybe_note_name_used_in_class (identifier, type_decl);
12215 return type_decl;
12218 /* Parse an elaborated-type-specifier. Note that the grammar given
12219 here incorporates the resolution to DR68.
12221 elaborated-type-specifier:
12222 class-key :: [opt] nested-name-specifier [opt] identifier
12223 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
12224 enum-key :: [opt] nested-name-specifier [opt] identifier
12225 typename :: [opt] nested-name-specifier identifier
12226 typename :: [opt] nested-name-specifier template [opt]
12227 template-id
12229 GNU extension:
12231 elaborated-type-specifier:
12232 class-key attributes :: [opt] nested-name-specifier [opt] identifier
12233 class-key attributes :: [opt] nested-name-specifier [opt]
12234 template [opt] template-id
12235 enum attributes :: [opt] nested-name-specifier [opt] identifier
12237 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
12238 declared `friend'. If IS_DECLARATION is TRUE, then this
12239 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
12240 something is being declared.
12242 Returns the TYPE specified. */
12244 static tree
12245 cp_parser_elaborated_type_specifier (cp_parser* parser,
12246 bool is_friend,
12247 bool is_declaration)
12249 enum tag_types tag_type;
12250 tree identifier;
12251 tree type = NULL_TREE;
12252 tree attributes = NULL_TREE;
12253 tree globalscope;
12254 cp_token *token = NULL;
12256 /* See if we're looking at the `enum' keyword. */
12257 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
12259 /* Consume the `enum' token. */
12260 cp_lexer_consume_token (parser->lexer);
12261 /* Remember that it's an enumeration type. */
12262 tag_type = enum_type;
12263 /* Parse the optional `struct' or `class' key (for C++0x scoped
12264 enums). */
12265 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12266 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12268 if (cxx_dialect == cxx98)
12269 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
12271 /* Consume the `struct' or `class'. */
12272 cp_lexer_consume_token (parser->lexer);
12274 /* Parse the attributes. */
12275 attributes = cp_parser_attributes_opt (parser);
12277 /* Or, it might be `typename'. */
12278 else if (cp_lexer_next_token_is_keyword (parser->lexer,
12279 RID_TYPENAME))
12281 /* Consume the `typename' token. */
12282 cp_lexer_consume_token (parser->lexer);
12283 /* Remember that it's a `typename' type. */
12284 tag_type = typename_type;
12286 /* Otherwise it must be a class-key. */
12287 else
12289 tag_type = cp_parser_class_key (parser);
12290 if (tag_type == none_type)
12291 return error_mark_node;
12292 /* Parse the attributes. */
12293 attributes = cp_parser_attributes_opt (parser);
12296 /* Look for the `::' operator. */
12297 globalscope = cp_parser_global_scope_opt (parser,
12298 /*current_scope_valid_p=*/false);
12299 /* Look for the nested-name-specifier. */
12300 if (tag_type == typename_type && !globalscope)
12302 if (!cp_parser_nested_name_specifier (parser,
12303 /*typename_keyword_p=*/true,
12304 /*check_dependency_p=*/true,
12305 /*type_p=*/true,
12306 is_declaration))
12307 return error_mark_node;
12309 else
12310 /* Even though `typename' is not present, the proposed resolution
12311 to Core Issue 180 says that in `class A<T>::B', `B' should be
12312 considered a type-name, even if `A<T>' is dependent. */
12313 cp_parser_nested_name_specifier_opt (parser,
12314 /*typename_keyword_p=*/true,
12315 /*check_dependency_p=*/true,
12316 /*type_p=*/true,
12317 is_declaration);
12318 /* For everything but enumeration types, consider a template-id.
12319 For an enumeration type, consider only a plain identifier. */
12320 if (tag_type != enum_type)
12322 bool template_p = false;
12323 tree decl;
12325 /* Allow the `template' keyword. */
12326 template_p = cp_parser_optional_template_keyword (parser);
12327 /* If we didn't see `template', we don't know if there's a
12328 template-id or not. */
12329 if (!template_p)
12330 cp_parser_parse_tentatively (parser);
12331 /* Parse the template-id. */
12332 token = cp_lexer_peek_token (parser->lexer);
12333 decl = cp_parser_template_id (parser, template_p,
12334 /*check_dependency_p=*/true,
12335 is_declaration);
12336 /* If we didn't find a template-id, look for an ordinary
12337 identifier. */
12338 if (!template_p && !cp_parser_parse_definitely (parser))
12340 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
12341 in effect, then we must assume that, upon instantiation, the
12342 template will correspond to a class. */
12343 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12344 && tag_type == typename_type)
12345 type = make_typename_type (parser->scope, decl,
12346 typename_type,
12347 /*complain=*/tf_error);
12348 /* If the `typename' keyword is in effect and DECL is not a type
12349 decl. Then type is non existant. */
12350 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
12351 type = NULL_TREE;
12352 else
12353 type = TREE_TYPE (decl);
12356 if (!type)
12358 token = cp_lexer_peek_token (parser->lexer);
12359 identifier = cp_parser_identifier (parser);
12361 if (identifier == error_mark_node)
12363 parser->scope = NULL_TREE;
12364 return error_mark_node;
12367 /* For a `typename', we needn't call xref_tag. */
12368 if (tag_type == typename_type
12369 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
12370 return cp_parser_make_typename_type (parser, parser->scope,
12371 identifier,
12372 token->location);
12373 /* Look up a qualified name in the usual way. */
12374 if (parser->scope)
12376 tree decl;
12377 tree ambiguous_decls;
12379 decl = cp_parser_lookup_name (parser, identifier,
12380 tag_type,
12381 /*is_template=*/false,
12382 /*is_namespace=*/false,
12383 /*check_dependency=*/true,
12384 &ambiguous_decls,
12385 token->location);
12387 /* If the lookup was ambiguous, an error will already have been
12388 issued. */
12389 if (ambiguous_decls)
12390 return error_mark_node;
12392 /* If we are parsing friend declaration, DECL may be a
12393 TEMPLATE_DECL tree node here. However, we need to check
12394 whether this TEMPLATE_DECL results in valid code. Consider
12395 the following example:
12397 namespace N {
12398 template <class T> class C {};
12400 class X {
12401 template <class T> friend class N::C; // #1, valid code
12403 template <class T> class Y {
12404 friend class N::C; // #2, invalid code
12407 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
12408 name lookup of `N::C'. We see that friend declaration must
12409 be template for the code to be valid. Note that
12410 processing_template_decl does not work here since it is
12411 always 1 for the above two cases. */
12413 decl = (cp_parser_maybe_treat_template_as_class
12414 (decl, /*tag_name_p=*/is_friend
12415 && parser->num_template_parameter_lists));
12417 if (TREE_CODE (decl) != TYPE_DECL)
12419 cp_parser_diagnose_invalid_type_name (parser,
12420 parser->scope,
12421 identifier,
12422 token->location);
12423 return error_mark_node;
12426 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
12428 bool allow_template = (parser->num_template_parameter_lists
12429 || DECL_SELF_REFERENCE_P (decl));
12430 type = check_elaborated_type_specifier (tag_type, decl,
12431 allow_template);
12433 if (type == error_mark_node)
12434 return error_mark_node;
12437 /* Forward declarations of nested types, such as
12439 class C1::C2;
12440 class C1::C2::C3;
12442 are invalid unless all components preceding the final '::'
12443 are complete. If all enclosing types are complete, these
12444 declarations become merely pointless.
12446 Invalid forward declarations of nested types are errors
12447 caught elsewhere in parsing. Those that are pointless arrive
12448 here. */
12450 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12451 && !is_friend && !processing_explicit_instantiation)
12452 warning (0, "declaration %qD does not declare anything", decl);
12454 type = TREE_TYPE (decl);
12456 else
12458 /* An elaborated-type-specifier sometimes introduces a new type and
12459 sometimes names an existing type. Normally, the rule is that it
12460 introduces a new type only if there is not an existing type of
12461 the same name already in scope. For example, given:
12463 struct S {};
12464 void f() { struct S s; }
12466 the `struct S' in the body of `f' is the same `struct S' as in
12467 the global scope; the existing definition is used. However, if
12468 there were no global declaration, this would introduce a new
12469 local class named `S'.
12471 An exception to this rule applies to the following code:
12473 namespace N { struct S; }
12475 Here, the elaborated-type-specifier names a new type
12476 unconditionally; even if there is already an `S' in the
12477 containing scope this declaration names a new type.
12478 This exception only applies if the elaborated-type-specifier
12479 forms the complete declaration:
12481 [class.name]
12483 A declaration consisting solely of `class-key identifier ;' is
12484 either a redeclaration of the name in the current scope or a
12485 forward declaration of the identifier as a class name. It
12486 introduces the name into the current scope.
12488 We are in this situation precisely when the next token is a `;'.
12490 An exception to the exception is that a `friend' declaration does
12491 *not* name a new type; i.e., given:
12493 struct S { friend struct T; };
12495 `T' is not a new type in the scope of `S'.
12497 Also, `new struct S' or `sizeof (struct S)' never results in the
12498 definition of a new type; a new type can only be declared in a
12499 declaration context. */
12501 tag_scope ts;
12502 bool template_p;
12504 if (is_friend)
12505 /* Friends have special name lookup rules. */
12506 ts = ts_within_enclosing_non_class;
12507 else if (is_declaration
12508 && cp_lexer_next_token_is (parser->lexer,
12509 CPP_SEMICOLON))
12510 /* This is a `class-key identifier ;' */
12511 ts = ts_current;
12512 else
12513 ts = ts_global;
12515 template_p =
12516 (parser->num_template_parameter_lists
12517 && (cp_parser_next_token_starts_class_definition_p (parser)
12518 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
12519 /* An unqualified name was used to reference this type, so
12520 there were no qualifying templates. */
12521 if (!cp_parser_check_template_parameters (parser,
12522 /*num_templates=*/0,
12523 token->location,
12524 /*declarator=*/NULL))
12525 return error_mark_node;
12526 type = xref_tag (tag_type, identifier, ts, template_p);
12530 if (type == error_mark_node)
12531 return error_mark_node;
12533 /* Allow attributes on forward declarations of classes. */
12534 if (attributes)
12536 if (TREE_CODE (type) == TYPENAME_TYPE)
12537 warning (OPT_Wattributes,
12538 "attributes ignored on uninstantiated type");
12539 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
12540 && ! processing_explicit_instantiation)
12541 warning (OPT_Wattributes,
12542 "attributes ignored on template instantiation");
12543 else if (is_declaration && cp_parser_declares_only_class_p (parser))
12544 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
12545 else
12546 warning (OPT_Wattributes,
12547 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
12550 if (tag_type != enum_type)
12551 cp_parser_check_class_key (tag_type, type);
12553 /* A "<" cannot follow an elaborated type specifier. If that
12554 happens, the user was probably trying to form a template-id. */
12555 cp_parser_check_for_invalid_template_id (parser, type, token->location);
12557 return type;
12560 /* Parse an enum-specifier.
12562 enum-specifier:
12563 enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
12565 enum-key:
12566 enum
12567 enum class [C++0x]
12568 enum struct [C++0x]
12570 enum-base: [C++0x]
12571 : type-specifier-seq
12573 GNU Extensions:
12574 enum-key attributes[opt] identifier [opt] enum-base [opt]
12575 { enumerator-list [opt] }attributes[opt]
12577 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
12578 if the token stream isn't an enum-specifier after all. */
12580 static tree
12581 cp_parser_enum_specifier (cp_parser* parser)
12583 tree identifier;
12584 tree type;
12585 tree attributes;
12586 bool scoped_enum_p = false;
12587 bool has_underlying_type = false;
12588 tree underlying_type = NULL_TREE;
12590 /* Parse tentatively so that we can back up if we don't find a
12591 enum-specifier. */
12592 cp_parser_parse_tentatively (parser);
12594 /* Caller guarantees that the current token is 'enum', an identifier
12595 possibly follows, and the token after that is an opening brace.
12596 If we don't have an identifier, fabricate an anonymous name for
12597 the enumeration being defined. */
12598 cp_lexer_consume_token (parser->lexer);
12600 /* Parse the "class" or "struct", which indicates a scoped
12601 enumeration type in C++0x. */
12602 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12603 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12605 if (cxx_dialect == cxx98)
12606 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
12608 /* Consume the `struct' or `class' token. */
12609 cp_lexer_consume_token (parser->lexer);
12611 scoped_enum_p = true;
12614 attributes = cp_parser_attributes_opt (parser);
12616 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12617 identifier = cp_parser_identifier (parser);
12618 else
12619 identifier = make_anon_name ();
12621 /* Check for the `:' that denotes a specified underlying type in C++0x.
12622 Note that a ':' could also indicate a bitfield width, however. */
12623 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12625 cp_decl_specifier_seq type_specifiers;
12627 /* Consume the `:'. */
12628 cp_lexer_consume_token (parser->lexer);
12630 /* Parse the type-specifier-seq. */
12631 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12632 /*is_trailing_return=*/false,
12633 &type_specifiers);
12635 /* At this point this is surely not elaborated type specifier. */
12636 if (!cp_parser_parse_definitely (parser))
12637 return NULL_TREE;
12639 if (cxx_dialect == cxx98)
12640 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
12642 has_underlying_type = true;
12644 /* If that didn't work, stop. */
12645 if (type_specifiers.type != error_mark_node)
12647 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
12648 /*initialized=*/0, NULL);
12649 if (underlying_type == error_mark_node)
12650 underlying_type = NULL_TREE;
12654 /* Look for the `{' but don't consume it yet. */
12655 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12657 cp_parser_error (parser, "expected %<{%>");
12658 if (has_underlying_type)
12659 return NULL_TREE;
12662 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
12663 return NULL_TREE;
12665 /* Issue an error message if type-definitions are forbidden here. */
12666 if (!cp_parser_check_type_definition (parser))
12667 type = error_mark_node;
12668 else
12669 /* Create the new type. We do this before consuming the opening
12670 brace so the enum will be recorded as being on the line of its
12671 tag (or the 'enum' keyword, if there is no tag). */
12672 type = start_enum (identifier, underlying_type, scoped_enum_p);
12674 /* Consume the opening brace. */
12675 cp_lexer_consume_token (parser->lexer);
12677 if (type == error_mark_node)
12679 cp_parser_skip_to_end_of_block_or_statement (parser);
12680 return error_mark_node;
12683 /* If the next token is not '}', then there are some enumerators. */
12684 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12685 cp_parser_enumerator_list (parser, type);
12687 /* Consume the final '}'. */
12688 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12690 /* Look for trailing attributes to apply to this enumeration, and
12691 apply them if appropriate. */
12692 if (cp_parser_allow_gnu_extensions_p (parser))
12694 tree trailing_attr = cp_parser_attributes_opt (parser);
12695 trailing_attr = chainon (trailing_attr, attributes);
12696 cplus_decl_attributes (&type,
12697 trailing_attr,
12698 (int) ATTR_FLAG_TYPE_IN_PLACE);
12701 /* Finish up the enumeration. */
12702 finish_enum (type);
12704 return type;
12707 /* Parse an enumerator-list. The enumerators all have the indicated
12708 TYPE.
12710 enumerator-list:
12711 enumerator-definition
12712 enumerator-list , enumerator-definition */
12714 static void
12715 cp_parser_enumerator_list (cp_parser* parser, tree type)
12717 while (true)
12719 /* Parse an enumerator-definition. */
12720 cp_parser_enumerator_definition (parser, type);
12722 /* If the next token is not a ',', we've reached the end of
12723 the list. */
12724 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12725 break;
12726 /* Otherwise, consume the `,' and keep going. */
12727 cp_lexer_consume_token (parser->lexer);
12728 /* If the next token is a `}', there is a trailing comma. */
12729 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
12731 if (!in_system_header)
12732 pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
12733 break;
12738 /* Parse an enumerator-definition. The enumerator has the indicated
12739 TYPE.
12741 enumerator-definition:
12742 enumerator
12743 enumerator = constant-expression
12745 enumerator:
12746 identifier */
12748 static void
12749 cp_parser_enumerator_definition (cp_parser* parser, tree type)
12751 tree identifier;
12752 tree value;
12754 /* Look for the identifier. */
12755 identifier = cp_parser_identifier (parser);
12756 if (identifier == error_mark_node)
12757 return;
12759 /* If the next token is an '=', then there is an explicit value. */
12760 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12762 /* Consume the `=' token. */
12763 cp_lexer_consume_token (parser->lexer);
12764 /* Parse the value. */
12765 value = cp_parser_constant_expression (parser,
12766 /*allow_non_constant_p=*/false,
12767 NULL);
12769 else
12770 value = NULL_TREE;
12772 /* If we are processing a template, make sure the initializer of the
12773 enumerator doesn't contain any bare template parameter pack. */
12774 if (check_for_bare_parameter_packs (value))
12775 value = error_mark_node;
12777 /* Create the enumerator. */
12778 build_enumerator (identifier, value, type);
12781 /* Parse a namespace-name.
12783 namespace-name:
12784 original-namespace-name
12785 namespace-alias
12787 Returns the NAMESPACE_DECL for the namespace. */
12789 static tree
12790 cp_parser_namespace_name (cp_parser* parser)
12792 tree identifier;
12793 tree namespace_decl;
12795 cp_token *token = cp_lexer_peek_token (parser->lexer);
12797 /* Get the name of the namespace. */
12798 identifier = cp_parser_identifier (parser);
12799 if (identifier == error_mark_node)
12800 return error_mark_node;
12802 /* Look up the identifier in the currently active scope. Look only
12803 for namespaces, due to:
12805 [basic.lookup.udir]
12807 When looking up a namespace-name in a using-directive or alias
12808 definition, only namespace names are considered.
12810 And:
12812 [basic.lookup.qual]
12814 During the lookup of a name preceding the :: scope resolution
12815 operator, object, function, and enumerator names are ignored.
12817 (Note that cp_parser_qualifying_entity only calls this
12818 function if the token after the name is the scope resolution
12819 operator.) */
12820 namespace_decl = cp_parser_lookup_name (parser, identifier,
12821 none_type,
12822 /*is_template=*/false,
12823 /*is_namespace=*/true,
12824 /*check_dependency=*/true,
12825 /*ambiguous_decls=*/NULL,
12826 token->location);
12827 /* If it's not a namespace, issue an error. */
12828 if (namespace_decl == error_mark_node
12829 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
12831 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12832 error_at (token->location, "%qD is not a namespace-name", identifier);
12833 cp_parser_error (parser, "expected namespace-name");
12834 namespace_decl = error_mark_node;
12837 return namespace_decl;
12840 /* Parse a namespace-definition.
12842 namespace-definition:
12843 named-namespace-definition
12844 unnamed-namespace-definition
12846 named-namespace-definition:
12847 original-namespace-definition
12848 extension-namespace-definition
12850 original-namespace-definition:
12851 namespace identifier { namespace-body }
12853 extension-namespace-definition:
12854 namespace original-namespace-name { namespace-body }
12856 unnamed-namespace-definition:
12857 namespace { namespace-body } */
12859 static void
12860 cp_parser_namespace_definition (cp_parser* parser)
12862 tree identifier, attribs;
12863 bool has_visibility;
12864 bool is_inline;
12866 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
12868 is_inline = true;
12869 cp_lexer_consume_token (parser->lexer);
12871 else
12872 is_inline = false;
12874 /* Look for the `namespace' keyword. */
12875 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12877 /* Get the name of the namespace. We do not attempt to distinguish
12878 between an original-namespace-definition and an
12879 extension-namespace-definition at this point. The semantic
12880 analysis routines are responsible for that. */
12881 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12882 identifier = cp_parser_identifier (parser);
12883 else
12884 identifier = NULL_TREE;
12886 /* Parse any specified attributes. */
12887 attribs = cp_parser_attributes_opt (parser);
12889 /* Look for the `{' to start the namespace. */
12890 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
12891 /* Start the namespace. */
12892 push_namespace (identifier);
12894 /* "inline namespace" is equivalent to a stub namespace definition
12895 followed by a strong using directive. */
12896 if (is_inline)
12898 tree name_space = current_namespace;
12899 /* Set up namespace association. */
12900 DECL_NAMESPACE_ASSOCIATIONS (name_space)
12901 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
12902 DECL_NAMESPACE_ASSOCIATIONS (name_space));
12903 /* Import the contents of the inline namespace. */
12904 pop_namespace ();
12905 do_using_directive (name_space);
12906 push_namespace (identifier);
12909 has_visibility = handle_namespace_attrs (current_namespace, attribs);
12911 /* Parse the body of the namespace. */
12912 cp_parser_namespace_body (parser);
12914 #ifdef HANDLE_PRAGMA_VISIBILITY
12915 if (has_visibility)
12916 pop_visibility (1);
12917 #endif
12919 /* Finish the namespace. */
12920 pop_namespace ();
12921 /* Look for the final `}'. */
12922 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12925 /* Parse a namespace-body.
12927 namespace-body:
12928 declaration-seq [opt] */
12930 static void
12931 cp_parser_namespace_body (cp_parser* parser)
12933 cp_parser_declaration_seq_opt (parser);
12936 /* Parse a namespace-alias-definition.
12938 namespace-alias-definition:
12939 namespace identifier = qualified-namespace-specifier ; */
12941 static void
12942 cp_parser_namespace_alias_definition (cp_parser* parser)
12944 tree identifier;
12945 tree namespace_specifier;
12947 cp_token *token = cp_lexer_peek_token (parser->lexer);
12949 /* Look for the `namespace' keyword. */
12950 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12951 /* Look for the identifier. */
12952 identifier = cp_parser_identifier (parser);
12953 if (identifier == error_mark_node)
12954 return;
12955 /* Look for the `=' token. */
12956 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12957 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12959 error_at (token->location, "%<namespace%> definition is not allowed here");
12960 /* Skip the definition. */
12961 cp_lexer_consume_token (parser->lexer);
12962 if (cp_parser_skip_to_closing_brace (parser))
12963 cp_lexer_consume_token (parser->lexer);
12964 return;
12966 cp_parser_require (parser, CPP_EQ, "%<=%>");
12967 /* Look for the qualified-namespace-specifier. */
12968 namespace_specifier
12969 = cp_parser_qualified_namespace_specifier (parser);
12970 /* Look for the `;' token. */
12971 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12973 /* Register the alias in the symbol table. */
12974 do_namespace_alias (identifier, namespace_specifier);
12977 /* Parse a qualified-namespace-specifier.
12979 qualified-namespace-specifier:
12980 :: [opt] nested-name-specifier [opt] namespace-name
12982 Returns a NAMESPACE_DECL corresponding to the specified
12983 namespace. */
12985 static tree
12986 cp_parser_qualified_namespace_specifier (cp_parser* parser)
12988 /* Look for the optional `::'. */
12989 cp_parser_global_scope_opt (parser,
12990 /*current_scope_valid_p=*/false);
12992 /* Look for the optional nested-name-specifier. */
12993 cp_parser_nested_name_specifier_opt (parser,
12994 /*typename_keyword_p=*/false,
12995 /*check_dependency_p=*/true,
12996 /*type_p=*/false,
12997 /*is_declaration=*/true);
12999 return cp_parser_namespace_name (parser);
13002 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
13003 access declaration.
13005 using-declaration:
13006 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
13007 using :: unqualified-id ;
13009 access-declaration:
13010 qualified-id ;
13014 static bool
13015 cp_parser_using_declaration (cp_parser* parser,
13016 bool access_declaration_p)
13018 cp_token *token;
13019 bool typename_p = false;
13020 bool global_scope_p;
13021 tree decl;
13022 tree identifier;
13023 tree qscope;
13025 if (access_declaration_p)
13026 cp_parser_parse_tentatively (parser);
13027 else
13029 /* Look for the `using' keyword. */
13030 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
13032 /* Peek at the next token. */
13033 token = cp_lexer_peek_token (parser->lexer);
13034 /* See if it's `typename'. */
13035 if (token->keyword == RID_TYPENAME)
13037 /* Remember that we've seen it. */
13038 typename_p = true;
13039 /* Consume the `typename' token. */
13040 cp_lexer_consume_token (parser->lexer);
13044 /* Look for the optional global scope qualification. */
13045 global_scope_p
13046 = (cp_parser_global_scope_opt (parser,
13047 /*current_scope_valid_p=*/false)
13048 != NULL_TREE);
13050 /* If we saw `typename', or didn't see `::', then there must be a
13051 nested-name-specifier present. */
13052 if (typename_p || !global_scope_p)
13053 qscope = cp_parser_nested_name_specifier (parser, typename_p,
13054 /*check_dependency_p=*/true,
13055 /*type_p=*/false,
13056 /*is_declaration=*/true);
13057 /* Otherwise, we could be in either of the two productions. In that
13058 case, treat the nested-name-specifier as optional. */
13059 else
13060 qscope = cp_parser_nested_name_specifier_opt (parser,
13061 /*typename_keyword_p=*/false,
13062 /*check_dependency_p=*/true,
13063 /*type_p=*/false,
13064 /*is_declaration=*/true);
13065 if (!qscope)
13066 qscope = global_namespace;
13068 if (access_declaration_p && cp_parser_error_occurred (parser))
13069 /* Something has already gone wrong; there's no need to parse
13070 further. Since an error has occurred, the return value of
13071 cp_parser_parse_definitely will be false, as required. */
13072 return cp_parser_parse_definitely (parser);
13074 token = cp_lexer_peek_token (parser->lexer);
13075 /* Parse the unqualified-id. */
13076 identifier = cp_parser_unqualified_id (parser,
13077 /*template_keyword_p=*/false,
13078 /*check_dependency_p=*/true,
13079 /*declarator_p=*/true,
13080 /*optional_p=*/false);
13082 if (access_declaration_p)
13084 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13085 cp_parser_simulate_error (parser);
13086 if (!cp_parser_parse_definitely (parser))
13087 return false;
13090 /* The function we call to handle a using-declaration is different
13091 depending on what scope we are in. */
13092 if (qscope == error_mark_node || identifier == error_mark_node)
13094 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
13095 && TREE_CODE (identifier) != BIT_NOT_EXPR)
13096 /* [namespace.udecl]
13098 A using declaration shall not name a template-id. */
13099 error_at (token->location,
13100 "a template-id may not appear in a using-declaration");
13101 else
13103 if (at_class_scope_p ())
13105 /* Create the USING_DECL. */
13106 decl = do_class_using_decl (parser->scope, identifier);
13108 if (check_for_bare_parameter_packs (decl))
13109 return false;
13110 else
13111 /* Add it to the list of members in this class. */
13112 finish_member_declaration (decl);
13114 else
13116 decl = cp_parser_lookup_name_simple (parser,
13117 identifier,
13118 token->location);
13119 if (decl == error_mark_node)
13120 cp_parser_name_lookup_error (parser, identifier,
13121 decl, NULL,
13122 token->location);
13123 else if (check_for_bare_parameter_packs (decl))
13124 return false;
13125 else if (!at_namespace_scope_p ())
13126 do_local_using_decl (decl, qscope, identifier);
13127 else
13128 do_toplevel_using_decl (decl, qscope, identifier);
13132 /* Look for the final `;'. */
13133 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13135 return true;
13138 /* Parse a using-directive.
13140 using-directive:
13141 using namespace :: [opt] nested-name-specifier [opt]
13142 namespace-name ; */
13144 static void
13145 cp_parser_using_directive (cp_parser* parser)
13147 tree namespace_decl;
13148 tree attribs;
13150 /* Look for the `using' keyword. */
13151 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
13152 /* And the `namespace' keyword. */
13153 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
13154 /* Look for the optional `::' operator. */
13155 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13156 /* And the optional nested-name-specifier. */
13157 cp_parser_nested_name_specifier_opt (parser,
13158 /*typename_keyword_p=*/false,
13159 /*check_dependency_p=*/true,
13160 /*type_p=*/false,
13161 /*is_declaration=*/true);
13162 /* Get the namespace being used. */
13163 namespace_decl = cp_parser_namespace_name (parser);
13164 /* And any specified attributes. */
13165 attribs = cp_parser_attributes_opt (parser);
13166 /* Update the symbol table. */
13167 parse_using_directive (namespace_decl, attribs);
13168 /* Look for the final `;'. */
13169 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13172 /* Parse an asm-definition.
13174 asm-definition:
13175 asm ( string-literal ) ;
13177 GNU Extension:
13179 asm-definition:
13180 asm volatile [opt] ( string-literal ) ;
13181 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
13182 asm volatile [opt] ( string-literal : asm-operand-list [opt]
13183 : asm-operand-list [opt] ) ;
13184 asm volatile [opt] ( string-literal : asm-operand-list [opt]
13185 : asm-operand-list [opt]
13186 : asm-clobber-list [opt] ) ;
13187 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
13188 : asm-clobber-list [opt]
13189 : asm-goto-list ) ; */
13191 static void
13192 cp_parser_asm_definition (cp_parser* parser)
13194 tree string;
13195 tree outputs = NULL_TREE;
13196 tree inputs = NULL_TREE;
13197 tree clobbers = NULL_TREE;
13198 tree labels = NULL_TREE;
13199 tree asm_stmt;
13200 bool volatile_p = false;
13201 bool extended_p = false;
13202 bool invalid_inputs_p = false;
13203 bool invalid_outputs_p = false;
13204 bool goto_p = false;
13205 const char *missing = NULL;
13207 /* Look for the `asm' keyword. */
13208 cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
13209 /* See if the next token is `volatile'. */
13210 if (cp_parser_allow_gnu_extensions_p (parser)
13211 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
13213 /* Remember that we saw the `volatile' keyword. */
13214 volatile_p = true;
13215 /* Consume the token. */
13216 cp_lexer_consume_token (parser->lexer);
13218 if (cp_parser_allow_gnu_extensions_p (parser)
13219 && parser->in_function_body
13220 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
13222 /* Remember that we saw the `goto' keyword. */
13223 goto_p = true;
13224 /* Consume the token. */
13225 cp_lexer_consume_token (parser->lexer);
13227 /* Look for the opening `('. */
13228 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
13229 return;
13230 /* Look for the string. */
13231 string = cp_parser_string_literal (parser, false, false);
13232 if (string == error_mark_node)
13234 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13235 /*consume_paren=*/true);
13236 return;
13239 /* If we're allowing GNU extensions, check for the extended assembly
13240 syntax. Unfortunately, the `:' tokens need not be separated by
13241 a space in C, and so, for compatibility, we tolerate that here
13242 too. Doing that means that we have to treat the `::' operator as
13243 two `:' tokens. */
13244 if (cp_parser_allow_gnu_extensions_p (parser)
13245 && parser->in_function_body
13246 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
13247 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
13249 bool inputs_p = false;
13250 bool clobbers_p = false;
13251 bool labels_p = false;
13253 /* The extended syntax was used. */
13254 extended_p = true;
13256 /* Look for outputs. */
13257 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13259 /* Consume the `:'. */
13260 cp_lexer_consume_token (parser->lexer);
13261 /* Parse the output-operands. */
13262 if (cp_lexer_next_token_is_not (parser->lexer,
13263 CPP_COLON)
13264 && cp_lexer_next_token_is_not (parser->lexer,
13265 CPP_SCOPE)
13266 && cp_lexer_next_token_is_not (parser->lexer,
13267 CPP_CLOSE_PAREN)
13268 && !goto_p)
13269 outputs = cp_parser_asm_operand_list (parser);
13271 if (outputs == error_mark_node)
13272 invalid_outputs_p = true;
13274 /* If the next token is `::', there are no outputs, and the
13275 next token is the beginning of the inputs. */
13276 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13277 /* The inputs are coming next. */
13278 inputs_p = true;
13280 /* Look for inputs. */
13281 if (inputs_p
13282 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13284 /* Consume the `:' or `::'. */
13285 cp_lexer_consume_token (parser->lexer);
13286 /* Parse the output-operands. */
13287 if (cp_lexer_next_token_is_not (parser->lexer,
13288 CPP_COLON)
13289 && cp_lexer_next_token_is_not (parser->lexer,
13290 CPP_SCOPE)
13291 && cp_lexer_next_token_is_not (parser->lexer,
13292 CPP_CLOSE_PAREN))
13293 inputs = cp_parser_asm_operand_list (parser);
13295 if (inputs == error_mark_node)
13296 invalid_inputs_p = true;
13298 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13299 /* The clobbers are coming next. */
13300 clobbers_p = true;
13302 /* Look for clobbers. */
13303 if (clobbers_p
13304 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13306 clobbers_p = true;
13307 /* Consume the `:' or `::'. */
13308 cp_lexer_consume_token (parser->lexer);
13309 /* Parse the clobbers. */
13310 if (cp_lexer_next_token_is_not (parser->lexer,
13311 CPP_COLON)
13312 && cp_lexer_next_token_is_not (parser->lexer,
13313 CPP_CLOSE_PAREN))
13314 clobbers = cp_parser_asm_clobber_list (parser);
13316 else if (goto_p
13317 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13318 /* The labels are coming next. */
13319 labels_p = true;
13321 /* Look for labels. */
13322 if (labels_p
13323 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
13325 labels_p = true;
13326 /* Consume the `:' or `::'. */
13327 cp_lexer_consume_token (parser->lexer);
13328 /* Parse the labels. */
13329 labels = cp_parser_asm_label_list (parser);
13332 if (goto_p && !labels_p)
13333 missing = clobbers_p ? "%<:%>" : "%<:%> or %<::%>";
13335 else if (goto_p)
13336 missing = "%<:%> or %<::%>";
13338 /* Look for the closing `)'. */
13339 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
13340 missing ? missing : "%<)%>"))
13341 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13342 /*consume_paren=*/true);
13343 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13345 if (!invalid_inputs_p && !invalid_outputs_p)
13347 /* Create the ASM_EXPR. */
13348 if (parser->in_function_body)
13350 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
13351 inputs, clobbers, labels);
13352 /* If the extended syntax was not used, mark the ASM_EXPR. */
13353 if (!extended_p)
13355 tree temp = asm_stmt;
13356 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
13357 temp = TREE_OPERAND (temp, 0);
13359 ASM_INPUT_P (temp) = 1;
13362 else
13363 cgraph_add_asm_node (string);
13367 /* Declarators [gram.dcl.decl] */
13369 /* Parse an init-declarator.
13371 init-declarator:
13372 declarator initializer [opt]
13374 GNU Extension:
13376 init-declarator:
13377 declarator asm-specification [opt] attributes [opt] initializer [opt]
13379 function-definition:
13380 decl-specifier-seq [opt] declarator ctor-initializer [opt]
13381 function-body
13382 decl-specifier-seq [opt] declarator function-try-block
13384 GNU Extension:
13386 function-definition:
13387 __extension__ function-definition
13389 The DECL_SPECIFIERS apply to this declarator. Returns a
13390 representation of the entity declared. If MEMBER_P is TRUE, then
13391 this declarator appears in a class scope. The new DECL created by
13392 this declarator is returned.
13394 The CHECKS are access checks that should be performed once we know
13395 what entity is being declared (and, therefore, what classes have
13396 befriended it).
13398 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
13399 for a function-definition here as well. If the declarator is a
13400 declarator for a function-definition, *FUNCTION_DEFINITION_P will
13401 be TRUE upon return. By that point, the function-definition will
13402 have been completely parsed.
13404 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
13405 is FALSE. */
13407 static tree
13408 cp_parser_init_declarator (cp_parser* parser,
13409 cp_decl_specifier_seq *decl_specifiers,
13410 VEC (deferred_access_check,gc)* checks,
13411 bool function_definition_allowed_p,
13412 bool member_p,
13413 int declares_class_or_enum,
13414 bool* function_definition_p)
13416 cp_token *token = NULL, *asm_spec_start_token = NULL,
13417 *attributes_start_token = NULL;
13418 cp_declarator *declarator;
13419 tree prefix_attributes;
13420 tree attributes;
13421 tree asm_specification;
13422 tree initializer;
13423 tree decl = NULL_TREE;
13424 tree scope;
13425 int is_initialized;
13426 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
13427 initialized with "= ..", CPP_OPEN_PAREN if initialized with
13428 "(...)". */
13429 enum cpp_ttype initialization_kind;
13430 bool is_direct_init = false;
13431 bool is_non_constant_init;
13432 int ctor_dtor_or_conv_p;
13433 bool friend_p;
13434 tree pushed_scope = NULL;
13436 /* Gather the attributes that were provided with the
13437 decl-specifiers. */
13438 prefix_attributes = decl_specifiers->attributes;
13440 /* Assume that this is not the declarator for a function
13441 definition. */
13442 if (function_definition_p)
13443 *function_definition_p = false;
13445 /* Defer access checks while parsing the declarator; we cannot know
13446 what names are accessible until we know what is being
13447 declared. */
13448 resume_deferring_access_checks ();
13450 /* Parse the declarator. */
13451 token = cp_lexer_peek_token (parser->lexer);
13452 declarator
13453 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13454 &ctor_dtor_or_conv_p,
13455 /*parenthesized_p=*/NULL,
13456 /*member_p=*/false);
13457 /* Gather up the deferred checks. */
13458 stop_deferring_access_checks ();
13460 /* If the DECLARATOR was erroneous, there's no need to go
13461 further. */
13462 if (declarator == cp_error_declarator)
13463 return error_mark_node;
13465 /* Check that the number of template-parameter-lists is OK. */
13466 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
13467 token->location))
13468 return error_mark_node;
13470 if (declares_class_or_enum & 2)
13471 cp_parser_check_for_definition_in_return_type (declarator,
13472 decl_specifiers->type,
13473 decl_specifiers->type_location);
13475 /* Figure out what scope the entity declared by the DECLARATOR is
13476 located in. `grokdeclarator' sometimes changes the scope, so
13477 we compute it now. */
13478 scope = get_scope_of_declarator (declarator);
13480 /* Perform any lookups in the declared type which were thought to be
13481 dependent, but are not in the scope of the declarator. */
13482 decl_specifiers->type
13483 = maybe_update_decl_type (decl_specifiers->type, scope);
13485 /* If we're allowing GNU extensions, look for an asm-specification
13486 and attributes. */
13487 if (cp_parser_allow_gnu_extensions_p (parser))
13489 /* Look for an asm-specification. */
13490 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
13491 asm_specification = cp_parser_asm_specification_opt (parser);
13492 /* And attributes. */
13493 attributes_start_token = cp_lexer_peek_token (parser->lexer);
13494 attributes = cp_parser_attributes_opt (parser);
13496 else
13498 asm_specification = NULL_TREE;
13499 attributes = NULL_TREE;
13502 /* Peek at the next token. */
13503 token = cp_lexer_peek_token (parser->lexer);
13504 /* Check to see if the token indicates the start of a
13505 function-definition. */
13506 if (function_declarator_p (declarator)
13507 && cp_parser_token_starts_function_definition_p (token))
13509 if (!function_definition_allowed_p)
13511 /* If a function-definition should not appear here, issue an
13512 error message. */
13513 cp_parser_error (parser,
13514 "a function-definition is not allowed here");
13515 return error_mark_node;
13517 else
13519 location_t func_brace_location
13520 = cp_lexer_peek_token (parser->lexer)->location;
13522 /* Neither attributes nor an asm-specification are allowed
13523 on a function-definition. */
13524 if (asm_specification)
13525 error_at (asm_spec_start_token->location,
13526 "an asm-specification is not allowed "
13527 "on a function-definition");
13528 if (attributes)
13529 error_at (attributes_start_token->location,
13530 "attributes are not allowed on a function-definition");
13531 /* This is a function-definition. */
13532 *function_definition_p = true;
13534 /* Parse the function definition. */
13535 if (member_p)
13536 decl = cp_parser_save_member_function_body (parser,
13537 decl_specifiers,
13538 declarator,
13539 prefix_attributes);
13540 else
13541 decl
13542 = (cp_parser_function_definition_from_specifiers_and_declarator
13543 (parser, decl_specifiers, prefix_attributes, declarator));
13545 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
13547 /* This is where the prologue starts... */
13548 DECL_STRUCT_FUNCTION (decl)->function_start_locus
13549 = func_brace_location;
13552 return decl;
13556 /* [dcl.dcl]
13558 Only in function declarations for constructors, destructors, and
13559 type conversions can the decl-specifier-seq be omitted.
13561 We explicitly postpone this check past the point where we handle
13562 function-definitions because we tolerate function-definitions
13563 that are missing their return types in some modes. */
13564 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
13566 cp_parser_error (parser,
13567 "expected constructor, destructor, or type conversion");
13568 return error_mark_node;
13571 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
13572 if (token->type == CPP_EQ
13573 || token->type == CPP_OPEN_PAREN
13574 || token->type == CPP_OPEN_BRACE)
13576 is_initialized = SD_INITIALIZED;
13577 initialization_kind = token->type;
13579 if (token->type == CPP_EQ
13580 && function_declarator_p (declarator))
13582 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13583 if (t2->keyword == RID_DEFAULT)
13584 is_initialized = SD_DEFAULTED;
13585 else if (t2->keyword == RID_DELETE)
13586 is_initialized = SD_DELETED;
13589 else
13591 /* If the init-declarator isn't initialized and isn't followed by a
13592 `,' or `;', it's not a valid init-declarator. */
13593 if (token->type != CPP_COMMA
13594 && token->type != CPP_SEMICOLON)
13596 cp_parser_error (parser, "expected initializer");
13597 return error_mark_node;
13599 is_initialized = SD_UNINITIALIZED;
13600 initialization_kind = CPP_EOF;
13603 /* Because start_decl has side-effects, we should only call it if we
13604 know we're going ahead. By this point, we know that we cannot
13605 possibly be looking at any other construct. */
13606 cp_parser_commit_to_tentative_parse (parser);
13608 /* If the decl specifiers were bad, issue an error now that we're
13609 sure this was intended to be a declarator. Then continue
13610 declaring the variable(s), as int, to try to cut down on further
13611 errors. */
13612 if (decl_specifiers->any_specifiers_p
13613 && decl_specifiers->type == error_mark_node)
13615 cp_parser_error (parser, "invalid type in declaration");
13616 decl_specifiers->type = integer_type_node;
13619 /* Check to see whether or not this declaration is a friend. */
13620 friend_p = cp_parser_friend_p (decl_specifiers);
13622 /* Enter the newly declared entry in the symbol table. If we're
13623 processing a declaration in a class-specifier, we wait until
13624 after processing the initializer. */
13625 if (!member_p)
13627 if (parser->in_unbraced_linkage_specification_p)
13628 decl_specifiers->storage_class = sc_extern;
13629 decl = start_decl (declarator, decl_specifiers,
13630 is_initialized, attributes, prefix_attributes,
13631 &pushed_scope);
13633 else if (scope)
13634 /* Enter the SCOPE. That way unqualified names appearing in the
13635 initializer will be looked up in SCOPE. */
13636 pushed_scope = push_scope (scope);
13638 /* Perform deferred access control checks, now that we know in which
13639 SCOPE the declared entity resides. */
13640 if (!member_p && decl)
13642 tree saved_current_function_decl = NULL_TREE;
13644 /* If the entity being declared is a function, pretend that we
13645 are in its scope. If it is a `friend', it may have access to
13646 things that would not otherwise be accessible. */
13647 if (TREE_CODE (decl) == FUNCTION_DECL)
13649 saved_current_function_decl = current_function_decl;
13650 current_function_decl = decl;
13653 /* Perform access checks for template parameters. */
13654 cp_parser_perform_template_parameter_access_checks (checks);
13656 /* Perform the access control checks for the declarator and the
13657 decl-specifiers. */
13658 perform_deferred_access_checks ();
13660 /* Restore the saved value. */
13661 if (TREE_CODE (decl) == FUNCTION_DECL)
13662 current_function_decl = saved_current_function_decl;
13665 /* Parse the initializer. */
13666 initializer = NULL_TREE;
13667 is_direct_init = false;
13668 is_non_constant_init = true;
13669 if (is_initialized)
13671 if (function_declarator_p (declarator))
13673 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
13674 if (initialization_kind == CPP_EQ)
13675 initializer = cp_parser_pure_specifier (parser);
13676 else
13678 /* If the declaration was erroneous, we don't really
13679 know what the user intended, so just silently
13680 consume the initializer. */
13681 if (decl != error_mark_node)
13682 error_at (initializer_start_token->location,
13683 "initializer provided for function");
13684 cp_parser_skip_to_closing_parenthesis (parser,
13685 /*recovering=*/true,
13686 /*or_comma=*/false,
13687 /*consume_paren=*/true);
13690 else
13692 /* We want to record the extra mangling scope for in-class
13693 initializers of class members and initializers of static data
13694 member templates. The former is a C++0x feature which isn't
13695 implemented yet, and I expect it will involve deferring
13696 parsing of the initializer until end of class as with default
13697 arguments. So right here we only handle the latter. */
13698 if (!member_p && processing_template_decl)
13699 start_lambda_scope (decl);
13700 initializer = cp_parser_initializer (parser,
13701 &is_direct_init,
13702 &is_non_constant_init);
13703 if (!member_p && processing_template_decl)
13704 finish_lambda_scope ();
13708 /* The old parser allows attributes to appear after a parenthesized
13709 initializer. Mark Mitchell proposed removing this functionality
13710 on the GCC mailing lists on 2002-08-13. This parser accepts the
13711 attributes -- but ignores them. */
13712 if (cp_parser_allow_gnu_extensions_p (parser)
13713 && initialization_kind == CPP_OPEN_PAREN)
13714 if (cp_parser_attributes_opt (parser))
13715 warning (OPT_Wattributes,
13716 "attributes after parenthesized initializer ignored");
13718 /* For an in-class declaration, use `grokfield' to create the
13719 declaration. */
13720 if (member_p)
13722 if (pushed_scope)
13724 pop_scope (pushed_scope);
13725 pushed_scope = false;
13727 decl = grokfield (declarator, decl_specifiers,
13728 initializer, !is_non_constant_init,
13729 /*asmspec=*/NULL_TREE,
13730 prefix_attributes);
13731 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
13732 cp_parser_save_default_args (parser, decl);
13735 /* Finish processing the declaration. But, skip friend
13736 declarations. */
13737 if (!friend_p && decl && decl != error_mark_node)
13739 cp_finish_decl (decl,
13740 initializer, !is_non_constant_init,
13741 asm_specification,
13742 /* If the initializer is in parentheses, then this is
13743 a direct-initialization, which means that an
13744 `explicit' constructor is OK. Otherwise, an
13745 `explicit' constructor cannot be used. */
13746 ((is_direct_init || !is_initialized)
13747 ? 0 : LOOKUP_ONLYCONVERTING));
13749 else if ((cxx_dialect != cxx98) && friend_p
13750 && decl && TREE_CODE (decl) == FUNCTION_DECL)
13751 /* Core issue #226 (C++0x only): A default template-argument
13752 shall not be specified in a friend class template
13753 declaration. */
13754 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
13755 /*is_partial=*/0, /*is_friend_decl=*/1);
13757 if (!friend_p && pushed_scope)
13758 pop_scope (pushed_scope);
13760 return decl;
13763 /* Parse a declarator.
13765 declarator:
13766 direct-declarator
13767 ptr-operator declarator
13769 abstract-declarator:
13770 ptr-operator abstract-declarator [opt]
13771 direct-abstract-declarator
13773 GNU Extensions:
13775 declarator:
13776 attributes [opt] direct-declarator
13777 attributes [opt] ptr-operator declarator
13779 abstract-declarator:
13780 attributes [opt] ptr-operator abstract-declarator [opt]
13781 attributes [opt] direct-abstract-declarator
13783 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
13784 detect constructor, destructor or conversion operators. It is set
13785 to -1 if the declarator is a name, and +1 if it is a
13786 function. Otherwise it is set to zero. Usually you just want to
13787 test for >0, but internally the negative value is used.
13789 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
13790 a decl-specifier-seq unless it declares a constructor, destructor,
13791 or conversion. It might seem that we could check this condition in
13792 semantic analysis, rather than parsing, but that makes it difficult
13793 to handle something like `f()'. We want to notice that there are
13794 no decl-specifiers, and therefore realize that this is an
13795 expression, not a declaration.)
13797 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
13798 the declarator is a direct-declarator of the form "(...)".
13800 MEMBER_P is true iff this declarator is a member-declarator. */
13802 static cp_declarator *
13803 cp_parser_declarator (cp_parser* parser,
13804 cp_parser_declarator_kind dcl_kind,
13805 int* ctor_dtor_or_conv_p,
13806 bool* parenthesized_p,
13807 bool member_p)
13809 cp_declarator *declarator;
13810 enum tree_code code;
13811 cp_cv_quals cv_quals;
13812 tree class_type;
13813 tree attributes = NULL_TREE;
13815 /* Assume this is not a constructor, destructor, or type-conversion
13816 operator. */
13817 if (ctor_dtor_or_conv_p)
13818 *ctor_dtor_or_conv_p = 0;
13820 if (cp_parser_allow_gnu_extensions_p (parser))
13821 attributes = cp_parser_attributes_opt (parser);
13823 /* Check for the ptr-operator production. */
13824 cp_parser_parse_tentatively (parser);
13825 /* Parse the ptr-operator. */
13826 code = cp_parser_ptr_operator (parser,
13827 &class_type,
13828 &cv_quals);
13829 /* If that worked, then we have a ptr-operator. */
13830 if (cp_parser_parse_definitely (parser))
13832 /* If a ptr-operator was found, then this declarator was not
13833 parenthesized. */
13834 if (parenthesized_p)
13835 *parenthesized_p = true;
13836 /* The dependent declarator is optional if we are parsing an
13837 abstract-declarator. */
13838 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13839 cp_parser_parse_tentatively (parser);
13841 /* Parse the dependent declarator. */
13842 declarator = cp_parser_declarator (parser, dcl_kind,
13843 /*ctor_dtor_or_conv_p=*/NULL,
13844 /*parenthesized_p=*/NULL,
13845 /*member_p=*/false);
13847 /* If we are parsing an abstract-declarator, we must handle the
13848 case where the dependent declarator is absent. */
13849 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
13850 && !cp_parser_parse_definitely (parser))
13851 declarator = NULL;
13853 declarator = cp_parser_make_indirect_declarator
13854 (code, class_type, cv_quals, declarator);
13856 /* Everything else is a direct-declarator. */
13857 else
13859 if (parenthesized_p)
13860 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
13861 CPP_OPEN_PAREN);
13862 declarator = cp_parser_direct_declarator (parser, dcl_kind,
13863 ctor_dtor_or_conv_p,
13864 member_p);
13867 if (attributes && declarator && declarator != cp_error_declarator)
13868 declarator->attributes = attributes;
13870 return declarator;
13873 /* Parse a direct-declarator or direct-abstract-declarator.
13875 direct-declarator:
13876 declarator-id
13877 direct-declarator ( parameter-declaration-clause )
13878 cv-qualifier-seq [opt]
13879 exception-specification [opt]
13880 direct-declarator [ constant-expression [opt] ]
13881 ( declarator )
13883 direct-abstract-declarator:
13884 direct-abstract-declarator [opt]
13885 ( parameter-declaration-clause )
13886 cv-qualifier-seq [opt]
13887 exception-specification [opt]
13888 direct-abstract-declarator [opt] [ constant-expression [opt] ]
13889 ( abstract-declarator )
13891 Returns a representation of the declarator. DCL_KIND is
13892 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
13893 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
13894 we are parsing a direct-declarator. It is
13895 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
13896 of ambiguity we prefer an abstract declarator, as per
13897 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
13898 cp_parser_declarator. */
13900 static cp_declarator *
13901 cp_parser_direct_declarator (cp_parser* parser,
13902 cp_parser_declarator_kind dcl_kind,
13903 int* ctor_dtor_or_conv_p,
13904 bool member_p)
13906 cp_token *token;
13907 cp_declarator *declarator = NULL;
13908 tree scope = NULL_TREE;
13909 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13910 bool saved_in_declarator_p = parser->in_declarator_p;
13911 bool first = true;
13912 tree pushed_scope = NULL_TREE;
13914 while (true)
13916 /* Peek at the next token. */
13917 token = cp_lexer_peek_token (parser->lexer);
13918 if (token->type == CPP_OPEN_PAREN)
13920 /* This is either a parameter-declaration-clause, or a
13921 parenthesized declarator. When we know we are parsing a
13922 named declarator, it must be a parenthesized declarator
13923 if FIRST is true. For instance, `(int)' is a
13924 parameter-declaration-clause, with an omitted
13925 direct-abstract-declarator. But `((*))', is a
13926 parenthesized abstract declarator. Finally, when T is a
13927 template parameter `(T)' is a
13928 parameter-declaration-clause, and not a parenthesized
13929 named declarator.
13931 We first try and parse a parameter-declaration-clause,
13932 and then try a nested declarator (if FIRST is true).
13934 It is not an error for it not to be a
13935 parameter-declaration-clause, even when FIRST is
13936 false. Consider,
13938 int i (int);
13939 int i (3);
13941 The first is the declaration of a function while the
13942 second is the definition of a variable, including its
13943 initializer.
13945 Having seen only the parenthesis, we cannot know which of
13946 these two alternatives should be selected. Even more
13947 complex are examples like:
13949 int i (int (a));
13950 int i (int (3));
13952 The former is a function-declaration; the latter is a
13953 variable initialization.
13955 Thus again, we try a parameter-declaration-clause, and if
13956 that fails, we back out and return. */
13958 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13960 tree params;
13961 unsigned saved_num_template_parameter_lists;
13962 bool is_declarator = false;
13963 tree t;
13965 /* In a member-declarator, the only valid interpretation
13966 of a parenthesis is the start of a
13967 parameter-declaration-clause. (It is invalid to
13968 initialize a static data member with a parenthesized
13969 initializer; only the "=" form of initialization is
13970 permitted.) */
13971 if (!member_p)
13972 cp_parser_parse_tentatively (parser);
13974 /* Consume the `('. */
13975 cp_lexer_consume_token (parser->lexer);
13976 if (first)
13978 /* If this is going to be an abstract declarator, we're
13979 in a declarator and we can't have default args. */
13980 parser->default_arg_ok_p = false;
13981 parser->in_declarator_p = true;
13984 /* Inside the function parameter list, surrounding
13985 template-parameter-lists do not apply. */
13986 saved_num_template_parameter_lists
13987 = parser->num_template_parameter_lists;
13988 parser->num_template_parameter_lists = 0;
13990 begin_scope (sk_function_parms, NULL_TREE);
13992 /* Parse the parameter-declaration-clause. */
13993 params = cp_parser_parameter_declaration_clause (parser);
13995 parser->num_template_parameter_lists
13996 = saved_num_template_parameter_lists;
13998 /* If all went well, parse the cv-qualifier-seq and the
13999 exception-specification. */
14000 if (member_p || cp_parser_parse_definitely (parser))
14002 cp_cv_quals cv_quals;
14003 tree exception_specification;
14004 tree late_return;
14006 is_declarator = true;
14008 if (ctor_dtor_or_conv_p)
14009 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
14010 first = false;
14011 /* Consume the `)'. */
14012 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
14014 /* Parse the cv-qualifier-seq. */
14015 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14016 /* And the exception-specification. */
14017 exception_specification
14018 = cp_parser_exception_specification_opt (parser);
14020 late_return
14021 = cp_parser_late_return_type_opt (parser);
14023 /* Create the function-declarator. */
14024 declarator = make_call_declarator (declarator,
14025 params,
14026 cv_quals,
14027 exception_specification,
14028 late_return);
14029 /* Any subsequent parameter lists are to do with
14030 return type, so are not those of the declared
14031 function. */
14032 parser->default_arg_ok_p = false;
14035 /* Remove the function parms from scope. */
14036 for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
14037 pop_binding (DECL_NAME (t), t);
14038 leave_scope();
14040 if (is_declarator)
14041 /* Repeat the main loop. */
14042 continue;
14045 /* If this is the first, we can try a parenthesized
14046 declarator. */
14047 if (first)
14049 bool saved_in_type_id_in_expr_p;
14051 parser->default_arg_ok_p = saved_default_arg_ok_p;
14052 parser->in_declarator_p = saved_in_declarator_p;
14054 /* Consume the `('. */
14055 cp_lexer_consume_token (parser->lexer);
14056 /* Parse the nested declarator. */
14057 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14058 parser->in_type_id_in_expr_p = true;
14059 declarator
14060 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
14061 /*parenthesized_p=*/NULL,
14062 member_p);
14063 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14064 first = false;
14065 /* Expect a `)'. */
14066 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
14067 declarator = cp_error_declarator;
14068 if (declarator == cp_error_declarator)
14069 break;
14071 goto handle_declarator;
14073 /* Otherwise, we must be done. */
14074 else
14075 break;
14077 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14078 && token->type == CPP_OPEN_SQUARE)
14080 /* Parse an array-declarator. */
14081 tree bounds;
14083 if (ctor_dtor_or_conv_p)
14084 *ctor_dtor_or_conv_p = 0;
14086 first = false;
14087 parser->default_arg_ok_p = false;
14088 parser->in_declarator_p = true;
14089 /* Consume the `['. */
14090 cp_lexer_consume_token (parser->lexer);
14091 /* Peek at the next token. */
14092 token = cp_lexer_peek_token (parser->lexer);
14093 /* If the next token is `]', then there is no
14094 constant-expression. */
14095 if (token->type != CPP_CLOSE_SQUARE)
14097 bool non_constant_p;
14099 bounds
14100 = cp_parser_constant_expression (parser,
14101 /*allow_non_constant=*/true,
14102 &non_constant_p);
14103 if (!non_constant_p)
14104 bounds = fold_non_dependent_expr (bounds);
14105 /* Normally, the array bound must be an integral constant
14106 expression. However, as an extension, we allow VLAs
14107 in function scopes. */
14108 else if (!parser->in_function_body)
14110 error_at (token->location,
14111 "array bound is not an integer constant");
14112 bounds = error_mark_node;
14114 else if (processing_template_decl && !error_operand_p (bounds))
14116 /* Remember this wasn't a constant-expression. */
14117 bounds = build_nop (TREE_TYPE (bounds), bounds);
14118 TREE_SIDE_EFFECTS (bounds) = 1;
14121 else
14122 bounds = NULL_TREE;
14123 /* Look for the closing `]'. */
14124 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
14126 declarator = cp_error_declarator;
14127 break;
14130 declarator = make_array_declarator (declarator, bounds);
14132 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
14135 tree qualifying_scope;
14136 tree unqualified_name;
14137 special_function_kind sfk;
14138 bool abstract_ok;
14139 bool pack_expansion_p = false;
14140 cp_token *declarator_id_start_token;
14142 /* Parse a declarator-id */
14143 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
14144 if (abstract_ok)
14146 cp_parser_parse_tentatively (parser);
14148 /* If we see an ellipsis, we should be looking at a
14149 parameter pack. */
14150 if (token->type == CPP_ELLIPSIS)
14152 /* Consume the `...' */
14153 cp_lexer_consume_token (parser->lexer);
14155 pack_expansion_p = true;
14159 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
14160 unqualified_name
14161 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
14162 qualifying_scope = parser->scope;
14163 if (abstract_ok)
14165 bool okay = false;
14167 if (!unqualified_name && pack_expansion_p)
14169 /* Check whether an error occurred. */
14170 okay = !cp_parser_error_occurred (parser);
14172 /* We already consumed the ellipsis to mark a
14173 parameter pack, but we have no way to report it,
14174 so abort the tentative parse. We will be exiting
14175 immediately anyway. */
14176 cp_parser_abort_tentative_parse (parser);
14178 else
14179 okay = cp_parser_parse_definitely (parser);
14181 if (!okay)
14182 unqualified_name = error_mark_node;
14183 else if (unqualified_name
14184 && (qualifying_scope
14185 || (TREE_CODE (unqualified_name)
14186 != IDENTIFIER_NODE)))
14188 cp_parser_error (parser, "expected unqualified-id");
14189 unqualified_name = error_mark_node;
14193 if (!unqualified_name)
14194 return NULL;
14195 if (unqualified_name == error_mark_node)
14197 declarator = cp_error_declarator;
14198 pack_expansion_p = false;
14199 declarator->parameter_pack_p = false;
14200 break;
14203 if (qualifying_scope && at_namespace_scope_p ()
14204 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
14206 /* In the declaration of a member of a template class
14207 outside of the class itself, the SCOPE will sometimes
14208 be a TYPENAME_TYPE. For example, given:
14210 template <typename T>
14211 int S<T>::R::i = 3;
14213 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
14214 this context, we must resolve S<T>::R to an ordinary
14215 type, rather than a typename type.
14217 The reason we normally avoid resolving TYPENAME_TYPEs
14218 is that a specialization of `S' might render
14219 `S<T>::R' not a type. However, if `S' is
14220 specialized, then this `i' will not be used, so there
14221 is no harm in resolving the types here. */
14222 tree type;
14224 /* Resolve the TYPENAME_TYPE. */
14225 type = resolve_typename_type (qualifying_scope,
14226 /*only_current_p=*/false);
14227 /* If that failed, the declarator is invalid. */
14228 if (TREE_CODE (type) == TYPENAME_TYPE)
14230 if (typedef_variant_p (type))
14231 error_at (declarator_id_start_token->location,
14232 "cannot define member of dependent typedef "
14233 "%qT", type);
14234 else
14235 error_at (declarator_id_start_token->location,
14236 "%<%T::%E%> is not a type",
14237 TYPE_CONTEXT (qualifying_scope),
14238 TYPE_IDENTIFIER (qualifying_scope));
14240 qualifying_scope = type;
14243 sfk = sfk_none;
14245 if (unqualified_name)
14247 tree class_type;
14249 if (qualifying_scope
14250 && CLASS_TYPE_P (qualifying_scope))
14251 class_type = qualifying_scope;
14252 else
14253 class_type = current_class_type;
14255 if (TREE_CODE (unqualified_name) == TYPE_DECL)
14257 tree name_type = TREE_TYPE (unqualified_name);
14258 if (class_type && same_type_p (name_type, class_type))
14260 if (qualifying_scope
14261 && CLASSTYPE_USE_TEMPLATE (name_type))
14263 error_at (declarator_id_start_token->location,
14264 "invalid use of constructor as a template");
14265 inform (declarator_id_start_token->location,
14266 "use %<%T::%D%> instead of %<%T::%D%> to "
14267 "name the constructor in a qualified name",
14268 class_type,
14269 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
14270 class_type, name_type);
14271 declarator = cp_error_declarator;
14272 break;
14274 else
14275 unqualified_name = constructor_name (class_type);
14277 else
14279 /* We do not attempt to print the declarator
14280 here because we do not have enough
14281 information about its original syntactic
14282 form. */
14283 cp_parser_error (parser, "invalid declarator");
14284 declarator = cp_error_declarator;
14285 break;
14289 if (class_type)
14291 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
14292 sfk = sfk_destructor;
14293 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
14294 sfk = sfk_conversion;
14295 else if (/* There's no way to declare a constructor
14296 for an anonymous type, even if the type
14297 got a name for linkage purposes. */
14298 !TYPE_WAS_ANONYMOUS (class_type)
14299 && constructor_name_p (unqualified_name,
14300 class_type))
14302 unqualified_name = constructor_name (class_type);
14303 sfk = sfk_constructor;
14305 else if (is_overloaded_fn (unqualified_name)
14306 && DECL_CONSTRUCTOR_P (get_first_fn
14307 (unqualified_name)))
14308 sfk = sfk_constructor;
14310 if (ctor_dtor_or_conv_p && sfk != sfk_none)
14311 *ctor_dtor_or_conv_p = -1;
14314 declarator = make_id_declarator (qualifying_scope,
14315 unqualified_name,
14316 sfk);
14317 declarator->id_loc = token->location;
14318 declarator->parameter_pack_p = pack_expansion_p;
14320 if (pack_expansion_p)
14321 maybe_warn_variadic_templates ();
14324 handle_declarator:;
14325 scope = get_scope_of_declarator (declarator);
14326 if (scope)
14327 /* Any names that appear after the declarator-id for a
14328 member are looked up in the containing scope. */
14329 pushed_scope = push_scope (scope);
14330 parser->in_declarator_p = true;
14331 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
14332 || (declarator && declarator->kind == cdk_id))
14333 /* Default args are only allowed on function
14334 declarations. */
14335 parser->default_arg_ok_p = saved_default_arg_ok_p;
14336 else
14337 parser->default_arg_ok_p = false;
14339 first = false;
14341 /* We're done. */
14342 else
14343 break;
14346 /* For an abstract declarator, we might wind up with nothing at this
14347 point. That's an error; the declarator is not optional. */
14348 if (!declarator)
14349 cp_parser_error (parser, "expected declarator");
14351 /* If we entered a scope, we must exit it now. */
14352 if (pushed_scope)
14353 pop_scope (pushed_scope);
14355 parser->default_arg_ok_p = saved_default_arg_ok_p;
14356 parser->in_declarator_p = saved_in_declarator_p;
14358 return declarator;
14361 /* Parse a ptr-operator.
14363 ptr-operator:
14364 * cv-qualifier-seq [opt]
14366 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
14368 GNU Extension:
14370 ptr-operator:
14371 & cv-qualifier-seq [opt]
14373 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
14374 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
14375 an rvalue reference. In the case of a pointer-to-member, *TYPE is
14376 filled in with the TYPE containing the member. *CV_QUALS is
14377 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
14378 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
14379 Note that the tree codes returned by this function have nothing
14380 to do with the types of trees that will be eventually be created
14381 to represent the pointer or reference type being parsed. They are
14382 just constants with suggestive names. */
14383 static enum tree_code
14384 cp_parser_ptr_operator (cp_parser* parser,
14385 tree* type,
14386 cp_cv_quals *cv_quals)
14388 enum tree_code code = ERROR_MARK;
14389 cp_token *token;
14391 /* Assume that it's not a pointer-to-member. */
14392 *type = NULL_TREE;
14393 /* And that there are no cv-qualifiers. */
14394 *cv_quals = TYPE_UNQUALIFIED;
14396 /* Peek at the next token. */
14397 token = cp_lexer_peek_token (parser->lexer);
14399 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
14400 if (token->type == CPP_MULT)
14401 code = INDIRECT_REF;
14402 else if (token->type == CPP_AND)
14403 code = ADDR_EXPR;
14404 else if ((cxx_dialect != cxx98) &&
14405 token->type == CPP_AND_AND) /* C++0x only */
14406 code = NON_LVALUE_EXPR;
14408 if (code != ERROR_MARK)
14410 /* Consume the `*', `&' or `&&'. */
14411 cp_lexer_consume_token (parser->lexer);
14413 /* A `*' can be followed by a cv-qualifier-seq, and so can a
14414 `&', if we are allowing GNU extensions. (The only qualifier
14415 that can legally appear after `&' is `restrict', but that is
14416 enforced during semantic analysis. */
14417 if (code == INDIRECT_REF
14418 || cp_parser_allow_gnu_extensions_p (parser))
14419 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14421 else
14423 /* Try the pointer-to-member case. */
14424 cp_parser_parse_tentatively (parser);
14425 /* Look for the optional `::' operator. */
14426 cp_parser_global_scope_opt (parser,
14427 /*current_scope_valid_p=*/false);
14428 /* Look for the nested-name specifier. */
14429 token = cp_lexer_peek_token (parser->lexer);
14430 cp_parser_nested_name_specifier (parser,
14431 /*typename_keyword_p=*/false,
14432 /*check_dependency_p=*/true,
14433 /*type_p=*/false,
14434 /*is_declaration=*/false);
14435 /* If we found it, and the next token is a `*', then we are
14436 indeed looking at a pointer-to-member operator. */
14437 if (!cp_parser_error_occurred (parser)
14438 && cp_parser_require (parser, CPP_MULT, "%<*%>"))
14440 /* Indicate that the `*' operator was used. */
14441 code = INDIRECT_REF;
14443 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
14444 error_at (token->location, "%qD is a namespace", parser->scope);
14445 else
14447 /* The type of which the member is a member is given by the
14448 current SCOPE. */
14449 *type = parser->scope;
14450 /* The next name will not be qualified. */
14451 parser->scope = NULL_TREE;
14452 parser->qualifying_scope = NULL_TREE;
14453 parser->object_scope = NULL_TREE;
14454 /* Look for the optional cv-qualifier-seq. */
14455 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14458 /* If that didn't work we don't have a ptr-operator. */
14459 if (!cp_parser_parse_definitely (parser))
14460 cp_parser_error (parser, "expected ptr-operator");
14463 return code;
14466 /* Parse an (optional) cv-qualifier-seq.
14468 cv-qualifier-seq:
14469 cv-qualifier cv-qualifier-seq [opt]
14471 cv-qualifier:
14472 const
14473 volatile
14475 GNU Extension:
14477 cv-qualifier:
14478 __restrict__
14480 Returns a bitmask representing the cv-qualifiers. */
14482 static cp_cv_quals
14483 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
14485 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
14487 while (true)
14489 cp_token *token;
14490 cp_cv_quals cv_qualifier;
14492 /* Peek at the next token. */
14493 token = cp_lexer_peek_token (parser->lexer);
14494 /* See if it's a cv-qualifier. */
14495 switch (token->keyword)
14497 case RID_CONST:
14498 cv_qualifier = TYPE_QUAL_CONST;
14499 break;
14501 case RID_VOLATILE:
14502 cv_qualifier = TYPE_QUAL_VOLATILE;
14503 break;
14505 case RID_RESTRICT:
14506 cv_qualifier = TYPE_QUAL_RESTRICT;
14507 break;
14509 default:
14510 cv_qualifier = TYPE_UNQUALIFIED;
14511 break;
14514 if (!cv_qualifier)
14515 break;
14517 if (cv_quals & cv_qualifier)
14519 error_at (token->location, "duplicate cv-qualifier");
14520 cp_lexer_purge_token (parser->lexer);
14522 else
14524 cp_lexer_consume_token (parser->lexer);
14525 cv_quals |= cv_qualifier;
14529 return cv_quals;
14532 /* Parse a late-specified return type, if any. This is not a separate
14533 non-terminal, but part of a function declarator, which looks like
14535 -> trailing-type-specifier-seq abstract-declarator(opt)
14537 Returns the type indicated by the type-id. */
14539 static tree
14540 cp_parser_late_return_type_opt (cp_parser* parser)
14542 cp_token *token;
14544 /* Peek at the next token. */
14545 token = cp_lexer_peek_token (parser->lexer);
14546 /* A late-specified return type is indicated by an initial '->'. */
14547 if (token->type != CPP_DEREF)
14548 return NULL_TREE;
14550 /* Consume the ->. */
14551 cp_lexer_consume_token (parser->lexer);
14553 return cp_parser_trailing_type_id (parser);
14556 /* Parse a declarator-id.
14558 declarator-id:
14559 id-expression
14560 :: [opt] nested-name-specifier [opt] type-name
14562 In the `id-expression' case, the value returned is as for
14563 cp_parser_id_expression if the id-expression was an unqualified-id.
14564 If the id-expression was a qualified-id, then a SCOPE_REF is
14565 returned. The first operand is the scope (either a NAMESPACE_DECL
14566 or TREE_TYPE), but the second is still just a representation of an
14567 unqualified-id. */
14569 static tree
14570 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
14572 tree id;
14573 /* The expression must be an id-expression. Assume that qualified
14574 names are the names of types so that:
14576 template <class T>
14577 int S<T>::R::i = 3;
14579 will work; we must treat `S<T>::R' as the name of a type.
14580 Similarly, assume that qualified names are templates, where
14581 required, so that:
14583 template <class T>
14584 int S<T>::R<T>::i = 3;
14586 will work, too. */
14587 id = cp_parser_id_expression (parser,
14588 /*template_keyword_p=*/false,
14589 /*check_dependency_p=*/false,
14590 /*template_p=*/NULL,
14591 /*declarator_p=*/true,
14592 optional_p);
14593 if (id && BASELINK_P (id))
14594 id = BASELINK_FUNCTIONS (id);
14595 return id;
14598 /* Parse a type-id.
14600 type-id:
14601 type-specifier-seq abstract-declarator [opt]
14603 Returns the TYPE specified. */
14605 static tree
14606 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
14607 bool is_trailing_return)
14609 cp_decl_specifier_seq type_specifier_seq;
14610 cp_declarator *abstract_declarator;
14612 /* Parse the type-specifier-seq. */
14613 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14614 is_trailing_return,
14615 &type_specifier_seq);
14616 if (type_specifier_seq.type == error_mark_node)
14617 return error_mark_node;
14619 /* There might or might not be an abstract declarator. */
14620 cp_parser_parse_tentatively (parser);
14621 /* Look for the declarator. */
14622 abstract_declarator
14623 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
14624 /*parenthesized_p=*/NULL,
14625 /*member_p=*/false);
14626 /* Check to see if there really was a declarator. */
14627 if (!cp_parser_parse_definitely (parser))
14628 abstract_declarator = NULL;
14630 if (type_specifier_seq.type
14631 && type_uses_auto (type_specifier_seq.type))
14633 /* A type-id with type 'auto' is only ok if the abstract declarator
14634 is a function declarator with a late-specified return type. */
14635 if (abstract_declarator
14636 && abstract_declarator->kind == cdk_function
14637 && abstract_declarator->u.function.late_return_type)
14638 /* OK */;
14639 else
14641 error ("invalid use of %<auto%>");
14642 return error_mark_node;
14646 return groktypename (&type_specifier_seq, abstract_declarator,
14647 is_template_arg);
14650 static tree cp_parser_type_id (cp_parser *parser)
14652 return cp_parser_type_id_1 (parser, false, false);
14655 static tree cp_parser_template_type_arg (cp_parser *parser)
14657 return cp_parser_type_id_1 (parser, true, false);
14660 static tree cp_parser_trailing_type_id (cp_parser *parser)
14662 return cp_parser_type_id_1 (parser, false, true);
14665 /* Parse a type-specifier-seq.
14667 type-specifier-seq:
14668 type-specifier type-specifier-seq [opt]
14670 GNU extension:
14672 type-specifier-seq:
14673 attributes type-specifier-seq [opt]
14675 If IS_DECLARATION is true, we are at the start of a "condition" or
14676 exception-declaration, so we might be followed by a declarator-id.
14678 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
14679 i.e. we've just seen "->".
14681 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
14683 static void
14684 cp_parser_type_specifier_seq (cp_parser* parser,
14685 bool is_declaration,
14686 bool is_trailing_return,
14687 cp_decl_specifier_seq *type_specifier_seq)
14689 bool seen_type_specifier = false;
14690 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
14691 cp_token *start_token = NULL;
14693 /* Clear the TYPE_SPECIFIER_SEQ. */
14694 clear_decl_specs (type_specifier_seq);
14696 /* In the context of a trailing return type, enum E { } is an
14697 elaborated-type-specifier followed by a function-body, not an
14698 enum-specifier. */
14699 if (is_trailing_return)
14700 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
14702 /* Parse the type-specifiers and attributes. */
14703 while (true)
14705 tree type_specifier;
14706 bool is_cv_qualifier;
14708 /* Check for attributes first. */
14709 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
14711 type_specifier_seq->attributes =
14712 chainon (type_specifier_seq->attributes,
14713 cp_parser_attributes_opt (parser));
14714 continue;
14717 /* record the token of the beginning of the type specifier seq,
14718 for error reporting purposes*/
14719 if (!start_token)
14720 start_token = cp_lexer_peek_token (parser->lexer);
14722 /* Look for the type-specifier. */
14723 type_specifier = cp_parser_type_specifier (parser,
14724 flags,
14725 type_specifier_seq,
14726 /*is_declaration=*/false,
14727 NULL,
14728 &is_cv_qualifier);
14729 if (!type_specifier)
14731 /* If the first type-specifier could not be found, this is not a
14732 type-specifier-seq at all. */
14733 if (!seen_type_specifier)
14735 cp_parser_error (parser, "expected type-specifier");
14736 type_specifier_seq->type = error_mark_node;
14737 return;
14739 /* If subsequent type-specifiers could not be found, the
14740 type-specifier-seq is complete. */
14741 break;
14744 seen_type_specifier = true;
14745 /* The standard says that a condition can be:
14747 type-specifier-seq declarator = assignment-expression
14749 However, given:
14751 struct S {};
14752 if (int S = ...)
14754 we should treat the "S" as a declarator, not as a
14755 type-specifier. The standard doesn't say that explicitly for
14756 type-specifier-seq, but it does say that for
14757 decl-specifier-seq in an ordinary declaration. Perhaps it
14758 would be clearer just to allow a decl-specifier-seq here, and
14759 then add a semantic restriction that if any decl-specifiers
14760 that are not type-specifiers appear, the program is invalid. */
14761 if (is_declaration && !is_cv_qualifier)
14762 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
14765 cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
14768 /* Parse a parameter-declaration-clause.
14770 parameter-declaration-clause:
14771 parameter-declaration-list [opt] ... [opt]
14772 parameter-declaration-list , ...
14774 Returns a representation for the parameter declarations. A return
14775 value of NULL indicates a parameter-declaration-clause consisting
14776 only of an ellipsis. */
14778 static tree
14779 cp_parser_parameter_declaration_clause (cp_parser* parser)
14781 tree parameters;
14782 cp_token *token;
14783 bool ellipsis_p;
14784 bool is_error;
14786 /* Peek at the next token. */
14787 token = cp_lexer_peek_token (parser->lexer);
14788 /* Check for trivial parameter-declaration-clauses. */
14789 if (token->type == CPP_ELLIPSIS)
14791 /* Consume the `...' token. */
14792 cp_lexer_consume_token (parser->lexer);
14793 return NULL_TREE;
14795 else if (token->type == CPP_CLOSE_PAREN)
14796 /* There are no parameters. */
14798 #ifndef NO_IMPLICIT_EXTERN_C
14799 if (in_system_header && current_class_type == NULL
14800 && current_lang_name == lang_name_c)
14801 return NULL_TREE;
14802 else
14803 #endif
14804 return void_list_node;
14806 /* Check for `(void)', too, which is a special case. */
14807 else if (token->keyword == RID_VOID
14808 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
14809 == CPP_CLOSE_PAREN))
14811 /* Consume the `void' token. */
14812 cp_lexer_consume_token (parser->lexer);
14813 /* There are no parameters. */
14814 return void_list_node;
14817 /* Parse the parameter-declaration-list. */
14818 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
14819 /* If a parse error occurred while parsing the
14820 parameter-declaration-list, then the entire
14821 parameter-declaration-clause is erroneous. */
14822 if (is_error)
14823 return NULL;
14825 /* Peek at the next token. */
14826 token = cp_lexer_peek_token (parser->lexer);
14827 /* If it's a `,', the clause should terminate with an ellipsis. */
14828 if (token->type == CPP_COMMA)
14830 /* Consume the `,'. */
14831 cp_lexer_consume_token (parser->lexer);
14832 /* Expect an ellipsis. */
14833 ellipsis_p
14834 = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
14836 /* It might also be `...' if the optional trailing `,' was
14837 omitted. */
14838 else if (token->type == CPP_ELLIPSIS)
14840 /* Consume the `...' token. */
14841 cp_lexer_consume_token (parser->lexer);
14842 /* And remember that we saw it. */
14843 ellipsis_p = true;
14845 else
14846 ellipsis_p = false;
14848 /* Finish the parameter list. */
14849 if (!ellipsis_p)
14850 parameters = chainon (parameters, void_list_node);
14852 return parameters;
14855 /* Parse a parameter-declaration-list.
14857 parameter-declaration-list:
14858 parameter-declaration
14859 parameter-declaration-list , parameter-declaration
14861 Returns a representation of the parameter-declaration-list, as for
14862 cp_parser_parameter_declaration_clause. However, the
14863 `void_list_node' is never appended to the list. Upon return,
14864 *IS_ERROR will be true iff an error occurred. */
14866 static tree
14867 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
14869 tree parameters = NULL_TREE;
14870 tree *tail = &parameters;
14871 bool saved_in_unbraced_linkage_specification_p;
14872 int index = 0;
14874 /* Assume all will go well. */
14875 *is_error = false;
14876 /* The special considerations that apply to a function within an
14877 unbraced linkage specifications do not apply to the parameters
14878 to the function. */
14879 saved_in_unbraced_linkage_specification_p
14880 = parser->in_unbraced_linkage_specification_p;
14881 parser->in_unbraced_linkage_specification_p = false;
14883 /* Look for more parameters. */
14884 while (true)
14886 cp_parameter_declarator *parameter;
14887 tree decl = error_mark_node;
14888 bool parenthesized_p;
14889 /* Parse the parameter. */
14890 parameter
14891 = cp_parser_parameter_declaration (parser,
14892 /*template_parm_p=*/false,
14893 &parenthesized_p);
14895 /* We don't know yet if the enclosing context is deprecated, so wait
14896 and warn in grokparms if appropriate. */
14897 deprecated_state = DEPRECATED_SUPPRESS;
14899 if (parameter)
14900 decl = grokdeclarator (parameter->declarator,
14901 &parameter->decl_specifiers,
14902 PARM,
14903 parameter->default_argument != NULL_TREE,
14904 &parameter->decl_specifiers.attributes);
14906 deprecated_state = DEPRECATED_NORMAL;
14908 /* If a parse error occurred parsing the parameter declaration,
14909 then the entire parameter-declaration-list is erroneous. */
14910 if (decl == error_mark_node)
14912 *is_error = true;
14913 parameters = error_mark_node;
14914 break;
14917 if (parameter->decl_specifiers.attributes)
14918 cplus_decl_attributes (&decl,
14919 parameter->decl_specifiers.attributes,
14921 if (DECL_NAME (decl))
14922 decl = pushdecl (decl);
14924 if (decl != error_mark_node)
14926 retrofit_lang_decl (decl);
14927 DECL_PARM_INDEX (decl) = ++index;
14930 /* Add the new parameter to the list. */
14931 *tail = build_tree_list (parameter->default_argument, decl);
14932 tail = &TREE_CHAIN (*tail);
14934 /* Peek at the next token. */
14935 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
14936 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
14937 /* These are for Objective-C++ */
14938 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14939 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14940 /* The parameter-declaration-list is complete. */
14941 break;
14942 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14944 cp_token *token;
14946 /* Peek at the next token. */
14947 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14948 /* If it's an ellipsis, then the list is complete. */
14949 if (token->type == CPP_ELLIPSIS)
14950 break;
14951 /* Otherwise, there must be more parameters. Consume the
14952 `,'. */
14953 cp_lexer_consume_token (parser->lexer);
14954 /* When parsing something like:
14956 int i(float f, double d)
14958 we can tell after seeing the declaration for "f" that we
14959 are not looking at an initialization of a variable "i",
14960 but rather at the declaration of a function "i".
14962 Due to the fact that the parsing of template arguments
14963 (as specified to a template-id) requires backtracking we
14964 cannot use this technique when inside a template argument
14965 list. */
14966 if (!parser->in_template_argument_list_p
14967 && !parser->in_type_id_in_expr_p
14968 && cp_parser_uncommitted_to_tentative_parse_p (parser)
14969 /* However, a parameter-declaration of the form
14970 "foat(f)" (which is a valid declaration of a
14971 parameter "f") can also be interpreted as an
14972 expression (the conversion of "f" to "float"). */
14973 && !parenthesized_p)
14974 cp_parser_commit_to_tentative_parse (parser);
14976 else
14978 cp_parser_error (parser, "expected %<,%> or %<...%>");
14979 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14980 cp_parser_skip_to_closing_parenthesis (parser,
14981 /*recovering=*/true,
14982 /*or_comma=*/false,
14983 /*consume_paren=*/false);
14984 break;
14988 parser->in_unbraced_linkage_specification_p
14989 = saved_in_unbraced_linkage_specification_p;
14991 return parameters;
14994 /* Parse a parameter declaration.
14996 parameter-declaration:
14997 decl-specifier-seq ... [opt] declarator
14998 decl-specifier-seq declarator = assignment-expression
14999 decl-specifier-seq ... [opt] abstract-declarator [opt]
15000 decl-specifier-seq abstract-declarator [opt] = assignment-expression
15002 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
15003 declares a template parameter. (In that case, a non-nested `>'
15004 token encountered during the parsing of the assignment-expression
15005 is not interpreted as a greater-than operator.)
15007 Returns a representation of the parameter, or NULL if an error
15008 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
15009 true iff the declarator is of the form "(p)". */
15011 static cp_parameter_declarator *
15012 cp_parser_parameter_declaration (cp_parser *parser,
15013 bool template_parm_p,
15014 bool *parenthesized_p)
15016 int declares_class_or_enum;
15017 cp_decl_specifier_seq decl_specifiers;
15018 cp_declarator *declarator;
15019 tree default_argument;
15020 cp_token *token = NULL, *declarator_token_start = NULL;
15021 const char *saved_message;
15023 /* In a template parameter, `>' is not an operator.
15025 [temp.param]
15027 When parsing a default template-argument for a non-type
15028 template-parameter, the first non-nested `>' is taken as the end
15029 of the template parameter-list rather than a greater-than
15030 operator. */
15032 /* Type definitions may not appear in parameter types. */
15033 saved_message = parser->type_definition_forbidden_message;
15034 parser->type_definition_forbidden_message
15035 = G_("types may not be defined in parameter types");
15037 /* Parse the declaration-specifiers. */
15038 cp_parser_decl_specifier_seq (parser,
15039 CP_PARSER_FLAGS_NONE,
15040 &decl_specifiers,
15041 &declares_class_or_enum);
15043 /* Complain about missing 'typename' or other invalid type names. */
15044 if (!decl_specifiers.any_type_specifiers_p)
15045 cp_parser_parse_and_diagnose_invalid_type_name (parser);
15047 /* If an error occurred, there's no reason to attempt to parse the
15048 rest of the declaration. */
15049 if (cp_parser_error_occurred (parser))
15051 parser->type_definition_forbidden_message = saved_message;
15052 return NULL;
15055 /* Peek at the next token. */
15056 token = cp_lexer_peek_token (parser->lexer);
15058 /* If the next token is a `)', `,', `=', `>', or `...', then there
15059 is no declarator. However, when variadic templates are enabled,
15060 there may be a declarator following `...'. */
15061 if (token->type == CPP_CLOSE_PAREN
15062 || token->type == CPP_COMMA
15063 || token->type == CPP_EQ
15064 || token->type == CPP_GREATER)
15066 declarator = NULL;
15067 if (parenthesized_p)
15068 *parenthesized_p = false;
15070 /* Otherwise, there should be a declarator. */
15071 else
15073 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
15074 parser->default_arg_ok_p = false;
15076 /* After seeing a decl-specifier-seq, if the next token is not a
15077 "(", there is no possibility that the code is a valid
15078 expression. Therefore, if parsing tentatively, we commit at
15079 this point. */
15080 if (!parser->in_template_argument_list_p
15081 /* In an expression context, having seen:
15083 (int((char ...
15085 we cannot be sure whether we are looking at a
15086 function-type (taking a "char" as a parameter) or a cast
15087 of some object of type "char" to "int". */
15088 && !parser->in_type_id_in_expr_p
15089 && cp_parser_uncommitted_to_tentative_parse_p (parser)
15090 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
15091 cp_parser_commit_to_tentative_parse (parser);
15092 /* Parse the declarator. */
15093 declarator_token_start = token;
15094 declarator = cp_parser_declarator (parser,
15095 CP_PARSER_DECLARATOR_EITHER,
15096 /*ctor_dtor_or_conv_p=*/NULL,
15097 parenthesized_p,
15098 /*member_p=*/false);
15099 parser->default_arg_ok_p = saved_default_arg_ok_p;
15100 /* After the declarator, allow more attributes. */
15101 decl_specifiers.attributes
15102 = chainon (decl_specifiers.attributes,
15103 cp_parser_attributes_opt (parser));
15106 /* If the next token is an ellipsis, and we have not seen a
15107 declarator name, and the type of the declarator contains parameter
15108 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
15109 a parameter pack expansion expression. Otherwise, leave the
15110 ellipsis for a C-style variadic function. */
15111 token = cp_lexer_peek_token (parser->lexer);
15112 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15114 tree type = decl_specifiers.type;
15116 if (type && DECL_P (type))
15117 type = TREE_TYPE (type);
15119 if (type
15120 && TREE_CODE (type) != TYPE_PACK_EXPANSION
15121 && declarator_can_be_parameter_pack (declarator)
15122 && (!declarator || !declarator->parameter_pack_p)
15123 && uses_parameter_packs (type))
15125 /* Consume the `...'. */
15126 cp_lexer_consume_token (parser->lexer);
15127 maybe_warn_variadic_templates ();
15129 /* Build a pack expansion type */
15130 if (declarator)
15131 declarator->parameter_pack_p = true;
15132 else
15133 decl_specifiers.type = make_pack_expansion (type);
15137 /* The restriction on defining new types applies only to the type
15138 of the parameter, not to the default argument. */
15139 parser->type_definition_forbidden_message = saved_message;
15141 /* If the next token is `=', then process a default argument. */
15142 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15144 /* Consume the `='. */
15145 cp_lexer_consume_token (parser->lexer);
15147 /* If we are defining a class, then the tokens that make up the
15148 default argument must be saved and processed later. */
15149 if (!template_parm_p && at_class_scope_p ()
15150 && TYPE_BEING_DEFINED (current_class_type)
15151 && !LAMBDA_TYPE_P (current_class_type))
15153 unsigned depth = 0;
15154 int maybe_template_id = 0;
15155 cp_token *first_token;
15156 cp_token *token;
15158 /* Add tokens until we have processed the entire default
15159 argument. We add the range [first_token, token). */
15160 first_token = cp_lexer_peek_token (parser->lexer);
15161 while (true)
15163 bool done = false;
15165 /* Peek at the next token. */
15166 token = cp_lexer_peek_token (parser->lexer);
15167 /* What we do depends on what token we have. */
15168 switch (token->type)
15170 /* In valid code, a default argument must be
15171 immediately followed by a `,' `)', or `...'. */
15172 case CPP_COMMA:
15173 if (depth == 0 && maybe_template_id)
15175 /* If we've seen a '<', we might be in a
15176 template-argument-list. Until Core issue 325 is
15177 resolved, we don't know how this situation ought
15178 to be handled, so try to DTRT. We check whether
15179 what comes after the comma is a valid parameter
15180 declaration list. If it is, then the comma ends
15181 the default argument; otherwise the default
15182 argument continues. */
15183 bool error = false;
15185 /* Set ITALP so cp_parser_parameter_declaration_list
15186 doesn't decide to commit to this parse. */
15187 bool saved_italp = parser->in_template_argument_list_p;
15188 parser->in_template_argument_list_p = true;
15190 cp_parser_parse_tentatively (parser);
15191 cp_lexer_consume_token (parser->lexer);
15192 cp_parser_parameter_declaration_list (parser, &error);
15193 if (!cp_parser_error_occurred (parser) && !error)
15194 done = true;
15195 cp_parser_abort_tentative_parse (parser);
15197 parser->in_template_argument_list_p = saved_italp;
15198 break;
15200 case CPP_CLOSE_PAREN:
15201 case CPP_ELLIPSIS:
15202 /* If we run into a non-nested `;', `}', or `]',
15203 then the code is invalid -- but the default
15204 argument is certainly over. */
15205 case CPP_SEMICOLON:
15206 case CPP_CLOSE_BRACE:
15207 case CPP_CLOSE_SQUARE:
15208 if (depth == 0)
15209 done = true;
15210 /* Update DEPTH, if necessary. */
15211 else if (token->type == CPP_CLOSE_PAREN
15212 || token->type == CPP_CLOSE_BRACE
15213 || token->type == CPP_CLOSE_SQUARE)
15214 --depth;
15215 break;
15217 case CPP_OPEN_PAREN:
15218 case CPP_OPEN_SQUARE:
15219 case CPP_OPEN_BRACE:
15220 ++depth;
15221 break;
15223 case CPP_LESS:
15224 if (depth == 0)
15225 /* This might be the comparison operator, or it might
15226 start a template argument list. */
15227 ++maybe_template_id;
15228 break;
15230 case CPP_RSHIFT:
15231 if (cxx_dialect == cxx98)
15232 break;
15233 /* Fall through for C++0x, which treats the `>>'
15234 operator like two `>' tokens in certain
15235 cases. */
15237 case CPP_GREATER:
15238 if (depth == 0)
15240 /* This might be an operator, or it might close a
15241 template argument list. But if a previous '<'
15242 started a template argument list, this will have
15243 closed it, so we can't be in one anymore. */
15244 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
15245 if (maybe_template_id < 0)
15246 maybe_template_id = 0;
15248 break;
15250 /* If we run out of tokens, issue an error message. */
15251 case CPP_EOF:
15252 case CPP_PRAGMA_EOL:
15253 error_at (token->location, "file ends in default argument");
15254 done = true;
15255 break;
15257 case CPP_NAME:
15258 case CPP_SCOPE:
15259 /* In these cases, we should look for template-ids.
15260 For example, if the default argument is
15261 `X<int, double>()', we need to do name lookup to
15262 figure out whether or not `X' is a template; if
15263 so, the `,' does not end the default argument.
15265 That is not yet done. */
15266 break;
15268 default:
15269 break;
15272 /* If we've reached the end, stop. */
15273 if (done)
15274 break;
15276 /* Add the token to the token block. */
15277 token = cp_lexer_consume_token (parser->lexer);
15280 /* Create a DEFAULT_ARG to represent the unparsed default
15281 argument. */
15282 default_argument = make_node (DEFAULT_ARG);
15283 DEFARG_TOKENS (default_argument)
15284 = cp_token_cache_new (first_token, token);
15285 DEFARG_INSTANTIATIONS (default_argument) = NULL;
15287 /* Outside of a class definition, we can just parse the
15288 assignment-expression. */
15289 else
15291 token = cp_lexer_peek_token (parser->lexer);
15292 default_argument
15293 = cp_parser_default_argument (parser, template_parm_p);
15296 if (!parser->default_arg_ok_p)
15298 if (flag_permissive)
15299 warning (0, "deprecated use of default argument for parameter of non-function");
15300 else
15302 error_at (token->location,
15303 "default arguments are only "
15304 "permitted for function parameters");
15305 default_argument = NULL_TREE;
15308 else if ((declarator && declarator->parameter_pack_p)
15309 || (decl_specifiers.type
15310 && PACK_EXPANSION_P (decl_specifiers.type)))
15312 /* Find the name of the parameter pack. */
15313 cp_declarator *id_declarator = declarator;
15314 while (id_declarator && id_declarator->kind != cdk_id)
15315 id_declarator = id_declarator->declarator;
15317 if (id_declarator && id_declarator->kind == cdk_id)
15318 error_at (declarator_token_start->location,
15319 template_parm_p
15320 ? "template parameter pack %qD"
15321 " cannot have a default argument"
15322 : "parameter pack %qD cannot have a default argument",
15323 id_declarator->u.id.unqualified_name);
15324 else
15325 error_at (declarator_token_start->location,
15326 template_parm_p
15327 ? "template parameter pack cannot have a default argument"
15328 : "parameter pack cannot have a default argument");
15330 default_argument = NULL_TREE;
15333 else
15334 default_argument = NULL_TREE;
15336 return make_parameter_declarator (&decl_specifiers,
15337 declarator,
15338 default_argument);
15341 /* Parse a default argument and return it.
15343 TEMPLATE_PARM_P is true if this is a default argument for a
15344 non-type template parameter. */
15345 static tree
15346 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
15348 tree default_argument = NULL_TREE;
15349 bool saved_greater_than_is_operator_p;
15350 bool saved_local_variables_forbidden_p;
15352 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
15353 set correctly. */
15354 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
15355 parser->greater_than_is_operator_p = !template_parm_p;
15356 /* Local variable names (and the `this' keyword) may not
15357 appear in a default argument. */
15358 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15359 parser->local_variables_forbidden_p = true;
15360 /* Parse the assignment-expression. */
15361 if (template_parm_p)
15362 push_deferring_access_checks (dk_no_deferred);
15363 default_argument
15364 = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
15365 if (template_parm_p)
15366 pop_deferring_access_checks ();
15367 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
15368 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15370 return default_argument;
15373 /* Parse a function-body.
15375 function-body:
15376 compound_statement */
15378 static void
15379 cp_parser_function_body (cp_parser *parser)
15381 cp_parser_compound_statement (parser, NULL, false);
15384 /* Parse a ctor-initializer-opt followed by a function-body. Return
15385 true if a ctor-initializer was present. */
15387 static bool
15388 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
15390 tree body;
15391 bool ctor_initializer_p;
15393 /* Begin the function body. */
15394 body = begin_function_body ();
15395 /* Parse the optional ctor-initializer. */
15396 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
15397 /* Parse the function-body. */
15398 cp_parser_function_body (parser);
15399 /* Finish the function body. */
15400 finish_function_body (body);
15402 return ctor_initializer_p;
15405 /* Parse an initializer.
15407 initializer:
15408 = initializer-clause
15409 ( expression-list )
15411 Returns an expression representing the initializer. If no
15412 initializer is present, NULL_TREE is returned.
15414 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
15415 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
15416 set to TRUE if there is no initializer present. If there is an
15417 initializer, and it is not a constant-expression, *NON_CONSTANT_P
15418 is set to true; otherwise it is set to false. */
15420 static tree
15421 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
15422 bool* non_constant_p)
15424 cp_token *token;
15425 tree init;
15427 /* Peek at the next token. */
15428 token = cp_lexer_peek_token (parser->lexer);
15430 /* Let our caller know whether or not this initializer was
15431 parenthesized. */
15432 *is_direct_init = (token->type != CPP_EQ);
15433 /* Assume that the initializer is constant. */
15434 *non_constant_p = false;
15436 if (token->type == CPP_EQ)
15438 /* Consume the `='. */
15439 cp_lexer_consume_token (parser->lexer);
15440 /* Parse the initializer-clause. */
15441 init = cp_parser_initializer_clause (parser, non_constant_p);
15443 else if (token->type == CPP_OPEN_PAREN)
15445 VEC(tree,gc) *vec;
15446 vec = cp_parser_parenthesized_expression_list (parser, false,
15447 /*cast_p=*/false,
15448 /*allow_expansion_p=*/true,
15449 non_constant_p);
15450 if (vec == NULL)
15451 return error_mark_node;
15452 init = build_tree_list_vec (vec);
15453 release_tree_vector (vec);
15455 else if (token->type == CPP_OPEN_BRACE)
15457 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
15458 init = cp_parser_braced_list (parser, non_constant_p);
15459 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
15461 else
15463 /* Anything else is an error. */
15464 cp_parser_error (parser, "expected initializer");
15465 init = error_mark_node;
15468 return init;
15471 /* Parse an initializer-clause.
15473 initializer-clause:
15474 assignment-expression
15475 braced-init-list
15477 Returns an expression representing the initializer.
15479 If the `assignment-expression' production is used the value
15480 returned is simply a representation for the expression.
15482 Otherwise, calls cp_parser_braced_list. */
15484 static tree
15485 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
15487 tree initializer;
15489 /* Assume the expression is constant. */
15490 *non_constant_p = false;
15492 /* If it is not a `{', then we are looking at an
15493 assignment-expression. */
15494 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
15496 initializer
15497 = cp_parser_constant_expression (parser,
15498 /*allow_non_constant_p=*/true,
15499 non_constant_p);
15500 if (!*non_constant_p)
15501 initializer = fold_non_dependent_expr (initializer);
15503 else
15504 initializer = cp_parser_braced_list (parser, non_constant_p);
15506 return initializer;
15509 /* Parse a brace-enclosed initializer list.
15511 braced-init-list:
15512 { initializer-list , [opt] }
15515 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
15516 the elements of the initializer-list (or NULL, if the last
15517 production is used). The TREE_TYPE for the CONSTRUCTOR will be
15518 NULL_TREE. There is no way to detect whether or not the optional
15519 trailing `,' was provided. NON_CONSTANT_P is as for
15520 cp_parser_initializer. */
15522 static tree
15523 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
15525 tree initializer;
15527 /* Consume the `{' token. */
15528 cp_lexer_consume_token (parser->lexer);
15529 /* Create a CONSTRUCTOR to represent the braced-initializer. */
15530 initializer = make_node (CONSTRUCTOR);
15531 /* If it's not a `}', then there is a non-trivial initializer. */
15532 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
15534 /* Parse the initializer list. */
15535 CONSTRUCTOR_ELTS (initializer)
15536 = cp_parser_initializer_list (parser, non_constant_p);
15537 /* A trailing `,' token is allowed. */
15538 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15539 cp_lexer_consume_token (parser->lexer);
15541 /* Now, there should be a trailing `}'. */
15542 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15543 TREE_TYPE (initializer) = init_list_type_node;
15544 return initializer;
15547 /* Parse an initializer-list.
15549 initializer-list:
15550 initializer-clause ... [opt]
15551 initializer-list , initializer-clause ... [opt]
15553 GNU Extension:
15555 initializer-list:
15556 identifier : initializer-clause
15557 initializer-list, identifier : initializer-clause
15559 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
15560 for the initializer. If the INDEX of the elt is non-NULL, it is the
15561 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
15562 as for cp_parser_initializer. */
15564 static VEC(constructor_elt,gc) *
15565 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
15567 VEC(constructor_elt,gc) *v = NULL;
15569 /* Assume all of the expressions are constant. */
15570 *non_constant_p = false;
15572 /* Parse the rest of the list. */
15573 while (true)
15575 cp_token *token;
15576 tree identifier;
15577 tree initializer;
15578 bool clause_non_constant_p;
15580 /* If the next token is an identifier and the following one is a
15581 colon, we are looking at the GNU designated-initializer
15582 syntax. */
15583 if (cp_parser_allow_gnu_extensions_p (parser)
15584 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
15585 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
15587 /* Warn the user that they are using an extension. */
15588 pedwarn (input_location, OPT_pedantic,
15589 "ISO C++ does not allow designated initializers");
15590 /* Consume the identifier. */
15591 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
15592 /* Consume the `:'. */
15593 cp_lexer_consume_token (parser->lexer);
15595 else
15596 identifier = NULL_TREE;
15598 /* Parse the initializer. */
15599 initializer = cp_parser_initializer_clause (parser,
15600 &clause_non_constant_p);
15601 /* If any clause is non-constant, so is the entire initializer. */
15602 if (clause_non_constant_p)
15603 *non_constant_p = true;
15605 /* If we have an ellipsis, this is an initializer pack
15606 expansion. */
15607 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15609 /* Consume the `...'. */
15610 cp_lexer_consume_token (parser->lexer);
15612 /* Turn the initializer into an initializer expansion. */
15613 initializer = make_pack_expansion (initializer);
15616 /* Add it to the vector. */
15617 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
15619 /* If the next token is not a comma, we have reached the end of
15620 the list. */
15621 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15622 break;
15624 /* Peek at the next token. */
15625 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15626 /* If the next token is a `}', then we're still done. An
15627 initializer-clause can have a trailing `,' after the
15628 initializer-list and before the closing `}'. */
15629 if (token->type == CPP_CLOSE_BRACE)
15630 break;
15632 /* Consume the `,' token. */
15633 cp_lexer_consume_token (parser->lexer);
15636 return v;
15639 /* Classes [gram.class] */
15641 /* Parse a class-name.
15643 class-name:
15644 identifier
15645 template-id
15647 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
15648 to indicate that names looked up in dependent types should be
15649 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
15650 keyword has been used to indicate that the name that appears next
15651 is a template. TAG_TYPE indicates the explicit tag given before
15652 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
15653 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
15654 is the class being defined in a class-head.
15656 Returns the TYPE_DECL representing the class. */
15658 static tree
15659 cp_parser_class_name (cp_parser *parser,
15660 bool typename_keyword_p,
15661 bool template_keyword_p,
15662 enum tag_types tag_type,
15663 bool check_dependency_p,
15664 bool class_head_p,
15665 bool is_declaration)
15667 tree decl;
15668 tree scope;
15669 bool typename_p;
15670 cp_token *token;
15671 tree identifier = NULL_TREE;
15673 /* All class-names start with an identifier. */
15674 token = cp_lexer_peek_token (parser->lexer);
15675 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
15677 cp_parser_error (parser, "expected class-name");
15678 return error_mark_node;
15681 /* PARSER->SCOPE can be cleared when parsing the template-arguments
15682 to a template-id, so we save it here. */
15683 scope = parser->scope;
15684 if (scope == error_mark_node)
15685 return error_mark_node;
15687 /* Any name names a type if we're following the `typename' keyword
15688 in a qualified name where the enclosing scope is type-dependent. */
15689 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
15690 && dependent_type_p (scope));
15691 /* Handle the common case (an identifier, but not a template-id)
15692 efficiently. */
15693 if (token->type == CPP_NAME
15694 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
15696 cp_token *identifier_token;
15697 bool ambiguous_p;
15699 /* Look for the identifier. */
15700 identifier_token = cp_lexer_peek_token (parser->lexer);
15701 ambiguous_p = identifier_token->ambiguous_p;
15702 identifier = cp_parser_identifier (parser);
15703 /* If the next token isn't an identifier, we are certainly not
15704 looking at a class-name. */
15705 if (identifier == error_mark_node)
15706 decl = error_mark_node;
15707 /* If we know this is a type-name, there's no need to look it
15708 up. */
15709 else if (typename_p)
15710 decl = identifier;
15711 else
15713 tree ambiguous_decls;
15714 /* If we already know that this lookup is ambiguous, then
15715 we've already issued an error message; there's no reason
15716 to check again. */
15717 if (ambiguous_p)
15719 cp_parser_simulate_error (parser);
15720 return error_mark_node;
15722 /* If the next token is a `::', then the name must be a type
15723 name.
15725 [basic.lookup.qual]
15727 During the lookup for a name preceding the :: scope
15728 resolution operator, object, function, and enumerator
15729 names are ignored. */
15730 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15731 tag_type = typename_type;
15732 /* Look up the name. */
15733 decl = cp_parser_lookup_name (parser, identifier,
15734 tag_type,
15735 /*is_template=*/false,
15736 /*is_namespace=*/false,
15737 check_dependency_p,
15738 &ambiguous_decls,
15739 identifier_token->location);
15740 if (ambiguous_decls)
15742 if (cp_parser_parsing_tentatively (parser))
15743 cp_parser_simulate_error (parser);
15744 return error_mark_node;
15748 else
15750 /* Try a template-id. */
15751 decl = cp_parser_template_id (parser, template_keyword_p,
15752 check_dependency_p,
15753 is_declaration);
15754 if (decl == error_mark_node)
15755 return error_mark_node;
15758 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
15760 /* If this is a typename, create a TYPENAME_TYPE. */
15761 if (typename_p && decl != error_mark_node)
15763 decl = make_typename_type (scope, decl, typename_type,
15764 /*complain=*/tf_error);
15765 if (decl != error_mark_node)
15766 decl = TYPE_NAME (decl);
15769 /* Check to see that it is really the name of a class. */
15770 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15771 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
15772 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15773 /* Situations like this:
15775 template <typename T> struct A {
15776 typename T::template X<int>::I i;
15779 are problematic. Is `T::template X<int>' a class-name? The
15780 standard does not seem to be definitive, but there is no other
15781 valid interpretation of the following `::'. Therefore, those
15782 names are considered class-names. */
15784 decl = make_typename_type (scope, decl, tag_type, tf_error);
15785 if (decl != error_mark_node)
15786 decl = TYPE_NAME (decl);
15788 else if (TREE_CODE (decl) != TYPE_DECL
15789 || TREE_TYPE (decl) == error_mark_node
15790 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
15791 decl = error_mark_node;
15793 if (decl == error_mark_node)
15794 cp_parser_error (parser, "expected class-name");
15795 else if (identifier && !parser->scope)
15796 maybe_note_name_used_in_class (identifier, decl);
15798 return decl;
15801 /* Parse a class-specifier.
15803 class-specifier:
15804 class-head { member-specification [opt] }
15806 Returns the TREE_TYPE representing the class. */
15808 static tree
15809 cp_parser_class_specifier (cp_parser* parser)
15811 tree type;
15812 tree attributes = NULL_TREE;
15813 bool nested_name_specifier_p;
15814 unsigned saved_num_template_parameter_lists;
15815 bool saved_in_function_body;
15816 bool saved_in_unbraced_linkage_specification_p;
15817 tree old_scope = NULL_TREE;
15818 tree scope = NULL_TREE;
15819 tree bases;
15821 push_deferring_access_checks (dk_no_deferred);
15823 /* Parse the class-head. */
15824 type = cp_parser_class_head (parser,
15825 &nested_name_specifier_p,
15826 &attributes,
15827 &bases);
15828 /* If the class-head was a semantic disaster, skip the entire body
15829 of the class. */
15830 if (!type)
15832 cp_parser_skip_to_end_of_block_or_statement (parser);
15833 pop_deferring_access_checks ();
15834 return error_mark_node;
15837 /* Look for the `{'. */
15838 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
15840 pop_deferring_access_checks ();
15841 return error_mark_node;
15844 /* Process the base classes. If they're invalid, skip the
15845 entire class body. */
15846 if (!xref_basetypes (type, bases))
15848 /* Consuming the closing brace yields better error messages
15849 later on. */
15850 if (cp_parser_skip_to_closing_brace (parser))
15851 cp_lexer_consume_token (parser->lexer);
15852 pop_deferring_access_checks ();
15853 return error_mark_node;
15856 /* Issue an error message if type-definitions are forbidden here. */
15857 cp_parser_check_type_definition (parser);
15858 /* Remember that we are defining one more class. */
15859 ++parser->num_classes_being_defined;
15860 /* Inside the class, surrounding template-parameter-lists do not
15861 apply. */
15862 saved_num_template_parameter_lists
15863 = parser->num_template_parameter_lists;
15864 parser->num_template_parameter_lists = 0;
15865 /* We are not in a function body. */
15866 saved_in_function_body = parser->in_function_body;
15867 parser->in_function_body = false;
15868 /* We are not immediately inside an extern "lang" block. */
15869 saved_in_unbraced_linkage_specification_p
15870 = parser->in_unbraced_linkage_specification_p;
15871 parser->in_unbraced_linkage_specification_p = false;
15873 /* Start the class. */
15874 if (nested_name_specifier_p)
15876 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
15877 old_scope = push_inner_scope (scope);
15879 type = begin_class_definition (type, attributes);
15881 if (type == error_mark_node)
15882 /* If the type is erroneous, skip the entire body of the class. */
15883 cp_parser_skip_to_closing_brace (parser);
15884 else
15885 /* Parse the member-specification. */
15886 cp_parser_member_specification_opt (parser);
15888 /* Look for the trailing `}'. */
15889 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15890 /* Look for trailing attributes to apply to this class. */
15891 if (cp_parser_allow_gnu_extensions_p (parser))
15892 attributes = cp_parser_attributes_opt (parser);
15893 if (type != error_mark_node)
15894 type = finish_struct (type, attributes);
15895 if (nested_name_specifier_p)
15896 pop_inner_scope (old_scope, scope);
15897 /* If this class is not itself within the scope of another class,
15898 then we need to parse the bodies of all of the queued function
15899 definitions. Note that the queued functions defined in a class
15900 are not always processed immediately following the
15901 class-specifier for that class. Consider:
15903 struct A {
15904 struct B { void f() { sizeof (A); } };
15907 If `f' were processed before the processing of `A' were
15908 completed, there would be no way to compute the size of `A'.
15909 Note that the nesting we are interested in here is lexical --
15910 not the semantic nesting given by TYPE_CONTEXT. In particular,
15911 for:
15913 struct A { struct B; };
15914 struct A::B { void f() { } };
15916 there is no need to delay the parsing of `A::B::f'. */
15917 if (--parser->num_classes_being_defined == 0)
15919 tree queue_entry;
15920 tree fn;
15921 tree class_type = NULL_TREE;
15922 tree pushed_scope = NULL_TREE;
15924 /* In a first pass, parse default arguments to the functions.
15925 Then, in a second pass, parse the bodies of the functions.
15926 This two-phased approach handles cases like:
15928 struct S {
15929 void f() { g(); }
15930 void g(int i = 3);
15934 for (TREE_PURPOSE (parser->unparsed_functions_queues)
15935 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
15936 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
15937 TREE_PURPOSE (parser->unparsed_functions_queues)
15938 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
15940 fn = TREE_VALUE (queue_entry);
15941 /* If there are default arguments that have not yet been processed,
15942 take care of them now. */
15943 if (class_type != TREE_PURPOSE (queue_entry))
15945 if (pushed_scope)
15946 pop_scope (pushed_scope);
15947 class_type = TREE_PURPOSE (queue_entry);
15948 pushed_scope = push_scope (class_type);
15950 /* Make sure that any template parameters are in scope. */
15951 maybe_begin_member_template_processing (fn);
15952 /* Parse the default argument expressions. */
15953 cp_parser_late_parsing_default_args (parser, fn);
15954 /* Remove any template parameters from the symbol table. */
15955 maybe_end_member_template_processing ();
15957 if (pushed_scope)
15958 pop_scope (pushed_scope);
15959 /* Now parse the body of the functions. */
15960 for (TREE_VALUE (parser->unparsed_functions_queues)
15961 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
15962 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
15963 TREE_VALUE (parser->unparsed_functions_queues)
15964 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
15966 /* Figure out which function we need to process. */
15967 fn = TREE_VALUE (queue_entry);
15968 /* Parse the function. */
15969 cp_parser_late_parsing_for_member (parser, fn);
15973 /* Put back any saved access checks. */
15974 pop_deferring_access_checks ();
15976 /* Restore saved state. */
15977 parser->in_function_body = saved_in_function_body;
15978 parser->num_template_parameter_lists
15979 = saved_num_template_parameter_lists;
15980 parser->in_unbraced_linkage_specification_p
15981 = saved_in_unbraced_linkage_specification_p;
15983 return type;
15986 /* Parse a class-head.
15988 class-head:
15989 class-key identifier [opt] base-clause [opt]
15990 class-key nested-name-specifier identifier base-clause [opt]
15991 class-key nested-name-specifier [opt] template-id
15992 base-clause [opt]
15994 GNU Extensions:
15995 class-key attributes identifier [opt] base-clause [opt]
15996 class-key attributes nested-name-specifier identifier base-clause [opt]
15997 class-key attributes nested-name-specifier [opt] template-id
15998 base-clause [opt]
16000 Upon return BASES is initialized to the list of base classes (or
16001 NULL, if there are none) in the same form returned by
16002 cp_parser_base_clause.
16004 Returns the TYPE of the indicated class. Sets
16005 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
16006 involving a nested-name-specifier was used, and FALSE otherwise.
16008 Returns error_mark_node if this is not a class-head.
16010 Returns NULL_TREE if the class-head is syntactically valid, but
16011 semantically invalid in a way that means we should skip the entire
16012 body of the class. */
16014 static tree
16015 cp_parser_class_head (cp_parser* parser,
16016 bool* nested_name_specifier_p,
16017 tree *attributes_p,
16018 tree *bases)
16020 tree nested_name_specifier;
16021 enum tag_types class_key;
16022 tree id = NULL_TREE;
16023 tree type = NULL_TREE;
16024 tree attributes;
16025 bool template_id_p = false;
16026 bool qualified_p = false;
16027 bool invalid_nested_name_p = false;
16028 bool invalid_explicit_specialization_p = false;
16029 tree pushed_scope = NULL_TREE;
16030 unsigned num_templates;
16031 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
16032 /* Assume no nested-name-specifier will be present. */
16033 *nested_name_specifier_p = false;
16034 /* Assume no template parameter lists will be used in defining the
16035 type. */
16036 num_templates = 0;
16038 *bases = NULL_TREE;
16040 /* Look for the class-key. */
16041 class_key = cp_parser_class_key (parser);
16042 if (class_key == none_type)
16043 return error_mark_node;
16045 /* Parse the attributes. */
16046 attributes = cp_parser_attributes_opt (parser);
16048 /* If the next token is `::', that is invalid -- but sometimes
16049 people do try to write:
16051 struct ::S {};
16053 Handle this gracefully by accepting the extra qualifier, and then
16054 issuing an error about it later if this really is a
16055 class-head. If it turns out just to be an elaborated type
16056 specifier, remain silent. */
16057 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
16058 qualified_p = true;
16060 push_deferring_access_checks (dk_no_check);
16062 /* Determine the name of the class. Begin by looking for an
16063 optional nested-name-specifier. */
16064 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
16065 nested_name_specifier
16066 = cp_parser_nested_name_specifier_opt (parser,
16067 /*typename_keyword_p=*/false,
16068 /*check_dependency_p=*/false,
16069 /*type_p=*/false,
16070 /*is_declaration=*/false);
16071 /* If there was a nested-name-specifier, then there *must* be an
16072 identifier. */
16073 if (nested_name_specifier)
16075 type_start_token = cp_lexer_peek_token (parser->lexer);
16076 /* Although the grammar says `identifier', it really means
16077 `class-name' or `template-name'. You are only allowed to
16078 define a class that has already been declared with this
16079 syntax.
16081 The proposed resolution for Core Issue 180 says that wherever
16082 you see `class T::X' you should treat `X' as a type-name.
16084 It is OK to define an inaccessible class; for example:
16086 class A { class B; };
16087 class A::B {};
16089 We do not know if we will see a class-name, or a
16090 template-name. We look for a class-name first, in case the
16091 class-name is a template-id; if we looked for the
16092 template-name first we would stop after the template-name. */
16093 cp_parser_parse_tentatively (parser);
16094 type = cp_parser_class_name (parser,
16095 /*typename_keyword_p=*/false,
16096 /*template_keyword_p=*/false,
16097 class_type,
16098 /*check_dependency_p=*/false,
16099 /*class_head_p=*/true,
16100 /*is_declaration=*/false);
16101 /* If that didn't work, ignore the nested-name-specifier. */
16102 if (!cp_parser_parse_definitely (parser))
16104 invalid_nested_name_p = true;
16105 type_start_token = cp_lexer_peek_token (parser->lexer);
16106 id = cp_parser_identifier (parser);
16107 if (id == error_mark_node)
16108 id = NULL_TREE;
16110 /* If we could not find a corresponding TYPE, treat this
16111 declaration like an unqualified declaration. */
16112 if (type == error_mark_node)
16113 nested_name_specifier = NULL_TREE;
16114 /* Otherwise, count the number of templates used in TYPE and its
16115 containing scopes. */
16116 else
16118 tree scope;
16120 for (scope = TREE_TYPE (type);
16121 scope && TREE_CODE (scope) != NAMESPACE_DECL;
16122 scope = (TYPE_P (scope)
16123 ? TYPE_CONTEXT (scope)
16124 : DECL_CONTEXT (scope)))
16125 if (TYPE_P (scope)
16126 && CLASS_TYPE_P (scope)
16127 && CLASSTYPE_TEMPLATE_INFO (scope)
16128 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
16129 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
16130 ++num_templates;
16133 /* Otherwise, the identifier is optional. */
16134 else
16136 /* We don't know whether what comes next is a template-id,
16137 an identifier, or nothing at all. */
16138 cp_parser_parse_tentatively (parser);
16139 /* Check for a template-id. */
16140 type_start_token = cp_lexer_peek_token (parser->lexer);
16141 id = cp_parser_template_id (parser,
16142 /*template_keyword_p=*/false,
16143 /*check_dependency_p=*/true,
16144 /*is_declaration=*/true);
16145 /* If that didn't work, it could still be an identifier. */
16146 if (!cp_parser_parse_definitely (parser))
16148 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16150 type_start_token = cp_lexer_peek_token (parser->lexer);
16151 id = cp_parser_identifier (parser);
16153 else
16154 id = NULL_TREE;
16156 else
16158 template_id_p = true;
16159 ++num_templates;
16163 pop_deferring_access_checks ();
16165 if (id)
16166 cp_parser_check_for_invalid_template_id (parser, id,
16167 type_start_token->location);
16169 /* If it's not a `:' or a `{' then we can't really be looking at a
16170 class-head, since a class-head only appears as part of a
16171 class-specifier. We have to detect this situation before calling
16172 xref_tag, since that has irreversible side-effects. */
16173 if (!cp_parser_next_token_starts_class_definition_p (parser))
16175 cp_parser_error (parser, "expected %<{%> or %<:%>");
16176 return error_mark_node;
16179 /* At this point, we're going ahead with the class-specifier, even
16180 if some other problem occurs. */
16181 cp_parser_commit_to_tentative_parse (parser);
16182 /* Issue the error about the overly-qualified name now. */
16183 if (qualified_p)
16185 cp_parser_error (parser,
16186 "global qualification of class name is invalid");
16187 return error_mark_node;
16189 else if (invalid_nested_name_p)
16191 cp_parser_error (parser,
16192 "qualified name does not name a class");
16193 return error_mark_node;
16195 else if (nested_name_specifier)
16197 tree scope;
16199 /* Reject typedef-names in class heads. */
16200 if (!DECL_IMPLICIT_TYPEDEF_P (type))
16202 error_at (type_start_token->location,
16203 "invalid class name in declaration of %qD",
16204 type);
16205 type = NULL_TREE;
16206 goto done;
16209 /* Figure out in what scope the declaration is being placed. */
16210 scope = current_scope ();
16211 /* If that scope does not contain the scope in which the
16212 class was originally declared, the program is invalid. */
16213 if (scope && !is_ancestor (scope, nested_name_specifier))
16215 if (at_namespace_scope_p ())
16216 error_at (type_start_token->location,
16217 "declaration of %qD in namespace %qD which does not "
16218 "enclose %qD",
16219 type, scope, nested_name_specifier);
16220 else
16221 error_at (type_start_token->location,
16222 "declaration of %qD in %qD which does not enclose %qD",
16223 type, scope, nested_name_specifier);
16224 type = NULL_TREE;
16225 goto done;
16227 /* [dcl.meaning]
16229 A declarator-id shall not be qualified except for the
16230 definition of a ... nested class outside of its class
16231 ... [or] the definition or explicit instantiation of a
16232 class member of a namespace outside of its namespace. */
16233 if (scope == nested_name_specifier)
16235 permerror (nested_name_specifier_token_start->location,
16236 "extra qualification not allowed");
16237 nested_name_specifier = NULL_TREE;
16238 num_templates = 0;
16241 /* An explicit-specialization must be preceded by "template <>". If
16242 it is not, try to recover gracefully. */
16243 if (at_namespace_scope_p ()
16244 && parser->num_template_parameter_lists == 0
16245 && template_id_p)
16247 error_at (type_start_token->location,
16248 "an explicit specialization must be preceded by %<template <>%>");
16249 invalid_explicit_specialization_p = true;
16250 /* Take the same action that would have been taken by
16251 cp_parser_explicit_specialization. */
16252 ++parser->num_template_parameter_lists;
16253 begin_specialization ();
16255 /* There must be no "return" statements between this point and the
16256 end of this function; set "type "to the correct return value and
16257 use "goto done;" to return. */
16258 /* Make sure that the right number of template parameters were
16259 present. */
16260 if (!cp_parser_check_template_parameters (parser, num_templates,
16261 type_start_token->location,
16262 /*declarator=*/NULL))
16264 /* If something went wrong, there is no point in even trying to
16265 process the class-definition. */
16266 type = NULL_TREE;
16267 goto done;
16270 /* Look up the type. */
16271 if (template_id_p)
16273 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
16274 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
16275 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
16277 error_at (type_start_token->location,
16278 "function template %qD redeclared as a class template", id);
16279 type = error_mark_node;
16281 else
16283 type = TREE_TYPE (id);
16284 type = maybe_process_partial_specialization (type);
16286 if (nested_name_specifier)
16287 pushed_scope = push_scope (nested_name_specifier);
16289 else if (nested_name_specifier)
16291 tree class_type;
16293 /* Given:
16295 template <typename T> struct S { struct T };
16296 template <typename T> struct S<T>::T { };
16298 we will get a TYPENAME_TYPE when processing the definition of
16299 `S::T'. We need to resolve it to the actual type before we
16300 try to define it. */
16301 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
16303 class_type = resolve_typename_type (TREE_TYPE (type),
16304 /*only_current_p=*/false);
16305 if (TREE_CODE (class_type) != TYPENAME_TYPE)
16306 type = TYPE_NAME (class_type);
16307 else
16309 cp_parser_error (parser, "could not resolve typename type");
16310 type = error_mark_node;
16314 if (maybe_process_partial_specialization (TREE_TYPE (type))
16315 == error_mark_node)
16317 type = NULL_TREE;
16318 goto done;
16321 class_type = current_class_type;
16322 /* Enter the scope indicated by the nested-name-specifier. */
16323 pushed_scope = push_scope (nested_name_specifier);
16324 /* Get the canonical version of this type. */
16325 type = TYPE_MAIN_DECL (TREE_TYPE (type));
16326 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
16327 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
16329 type = push_template_decl (type);
16330 if (type == error_mark_node)
16332 type = NULL_TREE;
16333 goto done;
16337 type = TREE_TYPE (type);
16338 *nested_name_specifier_p = true;
16340 else /* The name is not a nested name. */
16342 /* If the class was unnamed, create a dummy name. */
16343 if (!id)
16344 id = make_anon_name ();
16345 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
16346 parser->num_template_parameter_lists);
16349 /* Indicate whether this class was declared as a `class' or as a
16350 `struct'. */
16351 if (TREE_CODE (type) == RECORD_TYPE)
16352 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
16353 cp_parser_check_class_key (class_key, type);
16355 /* If this type was already complete, and we see another definition,
16356 that's an error. */
16357 if (type != error_mark_node && COMPLETE_TYPE_P (type))
16359 error_at (type_start_token->location, "redefinition of %q#T",
16360 type);
16361 error_at (type_start_token->location, "previous definition of %q+#T",
16362 type);
16363 type = NULL_TREE;
16364 goto done;
16366 else if (type == error_mark_node)
16367 type = NULL_TREE;
16369 /* We will have entered the scope containing the class; the names of
16370 base classes should be looked up in that context. For example:
16372 struct A { struct B {}; struct C; };
16373 struct A::C : B {};
16375 is valid. */
16377 /* Get the list of base-classes, if there is one. */
16378 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16379 *bases = cp_parser_base_clause (parser);
16381 done:
16382 /* Leave the scope given by the nested-name-specifier. We will
16383 enter the class scope itself while processing the members. */
16384 if (pushed_scope)
16385 pop_scope (pushed_scope);
16387 if (invalid_explicit_specialization_p)
16389 end_specialization ();
16390 --parser->num_template_parameter_lists;
16392 *attributes_p = attributes;
16393 return type;
16396 /* Parse a class-key.
16398 class-key:
16399 class
16400 struct
16401 union
16403 Returns the kind of class-key specified, or none_type to indicate
16404 error. */
16406 static enum tag_types
16407 cp_parser_class_key (cp_parser* parser)
16409 cp_token *token;
16410 enum tag_types tag_type;
16412 /* Look for the class-key. */
16413 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
16414 if (!token)
16415 return none_type;
16417 /* Check to see if the TOKEN is a class-key. */
16418 tag_type = cp_parser_token_is_class_key (token);
16419 if (!tag_type)
16420 cp_parser_error (parser, "expected class-key");
16421 return tag_type;
16424 /* Parse an (optional) member-specification.
16426 member-specification:
16427 member-declaration member-specification [opt]
16428 access-specifier : member-specification [opt] */
16430 static void
16431 cp_parser_member_specification_opt (cp_parser* parser)
16433 while (true)
16435 cp_token *token;
16436 enum rid keyword;
16438 /* Peek at the next token. */
16439 token = cp_lexer_peek_token (parser->lexer);
16440 /* If it's a `}', or EOF then we've seen all the members. */
16441 if (token->type == CPP_CLOSE_BRACE
16442 || token->type == CPP_EOF
16443 || token->type == CPP_PRAGMA_EOL)
16444 break;
16446 /* See if this token is a keyword. */
16447 keyword = token->keyword;
16448 switch (keyword)
16450 case RID_PUBLIC:
16451 case RID_PROTECTED:
16452 case RID_PRIVATE:
16453 /* Consume the access-specifier. */
16454 cp_lexer_consume_token (parser->lexer);
16455 /* Remember which access-specifier is active. */
16456 current_access_specifier = token->u.value;
16457 /* Look for the `:'. */
16458 cp_parser_require (parser, CPP_COLON, "%<:%>");
16459 break;
16461 default:
16462 /* Accept #pragmas at class scope. */
16463 if (token->type == CPP_PRAGMA)
16465 cp_parser_pragma (parser, pragma_external);
16466 break;
16469 /* Otherwise, the next construction must be a
16470 member-declaration. */
16471 cp_parser_member_declaration (parser);
16476 /* Parse a member-declaration.
16478 member-declaration:
16479 decl-specifier-seq [opt] member-declarator-list [opt] ;
16480 function-definition ; [opt]
16481 :: [opt] nested-name-specifier template [opt] unqualified-id ;
16482 using-declaration
16483 template-declaration
16485 member-declarator-list:
16486 member-declarator
16487 member-declarator-list , member-declarator
16489 member-declarator:
16490 declarator pure-specifier [opt]
16491 declarator constant-initializer [opt]
16492 identifier [opt] : constant-expression
16494 GNU Extensions:
16496 member-declaration:
16497 __extension__ member-declaration
16499 member-declarator:
16500 declarator attributes [opt] pure-specifier [opt]
16501 declarator attributes [opt] constant-initializer [opt]
16502 identifier [opt] attributes [opt] : constant-expression
16504 C++0x Extensions:
16506 member-declaration:
16507 static_assert-declaration */
16509 static void
16510 cp_parser_member_declaration (cp_parser* parser)
16512 cp_decl_specifier_seq decl_specifiers;
16513 tree prefix_attributes;
16514 tree decl;
16515 int declares_class_or_enum;
16516 bool friend_p;
16517 cp_token *token = NULL;
16518 cp_token *decl_spec_token_start = NULL;
16519 cp_token *initializer_token_start = NULL;
16520 int saved_pedantic;
16522 /* Check for the `__extension__' keyword. */
16523 if (cp_parser_extension_opt (parser, &saved_pedantic))
16525 /* Recurse. */
16526 cp_parser_member_declaration (parser);
16527 /* Restore the old value of the PEDANTIC flag. */
16528 pedantic = saved_pedantic;
16530 return;
16533 /* Check for a template-declaration. */
16534 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16536 /* An explicit specialization here is an error condition, and we
16537 expect the specialization handler to detect and report this. */
16538 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16539 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
16540 cp_parser_explicit_specialization (parser);
16541 else
16542 cp_parser_template_declaration (parser, /*member_p=*/true);
16544 return;
16547 /* Check for a using-declaration. */
16548 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
16550 /* Parse the using-declaration. */
16551 cp_parser_using_declaration (parser,
16552 /*access_declaration_p=*/false);
16553 return;
16556 /* Check for @defs. */
16557 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
16559 tree ivar, member;
16560 tree ivar_chains = cp_parser_objc_defs_expression (parser);
16561 ivar = ivar_chains;
16562 while (ivar)
16564 member = ivar;
16565 ivar = TREE_CHAIN (member);
16566 TREE_CHAIN (member) = NULL_TREE;
16567 finish_member_declaration (member);
16569 return;
16572 /* If the next token is `static_assert' we have a static assertion. */
16573 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
16575 cp_parser_static_assert (parser, /*member_p=*/true);
16576 return;
16579 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
16580 return;
16582 /* Parse the decl-specifier-seq. */
16583 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
16584 cp_parser_decl_specifier_seq (parser,
16585 CP_PARSER_FLAGS_OPTIONAL,
16586 &decl_specifiers,
16587 &declares_class_or_enum);
16588 prefix_attributes = decl_specifiers.attributes;
16589 decl_specifiers.attributes = NULL_TREE;
16590 /* Check for an invalid type-name. */
16591 if (!decl_specifiers.any_type_specifiers_p
16592 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
16593 return;
16594 /* If there is no declarator, then the decl-specifier-seq should
16595 specify a type. */
16596 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16598 /* If there was no decl-specifier-seq, and the next token is a
16599 `;', then we have something like:
16601 struct S { ; };
16603 [class.mem]
16605 Each member-declaration shall declare at least one member
16606 name of the class. */
16607 if (!decl_specifiers.any_specifiers_p)
16609 cp_token *token = cp_lexer_peek_token (parser->lexer);
16610 if (!in_system_header_at (token->location))
16611 pedwarn (token->location, OPT_pedantic, "extra %<;%>");
16613 else
16615 tree type;
16617 /* See if this declaration is a friend. */
16618 friend_p = cp_parser_friend_p (&decl_specifiers);
16619 /* If there were decl-specifiers, check to see if there was
16620 a class-declaration. */
16621 type = check_tag_decl (&decl_specifiers);
16622 /* Nested classes have already been added to the class, but
16623 a `friend' needs to be explicitly registered. */
16624 if (friend_p)
16626 /* If the `friend' keyword was present, the friend must
16627 be introduced with a class-key. */
16628 if (!declares_class_or_enum)
16629 error_at (decl_spec_token_start->location,
16630 "a class-key must be used when declaring a friend");
16631 /* In this case:
16633 template <typename T> struct A {
16634 friend struct A<T>::B;
16637 A<T>::B will be represented by a TYPENAME_TYPE, and
16638 therefore not recognized by check_tag_decl. */
16639 if (!type
16640 && decl_specifiers.type
16641 && TYPE_P (decl_specifiers.type))
16642 type = decl_specifiers.type;
16643 if (!type || !TYPE_P (type))
16644 error_at (decl_spec_token_start->location,
16645 "friend declaration does not name a class or "
16646 "function");
16647 else
16648 make_friend_class (current_class_type, type,
16649 /*complain=*/true);
16651 /* If there is no TYPE, an error message will already have
16652 been issued. */
16653 else if (!type || type == error_mark_node)
16655 /* An anonymous aggregate has to be handled specially; such
16656 a declaration really declares a data member (with a
16657 particular type), as opposed to a nested class. */
16658 else if (ANON_AGGR_TYPE_P (type))
16660 /* Remove constructors and such from TYPE, now that we
16661 know it is an anonymous aggregate. */
16662 fixup_anonymous_aggr (type);
16663 /* And make the corresponding data member. */
16664 decl = build_decl (decl_spec_token_start->location,
16665 FIELD_DECL, NULL_TREE, type);
16666 /* Add it to the class. */
16667 finish_member_declaration (decl);
16669 else
16670 cp_parser_check_access_in_redeclaration
16671 (TYPE_NAME (type),
16672 decl_spec_token_start->location);
16675 else
16677 /* See if these declarations will be friends. */
16678 friend_p = cp_parser_friend_p (&decl_specifiers);
16680 /* Keep going until we hit the `;' at the end of the
16681 declaration. */
16682 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16684 tree attributes = NULL_TREE;
16685 tree first_attribute;
16687 /* Peek at the next token. */
16688 token = cp_lexer_peek_token (parser->lexer);
16690 /* Check for a bitfield declaration. */
16691 if (token->type == CPP_COLON
16692 || (token->type == CPP_NAME
16693 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
16694 == CPP_COLON))
16696 tree identifier;
16697 tree width;
16699 /* Get the name of the bitfield. Note that we cannot just
16700 check TOKEN here because it may have been invalidated by
16701 the call to cp_lexer_peek_nth_token above. */
16702 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
16703 identifier = cp_parser_identifier (parser);
16704 else
16705 identifier = NULL_TREE;
16707 /* Consume the `:' token. */
16708 cp_lexer_consume_token (parser->lexer);
16709 /* Get the width of the bitfield. */
16710 width
16711 = cp_parser_constant_expression (parser,
16712 /*allow_non_constant=*/false,
16713 NULL);
16715 /* Look for attributes that apply to the bitfield. */
16716 attributes = cp_parser_attributes_opt (parser);
16717 /* Remember which attributes are prefix attributes and
16718 which are not. */
16719 first_attribute = attributes;
16720 /* Combine the attributes. */
16721 attributes = chainon (prefix_attributes, attributes);
16723 /* Create the bitfield declaration. */
16724 decl = grokbitfield (identifier
16725 ? make_id_declarator (NULL_TREE,
16726 identifier,
16727 sfk_none)
16728 : NULL,
16729 &decl_specifiers,
16730 width,
16731 attributes);
16733 else
16735 cp_declarator *declarator;
16736 tree initializer;
16737 tree asm_specification;
16738 int ctor_dtor_or_conv_p;
16740 /* Parse the declarator. */
16741 declarator
16742 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16743 &ctor_dtor_or_conv_p,
16744 /*parenthesized_p=*/NULL,
16745 /*member_p=*/true);
16747 /* If something went wrong parsing the declarator, make sure
16748 that we at least consume some tokens. */
16749 if (declarator == cp_error_declarator)
16751 /* Skip to the end of the statement. */
16752 cp_parser_skip_to_end_of_statement (parser);
16753 /* If the next token is not a semicolon, that is
16754 probably because we just skipped over the body of
16755 a function. So, we consume a semicolon if
16756 present, but do not issue an error message if it
16757 is not present. */
16758 if (cp_lexer_next_token_is (parser->lexer,
16759 CPP_SEMICOLON))
16760 cp_lexer_consume_token (parser->lexer);
16761 return;
16764 if (declares_class_or_enum & 2)
16765 cp_parser_check_for_definition_in_return_type
16766 (declarator, decl_specifiers.type,
16767 decl_specifiers.type_location);
16769 /* Look for an asm-specification. */
16770 asm_specification = cp_parser_asm_specification_opt (parser);
16771 /* Look for attributes that apply to the declaration. */
16772 attributes = cp_parser_attributes_opt (parser);
16773 /* Remember which attributes are prefix attributes and
16774 which are not. */
16775 first_attribute = attributes;
16776 /* Combine the attributes. */
16777 attributes = chainon (prefix_attributes, attributes);
16779 /* If it's an `=', then we have a constant-initializer or a
16780 pure-specifier. It is not correct to parse the
16781 initializer before registering the member declaration
16782 since the member declaration should be in scope while
16783 its initializer is processed. However, the rest of the
16784 front end does not yet provide an interface that allows
16785 us to handle this correctly. */
16786 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16788 /* In [class.mem]:
16790 A pure-specifier shall be used only in the declaration of
16791 a virtual function.
16793 A member-declarator can contain a constant-initializer
16794 only if it declares a static member of integral or
16795 enumeration type.
16797 Therefore, if the DECLARATOR is for a function, we look
16798 for a pure-specifier; otherwise, we look for a
16799 constant-initializer. When we call `grokfield', it will
16800 perform more stringent semantics checks. */
16801 initializer_token_start = cp_lexer_peek_token (parser->lexer);
16802 if (function_declarator_p (declarator))
16803 initializer = cp_parser_pure_specifier (parser);
16804 else
16805 /* Parse the initializer. */
16806 initializer = cp_parser_constant_initializer (parser);
16808 /* Otherwise, there is no initializer. */
16809 else
16810 initializer = NULL_TREE;
16812 /* See if we are probably looking at a function
16813 definition. We are certainly not looking at a
16814 member-declarator. Calling `grokfield' has
16815 side-effects, so we must not do it unless we are sure
16816 that we are looking at a member-declarator. */
16817 if (cp_parser_token_starts_function_definition_p
16818 (cp_lexer_peek_token (parser->lexer)))
16820 /* The grammar does not allow a pure-specifier to be
16821 used when a member function is defined. (It is
16822 possible that this fact is an oversight in the
16823 standard, since a pure function may be defined
16824 outside of the class-specifier. */
16825 if (initializer)
16826 error_at (initializer_token_start->location,
16827 "pure-specifier on function-definition");
16828 decl = cp_parser_save_member_function_body (parser,
16829 &decl_specifiers,
16830 declarator,
16831 attributes);
16832 /* If the member was not a friend, declare it here. */
16833 if (!friend_p)
16834 finish_member_declaration (decl);
16835 /* Peek at the next token. */
16836 token = cp_lexer_peek_token (parser->lexer);
16837 /* If the next token is a semicolon, consume it. */
16838 if (token->type == CPP_SEMICOLON)
16839 cp_lexer_consume_token (parser->lexer);
16840 return;
16842 else
16843 if (declarator->kind == cdk_function)
16844 declarator->id_loc = token->location;
16845 /* Create the declaration. */
16846 decl = grokfield (declarator, &decl_specifiers,
16847 initializer, /*init_const_expr_p=*/true,
16848 asm_specification,
16849 attributes);
16852 /* Reset PREFIX_ATTRIBUTES. */
16853 while (attributes && TREE_CHAIN (attributes) != first_attribute)
16854 attributes = TREE_CHAIN (attributes);
16855 if (attributes)
16856 TREE_CHAIN (attributes) = NULL_TREE;
16858 /* If there is any qualification still in effect, clear it
16859 now; we will be starting fresh with the next declarator. */
16860 parser->scope = NULL_TREE;
16861 parser->qualifying_scope = NULL_TREE;
16862 parser->object_scope = NULL_TREE;
16863 /* If it's a `,', then there are more declarators. */
16864 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16865 cp_lexer_consume_token (parser->lexer);
16866 /* If the next token isn't a `;', then we have a parse error. */
16867 else if (cp_lexer_next_token_is_not (parser->lexer,
16868 CPP_SEMICOLON))
16870 cp_parser_error (parser, "expected %<;%>");
16871 /* Skip tokens until we find a `;'. */
16872 cp_parser_skip_to_end_of_statement (parser);
16874 break;
16877 if (decl)
16879 /* Add DECL to the list of members. */
16880 if (!friend_p)
16881 finish_member_declaration (decl);
16883 if (TREE_CODE (decl) == FUNCTION_DECL)
16884 cp_parser_save_default_args (parser, decl);
16889 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16892 /* Parse a pure-specifier.
16894 pure-specifier:
16897 Returns INTEGER_ZERO_NODE if a pure specifier is found.
16898 Otherwise, ERROR_MARK_NODE is returned. */
16900 static tree
16901 cp_parser_pure_specifier (cp_parser* parser)
16903 cp_token *token;
16905 /* Look for the `=' token. */
16906 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16907 return error_mark_node;
16908 /* Look for the `0' token. */
16909 token = cp_lexer_peek_token (parser->lexer);
16911 if (token->type == CPP_EOF
16912 || token->type == CPP_PRAGMA_EOL)
16913 return error_mark_node;
16915 cp_lexer_consume_token (parser->lexer);
16917 /* Accept = default or = delete in c++0x mode. */
16918 if (token->keyword == RID_DEFAULT
16919 || token->keyword == RID_DELETE)
16921 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
16922 return token->u.value;
16925 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
16926 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
16928 cp_parser_error (parser,
16929 "invalid pure specifier (only %<= 0%> is allowed)");
16930 cp_parser_skip_to_end_of_statement (parser);
16931 return error_mark_node;
16933 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
16935 error_at (token->location, "templates may not be %<virtual%>");
16936 return error_mark_node;
16939 return integer_zero_node;
16942 /* Parse a constant-initializer.
16944 constant-initializer:
16945 = constant-expression
16947 Returns a representation of the constant-expression. */
16949 static tree
16950 cp_parser_constant_initializer (cp_parser* parser)
16952 /* Look for the `=' token. */
16953 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16954 return error_mark_node;
16956 /* It is invalid to write:
16958 struct S { static const int i = { 7 }; };
16961 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16963 cp_parser_error (parser,
16964 "a brace-enclosed initializer is not allowed here");
16965 /* Consume the opening brace. */
16966 cp_lexer_consume_token (parser->lexer);
16967 /* Skip the initializer. */
16968 cp_parser_skip_to_closing_brace (parser);
16969 /* Look for the trailing `}'. */
16970 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
16972 return error_mark_node;
16975 return cp_parser_constant_expression (parser,
16976 /*allow_non_constant=*/false,
16977 NULL);
16980 /* Derived classes [gram.class.derived] */
16982 /* Parse a base-clause.
16984 base-clause:
16985 : base-specifier-list
16987 base-specifier-list:
16988 base-specifier ... [opt]
16989 base-specifier-list , base-specifier ... [opt]
16991 Returns a TREE_LIST representing the base-classes, in the order in
16992 which they were declared. The representation of each node is as
16993 described by cp_parser_base_specifier.
16995 In the case that no bases are specified, this function will return
16996 NULL_TREE, not ERROR_MARK_NODE. */
16998 static tree
16999 cp_parser_base_clause (cp_parser* parser)
17001 tree bases = NULL_TREE;
17003 /* Look for the `:' that begins the list. */
17004 cp_parser_require (parser, CPP_COLON, "%<:%>");
17006 /* Scan the base-specifier-list. */
17007 while (true)
17009 cp_token *token;
17010 tree base;
17011 bool pack_expansion_p = false;
17013 /* Look for the base-specifier. */
17014 base = cp_parser_base_specifier (parser);
17015 /* Look for the (optional) ellipsis. */
17016 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17018 /* Consume the `...'. */
17019 cp_lexer_consume_token (parser->lexer);
17021 pack_expansion_p = true;
17024 /* Add BASE to the front of the list. */
17025 if (base != error_mark_node)
17027 if (pack_expansion_p)
17028 /* Make this a pack expansion type. */
17029 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
17032 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
17034 TREE_CHAIN (base) = bases;
17035 bases = base;
17038 /* Peek at the next token. */
17039 token = cp_lexer_peek_token (parser->lexer);
17040 /* If it's not a comma, then the list is complete. */
17041 if (token->type != CPP_COMMA)
17042 break;
17043 /* Consume the `,'. */
17044 cp_lexer_consume_token (parser->lexer);
17047 /* PARSER->SCOPE may still be non-NULL at this point, if the last
17048 base class had a qualified name. However, the next name that
17049 appears is certainly not qualified. */
17050 parser->scope = NULL_TREE;
17051 parser->qualifying_scope = NULL_TREE;
17052 parser->object_scope = NULL_TREE;
17054 return nreverse (bases);
17057 /* Parse a base-specifier.
17059 base-specifier:
17060 :: [opt] nested-name-specifier [opt] class-name
17061 virtual access-specifier [opt] :: [opt] nested-name-specifier
17062 [opt] class-name
17063 access-specifier virtual [opt] :: [opt] nested-name-specifier
17064 [opt] class-name
17066 Returns a TREE_LIST. The TREE_PURPOSE will be one of
17067 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
17068 indicate the specifiers provided. The TREE_VALUE will be a TYPE
17069 (or the ERROR_MARK_NODE) indicating the type that was specified. */
17071 static tree
17072 cp_parser_base_specifier (cp_parser* parser)
17074 cp_token *token;
17075 bool done = false;
17076 bool virtual_p = false;
17077 bool duplicate_virtual_error_issued_p = false;
17078 bool duplicate_access_error_issued_p = false;
17079 bool class_scope_p, template_p;
17080 tree access = access_default_node;
17081 tree type;
17083 /* Process the optional `virtual' and `access-specifier'. */
17084 while (!done)
17086 /* Peek at the next token. */
17087 token = cp_lexer_peek_token (parser->lexer);
17088 /* Process `virtual'. */
17089 switch (token->keyword)
17091 case RID_VIRTUAL:
17092 /* If `virtual' appears more than once, issue an error. */
17093 if (virtual_p && !duplicate_virtual_error_issued_p)
17095 cp_parser_error (parser,
17096 "%<virtual%> specified more than once in base-specified");
17097 duplicate_virtual_error_issued_p = true;
17100 virtual_p = true;
17102 /* Consume the `virtual' token. */
17103 cp_lexer_consume_token (parser->lexer);
17105 break;
17107 case RID_PUBLIC:
17108 case RID_PROTECTED:
17109 case RID_PRIVATE:
17110 /* If more than one access specifier appears, issue an
17111 error. */
17112 if (access != access_default_node
17113 && !duplicate_access_error_issued_p)
17115 cp_parser_error (parser,
17116 "more than one access specifier in base-specified");
17117 duplicate_access_error_issued_p = true;
17120 access = ridpointers[(int) token->keyword];
17122 /* Consume the access-specifier. */
17123 cp_lexer_consume_token (parser->lexer);
17125 break;
17127 default:
17128 done = true;
17129 break;
17132 /* It is not uncommon to see programs mechanically, erroneously, use
17133 the 'typename' keyword to denote (dependent) qualified types
17134 as base classes. */
17135 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
17137 token = cp_lexer_peek_token (parser->lexer);
17138 if (!processing_template_decl)
17139 error_at (token->location,
17140 "keyword %<typename%> not allowed outside of templates");
17141 else
17142 error_at (token->location,
17143 "keyword %<typename%> not allowed in this context "
17144 "(the base class is implicitly a type)");
17145 cp_lexer_consume_token (parser->lexer);
17148 /* Look for the optional `::' operator. */
17149 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
17150 /* Look for the nested-name-specifier. The simplest way to
17151 implement:
17153 [temp.res]
17155 The keyword `typename' is not permitted in a base-specifier or
17156 mem-initializer; in these contexts a qualified name that
17157 depends on a template-parameter is implicitly assumed to be a
17158 type name.
17160 is to pretend that we have seen the `typename' keyword at this
17161 point. */
17162 cp_parser_nested_name_specifier_opt (parser,
17163 /*typename_keyword_p=*/true,
17164 /*check_dependency_p=*/true,
17165 typename_type,
17166 /*is_declaration=*/true);
17167 /* If the base class is given by a qualified name, assume that names
17168 we see are type names or templates, as appropriate. */
17169 class_scope_p = (parser->scope && TYPE_P (parser->scope));
17170 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
17172 /* Finally, look for the class-name. */
17173 type = cp_parser_class_name (parser,
17174 class_scope_p,
17175 template_p,
17176 typename_type,
17177 /*check_dependency_p=*/true,
17178 /*class_head_p=*/false,
17179 /*is_declaration=*/true);
17181 if (type == error_mark_node)
17182 return error_mark_node;
17184 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
17187 /* Exception handling [gram.exception] */
17189 /* Parse an (optional) exception-specification.
17191 exception-specification:
17192 throw ( type-id-list [opt] )
17194 Returns a TREE_LIST representing the exception-specification. The
17195 TREE_VALUE of each node is a type. */
17197 static tree
17198 cp_parser_exception_specification_opt (cp_parser* parser)
17200 cp_token *token;
17201 tree type_id_list;
17203 /* Peek at the next token. */
17204 token = cp_lexer_peek_token (parser->lexer);
17205 /* If it's not `throw', then there's no exception-specification. */
17206 if (!cp_parser_is_keyword (token, RID_THROW))
17207 return NULL_TREE;
17209 /* Consume the `throw'. */
17210 cp_lexer_consume_token (parser->lexer);
17212 /* Look for the `('. */
17213 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17215 /* Peek at the next token. */
17216 token = cp_lexer_peek_token (parser->lexer);
17217 /* If it's not a `)', then there is a type-id-list. */
17218 if (token->type != CPP_CLOSE_PAREN)
17220 const char *saved_message;
17222 /* Types may not be defined in an exception-specification. */
17223 saved_message = parser->type_definition_forbidden_message;
17224 parser->type_definition_forbidden_message
17225 = G_("types may not be defined in an exception-specification");
17226 /* Parse the type-id-list. */
17227 type_id_list = cp_parser_type_id_list (parser);
17228 /* Restore the saved message. */
17229 parser->type_definition_forbidden_message = saved_message;
17231 else
17232 type_id_list = empty_except_spec;
17234 /* Look for the `)'. */
17235 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17237 return type_id_list;
17240 /* Parse an (optional) type-id-list.
17242 type-id-list:
17243 type-id ... [opt]
17244 type-id-list , type-id ... [opt]
17246 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
17247 in the order that the types were presented. */
17249 static tree
17250 cp_parser_type_id_list (cp_parser* parser)
17252 tree types = NULL_TREE;
17254 while (true)
17256 cp_token *token;
17257 tree type;
17259 /* Get the next type-id. */
17260 type = cp_parser_type_id (parser);
17261 /* Parse the optional ellipsis. */
17262 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17264 /* Consume the `...'. */
17265 cp_lexer_consume_token (parser->lexer);
17267 /* Turn the type into a pack expansion expression. */
17268 type = make_pack_expansion (type);
17270 /* Add it to the list. */
17271 types = add_exception_specifier (types, type, /*complain=*/1);
17272 /* Peek at the next token. */
17273 token = cp_lexer_peek_token (parser->lexer);
17274 /* If it is not a `,', we are done. */
17275 if (token->type != CPP_COMMA)
17276 break;
17277 /* Consume the `,'. */
17278 cp_lexer_consume_token (parser->lexer);
17281 return nreverse (types);
17284 /* Parse a try-block.
17286 try-block:
17287 try compound-statement handler-seq */
17289 static tree
17290 cp_parser_try_block (cp_parser* parser)
17292 tree try_block;
17294 cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
17295 try_block = begin_try_block ();
17296 cp_parser_compound_statement (parser, NULL, true);
17297 finish_try_block (try_block);
17298 cp_parser_handler_seq (parser);
17299 finish_handler_sequence (try_block);
17301 return try_block;
17304 /* Parse a function-try-block.
17306 function-try-block:
17307 try ctor-initializer [opt] function-body handler-seq */
17309 static bool
17310 cp_parser_function_try_block (cp_parser* parser)
17312 tree compound_stmt;
17313 tree try_block;
17314 bool ctor_initializer_p;
17316 /* Look for the `try' keyword. */
17317 if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
17318 return false;
17319 /* Let the rest of the front end know where we are. */
17320 try_block = begin_function_try_block (&compound_stmt);
17321 /* Parse the function-body. */
17322 ctor_initializer_p
17323 = cp_parser_ctor_initializer_opt_and_function_body (parser);
17324 /* We're done with the `try' part. */
17325 finish_function_try_block (try_block);
17326 /* Parse the handlers. */
17327 cp_parser_handler_seq (parser);
17328 /* We're done with the handlers. */
17329 finish_function_handler_sequence (try_block, compound_stmt);
17331 return ctor_initializer_p;
17334 /* Parse a handler-seq.
17336 handler-seq:
17337 handler handler-seq [opt] */
17339 static void
17340 cp_parser_handler_seq (cp_parser* parser)
17342 while (true)
17344 cp_token *token;
17346 /* Parse the handler. */
17347 cp_parser_handler (parser);
17348 /* Peek at the next token. */
17349 token = cp_lexer_peek_token (parser->lexer);
17350 /* If it's not `catch' then there are no more handlers. */
17351 if (!cp_parser_is_keyword (token, RID_CATCH))
17352 break;
17356 /* Parse a handler.
17358 handler:
17359 catch ( exception-declaration ) compound-statement */
17361 static void
17362 cp_parser_handler (cp_parser* parser)
17364 tree handler;
17365 tree declaration;
17367 cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
17368 handler = begin_handler ();
17369 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17370 declaration = cp_parser_exception_declaration (parser);
17371 finish_handler_parms (declaration, handler);
17372 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17373 cp_parser_compound_statement (parser, NULL, false);
17374 finish_handler (handler);
17377 /* Parse an exception-declaration.
17379 exception-declaration:
17380 type-specifier-seq declarator
17381 type-specifier-seq abstract-declarator
17382 type-specifier-seq
17385 Returns a VAR_DECL for the declaration, or NULL_TREE if the
17386 ellipsis variant is used. */
17388 static tree
17389 cp_parser_exception_declaration (cp_parser* parser)
17391 cp_decl_specifier_seq type_specifiers;
17392 cp_declarator *declarator;
17393 const char *saved_message;
17395 /* If it's an ellipsis, it's easy to handle. */
17396 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17398 /* Consume the `...' token. */
17399 cp_lexer_consume_token (parser->lexer);
17400 return NULL_TREE;
17403 /* Types may not be defined in exception-declarations. */
17404 saved_message = parser->type_definition_forbidden_message;
17405 parser->type_definition_forbidden_message
17406 = G_("types may not be defined in exception-declarations");
17408 /* Parse the type-specifier-seq. */
17409 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
17410 /*is_trailing_return=*/false,
17411 &type_specifiers);
17412 /* If it's a `)', then there is no declarator. */
17413 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
17414 declarator = NULL;
17415 else
17416 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
17417 /*ctor_dtor_or_conv_p=*/NULL,
17418 /*parenthesized_p=*/NULL,
17419 /*member_p=*/false);
17421 /* Restore the saved message. */
17422 parser->type_definition_forbidden_message = saved_message;
17424 if (!type_specifiers.any_specifiers_p)
17425 return error_mark_node;
17427 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
17430 /* Parse a throw-expression.
17432 throw-expression:
17433 throw assignment-expression [opt]
17435 Returns a THROW_EXPR representing the throw-expression. */
17437 static tree
17438 cp_parser_throw_expression (cp_parser* parser)
17440 tree expression;
17441 cp_token* token;
17443 cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
17444 token = cp_lexer_peek_token (parser->lexer);
17445 /* Figure out whether or not there is an assignment-expression
17446 following the "throw" keyword. */
17447 if (token->type == CPP_COMMA
17448 || token->type == CPP_SEMICOLON
17449 || token->type == CPP_CLOSE_PAREN
17450 || token->type == CPP_CLOSE_SQUARE
17451 || token->type == CPP_CLOSE_BRACE
17452 || token->type == CPP_COLON)
17453 expression = NULL_TREE;
17454 else
17455 expression = cp_parser_assignment_expression (parser,
17456 /*cast_p=*/false, NULL);
17458 return build_throw (expression);
17461 /* GNU Extensions */
17463 /* Parse an (optional) asm-specification.
17465 asm-specification:
17466 asm ( string-literal )
17468 If the asm-specification is present, returns a STRING_CST
17469 corresponding to the string-literal. Otherwise, returns
17470 NULL_TREE. */
17472 static tree
17473 cp_parser_asm_specification_opt (cp_parser* parser)
17475 cp_token *token;
17476 tree asm_specification;
17478 /* Peek at the next token. */
17479 token = cp_lexer_peek_token (parser->lexer);
17480 /* If the next token isn't the `asm' keyword, then there's no
17481 asm-specification. */
17482 if (!cp_parser_is_keyword (token, RID_ASM))
17483 return NULL_TREE;
17485 /* Consume the `asm' token. */
17486 cp_lexer_consume_token (parser->lexer);
17487 /* Look for the `('. */
17488 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17490 /* Look for the string-literal. */
17491 asm_specification = cp_parser_string_literal (parser, false, false);
17493 /* Look for the `)'. */
17494 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17496 return asm_specification;
17499 /* Parse an asm-operand-list.
17501 asm-operand-list:
17502 asm-operand
17503 asm-operand-list , asm-operand
17505 asm-operand:
17506 string-literal ( expression )
17507 [ string-literal ] string-literal ( expression )
17509 Returns a TREE_LIST representing the operands. The TREE_VALUE of
17510 each node is the expression. The TREE_PURPOSE is itself a
17511 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
17512 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
17513 is a STRING_CST for the string literal before the parenthesis. Returns
17514 ERROR_MARK_NODE if any of the operands are invalid. */
17516 static tree
17517 cp_parser_asm_operand_list (cp_parser* parser)
17519 tree asm_operands = NULL_TREE;
17520 bool invalid_operands = false;
17522 while (true)
17524 tree string_literal;
17525 tree expression;
17526 tree name;
17528 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
17530 /* Consume the `[' token. */
17531 cp_lexer_consume_token (parser->lexer);
17532 /* Read the operand name. */
17533 name = cp_parser_identifier (parser);
17534 if (name != error_mark_node)
17535 name = build_string (IDENTIFIER_LENGTH (name),
17536 IDENTIFIER_POINTER (name));
17537 /* Look for the closing `]'. */
17538 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
17540 else
17541 name = NULL_TREE;
17542 /* Look for the string-literal. */
17543 string_literal = cp_parser_string_literal (parser, false, false);
17545 /* Look for the `('. */
17546 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17547 /* Parse the expression. */
17548 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
17549 /* Look for the `)'. */
17550 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17552 if (name == error_mark_node
17553 || string_literal == error_mark_node
17554 || expression == error_mark_node)
17555 invalid_operands = true;
17557 /* Add this operand to the list. */
17558 asm_operands = tree_cons (build_tree_list (name, string_literal),
17559 expression,
17560 asm_operands);
17561 /* If the next token is not a `,', there are no more
17562 operands. */
17563 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17564 break;
17565 /* Consume the `,'. */
17566 cp_lexer_consume_token (parser->lexer);
17569 return invalid_operands ? error_mark_node : nreverse (asm_operands);
17572 /* Parse an asm-clobber-list.
17574 asm-clobber-list:
17575 string-literal
17576 asm-clobber-list , string-literal
17578 Returns a TREE_LIST, indicating the clobbers in the order that they
17579 appeared. The TREE_VALUE of each node is a STRING_CST. */
17581 static tree
17582 cp_parser_asm_clobber_list (cp_parser* parser)
17584 tree clobbers = NULL_TREE;
17586 while (true)
17588 tree string_literal;
17590 /* Look for the string literal. */
17591 string_literal = cp_parser_string_literal (parser, false, false);
17592 /* Add it to the list. */
17593 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
17594 /* If the next token is not a `,', then the list is
17595 complete. */
17596 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17597 break;
17598 /* Consume the `,' token. */
17599 cp_lexer_consume_token (parser->lexer);
17602 return clobbers;
17605 /* Parse an asm-label-list.
17607 asm-label-list:
17608 identifier
17609 asm-label-list , identifier
17611 Returns a TREE_LIST, indicating the labels in the order that they
17612 appeared. The TREE_VALUE of each node is a label. */
17614 static tree
17615 cp_parser_asm_label_list (cp_parser* parser)
17617 tree labels = NULL_TREE;
17619 while (true)
17621 tree identifier, label, name;
17623 /* Look for the identifier. */
17624 identifier = cp_parser_identifier (parser);
17625 if (!error_operand_p (identifier))
17627 label = lookup_label (identifier);
17628 if (TREE_CODE (label) == LABEL_DECL)
17630 TREE_USED (label) = 1;
17631 check_goto (label);
17632 name = build_string (IDENTIFIER_LENGTH (identifier),
17633 IDENTIFIER_POINTER (identifier));
17634 labels = tree_cons (name, label, labels);
17637 /* If the next token is not a `,', then the list is
17638 complete. */
17639 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17640 break;
17641 /* Consume the `,' token. */
17642 cp_lexer_consume_token (parser->lexer);
17645 return nreverse (labels);
17648 /* Parse an (optional) series of attributes.
17650 attributes:
17651 attributes attribute
17653 attribute:
17654 __attribute__ (( attribute-list [opt] ))
17656 The return value is as for cp_parser_attribute_list. */
17658 static tree
17659 cp_parser_attributes_opt (cp_parser* parser)
17661 tree attributes = NULL_TREE;
17663 while (true)
17665 cp_token *token;
17666 tree attribute_list;
17668 /* Peek at the next token. */
17669 token = cp_lexer_peek_token (parser->lexer);
17670 /* If it's not `__attribute__', then we're done. */
17671 if (token->keyword != RID_ATTRIBUTE)
17672 break;
17674 /* Consume the `__attribute__' keyword. */
17675 cp_lexer_consume_token (parser->lexer);
17676 /* Look for the two `(' tokens. */
17677 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17678 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17680 /* Peek at the next token. */
17681 token = cp_lexer_peek_token (parser->lexer);
17682 if (token->type != CPP_CLOSE_PAREN)
17683 /* Parse the attribute-list. */
17684 attribute_list = cp_parser_attribute_list (parser);
17685 else
17686 /* If the next token is a `)', then there is no attribute
17687 list. */
17688 attribute_list = NULL;
17690 /* Look for the two `)' tokens. */
17691 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17692 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17694 /* Add these new attributes to the list. */
17695 attributes = chainon (attributes, attribute_list);
17698 return attributes;
17701 /* Parse an attribute-list.
17703 attribute-list:
17704 attribute
17705 attribute-list , attribute
17707 attribute:
17708 identifier
17709 identifier ( identifier )
17710 identifier ( identifier , expression-list )
17711 identifier ( expression-list )
17713 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
17714 to an attribute. The TREE_PURPOSE of each node is the identifier
17715 indicating which attribute is in use. The TREE_VALUE represents
17716 the arguments, if any. */
17718 static tree
17719 cp_parser_attribute_list (cp_parser* parser)
17721 tree attribute_list = NULL_TREE;
17722 bool save_translate_strings_p = parser->translate_strings_p;
17724 parser->translate_strings_p = false;
17725 while (true)
17727 cp_token *token;
17728 tree identifier;
17729 tree attribute;
17731 /* Look for the identifier. We also allow keywords here; for
17732 example `__attribute__ ((const))' is legal. */
17733 token = cp_lexer_peek_token (parser->lexer);
17734 if (token->type == CPP_NAME
17735 || token->type == CPP_KEYWORD)
17737 tree arguments = NULL_TREE;
17739 /* Consume the token. */
17740 token = cp_lexer_consume_token (parser->lexer);
17742 /* Save away the identifier that indicates which attribute
17743 this is. */
17744 identifier = (token->type == CPP_KEYWORD)
17745 /* For keywords, use the canonical spelling, not the
17746 parsed identifier. */
17747 ? ridpointers[(int) token->keyword]
17748 : token->u.value;
17750 attribute = build_tree_list (identifier, NULL_TREE);
17752 /* Peek at the next token. */
17753 token = cp_lexer_peek_token (parser->lexer);
17754 /* If it's an `(', then parse the attribute arguments. */
17755 if (token->type == CPP_OPEN_PAREN)
17757 VEC(tree,gc) *vec;
17758 vec = cp_parser_parenthesized_expression_list
17759 (parser, true, /*cast_p=*/false,
17760 /*allow_expansion_p=*/false,
17761 /*non_constant_p=*/NULL);
17762 if (vec == NULL)
17763 arguments = error_mark_node;
17764 else
17766 arguments = build_tree_list_vec (vec);
17767 release_tree_vector (vec);
17769 /* Save the arguments away. */
17770 TREE_VALUE (attribute) = arguments;
17773 if (arguments != error_mark_node)
17775 /* Add this attribute to the list. */
17776 TREE_CHAIN (attribute) = attribute_list;
17777 attribute_list = attribute;
17780 token = cp_lexer_peek_token (parser->lexer);
17782 /* Now, look for more attributes. If the next token isn't a
17783 `,', we're done. */
17784 if (token->type != CPP_COMMA)
17785 break;
17787 /* Consume the comma and keep going. */
17788 cp_lexer_consume_token (parser->lexer);
17790 parser->translate_strings_p = save_translate_strings_p;
17792 /* We built up the list in reverse order. */
17793 return nreverse (attribute_list);
17796 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
17797 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
17798 current value of the PEDANTIC flag, regardless of whether or not
17799 the `__extension__' keyword is present. The caller is responsible
17800 for restoring the value of the PEDANTIC flag. */
17802 static bool
17803 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
17805 /* Save the old value of the PEDANTIC flag. */
17806 *saved_pedantic = pedantic;
17808 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
17810 /* Consume the `__extension__' token. */
17811 cp_lexer_consume_token (parser->lexer);
17812 /* We're not being pedantic while the `__extension__' keyword is
17813 in effect. */
17814 pedantic = 0;
17816 return true;
17819 return false;
17822 /* Parse a label declaration.
17824 label-declaration:
17825 __label__ label-declarator-seq ;
17827 label-declarator-seq:
17828 identifier , label-declarator-seq
17829 identifier */
17831 static void
17832 cp_parser_label_declaration (cp_parser* parser)
17834 /* Look for the `__label__' keyword. */
17835 cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
17837 while (true)
17839 tree identifier;
17841 /* Look for an identifier. */
17842 identifier = cp_parser_identifier (parser);
17843 /* If we failed, stop. */
17844 if (identifier == error_mark_node)
17845 break;
17846 /* Declare it as a label. */
17847 finish_label_decl (identifier);
17848 /* If the next token is a `;', stop. */
17849 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17850 break;
17851 /* Look for the `,' separating the label declarations. */
17852 cp_parser_require (parser, CPP_COMMA, "%<,%>");
17855 /* Look for the final `;'. */
17856 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
17859 /* Support Functions */
17861 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
17862 NAME should have one of the representations used for an
17863 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
17864 is returned. If PARSER->SCOPE is a dependent type, then a
17865 SCOPE_REF is returned.
17867 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
17868 returned; the name was already resolved when the TEMPLATE_ID_EXPR
17869 was formed. Abstractly, such entities should not be passed to this
17870 function, because they do not need to be looked up, but it is
17871 simpler to check for this special case here, rather than at the
17872 call-sites.
17874 In cases not explicitly covered above, this function returns a
17875 DECL, OVERLOAD, or baselink representing the result of the lookup.
17876 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
17877 is returned.
17879 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
17880 (e.g., "struct") that was used. In that case bindings that do not
17881 refer to types are ignored.
17883 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
17884 ignored.
17886 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
17887 are ignored.
17889 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
17890 types.
17892 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
17893 TREE_LIST of candidates if name-lookup results in an ambiguity, and
17894 NULL_TREE otherwise. */
17896 static tree
17897 cp_parser_lookup_name (cp_parser *parser, tree name,
17898 enum tag_types tag_type,
17899 bool is_template,
17900 bool is_namespace,
17901 bool check_dependency,
17902 tree *ambiguous_decls,
17903 location_t name_location)
17905 int flags = 0;
17906 tree decl;
17907 tree object_type = parser->context->object_type;
17909 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17910 flags |= LOOKUP_COMPLAIN;
17912 /* Assume that the lookup will be unambiguous. */
17913 if (ambiguous_decls)
17914 *ambiguous_decls = NULL_TREE;
17916 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
17917 no longer valid. Note that if we are parsing tentatively, and
17918 the parse fails, OBJECT_TYPE will be automatically restored. */
17919 parser->context->object_type = NULL_TREE;
17921 if (name == error_mark_node)
17922 return error_mark_node;
17924 /* A template-id has already been resolved; there is no lookup to
17925 do. */
17926 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
17927 return name;
17928 if (BASELINK_P (name))
17930 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
17931 == TEMPLATE_ID_EXPR);
17932 return name;
17935 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
17936 it should already have been checked to make sure that the name
17937 used matches the type being destroyed. */
17938 if (TREE_CODE (name) == BIT_NOT_EXPR)
17940 tree type;
17942 /* Figure out to which type this destructor applies. */
17943 if (parser->scope)
17944 type = parser->scope;
17945 else if (object_type)
17946 type = object_type;
17947 else
17948 type = current_class_type;
17949 /* If that's not a class type, there is no destructor. */
17950 if (!type || !CLASS_TYPE_P (type))
17951 return error_mark_node;
17952 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
17953 lazily_declare_fn (sfk_destructor, type);
17954 if (!CLASSTYPE_DESTRUCTORS (type))
17955 return error_mark_node;
17956 /* If it was a class type, return the destructor. */
17957 return CLASSTYPE_DESTRUCTORS (type);
17960 /* By this point, the NAME should be an ordinary identifier. If
17961 the id-expression was a qualified name, the qualifying scope is
17962 stored in PARSER->SCOPE at this point. */
17963 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
17965 /* Perform the lookup. */
17966 if (parser->scope)
17968 bool dependent_p;
17970 if (parser->scope == error_mark_node)
17971 return error_mark_node;
17973 /* If the SCOPE is dependent, the lookup must be deferred until
17974 the template is instantiated -- unless we are explicitly
17975 looking up names in uninstantiated templates. Even then, we
17976 cannot look up the name if the scope is not a class type; it
17977 might, for example, be a template type parameter. */
17978 dependent_p = (TYPE_P (parser->scope)
17979 && dependent_scope_p (parser->scope));
17980 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
17981 && dependent_p)
17982 /* Defer lookup. */
17983 decl = error_mark_node;
17984 else
17986 tree pushed_scope = NULL_TREE;
17988 /* If PARSER->SCOPE is a dependent type, then it must be a
17989 class type, and we must not be checking dependencies;
17990 otherwise, we would have processed this lookup above. So
17991 that PARSER->SCOPE is not considered a dependent base by
17992 lookup_member, we must enter the scope here. */
17993 if (dependent_p)
17994 pushed_scope = push_scope (parser->scope);
17996 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
17997 lookup result and the nested-name-specifier nominates a class C:
17998 * if the name specified after the nested-name-specifier, when
17999 looked up in C, is the injected-class-name of C (Clause 9), or
18000 * if the name specified after the nested-name-specifier is the
18001 same as the identifier or the simple-template-id's template-
18002 name in the last component of the nested-name-specifier,
18003 the name is instead considered to name the constructor of
18004 class C. [ Note: for example, the constructor is not an
18005 acceptable lookup result in an elaborated-type-specifier so
18006 the constructor would not be used in place of the
18007 injected-class-name. --end note ] Such a constructor name
18008 shall be used only in the declarator-id of a declaration that
18009 names a constructor or in a using-declaration. */
18010 if (tag_type == none_type
18011 && CLASS_TYPE_P (parser->scope)
18012 && constructor_name_p (name, parser->scope))
18013 name = ctor_identifier;
18015 /* If the PARSER->SCOPE is a template specialization, it
18016 may be instantiated during name lookup. In that case,
18017 errors may be issued. Even if we rollback the current
18018 tentative parse, those errors are valid. */
18019 decl = lookup_qualified_name (parser->scope, name,
18020 tag_type != none_type,
18021 /*complain=*/true);
18023 /* If we have a single function from a using decl, pull it out. */
18024 if (TREE_CODE (decl) == OVERLOAD
18025 && !really_overloaded_fn (decl))
18026 decl = OVL_FUNCTION (decl);
18028 if (pushed_scope)
18029 pop_scope (pushed_scope);
18032 /* If the scope is a dependent type and either we deferred lookup or
18033 we did lookup but didn't find the name, rememeber the name. */
18034 if (decl == error_mark_node && TYPE_P (parser->scope)
18035 && dependent_type_p (parser->scope))
18037 if (tag_type)
18039 tree type;
18041 /* The resolution to Core Issue 180 says that `struct
18042 A::B' should be considered a type-name, even if `A'
18043 is dependent. */
18044 type = make_typename_type (parser->scope, name, tag_type,
18045 /*complain=*/tf_error);
18046 decl = TYPE_NAME (type);
18048 else if (is_template
18049 && (cp_parser_next_token_ends_template_argument_p (parser)
18050 || cp_lexer_next_token_is (parser->lexer,
18051 CPP_CLOSE_PAREN)))
18052 decl = make_unbound_class_template (parser->scope,
18053 name, NULL_TREE,
18054 /*complain=*/tf_error);
18055 else
18056 decl = build_qualified_name (/*type=*/NULL_TREE,
18057 parser->scope, name,
18058 is_template);
18060 parser->qualifying_scope = parser->scope;
18061 parser->object_scope = NULL_TREE;
18063 else if (object_type)
18065 tree object_decl = NULL_TREE;
18066 /* Look up the name in the scope of the OBJECT_TYPE, unless the
18067 OBJECT_TYPE is not a class. */
18068 if (CLASS_TYPE_P (object_type))
18069 /* If the OBJECT_TYPE is a template specialization, it may
18070 be instantiated during name lookup. In that case, errors
18071 may be issued. Even if we rollback the current tentative
18072 parse, those errors are valid. */
18073 object_decl = lookup_member (object_type,
18074 name,
18075 /*protect=*/0,
18076 tag_type != none_type);
18077 /* Look it up in the enclosing context, too. */
18078 decl = lookup_name_real (name, tag_type != none_type,
18079 /*nonclass=*/0,
18080 /*block_p=*/true, is_namespace, flags);
18081 parser->object_scope = object_type;
18082 parser->qualifying_scope = NULL_TREE;
18083 if (object_decl)
18084 decl = object_decl;
18086 else
18088 decl = lookup_name_real (name, tag_type != none_type,
18089 /*nonclass=*/0,
18090 /*block_p=*/true, is_namespace, flags);
18091 parser->qualifying_scope = NULL_TREE;
18092 parser->object_scope = NULL_TREE;
18095 /* If the lookup failed, let our caller know. */
18096 if (!decl || decl == error_mark_node)
18097 return error_mark_node;
18099 /* Pull out the template from an injected-class-name (or multiple). */
18100 if (is_template)
18101 decl = maybe_get_template_decl_from_type_decl (decl);
18103 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
18104 if (TREE_CODE (decl) == TREE_LIST)
18106 if (ambiguous_decls)
18107 *ambiguous_decls = decl;
18108 /* The error message we have to print is too complicated for
18109 cp_parser_error, so we incorporate its actions directly. */
18110 if (!cp_parser_simulate_error (parser))
18112 error_at (name_location, "reference to %qD is ambiguous",
18113 name);
18114 print_candidates (decl);
18116 return error_mark_node;
18119 gcc_assert (DECL_P (decl)
18120 || TREE_CODE (decl) == OVERLOAD
18121 || TREE_CODE (decl) == SCOPE_REF
18122 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
18123 || BASELINK_P (decl));
18125 /* If we have resolved the name of a member declaration, check to
18126 see if the declaration is accessible. When the name resolves to
18127 set of overloaded functions, accessibility is checked when
18128 overload resolution is done.
18130 During an explicit instantiation, access is not checked at all,
18131 as per [temp.explicit]. */
18132 if (DECL_P (decl))
18133 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
18135 return decl;
18138 /* Like cp_parser_lookup_name, but for use in the typical case where
18139 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
18140 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
18142 static tree
18143 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
18145 return cp_parser_lookup_name (parser, name,
18146 none_type,
18147 /*is_template=*/false,
18148 /*is_namespace=*/false,
18149 /*check_dependency=*/true,
18150 /*ambiguous_decls=*/NULL,
18151 location);
18154 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
18155 the current context, return the TYPE_DECL. If TAG_NAME_P is
18156 true, the DECL indicates the class being defined in a class-head,
18157 or declared in an elaborated-type-specifier.
18159 Otherwise, return DECL. */
18161 static tree
18162 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
18164 /* If the TEMPLATE_DECL is being declared as part of a class-head,
18165 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
18167 struct A {
18168 template <typename T> struct B;
18171 template <typename T> struct A::B {};
18173 Similarly, in an elaborated-type-specifier:
18175 namespace N { struct X{}; }
18177 struct A {
18178 template <typename T> friend struct N::X;
18181 However, if the DECL refers to a class type, and we are in
18182 the scope of the class, then the name lookup automatically
18183 finds the TYPE_DECL created by build_self_reference rather
18184 than a TEMPLATE_DECL. For example, in:
18186 template <class T> struct S {
18187 S s;
18190 there is no need to handle such case. */
18192 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
18193 return DECL_TEMPLATE_RESULT (decl);
18195 return decl;
18198 /* If too many, or too few, template-parameter lists apply to the
18199 declarator, issue an error message. Returns TRUE if all went well,
18200 and FALSE otherwise. */
18202 static bool
18203 cp_parser_check_declarator_template_parameters (cp_parser* parser,
18204 cp_declarator *declarator,
18205 location_t declarator_location)
18207 unsigned num_templates;
18209 /* We haven't seen any classes that involve template parameters yet. */
18210 num_templates = 0;
18212 switch (declarator->kind)
18214 case cdk_id:
18215 if (declarator->u.id.qualifying_scope)
18217 tree scope;
18219 scope = declarator->u.id.qualifying_scope;
18221 while (scope && CLASS_TYPE_P (scope))
18223 /* You're supposed to have one `template <...>'
18224 for every template class, but you don't need one
18225 for a full specialization. For example:
18227 template <class T> struct S{};
18228 template <> struct S<int> { void f(); };
18229 void S<int>::f () {}
18231 is correct; there shouldn't be a `template <>' for
18232 the definition of `S<int>::f'. */
18233 if (!CLASSTYPE_TEMPLATE_INFO (scope))
18234 /* If SCOPE does not have template information of any
18235 kind, then it is not a template, nor is it nested
18236 within a template. */
18237 break;
18238 if (explicit_class_specialization_p (scope))
18239 break;
18240 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
18241 ++num_templates;
18243 scope = TYPE_CONTEXT (scope);
18246 else if (TREE_CODE (declarator->u.id.unqualified_name)
18247 == TEMPLATE_ID_EXPR)
18248 /* If the DECLARATOR has the form `X<y>' then it uses one
18249 additional level of template parameters. */
18250 ++num_templates;
18252 return cp_parser_check_template_parameters
18253 (parser, num_templates, declarator_location, declarator);
18256 case cdk_function:
18257 case cdk_array:
18258 case cdk_pointer:
18259 case cdk_reference:
18260 case cdk_ptrmem:
18261 return (cp_parser_check_declarator_template_parameters
18262 (parser, declarator->declarator, declarator_location));
18264 case cdk_error:
18265 return true;
18267 default:
18268 gcc_unreachable ();
18270 return false;
18273 /* NUM_TEMPLATES were used in the current declaration. If that is
18274 invalid, return FALSE and issue an error messages. Otherwise,
18275 return TRUE. If DECLARATOR is non-NULL, then we are checking a
18276 declarator and we can print more accurate diagnostics. */
18278 static bool
18279 cp_parser_check_template_parameters (cp_parser* parser,
18280 unsigned num_templates,
18281 location_t location,
18282 cp_declarator *declarator)
18284 /* If there are the same number of template classes and parameter
18285 lists, that's OK. */
18286 if (parser->num_template_parameter_lists == num_templates)
18287 return true;
18288 /* If there are more, but only one more, then we are referring to a
18289 member template. That's OK too. */
18290 if (parser->num_template_parameter_lists == num_templates + 1)
18291 return true;
18292 /* If there are more template classes than parameter lists, we have
18293 something like:
18295 template <class T> void S<T>::R<T>::f (); */
18296 if (parser->num_template_parameter_lists < num_templates)
18298 if (declarator && !current_function_decl)
18299 error_at (location, "specializing member %<%T::%E%> "
18300 "requires %<template<>%> syntax",
18301 declarator->u.id.qualifying_scope,
18302 declarator->u.id.unqualified_name);
18303 else if (declarator)
18304 error_at (location, "invalid declaration of %<%T::%E%>",
18305 declarator->u.id.qualifying_scope,
18306 declarator->u.id.unqualified_name);
18307 else
18308 error_at (location, "too few template-parameter-lists");
18309 return false;
18311 /* Otherwise, there are too many template parameter lists. We have
18312 something like:
18314 template <class T> template <class U> void S::f(); */
18315 error_at (location, "too many template-parameter-lists");
18316 return false;
18319 /* Parse an optional `::' token indicating that the following name is
18320 from the global namespace. If so, PARSER->SCOPE is set to the
18321 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
18322 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
18323 Returns the new value of PARSER->SCOPE, if the `::' token is
18324 present, and NULL_TREE otherwise. */
18326 static tree
18327 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
18329 cp_token *token;
18331 /* Peek at the next token. */
18332 token = cp_lexer_peek_token (parser->lexer);
18333 /* If we're looking at a `::' token then we're starting from the
18334 global namespace, not our current location. */
18335 if (token->type == CPP_SCOPE)
18337 /* Consume the `::' token. */
18338 cp_lexer_consume_token (parser->lexer);
18339 /* Set the SCOPE so that we know where to start the lookup. */
18340 parser->scope = global_namespace;
18341 parser->qualifying_scope = global_namespace;
18342 parser->object_scope = NULL_TREE;
18344 return parser->scope;
18346 else if (!current_scope_valid_p)
18348 parser->scope = NULL_TREE;
18349 parser->qualifying_scope = NULL_TREE;
18350 parser->object_scope = NULL_TREE;
18353 return NULL_TREE;
18356 /* Returns TRUE if the upcoming token sequence is the start of a
18357 constructor declarator. If FRIEND_P is true, the declarator is
18358 preceded by the `friend' specifier. */
18360 static bool
18361 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
18363 bool constructor_p;
18364 tree nested_name_specifier;
18365 cp_token *next_token;
18367 /* The common case is that this is not a constructor declarator, so
18368 try to avoid doing lots of work if at all possible. It's not
18369 valid declare a constructor at function scope. */
18370 if (parser->in_function_body)
18371 return false;
18372 /* And only certain tokens can begin a constructor declarator. */
18373 next_token = cp_lexer_peek_token (parser->lexer);
18374 if (next_token->type != CPP_NAME
18375 && next_token->type != CPP_SCOPE
18376 && next_token->type != CPP_NESTED_NAME_SPECIFIER
18377 && next_token->type != CPP_TEMPLATE_ID)
18378 return false;
18380 /* Parse tentatively; we are going to roll back all of the tokens
18381 consumed here. */
18382 cp_parser_parse_tentatively (parser);
18383 /* Assume that we are looking at a constructor declarator. */
18384 constructor_p = true;
18386 /* Look for the optional `::' operator. */
18387 cp_parser_global_scope_opt (parser,
18388 /*current_scope_valid_p=*/false);
18389 /* Look for the nested-name-specifier. */
18390 nested_name_specifier
18391 = (cp_parser_nested_name_specifier_opt (parser,
18392 /*typename_keyword_p=*/false,
18393 /*check_dependency_p=*/false,
18394 /*type_p=*/false,
18395 /*is_declaration=*/false));
18396 /* Outside of a class-specifier, there must be a
18397 nested-name-specifier. */
18398 if (!nested_name_specifier &&
18399 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
18400 || friend_p))
18401 constructor_p = false;
18402 else if (nested_name_specifier == error_mark_node)
18403 constructor_p = false;
18405 /* If we have a class scope, this is easy; DR 147 says that S::S always
18406 names the constructor, and no other qualified name could. */
18407 if (constructor_p && nested_name_specifier
18408 && TYPE_P (nested_name_specifier))
18410 tree id = cp_parser_unqualified_id (parser,
18411 /*template_keyword_p=*/false,
18412 /*check_dependency_p=*/false,
18413 /*declarator_p=*/true,
18414 /*optional_p=*/false);
18415 if (is_overloaded_fn (id))
18416 id = DECL_NAME (get_first_fn (id));
18417 if (!constructor_name_p (id, nested_name_specifier))
18418 constructor_p = false;
18420 /* If we still think that this might be a constructor-declarator,
18421 look for a class-name. */
18422 else if (constructor_p)
18424 /* If we have:
18426 template <typename T> struct S {
18427 S();
18430 we must recognize that the nested `S' names a class. */
18431 tree type_decl;
18432 type_decl = cp_parser_class_name (parser,
18433 /*typename_keyword_p=*/false,
18434 /*template_keyword_p=*/false,
18435 none_type,
18436 /*check_dependency_p=*/false,
18437 /*class_head_p=*/false,
18438 /*is_declaration=*/false);
18439 /* If there was no class-name, then this is not a constructor. */
18440 constructor_p = !cp_parser_error_occurred (parser);
18442 /* If we're still considering a constructor, we have to see a `(',
18443 to begin the parameter-declaration-clause, followed by either a
18444 `)', an `...', or a decl-specifier. We need to check for a
18445 type-specifier to avoid being fooled into thinking that:
18447 S (f) (int);
18449 is a constructor. (It is actually a function named `f' that
18450 takes one parameter (of type `int') and returns a value of type
18451 `S'. */
18452 if (constructor_p
18453 && !cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
18454 constructor_p = false;
18456 if (constructor_p
18457 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
18458 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
18459 /* A parameter declaration begins with a decl-specifier,
18460 which is either the "attribute" keyword, a storage class
18461 specifier, or (usually) a type-specifier. */
18462 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
18464 tree type;
18465 tree pushed_scope = NULL_TREE;
18466 unsigned saved_num_template_parameter_lists;
18468 /* Names appearing in the type-specifier should be looked up
18469 in the scope of the class. */
18470 if (current_class_type)
18471 type = NULL_TREE;
18472 else
18474 type = TREE_TYPE (type_decl);
18475 if (TREE_CODE (type) == TYPENAME_TYPE)
18477 type = resolve_typename_type (type,
18478 /*only_current_p=*/false);
18479 if (TREE_CODE (type) == TYPENAME_TYPE)
18481 cp_parser_abort_tentative_parse (parser);
18482 return false;
18485 pushed_scope = push_scope (type);
18488 /* Inside the constructor parameter list, surrounding
18489 template-parameter-lists do not apply. */
18490 saved_num_template_parameter_lists
18491 = parser->num_template_parameter_lists;
18492 parser->num_template_parameter_lists = 0;
18494 /* Look for the type-specifier. */
18495 cp_parser_type_specifier (parser,
18496 CP_PARSER_FLAGS_NONE,
18497 /*decl_specs=*/NULL,
18498 /*is_declarator=*/true,
18499 /*declares_class_or_enum=*/NULL,
18500 /*is_cv_qualifier=*/NULL);
18502 parser->num_template_parameter_lists
18503 = saved_num_template_parameter_lists;
18505 /* Leave the scope of the class. */
18506 if (pushed_scope)
18507 pop_scope (pushed_scope);
18509 constructor_p = !cp_parser_error_occurred (parser);
18513 /* We did not really want to consume any tokens. */
18514 cp_parser_abort_tentative_parse (parser);
18516 return constructor_p;
18519 /* Parse the definition of the function given by the DECL_SPECIFIERS,
18520 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
18521 they must be performed once we are in the scope of the function.
18523 Returns the function defined. */
18525 static tree
18526 cp_parser_function_definition_from_specifiers_and_declarator
18527 (cp_parser* parser,
18528 cp_decl_specifier_seq *decl_specifiers,
18529 tree attributes,
18530 const cp_declarator *declarator)
18532 tree fn;
18533 bool success_p;
18535 /* Begin the function-definition. */
18536 success_p = start_function (decl_specifiers, declarator, attributes);
18538 /* The things we're about to see are not directly qualified by any
18539 template headers we've seen thus far. */
18540 reset_specialization ();
18542 /* If there were names looked up in the decl-specifier-seq that we
18543 did not check, check them now. We must wait until we are in the
18544 scope of the function to perform the checks, since the function
18545 might be a friend. */
18546 perform_deferred_access_checks ();
18548 if (!success_p)
18550 /* Skip the entire function. */
18551 cp_parser_skip_to_end_of_block_or_statement (parser);
18552 fn = error_mark_node;
18554 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
18556 /* Seen already, skip it. An error message has already been output. */
18557 cp_parser_skip_to_end_of_block_or_statement (parser);
18558 fn = current_function_decl;
18559 current_function_decl = NULL_TREE;
18560 /* If this is a function from a class, pop the nested class. */
18561 if (current_class_name)
18562 pop_nested_class ();
18564 else
18565 fn = cp_parser_function_definition_after_declarator (parser,
18566 /*inline_p=*/false);
18568 return fn;
18571 /* Parse the part of a function-definition that follows the
18572 declarator. INLINE_P is TRUE iff this function is an inline
18573 function defined within a class-specifier.
18575 Returns the function defined. */
18577 static tree
18578 cp_parser_function_definition_after_declarator (cp_parser* parser,
18579 bool inline_p)
18581 tree fn;
18582 bool ctor_initializer_p = false;
18583 bool saved_in_unbraced_linkage_specification_p;
18584 bool saved_in_function_body;
18585 unsigned saved_num_template_parameter_lists;
18586 cp_token *token;
18588 saved_in_function_body = parser->in_function_body;
18589 parser->in_function_body = true;
18590 /* If the next token is `return', then the code may be trying to
18591 make use of the "named return value" extension that G++ used to
18592 support. */
18593 token = cp_lexer_peek_token (parser->lexer);
18594 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
18596 /* Consume the `return' keyword. */
18597 cp_lexer_consume_token (parser->lexer);
18598 /* Look for the identifier that indicates what value is to be
18599 returned. */
18600 cp_parser_identifier (parser);
18601 /* Issue an error message. */
18602 error_at (token->location,
18603 "named return values are no longer supported");
18604 /* Skip tokens until we reach the start of the function body. */
18605 while (true)
18607 cp_token *token = cp_lexer_peek_token (parser->lexer);
18608 if (token->type == CPP_OPEN_BRACE
18609 || token->type == CPP_EOF
18610 || token->type == CPP_PRAGMA_EOL)
18611 break;
18612 cp_lexer_consume_token (parser->lexer);
18615 /* The `extern' in `extern "C" void f () { ... }' does not apply to
18616 anything declared inside `f'. */
18617 saved_in_unbraced_linkage_specification_p
18618 = parser->in_unbraced_linkage_specification_p;
18619 parser->in_unbraced_linkage_specification_p = false;
18620 /* Inside the function, surrounding template-parameter-lists do not
18621 apply. */
18622 saved_num_template_parameter_lists
18623 = parser->num_template_parameter_lists;
18624 parser->num_template_parameter_lists = 0;
18626 start_lambda_scope (current_function_decl);
18628 /* If the next token is `try', then we are looking at a
18629 function-try-block. */
18630 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
18631 ctor_initializer_p = cp_parser_function_try_block (parser);
18632 /* A function-try-block includes the function-body, so we only do
18633 this next part if we're not processing a function-try-block. */
18634 else
18635 ctor_initializer_p
18636 = cp_parser_ctor_initializer_opt_and_function_body (parser);
18638 finish_lambda_scope ();
18640 /* Finish the function. */
18641 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
18642 (inline_p ? 2 : 0));
18643 /* Generate code for it, if necessary. */
18644 expand_or_defer_fn (fn);
18645 /* Restore the saved values. */
18646 parser->in_unbraced_linkage_specification_p
18647 = saved_in_unbraced_linkage_specification_p;
18648 parser->num_template_parameter_lists
18649 = saved_num_template_parameter_lists;
18650 parser->in_function_body = saved_in_function_body;
18652 return fn;
18655 /* Parse a template-declaration, assuming that the `export' (and
18656 `extern') keywords, if present, has already been scanned. MEMBER_P
18657 is as for cp_parser_template_declaration. */
18659 static void
18660 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
18662 tree decl = NULL_TREE;
18663 VEC (deferred_access_check,gc) *checks;
18664 tree parameter_list;
18665 bool friend_p = false;
18666 bool need_lang_pop;
18667 cp_token *token;
18669 /* Look for the `template' keyword. */
18670 token = cp_lexer_peek_token (parser->lexer);
18671 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
18672 return;
18674 /* And the `<'. */
18675 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
18676 return;
18677 if (at_class_scope_p () && current_function_decl)
18679 /* 14.5.2.2 [temp.mem]
18681 A local class shall not have member templates. */
18682 error_at (token->location,
18683 "invalid declaration of member template in local class");
18684 cp_parser_skip_to_end_of_block_or_statement (parser);
18685 return;
18687 /* [temp]
18689 A template ... shall not have C linkage. */
18690 if (current_lang_name == lang_name_c)
18692 error_at (token->location, "template with C linkage");
18693 /* Give it C++ linkage to avoid confusing other parts of the
18694 front end. */
18695 push_lang_context (lang_name_cplusplus);
18696 need_lang_pop = true;
18698 else
18699 need_lang_pop = false;
18701 /* We cannot perform access checks on the template parameter
18702 declarations until we know what is being declared, just as we
18703 cannot check the decl-specifier list. */
18704 push_deferring_access_checks (dk_deferred);
18706 /* If the next token is `>', then we have an invalid
18707 specialization. Rather than complain about an invalid template
18708 parameter, issue an error message here. */
18709 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
18711 cp_parser_error (parser, "invalid explicit specialization");
18712 begin_specialization ();
18713 parameter_list = NULL_TREE;
18715 else
18716 /* Parse the template parameters. */
18717 parameter_list = cp_parser_template_parameter_list (parser);
18719 /* Get the deferred access checks from the parameter list. These
18720 will be checked once we know what is being declared, as for a
18721 member template the checks must be performed in the scope of the
18722 class containing the member. */
18723 checks = get_deferred_access_checks ();
18725 /* Look for the `>'. */
18726 cp_parser_skip_to_end_of_template_parameter_list (parser);
18727 /* We just processed one more parameter list. */
18728 ++parser->num_template_parameter_lists;
18729 /* If the next token is `template', there are more template
18730 parameters. */
18731 if (cp_lexer_next_token_is_keyword (parser->lexer,
18732 RID_TEMPLATE))
18733 cp_parser_template_declaration_after_export (parser, member_p);
18734 else
18736 /* There are no access checks when parsing a template, as we do not
18737 know if a specialization will be a friend. */
18738 push_deferring_access_checks (dk_no_check);
18739 token = cp_lexer_peek_token (parser->lexer);
18740 decl = cp_parser_single_declaration (parser,
18741 checks,
18742 member_p,
18743 /*explicit_specialization_p=*/false,
18744 &friend_p);
18745 pop_deferring_access_checks ();
18747 /* If this is a member template declaration, let the front
18748 end know. */
18749 if (member_p && !friend_p && decl)
18751 if (TREE_CODE (decl) == TYPE_DECL)
18752 cp_parser_check_access_in_redeclaration (decl, token->location);
18754 decl = finish_member_template_decl (decl);
18756 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
18757 make_friend_class (current_class_type, TREE_TYPE (decl),
18758 /*complain=*/true);
18760 /* We are done with the current parameter list. */
18761 --parser->num_template_parameter_lists;
18763 pop_deferring_access_checks ();
18765 /* Finish up. */
18766 finish_template_decl (parameter_list);
18768 /* Register member declarations. */
18769 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
18770 finish_member_declaration (decl);
18771 /* For the erroneous case of a template with C linkage, we pushed an
18772 implicit C++ linkage scope; exit that scope now. */
18773 if (need_lang_pop)
18774 pop_lang_context ();
18775 /* If DECL is a function template, we must return to parse it later.
18776 (Even though there is no definition, there might be default
18777 arguments that need handling.) */
18778 if (member_p && decl
18779 && (TREE_CODE (decl) == FUNCTION_DECL
18780 || DECL_FUNCTION_TEMPLATE_P (decl)))
18781 TREE_VALUE (parser->unparsed_functions_queues)
18782 = tree_cons (NULL_TREE, decl,
18783 TREE_VALUE (parser->unparsed_functions_queues));
18786 /* Perform the deferred access checks from a template-parameter-list.
18787 CHECKS is a TREE_LIST of access checks, as returned by
18788 get_deferred_access_checks. */
18790 static void
18791 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
18793 ++processing_template_parmlist;
18794 perform_access_checks (checks);
18795 --processing_template_parmlist;
18798 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
18799 `function-definition' sequence. MEMBER_P is true, this declaration
18800 appears in a class scope.
18802 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
18803 *FRIEND_P is set to TRUE iff the declaration is a friend. */
18805 static tree
18806 cp_parser_single_declaration (cp_parser* parser,
18807 VEC (deferred_access_check,gc)* checks,
18808 bool member_p,
18809 bool explicit_specialization_p,
18810 bool* friend_p)
18812 int declares_class_or_enum;
18813 tree decl = NULL_TREE;
18814 cp_decl_specifier_seq decl_specifiers;
18815 bool function_definition_p = false;
18816 cp_token *decl_spec_token_start;
18818 /* This function is only used when processing a template
18819 declaration. */
18820 gcc_assert (innermost_scope_kind () == sk_template_parms
18821 || innermost_scope_kind () == sk_template_spec);
18823 /* Defer access checks until we know what is being declared. */
18824 push_deferring_access_checks (dk_deferred);
18826 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
18827 alternative. */
18828 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
18829 cp_parser_decl_specifier_seq (parser,
18830 CP_PARSER_FLAGS_OPTIONAL,
18831 &decl_specifiers,
18832 &declares_class_or_enum);
18833 if (friend_p)
18834 *friend_p = cp_parser_friend_p (&decl_specifiers);
18836 /* There are no template typedefs. */
18837 if (decl_specifiers.specs[(int) ds_typedef])
18839 error_at (decl_spec_token_start->location,
18840 "template declaration of %<typedef%>");
18841 decl = error_mark_node;
18844 /* Gather up the access checks that occurred the
18845 decl-specifier-seq. */
18846 stop_deferring_access_checks ();
18848 /* Check for the declaration of a template class. */
18849 if (declares_class_or_enum)
18851 if (cp_parser_declares_only_class_p (parser))
18853 decl = shadow_tag (&decl_specifiers);
18855 /* In this case:
18857 struct C {
18858 friend template <typename T> struct A<T>::B;
18861 A<T>::B will be represented by a TYPENAME_TYPE, and
18862 therefore not recognized by shadow_tag. */
18863 if (friend_p && *friend_p
18864 && !decl
18865 && decl_specifiers.type
18866 && TYPE_P (decl_specifiers.type))
18867 decl = decl_specifiers.type;
18869 if (decl && decl != error_mark_node)
18870 decl = TYPE_NAME (decl);
18871 else
18872 decl = error_mark_node;
18874 /* Perform access checks for template parameters. */
18875 cp_parser_perform_template_parameter_access_checks (checks);
18879 /* Complain about missing 'typename' or other invalid type names. */
18880 if (!decl_specifiers.any_type_specifiers_p)
18881 cp_parser_parse_and_diagnose_invalid_type_name (parser);
18883 /* If it's not a template class, try for a template function. If
18884 the next token is a `;', then this declaration does not declare
18885 anything. But, if there were errors in the decl-specifiers, then
18886 the error might well have come from an attempted class-specifier.
18887 In that case, there's no need to warn about a missing declarator. */
18888 if (!decl
18889 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
18890 || decl_specifiers.type != error_mark_node))
18892 decl = cp_parser_init_declarator (parser,
18893 &decl_specifiers,
18894 checks,
18895 /*function_definition_allowed_p=*/true,
18896 member_p,
18897 declares_class_or_enum,
18898 &function_definition_p);
18900 /* 7.1.1-1 [dcl.stc]
18902 A storage-class-specifier shall not be specified in an explicit
18903 specialization... */
18904 if (decl
18905 && explicit_specialization_p
18906 && decl_specifiers.storage_class != sc_none)
18908 error_at (decl_spec_token_start->location,
18909 "explicit template specialization cannot have a storage class");
18910 decl = error_mark_node;
18914 pop_deferring_access_checks ();
18916 /* Clear any current qualification; whatever comes next is the start
18917 of something new. */
18918 parser->scope = NULL_TREE;
18919 parser->qualifying_scope = NULL_TREE;
18920 parser->object_scope = NULL_TREE;
18921 /* Look for a trailing `;' after the declaration. */
18922 if (!function_definition_p
18923 && (decl == error_mark_node
18924 || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
18925 cp_parser_skip_to_end_of_block_or_statement (parser);
18927 return decl;
18930 /* Parse a cast-expression that is not the operand of a unary "&". */
18932 static tree
18933 cp_parser_simple_cast_expression (cp_parser *parser)
18935 return cp_parser_cast_expression (parser, /*address_p=*/false,
18936 /*cast_p=*/false, NULL);
18939 /* Parse a functional cast to TYPE. Returns an expression
18940 representing the cast. */
18942 static tree
18943 cp_parser_functional_cast (cp_parser* parser, tree type)
18945 VEC(tree,gc) *vec;
18946 tree expression_list;
18947 tree cast;
18948 bool nonconst_p;
18950 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18952 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
18953 expression_list = cp_parser_braced_list (parser, &nonconst_p);
18954 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
18955 if (TREE_CODE (type) == TYPE_DECL)
18956 type = TREE_TYPE (type);
18957 return finish_compound_literal (type, expression_list);
18961 vec = cp_parser_parenthesized_expression_list (parser, false,
18962 /*cast_p=*/true,
18963 /*allow_expansion_p=*/true,
18964 /*non_constant_p=*/NULL);
18965 if (vec == NULL)
18966 expression_list = error_mark_node;
18967 else
18969 expression_list = build_tree_list_vec (vec);
18970 release_tree_vector (vec);
18973 cast = build_functional_cast (type, expression_list,
18974 tf_warning_or_error);
18975 /* [expr.const]/1: In an integral constant expression "only type
18976 conversions to integral or enumeration type can be used". */
18977 if (TREE_CODE (type) == TYPE_DECL)
18978 type = TREE_TYPE (type);
18979 if (cast != error_mark_node
18980 && !cast_valid_in_integral_constant_expression_p (type)
18981 && (cp_parser_non_integral_constant_expression
18982 (parser, "a call to a constructor")))
18983 return error_mark_node;
18984 return cast;
18987 /* Save the tokens that make up the body of a member function defined
18988 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
18989 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
18990 specifiers applied to the declaration. Returns the FUNCTION_DECL
18991 for the member function. */
18993 static tree
18994 cp_parser_save_member_function_body (cp_parser* parser,
18995 cp_decl_specifier_seq *decl_specifiers,
18996 cp_declarator *declarator,
18997 tree attributes)
18999 cp_token *first;
19000 cp_token *last;
19001 tree fn;
19003 /* Create the FUNCTION_DECL. */
19004 fn = grokmethod (decl_specifiers, declarator, attributes);
19005 /* If something went badly wrong, bail out now. */
19006 if (fn == error_mark_node)
19008 /* If there's a function-body, skip it. */
19009 if (cp_parser_token_starts_function_definition_p
19010 (cp_lexer_peek_token (parser->lexer)))
19011 cp_parser_skip_to_end_of_block_or_statement (parser);
19012 return error_mark_node;
19015 /* Remember it, if there default args to post process. */
19016 cp_parser_save_default_args (parser, fn);
19018 /* Save away the tokens that make up the body of the
19019 function. */
19020 first = parser->lexer->next_token;
19021 /* We can have braced-init-list mem-initializers before the fn body. */
19022 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19024 cp_lexer_consume_token (parser->lexer);
19025 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
19026 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
19028 /* cache_group will stop after an un-nested { } pair, too. */
19029 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
19030 break;
19032 /* variadic mem-inits have ... after the ')'. */
19033 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19034 cp_lexer_consume_token (parser->lexer);
19037 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
19038 /* Handle function try blocks. */
19039 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
19040 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
19041 last = parser->lexer->next_token;
19043 /* Save away the inline definition; we will process it when the
19044 class is complete. */
19045 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
19046 DECL_PENDING_INLINE_P (fn) = 1;
19048 /* We need to know that this was defined in the class, so that
19049 friend templates are handled correctly. */
19050 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
19052 /* Add FN to the queue of functions to be parsed later. */
19053 TREE_VALUE (parser->unparsed_functions_queues)
19054 = tree_cons (NULL_TREE, fn,
19055 TREE_VALUE (parser->unparsed_functions_queues));
19057 return fn;
19060 /* Parse a template-argument-list, as well as the trailing ">" (but
19061 not the opening ">"). See cp_parser_template_argument_list for the
19062 return value. */
19064 static tree
19065 cp_parser_enclosed_template_argument_list (cp_parser* parser)
19067 tree arguments;
19068 tree saved_scope;
19069 tree saved_qualifying_scope;
19070 tree saved_object_scope;
19071 bool saved_greater_than_is_operator_p;
19072 int saved_unevaluated_operand;
19073 int saved_inhibit_evaluation_warnings;
19075 /* [temp.names]
19077 When parsing a template-id, the first non-nested `>' is taken as
19078 the end of the template-argument-list rather than a greater-than
19079 operator. */
19080 saved_greater_than_is_operator_p
19081 = parser->greater_than_is_operator_p;
19082 parser->greater_than_is_operator_p = false;
19083 /* Parsing the argument list may modify SCOPE, so we save it
19084 here. */
19085 saved_scope = parser->scope;
19086 saved_qualifying_scope = parser->qualifying_scope;
19087 saved_object_scope = parser->object_scope;
19088 /* We need to evaluate the template arguments, even though this
19089 template-id may be nested within a "sizeof". */
19090 saved_unevaluated_operand = cp_unevaluated_operand;
19091 cp_unevaluated_operand = 0;
19092 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
19093 c_inhibit_evaluation_warnings = 0;
19094 /* Parse the template-argument-list itself. */
19095 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
19096 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
19097 arguments = NULL_TREE;
19098 else
19099 arguments = cp_parser_template_argument_list (parser);
19100 /* Look for the `>' that ends the template-argument-list. If we find
19101 a '>>' instead, it's probably just a typo. */
19102 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
19104 if (cxx_dialect != cxx98)
19106 /* In C++0x, a `>>' in a template argument list or cast
19107 expression is considered to be two separate `>'
19108 tokens. So, change the current token to a `>', but don't
19109 consume it: it will be consumed later when the outer
19110 template argument list (or cast expression) is parsed.
19111 Note that this replacement of `>' for `>>' is necessary
19112 even if we are parsing tentatively: in the tentative
19113 case, after calling
19114 cp_parser_enclosed_template_argument_list we will always
19115 throw away all of the template arguments and the first
19116 closing `>', either because the template argument list
19117 was erroneous or because we are replacing those tokens
19118 with a CPP_TEMPLATE_ID token. The second `>' (which will
19119 not have been thrown away) is needed either to close an
19120 outer template argument list or to complete a new-style
19121 cast. */
19122 cp_token *token = cp_lexer_peek_token (parser->lexer);
19123 token->type = CPP_GREATER;
19125 else if (!saved_greater_than_is_operator_p)
19127 /* If we're in a nested template argument list, the '>>' has
19128 to be a typo for '> >'. We emit the error message, but we
19129 continue parsing and we push a '>' as next token, so that
19130 the argument list will be parsed correctly. Note that the
19131 global source location is still on the token before the
19132 '>>', so we need to say explicitly where we want it. */
19133 cp_token *token = cp_lexer_peek_token (parser->lexer);
19134 error_at (token->location, "%<>>%> should be %<> >%> "
19135 "within a nested template argument list");
19137 token->type = CPP_GREATER;
19139 else
19141 /* If this is not a nested template argument list, the '>>'
19142 is a typo for '>'. Emit an error message and continue.
19143 Same deal about the token location, but here we can get it
19144 right by consuming the '>>' before issuing the diagnostic. */
19145 cp_token *token = cp_lexer_consume_token (parser->lexer);
19146 error_at (token->location,
19147 "spurious %<>>%>, use %<>%> to terminate "
19148 "a template argument list");
19151 else
19152 cp_parser_skip_to_end_of_template_parameter_list (parser);
19153 /* The `>' token might be a greater-than operator again now. */
19154 parser->greater_than_is_operator_p
19155 = saved_greater_than_is_operator_p;
19156 /* Restore the SAVED_SCOPE. */
19157 parser->scope = saved_scope;
19158 parser->qualifying_scope = saved_qualifying_scope;
19159 parser->object_scope = saved_object_scope;
19160 cp_unevaluated_operand = saved_unevaluated_operand;
19161 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
19163 return arguments;
19166 /* MEMBER_FUNCTION is a member function, or a friend. If default
19167 arguments, or the body of the function have not yet been parsed,
19168 parse them now. */
19170 static void
19171 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
19173 /* If this member is a template, get the underlying
19174 FUNCTION_DECL. */
19175 if (DECL_FUNCTION_TEMPLATE_P (member_function))
19176 member_function = DECL_TEMPLATE_RESULT (member_function);
19178 /* There should not be any class definitions in progress at this
19179 point; the bodies of members are only parsed outside of all class
19180 definitions. */
19181 gcc_assert (parser->num_classes_being_defined == 0);
19182 /* While we're parsing the member functions we might encounter more
19183 classes. We want to handle them right away, but we don't want
19184 them getting mixed up with functions that are currently in the
19185 queue. */
19186 parser->unparsed_functions_queues
19187 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
19189 /* Make sure that any template parameters are in scope. */
19190 maybe_begin_member_template_processing (member_function);
19192 /* If the body of the function has not yet been parsed, parse it
19193 now. */
19194 if (DECL_PENDING_INLINE_P (member_function))
19196 tree function_scope;
19197 cp_token_cache *tokens;
19199 /* The function is no longer pending; we are processing it. */
19200 tokens = DECL_PENDING_INLINE_INFO (member_function);
19201 DECL_PENDING_INLINE_INFO (member_function) = NULL;
19202 DECL_PENDING_INLINE_P (member_function) = 0;
19204 /* If this is a local class, enter the scope of the containing
19205 function. */
19206 function_scope = current_function_decl;
19207 if (function_scope)
19208 push_function_context ();
19210 /* Push the body of the function onto the lexer stack. */
19211 cp_parser_push_lexer_for_tokens (parser, tokens);
19213 /* Let the front end know that we going to be defining this
19214 function. */
19215 start_preparsed_function (member_function, NULL_TREE,
19216 SF_PRE_PARSED | SF_INCLASS_INLINE);
19218 /* Don't do access checking if it is a templated function. */
19219 if (processing_template_decl)
19220 push_deferring_access_checks (dk_no_check);
19222 /* Now, parse the body of the function. */
19223 cp_parser_function_definition_after_declarator (parser,
19224 /*inline_p=*/true);
19226 if (processing_template_decl)
19227 pop_deferring_access_checks ();
19229 /* Leave the scope of the containing function. */
19230 if (function_scope)
19231 pop_function_context ();
19232 cp_parser_pop_lexer (parser);
19235 /* Remove any template parameters from the symbol table. */
19236 maybe_end_member_template_processing ();
19238 /* Restore the queue. */
19239 parser->unparsed_functions_queues
19240 = TREE_CHAIN (parser->unparsed_functions_queues);
19243 /* If DECL contains any default args, remember it on the unparsed
19244 functions queue. */
19246 static void
19247 cp_parser_save_default_args (cp_parser* parser, tree decl)
19249 tree probe;
19251 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
19252 probe;
19253 probe = TREE_CHAIN (probe))
19254 if (TREE_PURPOSE (probe))
19256 TREE_PURPOSE (parser->unparsed_functions_queues)
19257 = tree_cons (current_class_type, decl,
19258 TREE_PURPOSE (parser->unparsed_functions_queues));
19259 break;
19263 /* FN is a FUNCTION_DECL which may contains a parameter with an
19264 unparsed DEFAULT_ARG. Parse the default args now. This function
19265 assumes that the current scope is the scope in which the default
19266 argument should be processed. */
19268 static void
19269 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
19271 bool saved_local_variables_forbidden_p;
19272 tree parm, parmdecl;
19274 /* While we're parsing the default args, we might (due to the
19275 statement expression extension) encounter more classes. We want
19276 to handle them right away, but we don't want them getting mixed
19277 up with default args that are currently in the queue. */
19278 parser->unparsed_functions_queues
19279 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
19281 /* Local variable names (and the `this' keyword) may not appear
19282 in a default argument. */
19283 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
19284 parser->local_variables_forbidden_p = true;
19286 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
19287 parmdecl = DECL_ARGUMENTS (fn);
19288 parm && parm != void_list_node;
19289 parm = TREE_CHAIN (parm),
19290 parmdecl = TREE_CHAIN (parmdecl))
19292 cp_token_cache *tokens;
19293 tree default_arg = TREE_PURPOSE (parm);
19294 tree parsed_arg;
19295 VEC(tree,gc) *insts;
19296 tree copy;
19297 unsigned ix;
19299 if (!default_arg)
19300 continue;
19302 if (TREE_CODE (default_arg) != DEFAULT_ARG)
19303 /* This can happen for a friend declaration for a function
19304 already declared with default arguments. */
19305 continue;
19307 /* Push the saved tokens for the default argument onto the parser's
19308 lexer stack. */
19309 tokens = DEFARG_TOKENS (default_arg);
19310 cp_parser_push_lexer_for_tokens (parser, tokens);
19312 start_lambda_scope (parmdecl);
19314 /* Parse the assignment-expression. */
19315 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
19316 if (parsed_arg == error_mark_node)
19318 cp_parser_pop_lexer (parser);
19319 continue;
19322 if (!processing_template_decl)
19323 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
19325 TREE_PURPOSE (parm) = parsed_arg;
19327 /* Update any instantiations we've already created. */
19328 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
19329 VEC_iterate (tree, insts, ix, copy); ix++)
19330 TREE_PURPOSE (copy) = parsed_arg;
19332 finish_lambda_scope ();
19334 /* If the token stream has not been completely used up, then
19335 there was extra junk after the end of the default
19336 argument. */
19337 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
19338 cp_parser_error (parser, "expected %<,%>");
19340 /* Revert to the main lexer. */
19341 cp_parser_pop_lexer (parser);
19344 /* Make sure no default arg is missing. */
19345 check_default_args (fn);
19347 /* Restore the state of local_variables_forbidden_p. */
19348 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
19350 /* Restore the queue. */
19351 parser->unparsed_functions_queues
19352 = TREE_CHAIN (parser->unparsed_functions_queues);
19355 /* Parse the operand of `sizeof' (or a similar operator). Returns
19356 either a TYPE or an expression, depending on the form of the
19357 input. The KEYWORD indicates which kind of expression we have
19358 encountered. */
19360 static tree
19361 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
19363 tree expr = NULL_TREE;
19364 const char *saved_message;
19365 char *tmp;
19366 bool saved_integral_constant_expression_p;
19367 bool saved_non_integral_constant_expression_p;
19368 bool pack_expansion_p = false;
19370 /* Types cannot be defined in a `sizeof' expression. Save away the
19371 old message. */
19372 saved_message = parser->type_definition_forbidden_message;
19373 /* And create the new one. */
19374 tmp = concat ("types may not be defined in %<",
19375 IDENTIFIER_POINTER (ridpointers[keyword]),
19376 "%> expressions", NULL);
19377 parser->type_definition_forbidden_message = tmp;
19379 /* The restrictions on constant-expressions do not apply inside
19380 sizeof expressions. */
19381 saved_integral_constant_expression_p
19382 = parser->integral_constant_expression_p;
19383 saved_non_integral_constant_expression_p
19384 = parser->non_integral_constant_expression_p;
19385 parser->integral_constant_expression_p = false;
19387 /* If it's a `...', then we are computing the length of a parameter
19388 pack. */
19389 if (keyword == RID_SIZEOF
19390 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19392 /* Consume the `...'. */
19393 cp_lexer_consume_token (parser->lexer);
19394 maybe_warn_variadic_templates ();
19396 /* Note that this is an expansion. */
19397 pack_expansion_p = true;
19400 /* Do not actually evaluate the expression. */
19401 ++cp_unevaluated_operand;
19402 ++c_inhibit_evaluation_warnings;
19403 /* If it's a `(', then we might be looking at the type-id
19404 construction. */
19405 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19407 tree type;
19408 bool saved_in_type_id_in_expr_p;
19410 /* We can't be sure yet whether we're looking at a type-id or an
19411 expression. */
19412 cp_parser_parse_tentatively (parser);
19413 /* Consume the `('. */
19414 cp_lexer_consume_token (parser->lexer);
19415 /* Parse the type-id. */
19416 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
19417 parser->in_type_id_in_expr_p = true;
19418 type = cp_parser_type_id (parser);
19419 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
19420 /* Now, look for the trailing `)'. */
19421 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19422 /* If all went well, then we're done. */
19423 if (cp_parser_parse_definitely (parser))
19425 cp_decl_specifier_seq decl_specs;
19427 /* Build a trivial decl-specifier-seq. */
19428 clear_decl_specs (&decl_specs);
19429 decl_specs.type = type;
19431 /* Call grokdeclarator to figure out what type this is. */
19432 expr = grokdeclarator (NULL,
19433 &decl_specs,
19434 TYPENAME,
19435 /*initialized=*/0,
19436 /*attrlist=*/NULL);
19440 /* If the type-id production did not work out, then we must be
19441 looking at the unary-expression production. */
19442 if (!expr)
19443 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
19444 /*cast_p=*/false, NULL);
19446 if (pack_expansion_p)
19447 /* Build a pack expansion. */
19448 expr = make_pack_expansion (expr);
19450 /* Go back to evaluating expressions. */
19451 --cp_unevaluated_operand;
19452 --c_inhibit_evaluation_warnings;
19454 /* Free the message we created. */
19455 free (tmp);
19456 /* And restore the old one. */
19457 parser->type_definition_forbidden_message = saved_message;
19458 parser->integral_constant_expression_p
19459 = saved_integral_constant_expression_p;
19460 parser->non_integral_constant_expression_p
19461 = saved_non_integral_constant_expression_p;
19463 return expr;
19466 /* If the current declaration has no declarator, return true. */
19468 static bool
19469 cp_parser_declares_only_class_p (cp_parser *parser)
19471 /* If the next token is a `;' or a `,' then there is no
19472 declarator. */
19473 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
19474 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
19477 /* Update the DECL_SPECS to reflect the storage class indicated by
19478 KEYWORD. */
19480 static void
19481 cp_parser_set_storage_class (cp_parser *parser,
19482 cp_decl_specifier_seq *decl_specs,
19483 enum rid keyword,
19484 location_t location)
19486 cp_storage_class storage_class;
19488 if (parser->in_unbraced_linkage_specification_p)
19490 error_at (location, "invalid use of %qD in linkage specification",
19491 ridpointers[keyword]);
19492 return;
19494 else if (decl_specs->storage_class != sc_none)
19496 decl_specs->conflicting_specifiers_p = true;
19497 return;
19500 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
19501 && decl_specs->specs[(int) ds_thread])
19503 error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
19504 decl_specs->specs[(int) ds_thread] = 0;
19507 switch (keyword)
19509 case RID_AUTO:
19510 storage_class = sc_auto;
19511 break;
19512 case RID_REGISTER:
19513 storage_class = sc_register;
19514 break;
19515 case RID_STATIC:
19516 storage_class = sc_static;
19517 break;
19518 case RID_EXTERN:
19519 storage_class = sc_extern;
19520 break;
19521 case RID_MUTABLE:
19522 storage_class = sc_mutable;
19523 break;
19524 default:
19525 gcc_unreachable ();
19527 decl_specs->storage_class = storage_class;
19529 /* A storage class specifier cannot be applied alongside a typedef
19530 specifier. If there is a typedef specifier present then set
19531 conflicting_specifiers_p which will trigger an error later
19532 on in grokdeclarator. */
19533 if (decl_specs->specs[(int)ds_typedef])
19534 decl_specs->conflicting_specifiers_p = true;
19537 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
19538 is true, the type is a user-defined type; otherwise it is a
19539 built-in type specified by a keyword. */
19541 static void
19542 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
19543 tree type_spec,
19544 location_t location,
19545 bool user_defined_p)
19547 decl_specs->any_specifiers_p = true;
19549 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
19550 (with, for example, in "typedef int wchar_t;") we remember that
19551 this is what happened. In system headers, we ignore these
19552 declarations so that G++ can work with system headers that are not
19553 C++-safe. */
19554 if (decl_specs->specs[(int) ds_typedef]
19555 && !user_defined_p
19556 && (type_spec == boolean_type_node
19557 || type_spec == char16_type_node
19558 || type_spec == char32_type_node
19559 || type_spec == wchar_type_node)
19560 && (decl_specs->type
19561 || decl_specs->specs[(int) ds_long]
19562 || decl_specs->specs[(int) ds_short]
19563 || decl_specs->specs[(int) ds_unsigned]
19564 || decl_specs->specs[(int) ds_signed]))
19566 decl_specs->redefined_builtin_type = type_spec;
19567 if (!decl_specs->type)
19569 decl_specs->type = type_spec;
19570 decl_specs->user_defined_type_p = false;
19571 decl_specs->type_location = location;
19574 else if (decl_specs->type)
19575 decl_specs->multiple_types_p = true;
19576 else
19578 decl_specs->type = type_spec;
19579 decl_specs->user_defined_type_p = user_defined_p;
19580 decl_specs->redefined_builtin_type = NULL_TREE;
19581 decl_specs->type_location = location;
19585 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
19586 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
19588 static bool
19589 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
19591 return decl_specifiers->specs[(int) ds_friend] != 0;
19594 /* If the next token is of the indicated TYPE, consume it. Otherwise,
19595 issue an error message indicating that TOKEN_DESC was expected.
19597 Returns the token consumed, if the token had the appropriate type.
19598 Otherwise, returns NULL. */
19600 static cp_token *
19601 cp_parser_require (cp_parser* parser,
19602 enum cpp_ttype type,
19603 const char* token_desc)
19605 if (cp_lexer_next_token_is (parser->lexer, type))
19606 return cp_lexer_consume_token (parser->lexer);
19607 else
19609 /* Output the MESSAGE -- unless we're parsing tentatively. */
19610 if (!cp_parser_simulate_error (parser))
19612 char *message = concat ("expected ", token_desc, NULL);
19613 cp_parser_error (parser, message);
19614 free (message);
19616 return NULL;
19620 /* An error message is produced if the next token is not '>'.
19621 All further tokens are skipped until the desired token is
19622 found or '{', '}', ';' or an unbalanced ')' or ']'. */
19624 static void
19625 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
19627 /* Current level of '< ... >'. */
19628 unsigned level = 0;
19629 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
19630 unsigned nesting_depth = 0;
19632 /* Are we ready, yet? If not, issue error message. */
19633 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
19634 return;
19636 /* Skip tokens until the desired token is found. */
19637 while (true)
19639 /* Peek at the next token. */
19640 switch (cp_lexer_peek_token (parser->lexer)->type)
19642 case CPP_LESS:
19643 if (!nesting_depth)
19644 ++level;
19645 break;
19647 case CPP_RSHIFT:
19648 if (cxx_dialect == cxx98)
19649 /* C++0x views the `>>' operator as two `>' tokens, but
19650 C++98 does not. */
19651 break;
19652 else if (!nesting_depth && level-- == 0)
19654 /* We've hit a `>>' where the first `>' closes the
19655 template argument list, and the second `>' is
19656 spurious. Just consume the `>>' and stop; we've
19657 already produced at least one error. */
19658 cp_lexer_consume_token (parser->lexer);
19659 return;
19661 /* Fall through for C++0x, so we handle the second `>' in
19662 the `>>'. */
19664 case CPP_GREATER:
19665 if (!nesting_depth && level-- == 0)
19667 /* We've reached the token we want, consume it and stop. */
19668 cp_lexer_consume_token (parser->lexer);
19669 return;
19671 break;
19673 case CPP_OPEN_PAREN:
19674 case CPP_OPEN_SQUARE:
19675 ++nesting_depth;
19676 break;
19678 case CPP_CLOSE_PAREN:
19679 case CPP_CLOSE_SQUARE:
19680 if (nesting_depth-- == 0)
19681 return;
19682 break;
19684 case CPP_EOF:
19685 case CPP_PRAGMA_EOL:
19686 case CPP_SEMICOLON:
19687 case CPP_OPEN_BRACE:
19688 case CPP_CLOSE_BRACE:
19689 /* The '>' was probably forgotten, don't look further. */
19690 return;
19692 default:
19693 break;
19696 /* Consume this token. */
19697 cp_lexer_consume_token (parser->lexer);
19701 /* If the next token is the indicated keyword, consume it. Otherwise,
19702 issue an error message indicating that TOKEN_DESC was expected.
19704 Returns the token consumed, if the token had the appropriate type.
19705 Otherwise, returns NULL. */
19707 static cp_token *
19708 cp_parser_require_keyword (cp_parser* parser,
19709 enum rid keyword,
19710 const char* token_desc)
19712 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
19714 if (token && token->keyword != keyword)
19716 dyn_string_t error_msg;
19718 /* Format the error message. */
19719 error_msg = dyn_string_new (0);
19720 dyn_string_append_cstr (error_msg, "expected ");
19721 dyn_string_append_cstr (error_msg, token_desc);
19722 cp_parser_error (parser, error_msg->s);
19723 dyn_string_delete (error_msg);
19724 return NULL;
19727 return token;
19730 /* Returns TRUE iff TOKEN is a token that can begin the body of a
19731 function-definition. */
19733 static bool
19734 cp_parser_token_starts_function_definition_p (cp_token* token)
19736 return (/* An ordinary function-body begins with an `{'. */
19737 token->type == CPP_OPEN_BRACE
19738 /* A ctor-initializer begins with a `:'. */
19739 || token->type == CPP_COLON
19740 /* A function-try-block begins with `try'. */
19741 || token->keyword == RID_TRY
19742 /* The named return value extension begins with `return'. */
19743 || token->keyword == RID_RETURN);
19746 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
19747 definition. */
19749 static bool
19750 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
19752 cp_token *token;
19754 token = cp_lexer_peek_token (parser->lexer);
19755 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
19758 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
19759 C++0x) ending a template-argument. */
19761 static bool
19762 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
19764 cp_token *token;
19766 token = cp_lexer_peek_token (parser->lexer);
19767 return (token->type == CPP_COMMA
19768 || token->type == CPP_GREATER
19769 || token->type == CPP_ELLIPSIS
19770 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
19773 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
19774 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
19776 static bool
19777 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
19778 size_t n)
19780 cp_token *token;
19782 token = cp_lexer_peek_nth_token (parser->lexer, n);
19783 if (token->type == CPP_LESS)
19784 return true;
19785 /* Check for the sequence `<::' in the original code. It would be lexed as
19786 `[:', where `[' is a digraph, and there is no whitespace before
19787 `:'. */
19788 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
19790 cp_token *token2;
19791 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
19792 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
19793 return true;
19795 return false;
19798 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
19799 or none_type otherwise. */
19801 static enum tag_types
19802 cp_parser_token_is_class_key (cp_token* token)
19804 switch (token->keyword)
19806 case RID_CLASS:
19807 return class_type;
19808 case RID_STRUCT:
19809 return record_type;
19810 case RID_UNION:
19811 return union_type;
19813 default:
19814 return none_type;
19818 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
19820 static void
19821 cp_parser_check_class_key (enum tag_types class_key, tree type)
19823 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
19824 permerror (input_location, "%qs tag used in naming %q#T",
19825 class_key == union_type ? "union"
19826 : class_key == record_type ? "struct" : "class",
19827 type);
19830 /* Issue an error message if DECL is redeclared with different
19831 access than its original declaration [class.access.spec/3].
19832 This applies to nested classes and nested class templates.
19833 [class.mem/1]. */
19835 static void
19836 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
19838 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
19839 return;
19841 if ((TREE_PRIVATE (decl)
19842 != (current_access_specifier == access_private_node))
19843 || (TREE_PROTECTED (decl)
19844 != (current_access_specifier == access_protected_node)))
19845 error_at (location, "%qD redeclared with different access", decl);
19848 /* Look for the `template' keyword, as a syntactic disambiguator.
19849 Return TRUE iff it is present, in which case it will be
19850 consumed. */
19852 static bool
19853 cp_parser_optional_template_keyword (cp_parser *parser)
19855 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
19857 /* The `template' keyword can only be used within templates;
19858 outside templates the parser can always figure out what is a
19859 template and what is not. */
19860 if (!processing_template_decl)
19862 cp_token *token = cp_lexer_peek_token (parser->lexer);
19863 error_at (token->location,
19864 "%<template%> (as a disambiguator) is only allowed "
19865 "within templates");
19866 /* If this part of the token stream is rescanned, the same
19867 error message would be generated. So, we purge the token
19868 from the stream. */
19869 cp_lexer_purge_token (parser->lexer);
19870 return false;
19872 else
19874 /* Consume the `template' keyword. */
19875 cp_lexer_consume_token (parser->lexer);
19876 return true;
19880 return false;
19883 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
19884 set PARSER->SCOPE, and perform other related actions. */
19886 static void
19887 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
19889 int i;
19890 struct tree_check *check_value;
19891 deferred_access_check *chk;
19892 VEC (deferred_access_check,gc) *checks;
19894 /* Get the stored value. */
19895 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
19896 /* Perform any access checks that were deferred. */
19897 checks = check_value->checks;
19898 if (checks)
19900 for (i = 0 ;
19901 VEC_iterate (deferred_access_check, checks, i, chk) ;
19902 ++i)
19904 perform_or_defer_access_check (chk->binfo,
19905 chk->decl,
19906 chk->diag_decl);
19909 /* Set the scope from the stored value. */
19910 parser->scope = check_value->value;
19911 parser->qualifying_scope = check_value->qualifying_scope;
19912 parser->object_scope = NULL_TREE;
19915 /* Consume tokens up through a non-nested END token. Returns TRUE if we
19916 encounter the end of a block before what we were looking for. */
19918 static bool
19919 cp_parser_cache_group (cp_parser *parser,
19920 enum cpp_ttype end,
19921 unsigned depth)
19923 while (true)
19925 cp_token *token = cp_lexer_peek_token (parser->lexer);
19927 /* Abort a parenthesized expression if we encounter a semicolon. */
19928 if ((end == CPP_CLOSE_PAREN || depth == 0)
19929 && token->type == CPP_SEMICOLON)
19930 return true;
19931 /* If we've reached the end of the file, stop. */
19932 if (token->type == CPP_EOF
19933 || (end != CPP_PRAGMA_EOL
19934 && token->type == CPP_PRAGMA_EOL))
19935 return true;
19936 if (token->type == CPP_CLOSE_BRACE && depth == 0)
19937 /* We've hit the end of an enclosing block, so there's been some
19938 kind of syntax error. */
19939 return true;
19941 /* Consume the token. */
19942 cp_lexer_consume_token (parser->lexer);
19943 /* See if it starts a new group. */
19944 if (token->type == CPP_OPEN_BRACE)
19946 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
19947 /* In theory this should probably check end == '}', but
19948 cp_parser_save_member_function_body needs it to exit
19949 after either '}' or ')' when called with ')'. */
19950 if (depth == 0)
19951 return false;
19953 else if (token->type == CPP_OPEN_PAREN)
19955 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
19956 if (depth == 0 && end == CPP_CLOSE_PAREN)
19957 return false;
19959 else if (token->type == CPP_PRAGMA)
19960 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
19961 else if (token->type == end)
19962 return false;
19966 /* Begin parsing tentatively. We always save tokens while parsing
19967 tentatively so that if the tentative parsing fails we can restore the
19968 tokens. */
19970 static void
19971 cp_parser_parse_tentatively (cp_parser* parser)
19973 /* Enter a new parsing context. */
19974 parser->context = cp_parser_context_new (parser->context);
19975 /* Begin saving tokens. */
19976 cp_lexer_save_tokens (parser->lexer);
19977 /* In order to avoid repetitive access control error messages,
19978 access checks are queued up until we are no longer parsing
19979 tentatively. */
19980 push_deferring_access_checks (dk_deferred);
19983 /* Commit to the currently active tentative parse. */
19985 static void
19986 cp_parser_commit_to_tentative_parse (cp_parser* parser)
19988 cp_parser_context *context;
19989 cp_lexer *lexer;
19991 /* Mark all of the levels as committed. */
19992 lexer = parser->lexer;
19993 for (context = parser->context; context->next; context = context->next)
19995 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
19996 break;
19997 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
19998 while (!cp_lexer_saving_tokens (lexer))
19999 lexer = lexer->next;
20000 cp_lexer_commit_tokens (lexer);
20004 /* Abort the currently active tentative parse. All consumed tokens
20005 will be rolled back, and no diagnostics will be issued. */
20007 static void
20008 cp_parser_abort_tentative_parse (cp_parser* parser)
20010 cp_parser_simulate_error (parser);
20011 /* Now, pretend that we want to see if the construct was
20012 successfully parsed. */
20013 cp_parser_parse_definitely (parser);
20016 /* Stop parsing tentatively. If a parse error has occurred, restore the
20017 token stream. Otherwise, commit to the tokens we have consumed.
20018 Returns true if no error occurred; false otherwise. */
20020 static bool
20021 cp_parser_parse_definitely (cp_parser* parser)
20023 bool error_occurred;
20024 cp_parser_context *context;
20026 /* Remember whether or not an error occurred, since we are about to
20027 destroy that information. */
20028 error_occurred = cp_parser_error_occurred (parser);
20029 /* Remove the topmost context from the stack. */
20030 context = parser->context;
20031 parser->context = context->next;
20032 /* If no parse errors occurred, commit to the tentative parse. */
20033 if (!error_occurred)
20035 /* Commit to the tokens read tentatively, unless that was
20036 already done. */
20037 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
20038 cp_lexer_commit_tokens (parser->lexer);
20040 pop_to_parent_deferring_access_checks ();
20042 /* Otherwise, if errors occurred, roll back our state so that things
20043 are just as they were before we began the tentative parse. */
20044 else
20046 cp_lexer_rollback_tokens (parser->lexer);
20047 pop_deferring_access_checks ();
20049 /* Add the context to the front of the free list. */
20050 context->next = cp_parser_context_free_list;
20051 cp_parser_context_free_list = context;
20053 return !error_occurred;
20056 /* Returns true if we are parsing tentatively and are not committed to
20057 this tentative parse. */
20059 static bool
20060 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
20062 return (cp_parser_parsing_tentatively (parser)
20063 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
20066 /* Returns nonzero iff an error has occurred during the most recent
20067 tentative parse. */
20069 static bool
20070 cp_parser_error_occurred (cp_parser* parser)
20072 return (cp_parser_parsing_tentatively (parser)
20073 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
20076 /* Returns nonzero if GNU extensions are allowed. */
20078 static bool
20079 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
20081 return parser->allow_gnu_extensions_p;
20084 /* Objective-C++ Productions */
20087 /* Parse an Objective-C expression, which feeds into a primary-expression
20088 above.
20090 objc-expression:
20091 objc-message-expression
20092 objc-string-literal
20093 objc-encode-expression
20094 objc-protocol-expression
20095 objc-selector-expression
20097 Returns a tree representation of the expression. */
20099 static tree
20100 cp_parser_objc_expression (cp_parser* parser)
20102 /* Try to figure out what kind of declaration is present. */
20103 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20105 switch (kwd->type)
20107 case CPP_OPEN_SQUARE:
20108 return cp_parser_objc_message_expression (parser);
20110 case CPP_OBJC_STRING:
20111 kwd = cp_lexer_consume_token (parser->lexer);
20112 return objc_build_string_object (kwd->u.value);
20114 case CPP_KEYWORD:
20115 switch (kwd->keyword)
20117 case RID_AT_ENCODE:
20118 return cp_parser_objc_encode_expression (parser);
20120 case RID_AT_PROTOCOL:
20121 return cp_parser_objc_protocol_expression (parser);
20123 case RID_AT_SELECTOR:
20124 return cp_parser_objc_selector_expression (parser);
20126 default:
20127 break;
20129 default:
20130 error_at (kwd->location,
20131 "misplaced %<@%D%> Objective-C++ construct",
20132 kwd->u.value);
20133 cp_parser_skip_to_end_of_block_or_statement (parser);
20136 return error_mark_node;
20139 /* Parse an Objective-C message expression.
20141 objc-message-expression:
20142 [ objc-message-receiver objc-message-args ]
20144 Returns a representation of an Objective-C message. */
20146 static tree
20147 cp_parser_objc_message_expression (cp_parser* parser)
20149 tree receiver, messageargs;
20151 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
20152 receiver = cp_parser_objc_message_receiver (parser);
20153 messageargs = cp_parser_objc_message_args (parser);
20154 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
20156 return objc_build_message_expr (build_tree_list (receiver, messageargs));
20159 /* Parse an objc-message-receiver.
20161 objc-message-receiver:
20162 expression
20163 simple-type-specifier
20165 Returns a representation of the type or expression. */
20167 static tree
20168 cp_parser_objc_message_receiver (cp_parser* parser)
20170 tree rcv;
20172 /* An Objective-C message receiver may be either (1) a type
20173 or (2) an expression. */
20174 cp_parser_parse_tentatively (parser);
20175 rcv = cp_parser_expression (parser, false, NULL);
20177 if (cp_parser_parse_definitely (parser))
20178 return rcv;
20180 rcv = cp_parser_simple_type_specifier (parser,
20181 /*decl_specs=*/NULL,
20182 CP_PARSER_FLAGS_NONE);
20184 return objc_get_class_reference (rcv);
20187 /* Parse the arguments and selectors comprising an Objective-C message.
20189 objc-message-args:
20190 objc-selector
20191 objc-selector-args
20192 objc-selector-args , objc-comma-args
20194 objc-selector-args:
20195 objc-selector [opt] : assignment-expression
20196 objc-selector-args objc-selector [opt] : assignment-expression
20198 objc-comma-args:
20199 assignment-expression
20200 objc-comma-args , assignment-expression
20202 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
20203 selector arguments and TREE_VALUE containing a list of comma
20204 arguments. */
20206 static tree
20207 cp_parser_objc_message_args (cp_parser* parser)
20209 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
20210 bool maybe_unary_selector_p = true;
20211 cp_token *token = cp_lexer_peek_token (parser->lexer);
20213 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
20215 tree selector = NULL_TREE, arg;
20217 if (token->type != CPP_COLON)
20218 selector = cp_parser_objc_selector (parser);
20220 /* Detect if we have a unary selector. */
20221 if (maybe_unary_selector_p
20222 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
20223 return build_tree_list (selector, NULL_TREE);
20225 maybe_unary_selector_p = false;
20226 cp_parser_require (parser, CPP_COLON, "%<:%>");
20227 arg = cp_parser_assignment_expression (parser, false, NULL);
20229 sel_args
20230 = chainon (sel_args,
20231 build_tree_list (selector, arg));
20233 token = cp_lexer_peek_token (parser->lexer);
20236 /* Handle non-selector arguments, if any. */
20237 while (token->type == CPP_COMMA)
20239 tree arg;
20241 cp_lexer_consume_token (parser->lexer);
20242 arg = cp_parser_assignment_expression (parser, false, NULL);
20244 addl_args
20245 = chainon (addl_args,
20246 build_tree_list (NULL_TREE, arg));
20248 token = cp_lexer_peek_token (parser->lexer);
20251 return build_tree_list (sel_args, addl_args);
20254 /* Parse an Objective-C encode expression.
20256 objc-encode-expression:
20257 @encode objc-typename
20259 Returns an encoded representation of the type argument. */
20261 static tree
20262 cp_parser_objc_encode_expression (cp_parser* parser)
20264 tree type;
20265 cp_token *token;
20267 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
20268 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20269 token = cp_lexer_peek_token (parser->lexer);
20270 type = complete_type (cp_parser_type_id (parser));
20271 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20273 if (!type)
20275 error_at (token->location,
20276 "%<@encode%> must specify a type as an argument");
20277 return error_mark_node;
20280 return objc_build_encode_expr (type);
20283 /* Parse an Objective-C @defs expression. */
20285 static tree
20286 cp_parser_objc_defs_expression (cp_parser *parser)
20288 tree name;
20290 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
20291 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20292 name = cp_parser_identifier (parser);
20293 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20295 return objc_get_class_ivars (name);
20298 /* Parse an Objective-C protocol expression.
20300 objc-protocol-expression:
20301 @protocol ( identifier )
20303 Returns a representation of the protocol expression. */
20305 static tree
20306 cp_parser_objc_protocol_expression (cp_parser* parser)
20308 tree proto;
20310 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
20311 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20312 proto = cp_parser_identifier (parser);
20313 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20315 return objc_build_protocol_expr (proto);
20318 /* Parse an Objective-C selector expression.
20320 objc-selector-expression:
20321 @selector ( objc-method-signature )
20323 objc-method-signature:
20324 objc-selector
20325 objc-selector-seq
20327 objc-selector-seq:
20328 objc-selector :
20329 objc-selector-seq objc-selector :
20331 Returns a representation of the method selector. */
20333 static tree
20334 cp_parser_objc_selector_expression (cp_parser* parser)
20336 tree sel_seq = NULL_TREE;
20337 bool maybe_unary_selector_p = true;
20338 cp_token *token;
20339 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
20341 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
20342 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20343 token = cp_lexer_peek_token (parser->lexer);
20345 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
20346 || token->type == CPP_SCOPE)
20348 tree selector = NULL_TREE;
20350 if (token->type != CPP_COLON
20351 || token->type == CPP_SCOPE)
20352 selector = cp_parser_objc_selector (parser);
20354 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
20355 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
20357 /* Detect if we have a unary selector. */
20358 if (maybe_unary_selector_p)
20360 sel_seq = selector;
20361 goto finish_selector;
20363 else
20365 cp_parser_error (parser, "expected %<:%>");
20368 maybe_unary_selector_p = false;
20369 token = cp_lexer_consume_token (parser->lexer);
20371 if (token->type == CPP_SCOPE)
20373 sel_seq
20374 = chainon (sel_seq,
20375 build_tree_list (selector, NULL_TREE));
20376 sel_seq
20377 = chainon (sel_seq,
20378 build_tree_list (NULL_TREE, NULL_TREE));
20380 else
20381 sel_seq
20382 = chainon (sel_seq,
20383 build_tree_list (selector, NULL_TREE));
20385 token = cp_lexer_peek_token (parser->lexer);
20388 finish_selector:
20389 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20391 return objc_build_selector_expr (loc, sel_seq);
20394 /* Parse a list of identifiers.
20396 objc-identifier-list:
20397 identifier
20398 objc-identifier-list , identifier
20400 Returns a TREE_LIST of identifier nodes. */
20402 static tree
20403 cp_parser_objc_identifier_list (cp_parser* parser)
20405 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
20406 cp_token *sep = cp_lexer_peek_token (parser->lexer);
20408 while (sep->type == CPP_COMMA)
20410 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
20411 list = chainon (list,
20412 build_tree_list (NULL_TREE,
20413 cp_parser_identifier (parser)));
20414 sep = cp_lexer_peek_token (parser->lexer);
20417 return list;
20420 /* Parse an Objective-C alias declaration.
20422 objc-alias-declaration:
20423 @compatibility_alias identifier identifier ;
20425 This function registers the alias mapping with the Objective-C front end.
20426 It returns nothing. */
20428 static void
20429 cp_parser_objc_alias_declaration (cp_parser* parser)
20431 tree alias, orig;
20433 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
20434 alias = cp_parser_identifier (parser);
20435 orig = cp_parser_identifier (parser);
20436 objc_declare_alias (alias, orig);
20437 cp_parser_consume_semicolon_at_end_of_statement (parser);
20440 /* Parse an Objective-C class forward-declaration.
20442 objc-class-declaration:
20443 @class objc-identifier-list ;
20445 The function registers the forward declarations with the Objective-C
20446 front end. It returns nothing. */
20448 static void
20449 cp_parser_objc_class_declaration (cp_parser* parser)
20451 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
20452 objc_declare_class (cp_parser_objc_identifier_list (parser));
20453 cp_parser_consume_semicolon_at_end_of_statement (parser);
20456 /* Parse a list of Objective-C protocol references.
20458 objc-protocol-refs-opt:
20459 objc-protocol-refs [opt]
20461 objc-protocol-refs:
20462 < objc-identifier-list >
20464 Returns a TREE_LIST of identifiers, if any. */
20466 static tree
20467 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
20469 tree protorefs = NULL_TREE;
20471 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
20473 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
20474 protorefs = cp_parser_objc_identifier_list (parser);
20475 cp_parser_require (parser, CPP_GREATER, "%<>%>");
20478 return protorefs;
20481 /* Parse a Objective-C visibility specification. */
20483 static void
20484 cp_parser_objc_visibility_spec (cp_parser* parser)
20486 cp_token *vis = cp_lexer_peek_token (parser->lexer);
20488 switch (vis->keyword)
20490 case RID_AT_PRIVATE:
20491 objc_set_visibility (2);
20492 break;
20493 case RID_AT_PROTECTED:
20494 objc_set_visibility (0);
20495 break;
20496 case RID_AT_PUBLIC:
20497 objc_set_visibility (1);
20498 break;
20499 default:
20500 return;
20503 /* Eat '@private'/'@protected'/'@public'. */
20504 cp_lexer_consume_token (parser->lexer);
20507 /* Parse an Objective-C method type. */
20509 static void
20510 cp_parser_objc_method_type (cp_parser* parser)
20512 objc_set_method_type
20513 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
20514 ? PLUS_EXPR
20515 : MINUS_EXPR);
20518 /* Parse an Objective-C protocol qualifier. */
20520 static tree
20521 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
20523 tree quals = NULL_TREE, node;
20524 cp_token *token = cp_lexer_peek_token (parser->lexer);
20526 node = token->u.value;
20528 while (node && TREE_CODE (node) == IDENTIFIER_NODE
20529 && (node == ridpointers [(int) RID_IN]
20530 || node == ridpointers [(int) RID_OUT]
20531 || node == ridpointers [(int) RID_INOUT]
20532 || node == ridpointers [(int) RID_BYCOPY]
20533 || node == ridpointers [(int) RID_BYREF]
20534 || node == ridpointers [(int) RID_ONEWAY]))
20536 quals = tree_cons (NULL_TREE, node, quals);
20537 cp_lexer_consume_token (parser->lexer);
20538 token = cp_lexer_peek_token (parser->lexer);
20539 node = token->u.value;
20542 return quals;
20545 /* Parse an Objective-C typename. */
20547 static tree
20548 cp_parser_objc_typename (cp_parser* parser)
20550 tree type_name = NULL_TREE;
20552 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20554 tree proto_quals, cp_type = NULL_TREE;
20556 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
20557 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
20559 /* An ObjC type name may consist of just protocol qualifiers, in which
20560 case the type shall default to 'id'. */
20561 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
20562 cp_type = cp_parser_type_id (parser);
20564 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20565 type_name = build_tree_list (proto_quals, cp_type);
20568 return type_name;
20571 /* Check to see if TYPE refers to an Objective-C selector name. */
20573 static bool
20574 cp_parser_objc_selector_p (enum cpp_ttype type)
20576 return (type == CPP_NAME || type == CPP_KEYWORD
20577 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
20578 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
20579 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
20580 || type == CPP_XOR || type == CPP_XOR_EQ);
20583 /* Parse an Objective-C selector. */
20585 static tree
20586 cp_parser_objc_selector (cp_parser* parser)
20588 cp_token *token = cp_lexer_consume_token (parser->lexer);
20590 if (!cp_parser_objc_selector_p (token->type))
20592 error_at (token->location, "invalid Objective-C++ selector name");
20593 return error_mark_node;
20596 /* C++ operator names are allowed to appear in ObjC selectors. */
20597 switch (token->type)
20599 case CPP_AND_AND: return get_identifier ("and");
20600 case CPP_AND_EQ: return get_identifier ("and_eq");
20601 case CPP_AND: return get_identifier ("bitand");
20602 case CPP_OR: return get_identifier ("bitor");
20603 case CPP_COMPL: return get_identifier ("compl");
20604 case CPP_NOT: return get_identifier ("not");
20605 case CPP_NOT_EQ: return get_identifier ("not_eq");
20606 case CPP_OR_OR: return get_identifier ("or");
20607 case CPP_OR_EQ: return get_identifier ("or_eq");
20608 case CPP_XOR: return get_identifier ("xor");
20609 case CPP_XOR_EQ: return get_identifier ("xor_eq");
20610 default: return token->u.value;
20614 /* Parse an Objective-C params list. */
20616 static tree
20617 cp_parser_objc_method_keyword_params (cp_parser* parser)
20619 tree params = NULL_TREE;
20620 bool maybe_unary_selector_p = true;
20621 cp_token *token = cp_lexer_peek_token (parser->lexer);
20623 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
20625 tree selector = NULL_TREE, type_name, identifier;
20627 if (token->type != CPP_COLON)
20628 selector = cp_parser_objc_selector (parser);
20630 /* Detect if we have a unary selector. */
20631 if (maybe_unary_selector_p
20632 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
20633 return selector;
20635 maybe_unary_selector_p = false;
20636 cp_parser_require (parser, CPP_COLON, "%<:%>");
20637 type_name = cp_parser_objc_typename (parser);
20638 identifier = cp_parser_identifier (parser);
20640 params
20641 = chainon (params,
20642 objc_build_keyword_decl (selector,
20643 type_name,
20644 identifier));
20646 token = cp_lexer_peek_token (parser->lexer);
20649 return params;
20652 /* Parse the non-keyword Objective-C params. */
20654 static tree
20655 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
20657 tree params = make_node (TREE_LIST);
20658 cp_token *token = cp_lexer_peek_token (parser->lexer);
20659 *ellipsisp = false; /* Initially, assume no ellipsis. */
20661 while (token->type == CPP_COMMA)
20663 cp_parameter_declarator *parmdecl;
20664 tree parm;
20666 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
20667 token = cp_lexer_peek_token (parser->lexer);
20669 if (token->type == CPP_ELLIPSIS)
20671 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
20672 *ellipsisp = true;
20673 break;
20676 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20677 parm = grokdeclarator (parmdecl->declarator,
20678 &parmdecl->decl_specifiers,
20679 PARM, /*initialized=*/0,
20680 /*attrlist=*/NULL);
20682 chainon (params, build_tree_list (NULL_TREE, parm));
20683 token = cp_lexer_peek_token (parser->lexer);
20686 return params;
20689 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
20691 static void
20692 cp_parser_objc_interstitial_code (cp_parser* parser)
20694 cp_token *token = cp_lexer_peek_token (parser->lexer);
20696 /* If the next token is `extern' and the following token is a string
20697 literal, then we have a linkage specification. */
20698 if (token->keyword == RID_EXTERN
20699 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
20700 cp_parser_linkage_specification (parser);
20701 /* Handle #pragma, if any. */
20702 else if (token->type == CPP_PRAGMA)
20703 cp_parser_pragma (parser, pragma_external);
20704 /* Allow stray semicolons. */
20705 else if (token->type == CPP_SEMICOLON)
20706 cp_lexer_consume_token (parser->lexer);
20707 /* Finally, try to parse a block-declaration, or a function-definition. */
20708 else
20709 cp_parser_block_declaration (parser, /*statement_p=*/false);
20712 /* Parse a method signature. */
20714 static tree
20715 cp_parser_objc_method_signature (cp_parser* parser)
20717 tree rettype, kwdparms, optparms;
20718 bool ellipsis = false;
20720 cp_parser_objc_method_type (parser);
20721 rettype = cp_parser_objc_typename (parser);
20722 kwdparms = cp_parser_objc_method_keyword_params (parser);
20723 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
20725 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
20728 /* Pars an Objective-C method prototype list. */
20730 static void
20731 cp_parser_objc_method_prototype_list (cp_parser* parser)
20733 cp_token *token = cp_lexer_peek_token (parser->lexer);
20735 while (token->keyword != RID_AT_END)
20737 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
20739 objc_add_method_declaration
20740 (cp_parser_objc_method_signature (parser));
20741 cp_parser_consume_semicolon_at_end_of_statement (parser);
20743 else
20744 /* Allow for interspersed non-ObjC++ code. */
20745 cp_parser_objc_interstitial_code (parser);
20747 token = cp_lexer_peek_token (parser->lexer);
20750 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
20751 objc_finish_interface ();
20754 /* Parse an Objective-C method definition list. */
20756 static void
20757 cp_parser_objc_method_definition_list (cp_parser* parser)
20759 cp_token *token = cp_lexer_peek_token (parser->lexer);
20761 while (token->keyword != RID_AT_END)
20763 tree meth;
20765 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
20767 push_deferring_access_checks (dk_deferred);
20768 objc_start_method_definition
20769 (cp_parser_objc_method_signature (parser));
20771 /* For historical reasons, we accept an optional semicolon. */
20772 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20773 cp_lexer_consume_token (parser->lexer);
20775 perform_deferred_access_checks ();
20776 stop_deferring_access_checks ();
20777 meth = cp_parser_function_definition_after_declarator (parser,
20778 false);
20779 pop_deferring_access_checks ();
20780 objc_finish_method_definition (meth);
20782 else
20783 /* Allow for interspersed non-ObjC++ code. */
20784 cp_parser_objc_interstitial_code (parser);
20786 token = cp_lexer_peek_token (parser->lexer);
20789 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
20790 objc_finish_implementation ();
20793 /* Parse Objective-C ivars. */
20795 static void
20796 cp_parser_objc_class_ivars (cp_parser* parser)
20798 cp_token *token = cp_lexer_peek_token (parser->lexer);
20800 if (token->type != CPP_OPEN_BRACE)
20801 return; /* No ivars specified. */
20803 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
20804 token = cp_lexer_peek_token (parser->lexer);
20806 while (token->type != CPP_CLOSE_BRACE)
20808 cp_decl_specifier_seq declspecs;
20809 int decl_class_or_enum_p;
20810 tree prefix_attributes;
20812 cp_parser_objc_visibility_spec (parser);
20814 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
20815 break;
20817 cp_parser_decl_specifier_seq (parser,
20818 CP_PARSER_FLAGS_OPTIONAL,
20819 &declspecs,
20820 &decl_class_or_enum_p);
20821 prefix_attributes = declspecs.attributes;
20822 declspecs.attributes = NULL_TREE;
20824 /* Keep going until we hit the `;' at the end of the
20825 declaration. */
20826 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20828 tree width = NULL_TREE, attributes, first_attribute, decl;
20829 cp_declarator *declarator = NULL;
20830 int ctor_dtor_or_conv_p;
20832 /* Check for a (possibly unnamed) bitfield declaration. */
20833 token = cp_lexer_peek_token (parser->lexer);
20834 if (token->type == CPP_COLON)
20835 goto eat_colon;
20837 if (token->type == CPP_NAME
20838 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
20839 == CPP_COLON))
20841 /* Get the name of the bitfield. */
20842 declarator = make_id_declarator (NULL_TREE,
20843 cp_parser_identifier (parser),
20844 sfk_none);
20846 eat_colon:
20847 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
20848 /* Get the width of the bitfield. */
20849 width
20850 = cp_parser_constant_expression (parser,
20851 /*allow_non_constant=*/false,
20852 NULL);
20854 else
20856 /* Parse the declarator. */
20857 declarator
20858 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20859 &ctor_dtor_or_conv_p,
20860 /*parenthesized_p=*/NULL,
20861 /*member_p=*/false);
20864 /* Look for attributes that apply to the ivar. */
20865 attributes = cp_parser_attributes_opt (parser);
20866 /* Remember which attributes are prefix attributes and
20867 which are not. */
20868 first_attribute = attributes;
20869 /* Combine the attributes. */
20870 attributes = chainon (prefix_attributes, attributes);
20872 if (width)
20873 /* Create the bitfield declaration. */
20874 decl = grokbitfield (declarator, &declspecs,
20875 width,
20876 attributes);
20877 else
20878 decl = grokfield (declarator, &declspecs,
20879 NULL_TREE, /*init_const_expr_p=*/false,
20880 NULL_TREE, attributes);
20882 /* Add the instance variable. */
20883 objc_add_instance_variable (decl);
20885 /* Reset PREFIX_ATTRIBUTES. */
20886 while (attributes && TREE_CHAIN (attributes) != first_attribute)
20887 attributes = TREE_CHAIN (attributes);
20888 if (attributes)
20889 TREE_CHAIN (attributes) = NULL_TREE;
20891 token = cp_lexer_peek_token (parser->lexer);
20893 if (token->type == CPP_COMMA)
20895 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
20896 continue;
20898 break;
20901 cp_parser_consume_semicolon_at_end_of_statement (parser);
20902 token = cp_lexer_peek_token (parser->lexer);
20905 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
20906 /* For historical reasons, we accept an optional semicolon. */
20907 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20908 cp_lexer_consume_token (parser->lexer);
20911 /* Parse an Objective-C protocol declaration. */
20913 static void
20914 cp_parser_objc_protocol_declaration (cp_parser* parser)
20916 tree proto, protorefs;
20917 cp_token *tok;
20919 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
20920 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
20922 tok = cp_lexer_peek_token (parser->lexer);
20923 error_at (tok->location, "identifier expected after %<@protocol%>");
20924 goto finish;
20927 /* See if we have a forward declaration or a definition. */
20928 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
20930 /* Try a forward declaration first. */
20931 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
20933 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
20934 finish:
20935 cp_parser_consume_semicolon_at_end_of_statement (parser);
20938 /* Ok, we got a full-fledged definition (or at least should). */
20939 else
20941 proto = cp_parser_identifier (parser);
20942 protorefs = cp_parser_objc_protocol_refs_opt (parser);
20943 objc_start_protocol (proto, protorefs);
20944 cp_parser_objc_method_prototype_list (parser);
20948 /* Parse an Objective-C superclass or category. */
20950 static void
20951 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
20952 tree *categ)
20954 cp_token *next = cp_lexer_peek_token (parser->lexer);
20956 *super = *categ = NULL_TREE;
20957 if (next->type == CPP_COLON)
20959 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
20960 *super = cp_parser_identifier (parser);
20962 else if (next->type == CPP_OPEN_PAREN)
20964 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
20965 *categ = cp_parser_identifier (parser);
20966 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20970 /* Parse an Objective-C class interface. */
20972 static void
20973 cp_parser_objc_class_interface (cp_parser* parser)
20975 tree name, super, categ, protos;
20977 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
20978 name = cp_parser_identifier (parser);
20979 cp_parser_objc_superclass_or_category (parser, &super, &categ);
20980 protos = cp_parser_objc_protocol_refs_opt (parser);
20982 /* We have either a class or a category on our hands. */
20983 if (categ)
20984 objc_start_category_interface (name, categ, protos);
20985 else
20987 objc_start_class_interface (name, super, protos);
20988 /* Handle instance variable declarations, if any. */
20989 cp_parser_objc_class_ivars (parser);
20990 objc_continue_interface ();
20993 cp_parser_objc_method_prototype_list (parser);
20996 /* Parse an Objective-C class implementation. */
20998 static void
20999 cp_parser_objc_class_implementation (cp_parser* parser)
21001 tree name, super, categ;
21003 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
21004 name = cp_parser_identifier (parser);
21005 cp_parser_objc_superclass_or_category (parser, &super, &categ);
21007 /* We have either a class or a category on our hands. */
21008 if (categ)
21009 objc_start_category_implementation (name, categ);
21010 else
21012 objc_start_class_implementation (name, super);
21013 /* Handle instance variable declarations, if any. */
21014 cp_parser_objc_class_ivars (parser);
21015 objc_continue_implementation ();
21018 cp_parser_objc_method_definition_list (parser);
21021 /* Consume the @end token and finish off the implementation. */
21023 static void
21024 cp_parser_objc_end_implementation (cp_parser* parser)
21026 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
21027 objc_finish_implementation ();
21030 /* Parse an Objective-C declaration. */
21032 static void
21033 cp_parser_objc_declaration (cp_parser* parser)
21035 /* Try to figure out what kind of declaration is present. */
21036 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21038 switch (kwd->keyword)
21040 case RID_AT_ALIAS:
21041 cp_parser_objc_alias_declaration (parser);
21042 break;
21043 case RID_AT_CLASS:
21044 cp_parser_objc_class_declaration (parser);
21045 break;
21046 case RID_AT_PROTOCOL:
21047 cp_parser_objc_protocol_declaration (parser);
21048 break;
21049 case RID_AT_INTERFACE:
21050 cp_parser_objc_class_interface (parser);
21051 break;
21052 case RID_AT_IMPLEMENTATION:
21053 cp_parser_objc_class_implementation (parser);
21054 break;
21055 case RID_AT_END:
21056 cp_parser_objc_end_implementation (parser);
21057 break;
21058 default:
21059 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
21060 kwd->u.value);
21061 cp_parser_skip_to_end_of_block_or_statement (parser);
21065 /* Parse an Objective-C try-catch-finally statement.
21067 objc-try-catch-finally-stmt:
21068 @try compound-statement objc-catch-clause-seq [opt]
21069 objc-finally-clause [opt]
21071 objc-catch-clause-seq:
21072 objc-catch-clause objc-catch-clause-seq [opt]
21074 objc-catch-clause:
21075 @catch ( exception-declaration ) compound-statement
21077 objc-finally-clause
21078 @finally compound-statement
21080 Returns NULL_TREE. */
21082 static tree
21083 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
21084 location_t location;
21085 tree stmt;
21087 cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
21088 location = cp_lexer_peek_token (parser->lexer)->location;
21089 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
21090 node, lest it get absorbed into the surrounding block. */
21091 stmt = push_stmt_list ();
21092 cp_parser_compound_statement (parser, NULL, false);
21093 objc_begin_try_stmt (location, pop_stmt_list (stmt));
21095 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
21097 cp_parameter_declarator *parmdecl;
21098 tree parm;
21100 cp_lexer_consume_token (parser->lexer);
21101 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
21102 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
21103 parm = grokdeclarator (parmdecl->declarator,
21104 &parmdecl->decl_specifiers,
21105 PARM, /*initialized=*/0,
21106 /*attrlist=*/NULL);
21107 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
21108 objc_begin_catch_clause (parm);
21109 cp_parser_compound_statement (parser, NULL, false);
21110 objc_finish_catch_clause ();
21113 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
21115 cp_lexer_consume_token (parser->lexer);
21116 location = cp_lexer_peek_token (parser->lexer)->location;
21117 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
21118 node, lest it get absorbed into the surrounding block. */
21119 stmt = push_stmt_list ();
21120 cp_parser_compound_statement (parser, NULL, false);
21121 objc_build_finally_clause (location, pop_stmt_list (stmt));
21124 return objc_finish_try_stmt ();
21127 /* Parse an Objective-C synchronized statement.
21129 objc-synchronized-stmt:
21130 @synchronized ( expression ) compound-statement
21132 Returns NULL_TREE. */
21134 static tree
21135 cp_parser_objc_synchronized_statement (cp_parser *parser) {
21136 location_t location;
21137 tree lock, stmt;
21139 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
21141 location = cp_lexer_peek_token (parser->lexer)->location;
21142 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
21143 lock = cp_parser_expression (parser, false, NULL);
21144 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
21146 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
21147 node, lest it get absorbed into the surrounding block. */
21148 stmt = push_stmt_list ();
21149 cp_parser_compound_statement (parser, NULL, false);
21151 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
21154 /* Parse an Objective-C throw statement.
21156 objc-throw-stmt:
21157 @throw assignment-expression [opt] ;
21159 Returns a constructed '@throw' statement. */
21161 static tree
21162 cp_parser_objc_throw_statement (cp_parser *parser) {
21163 tree expr = NULL_TREE;
21164 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21166 cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
21168 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21169 expr = cp_parser_assignment_expression (parser, false, NULL);
21171 cp_parser_consume_semicolon_at_end_of_statement (parser);
21173 return objc_build_throw_stmt (loc, expr);
21176 /* Parse an Objective-C statement. */
21178 static tree
21179 cp_parser_objc_statement (cp_parser * parser) {
21180 /* Try to figure out what kind of declaration is present. */
21181 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21183 switch (kwd->keyword)
21185 case RID_AT_TRY:
21186 return cp_parser_objc_try_catch_finally_statement (parser);
21187 case RID_AT_SYNCHRONIZED:
21188 return cp_parser_objc_synchronized_statement (parser);
21189 case RID_AT_THROW:
21190 return cp_parser_objc_throw_statement (parser);
21191 default:
21192 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
21193 kwd->u.value);
21194 cp_parser_skip_to_end_of_block_or_statement (parser);
21197 return error_mark_node;
21200 /* OpenMP 2.5 parsing routines. */
21202 /* Returns name of the next clause.
21203 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
21204 the token is not consumed. Otherwise appropriate pragma_omp_clause is
21205 returned and the token is consumed. */
21207 static pragma_omp_clause
21208 cp_parser_omp_clause_name (cp_parser *parser)
21210 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
21212 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
21213 result = PRAGMA_OMP_CLAUSE_IF;
21214 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
21215 result = PRAGMA_OMP_CLAUSE_DEFAULT;
21216 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
21217 result = PRAGMA_OMP_CLAUSE_PRIVATE;
21218 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21220 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21221 const char *p = IDENTIFIER_POINTER (id);
21223 switch (p[0])
21225 case 'c':
21226 if (!strcmp ("collapse", p))
21227 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
21228 else if (!strcmp ("copyin", p))
21229 result = PRAGMA_OMP_CLAUSE_COPYIN;
21230 else if (!strcmp ("copyprivate", p))
21231 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
21232 break;
21233 case 'f':
21234 if (!strcmp ("firstprivate", p))
21235 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
21236 break;
21237 case 'l':
21238 if (!strcmp ("lastprivate", p))
21239 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
21240 break;
21241 case 'n':
21242 if (!strcmp ("nowait", p))
21243 result = PRAGMA_OMP_CLAUSE_NOWAIT;
21244 else if (!strcmp ("num_threads", p))
21245 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
21246 break;
21247 case 'o':
21248 if (!strcmp ("ordered", p))
21249 result = PRAGMA_OMP_CLAUSE_ORDERED;
21250 break;
21251 case 'r':
21252 if (!strcmp ("reduction", p))
21253 result = PRAGMA_OMP_CLAUSE_REDUCTION;
21254 break;
21255 case 's':
21256 if (!strcmp ("schedule", p))
21257 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
21258 else if (!strcmp ("shared", p))
21259 result = PRAGMA_OMP_CLAUSE_SHARED;
21260 break;
21261 case 'u':
21262 if (!strcmp ("untied", p))
21263 result = PRAGMA_OMP_CLAUSE_UNTIED;
21264 break;
21268 if (result != PRAGMA_OMP_CLAUSE_NONE)
21269 cp_lexer_consume_token (parser->lexer);
21271 return result;
21274 /* Validate that a clause of the given type does not already exist. */
21276 static void
21277 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
21278 const char *name, location_t location)
21280 tree c;
21282 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21283 if (OMP_CLAUSE_CODE (c) == code)
21285 error_at (location, "too many %qs clauses", name);
21286 break;
21290 /* OpenMP 2.5:
21291 variable-list:
21292 identifier
21293 variable-list , identifier
21295 In addition, we match a closing parenthesis. An opening parenthesis
21296 will have been consumed by the caller.
21298 If KIND is nonzero, create the appropriate node and install the decl
21299 in OMP_CLAUSE_DECL and add the node to the head of the list.
21301 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
21302 return the list created. */
21304 static tree
21305 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
21306 tree list)
21308 cp_token *token;
21309 while (1)
21311 tree name, decl;
21313 token = cp_lexer_peek_token (parser->lexer);
21314 name = cp_parser_id_expression (parser, /*template_p=*/false,
21315 /*check_dependency_p=*/true,
21316 /*template_p=*/NULL,
21317 /*declarator_p=*/false,
21318 /*optional_p=*/false);
21319 if (name == error_mark_node)
21320 goto skip_comma;
21322 decl = cp_parser_lookup_name_simple (parser, name, token->location);
21323 if (decl == error_mark_node)
21324 cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
21325 else if (kind != 0)
21327 tree u = build_omp_clause (token->location, kind);
21328 OMP_CLAUSE_DECL (u) = decl;
21329 OMP_CLAUSE_CHAIN (u) = list;
21330 list = u;
21332 else
21333 list = tree_cons (decl, NULL_TREE, list);
21335 get_comma:
21336 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21337 break;
21338 cp_lexer_consume_token (parser->lexer);
21341 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21343 int ending;
21345 /* Try to resync to an unnested comma. Copied from
21346 cp_parser_parenthesized_expression_list. */
21347 skip_comma:
21348 ending = cp_parser_skip_to_closing_parenthesis (parser,
21349 /*recovering=*/true,
21350 /*or_comma=*/true,
21351 /*consume_paren=*/true);
21352 if (ending < 0)
21353 goto get_comma;
21356 return list;
21359 /* Similarly, but expect leading and trailing parenthesis. This is a very
21360 common case for omp clauses. */
21362 static tree
21363 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
21365 if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21366 return cp_parser_omp_var_list_no_open (parser, kind, list);
21367 return list;
21370 /* OpenMP 3.0:
21371 collapse ( constant-expression ) */
21373 static tree
21374 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
21376 tree c, num;
21377 location_t loc;
21378 HOST_WIDE_INT n;
21380 loc = cp_lexer_peek_token (parser->lexer)->location;
21381 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21382 return list;
21384 num = cp_parser_constant_expression (parser, false, NULL);
21386 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21387 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21388 /*or_comma=*/false,
21389 /*consume_paren=*/true);
21391 if (num == error_mark_node)
21392 return list;
21393 num = fold_non_dependent_expr (num);
21394 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
21395 || !host_integerp (num, 0)
21396 || (n = tree_low_cst (num, 0)) <= 0
21397 || (int) n != n)
21399 error_at (loc, "collapse argument needs positive constant integer expression");
21400 return list;
21403 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
21404 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
21405 OMP_CLAUSE_CHAIN (c) = list;
21406 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
21408 return c;
21411 /* OpenMP 2.5:
21412 default ( shared | none ) */
21414 static tree
21415 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
21417 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
21418 tree c;
21420 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21421 return list;
21422 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21424 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21425 const char *p = IDENTIFIER_POINTER (id);
21427 switch (p[0])
21429 case 'n':
21430 if (strcmp ("none", p) != 0)
21431 goto invalid_kind;
21432 kind = OMP_CLAUSE_DEFAULT_NONE;
21433 break;
21435 case 's':
21436 if (strcmp ("shared", p) != 0)
21437 goto invalid_kind;
21438 kind = OMP_CLAUSE_DEFAULT_SHARED;
21439 break;
21441 default:
21442 goto invalid_kind;
21445 cp_lexer_consume_token (parser->lexer);
21447 else
21449 invalid_kind:
21450 cp_parser_error (parser, "expected %<none%> or %<shared%>");
21453 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21454 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21455 /*or_comma=*/false,
21456 /*consume_paren=*/true);
21458 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
21459 return list;
21461 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
21462 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
21463 OMP_CLAUSE_CHAIN (c) = list;
21464 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
21466 return c;
21469 /* OpenMP 2.5:
21470 if ( expression ) */
21472 static tree
21473 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
21475 tree t, c;
21477 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21478 return list;
21480 t = cp_parser_condition (parser);
21482 if (t == error_mark_node
21483 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21484 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21485 /*or_comma=*/false,
21486 /*consume_paren=*/true);
21488 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
21490 c = build_omp_clause (location, OMP_CLAUSE_IF);
21491 OMP_CLAUSE_IF_EXPR (c) = t;
21492 OMP_CLAUSE_CHAIN (c) = list;
21494 return c;
21497 /* OpenMP 2.5:
21498 nowait */
21500 static tree
21501 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
21502 tree list, location_t location)
21504 tree c;
21506 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
21508 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
21509 OMP_CLAUSE_CHAIN (c) = list;
21510 return c;
21513 /* OpenMP 2.5:
21514 num_threads ( expression ) */
21516 static tree
21517 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
21518 location_t location)
21520 tree t, c;
21522 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21523 return list;
21525 t = cp_parser_expression (parser, false, NULL);
21527 if (t == error_mark_node
21528 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21529 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21530 /*or_comma=*/false,
21531 /*consume_paren=*/true);
21533 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
21534 "num_threads", location);
21536 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
21537 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
21538 OMP_CLAUSE_CHAIN (c) = list;
21540 return c;
21543 /* OpenMP 2.5:
21544 ordered */
21546 static tree
21547 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
21548 tree list, location_t location)
21550 tree c;
21552 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
21553 "ordered", location);
21555 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
21556 OMP_CLAUSE_CHAIN (c) = list;
21557 return c;
21560 /* OpenMP 2.5:
21561 reduction ( reduction-operator : variable-list )
21563 reduction-operator:
21564 One of: + * - & ^ | && || */
21566 static tree
21567 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
21569 enum tree_code code;
21570 tree nlist, c;
21572 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21573 return list;
21575 switch (cp_lexer_peek_token (parser->lexer)->type)
21577 case CPP_PLUS:
21578 code = PLUS_EXPR;
21579 break;
21580 case CPP_MULT:
21581 code = MULT_EXPR;
21582 break;
21583 case CPP_MINUS:
21584 code = MINUS_EXPR;
21585 break;
21586 case CPP_AND:
21587 code = BIT_AND_EXPR;
21588 break;
21589 case CPP_XOR:
21590 code = BIT_XOR_EXPR;
21591 break;
21592 case CPP_OR:
21593 code = BIT_IOR_EXPR;
21594 break;
21595 case CPP_AND_AND:
21596 code = TRUTH_ANDIF_EXPR;
21597 break;
21598 case CPP_OR_OR:
21599 code = TRUTH_ORIF_EXPR;
21600 break;
21601 default:
21602 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
21603 "%<|%>, %<&&%>, or %<||%>");
21604 resync_fail:
21605 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21606 /*or_comma=*/false,
21607 /*consume_paren=*/true);
21608 return list;
21610 cp_lexer_consume_token (parser->lexer);
21612 if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
21613 goto resync_fail;
21615 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
21616 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
21617 OMP_CLAUSE_REDUCTION_CODE (c) = code;
21619 return nlist;
21622 /* OpenMP 2.5:
21623 schedule ( schedule-kind )
21624 schedule ( schedule-kind , expression )
21626 schedule-kind:
21627 static | dynamic | guided | runtime | auto */
21629 static tree
21630 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
21632 tree c, t;
21634 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21635 return list;
21637 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
21639 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21641 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21642 const char *p = IDENTIFIER_POINTER (id);
21644 switch (p[0])
21646 case 'd':
21647 if (strcmp ("dynamic", p) != 0)
21648 goto invalid_kind;
21649 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
21650 break;
21652 case 'g':
21653 if (strcmp ("guided", p) != 0)
21654 goto invalid_kind;
21655 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
21656 break;
21658 case 'r':
21659 if (strcmp ("runtime", p) != 0)
21660 goto invalid_kind;
21661 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
21662 break;
21664 default:
21665 goto invalid_kind;
21668 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
21669 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
21670 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
21671 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
21672 else
21673 goto invalid_kind;
21674 cp_lexer_consume_token (parser->lexer);
21676 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21678 cp_token *token;
21679 cp_lexer_consume_token (parser->lexer);
21681 token = cp_lexer_peek_token (parser->lexer);
21682 t = cp_parser_assignment_expression (parser, false, NULL);
21684 if (t == error_mark_node)
21685 goto resync_fail;
21686 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
21687 error_at (token->location, "schedule %<runtime%> does not take "
21688 "a %<chunk_size%> parameter");
21689 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
21690 error_at (token->location, "schedule %<auto%> does not take "
21691 "a %<chunk_size%> parameter");
21692 else
21693 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
21695 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21696 goto resync_fail;
21698 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
21699 goto resync_fail;
21701 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
21702 OMP_CLAUSE_CHAIN (c) = list;
21703 return c;
21705 invalid_kind:
21706 cp_parser_error (parser, "invalid schedule kind");
21707 resync_fail:
21708 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21709 /*or_comma=*/false,
21710 /*consume_paren=*/true);
21711 return list;
21714 /* OpenMP 3.0:
21715 untied */
21717 static tree
21718 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
21719 tree list, location_t location)
21721 tree c;
21723 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
21725 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
21726 OMP_CLAUSE_CHAIN (c) = list;
21727 return c;
21730 /* Parse all OpenMP clauses. The set clauses allowed by the directive
21731 is a bitmask in MASK. Return the list of clauses found; the result
21732 of clause default goes in *pdefault. */
21734 static tree
21735 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
21736 const char *where, cp_token *pragma_tok)
21738 tree clauses = NULL;
21739 bool first = true;
21740 cp_token *token = NULL;
21742 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
21744 pragma_omp_clause c_kind;
21745 const char *c_name;
21746 tree prev = clauses;
21748 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21749 cp_lexer_consume_token (parser->lexer);
21751 token = cp_lexer_peek_token (parser->lexer);
21752 c_kind = cp_parser_omp_clause_name (parser);
21753 first = false;
21755 switch (c_kind)
21757 case PRAGMA_OMP_CLAUSE_COLLAPSE:
21758 clauses = cp_parser_omp_clause_collapse (parser, clauses,
21759 token->location);
21760 c_name = "collapse";
21761 break;
21762 case PRAGMA_OMP_CLAUSE_COPYIN:
21763 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
21764 c_name = "copyin";
21765 break;
21766 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
21767 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
21768 clauses);
21769 c_name = "copyprivate";
21770 break;
21771 case PRAGMA_OMP_CLAUSE_DEFAULT:
21772 clauses = cp_parser_omp_clause_default (parser, clauses,
21773 token->location);
21774 c_name = "default";
21775 break;
21776 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
21777 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
21778 clauses);
21779 c_name = "firstprivate";
21780 break;
21781 case PRAGMA_OMP_CLAUSE_IF:
21782 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
21783 c_name = "if";
21784 break;
21785 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
21786 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
21787 clauses);
21788 c_name = "lastprivate";
21789 break;
21790 case PRAGMA_OMP_CLAUSE_NOWAIT:
21791 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
21792 c_name = "nowait";
21793 break;
21794 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
21795 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
21796 token->location);
21797 c_name = "num_threads";
21798 break;
21799 case PRAGMA_OMP_CLAUSE_ORDERED:
21800 clauses = cp_parser_omp_clause_ordered (parser, clauses,
21801 token->location);
21802 c_name = "ordered";
21803 break;
21804 case PRAGMA_OMP_CLAUSE_PRIVATE:
21805 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
21806 clauses);
21807 c_name = "private";
21808 break;
21809 case PRAGMA_OMP_CLAUSE_REDUCTION:
21810 clauses = cp_parser_omp_clause_reduction (parser, clauses);
21811 c_name = "reduction";
21812 break;
21813 case PRAGMA_OMP_CLAUSE_SCHEDULE:
21814 clauses = cp_parser_omp_clause_schedule (parser, clauses,
21815 token->location);
21816 c_name = "schedule";
21817 break;
21818 case PRAGMA_OMP_CLAUSE_SHARED:
21819 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
21820 clauses);
21821 c_name = "shared";
21822 break;
21823 case PRAGMA_OMP_CLAUSE_UNTIED:
21824 clauses = cp_parser_omp_clause_untied (parser, clauses,
21825 token->location);
21826 c_name = "nowait";
21827 break;
21828 default:
21829 cp_parser_error (parser, "expected %<#pragma omp%> clause");
21830 goto saw_error;
21833 if (((mask >> c_kind) & 1) == 0)
21835 /* Remove the invalid clause(s) from the list to avoid
21836 confusing the rest of the compiler. */
21837 clauses = prev;
21838 error_at (token->location, "%qs is not valid for %qs", c_name, where);
21841 saw_error:
21842 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
21843 return finish_omp_clauses (clauses);
21846 /* OpenMP 2.5:
21847 structured-block:
21848 statement
21850 In practice, we're also interested in adding the statement to an
21851 outer node. So it is convenient if we work around the fact that
21852 cp_parser_statement calls add_stmt. */
21854 static unsigned
21855 cp_parser_begin_omp_structured_block (cp_parser *parser)
21857 unsigned save = parser->in_statement;
21859 /* Only move the values to IN_OMP_BLOCK if they weren't false.
21860 This preserves the "not within loop or switch" style error messages
21861 for nonsense cases like
21862 void foo() {
21863 #pragma omp single
21864 break;
21867 if (parser->in_statement)
21868 parser->in_statement = IN_OMP_BLOCK;
21870 return save;
21873 static void
21874 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
21876 parser->in_statement = save;
21879 static tree
21880 cp_parser_omp_structured_block (cp_parser *parser)
21882 tree stmt = begin_omp_structured_block ();
21883 unsigned int save = cp_parser_begin_omp_structured_block (parser);
21885 cp_parser_statement (parser, NULL_TREE, false, NULL);
21887 cp_parser_end_omp_structured_block (parser, save);
21888 return finish_omp_structured_block (stmt);
21891 /* OpenMP 2.5:
21892 # pragma omp atomic new-line
21893 expression-stmt
21895 expression-stmt:
21896 x binop= expr | x++ | ++x | x-- | --x
21897 binop:
21898 +, *, -, /, &, ^, |, <<, >>
21900 where x is an lvalue expression with scalar type. */
21902 static void
21903 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
21905 tree lhs, rhs;
21906 enum tree_code code;
21908 cp_parser_require_pragma_eol (parser, pragma_tok);
21910 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
21911 /*cast_p=*/false, NULL);
21912 switch (TREE_CODE (lhs))
21914 case ERROR_MARK:
21915 goto saw_error;
21917 case PREINCREMENT_EXPR:
21918 case POSTINCREMENT_EXPR:
21919 lhs = TREE_OPERAND (lhs, 0);
21920 code = PLUS_EXPR;
21921 rhs = integer_one_node;
21922 break;
21924 case PREDECREMENT_EXPR:
21925 case POSTDECREMENT_EXPR:
21926 lhs = TREE_OPERAND (lhs, 0);
21927 code = MINUS_EXPR;
21928 rhs = integer_one_node;
21929 break;
21931 default:
21932 switch (cp_lexer_peek_token (parser->lexer)->type)
21934 case CPP_MULT_EQ:
21935 code = MULT_EXPR;
21936 break;
21937 case CPP_DIV_EQ:
21938 code = TRUNC_DIV_EXPR;
21939 break;
21940 case CPP_PLUS_EQ:
21941 code = PLUS_EXPR;
21942 break;
21943 case CPP_MINUS_EQ:
21944 code = MINUS_EXPR;
21945 break;
21946 case CPP_LSHIFT_EQ:
21947 code = LSHIFT_EXPR;
21948 break;
21949 case CPP_RSHIFT_EQ:
21950 code = RSHIFT_EXPR;
21951 break;
21952 case CPP_AND_EQ:
21953 code = BIT_AND_EXPR;
21954 break;
21955 case CPP_OR_EQ:
21956 code = BIT_IOR_EXPR;
21957 break;
21958 case CPP_XOR_EQ:
21959 code = BIT_XOR_EXPR;
21960 break;
21961 default:
21962 cp_parser_error (parser,
21963 "invalid operator for %<#pragma omp atomic%>");
21964 goto saw_error;
21966 cp_lexer_consume_token (parser->lexer);
21968 rhs = cp_parser_expression (parser, false, NULL);
21969 if (rhs == error_mark_node)
21970 goto saw_error;
21971 break;
21973 finish_omp_atomic (code, lhs, rhs);
21974 cp_parser_consume_semicolon_at_end_of_statement (parser);
21975 return;
21977 saw_error:
21978 cp_parser_skip_to_end_of_block_or_statement (parser);
21982 /* OpenMP 2.5:
21983 # pragma omp barrier new-line */
21985 static void
21986 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
21988 cp_parser_require_pragma_eol (parser, pragma_tok);
21989 finish_omp_barrier ();
21992 /* OpenMP 2.5:
21993 # pragma omp critical [(name)] new-line
21994 structured-block */
21996 static tree
21997 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
21999 tree stmt, name = NULL;
22001 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22003 cp_lexer_consume_token (parser->lexer);
22005 name = cp_parser_identifier (parser);
22007 if (name == error_mark_node
22008 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
22009 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22010 /*or_comma=*/false,
22011 /*consume_paren=*/true);
22012 if (name == error_mark_node)
22013 name = NULL;
22015 cp_parser_require_pragma_eol (parser, pragma_tok);
22017 stmt = cp_parser_omp_structured_block (parser);
22018 return c_finish_omp_critical (input_location, stmt, name);
22021 /* OpenMP 2.5:
22022 # pragma omp flush flush-vars[opt] new-line
22024 flush-vars:
22025 ( variable-list ) */
22027 static void
22028 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
22030 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22031 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
22032 cp_parser_require_pragma_eol (parser, pragma_tok);
22034 finish_omp_flush ();
22037 /* Helper function, to parse omp for increment expression. */
22039 static tree
22040 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
22042 tree cond = cp_parser_binary_expression (parser, false, true,
22043 PREC_NOT_OPERATOR, NULL);
22044 bool overloaded_p;
22046 if (cond == error_mark_node
22047 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22049 cp_parser_skip_to_end_of_statement (parser);
22050 return error_mark_node;
22053 switch (TREE_CODE (cond))
22055 case GT_EXPR:
22056 case GE_EXPR:
22057 case LT_EXPR:
22058 case LE_EXPR:
22059 break;
22060 default:
22061 return error_mark_node;
22064 /* If decl is an iterator, preserve LHS and RHS of the relational
22065 expr until finish_omp_for. */
22066 if (decl
22067 && (type_dependent_expression_p (decl)
22068 || CLASS_TYPE_P (TREE_TYPE (decl))))
22069 return cond;
22071 return build_x_binary_op (TREE_CODE (cond),
22072 TREE_OPERAND (cond, 0), ERROR_MARK,
22073 TREE_OPERAND (cond, 1), ERROR_MARK,
22074 &overloaded_p, tf_warning_or_error);
22077 /* Helper function, to parse omp for increment expression. */
22079 static tree
22080 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
22082 cp_token *token = cp_lexer_peek_token (parser->lexer);
22083 enum tree_code op;
22084 tree lhs, rhs;
22085 cp_id_kind idk;
22086 bool decl_first;
22088 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
22090 op = (token->type == CPP_PLUS_PLUS
22091 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
22092 cp_lexer_consume_token (parser->lexer);
22093 lhs = cp_parser_cast_expression (parser, false, false, NULL);
22094 if (lhs != decl)
22095 return error_mark_node;
22096 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
22099 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
22100 if (lhs != decl)
22101 return error_mark_node;
22103 token = cp_lexer_peek_token (parser->lexer);
22104 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
22106 op = (token->type == CPP_PLUS_PLUS
22107 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
22108 cp_lexer_consume_token (parser->lexer);
22109 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
22112 op = cp_parser_assignment_operator_opt (parser);
22113 if (op == ERROR_MARK)
22114 return error_mark_node;
22116 if (op != NOP_EXPR)
22118 rhs = cp_parser_assignment_expression (parser, false, NULL);
22119 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
22120 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
22123 lhs = cp_parser_binary_expression (parser, false, false,
22124 PREC_ADDITIVE_EXPRESSION, NULL);
22125 token = cp_lexer_peek_token (parser->lexer);
22126 decl_first = lhs == decl;
22127 if (decl_first)
22128 lhs = NULL_TREE;
22129 if (token->type != CPP_PLUS
22130 && token->type != CPP_MINUS)
22131 return error_mark_node;
22135 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
22136 cp_lexer_consume_token (parser->lexer);
22137 rhs = cp_parser_binary_expression (parser, false, false,
22138 PREC_ADDITIVE_EXPRESSION, NULL);
22139 token = cp_lexer_peek_token (parser->lexer);
22140 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
22142 if (lhs == NULL_TREE)
22144 if (op == PLUS_EXPR)
22145 lhs = rhs;
22146 else
22147 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
22149 else
22150 lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
22151 NULL, tf_warning_or_error);
22154 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
22156 if (!decl_first)
22158 if (rhs != decl || op == MINUS_EXPR)
22159 return error_mark_node;
22160 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
22162 else
22163 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
22165 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
22168 /* Parse the restricted form of the for statement allowed by OpenMP. */
22170 static tree
22171 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
22173 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
22174 tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
22175 tree this_pre_body, cl;
22176 location_t loc_first;
22177 bool collapse_err = false;
22178 int i, collapse = 1, nbraces = 0;
22180 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
22181 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
22182 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
22184 gcc_assert (collapse >= 1);
22186 declv = make_tree_vec (collapse);
22187 initv = make_tree_vec (collapse);
22188 condv = make_tree_vec (collapse);
22189 incrv = make_tree_vec (collapse);
22191 loc_first = cp_lexer_peek_token (parser->lexer)->location;
22193 for (i = 0; i < collapse; i++)
22195 int bracecount = 0;
22196 bool add_private_clause = false;
22197 location_t loc;
22199 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22201 cp_parser_error (parser, "for statement expected");
22202 return NULL;
22204 loc = cp_lexer_consume_token (parser->lexer)->location;
22206 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
22207 return NULL;
22209 init = decl = real_decl = NULL;
22210 this_pre_body = push_stmt_list ();
22211 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22213 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
22215 init-expr:
22216 var = lb
22217 integer-type var = lb
22218 random-access-iterator-type var = lb
22219 pointer-type var = lb
22221 cp_decl_specifier_seq type_specifiers;
22223 /* First, try to parse as an initialized declaration. See
22224 cp_parser_condition, from whence the bulk of this is copied. */
22226 cp_parser_parse_tentatively (parser);
22227 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
22228 /*is_trailing_return=*/false,
22229 &type_specifiers);
22230 if (cp_parser_parse_definitely (parser))
22232 /* If parsing a type specifier seq succeeded, then this
22233 MUST be a initialized declaration. */
22234 tree asm_specification, attributes;
22235 cp_declarator *declarator;
22237 declarator = cp_parser_declarator (parser,
22238 CP_PARSER_DECLARATOR_NAMED,
22239 /*ctor_dtor_or_conv_p=*/NULL,
22240 /*parenthesized_p=*/NULL,
22241 /*member_p=*/false);
22242 attributes = cp_parser_attributes_opt (parser);
22243 asm_specification = cp_parser_asm_specification_opt (parser);
22245 if (declarator == cp_error_declarator)
22246 cp_parser_skip_to_end_of_statement (parser);
22248 else
22250 tree pushed_scope, auto_node;
22252 decl = start_decl (declarator, &type_specifiers,
22253 SD_INITIALIZED, attributes,
22254 /*prefix_attributes=*/NULL_TREE,
22255 &pushed_scope);
22257 auto_node = type_uses_auto (TREE_TYPE (decl));
22258 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
22260 if (cp_lexer_next_token_is (parser->lexer,
22261 CPP_OPEN_PAREN))
22262 error ("parenthesized initialization is not allowed in "
22263 "OpenMP %<for%> loop");
22264 else
22265 /* Trigger an error. */
22266 cp_parser_require (parser, CPP_EQ, "%<=%>");
22268 init = error_mark_node;
22269 cp_parser_skip_to_end_of_statement (parser);
22271 else if (CLASS_TYPE_P (TREE_TYPE (decl))
22272 || type_dependent_expression_p (decl)
22273 || auto_node)
22275 bool is_direct_init, is_non_constant_init;
22277 init = cp_parser_initializer (parser,
22278 &is_direct_init,
22279 &is_non_constant_init);
22281 if (auto_node && describable_type (init))
22283 TREE_TYPE (decl)
22284 = do_auto_deduction (TREE_TYPE (decl), init,
22285 auto_node);
22287 if (!CLASS_TYPE_P (TREE_TYPE (decl))
22288 && !type_dependent_expression_p (decl))
22289 goto non_class;
22292 cp_finish_decl (decl, init, !is_non_constant_init,
22293 asm_specification,
22294 LOOKUP_ONLYCONVERTING);
22295 if (CLASS_TYPE_P (TREE_TYPE (decl)))
22297 for_block
22298 = tree_cons (NULL, this_pre_body, for_block);
22299 init = NULL_TREE;
22301 else
22302 init = pop_stmt_list (this_pre_body);
22303 this_pre_body = NULL_TREE;
22305 else
22307 /* Consume '='. */
22308 cp_lexer_consume_token (parser->lexer);
22309 init = cp_parser_assignment_expression (parser, false, NULL);
22311 non_class:
22312 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
22313 init = error_mark_node;
22314 else
22315 cp_finish_decl (decl, NULL_TREE,
22316 /*init_const_expr_p=*/false,
22317 asm_specification,
22318 LOOKUP_ONLYCONVERTING);
22321 if (pushed_scope)
22322 pop_scope (pushed_scope);
22325 else
22327 cp_id_kind idk;
22328 /* If parsing a type specifier sequence failed, then
22329 this MUST be a simple expression. */
22330 cp_parser_parse_tentatively (parser);
22331 decl = cp_parser_primary_expression (parser, false, false,
22332 false, &idk);
22333 if (!cp_parser_error_occurred (parser)
22334 && decl
22335 && DECL_P (decl)
22336 && CLASS_TYPE_P (TREE_TYPE (decl)))
22338 tree rhs;
22340 cp_parser_parse_definitely (parser);
22341 cp_parser_require (parser, CPP_EQ, "%<=%>");
22342 rhs = cp_parser_assignment_expression (parser, false, NULL);
22343 finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
22344 rhs,
22345 tf_warning_or_error));
22346 add_private_clause = true;
22348 else
22350 decl = NULL;
22351 cp_parser_abort_tentative_parse (parser);
22352 init = cp_parser_expression (parser, false, NULL);
22353 if (init)
22355 if (TREE_CODE (init) == MODIFY_EXPR
22356 || TREE_CODE (init) == MODOP_EXPR)
22357 real_decl = TREE_OPERAND (init, 0);
22362 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
22363 if (this_pre_body)
22365 this_pre_body = pop_stmt_list (this_pre_body);
22366 if (pre_body)
22368 tree t = pre_body;
22369 pre_body = push_stmt_list ();
22370 add_stmt (t);
22371 add_stmt (this_pre_body);
22372 pre_body = pop_stmt_list (pre_body);
22374 else
22375 pre_body = this_pre_body;
22378 if (decl)
22379 real_decl = decl;
22380 if (par_clauses != NULL && real_decl != NULL_TREE)
22382 tree *c;
22383 for (c = par_clauses; *c ; )
22384 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
22385 && OMP_CLAUSE_DECL (*c) == real_decl)
22387 error_at (loc, "iteration variable %qD"
22388 " should not be firstprivate", real_decl);
22389 *c = OMP_CLAUSE_CHAIN (*c);
22391 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
22392 && OMP_CLAUSE_DECL (*c) == real_decl)
22394 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
22395 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
22396 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
22397 OMP_CLAUSE_DECL (l) = real_decl;
22398 OMP_CLAUSE_CHAIN (l) = clauses;
22399 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
22400 clauses = l;
22401 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
22402 CP_OMP_CLAUSE_INFO (*c) = NULL;
22403 add_private_clause = false;
22405 else
22407 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
22408 && OMP_CLAUSE_DECL (*c) == real_decl)
22409 add_private_clause = false;
22410 c = &OMP_CLAUSE_CHAIN (*c);
22414 if (add_private_clause)
22416 tree c;
22417 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
22419 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
22420 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
22421 && OMP_CLAUSE_DECL (c) == decl)
22422 break;
22423 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
22424 && OMP_CLAUSE_DECL (c) == decl)
22425 error_at (loc, "iteration variable %qD "
22426 "should not be firstprivate",
22427 decl);
22428 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
22429 && OMP_CLAUSE_DECL (c) == decl)
22430 error_at (loc, "iteration variable %qD should not be reduction",
22431 decl);
22433 if (c == NULL)
22435 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
22436 OMP_CLAUSE_DECL (c) = decl;
22437 c = finish_omp_clauses (c);
22438 if (c)
22440 OMP_CLAUSE_CHAIN (c) = clauses;
22441 clauses = c;
22446 cond = NULL;
22447 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22448 cond = cp_parser_omp_for_cond (parser, decl);
22449 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
22451 incr = NULL;
22452 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
22454 /* If decl is an iterator, preserve the operator on decl
22455 until finish_omp_for. */
22456 if (decl
22457 && (type_dependent_expression_p (decl)
22458 || CLASS_TYPE_P (TREE_TYPE (decl))))
22459 incr = cp_parser_omp_for_incr (parser, decl);
22460 else
22461 incr = cp_parser_expression (parser, false, NULL);
22464 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
22465 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22466 /*or_comma=*/false,
22467 /*consume_paren=*/true);
22469 TREE_VEC_ELT (declv, i) = decl;
22470 TREE_VEC_ELT (initv, i) = init;
22471 TREE_VEC_ELT (condv, i) = cond;
22472 TREE_VEC_ELT (incrv, i) = incr;
22474 if (i == collapse - 1)
22475 break;
22477 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
22478 in between the collapsed for loops to be still considered perfectly
22479 nested. Hopefully the final version clarifies this.
22480 For now handle (multiple) {'s and empty statements. */
22481 cp_parser_parse_tentatively (parser);
22484 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22485 break;
22486 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22488 cp_lexer_consume_token (parser->lexer);
22489 bracecount++;
22491 else if (bracecount
22492 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22493 cp_lexer_consume_token (parser->lexer);
22494 else
22496 loc = cp_lexer_peek_token (parser->lexer)->location;
22497 error_at (loc, "not enough collapsed for loops");
22498 collapse_err = true;
22499 cp_parser_abort_tentative_parse (parser);
22500 declv = NULL_TREE;
22501 break;
22504 while (1);
22506 if (declv)
22508 cp_parser_parse_definitely (parser);
22509 nbraces += bracecount;
22513 /* Note that we saved the original contents of this flag when we entered
22514 the structured block, and so we don't need to re-save it here. */
22515 parser->in_statement = IN_OMP_FOR;
22517 /* Note that the grammar doesn't call for a structured block here,
22518 though the loop as a whole is a structured block. */
22519 body = push_stmt_list ();
22520 cp_parser_statement (parser, NULL_TREE, false, NULL);
22521 body = pop_stmt_list (body);
22523 if (declv == NULL_TREE)
22524 ret = NULL_TREE;
22525 else
22526 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
22527 pre_body, clauses);
22529 while (nbraces)
22531 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
22533 cp_lexer_consume_token (parser->lexer);
22534 nbraces--;
22536 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22537 cp_lexer_consume_token (parser->lexer);
22538 else
22540 if (!collapse_err)
22542 error_at (cp_lexer_peek_token (parser->lexer)->location,
22543 "collapsed loops not perfectly nested");
22545 collapse_err = true;
22546 cp_parser_statement_seq_opt (parser, NULL);
22547 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
22548 break;
22552 while (for_block)
22554 add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
22555 for_block = TREE_CHAIN (for_block);
22558 return ret;
22561 /* OpenMP 2.5:
22562 #pragma omp for for-clause[optseq] new-line
22563 for-loop */
22565 #define OMP_FOR_CLAUSE_MASK \
22566 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22567 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22568 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
22569 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
22570 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
22571 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
22572 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
22573 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
22575 static tree
22576 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
22578 tree clauses, sb, ret;
22579 unsigned int save;
22581 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
22582 "#pragma omp for", pragma_tok);
22584 sb = begin_omp_structured_block ();
22585 save = cp_parser_begin_omp_structured_block (parser);
22587 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
22589 cp_parser_end_omp_structured_block (parser, save);
22590 add_stmt (finish_omp_structured_block (sb));
22592 return ret;
22595 /* OpenMP 2.5:
22596 # pragma omp master new-line
22597 structured-block */
22599 static tree
22600 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
22602 cp_parser_require_pragma_eol (parser, pragma_tok);
22603 return c_finish_omp_master (input_location,
22604 cp_parser_omp_structured_block (parser));
22607 /* OpenMP 2.5:
22608 # pragma omp ordered new-line
22609 structured-block */
22611 static tree
22612 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
22614 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22615 cp_parser_require_pragma_eol (parser, pragma_tok);
22616 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
22619 /* OpenMP 2.5:
22621 section-scope:
22622 { section-sequence }
22624 section-sequence:
22625 section-directive[opt] structured-block
22626 section-sequence section-directive structured-block */
22628 static tree
22629 cp_parser_omp_sections_scope (cp_parser *parser)
22631 tree stmt, substmt;
22632 bool error_suppress = false;
22633 cp_token *tok;
22635 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
22636 return NULL_TREE;
22638 stmt = push_stmt_list ();
22640 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
22642 unsigned save;
22644 substmt = begin_omp_structured_block ();
22645 save = cp_parser_begin_omp_structured_block (parser);
22647 while (1)
22649 cp_parser_statement (parser, NULL_TREE, false, NULL);
22651 tok = cp_lexer_peek_token (parser->lexer);
22652 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
22653 break;
22654 if (tok->type == CPP_CLOSE_BRACE)
22655 break;
22656 if (tok->type == CPP_EOF)
22657 break;
22660 cp_parser_end_omp_structured_block (parser, save);
22661 substmt = finish_omp_structured_block (substmt);
22662 substmt = build1 (OMP_SECTION, void_type_node, substmt);
22663 add_stmt (substmt);
22666 while (1)
22668 tok = cp_lexer_peek_token (parser->lexer);
22669 if (tok->type == CPP_CLOSE_BRACE)
22670 break;
22671 if (tok->type == CPP_EOF)
22672 break;
22674 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
22676 cp_lexer_consume_token (parser->lexer);
22677 cp_parser_require_pragma_eol (parser, tok);
22678 error_suppress = false;
22680 else if (!error_suppress)
22682 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
22683 error_suppress = true;
22686 substmt = cp_parser_omp_structured_block (parser);
22687 substmt = build1 (OMP_SECTION, void_type_node, substmt);
22688 add_stmt (substmt);
22690 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
22692 substmt = pop_stmt_list (stmt);
22694 stmt = make_node (OMP_SECTIONS);
22695 TREE_TYPE (stmt) = void_type_node;
22696 OMP_SECTIONS_BODY (stmt) = substmt;
22698 add_stmt (stmt);
22699 return stmt;
22702 /* OpenMP 2.5:
22703 # pragma omp sections sections-clause[optseq] newline
22704 sections-scope */
22706 #define OMP_SECTIONS_CLAUSE_MASK \
22707 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22708 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22709 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
22710 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
22711 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
22713 static tree
22714 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
22716 tree clauses, ret;
22718 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
22719 "#pragma omp sections", pragma_tok);
22721 ret = cp_parser_omp_sections_scope (parser);
22722 if (ret)
22723 OMP_SECTIONS_CLAUSES (ret) = clauses;
22725 return ret;
22728 /* OpenMP 2.5:
22729 # pragma parallel parallel-clause new-line
22730 # pragma parallel for parallel-for-clause new-line
22731 # pragma parallel sections parallel-sections-clause new-line */
22733 #define OMP_PARALLEL_CLAUSE_MASK \
22734 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
22735 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22736 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22737 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
22738 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
22739 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
22740 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
22741 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
22743 static tree
22744 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
22746 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
22747 const char *p_name = "#pragma omp parallel";
22748 tree stmt, clauses, par_clause, ws_clause, block;
22749 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
22750 unsigned int save;
22751 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22753 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22755 cp_lexer_consume_token (parser->lexer);
22756 p_kind = PRAGMA_OMP_PARALLEL_FOR;
22757 p_name = "#pragma omp parallel for";
22758 mask |= OMP_FOR_CLAUSE_MASK;
22759 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
22761 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22763 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
22764 const char *p = IDENTIFIER_POINTER (id);
22765 if (strcmp (p, "sections") == 0)
22767 cp_lexer_consume_token (parser->lexer);
22768 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
22769 p_name = "#pragma omp parallel sections";
22770 mask |= OMP_SECTIONS_CLAUSE_MASK;
22771 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
22775 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
22776 block = begin_omp_parallel ();
22777 save = cp_parser_begin_omp_structured_block (parser);
22779 switch (p_kind)
22781 case PRAGMA_OMP_PARALLEL:
22782 cp_parser_statement (parser, NULL_TREE, false, NULL);
22783 par_clause = clauses;
22784 break;
22786 case PRAGMA_OMP_PARALLEL_FOR:
22787 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
22788 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
22789 break;
22791 case PRAGMA_OMP_PARALLEL_SECTIONS:
22792 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
22793 stmt = cp_parser_omp_sections_scope (parser);
22794 if (stmt)
22795 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
22796 break;
22798 default:
22799 gcc_unreachable ();
22802 cp_parser_end_omp_structured_block (parser, save);
22803 stmt = finish_omp_parallel (par_clause, block);
22804 if (p_kind != PRAGMA_OMP_PARALLEL)
22805 OMP_PARALLEL_COMBINED (stmt) = 1;
22806 return stmt;
22809 /* OpenMP 2.5:
22810 # pragma omp single single-clause[optseq] new-line
22811 structured-block */
22813 #define OMP_SINGLE_CLAUSE_MASK \
22814 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22815 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22816 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
22817 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
22819 static tree
22820 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
22822 tree stmt = make_node (OMP_SINGLE);
22823 TREE_TYPE (stmt) = void_type_node;
22825 OMP_SINGLE_CLAUSES (stmt)
22826 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
22827 "#pragma omp single", pragma_tok);
22828 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
22830 return add_stmt (stmt);
22833 /* OpenMP 3.0:
22834 # pragma omp task task-clause[optseq] new-line
22835 structured-block */
22837 #define OMP_TASK_CLAUSE_MASK \
22838 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
22839 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
22840 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
22841 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22842 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22843 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
22845 static tree
22846 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
22848 tree clauses, block;
22849 unsigned int save;
22851 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
22852 "#pragma omp task", pragma_tok);
22853 block = begin_omp_task ();
22854 save = cp_parser_begin_omp_structured_block (parser);
22855 cp_parser_statement (parser, NULL_TREE, false, NULL);
22856 cp_parser_end_omp_structured_block (parser, save);
22857 return finish_omp_task (clauses, block);
22860 /* OpenMP 3.0:
22861 # pragma omp taskwait new-line */
22863 static void
22864 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
22866 cp_parser_require_pragma_eol (parser, pragma_tok);
22867 finish_omp_taskwait ();
22870 /* OpenMP 2.5:
22871 # pragma omp threadprivate (variable-list) */
22873 static void
22874 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
22876 tree vars;
22878 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
22879 cp_parser_require_pragma_eol (parser, pragma_tok);
22881 finish_omp_threadprivate (vars);
22884 /* Main entry point to OpenMP statement pragmas. */
22886 static void
22887 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
22889 tree stmt;
22891 switch (pragma_tok->pragma_kind)
22893 case PRAGMA_OMP_ATOMIC:
22894 cp_parser_omp_atomic (parser, pragma_tok);
22895 return;
22896 case PRAGMA_OMP_CRITICAL:
22897 stmt = cp_parser_omp_critical (parser, pragma_tok);
22898 break;
22899 case PRAGMA_OMP_FOR:
22900 stmt = cp_parser_omp_for (parser, pragma_tok);
22901 break;
22902 case PRAGMA_OMP_MASTER:
22903 stmt = cp_parser_omp_master (parser, pragma_tok);
22904 break;
22905 case PRAGMA_OMP_ORDERED:
22906 stmt = cp_parser_omp_ordered (parser, pragma_tok);
22907 break;
22908 case PRAGMA_OMP_PARALLEL:
22909 stmt = cp_parser_omp_parallel (parser, pragma_tok);
22910 break;
22911 case PRAGMA_OMP_SECTIONS:
22912 stmt = cp_parser_omp_sections (parser, pragma_tok);
22913 break;
22914 case PRAGMA_OMP_SINGLE:
22915 stmt = cp_parser_omp_single (parser, pragma_tok);
22916 break;
22917 case PRAGMA_OMP_TASK:
22918 stmt = cp_parser_omp_task (parser, pragma_tok);
22919 break;
22920 default:
22921 gcc_unreachable ();
22924 if (stmt)
22925 SET_EXPR_LOCATION (stmt, pragma_tok->location);
22928 /* The parser. */
22930 static GTY (()) cp_parser *the_parser;
22933 /* Special handling for the first token or line in the file. The first
22934 thing in the file might be #pragma GCC pch_preprocess, which loads a
22935 PCH file, which is a GC collection point. So we need to handle this
22936 first pragma without benefit of an existing lexer structure.
22938 Always returns one token to the caller in *FIRST_TOKEN. This is
22939 either the true first token of the file, or the first token after
22940 the initial pragma. */
22942 static void
22943 cp_parser_initial_pragma (cp_token *first_token)
22945 tree name = NULL;
22947 cp_lexer_get_preprocessor_token (NULL, first_token);
22948 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
22949 return;
22951 cp_lexer_get_preprocessor_token (NULL, first_token);
22952 if (first_token->type == CPP_STRING)
22954 name = first_token->u.value;
22956 cp_lexer_get_preprocessor_token (NULL, first_token);
22957 if (first_token->type != CPP_PRAGMA_EOL)
22958 error_at (first_token->location,
22959 "junk at end of %<#pragma GCC pch_preprocess%>");
22961 else
22962 error_at (first_token->location, "expected string literal");
22964 /* Skip to the end of the pragma. */
22965 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
22966 cp_lexer_get_preprocessor_token (NULL, first_token);
22968 /* Now actually load the PCH file. */
22969 if (name)
22970 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
22972 /* Read one more token to return to our caller. We have to do this
22973 after reading the PCH file in, since its pointers have to be
22974 live. */
22975 cp_lexer_get_preprocessor_token (NULL, first_token);
22978 /* Normal parsing of a pragma token. Here we can (and must) use the
22979 regular lexer. */
22981 static bool
22982 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
22984 cp_token *pragma_tok;
22985 unsigned int id;
22987 pragma_tok = cp_lexer_consume_token (parser->lexer);
22988 gcc_assert (pragma_tok->type == CPP_PRAGMA);
22989 parser->lexer->in_pragma = true;
22991 id = pragma_tok->pragma_kind;
22992 switch (id)
22994 case PRAGMA_GCC_PCH_PREPROCESS:
22995 error_at (pragma_tok->location,
22996 "%<#pragma GCC pch_preprocess%> must be first");
22997 break;
22999 case PRAGMA_OMP_BARRIER:
23000 switch (context)
23002 case pragma_compound:
23003 cp_parser_omp_barrier (parser, pragma_tok);
23004 return false;
23005 case pragma_stmt:
23006 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
23007 "used in compound statements");
23008 break;
23009 default:
23010 goto bad_stmt;
23012 break;
23014 case PRAGMA_OMP_FLUSH:
23015 switch (context)
23017 case pragma_compound:
23018 cp_parser_omp_flush (parser, pragma_tok);
23019 return false;
23020 case pragma_stmt:
23021 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
23022 "used in compound statements");
23023 break;
23024 default:
23025 goto bad_stmt;
23027 break;
23029 case PRAGMA_OMP_TASKWAIT:
23030 switch (context)
23032 case pragma_compound:
23033 cp_parser_omp_taskwait (parser, pragma_tok);
23034 return false;
23035 case pragma_stmt:
23036 error_at (pragma_tok->location,
23037 "%<#pragma omp taskwait%> may only be "
23038 "used in compound statements");
23039 break;
23040 default:
23041 goto bad_stmt;
23043 break;
23045 case PRAGMA_OMP_THREADPRIVATE:
23046 cp_parser_omp_threadprivate (parser, pragma_tok);
23047 return false;
23049 case PRAGMA_OMP_ATOMIC:
23050 case PRAGMA_OMP_CRITICAL:
23051 case PRAGMA_OMP_FOR:
23052 case PRAGMA_OMP_MASTER:
23053 case PRAGMA_OMP_ORDERED:
23054 case PRAGMA_OMP_PARALLEL:
23055 case PRAGMA_OMP_SECTIONS:
23056 case PRAGMA_OMP_SINGLE:
23057 case PRAGMA_OMP_TASK:
23058 if (context == pragma_external)
23059 goto bad_stmt;
23060 cp_parser_omp_construct (parser, pragma_tok);
23061 return true;
23063 case PRAGMA_OMP_SECTION:
23064 error_at (pragma_tok->location,
23065 "%<#pragma omp section%> may only be used in "
23066 "%<#pragma omp sections%> construct");
23067 break;
23069 default:
23070 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
23071 c_invoke_pragma_handler (id);
23072 break;
23074 bad_stmt:
23075 cp_parser_error (parser, "expected declaration specifiers");
23076 break;
23079 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
23080 return false;
23083 /* The interface the pragma parsers have to the lexer. */
23085 enum cpp_ttype
23086 pragma_lex (tree *value)
23088 cp_token *tok;
23089 enum cpp_ttype ret;
23091 tok = cp_lexer_peek_token (the_parser->lexer);
23093 ret = tok->type;
23094 *value = tok->u.value;
23096 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
23097 ret = CPP_EOF;
23098 else if (ret == CPP_STRING)
23099 *value = cp_parser_string_literal (the_parser, false, false);
23100 else
23102 cp_lexer_consume_token (the_parser->lexer);
23103 if (ret == CPP_KEYWORD)
23104 ret = CPP_NAME;
23107 return ret;
23111 /* External interface. */
23113 /* Parse one entire translation unit. */
23115 void
23116 c_parse_file (void)
23118 static bool already_called = false;
23120 if (already_called)
23122 sorry ("inter-module optimizations not implemented for C++");
23123 return;
23125 already_called = true;
23127 the_parser = cp_parser_new ();
23128 push_deferring_access_checks (flag_access_control
23129 ? dk_no_deferred : dk_no_check);
23130 cp_parser_translation_unit (the_parser);
23131 the_parser = NULL;
23134 #include "gt-cp-parser.h"