Merged r158465 through r158660 into branch.
[official-gcc.git] / gcc / cp / parser.c
blob720a632ed7530522a94714e80402423664cee25f
1 /* C++ Parser.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005, 2007, 2008, 2009 Free Software Foundation, Inc.
4 Written by Mark Mitchell <mark@codesourcery.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "dyn-string.h"
27 #include "varray.h"
28 #include "cpplib.h"
29 #include "tree.h"
30 #include "cp-tree.h"
31 #include "intl.h"
32 #include "c-pragma.h"
33 #include "decl.h"
34 #include "flags.h"
35 #include "diagnostic.h"
36 #include "toplev.h"
37 #include "output.h"
38 #include "target.h"
39 #include "cgraph.h"
40 #include "c-common.h"
41 #include "plugin.h"
44 /* The lexer. */
46 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
47 and c-lex.c) and the C++ parser. */
49 /* A token's value and its associated deferred access checks and
50 qualifying scope. */
52 struct GTY(()) tree_check {
53 /* The value associated with the token. */
54 tree value;
55 /* The checks that have been associated with value. */
56 VEC (deferred_access_check, gc)* checks;
57 /* The token's qualifying scope (used when it is a
58 CPP_NESTED_NAME_SPECIFIER). */
59 tree qualifying_scope;
62 /* A C++ token. */
64 typedef struct GTY (()) cp_token {
65 /* The kind of token. */
66 ENUM_BITFIELD (cpp_ttype) type : 8;
67 /* If this token is a keyword, this value indicates which keyword.
68 Otherwise, this value is RID_MAX. */
69 ENUM_BITFIELD (rid) keyword : 8;
70 /* Token flags. */
71 unsigned char flags;
72 /* Identifier for the pragma. */
73 ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
74 /* True if this token is from a context where it is implicitly extern "C" */
75 BOOL_BITFIELD implicit_extern_c : 1;
76 /* True for a CPP_NAME token that is not a keyword (i.e., for which
77 KEYWORD is RID_MAX) iff this name was looked up and found to be
78 ambiguous. An error has already been reported. */
79 BOOL_BITFIELD ambiguous_p : 1;
80 /* The location at which this token was found. */
81 location_t location;
82 /* The value associated with this token, if any. */
83 union cp_token_value {
84 /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID. */
85 struct tree_check* GTY((tag ("1"))) tree_check_value;
86 /* Use for all other tokens. */
87 tree GTY((tag ("0"))) value;
88 } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
89 } cp_token;
91 /* We use a stack of token pointer for saving token sets. */
92 typedef struct cp_token *cp_token_position;
93 DEF_VEC_P (cp_token_position);
94 DEF_VEC_ALLOC_P (cp_token_position,heap);
96 static cp_token eof_token =
98 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, 0, 0, { NULL }
101 /* The cp_lexer structure represents the C++ lexer. It is responsible
102 for managing the token stream from the preprocessor and supplying
103 it to the parser. Tokens are never added to the cp_lexer after
104 it is created. */
106 typedef struct GTY (()) cp_lexer {
107 /* The memory allocated for the buffer. NULL if this lexer does not
108 own the token buffer. */
109 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
110 /* If the lexer owns the buffer, this is the number of tokens in the
111 buffer. */
112 size_t buffer_length;
114 /* A pointer just past the last available token. The tokens
115 in this lexer are [buffer, last_token). */
116 cp_token_position GTY ((skip)) last_token;
118 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
119 no more available tokens. */
120 cp_token_position GTY ((skip)) next_token;
122 /* A stack indicating positions at which cp_lexer_save_tokens was
123 called. The top entry is the most recent position at which we
124 began saving tokens. If the stack is non-empty, we are saving
125 tokens. */
126 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
128 /* The next lexer in a linked list of lexers. */
129 struct cp_lexer *next;
131 /* True if we should output debugging information. */
132 bool debugging_p;
134 /* True if we're in the context of parsing a pragma, and should not
135 increment past the end-of-line marker. */
136 bool in_pragma;
137 } cp_lexer;
139 /* cp_token_cache is a range of tokens. There is no need to represent
140 allocate heap memory for it, since tokens are never removed from the
141 lexer's array. There is also no need for the GC to walk through
142 a cp_token_cache, since everything in here is referenced through
143 a lexer. */
145 typedef struct GTY(()) cp_token_cache {
146 /* The beginning of the token range. */
147 cp_token * GTY((skip)) first;
149 /* Points immediately after the last token in the range. */
150 cp_token * GTY ((skip)) last;
151 } cp_token_cache;
153 /* Prototypes. */
155 static cp_lexer *cp_lexer_new_main
156 (void);
157 static cp_lexer *cp_lexer_new_from_tokens
158 (cp_token_cache *tokens);
159 static void cp_lexer_destroy
160 (cp_lexer *);
161 static int cp_lexer_saving_tokens
162 (const cp_lexer *);
163 static cp_token_position cp_lexer_token_position
164 (cp_lexer *, bool);
165 static cp_token *cp_lexer_token_at
166 (cp_lexer *, cp_token_position);
167 static void cp_lexer_get_preprocessor_token
168 (cp_lexer *, cp_token *);
169 static inline cp_token *cp_lexer_peek_token
170 (cp_lexer *);
171 static cp_token *cp_lexer_peek_nth_token
172 (cp_lexer *, size_t);
173 static inline bool cp_lexer_next_token_is
174 (cp_lexer *, enum cpp_ttype);
175 static bool cp_lexer_next_token_is_not
176 (cp_lexer *, enum cpp_ttype);
177 static bool cp_lexer_next_token_is_keyword
178 (cp_lexer *, enum rid);
179 static cp_token *cp_lexer_consume_token
180 (cp_lexer *);
181 static void cp_lexer_purge_token
182 (cp_lexer *);
183 static void cp_lexer_purge_tokens_after
184 (cp_lexer *, cp_token_position);
185 static void cp_lexer_save_tokens
186 (cp_lexer *);
187 static void cp_lexer_commit_tokens
188 (cp_lexer *);
189 static void cp_lexer_rollback_tokens
190 (cp_lexer *);
191 #ifdef ENABLE_CHECKING
192 static void cp_lexer_print_token
193 (FILE *, cp_token *);
194 static inline bool cp_lexer_debugging_p
195 (cp_lexer *);
196 static void cp_lexer_start_debugging
197 (cp_lexer *) ATTRIBUTE_UNUSED;
198 static void cp_lexer_stop_debugging
199 (cp_lexer *) ATTRIBUTE_UNUSED;
200 #else
201 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
202 about passing NULL to functions that require non-NULL arguments
203 (fputs, fprintf). It will never be used, so all we need is a value
204 of the right type that's guaranteed not to be NULL. */
205 #define cp_lexer_debug_stream stdout
206 #define cp_lexer_print_token(str, tok) (void) 0
207 #define cp_lexer_debugging_p(lexer) 0
208 #endif /* ENABLE_CHECKING */
210 static cp_token_cache *cp_token_cache_new
211 (cp_token *, cp_token *);
213 static void cp_parser_initial_pragma
214 (cp_token *);
216 /* Manifest constants. */
217 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
218 #define CP_SAVED_TOKEN_STACK 5
220 /* A token type for keywords, as opposed to ordinary identifiers. */
221 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
223 /* A token type for template-ids. If a template-id is processed while
224 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
225 the value of the CPP_TEMPLATE_ID is whatever was returned by
226 cp_parser_template_id. */
227 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
229 /* A token type for nested-name-specifiers. If a
230 nested-name-specifier is processed while parsing tentatively, it is
231 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
232 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
233 cp_parser_nested_name_specifier_opt. */
234 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
236 /* A token type for tokens that are not tokens at all; these are used
237 to represent slots in the array where there used to be a token
238 that has now been deleted. */
239 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
241 /* The number of token types, including C++-specific ones. */
242 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
244 /* Variables. */
246 #ifdef ENABLE_CHECKING
247 /* The stream to which debugging output should be written. */
248 static FILE *cp_lexer_debug_stream;
249 #endif /* ENABLE_CHECKING */
251 /* Nonzero if we are parsing an unevaluated operand: an operand to
252 sizeof, typeof, or alignof. */
253 int cp_unevaluated_operand;
255 /* Create a new main C++ lexer, the lexer that gets tokens from the
256 preprocessor. */
258 static cp_lexer *
259 cp_lexer_new_main (void)
261 cp_token first_token;
262 cp_lexer *lexer;
263 cp_token *pos;
264 size_t alloc;
265 size_t space;
266 cp_token *buffer;
268 /* It's possible that parsing the first pragma will load a PCH file,
269 which is a GC collection point. So we have to do that before
270 allocating any memory. */
271 cp_parser_initial_pragma (&first_token);
273 c_common_no_more_pch ();
275 /* Allocate the memory. */
276 lexer = GGC_CNEW (cp_lexer);
278 #ifdef ENABLE_CHECKING
279 /* Initially we are not debugging. */
280 lexer->debugging_p = false;
281 #endif /* ENABLE_CHECKING */
282 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
283 CP_SAVED_TOKEN_STACK);
285 /* Create the buffer. */
286 alloc = CP_LEXER_BUFFER_SIZE;
287 buffer = GGC_NEWVEC (cp_token, alloc);
289 /* Put the first token in the buffer. */
290 space = alloc;
291 pos = buffer;
292 *pos = first_token;
294 /* Get the remaining tokens from the preprocessor. */
295 while (pos->type != CPP_EOF)
297 pos++;
298 if (!--space)
300 space = alloc;
301 alloc *= 2;
302 buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
303 pos = buffer + space;
305 cp_lexer_get_preprocessor_token (lexer, pos);
307 lexer->buffer = buffer;
308 lexer->buffer_length = alloc - space;
309 lexer->last_token = pos;
310 lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
312 /* Subsequent preprocessor diagnostics should use compiler
313 diagnostic functions to get the compiler source location. */
314 done_lexing = true;
316 gcc_assert (lexer->next_token->type != CPP_PURGED);
317 return lexer;
320 /* Create a new lexer whose token stream is primed with the tokens in
321 CACHE. When these tokens are exhausted, no new tokens will be read. */
323 static cp_lexer *
324 cp_lexer_new_from_tokens (cp_token_cache *cache)
326 cp_token *first = cache->first;
327 cp_token *last = cache->last;
328 cp_lexer *lexer = GGC_CNEW (cp_lexer);
330 /* We do not own the buffer. */
331 lexer->buffer = NULL;
332 lexer->buffer_length = 0;
333 lexer->next_token = first == last ? &eof_token : first;
334 lexer->last_token = last;
336 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
337 CP_SAVED_TOKEN_STACK);
339 #ifdef ENABLE_CHECKING
340 /* Initially we are not debugging. */
341 lexer->debugging_p = false;
342 #endif
344 gcc_assert (lexer->next_token->type != CPP_PURGED);
345 return lexer;
348 /* Frees all resources associated with LEXER. */
350 static void
351 cp_lexer_destroy (cp_lexer *lexer)
353 if (lexer->buffer)
354 ggc_free (lexer->buffer);
355 VEC_free (cp_token_position, heap, lexer->saved_tokens);
356 ggc_free (lexer);
359 /* Returns nonzero if debugging information should be output. */
361 #ifdef ENABLE_CHECKING
363 static inline bool
364 cp_lexer_debugging_p (cp_lexer *lexer)
366 return lexer->debugging_p;
369 #endif /* ENABLE_CHECKING */
371 static inline cp_token_position
372 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
374 gcc_assert (!previous_p || lexer->next_token != &eof_token);
376 return lexer->next_token - previous_p;
379 static inline cp_token *
380 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
382 return pos;
385 /* nonzero if we are presently saving tokens. */
387 static inline int
388 cp_lexer_saving_tokens (const cp_lexer* lexer)
390 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
393 /* Store the next token from the preprocessor in *TOKEN. Return true
394 if we reach EOF. If LEXER is NULL, assume we are handling an
395 initial #pragma pch_preprocess, and thus want the lexer to return
396 processed strings. */
398 static void
399 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
401 static int is_extern_c = 0;
403 /* Get a new token from the preprocessor. */
404 token->type
405 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
406 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
407 token->keyword = RID_MAX;
408 token->pragma_kind = PRAGMA_NONE;
410 /* On some systems, some header files are surrounded by an
411 implicit extern "C" block. Set a flag in the token if it
412 comes from such a header. */
413 is_extern_c += pending_lang_change;
414 pending_lang_change = 0;
415 token->implicit_extern_c = is_extern_c > 0;
417 /* Check to see if this token is a keyword. */
418 if (token->type == CPP_NAME)
420 if (C_IS_RESERVED_WORD (token->u.value))
422 /* Mark this token as a keyword. */
423 token->type = CPP_KEYWORD;
424 /* Record which keyword. */
425 token->keyword = C_RID_CODE (token->u.value);
427 else
429 if (warn_cxx0x_compat
430 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
431 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
433 /* Warn about the C++0x keyword (but still treat it as
434 an identifier). */
435 warning (OPT_Wc__0x_compat,
436 "identifier %qE will become a keyword in C++0x",
437 token->u.value);
439 /* Clear out the C_RID_CODE so we don't warn about this
440 particular identifier-turned-keyword again. */
441 C_SET_RID_CODE (token->u.value, RID_MAX);
444 token->ambiguous_p = false;
445 token->keyword = RID_MAX;
448 /* Handle Objective-C++ keywords. */
449 else if (token->type == CPP_AT_NAME)
451 token->type = CPP_KEYWORD;
452 switch (C_RID_CODE (token->u.value))
454 /* Map 'class' to '@class', 'private' to '@private', etc. */
455 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
456 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
457 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
458 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
459 case RID_THROW: token->keyword = RID_AT_THROW; break;
460 case RID_TRY: token->keyword = RID_AT_TRY; break;
461 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
462 default: token->keyword = C_RID_CODE (token->u.value);
465 else if (token->type == CPP_PRAGMA)
467 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
468 token->pragma_kind = ((enum pragma_kind)
469 TREE_INT_CST_LOW (token->u.value));
470 token->u.value = NULL_TREE;
474 /* Update the globals input_location and the input file stack from TOKEN. */
475 static inline void
476 cp_lexer_set_source_position_from_token (cp_token *token)
478 if (token->type != CPP_EOF)
480 input_location = token->location;
484 /* Return a pointer to the next token in the token stream, but do not
485 consume it. */
487 static inline cp_token *
488 cp_lexer_peek_token (cp_lexer *lexer)
490 if (cp_lexer_debugging_p (lexer))
492 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
493 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
494 putc ('\n', cp_lexer_debug_stream);
496 return lexer->next_token;
499 /* Return true if the next token has the indicated TYPE. */
501 static inline bool
502 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
504 return cp_lexer_peek_token (lexer)->type == type;
507 /* Return true if the next token does not have the indicated TYPE. */
509 static inline bool
510 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
512 return !cp_lexer_next_token_is (lexer, type);
515 /* Return true if the next token is the indicated KEYWORD. */
517 static inline bool
518 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
520 return cp_lexer_peek_token (lexer)->keyword == keyword;
523 /* Return true if the next token is not the indicated KEYWORD. */
525 static inline bool
526 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
528 return cp_lexer_peek_token (lexer)->keyword != keyword;
531 /* Return true if the next token is a keyword for a decl-specifier. */
533 static bool
534 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
536 cp_token *token;
538 token = cp_lexer_peek_token (lexer);
539 switch (token->keyword)
541 /* auto specifier: storage-class-specifier in C++,
542 simple-type-specifier in C++0x. */
543 case RID_AUTO:
544 /* Storage classes. */
545 case RID_REGISTER:
546 case RID_STATIC:
547 case RID_EXTERN:
548 case RID_MUTABLE:
549 case RID_THREAD:
550 /* Elaborated type specifiers. */
551 case RID_ENUM:
552 case RID_CLASS:
553 case RID_STRUCT:
554 case RID_UNION:
555 case RID_TYPENAME:
556 /* Simple type specifiers. */
557 case RID_CHAR:
558 case RID_CHAR16:
559 case RID_CHAR32:
560 case RID_WCHAR:
561 case RID_BOOL:
562 case RID_SHORT:
563 case RID_INT:
564 case RID_LONG:
565 case RID_SIGNED:
566 case RID_UNSIGNED:
567 case RID_FLOAT:
568 case RID_DOUBLE:
569 case RID_VOID:
570 /* GNU extensions. */
571 case RID_ATTRIBUTE:
572 case RID_TYPEOF:
573 /* C++0x extensions. */
574 case RID_DECLTYPE:
575 return true;
577 default:
578 return false;
582 /* Return a pointer to the Nth token in the token stream. If N is 1,
583 then this is precisely equivalent to cp_lexer_peek_token (except
584 that it is not inline). One would like to disallow that case, but
585 there is one case (cp_parser_nth_token_starts_template_id) where
586 the caller passes a variable for N and it might be 1. */
588 static cp_token *
589 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
591 cp_token *token;
593 /* N is 1-based, not zero-based. */
594 gcc_assert (n > 0);
596 if (cp_lexer_debugging_p (lexer))
597 fprintf (cp_lexer_debug_stream,
598 "cp_lexer: peeking ahead %ld at token: ", (long)n);
600 --n;
601 token = lexer->next_token;
602 gcc_assert (!n || token != &eof_token);
603 while (n != 0)
605 ++token;
606 if (token == lexer->last_token)
608 token = &eof_token;
609 break;
612 if (token->type != CPP_PURGED)
613 --n;
616 if (cp_lexer_debugging_p (lexer))
618 cp_lexer_print_token (cp_lexer_debug_stream, token);
619 putc ('\n', cp_lexer_debug_stream);
622 return token;
625 /* Return the next token, and advance the lexer's next_token pointer
626 to point to the next non-purged token. */
628 static cp_token *
629 cp_lexer_consume_token (cp_lexer* lexer)
631 cp_token *token = lexer->next_token;
633 gcc_assert (token != &eof_token);
634 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
638 lexer->next_token++;
639 if (lexer->next_token == lexer->last_token)
641 lexer->next_token = &eof_token;
642 break;
646 while (lexer->next_token->type == CPP_PURGED);
648 cp_lexer_set_source_position_from_token (token);
650 /* Provide debugging output. */
651 if (cp_lexer_debugging_p (lexer))
653 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
654 cp_lexer_print_token (cp_lexer_debug_stream, token);
655 putc ('\n', cp_lexer_debug_stream);
658 return token;
661 /* Permanently remove the next token from the token stream, and
662 advance the next_token pointer to refer to the next non-purged
663 token. */
665 static void
666 cp_lexer_purge_token (cp_lexer *lexer)
668 cp_token *tok = lexer->next_token;
670 gcc_assert (tok != &eof_token);
671 tok->type = CPP_PURGED;
672 tok->location = UNKNOWN_LOCATION;
673 tok->u.value = NULL_TREE;
674 tok->keyword = RID_MAX;
678 tok++;
679 if (tok == lexer->last_token)
681 tok = &eof_token;
682 break;
685 while (tok->type == CPP_PURGED);
686 lexer->next_token = tok;
689 /* Permanently remove all tokens after TOK, up to, but not
690 including, the token that will be returned next by
691 cp_lexer_peek_token. */
693 static void
694 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
696 cp_token *peek = lexer->next_token;
698 if (peek == &eof_token)
699 peek = lexer->last_token;
701 gcc_assert (tok < peek);
703 for ( tok += 1; tok != peek; tok += 1)
705 tok->type = CPP_PURGED;
706 tok->location = UNKNOWN_LOCATION;
707 tok->u.value = NULL_TREE;
708 tok->keyword = RID_MAX;
712 /* Begin saving tokens. All tokens consumed after this point will be
713 preserved. */
715 static void
716 cp_lexer_save_tokens (cp_lexer* lexer)
718 /* Provide debugging output. */
719 if (cp_lexer_debugging_p (lexer))
720 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
722 VEC_safe_push (cp_token_position, heap,
723 lexer->saved_tokens, lexer->next_token);
726 /* Commit to the portion of the token stream most recently saved. */
728 static void
729 cp_lexer_commit_tokens (cp_lexer* lexer)
731 /* Provide debugging output. */
732 if (cp_lexer_debugging_p (lexer))
733 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
735 VEC_pop (cp_token_position, lexer->saved_tokens);
738 /* Return all tokens saved since the last call to cp_lexer_save_tokens
739 to the token stream. Stop saving tokens. */
741 static void
742 cp_lexer_rollback_tokens (cp_lexer* lexer)
744 /* Provide debugging output. */
745 if (cp_lexer_debugging_p (lexer))
746 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
748 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
751 /* Print a representation of the TOKEN on the STREAM. */
753 #ifdef ENABLE_CHECKING
755 static void
756 cp_lexer_print_token (FILE * stream, cp_token *token)
758 /* We don't use cpp_type2name here because the parser defines
759 a few tokens of its own. */
760 static const char *const token_names[] = {
761 /* cpplib-defined token types */
762 #define OP(e, s) #e,
763 #define TK(e, s) #e,
764 TTYPE_TABLE
765 #undef OP
766 #undef TK
767 /* C++ parser token types - see "Manifest constants", above. */
768 "KEYWORD",
769 "TEMPLATE_ID",
770 "NESTED_NAME_SPECIFIER",
771 "PURGED"
774 /* If we have a name for the token, print it out. Otherwise, we
775 simply give the numeric code. */
776 gcc_assert (token->type < ARRAY_SIZE(token_names));
777 fputs (token_names[token->type], stream);
779 /* For some tokens, print the associated data. */
780 switch (token->type)
782 case CPP_KEYWORD:
783 /* Some keywords have a value that is not an IDENTIFIER_NODE.
784 For example, `struct' is mapped to an INTEGER_CST. */
785 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
786 break;
787 /* else fall through */
788 case CPP_NAME:
789 fputs (IDENTIFIER_POINTER (token->u.value), stream);
790 break;
792 case CPP_STRING:
793 case CPP_STRING16:
794 case CPP_STRING32:
795 case CPP_WSTRING:
796 case CPP_UTF8STRING:
797 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
798 break;
800 default:
801 break;
805 /* Start emitting debugging information. */
807 static void
808 cp_lexer_start_debugging (cp_lexer* lexer)
810 lexer->debugging_p = true;
813 /* Stop emitting debugging information. */
815 static void
816 cp_lexer_stop_debugging (cp_lexer* lexer)
818 lexer->debugging_p = false;
821 #endif /* ENABLE_CHECKING */
823 /* Create a new cp_token_cache, representing a range of tokens. */
825 static cp_token_cache *
826 cp_token_cache_new (cp_token *first, cp_token *last)
828 cp_token_cache *cache = GGC_NEW (cp_token_cache);
829 cache->first = first;
830 cache->last = last;
831 return cache;
835 /* Decl-specifiers. */
837 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
839 static void
840 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
842 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
845 /* Declarators. */
847 /* Nothing other than the parser should be creating declarators;
848 declarators are a semi-syntactic representation of C++ entities.
849 Other parts of the front end that need to create entities (like
850 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
852 static cp_declarator *make_call_declarator
853 (cp_declarator *, tree, cp_cv_quals, tree, tree);
854 static cp_declarator *make_array_declarator
855 (cp_declarator *, tree);
856 static cp_declarator *make_pointer_declarator
857 (cp_cv_quals, cp_declarator *);
858 static cp_declarator *make_reference_declarator
859 (cp_cv_quals, cp_declarator *, bool);
860 static cp_parameter_declarator *make_parameter_declarator
861 (cp_decl_specifier_seq *, cp_declarator *, tree);
862 static cp_declarator *make_ptrmem_declarator
863 (cp_cv_quals, tree, cp_declarator *);
865 /* An erroneous declarator. */
866 static cp_declarator *cp_error_declarator;
868 /* The obstack on which declarators and related data structures are
869 allocated. */
870 static struct obstack declarator_obstack;
872 /* Alloc BYTES from the declarator memory pool. */
874 static inline void *
875 alloc_declarator (size_t bytes)
877 return obstack_alloc (&declarator_obstack, bytes);
880 /* Allocate a declarator of the indicated KIND. Clear fields that are
881 common to all declarators. */
883 static cp_declarator *
884 make_declarator (cp_declarator_kind kind)
886 cp_declarator *declarator;
888 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
889 declarator->kind = kind;
890 declarator->attributes = NULL_TREE;
891 declarator->declarator = NULL;
892 declarator->parameter_pack_p = false;
893 declarator->id_loc = UNKNOWN_LOCATION;
895 return declarator;
898 /* Make a declarator for a generalized identifier. If
899 QUALIFYING_SCOPE is non-NULL, the identifier is
900 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
901 UNQUALIFIED_NAME. SFK indicates the kind of special function this
902 is, if any. */
904 static cp_declarator *
905 make_id_declarator (tree qualifying_scope, tree unqualified_name,
906 special_function_kind sfk)
908 cp_declarator *declarator;
910 /* It is valid to write:
912 class C { void f(); };
913 typedef C D;
914 void D::f();
916 The standard is not clear about whether `typedef const C D' is
917 legal; as of 2002-09-15 the committee is considering that
918 question. EDG 3.0 allows that syntax. Therefore, we do as
919 well. */
920 if (qualifying_scope && TYPE_P (qualifying_scope))
921 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
923 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
924 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
925 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
927 declarator = make_declarator (cdk_id);
928 declarator->u.id.qualifying_scope = qualifying_scope;
929 declarator->u.id.unqualified_name = unqualified_name;
930 declarator->u.id.sfk = sfk;
932 return declarator;
935 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
936 of modifiers such as const or volatile to apply to the pointer
937 type, represented as identifiers. */
939 cp_declarator *
940 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
942 cp_declarator *declarator;
944 declarator = make_declarator (cdk_pointer);
945 declarator->declarator = target;
946 declarator->u.pointer.qualifiers = cv_qualifiers;
947 declarator->u.pointer.class_type = NULL_TREE;
948 if (target)
950 declarator->parameter_pack_p = target->parameter_pack_p;
951 target->parameter_pack_p = false;
953 else
954 declarator->parameter_pack_p = false;
956 return declarator;
959 /* Like make_pointer_declarator -- but for references. */
961 cp_declarator *
962 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
963 bool rvalue_ref)
965 cp_declarator *declarator;
967 declarator = make_declarator (cdk_reference);
968 declarator->declarator = target;
969 declarator->u.reference.qualifiers = cv_qualifiers;
970 declarator->u.reference.rvalue_ref = rvalue_ref;
971 if (target)
973 declarator->parameter_pack_p = target->parameter_pack_p;
974 target->parameter_pack_p = false;
976 else
977 declarator->parameter_pack_p = false;
979 return declarator;
982 /* Like make_pointer_declarator -- but for a pointer to a non-static
983 member of CLASS_TYPE. */
985 cp_declarator *
986 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
987 cp_declarator *pointee)
989 cp_declarator *declarator;
991 declarator = make_declarator (cdk_ptrmem);
992 declarator->declarator = pointee;
993 declarator->u.pointer.qualifiers = cv_qualifiers;
994 declarator->u.pointer.class_type = class_type;
996 if (pointee)
998 declarator->parameter_pack_p = pointee->parameter_pack_p;
999 pointee->parameter_pack_p = false;
1001 else
1002 declarator->parameter_pack_p = false;
1004 return declarator;
1007 /* Make a declarator for the function given by TARGET, with the
1008 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1009 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1010 indicates what exceptions can be thrown. */
1012 cp_declarator *
1013 make_call_declarator (cp_declarator *target,
1014 tree parms,
1015 cp_cv_quals cv_qualifiers,
1016 tree exception_specification,
1017 tree late_return_type)
1019 cp_declarator *declarator;
1021 declarator = make_declarator (cdk_function);
1022 declarator->declarator = target;
1023 declarator->u.function.parameters = parms;
1024 declarator->u.function.qualifiers = cv_qualifiers;
1025 declarator->u.function.exception_specification = exception_specification;
1026 declarator->u.function.late_return_type = late_return_type;
1027 if (target)
1029 declarator->parameter_pack_p = target->parameter_pack_p;
1030 target->parameter_pack_p = false;
1032 else
1033 declarator->parameter_pack_p = false;
1035 return declarator;
1038 /* Make a declarator for an array of BOUNDS elements, each of which is
1039 defined by ELEMENT. */
1041 cp_declarator *
1042 make_array_declarator (cp_declarator *element, tree bounds)
1044 cp_declarator *declarator;
1046 declarator = make_declarator (cdk_array);
1047 declarator->declarator = element;
1048 declarator->u.array.bounds = bounds;
1049 if (element)
1051 declarator->parameter_pack_p = element->parameter_pack_p;
1052 element->parameter_pack_p = false;
1054 else
1055 declarator->parameter_pack_p = false;
1057 return declarator;
1060 /* Determine whether the declarator we've seen so far can be a
1061 parameter pack, when followed by an ellipsis. */
1062 static bool
1063 declarator_can_be_parameter_pack (cp_declarator *declarator)
1065 /* Search for a declarator name, or any other declarator that goes
1066 after the point where the ellipsis could appear in a parameter
1067 pack. If we find any of these, then this declarator can not be
1068 made into a parameter pack. */
1069 bool found = false;
1070 while (declarator && !found)
1072 switch ((int)declarator->kind)
1074 case cdk_id:
1075 case cdk_array:
1076 found = true;
1077 break;
1079 case cdk_error:
1080 return true;
1082 default:
1083 declarator = declarator->declarator;
1084 break;
1088 return !found;
1091 cp_parameter_declarator *no_parameters;
1093 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1094 DECLARATOR and DEFAULT_ARGUMENT. */
1096 cp_parameter_declarator *
1097 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1098 cp_declarator *declarator,
1099 tree default_argument)
1101 cp_parameter_declarator *parameter;
1103 parameter = ((cp_parameter_declarator *)
1104 alloc_declarator (sizeof (cp_parameter_declarator)));
1105 parameter->next = NULL;
1106 if (decl_specifiers)
1107 parameter->decl_specifiers = *decl_specifiers;
1108 else
1109 clear_decl_specs (&parameter->decl_specifiers);
1110 parameter->declarator = declarator;
1111 parameter->default_argument = default_argument;
1112 parameter->ellipsis_p = false;
1114 return parameter;
1117 /* Returns true iff DECLARATOR is a declaration for a function. */
1119 static bool
1120 function_declarator_p (const cp_declarator *declarator)
1122 while (declarator)
1124 if (declarator->kind == cdk_function
1125 && declarator->declarator->kind == cdk_id)
1126 return true;
1127 if (declarator->kind == cdk_id
1128 || declarator->kind == cdk_error)
1129 return false;
1130 declarator = declarator->declarator;
1132 return false;
1135 /* The parser. */
1137 /* Overview
1138 --------
1140 A cp_parser parses the token stream as specified by the C++
1141 grammar. Its job is purely parsing, not semantic analysis. For
1142 example, the parser breaks the token stream into declarators,
1143 expressions, statements, and other similar syntactic constructs.
1144 It does not check that the types of the expressions on either side
1145 of an assignment-statement are compatible, or that a function is
1146 not declared with a parameter of type `void'.
1148 The parser invokes routines elsewhere in the compiler to perform
1149 semantic analysis and to build up the abstract syntax tree for the
1150 code processed.
1152 The parser (and the template instantiation code, which is, in a
1153 way, a close relative of parsing) are the only parts of the
1154 compiler that should be calling push_scope and pop_scope, or
1155 related functions. The parser (and template instantiation code)
1156 keeps track of what scope is presently active; everything else
1157 should simply honor that. (The code that generates static
1158 initializers may also need to set the scope, in order to check
1159 access control correctly when emitting the initializers.)
1161 Methodology
1162 -----------
1164 The parser is of the standard recursive-descent variety. Upcoming
1165 tokens in the token stream are examined in order to determine which
1166 production to use when parsing a non-terminal. Some C++ constructs
1167 require arbitrary look ahead to disambiguate. For example, it is
1168 impossible, in the general case, to tell whether a statement is an
1169 expression or declaration without scanning the entire statement.
1170 Therefore, the parser is capable of "parsing tentatively." When the
1171 parser is not sure what construct comes next, it enters this mode.
1172 Then, while we attempt to parse the construct, the parser queues up
1173 error messages, rather than issuing them immediately, and saves the
1174 tokens it consumes. If the construct is parsed successfully, the
1175 parser "commits", i.e., it issues any queued error messages and
1176 the tokens that were being preserved are permanently discarded.
1177 If, however, the construct is not parsed successfully, the parser
1178 rolls back its state completely so that it can resume parsing using
1179 a different alternative.
1181 Future Improvements
1182 -------------------
1184 The performance of the parser could probably be improved substantially.
1185 We could often eliminate the need to parse tentatively by looking ahead
1186 a little bit. In some places, this approach might not entirely eliminate
1187 the need to parse tentatively, but it might still speed up the average
1188 case. */
1190 /* Flags that are passed to some parsing functions. These values can
1191 be bitwise-ored together. */
1193 enum
1195 /* No flags. */
1196 CP_PARSER_FLAGS_NONE = 0x0,
1197 /* The construct is optional. If it is not present, then no error
1198 should be issued. */
1199 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1200 /* When parsing a type-specifier, treat user-defined type-names
1201 as non-type identifiers. */
1202 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1203 /* When parsing a type-specifier, do not try to parse a class-specifier
1204 or enum-specifier. */
1205 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4
1208 /* This type is used for parameters and variables which hold
1209 combinations of the above flags. */
1210 typedef int cp_parser_flags;
1212 /* The different kinds of declarators we want to parse. */
1214 typedef enum cp_parser_declarator_kind
1216 /* We want an abstract declarator. */
1217 CP_PARSER_DECLARATOR_ABSTRACT,
1218 /* We want a named declarator. */
1219 CP_PARSER_DECLARATOR_NAMED,
1220 /* We don't mind, but the name must be an unqualified-id. */
1221 CP_PARSER_DECLARATOR_EITHER
1222 } cp_parser_declarator_kind;
1224 /* The precedence values used to parse binary expressions. The minimum value
1225 of PREC must be 1, because zero is reserved to quickly discriminate
1226 binary operators from other tokens. */
1228 enum cp_parser_prec
1230 PREC_NOT_OPERATOR,
1231 PREC_LOGICAL_OR_EXPRESSION,
1232 PREC_LOGICAL_AND_EXPRESSION,
1233 PREC_INCLUSIVE_OR_EXPRESSION,
1234 PREC_EXCLUSIVE_OR_EXPRESSION,
1235 PREC_AND_EXPRESSION,
1236 PREC_EQUALITY_EXPRESSION,
1237 PREC_RELATIONAL_EXPRESSION,
1238 PREC_SHIFT_EXPRESSION,
1239 PREC_ADDITIVE_EXPRESSION,
1240 PREC_MULTIPLICATIVE_EXPRESSION,
1241 PREC_PM_EXPRESSION,
1242 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1245 /* A mapping from a token type to a corresponding tree node type, with a
1246 precedence value. */
1248 typedef struct cp_parser_binary_operations_map_node
1250 /* The token type. */
1251 enum cpp_ttype token_type;
1252 /* The corresponding tree code. */
1253 enum tree_code tree_type;
1254 /* The precedence of this operator. */
1255 enum cp_parser_prec prec;
1256 } cp_parser_binary_operations_map_node;
1258 /* The status of a tentative parse. */
1260 typedef enum cp_parser_status_kind
1262 /* No errors have occurred. */
1263 CP_PARSER_STATUS_KIND_NO_ERROR,
1264 /* An error has occurred. */
1265 CP_PARSER_STATUS_KIND_ERROR,
1266 /* We are committed to this tentative parse, whether or not an error
1267 has occurred. */
1268 CP_PARSER_STATUS_KIND_COMMITTED
1269 } cp_parser_status_kind;
1271 typedef struct cp_parser_expression_stack_entry
1273 /* Left hand side of the binary operation we are currently
1274 parsing. */
1275 tree lhs;
1276 /* Original tree code for left hand side, if it was a binary
1277 expression itself (used for -Wparentheses). */
1278 enum tree_code lhs_type;
1279 /* Tree code for the binary operation we are parsing. */
1280 enum tree_code tree_type;
1281 /* Precedence of the binary operation we are parsing. */
1282 enum cp_parser_prec prec;
1283 } cp_parser_expression_stack_entry;
1285 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1286 entries because precedence levels on the stack are monotonically
1287 increasing. */
1288 typedef struct cp_parser_expression_stack_entry
1289 cp_parser_expression_stack[NUM_PREC_VALUES];
1291 /* Context that is saved and restored when parsing tentatively. */
1292 typedef struct GTY (()) cp_parser_context {
1293 /* If this is a tentative parsing context, the status of the
1294 tentative parse. */
1295 enum cp_parser_status_kind status;
1296 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1297 that are looked up in this context must be looked up both in the
1298 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1299 the context of the containing expression. */
1300 tree object_type;
1302 /* The next parsing context in the stack. */
1303 struct cp_parser_context *next;
1304 } cp_parser_context;
1306 /* Prototypes. */
1308 /* Constructors and destructors. */
1310 static cp_parser_context *cp_parser_context_new
1311 (cp_parser_context *);
1313 /* Class variables. */
1315 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1317 /* The operator-precedence table used by cp_parser_binary_expression.
1318 Transformed into an associative array (binops_by_token) by
1319 cp_parser_new. */
1321 static const cp_parser_binary_operations_map_node binops[] = {
1322 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1323 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1325 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1326 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1327 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1329 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1330 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1332 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1333 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1335 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1336 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1337 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1338 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1340 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1341 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1343 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1345 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1347 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1349 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1351 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1354 /* The same as binops, but initialized by cp_parser_new so that
1355 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1356 for speed. */
1357 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1359 /* Constructors and destructors. */
1361 /* Construct a new context. The context below this one on the stack
1362 is given by NEXT. */
1364 static cp_parser_context *
1365 cp_parser_context_new (cp_parser_context* next)
1367 cp_parser_context *context;
1369 /* Allocate the storage. */
1370 if (cp_parser_context_free_list != NULL)
1372 /* Pull the first entry from the free list. */
1373 context = cp_parser_context_free_list;
1374 cp_parser_context_free_list = context->next;
1375 memset (context, 0, sizeof (*context));
1377 else
1378 context = GGC_CNEW (cp_parser_context);
1380 /* No errors have occurred yet in this context. */
1381 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1382 /* If this is not the bottommost context, copy information that we
1383 need from the previous context. */
1384 if (next)
1386 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1387 expression, then we are parsing one in this context, too. */
1388 context->object_type = next->object_type;
1389 /* Thread the stack. */
1390 context->next = next;
1393 return context;
1396 /* The cp_parser structure represents the C++ parser. */
1398 typedef struct GTY(()) cp_parser {
1399 /* The lexer from which we are obtaining tokens. */
1400 cp_lexer *lexer;
1402 /* The scope in which names should be looked up. If NULL_TREE, then
1403 we look up names in the scope that is currently open in the
1404 source program. If non-NULL, this is either a TYPE or
1405 NAMESPACE_DECL for the scope in which we should look. It can
1406 also be ERROR_MARK, when we've parsed a bogus scope.
1408 This value is not cleared automatically after a name is looked
1409 up, so we must be careful to clear it before starting a new look
1410 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1411 will look up `Z' in the scope of `X', rather than the current
1412 scope.) Unfortunately, it is difficult to tell when name lookup
1413 is complete, because we sometimes peek at a token, look it up,
1414 and then decide not to consume it. */
1415 tree scope;
1417 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1418 last lookup took place. OBJECT_SCOPE is used if an expression
1419 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1420 respectively. QUALIFYING_SCOPE is used for an expression of the
1421 form "X::Y"; it refers to X. */
1422 tree object_scope;
1423 tree qualifying_scope;
1425 /* A stack of parsing contexts. All but the bottom entry on the
1426 stack will be tentative contexts.
1428 We parse tentatively in order to determine which construct is in
1429 use in some situations. For example, in order to determine
1430 whether a statement is an expression-statement or a
1431 declaration-statement we parse it tentatively as a
1432 declaration-statement. If that fails, we then reparse the same
1433 token stream as an expression-statement. */
1434 cp_parser_context *context;
1436 /* True if we are parsing GNU C++. If this flag is not set, then
1437 GNU extensions are not recognized. */
1438 bool allow_gnu_extensions_p;
1440 /* TRUE if the `>' token should be interpreted as the greater-than
1441 operator. FALSE if it is the end of a template-id or
1442 template-parameter-list. In C++0x mode, this flag also applies to
1443 `>>' tokens, which are viewed as two consecutive `>' tokens when
1444 this flag is FALSE. */
1445 bool greater_than_is_operator_p;
1447 /* TRUE if default arguments are allowed within a parameter list
1448 that starts at this point. FALSE if only a gnu extension makes
1449 them permissible. */
1450 bool default_arg_ok_p;
1452 /* TRUE if we are parsing an integral constant-expression. See
1453 [expr.const] for a precise definition. */
1454 bool integral_constant_expression_p;
1456 /* TRUE if we are parsing an integral constant-expression -- but a
1457 non-constant expression should be permitted as well. This flag
1458 is used when parsing an array bound so that GNU variable-length
1459 arrays are tolerated. */
1460 bool allow_non_integral_constant_expression_p;
1462 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1463 been seen that makes the expression non-constant. */
1464 bool non_integral_constant_expression_p;
1466 /* TRUE if local variable names and `this' are forbidden in the
1467 current context. */
1468 bool local_variables_forbidden_p;
1470 /* TRUE if the declaration we are parsing is part of a
1471 linkage-specification of the form `extern string-literal
1472 declaration'. */
1473 bool in_unbraced_linkage_specification_p;
1475 /* TRUE if we are presently parsing a declarator, after the
1476 direct-declarator. */
1477 bool in_declarator_p;
1479 /* TRUE if we are presently parsing a template-argument-list. */
1480 bool in_template_argument_list_p;
1482 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1483 to IN_OMP_BLOCK if parsing OpenMP structured block and
1484 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1485 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1486 iteration-statement, OpenMP block or loop within that switch. */
1487 #define IN_SWITCH_STMT 1
1488 #define IN_ITERATION_STMT 2
1489 #define IN_OMP_BLOCK 4
1490 #define IN_OMP_FOR 8
1491 #define IN_IF_STMT 16
1492 unsigned char in_statement;
1494 /* TRUE if we are presently parsing the body of a switch statement.
1495 Note that this doesn't quite overlap with in_statement above.
1496 The difference relates to giving the right sets of error messages:
1497 "case not in switch" vs "break statement used with OpenMP...". */
1498 bool in_switch_statement_p;
1500 /* TRUE if we are parsing a type-id in an expression context. In
1501 such a situation, both "type (expr)" and "type (type)" are valid
1502 alternatives. */
1503 bool in_type_id_in_expr_p;
1505 /* TRUE if we are currently in a header file where declarations are
1506 implicitly extern "C". */
1507 bool implicit_extern_c;
1509 /* TRUE if strings in expressions should be translated to the execution
1510 character set. */
1511 bool translate_strings_p;
1513 /* TRUE if we are presently parsing the body of a function, but not
1514 a local class. */
1515 bool in_function_body;
1517 /* If non-NULL, then we are parsing a construct where new type
1518 definitions are not permitted. The string stored here will be
1519 issued as an error message if a type is defined. */
1520 const char *type_definition_forbidden_message;
1522 /* A list of lists. The outer list is a stack, used for member
1523 functions of local classes. At each level there are two sub-list,
1524 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1525 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1526 TREE_VALUE's. The functions are chained in reverse declaration
1527 order.
1529 The TREE_PURPOSE sublist contains those functions with default
1530 arguments that need post processing, and the TREE_VALUE sublist
1531 contains those functions with definitions that need post
1532 processing.
1534 These lists can only be processed once the outermost class being
1535 defined is complete. */
1536 tree unparsed_functions_queues;
1538 /* The number of classes whose definitions are currently in
1539 progress. */
1540 unsigned num_classes_being_defined;
1542 /* The number of template parameter lists that apply directly to the
1543 current declaration. */
1544 unsigned num_template_parameter_lists;
1545 } cp_parser;
1547 /* Prototypes. */
1549 /* Constructors and destructors. */
1551 static cp_parser *cp_parser_new
1552 (void);
1554 /* Routines to parse various constructs.
1556 Those that return `tree' will return the error_mark_node (rather
1557 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1558 Sometimes, they will return an ordinary node if error-recovery was
1559 attempted, even though a parse error occurred. So, to check
1560 whether or not a parse error occurred, you should always use
1561 cp_parser_error_occurred. If the construct is optional (indicated
1562 either by an `_opt' in the name of the function that does the
1563 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1564 the construct is not present. */
1566 /* Lexical conventions [gram.lex] */
1568 static tree cp_parser_identifier
1569 (cp_parser *);
1570 static tree cp_parser_string_literal
1571 (cp_parser *, bool, bool);
1573 /* Basic concepts [gram.basic] */
1575 static bool cp_parser_translation_unit
1576 (cp_parser *);
1578 /* Expressions [gram.expr] */
1580 static tree cp_parser_primary_expression
1581 (cp_parser *, bool, bool, bool, cp_id_kind *);
1582 static tree cp_parser_id_expression
1583 (cp_parser *, bool, bool, bool *, bool, bool);
1584 static tree cp_parser_unqualified_id
1585 (cp_parser *, bool, bool, bool, bool);
1586 static tree cp_parser_nested_name_specifier_opt
1587 (cp_parser *, bool, bool, bool, bool);
1588 static tree cp_parser_nested_name_specifier
1589 (cp_parser *, bool, bool, bool, bool);
1590 static tree cp_parser_qualifying_entity
1591 (cp_parser *, bool, bool, bool, bool, bool);
1592 static tree cp_parser_postfix_expression
1593 (cp_parser *, bool, bool, bool, cp_id_kind *);
1594 static tree cp_parser_postfix_open_square_expression
1595 (cp_parser *, tree, bool);
1596 static tree cp_parser_postfix_dot_deref_expression
1597 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1598 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1599 (cp_parser *, int, bool, bool, bool *);
1600 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1601 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1602 static void cp_parser_pseudo_destructor_name
1603 (cp_parser *, tree *, tree *);
1604 static tree cp_parser_unary_expression
1605 (cp_parser *, bool, bool, cp_id_kind *);
1606 static enum tree_code cp_parser_unary_operator
1607 (cp_token *);
1608 static tree cp_parser_new_expression
1609 (cp_parser *);
1610 static VEC(tree,gc) *cp_parser_new_placement
1611 (cp_parser *);
1612 static tree cp_parser_new_type_id
1613 (cp_parser *, tree *);
1614 static cp_declarator *cp_parser_new_declarator_opt
1615 (cp_parser *);
1616 static cp_declarator *cp_parser_direct_new_declarator
1617 (cp_parser *);
1618 static VEC(tree,gc) *cp_parser_new_initializer
1619 (cp_parser *);
1620 static tree cp_parser_delete_expression
1621 (cp_parser *);
1622 static tree cp_parser_cast_expression
1623 (cp_parser *, bool, bool, cp_id_kind *);
1624 static tree cp_parser_binary_expression
1625 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1626 static tree cp_parser_question_colon_clause
1627 (cp_parser *, tree);
1628 static tree cp_parser_assignment_expression
1629 (cp_parser *, bool, cp_id_kind *);
1630 static enum tree_code cp_parser_assignment_operator_opt
1631 (cp_parser *);
1632 static tree cp_parser_expression
1633 (cp_parser *, bool, cp_id_kind *);
1634 static tree cp_parser_constant_expression
1635 (cp_parser *, bool, bool *);
1636 static tree cp_parser_builtin_offsetof
1637 (cp_parser *);
1638 static tree cp_parser_lambda_expression
1639 (cp_parser *);
1640 static void cp_parser_lambda_introducer
1641 (cp_parser *, tree);
1642 static void cp_parser_lambda_declarator_opt
1643 (cp_parser *, tree);
1644 static void cp_parser_lambda_body
1645 (cp_parser *, tree);
1647 /* Statements [gram.stmt.stmt] */
1649 static void cp_parser_statement
1650 (cp_parser *, tree, bool, bool *);
1651 static void cp_parser_label_for_labeled_statement
1652 (cp_parser *);
1653 static tree cp_parser_expression_statement
1654 (cp_parser *, tree);
1655 static tree cp_parser_compound_statement
1656 (cp_parser *, tree, bool);
1657 static void cp_parser_statement_seq_opt
1658 (cp_parser *, tree);
1659 static tree cp_parser_selection_statement
1660 (cp_parser *, bool *);
1661 static tree cp_parser_condition
1662 (cp_parser *);
1663 static tree cp_parser_iteration_statement
1664 (cp_parser *);
1665 static void cp_parser_for_init_statement
1666 (cp_parser *);
1667 static tree cp_parser_jump_statement
1668 (cp_parser *);
1669 static void cp_parser_declaration_statement
1670 (cp_parser *);
1672 static tree cp_parser_implicitly_scoped_statement
1673 (cp_parser *, bool *);
1674 static void cp_parser_already_scoped_statement
1675 (cp_parser *);
1677 /* Declarations [gram.dcl.dcl] */
1679 static void cp_parser_declaration_seq_opt
1680 (cp_parser *);
1681 static void cp_parser_declaration
1682 (cp_parser *);
1683 static void cp_parser_block_declaration
1684 (cp_parser *, bool);
1685 static void cp_parser_simple_declaration
1686 (cp_parser *, bool);
1687 static void cp_parser_decl_specifier_seq
1688 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1689 static tree cp_parser_storage_class_specifier_opt
1690 (cp_parser *);
1691 static tree cp_parser_function_specifier_opt
1692 (cp_parser *, cp_decl_specifier_seq *);
1693 static tree cp_parser_type_specifier
1694 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1695 int *, bool *);
1696 static tree cp_parser_simple_type_specifier
1697 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1698 static tree cp_parser_type_name
1699 (cp_parser *);
1700 static tree cp_parser_nonclass_name
1701 (cp_parser* parser);
1702 static tree cp_parser_elaborated_type_specifier
1703 (cp_parser *, bool, bool);
1704 static tree cp_parser_enum_specifier
1705 (cp_parser *);
1706 static void cp_parser_enumerator_list
1707 (cp_parser *, tree);
1708 static void cp_parser_enumerator_definition
1709 (cp_parser *, tree);
1710 static tree cp_parser_namespace_name
1711 (cp_parser *);
1712 static void cp_parser_namespace_definition
1713 (cp_parser *);
1714 static void cp_parser_namespace_body
1715 (cp_parser *);
1716 static tree cp_parser_qualified_namespace_specifier
1717 (cp_parser *);
1718 static void cp_parser_namespace_alias_definition
1719 (cp_parser *);
1720 static bool cp_parser_using_declaration
1721 (cp_parser *, bool);
1722 static void cp_parser_using_directive
1723 (cp_parser *);
1724 static void cp_parser_asm_definition
1725 (cp_parser *);
1726 static void cp_parser_linkage_specification
1727 (cp_parser *);
1728 static void cp_parser_static_assert
1729 (cp_parser *, bool);
1730 static tree cp_parser_decltype
1731 (cp_parser *);
1733 /* Declarators [gram.dcl.decl] */
1735 static tree cp_parser_init_declarator
1736 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1737 static cp_declarator *cp_parser_declarator
1738 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1739 static cp_declarator *cp_parser_direct_declarator
1740 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1741 static enum tree_code cp_parser_ptr_operator
1742 (cp_parser *, tree *, cp_cv_quals *);
1743 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1744 (cp_parser *);
1745 static tree cp_parser_late_return_type_opt
1746 (cp_parser *);
1747 static tree cp_parser_declarator_id
1748 (cp_parser *, bool);
1749 static tree cp_parser_type_id
1750 (cp_parser *);
1751 static tree cp_parser_template_type_arg
1752 (cp_parser *);
1753 static tree cp_parser_trailing_type_id (cp_parser *);
1754 static tree cp_parser_type_id_1
1755 (cp_parser *, bool, bool);
1756 static void cp_parser_type_specifier_seq
1757 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1758 static tree cp_parser_parameter_declaration_clause
1759 (cp_parser *);
1760 static tree cp_parser_parameter_declaration_list
1761 (cp_parser *, bool *);
1762 static cp_parameter_declarator *cp_parser_parameter_declaration
1763 (cp_parser *, bool, bool *);
1764 static tree cp_parser_default_argument
1765 (cp_parser *, bool);
1766 static void cp_parser_function_body
1767 (cp_parser *);
1768 static tree cp_parser_initializer
1769 (cp_parser *, bool *, bool *);
1770 static tree cp_parser_initializer_clause
1771 (cp_parser *, bool *);
1772 static tree cp_parser_braced_list
1773 (cp_parser*, bool*);
1774 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1775 (cp_parser *, bool *);
1777 static bool cp_parser_ctor_initializer_opt_and_function_body
1778 (cp_parser *);
1780 /* Classes [gram.class] */
1782 static tree cp_parser_class_name
1783 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1784 static tree cp_parser_class_specifier
1785 (cp_parser *);
1786 static tree cp_parser_class_head
1787 (cp_parser *, bool *, tree *, tree *);
1788 static enum tag_types cp_parser_class_key
1789 (cp_parser *);
1790 static void cp_parser_member_specification_opt
1791 (cp_parser *);
1792 static void cp_parser_member_declaration
1793 (cp_parser *);
1794 static tree cp_parser_pure_specifier
1795 (cp_parser *);
1796 static tree cp_parser_constant_initializer
1797 (cp_parser *);
1799 /* Derived classes [gram.class.derived] */
1801 static tree cp_parser_base_clause
1802 (cp_parser *);
1803 static tree cp_parser_base_specifier
1804 (cp_parser *);
1806 /* Special member functions [gram.special] */
1808 static tree cp_parser_conversion_function_id
1809 (cp_parser *);
1810 static tree cp_parser_conversion_type_id
1811 (cp_parser *);
1812 static cp_declarator *cp_parser_conversion_declarator_opt
1813 (cp_parser *);
1814 static bool cp_parser_ctor_initializer_opt
1815 (cp_parser *);
1816 static void cp_parser_mem_initializer_list
1817 (cp_parser *);
1818 static tree cp_parser_mem_initializer
1819 (cp_parser *);
1820 static tree cp_parser_mem_initializer_id
1821 (cp_parser *);
1823 /* Overloading [gram.over] */
1825 static tree cp_parser_operator_function_id
1826 (cp_parser *);
1827 static tree cp_parser_operator
1828 (cp_parser *);
1830 /* Templates [gram.temp] */
1832 static void cp_parser_template_declaration
1833 (cp_parser *, bool);
1834 static tree cp_parser_template_parameter_list
1835 (cp_parser *);
1836 static tree cp_parser_template_parameter
1837 (cp_parser *, bool *, bool *);
1838 static tree cp_parser_type_parameter
1839 (cp_parser *, bool *);
1840 static tree cp_parser_template_id
1841 (cp_parser *, bool, bool, bool);
1842 static tree cp_parser_template_name
1843 (cp_parser *, bool, bool, bool, bool *);
1844 static tree cp_parser_template_argument_list
1845 (cp_parser *);
1846 static tree cp_parser_template_argument
1847 (cp_parser *);
1848 static void cp_parser_explicit_instantiation
1849 (cp_parser *);
1850 static void cp_parser_explicit_specialization
1851 (cp_parser *);
1853 /* Exception handling [gram.exception] */
1855 static tree cp_parser_try_block
1856 (cp_parser *);
1857 static bool cp_parser_function_try_block
1858 (cp_parser *);
1859 static void cp_parser_handler_seq
1860 (cp_parser *);
1861 static void cp_parser_handler
1862 (cp_parser *);
1863 static tree cp_parser_exception_declaration
1864 (cp_parser *);
1865 static tree cp_parser_throw_expression
1866 (cp_parser *);
1867 static tree cp_parser_exception_specification_opt
1868 (cp_parser *);
1869 static tree cp_parser_type_id_list
1870 (cp_parser *);
1872 /* GNU Extensions */
1874 static tree cp_parser_asm_specification_opt
1875 (cp_parser *);
1876 static tree cp_parser_asm_operand_list
1877 (cp_parser *);
1878 static tree cp_parser_asm_clobber_list
1879 (cp_parser *);
1880 static tree cp_parser_asm_label_list
1881 (cp_parser *);
1882 static tree cp_parser_attributes_opt
1883 (cp_parser *);
1884 static tree cp_parser_attribute_list
1885 (cp_parser *);
1886 static bool cp_parser_extension_opt
1887 (cp_parser *, int *);
1888 static void cp_parser_label_declaration
1889 (cp_parser *);
1891 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1892 static bool cp_parser_pragma
1893 (cp_parser *, enum pragma_context);
1895 /* Objective-C++ Productions */
1897 static tree cp_parser_objc_message_receiver
1898 (cp_parser *);
1899 static tree cp_parser_objc_message_args
1900 (cp_parser *);
1901 static tree cp_parser_objc_message_expression
1902 (cp_parser *);
1903 static tree cp_parser_objc_encode_expression
1904 (cp_parser *);
1905 static tree cp_parser_objc_defs_expression
1906 (cp_parser *);
1907 static tree cp_parser_objc_protocol_expression
1908 (cp_parser *);
1909 static tree cp_parser_objc_selector_expression
1910 (cp_parser *);
1911 static tree cp_parser_objc_expression
1912 (cp_parser *);
1913 static bool cp_parser_objc_selector_p
1914 (enum cpp_ttype);
1915 static tree cp_parser_objc_selector
1916 (cp_parser *);
1917 static tree cp_parser_objc_protocol_refs_opt
1918 (cp_parser *);
1919 static void cp_parser_objc_declaration
1920 (cp_parser *);
1921 static tree cp_parser_objc_statement
1922 (cp_parser *);
1924 /* Utility Routines */
1926 static tree cp_parser_lookup_name
1927 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1928 static tree cp_parser_lookup_name_simple
1929 (cp_parser *, tree, location_t);
1930 static tree cp_parser_maybe_treat_template_as_class
1931 (tree, bool);
1932 static bool cp_parser_check_declarator_template_parameters
1933 (cp_parser *, cp_declarator *, location_t);
1934 static bool cp_parser_check_template_parameters
1935 (cp_parser *, unsigned, location_t, cp_declarator *);
1936 static tree cp_parser_simple_cast_expression
1937 (cp_parser *);
1938 static tree cp_parser_global_scope_opt
1939 (cp_parser *, bool);
1940 static bool cp_parser_constructor_declarator_p
1941 (cp_parser *, bool);
1942 static tree cp_parser_function_definition_from_specifiers_and_declarator
1943 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1944 static tree cp_parser_function_definition_after_declarator
1945 (cp_parser *, bool);
1946 static void cp_parser_template_declaration_after_export
1947 (cp_parser *, bool);
1948 static void cp_parser_perform_template_parameter_access_checks
1949 (VEC (deferred_access_check,gc)*);
1950 static tree cp_parser_single_declaration
1951 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1952 static tree cp_parser_functional_cast
1953 (cp_parser *, tree);
1954 static tree cp_parser_save_member_function_body
1955 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1956 static tree cp_parser_enclosed_template_argument_list
1957 (cp_parser *);
1958 static void cp_parser_save_default_args
1959 (cp_parser *, tree);
1960 static void cp_parser_late_parsing_for_member
1961 (cp_parser *, tree);
1962 static void cp_parser_late_parsing_default_args
1963 (cp_parser *, tree);
1964 static tree cp_parser_sizeof_operand
1965 (cp_parser *, enum rid);
1966 static tree cp_parser_trait_expr
1967 (cp_parser *, enum rid);
1968 static bool cp_parser_declares_only_class_p
1969 (cp_parser *);
1970 static void cp_parser_set_storage_class
1971 (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1972 static void cp_parser_set_decl_spec_type
1973 (cp_decl_specifier_seq *, tree, location_t, bool);
1974 static bool cp_parser_friend_p
1975 (const cp_decl_specifier_seq *);
1976 static cp_token *cp_parser_require
1977 (cp_parser *, enum cpp_ttype, const char *);
1978 static cp_token *cp_parser_require_keyword
1979 (cp_parser *, enum rid, const char *);
1980 static bool cp_parser_token_starts_function_definition_p
1981 (cp_token *);
1982 static bool cp_parser_next_token_starts_class_definition_p
1983 (cp_parser *);
1984 static bool cp_parser_next_token_ends_template_argument_p
1985 (cp_parser *);
1986 static bool cp_parser_nth_token_starts_template_argument_list_p
1987 (cp_parser *, size_t);
1988 static enum tag_types cp_parser_token_is_class_key
1989 (cp_token *);
1990 static void cp_parser_check_class_key
1991 (enum tag_types, tree type);
1992 static void cp_parser_check_access_in_redeclaration
1993 (tree type, location_t location);
1994 static bool cp_parser_optional_template_keyword
1995 (cp_parser *);
1996 static void cp_parser_pre_parsed_nested_name_specifier
1997 (cp_parser *);
1998 static bool cp_parser_cache_group
1999 (cp_parser *, enum cpp_ttype, unsigned);
2000 static void cp_parser_parse_tentatively
2001 (cp_parser *);
2002 static void cp_parser_commit_to_tentative_parse
2003 (cp_parser *);
2004 static void cp_parser_abort_tentative_parse
2005 (cp_parser *);
2006 static bool cp_parser_parse_definitely
2007 (cp_parser *);
2008 static inline bool cp_parser_parsing_tentatively
2009 (cp_parser *);
2010 static bool cp_parser_uncommitted_to_tentative_parse_p
2011 (cp_parser *);
2012 static void cp_parser_error
2013 (cp_parser *, const char *);
2014 static void cp_parser_name_lookup_error
2015 (cp_parser *, tree, tree, const char *, location_t);
2016 static bool cp_parser_simulate_error
2017 (cp_parser *);
2018 static bool cp_parser_check_type_definition
2019 (cp_parser *);
2020 static void cp_parser_check_for_definition_in_return_type
2021 (cp_declarator *, tree, location_t type_location);
2022 static void cp_parser_check_for_invalid_template_id
2023 (cp_parser *, tree, location_t location);
2024 static bool cp_parser_non_integral_constant_expression
2025 (cp_parser *, const char *);
2026 static void cp_parser_diagnose_invalid_type_name
2027 (cp_parser *, tree, tree, location_t);
2028 static bool cp_parser_parse_and_diagnose_invalid_type_name
2029 (cp_parser *);
2030 static int cp_parser_skip_to_closing_parenthesis
2031 (cp_parser *, bool, bool, bool);
2032 static void cp_parser_skip_to_end_of_statement
2033 (cp_parser *);
2034 static void cp_parser_consume_semicolon_at_end_of_statement
2035 (cp_parser *);
2036 static void cp_parser_skip_to_end_of_block_or_statement
2037 (cp_parser *);
2038 static bool cp_parser_skip_to_closing_brace
2039 (cp_parser *);
2040 static void cp_parser_skip_to_end_of_template_parameter_list
2041 (cp_parser *);
2042 static void cp_parser_skip_to_pragma_eol
2043 (cp_parser*, cp_token *);
2044 static bool cp_parser_error_occurred
2045 (cp_parser *);
2046 static bool cp_parser_allow_gnu_extensions_p
2047 (cp_parser *);
2048 static bool cp_parser_is_string_literal
2049 (cp_token *);
2050 static bool cp_parser_is_keyword
2051 (cp_token *, enum rid);
2052 static tree cp_parser_make_typename_type
2053 (cp_parser *, tree, tree, location_t location);
2054 static cp_declarator * cp_parser_make_indirect_declarator
2055 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2057 /* Returns nonzero if we are parsing tentatively. */
2059 static inline bool
2060 cp_parser_parsing_tentatively (cp_parser* parser)
2062 return parser->context->next != NULL;
2065 /* Returns nonzero if TOKEN is a string literal. */
2067 static bool
2068 cp_parser_is_string_literal (cp_token* token)
2070 return (token->type == CPP_STRING ||
2071 token->type == CPP_STRING16 ||
2072 token->type == CPP_STRING32 ||
2073 token->type == CPP_WSTRING ||
2074 token->type == CPP_UTF8STRING);
2077 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2079 static bool
2080 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2082 return token->keyword == keyword;
2085 /* If not parsing tentatively, issue a diagnostic of the form
2086 FILE:LINE: MESSAGE before TOKEN
2087 where TOKEN is the next token in the input stream. MESSAGE
2088 (specified by the caller) is usually of the form "expected
2089 OTHER-TOKEN". */
2091 static void
2092 cp_parser_error (cp_parser* parser, const char* message)
2094 if (!cp_parser_simulate_error (parser))
2096 cp_token *token = cp_lexer_peek_token (parser->lexer);
2097 /* This diagnostic makes more sense if it is tagged to the line
2098 of the token we just peeked at. */
2099 cp_lexer_set_source_position_from_token (token);
2101 if (token->type == CPP_PRAGMA)
2103 error_at (token->location,
2104 "%<#pragma%> is not allowed here");
2105 cp_parser_skip_to_pragma_eol (parser, token);
2106 return;
2109 c_parse_error (message,
2110 /* Because c_parser_error does not understand
2111 CPP_KEYWORD, keywords are treated like
2112 identifiers. */
2113 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2114 token->u.value, token->flags);
2118 /* Issue an error about name-lookup failing. NAME is the
2119 IDENTIFIER_NODE DECL is the result of
2120 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2121 the thing that we hoped to find. */
2123 static void
2124 cp_parser_name_lookup_error (cp_parser* parser,
2125 tree name,
2126 tree decl,
2127 const char* desired,
2128 location_t location)
2130 /* If name lookup completely failed, tell the user that NAME was not
2131 declared. */
2132 if (decl == error_mark_node)
2134 if (parser->scope && parser->scope != global_namespace)
2135 error_at (location, "%<%E::%E%> has not been declared",
2136 parser->scope, name);
2137 else if (parser->scope == global_namespace)
2138 error_at (location, "%<::%E%> has not been declared", name);
2139 else if (parser->object_scope
2140 && !CLASS_TYPE_P (parser->object_scope))
2141 error_at (location, "request for member %qE in non-class type %qT",
2142 name, parser->object_scope);
2143 else if (parser->object_scope)
2144 error_at (location, "%<%T::%E%> has not been declared",
2145 parser->object_scope, name);
2146 else
2147 error_at (location, "%qE has not been declared", name);
2149 else if (parser->scope && parser->scope != global_namespace)
2150 error_at (location, "%<%E::%E%> %s", parser->scope, name, desired);
2151 else if (parser->scope == global_namespace)
2152 error_at (location, "%<::%E%> %s", name, desired);
2153 else
2154 error_at (location, "%qE %s", name, desired);
2157 /* If we are parsing tentatively, remember that an error has occurred
2158 during this tentative parse. Returns true if the error was
2159 simulated; false if a message should be issued by the caller. */
2161 static bool
2162 cp_parser_simulate_error (cp_parser* parser)
2164 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2166 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2167 return true;
2169 return false;
2172 /* Check for repeated decl-specifiers. */
2174 static void
2175 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2176 location_t location)
2178 int ds;
2180 for (ds = ds_first; ds != ds_last; ++ds)
2182 unsigned count = decl_specs->specs[ds];
2183 if (count < 2)
2184 continue;
2185 /* The "long" specifier is a special case because of "long long". */
2186 if (ds == ds_long)
2188 if (count > 2)
2189 error_at (location, "%<long long long%> is too long for GCC");
2190 else
2191 pedwarn_cxx98 (location, OPT_Wlong_long,
2192 "ISO C++ 1998 does not support %<long long%>");
2194 else if (count > 1)
2196 static const char *const decl_spec_names[] = {
2197 "signed",
2198 "unsigned",
2199 "short",
2200 "long",
2201 "const",
2202 "volatile",
2203 "restrict",
2204 "inline",
2205 "virtual",
2206 "explicit",
2207 "friend",
2208 "typedef",
2209 "constexpr",
2210 "__complex",
2211 "__thread"
2213 error_at (location, "duplicate %qs", decl_spec_names[ds]);
2218 /* This function is called when a type is defined. If type
2219 definitions are forbidden at this point, an error message is
2220 issued. */
2222 static bool
2223 cp_parser_check_type_definition (cp_parser* parser)
2225 /* If types are forbidden here, issue a message. */
2226 if (parser->type_definition_forbidden_message)
2228 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2229 in the message need to be interpreted. */
2230 error (parser->type_definition_forbidden_message);
2231 return false;
2233 return true;
2236 /* This function is called when the DECLARATOR is processed. The TYPE
2237 was a type defined in the decl-specifiers. If it is invalid to
2238 define a type in the decl-specifiers for DECLARATOR, an error is
2239 issued. TYPE_LOCATION is the location of TYPE and is used
2240 for error reporting. */
2242 static void
2243 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2244 tree type, location_t type_location)
2246 /* [dcl.fct] forbids type definitions in return types.
2247 Unfortunately, it's not easy to know whether or not we are
2248 processing a return type until after the fact. */
2249 while (declarator
2250 && (declarator->kind == cdk_pointer
2251 || declarator->kind == cdk_reference
2252 || declarator->kind == cdk_ptrmem))
2253 declarator = declarator->declarator;
2254 if (declarator
2255 && declarator->kind == cdk_function)
2257 error_at (type_location,
2258 "new types may not be defined in a return type");
2259 inform (type_location,
2260 "(perhaps a semicolon is missing after the definition of %qT)",
2261 type);
2265 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2266 "<" in any valid C++ program. If the next token is indeed "<",
2267 issue a message warning the user about what appears to be an
2268 invalid attempt to form a template-id. LOCATION is the location
2269 of the type-specifier (TYPE) */
2271 static void
2272 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2273 tree type, location_t location)
2275 cp_token_position start = 0;
2277 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2279 if (TYPE_P (type))
2280 error_at (location, "%qT is not a template", type);
2281 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2282 error_at (location, "%qE is not a template", type);
2283 else
2284 error_at (location, "invalid template-id");
2285 /* Remember the location of the invalid "<". */
2286 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2287 start = cp_lexer_token_position (parser->lexer, true);
2288 /* Consume the "<". */
2289 cp_lexer_consume_token (parser->lexer);
2290 /* Parse the template arguments. */
2291 cp_parser_enclosed_template_argument_list (parser);
2292 /* Permanently remove the invalid template arguments so that
2293 this error message is not issued again. */
2294 if (start)
2295 cp_lexer_purge_tokens_after (parser->lexer, start);
2299 /* If parsing an integral constant-expression, issue an error message
2300 about the fact that THING appeared and return true. Otherwise,
2301 return false. In either case, set
2302 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2304 static bool
2305 cp_parser_non_integral_constant_expression (cp_parser *parser,
2306 const char *thing)
2308 parser->non_integral_constant_expression_p = true;
2309 if (parser->integral_constant_expression_p)
2311 if (!parser->allow_non_integral_constant_expression_p)
2313 /* Don't use `%s' to print THING, because quotations (`%<', `%>')
2314 in the message need to be interpreted. */
2315 char *message = concat (thing,
2316 " cannot appear in a constant-expression",
2317 NULL);
2318 error (message);
2319 free (message);
2320 return true;
2323 return false;
2326 /* Emit a diagnostic for an invalid type name. SCOPE is the
2327 qualifying scope (or NULL, if none) for ID. This function commits
2328 to the current active tentative parse, if any. (Otherwise, the
2329 problematic construct might be encountered again later, resulting
2330 in duplicate error messages.) LOCATION is the location of ID. */
2332 static void
2333 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2334 tree scope, tree id,
2335 location_t location)
2337 tree decl, old_scope;
2338 /* Try to lookup the identifier. */
2339 old_scope = parser->scope;
2340 parser->scope = scope;
2341 decl = cp_parser_lookup_name_simple (parser, id, location);
2342 parser->scope = old_scope;
2343 /* If the lookup found a template-name, it means that the user forgot
2344 to specify an argument list. Emit a useful error message. */
2345 if (TREE_CODE (decl) == TEMPLATE_DECL)
2346 error_at (location,
2347 "invalid use of template-name %qE without an argument list",
2348 decl);
2349 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2350 error_at (location, "invalid use of destructor %qD as a type", id);
2351 else if (TREE_CODE (decl) == TYPE_DECL)
2352 /* Something like 'unsigned A a;' */
2353 error_at (location, "invalid combination of multiple type-specifiers");
2354 else if (!parser->scope)
2356 /* Issue an error message. */
2357 error_at (location, "%qE does not name a type", id);
2358 /* If we're in a template class, it's possible that the user was
2359 referring to a type from a base class. For example:
2361 template <typename T> struct A { typedef T X; };
2362 template <typename T> struct B : public A<T> { X x; };
2364 The user should have said "typename A<T>::X". */
2365 if (processing_template_decl && current_class_type
2366 && TYPE_BINFO (current_class_type))
2368 tree b;
2370 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2372 b = TREE_CHAIN (b))
2374 tree base_type = BINFO_TYPE (b);
2375 if (CLASS_TYPE_P (base_type)
2376 && dependent_type_p (base_type))
2378 tree field;
2379 /* Go from a particular instantiation of the
2380 template (which will have an empty TYPE_FIELDs),
2381 to the main version. */
2382 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2383 for (field = TYPE_FIELDS (base_type);
2384 field;
2385 field = TREE_CHAIN (field))
2386 if (TREE_CODE (field) == TYPE_DECL
2387 && DECL_NAME (field) == id)
2389 inform (location,
2390 "(perhaps %<typename %T::%E%> was intended)",
2391 BINFO_TYPE (b), id);
2392 break;
2394 if (field)
2395 break;
2400 /* Here we diagnose qualified-ids where the scope is actually correct,
2401 but the identifier does not resolve to a valid type name. */
2402 else if (parser->scope != error_mark_node)
2404 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2405 error_at (location, "%qE in namespace %qE does not name a type",
2406 id, parser->scope);
2407 else if (CLASS_TYPE_P (parser->scope)
2408 && constructor_name_p (id, parser->scope))
2410 /* A<T>::A<T>() */
2411 error_at (location, "%<%T::%E%> names the constructor, not"
2412 " the type", parser->scope, id);
2413 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2414 error_at (location, "and %qT has no template constructors",
2415 parser->scope);
2417 else if (TYPE_P (parser->scope)
2418 && dependent_scope_p (parser->scope))
2419 error_at (location, "need %<typename%> before %<%T::%E%> because "
2420 "%qT is a dependent scope",
2421 parser->scope, id, parser->scope);
2422 else if (TYPE_P (parser->scope))
2423 error_at (location, "%qE in class %qT does not name a type",
2424 id, parser->scope);
2425 else
2426 gcc_unreachable ();
2428 cp_parser_commit_to_tentative_parse (parser);
2431 /* Check for a common situation where a type-name should be present,
2432 but is not, and issue a sensible error message. Returns true if an
2433 invalid type-name was detected.
2435 The situation handled by this function are variable declarations of the
2436 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2437 Usually, `ID' should name a type, but if we got here it means that it
2438 does not. We try to emit the best possible error message depending on
2439 how exactly the id-expression looks like. */
2441 static bool
2442 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2444 tree id;
2445 cp_token *token = cp_lexer_peek_token (parser->lexer);
2447 /* Avoid duplicate error about ambiguous lookup. */
2448 if (token->type == CPP_NESTED_NAME_SPECIFIER)
2450 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2451 if (next->type == CPP_NAME && next->ambiguous_p)
2452 goto out;
2455 cp_parser_parse_tentatively (parser);
2456 id = cp_parser_id_expression (parser,
2457 /*template_keyword_p=*/false,
2458 /*check_dependency_p=*/true,
2459 /*template_p=*/NULL,
2460 /*declarator_p=*/true,
2461 /*optional_p=*/false);
2462 /* If the next token is a (, this is a function with no explicit return
2463 type, i.e. constructor, destructor or conversion op. */
2464 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2465 || TREE_CODE (id) == TYPE_DECL)
2467 cp_parser_abort_tentative_parse (parser);
2468 return false;
2470 if (!cp_parser_parse_definitely (parser))
2471 return false;
2473 /* Emit a diagnostic for the invalid type. */
2474 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2475 id, token->location);
2476 out:
2477 /* If we aren't in the middle of a declarator (i.e. in a
2478 parameter-declaration-clause), skip to the end of the declaration;
2479 there's no point in trying to process it. */
2480 if (!parser->in_declarator_p)
2481 cp_parser_skip_to_end_of_block_or_statement (parser);
2482 return true;
2485 /* Consume tokens up to, and including, the next non-nested closing `)'.
2486 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2487 are doing error recovery. Returns -1 if OR_COMMA is true and we
2488 found an unnested comma. */
2490 static int
2491 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2492 bool recovering,
2493 bool or_comma,
2494 bool consume_paren)
2496 unsigned paren_depth = 0;
2497 unsigned brace_depth = 0;
2498 unsigned square_depth = 0;
2500 if (recovering && !or_comma
2501 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2502 return 0;
2504 while (true)
2506 cp_token * token = cp_lexer_peek_token (parser->lexer);
2508 switch (token->type)
2510 case CPP_EOF:
2511 case CPP_PRAGMA_EOL:
2512 /* If we've run out of tokens, then there is no closing `)'. */
2513 return 0;
2515 /* This is good for lambda expression capture-lists. */
2516 case CPP_OPEN_SQUARE:
2517 ++square_depth;
2518 break;
2519 case CPP_CLOSE_SQUARE:
2520 if (!square_depth--)
2521 return 0;
2522 break;
2524 case CPP_SEMICOLON:
2525 /* This matches the processing in skip_to_end_of_statement. */
2526 if (!brace_depth)
2527 return 0;
2528 break;
2530 case CPP_OPEN_BRACE:
2531 ++brace_depth;
2532 break;
2533 case CPP_CLOSE_BRACE:
2534 if (!brace_depth--)
2535 return 0;
2536 break;
2538 case CPP_COMMA:
2539 if (recovering && or_comma && !brace_depth && !paren_depth
2540 && !square_depth)
2541 return -1;
2542 break;
2544 case CPP_OPEN_PAREN:
2545 if (!brace_depth)
2546 ++paren_depth;
2547 break;
2549 case CPP_CLOSE_PAREN:
2550 if (!brace_depth && !paren_depth--)
2552 if (consume_paren)
2553 cp_lexer_consume_token (parser->lexer);
2554 return 1;
2556 break;
2558 default:
2559 break;
2562 /* Consume the token. */
2563 cp_lexer_consume_token (parser->lexer);
2567 /* Consume tokens until we reach the end of the current statement.
2568 Normally, that will be just before consuming a `;'. However, if a
2569 non-nested `}' comes first, then we stop before consuming that. */
2571 static void
2572 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2574 unsigned nesting_depth = 0;
2576 while (true)
2578 cp_token *token = cp_lexer_peek_token (parser->lexer);
2580 switch (token->type)
2582 case CPP_EOF:
2583 case CPP_PRAGMA_EOL:
2584 /* If we've run out of tokens, stop. */
2585 return;
2587 case CPP_SEMICOLON:
2588 /* If the next token is a `;', we have reached the end of the
2589 statement. */
2590 if (!nesting_depth)
2591 return;
2592 break;
2594 case CPP_CLOSE_BRACE:
2595 /* If this is a non-nested '}', stop before consuming it.
2596 That way, when confronted with something like:
2598 { 3 + }
2600 we stop before consuming the closing '}', even though we
2601 have not yet reached a `;'. */
2602 if (nesting_depth == 0)
2603 return;
2605 /* If it is the closing '}' for a block that we have
2606 scanned, stop -- but only after consuming the token.
2607 That way given:
2609 void f g () { ... }
2610 typedef int I;
2612 we will stop after the body of the erroneously declared
2613 function, but before consuming the following `typedef'
2614 declaration. */
2615 if (--nesting_depth == 0)
2617 cp_lexer_consume_token (parser->lexer);
2618 return;
2621 case CPP_OPEN_BRACE:
2622 ++nesting_depth;
2623 break;
2625 default:
2626 break;
2629 /* Consume the token. */
2630 cp_lexer_consume_token (parser->lexer);
2634 /* This function is called at the end of a statement or declaration.
2635 If the next token is a semicolon, it is consumed; otherwise, error
2636 recovery is attempted. */
2638 static void
2639 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2641 /* Look for the trailing `;'. */
2642 if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2644 /* If there is additional (erroneous) input, skip to the end of
2645 the statement. */
2646 cp_parser_skip_to_end_of_statement (parser);
2647 /* If the next token is now a `;', consume it. */
2648 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2649 cp_lexer_consume_token (parser->lexer);
2653 /* Skip tokens until we have consumed an entire block, or until we
2654 have consumed a non-nested `;'. */
2656 static void
2657 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2659 int nesting_depth = 0;
2661 while (nesting_depth >= 0)
2663 cp_token *token = cp_lexer_peek_token (parser->lexer);
2665 switch (token->type)
2667 case CPP_EOF:
2668 case CPP_PRAGMA_EOL:
2669 /* If we've run out of tokens, stop. */
2670 return;
2672 case CPP_SEMICOLON:
2673 /* Stop if this is an unnested ';'. */
2674 if (!nesting_depth)
2675 nesting_depth = -1;
2676 break;
2678 case CPP_CLOSE_BRACE:
2679 /* Stop if this is an unnested '}', or closes the outermost
2680 nesting level. */
2681 nesting_depth--;
2682 if (nesting_depth < 0)
2683 return;
2684 if (!nesting_depth)
2685 nesting_depth = -1;
2686 break;
2688 case CPP_OPEN_BRACE:
2689 /* Nest. */
2690 nesting_depth++;
2691 break;
2693 default:
2694 break;
2697 /* Consume the token. */
2698 cp_lexer_consume_token (parser->lexer);
2702 /* Skip tokens until a non-nested closing curly brace is the next
2703 token, or there are no more tokens. Return true in the first case,
2704 false otherwise. */
2706 static bool
2707 cp_parser_skip_to_closing_brace (cp_parser *parser)
2709 unsigned nesting_depth = 0;
2711 while (true)
2713 cp_token *token = cp_lexer_peek_token (parser->lexer);
2715 switch (token->type)
2717 case CPP_EOF:
2718 case CPP_PRAGMA_EOL:
2719 /* If we've run out of tokens, stop. */
2720 return false;
2722 case CPP_CLOSE_BRACE:
2723 /* If the next token is a non-nested `}', then we have reached
2724 the end of the current block. */
2725 if (nesting_depth-- == 0)
2726 return true;
2727 break;
2729 case CPP_OPEN_BRACE:
2730 /* If it the next token is a `{', then we are entering a new
2731 block. Consume the entire block. */
2732 ++nesting_depth;
2733 break;
2735 default:
2736 break;
2739 /* Consume the token. */
2740 cp_lexer_consume_token (parser->lexer);
2744 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2745 parameter is the PRAGMA token, allowing us to purge the entire pragma
2746 sequence. */
2748 static void
2749 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2751 cp_token *token;
2753 parser->lexer->in_pragma = false;
2756 token = cp_lexer_consume_token (parser->lexer);
2757 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2759 /* Ensure that the pragma is not parsed again. */
2760 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2763 /* Require pragma end of line, resyncing with it as necessary. The
2764 arguments are as for cp_parser_skip_to_pragma_eol. */
2766 static void
2767 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2769 parser->lexer->in_pragma = false;
2770 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2771 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2774 /* This is a simple wrapper around make_typename_type. When the id is
2775 an unresolved identifier node, we can provide a superior diagnostic
2776 using cp_parser_diagnose_invalid_type_name. */
2778 static tree
2779 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2780 tree id, location_t id_location)
2782 tree result;
2783 if (TREE_CODE (id) == IDENTIFIER_NODE)
2785 result = make_typename_type (scope, id, typename_type,
2786 /*complain=*/tf_none);
2787 if (result == error_mark_node)
2788 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2789 return result;
2791 return make_typename_type (scope, id, typename_type, tf_error);
2794 /* This is a wrapper around the
2795 make_{pointer,ptrmem,reference}_declarator functions that decides
2796 which one to call based on the CODE and CLASS_TYPE arguments. The
2797 CODE argument should be one of the values returned by
2798 cp_parser_ptr_operator. */
2799 static cp_declarator *
2800 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2801 cp_cv_quals cv_qualifiers,
2802 cp_declarator *target)
2804 if (code == ERROR_MARK)
2805 return cp_error_declarator;
2807 if (code == INDIRECT_REF)
2808 if (class_type == NULL_TREE)
2809 return make_pointer_declarator (cv_qualifiers, target);
2810 else
2811 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2812 else if (code == ADDR_EXPR && class_type == NULL_TREE)
2813 return make_reference_declarator (cv_qualifiers, target, false);
2814 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2815 return make_reference_declarator (cv_qualifiers, target, true);
2816 gcc_unreachable ();
2819 /* Create a new C++ parser. */
2821 static cp_parser *
2822 cp_parser_new (void)
2824 cp_parser *parser;
2825 cp_lexer *lexer;
2826 unsigned i;
2828 /* cp_lexer_new_main is called before calling ggc_alloc because
2829 cp_lexer_new_main might load a PCH file. */
2830 lexer = cp_lexer_new_main ();
2832 /* Initialize the binops_by_token so that we can get the tree
2833 directly from the token. */
2834 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2835 binops_by_token[binops[i].token_type] = binops[i];
2837 parser = GGC_CNEW (cp_parser);
2838 parser->lexer = lexer;
2839 parser->context = cp_parser_context_new (NULL);
2841 /* For now, we always accept GNU extensions. */
2842 parser->allow_gnu_extensions_p = 1;
2844 /* The `>' token is a greater-than operator, not the end of a
2845 template-id. */
2846 parser->greater_than_is_operator_p = true;
2848 parser->default_arg_ok_p = true;
2850 /* We are not parsing a constant-expression. */
2851 parser->integral_constant_expression_p = false;
2852 parser->allow_non_integral_constant_expression_p = false;
2853 parser->non_integral_constant_expression_p = false;
2855 /* Local variable names are not forbidden. */
2856 parser->local_variables_forbidden_p = false;
2858 /* We are not processing an `extern "C"' declaration. */
2859 parser->in_unbraced_linkage_specification_p = false;
2861 /* We are not processing a declarator. */
2862 parser->in_declarator_p = false;
2864 /* We are not processing a template-argument-list. */
2865 parser->in_template_argument_list_p = false;
2867 /* We are not in an iteration statement. */
2868 parser->in_statement = 0;
2870 /* We are not in a switch statement. */
2871 parser->in_switch_statement_p = false;
2873 /* We are not parsing a type-id inside an expression. */
2874 parser->in_type_id_in_expr_p = false;
2876 /* Declarations aren't implicitly extern "C". */
2877 parser->implicit_extern_c = false;
2879 /* String literals should be translated to the execution character set. */
2880 parser->translate_strings_p = true;
2882 /* We are not parsing a function body. */
2883 parser->in_function_body = false;
2885 /* The unparsed function queue is empty. */
2886 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2888 /* There are no classes being defined. */
2889 parser->num_classes_being_defined = 0;
2891 /* No template parameters apply. */
2892 parser->num_template_parameter_lists = 0;
2894 return parser;
2897 /* Create a cp_lexer structure which will emit the tokens in CACHE
2898 and push it onto the parser's lexer stack. This is used for delayed
2899 parsing of in-class method bodies and default arguments, and should
2900 not be confused with tentative parsing. */
2901 static void
2902 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2904 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2905 lexer->next = parser->lexer;
2906 parser->lexer = lexer;
2908 /* Move the current source position to that of the first token in the
2909 new lexer. */
2910 cp_lexer_set_source_position_from_token (lexer->next_token);
2913 /* Pop the top lexer off the parser stack. This is never used for the
2914 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2915 static void
2916 cp_parser_pop_lexer (cp_parser *parser)
2918 cp_lexer *lexer = parser->lexer;
2919 parser->lexer = lexer->next;
2920 cp_lexer_destroy (lexer);
2922 /* Put the current source position back where it was before this
2923 lexer was pushed. */
2924 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2927 /* Lexical conventions [gram.lex] */
2929 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2930 identifier. */
2932 static tree
2933 cp_parser_identifier (cp_parser* parser)
2935 cp_token *token;
2937 /* Look for the identifier. */
2938 token = cp_parser_require (parser, CPP_NAME, "identifier");
2939 /* Return the value. */
2940 return token ? token->u.value : error_mark_node;
2943 /* Parse a sequence of adjacent string constants. Returns a
2944 TREE_STRING representing the combined, nul-terminated string
2945 constant. If TRANSLATE is true, translate the string to the
2946 execution character set. If WIDE_OK is true, a wide string is
2947 invalid here.
2949 C++98 [lex.string] says that if a narrow string literal token is
2950 adjacent to a wide string literal token, the behavior is undefined.
2951 However, C99 6.4.5p4 says that this results in a wide string literal.
2952 We follow C99 here, for consistency with the C front end.
2954 This code is largely lifted from lex_string() in c-lex.c.
2956 FUTURE: ObjC++ will need to handle @-strings here. */
2957 static tree
2958 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2960 tree value;
2961 size_t count;
2962 struct obstack str_ob;
2963 cpp_string str, istr, *strs;
2964 cp_token *tok;
2965 enum cpp_ttype type;
2967 tok = cp_lexer_peek_token (parser->lexer);
2968 if (!cp_parser_is_string_literal (tok))
2970 cp_parser_error (parser, "expected string-literal");
2971 return error_mark_node;
2974 type = tok->type;
2976 /* Try to avoid the overhead of creating and destroying an obstack
2977 for the common case of just one string. */
2978 if (!cp_parser_is_string_literal
2979 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2981 cp_lexer_consume_token (parser->lexer);
2983 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2984 str.len = TREE_STRING_LENGTH (tok->u.value);
2985 count = 1;
2987 strs = &str;
2989 else
2991 gcc_obstack_init (&str_ob);
2992 count = 0;
2996 cp_lexer_consume_token (parser->lexer);
2997 count++;
2998 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2999 str.len = TREE_STRING_LENGTH (tok->u.value);
3001 if (type != tok->type)
3003 if (type == CPP_STRING)
3004 type = tok->type;
3005 else if (tok->type != CPP_STRING)
3006 error_at (tok->location,
3007 "unsupported non-standard concatenation "
3008 "of string literals");
3011 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3013 tok = cp_lexer_peek_token (parser->lexer);
3015 while (cp_parser_is_string_literal (tok));
3017 strs = (cpp_string *) obstack_finish (&str_ob);
3020 if (type != CPP_STRING && !wide_ok)
3022 cp_parser_error (parser, "a wide string is invalid in this context");
3023 type = CPP_STRING;
3026 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3027 (parse_in, strs, count, &istr, type))
3029 value = build_string (istr.len, (const char *)istr.text);
3030 free (CONST_CAST (unsigned char *, istr.text));
3032 switch (type)
3034 default:
3035 case CPP_STRING:
3036 case CPP_UTF8STRING:
3037 TREE_TYPE (value) = char_array_type_node;
3038 break;
3039 case CPP_STRING16:
3040 TREE_TYPE (value) = char16_array_type_node;
3041 break;
3042 case CPP_STRING32:
3043 TREE_TYPE (value) = char32_array_type_node;
3044 break;
3045 case CPP_WSTRING:
3046 TREE_TYPE (value) = wchar_array_type_node;
3047 break;
3050 value = fix_string_type (value);
3052 else
3053 /* cpp_interpret_string has issued an error. */
3054 value = error_mark_node;
3056 if (count > 1)
3057 obstack_free (&str_ob, 0);
3059 return value;
3063 /* Basic concepts [gram.basic] */
3065 /* Parse a translation-unit.
3067 translation-unit:
3068 declaration-seq [opt]
3070 Returns TRUE if all went well. */
3072 static bool
3073 cp_parser_translation_unit (cp_parser* parser)
3075 /* The address of the first non-permanent object on the declarator
3076 obstack. */
3077 static void *declarator_obstack_base;
3079 bool success;
3081 /* Create the declarator obstack, if necessary. */
3082 if (!cp_error_declarator)
3084 gcc_obstack_init (&declarator_obstack);
3085 /* Create the error declarator. */
3086 cp_error_declarator = make_declarator (cdk_error);
3087 /* Create the empty parameter list. */
3088 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3089 /* Remember where the base of the declarator obstack lies. */
3090 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3093 cp_parser_declaration_seq_opt (parser);
3095 /* If there are no tokens left then all went well. */
3096 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3098 /* Get rid of the token array; we don't need it any more. */
3099 cp_lexer_destroy (parser->lexer);
3100 parser->lexer = NULL;
3102 /* This file might have been a context that's implicitly extern
3103 "C". If so, pop the lang context. (Only relevant for PCH.) */
3104 if (parser->implicit_extern_c)
3106 pop_lang_context ();
3107 parser->implicit_extern_c = false;
3110 /* Finish up. */
3111 finish_translation_unit ();
3113 success = true;
3115 else
3117 cp_parser_error (parser, "expected declaration");
3118 success = false;
3121 /* Make sure the declarator obstack was fully cleaned up. */
3122 gcc_assert (obstack_next_free (&declarator_obstack)
3123 == declarator_obstack_base);
3125 /* All went well. */
3126 return success;
3129 /* Expressions [gram.expr] */
3131 /* Parse a primary-expression.
3133 primary-expression:
3134 literal
3135 this
3136 ( expression )
3137 id-expression
3139 GNU Extensions:
3141 primary-expression:
3142 ( compound-statement )
3143 __builtin_va_arg ( assignment-expression , type-id )
3144 __builtin_offsetof ( type-id , offsetof-expression )
3146 C++ Extensions:
3147 __has_nothrow_assign ( type-id )
3148 __has_nothrow_constructor ( type-id )
3149 __has_nothrow_copy ( type-id )
3150 __has_trivial_assign ( type-id )
3151 __has_trivial_constructor ( type-id )
3152 __has_trivial_copy ( type-id )
3153 __has_trivial_destructor ( type-id )
3154 __has_virtual_destructor ( type-id )
3155 __is_abstract ( type-id )
3156 __is_base_of ( type-id , type-id )
3157 __is_class ( type-id )
3158 __is_convertible_to ( type-id , type-id )
3159 __is_empty ( type-id )
3160 __is_enum ( type-id )
3161 __is_pod ( type-id )
3162 __is_polymorphic ( type-id )
3163 __is_union ( type-id )
3165 Objective-C++ Extension:
3167 primary-expression:
3168 objc-expression
3170 literal:
3171 __null
3173 ADDRESS_P is true iff this expression was immediately preceded by
3174 "&" and therefore might denote a pointer-to-member. CAST_P is true
3175 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3176 true iff this expression is a template argument.
3178 Returns a representation of the expression. Upon return, *IDK
3179 indicates what kind of id-expression (if any) was present. */
3181 static tree
3182 cp_parser_primary_expression (cp_parser *parser,
3183 bool address_p,
3184 bool cast_p,
3185 bool template_arg_p,
3186 cp_id_kind *idk)
3188 cp_token *token = NULL;
3190 /* Assume the primary expression is not an id-expression. */
3191 *idk = CP_ID_KIND_NONE;
3193 /* Peek at the next token. */
3194 token = cp_lexer_peek_token (parser->lexer);
3195 switch (token->type)
3197 /* literal:
3198 integer-literal
3199 character-literal
3200 floating-literal
3201 string-literal
3202 boolean-literal */
3203 case CPP_CHAR:
3204 case CPP_CHAR16:
3205 case CPP_CHAR32:
3206 case CPP_WCHAR:
3207 case CPP_NUMBER:
3208 token = cp_lexer_consume_token (parser->lexer);
3209 if (TREE_CODE (token->u.value) == FIXED_CST)
3211 error_at (token->location,
3212 "fixed-point types not supported in C++");
3213 return error_mark_node;
3215 /* Floating-point literals are only allowed in an integral
3216 constant expression if they are cast to an integral or
3217 enumeration type. */
3218 if (TREE_CODE (token->u.value) == REAL_CST
3219 && parser->integral_constant_expression_p
3220 && pedantic)
3222 /* CAST_P will be set even in invalid code like "int(2.7 +
3223 ...)". Therefore, we have to check that the next token
3224 is sure to end the cast. */
3225 if (cast_p)
3227 cp_token *next_token;
3229 next_token = cp_lexer_peek_token (parser->lexer);
3230 if (/* The comma at the end of an
3231 enumerator-definition. */
3232 next_token->type != CPP_COMMA
3233 /* The curly brace at the end of an enum-specifier. */
3234 && next_token->type != CPP_CLOSE_BRACE
3235 /* The end of a statement. */
3236 && next_token->type != CPP_SEMICOLON
3237 /* The end of the cast-expression. */
3238 && next_token->type != CPP_CLOSE_PAREN
3239 /* The end of an array bound. */
3240 && next_token->type != CPP_CLOSE_SQUARE
3241 /* The closing ">" in a template-argument-list. */
3242 && (next_token->type != CPP_GREATER
3243 || parser->greater_than_is_operator_p)
3244 /* C++0x only: A ">>" treated like two ">" tokens,
3245 in a template-argument-list. */
3246 && (next_token->type != CPP_RSHIFT
3247 || (cxx_dialect == cxx98)
3248 || parser->greater_than_is_operator_p))
3249 cast_p = false;
3252 /* If we are within a cast, then the constraint that the
3253 cast is to an integral or enumeration type will be
3254 checked at that point. If we are not within a cast, then
3255 this code is invalid. */
3256 if (!cast_p)
3257 cp_parser_non_integral_constant_expression
3258 (parser, "floating-point literal");
3260 return token->u.value;
3262 case CPP_STRING:
3263 case CPP_STRING16:
3264 case CPP_STRING32:
3265 case CPP_WSTRING:
3266 case CPP_UTF8STRING:
3267 /* ??? Should wide strings be allowed when parser->translate_strings_p
3268 is false (i.e. in attributes)? If not, we can kill the third
3269 argument to cp_parser_string_literal. */
3270 return cp_parser_string_literal (parser,
3271 parser->translate_strings_p,
3272 true);
3274 case CPP_OPEN_PAREN:
3276 tree expr;
3277 bool saved_greater_than_is_operator_p;
3279 /* Consume the `('. */
3280 cp_lexer_consume_token (parser->lexer);
3281 /* Within a parenthesized expression, a `>' token is always
3282 the greater-than operator. */
3283 saved_greater_than_is_operator_p
3284 = parser->greater_than_is_operator_p;
3285 parser->greater_than_is_operator_p = true;
3286 /* If we see `( { ' then we are looking at the beginning of
3287 a GNU statement-expression. */
3288 if (cp_parser_allow_gnu_extensions_p (parser)
3289 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3291 /* Statement-expressions are not allowed by the standard. */
3292 pedwarn (token->location, OPT_pedantic,
3293 "ISO C++ forbids braced-groups within expressions");
3295 /* And they're not allowed outside of a function-body; you
3296 cannot, for example, write:
3298 int i = ({ int j = 3; j + 1; });
3300 at class or namespace scope. */
3301 if (!parser->in_function_body
3302 || parser->in_template_argument_list_p)
3304 error_at (token->location,
3305 "statement-expressions are not allowed outside "
3306 "functions nor in template-argument lists");
3307 cp_parser_skip_to_end_of_block_or_statement (parser);
3308 expr = error_mark_node;
3310 else
3312 /* Start the statement-expression. */
3313 expr = begin_stmt_expr ();
3314 /* Parse the compound-statement. */
3315 cp_parser_compound_statement (parser, expr, false);
3316 /* Finish up. */
3317 expr = finish_stmt_expr (expr, false);
3320 else
3322 /* Parse the parenthesized expression. */
3323 expr = cp_parser_expression (parser, cast_p, idk);
3324 /* Let the front end know that this expression was
3325 enclosed in parentheses. This matters in case, for
3326 example, the expression is of the form `A::B', since
3327 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3328 not. */
3329 finish_parenthesized_expr (expr);
3331 /* The `>' token might be the end of a template-id or
3332 template-parameter-list now. */
3333 parser->greater_than_is_operator_p
3334 = saved_greater_than_is_operator_p;
3335 /* Consume the `)'. */
3336 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3337 cp_parser_skip_to_end_of_statement (parser);
3339 return expr;
3342 case CPP_OPEN_SQUARE:
3343 if (c_dialect_objc ())
3344 /* We have an Objective-C++ message. */
3345 return cp_parser_objc_expression (parser);
3346 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
3347 return cp_parser_lambda_expression (parser);
3349 case CPP_OBJC_STRING:
3350 if (c_dialect_objc ())
3351 /* We have an Objective-C++ string literal. */
3352 return cp_parser_objc_expression (parser);
3353 cp_parser_error (parser, "expected primary-expression");
3354 return error_mark_node;
3356 case CPP_KEYWORD:
3357 switch (token->keyword)
3359 /* These two are the boolean literals. */
3360 case RID_TRUE:
3361 cp_lexer_consume_token (parser->lexer);
3362 return boolean_true_node;
3363 case RID_FALSE:
3364 cp_lexer_consume_token (parser->lexer);
3365 return boolean_false_node;
3367 /* The `__null' literal. */
3368 case RID_NULL:
3369 cp_lexer_consume_token (parser->lexer);
3370 return null_node;
3372 /* Recognize the `this' keyword. */
3373 case RID_THIS:
3374 cp_lexer_consume_token (parser->lexer);
3375 if (parser->local_variables_forbidden_p)
3377 error_at (token->location,
3378 "%<this%> may not be used in this context");
3379 return error_mark_node;
3381 /* Pointers cannot appear in constant-expressions. */
3382 if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3383 return error_mark_node;
3384 return finish_this_expr ();
3386 /* The `operator' keyword can be the beginning of an
3387 id-expression. */
3388 case RID_OPERATOR:
3389 goto id_expression;
3391 case RID_FUNCTION_NAME:
3392 case RID_PRETTY_FUNCTION_NAME:
3393 case RID_C99_FUNCTION_NAME:
3395 const char *name;
3397 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3398 __func__ are the names of variables -- but they are
3399 treated specially. Therefore, they are handled here,
3400 rather than relying on the generic id-expression logic
3401 below. Grammatically, these names are id-expressions.
3403 Consume the token. */
3404 token = cp_lexer_consume_token (parser->lexer);
3406 switch (token->keyword)
3408 case RID_FUNCTION_NAME:
3409 name = "%<__FUNCTION__%>";
3410 break;
3411 case RID_PRETTY_FUNCTION_NAME:
3412 name = "%<__PRETTY_FUNCTION__%>";
3413 break;
3414 case RID_C99_FUNCTION_NAME:
3415 name = "%<__func__%>";
3416 break;
3417 default:
3418 gcc_unreachable ();
3421 if (cp_parser_non_integral_constant_expression (parser, name))
3422 return error_mark_node;
3424 /* Look up the name. */
3425 return finish_fname (token->u.value);
3428 case RID_VA_ARG:
3430 tree expression;
3431 tree type;
3433 /* The `__builtin_va_arg' construct is used to handle
3434 `va_arg'. Consume the `__builtin_va_arg' token. */
3435 cp_lexer_consume_token (parser->lexer);
3436 /* Look for the opening `('. */
3437 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3438 /* Now, parse the assignment-expression. */
3439 expression = cp_parser_assignment_expression (parser,
3440 /*cast_p=*/false, NULL);
3441 /* Look for the `,'. */
3442 cp_parser_require (parser, CPP_COMMA, "%<,%>");
3443 /* Parse the type-id. */
3444 type = cp_parser_type_id (parser);
3445 /* Look for the closing `)'. */
3446 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3447 /* Using `va_arg' in a constant-expression is not
3448 allowed. */
3449 if (cp_parser_non_integral_constant_expression (parser,
3450 "%<va_arg%>"))
3451 return error_mark_node;
3452 return build_x_va_arg (expression, type);
3455 case RID_OFFSETOF:
3456 return cp_parser_builtin_offsetof (parser);
3458 case RID_HAS_NOTHROW_ASSIGN:
3459 case RID_HAS_NOTHROW_CONSTRUCTOR:
3460 case RID_HAS_NOTHROW_COPY:
3461 case RID_HAS_TRIVIAL_ASSIGN:
3462 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3463 case RID_HAS_TRIVIAL_COPY:
3464 case RID_HAS_TRIVIAL_DESTRUCTOR:
3465 case RID_HAS_VIRTUAL_DESTRUCTOR:
3466 case RID_IS_ABSTRACT:
3467 case RID_IS_BASE_OF:
3468 case RID_IS_CLASS:
3469 case RID_IS_CONVERTIBLE_TO:
3470 case RID_IS_EMPTY:
3471 case RID_IS_ENUM:
3472 case RID_IS_POD:
3473 case RID_IS_POLYMORPHIC:
3474 case RID_IS_STD_LAYOUT:
3475 case RID_IS_TRIVIAL:
3476 case RID_IS_UNION:
3477 return cp_parser_trait_expr (parser, token->keyword);
3479 /* Objective-C++ expressions. */
3480 case RID_AT_ENCODE:
3481 case RID_AT_PROTOCOL:
3482 case RID_AT_SELECTOR:
3483 return cp_parser_objc_expression (parser);
3485 default:
3486 cp_parser_error (parser, "expected primary-expression");
3487 return error_mark_node;
3490 /* An id-expression can start with either an identifier, a
3491 `::' as the beginning of a qualified-id, or the "operator"
3492 keyword. */
3493 case CPP_NAME:
3494 case CPP_SCOPE:
3495 case CPP_TEMPLATE_ID:
3496 case CPP_NESTED_NAME_SPECIFIER:
3498 tree id_expression;
3499 tree decl;
3500 const char *error_msg;
3501 bool template_p;
3502 bool done;
3503 cp_token *id_expr_token;
3505 id_expression:
3506 /* Parse the id-expression. */
3507 id_expression
3508 = cp_parser_id_expression (parser,
3509 /*template_keyword_p=*/false,
3510 /*check_dependency_p=*/true,
3511 &template_p,
3512 /*declarator_p=*/false,
3513 /*optional_p=*/false);
3514 if (id_expression == error_mark_node)
3515 return error_mark_node;
3516 id_expr_token = token;
3517 token = cp_lexer_peek_token (parser->lexer);
3518 done = (token->type != CPP_OPEN_SQUARE
3519 && token->type != CPP_OPEN_PAREN
3520 && token->type != CPP_DOT
3521 && token->type != CPP_DEREF
3522 && token->type != CPP_PLUS_PLUS
3523 && token->type != CPP_MINUS_MINUS);
3524 /* If we have a template-id, then no further lookup is
3525 required. If the template-id was for a template-class, we
3526 will sometimes have a TYPE_DECL at this point. */
3527 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3528 || TREE_CODE (id_expression) == TYPE_DECL)
3529 decl = id_expression;
3530 /* Look up the name. */
3531 else
3533 tree ambiguous_decls;
3535 /* If we already know that this lookup is ambiguous, then
3536 we've already issued an error message; there's no reason
3537 to check again. */
3538 if (id_expr_token->type == CPP_NAME
3539 && id_expr_token->ambiguous_p)
3541 cp_parser_simulate_error (parser);
3542 return error_mark_node;
3545 decl = cp_parser_lookup_name (parser, id_expression,
3546 none_type,
3547 template_p,
3548 /*is_namespace=*/false,
3549 /*check_dependency=*/true,
3550 &ambiguous_decls,
3551 id_expr_token->location);
3552 /* If the lookup was ambiguous, an error will already have
3553 been issued. */
3554 if (ambiguous_decls)
3555 return error_mark_node;
3557 /* In Objective-C++, an instance variable (ivar) may be preferred
3558 to whatever cp_parser_lookup_name() found. */
3559 decl = objc_lookup_ivar (decl, id_expression);
3561 /* If name lookup gives us a SCOPE_REF, then the
3562 qualifying scope was dependent. */
3563 if (TREE_CODE (decl) == SCOPE_REF)
3565 /* At this point, we do not know if DECL is a valid
3566 integral constant expression. We assume that it is
3567 in fact such an expression, so that code like:
3569 template <int N> struct A {
3570 int a[B<N>::i];
3573 is accepted. At template-instantiation time, we
3574 will check that B<N>::i is actually a constant. */
3575 return decl;
3577 /* Check to see if DECL is a local variable in a context
3578 where that is forbidden. */
3579 if (parser->local_variables_forbidden_p
3580 && local_variable_p (decl))
3582 /* It might be that we only found DECL because we are
3583 trying to be generous with pre-ISO scoping rules.
3584 For example, consider:
3586 int i;
3587 void g() {
3588 for (int i = 0; i < 10; ++i) {}
3589 extern void f(int j = i);
3592 Here, name look up will originally find the out
3593 of scope `i'. We need to issue a warning message,
3594 but then use the global `i'. */
3595 decl = check_for_out_of_scope_variable (decl);
3596 if (local_variable_p (decl))
3598 error_at (id_expr_token->location,
3599 "local variable %qD may not appear in this context",
3600 decl);
3601 return error_mark_node;
3606 decl = (finish_id_expression
3607 (id_expression, decl, parser->scope,
3608 idk,
3609 parser->integral_constant_expression_p,
3610 parser->allow_non_integral_constant_expression_p,
3611 &parser->non_integral_constant_expression_p,
3612 template_p, done, address_p,
3613 template_arg_p,
3614 &error_msg,
3615 id_expr_token->location));
3616 if (error_msg)
3617 cp_parser_error (parser, error_msg);
3618 return decl;
3621 /* Anything else is an error. */
3622 default:
3623 cp_parser_error (parser, "expected primary-expression");
3624 return error_mark_node;
3628 /* Parse an id-expression.
3630 id-expression:
3631 unqualified-id
3632 qualified-id
3634 qualified-id:
3635 :: [opt] nested-name-specifier template [opt] unqualified-id
3636 :: identifier
3637 :: operator-function-id
3638 :: template-id
3640 Return a representation of the unqualified portion of the
3641 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3642 a `::' or nested-name-specifier.
3644 Often, if the id-expression was a qualified-id, the caller will
3645 want to make a SCOPE_REF to represent the qualified-id. This
3646 function does not do this in order to avoid wastefully creating
3647 SCOPE_REFs when they are not required.
3649 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3650 `template' keyword.
3652 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3653 uninstantiated templates.
3655 If *TEMPLATE_P is non-NULL, it is set to true iff the
3656 `template' keyword is used to explicitly indicate that the entity
3657 named is a template.
3659 If DECLARATOR_P is true, the id-expression is appearing as part of
3660 a declarator, rather than as part of an expression. */
3662 static tree
3663 cp_parser_id_expression (cp_parser *parser,
3664 bool template_keyword_p,
3665 bool check_dependency_p,
3666 bool *template_p,
3667 bool declarator_p,
3668 bool optional_p)
3670 bool global_scope_p;
3671 bool nested_name_specifier_p;
3673 /* Assume the `template' keyword was not used. */
3674 if (template_p)
3675 *template_p = template_keyword_p;
3677 /* Look for the optional `::' operator. */
3678 global_scope_p
3679 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3680 != NULL_TREE);
3681 /* Look for the optional nested-name-specifier. */
3682 nested_name_specifier_p
3683 = (cp_parser_nested_name_specifier_opt (parser,
3684 /*typename_keyword_p=*/false,
3685 check_dependency_p,
3686 /*type_p=*/false,
3687 declarator_p)
3688 != NULL_TREE);
3689 /* If there is a nested-name-specifier, then we are looking at
3690 the first qualified-id production. */
3691 if (nested_name_specifier_p)
3693 tree saved_scope;
3694 tree saved_object_scope;
3695 tree saved_qualifying_scope;
3696 tree unqualified_id;
3697 bool is_template;
3699 /* See if the next token is the `template' keyword. */
3700 if (!template_p)
3701 template_p = &is_template;
3702 *template_p = cp_parser_optional_template_keyword (parser);
3703 /* Name lookup we do during the processing of the
3704 unqualified-id might obliterate SCOPE. */
3705 saved_scope = parser->scope;
3706 saved_object_scope = parser->object_scope;
3707 saved_qualifying_scope = parser->qualifying_scope;
3708 /* Process the final unqualified-id. */
3709 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3710 check_dependency_p,
3711 declarator_p,
3712 /*optional_p=*/false);
3713 /* Restore the SAVED_SCOPE for our caller. */
3714 parser->scope = saved_scope;
3715 parser->object_scope = saved_object_scope;
3716 parser->qualifying_scope = saved_qualifying_scope;
3718 return unqualified_id;
3720 /* Otherwise, if we are in global scope, then we are looking at one
3721 of the other qualified-id productions. */
3722 else if (global_scope_p)
3724 cp_token *token;
3725 tree id;
3727 /* Peek at the next token. */
3728 token = cp_lexer_peek_token (parser->lexer);
3730 /* If it's an identifier, and the next token is not a "<", then
3731 we can avoid the template-id case. This is an optimization
3732 for this common case. */
3733 if (token->type == CPP_NAME
3734 && !cp_parser_nth_token_starts_template_argument_list_p
3735 (parser, 2))
3736 return cp_parser_identifier (parser);
3738 cp_parser_parse_tentatively (parser);
3739 /* Try a template-id. */
3740 id = cp_parser_template_id (parser,
3741 /*template_keyword_p=*/false,
3742 /*check_dependency_p=*/true,
3743 declarator_p);
3744 /* If that worked, we're done. */
3745 if (cp_parser_parse_definitely (parser))
3746 return id;
3748 /* Peek at the next token. (Changes in the token buffer may
3749 have invalidated the pointer obtained above.) */
3750 token = cp_lexer_peek_token (parser->lexer);
3752 switch (token->type)
3754 case CPP_NAME:
3755 return cp_parser_identifier (parser);
3757 case CPP_KEYWORD:
3758 if (token->keyword == RID_OPERATOR)
3759 return cp_parser_operator_function_id (parser);
3760 /* Fall through. */
3762 default:
3763 cp_parser_error (parser, "expected id-expression");
3764 return error_mark_node;
3767 else
3768 return cp_parser_unqualified_id (parser, template_keyword_p,
3769 /*check_dependency_p=*/true,
3770 declarator_p,
3771 optional_p);
3774 /* Parse an unqualified-id.
3776 unqualified-id:
3777 identifier
3778 operator-function-id
3779 conversion-function-id
3780 ~ class-name
3781 template-id
3783 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3784 keyword, in a construct like `A::template ...'.
3786 Returns a representation of unqualified-id. For the `identifier'
3787 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3788 production a BIT_NOT_EXPR is returned; the operand of the
3789 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3790 other productions, see the documentation accompanying the
3791 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3792 names are looked up in uninstantiated templates. If DECLARATOR_P
3793 is true, the unqualified-id is appearing as part of a declarator,
3794 rather than as part of an expression. */
3796 static tree
3797 cp_parser_unqualified_id (cp_parser* parser,
3798 bool template_keyword_p,
3799 bool check_dependency_p,
3800 bool declarator_p,
3801 bool optional_p)
3803 cp_token *token;
3805 /* Peek at the next token. */
3806 token = cp_lexer_peek_token (parser->lexer);
3808 switch (token->type)
3810 case CPP_NAME:
3812 tree id;
3814 /* We don't know yet whether or not this will be a
3815 template-id. */
3816 cp_parser_parse_tentatively (parser);
3817 /* Try a template-id. */
3818 id = cp_parser_template_id (parser, template_keyword_p,
3819 check_dependency_p,
3820 declarator_p);
3821 /* If it worked, we're done. */
3822 if (cp_parser_parse_definitely (parser))
3823 return id;
3824 /* Otherwise, it's an ordinary identifier. */
3825 return cp_parser_identifier (parser);
3828 case CPP_TEMPLATE_ID:
3829 return cp_parser_template_id (parser, template_keyword_p,
3830 check_dependency_p,
3831 declarator_p);
3833 case CPP_COMPL:
3835 tree type_decl;
3836 tree qualifying_scope;
3837 tree object_scope;
3838 tree scope;
3839 bool done;
3841 /* Consume the `~' token. */
3842 cp_lexer_consume_token (parser->lexer);
3843 /* Parse the class-name. The standard, as written, seems to
3844 say that:
3846 template <typename T> struct S { ~S (); };
3847 template <typename T> S<T>::~S() {}
3849 is invalid, since `~' must be followed by a class-name, but
3850 `S<T>' is dependent, and so not known to be a class.
3851 That's not right; we need to look in uninstantiated
3852 templates. A further complication arises from:
3854 template <typename T> void f(T t) {
3855 t.T::~T();
3858 Here, it is not possible to look up `T' in the scope of `T'
3859 itself. We must look in both the current scope, and the
3860 scope of the containing complete expression.
3862 Yet another issue is:
3864 struct S {
3865 int S;
3866 ~S();
3869 S::~S() {}
3871 The standard does not seem to say that the `S' in `~S'
3872 should refer to the type `S' and not the data member
3873 `S::S'. */
3875 /* DR 244 says that we look up the name after the "~" in the
3876 same scope as we looked up the qualifying name. That idea
3877 isn't fully worked out; it's more complicated than that. */
3878 scope = parser->scope;
3879 object_scope = parser->object_scope;
3880 qualifying_scope = parser->qualifying_scope;
3882 /* Check for invalid scopes. */
3883 if (scope == error_mark_node)
3885 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3886 cp_lexer_consume_token (parser->lexer);
3887 return error_mark_node;
3889 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3891 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3892 error_at (token->location,
3893 "scope %qT before %<~%> is not a class-name",
3894 scope);
3895 cp_parser_simulate_error (parser);
3896 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3897 cp_lexer_consume_token (parser->lexer);
3898 return error_mark_node;
3900 gcc_assert (!scope || TYPE_P (scope));
3902 /* If the name is of the form "X::~X" it's OK even if X is a
3903 typedef. */
3904 token = cp_lexer_peek_token (parser->lexer);
3905 if (scope
3906 && token->type == CPP_NAME
3907 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3908 != CPP_LESS)
3909 && (token->u.value == TYPE_IDENTIFIER (scope)
3910 || constructor_name_p (token->u.value, scope)))
3912 cp_lexer_consume_token (parser->lexer);
3913 return build_nt (BIT_NOT_EXPR, scope);
3916 /* If there was an explicit qualification (S::~T), first look
3917 in the scope given by the qualification (i.e., S).
3919 Note: in the calls to cp_parser_class_name below we pass
3920 typename_type so that lookup finds the injected-class-name
3921 rather than the constructor. */
3922 done = false;
3923 type_decl = NULL_TREE;
3924 if (scope)
3926 cp_parser_parse_tentatively (parser);
3927 type_decl = cp_parser_class_name (parser,
3928 /*typename_keyword_p=*/false,
3929 /*template_keyword_p=*/false,
3930 typename_type,
3931 /*check_dependency=*/false,
3932 /*class_head_p=*/false,
3933 declarator_p);
3934 if (cp_parser_parse_definitely (parser))
3935 done = true;
3937 /* In "N::S::~S", look in "N" as well. */
3938 if (!done && scope && qualifying_scope)
3940 cp_parser_parse_tentatively (parser);
3941 parser->scope = qualifying_scope;
3942 parser->object_scope = NULL_TREE;
3943 parser->qualifying_scope = NULL_TREE;
3944 type_decl
3945 = cp_parser_class_name (parser,
3946 /*typename_keyword_p=*/false,
3947 /*template_keyword_p=*/false,
3948 typename_type,
3949 /*check_dependency=*/false,
3950 /*class_head_p=*/false,
3951 declarator_p);
3952 if (cp_parser_parse_definitely (parser))
3953 done = true;
3955 /* In "p->S::~T", look in the scope given by "*p" as well. */
3956 else if (!done && object_scope)
3958 cp_parser_parse_tentatively (parser);
3959 parser->scope = object_scope;
3960 parser->object_scope = NULL_TREE;
3961 parser->qualifying_scope = NULL_TREE;
3962 type_decl
3963 = cp_parser_class_name (parser,
3964 /*typename_keyword_p=*/false,
3965 /*template_keyword_p=*/false,
3966 typename_type,
3967 /*check_dependency=*/false,
3968 /*class_head_p=*/false,
3969 declarator_p);
3970 if (cp_parser_parse_definitely (parser))
3971 done = true;
3973 /* Look in the surrounding context. */
3974 if (!done)
3976 parser->scope = NULL_TREE;
3977 parser->object_scope = NULL_TREE;
3978 parser->qualifying_scope = NULL_TREE;
3979 if (processing_template_decl)
3980 cp_parser_parse_tentatively (parser);
3981 type_decl
3982 = cp_parser_class_name (parser,
3983 /*typename_keyword_p=*/false,
3984 /*template_keyword_p=*/false,
3985 typename_type,
3986 /*check_dependency=*/false,
3987 /*class_head_p=*/false,
3988 declarator_p);
3989 if (processing_template_decl
3990 && ! cp_parser_parse_definitely (parser))
3992 /* We couldn't find a type with this name, so just accept
3993 it and check for a match at instantiation time. */
3994 type_decl = cp_parser_identifier (parser);
3995 if (type_decl != error_mark_node)
3996 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
3997 return type_decl;
4000 /* If an error occurred, assume that the name of the
4001 destructor is the same as the name of the qualifying
4002 class. That allows us to keep parsing after running
4003 into ill-formed destructor names. */
4004 if (type_decl == error_mark_node && scope)
4005 return build_nt (BIT_NOT_EXPR, scope);
4006 else if (type_decl == error_mark_node)
4007 return error_mark_node;
4009 /* Check that destructor name and scope match. */
4010 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4012 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4013 error_at (token->location,
4014 "declaration of %<~%T%> as member of %qT",
4015 type_decl, scope);
4016 cp_parser_simulate_error (parser);
4017 return error_mark_node;
4020 /* [class.dtor]
4022 A typedef-name that names a class shall not be used as the
4023 identifier in the declarator for a destructor declaration. */
4024 if (declarator_p
4025 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4026 && !DECL_SELF_REFERENCE_P (type_decl)
4027 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4028 error_at (token->location,
4029 "typedef-name %qD used as destructor declarator",
4030 type_decl);
4032 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4035 case CPP_KEYWORD:
4036 if (token->keyword == RID_OPERATOR)
4038 tree id;
4040 /* This could be a template-id, so we try that first. */
4041 cp_parser_parse_tentatively (parser);
4042 /* Try a template-id. */
4043 id = cp_parser_template_id (parser, template_keyword_p,
4044 /*check_dependency_p=*/true,
4045 declarator_p);
4046 /* If that worked, we're done. */
4047 if (cp_parser_parse_definitely (parser))
4048 return id;
4049 /* We still don't know whether we're looking at an
4050 operator-function-id or a conversion-function-id. */
4051 cp_parser_parse_tentatively (parser);
4052 /* Try an operator-function-id. */
4053 id = cp_parser_operator_function_id (parser);
4054 /* If that didn't work, try a conversion-function-id. */
4055 if (!cp_parser_parse_definitely (parser))
4056 id = cp_parser_conversion_function_id (parser);
4058 return id;
4060 /* Fall through. */
4062 default:
4063 if (optional_p)
4064 return NULL_TREE;
4065 cp_parser_error (parser, "expected unqualified-id");
4066 return error_mark_node;
4070 /* Parse an (optional) nested-name-specifier.
4072 nested-name-specifier: [C++98]
4073 class-or-namespace-name :: nested-name-specifier [opt]
4074 class-or-namespace-name :: template nested-name-specifier [opt]
4076 nested-name-specifier: [C++0x]
4077 type-name ::
4078 namespace-name ::
4079 nested-name-specifier identifier ::
4080 nested-name-specifier template [opt] simple-template-id ::
4082 PARSER->SCOPE should be set appropriately before this function is
4083 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4084 effect. TYPE_P is TRUE if we non-type bindings should be ignored
4085 in name lookups.
4087 Sets PARSER->SCOPE to the class (TYPE) or namespace
4088 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4089 it unchanged if there is no nested-name-specifier. Returns the new
4090 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4092 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4093 part of a declaration and/or decl-specifier. */
4095 static tree
4096 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4097 bool typename_keyword_p,
4098 bool check_dependency_p,
4099 bool type_p,
4100 bool is_declaration)
4102 bool success = false;
4103 cp_token_position start = 0;
4104 cp_token *token;
4106 /* Remember where the nested-name-specifier starts. */
4107 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4109 start = cp_lexer_token_position (parser->lexer, false);
4110 push_deferring_access_checks (dk_deferred);
4113 while (true)
4115 tree new_scope;
4116 tree old_scope;
4117 tree saved_qualifying_scope;
4118 bool template_keyword_p;
4120 /* Spot cases that cannot be the beginning of a
4121 nested-name-specifier. */
4122 token = cp_lexer_peek_token (parser->lexer);
4124 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4125 the already parsed nested-name-specifier. */
4126 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4128 /* Grab the nested-name-specifier and continue the loop. */
4129 cp_parser_pre_parsed_nested_name_specifier (parser);
4130 /* If we originally encountered this nested-name-specifier
4131 with IS_DECLARATION set to false, we will not have
4132 resolved TYPENAME_TYPEs, so we must do so here. */
4133 if (is_declaration
4134 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4136 new_scope = resolve_typename_type (parser->scope,
4137 /*only_current_p=*/false);
4138 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4139 parser->scope = new_scope;
4141 success = true;
4142 continue;
4145 /* Spot cases that cannot be the beginning of a
4146 nested-name-specifier. On the second and subsequent times
4147 through the loop, we look for the `template' keyword. */
4148 if (success && token->keyword == RID_TEMPLATE)
4150 /* A template-id can start a nested-name-specifier. */
4151 else if (token->type == CPP_TEMPLATE_ID)
4153 else
4155 /* If the next token is not an identifier, then it is
4156 definitely not a type-name or namespace-name. */
4157 if (token->type != CPP_NAME)
4158 break;
4159 /* If the following token is neither a `<' (to begin a
4160 template-id), nor a `::', then we are not looking at a
4161 nested-name-specifier. */
4162 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4163 if (token->type != CPP_SCOPE
4164 && !cp_parser_nth_token_starts_template_argument_list_p
4165 (parser, 2))
4166 break;
4169 /* The nested-name-specifier is optional, so we parse
4170 tentatively. */
4171 cp_parser_parse_tentatively (parser);
4173 /* Look for the optional `template' keyword, if this isn't the
4174 first time through the loop. */
4175 if (success)
4176 template_keyword_p = cp_parser_optional_template_keyword (parser);
4177 else
4178 template_keyword_p = false;
4180 /* Save the old scope since the name lookup we are about to do
4181 might destroy it. */
4182 old_scope = parser->scope;
4183 saved_qualifying_scope = parser->qualifying_scope;
4184 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4185 look up names in "X<T>::I" in order to determine that "Y" is
4186 a template. So, if we have a typename at this point, we make
4187 an effort to look through it. */
4188 if (is_declaration
4189 && !typename_keyword_p
4190 && parser->scope
4191 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4192 parser->scope = resolve_typename_type (parser->scope,
4193 /*only_current_p=*/false);
4194 /* Parse the qualifying entity. */
4195 new_scope
4196 = cp_parser_qualifying_entity (parser,
4197 typename_keyword_p,
4198 template_keyword_p,
4199 check_dependency_p,
4200 type_p,
4201 is_declaration);
4202 /* Look for the `::' token. */
4203 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4205 /* If we found what we wanted, we keep going; otherwise, we're
4206 done. */
4207 if (!cp_parser_parse_definitely (parser))
4209 bool error_p = false;
4211 /* Restore the OLD_SCOPE since it was valid before the
4212 failed attempt at finding the last
4213 class-or-namespace-name. */
4214 parser->scope = old_scope;
4215 parser->qualifying_scope = saved_qualifying_scope;
4216 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4217 break;
4218 /* If the next token is an identifier, and the one after
4219 that is a `::', then any valid interpretation would have
4220 found a class-or-namespace-name. */
4221 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4222 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4223 == CPP_SCOPE)
4224 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4225 != CPP_COMPL))
4227 token = cp_lexer_consume_token (parser->lexer);
4228 if (!error_p)
4230 if (!token->ambiguous_p)
4232 tree decl;
4233 tree ambiguous_decls;
4235 decl = cp_parser_lookup_name (parser, token->u.value,
4236 none_type,
4237 /*is_template=*/false,
4238 /*is_namespace=*/false,
4239 /*check_dependency=*/true,
4240 &ambiguous_decls,
4241 token->location);
4242 if (TREE_CODE (decl) == TEMPLATE_DECL)
4243 error_at (token->location,
4244 "%qD used without template parameters",
4245 decl);
4246 else if (ambiguous_decls)
4248 error_at (token->location,
4249 "reference to %qD is ambiguous",
4250 token->u.value);
4251 print_candidates (ambiguous_decls);
4252 decl = error_mark_node;
4254 else
4256 const char* msg = "is not a class or namespace";
4257 if (cxx_dialect != cxx98)
4258 msg = "is not a class, namespace, or enumeration";
4259 cp_parser_name_lookup_error
4260 (parser, token->u.value, decl, msg,
4261 token->location);
4264 parser->scope = error_mark_node;
4265 error_p = true;
4266 /* Treat this as a successful nested-name-specifier
4267 due to:
4269 [basic.lookup.qual]
4271 If the name found is not a class-name (clause
4272 _class_) or namespace-name (_namespace.def_), the
4273 program is ill-formed. */
4274 success = true;
4276 cp_lexer_consume_token (parser->lexer);
4278 break;
4280 /* We've found one valid nested-name-specifier. */
4281 success = true;
4282 /* Name lookup always gives us a DECL. */
4283 if (TREE_CODE (new_scope) == TYPE_DECL)
4284 new_scope = TREE_TYPE (new_scope);
4285 /* Uses of "template" must be followed by actual templates. */
4286 if (template_keyword_p
4287 && !(CLASS_TYPE_P (new_scope)
4288 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4289 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4290 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4291 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4292 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4293 == TEMPLATE_ID_EXPR)))
4294 permerror (input_location, TYPE_P (new_scope)
4295 ? "%qT is not a template"
4296 : "%qD is not a template",
4297 new_scope);
4298 /* If it is a class scope, try to complete it; we are about to
4299 be looking up names inside the class. */
4300 if (TYPE_P (new_scope)
4301 /* Since checking types for dependency can be expensive,
4302 avoid doing it if the type is already complete. */
4303 && !COMPLETE_TYPE_P (new_scope)
4304 /* Do not try to complete dependent types. */
4305 && !dependent_type_p (new_scope))
4307 new_scope = complete_type (new_scope);
4308 /* If it is a typedef to current class, use the current
4309 class instead, as the typedef won't have any names inside
4310 it yet. */
4311 if (!COMPLETE_TYPE_P (new_scope)
4312 && currently_open_class (new_scope))
4313 new_scope = TYPE_MAIN_VARIANT (new_scope);
4315 /* Make sure we look in the right scope the next time through
4316 the loop. */
4317 parser->scope = new_scope;
4320 /* If parsing tentatively, replace the sequence of tokens that makes
4321 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4322 token. That way, should we re-parse the token stream, we will
4323 not have to repeat the effort required to do the parse, nor will
4324 we issue duplicate error messages. */
4325 if (success && start)
4327 cp_token *token;
4329 token = cp_lexer_token_at (parser->lexer, start);
4330 /* Reset the contents of the START token. */
4331 token->type = CPP_NESTED_NAME_SPECIFIER;
4332 /* Retrieve any deferred checks. Do not pop this access checks yet
4333 so the memory will not be reclaimed during token replacing below. */
4334 token->u.tree_check_value = GGC_CNEW (struct tree_check);
4335 token->u.tree_check_value->value = parser->scope;
4336 token->u.tree_check_value->checks = get_deferred_access_checks ();
4337 token->u.tree_check_value->qualifying_scope =
4338 parser->qualifying_scope;
4339 token->keyword = RID_MAX;
4341 /* Purge all subsequent tokens. */
4342 cp_lexer_purge_tokens_after (parser->lexer, start);
4345 if (start)
4346 pop_to_parent_deferring_access_checks ();
4348 return success ? parser->scope : NULL_TREE;
4351 /* Parse a nested-name-specifier. See
4352 cp_parser_nested_name_specifier_opt for details. This function
4353 behaves identically, except that it will an issue an error if no
4354 nested-name-specifier is present. */
4356 static tree
4357 cp_parser_nested_name_specifier (cp_parser *parser,
4358 bool typename_keyword_p,
4359 bool check_dependency_p,
4360 bool type_p,
4361 bool is_declaration)
4363 tree scope;
4365 /* Look for the nested-name-specifier. */
4366 scope = cp_parser_nested_name_specifier_opt (parser,
4367 typename_keyword_p,
4368 check_dependency_p,
4369 type_p,
4370 is_declaration);
4371 /* If it was not present, issue an error message. */
4372 if (!scope)
4374 cp_parser_error (parser, "expected nested-name-specifier");
4375 parser->scope = NULL_TREE;
4378 return scope;
4381 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4382 this is either a class-name or a namespace-name (which corresponds
4383 to the class-or-namespace-name production in the grammar). For
4384 C++0x, it can also be a type-name that refers to an enumeration
4385 type.
4387 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4388 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4389 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4390 TYPE_P is TRUE iff the next name should be taken as a class-name,
4391 even the same name is declared to be another entity in the same
4392 scope.
4394 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4395 specified by the class-or-namespace-name. If neither is found the
4396 ERROR_MARK_NODE is returned. */
4398 static tree
4399 cp_parser_qualifying_entity (cp_parser *parser,
4400 bool typename_keyword_p,
4401 bool template_keyword_p,
4402 bool check_dependency_p,
4403 bool type_p,
4404 bool is_declaration)
4406 tree saved_scope;
4407 tree saved_qualifying_scope;
4408 tree saved_object_scope;
4409 tree scope;
4410 bool only_class_p;
4411 bool successful_parse_p;
4413 /* Before we try to parse the class-name, we must save away the
4414 current PARSER->SCOPE since cp_parser_class_name will destroy
4415 it. */
4416 saved_scope = parser->scope;
4417 saved_qualifying_scope = parser->qualifying_scope;
4418 saved_object_scope = parser->object_scope;
4419 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4420 there is no need to look for a namespace-name. */
4421 only_class_p = template_keyword_p
4422 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4423 if (!only_class_p)
4424 cp_parser_parse_tentatively (parser);
4425 scope = cp_parser_class_name (parser,
4426 typename_keyword_p,
4427 template_keyword_p,
4428 type_p ? class_type : none_type,
4429 check_dependency_p,
4430 /*class_head_p=*/false,
4431 is_declaration);
4432 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4433 /* If that didn't work and we're in C++0x mode, try for a type-name. */
4434 if (!only_class_p
4435 && cxx_dialect != cxx98
4436 && !successful_parse_p)
4438 /* Restore the saved scope. */
4439 parser->scope = saved_scope;
4440 parser->qualifying_scope = saved_qualifying_scope;
4441 parser->object_scope = saved_object_scope;
4443 /* Parse tentatively. */
4444 cp_parser_parse_tentatively (parser);
4446 /* Parse a typedef-name or enum-name. */
4447 scope = cp_parser_nonclass_name (parser);
4449 /* "If the name found does not designate a namespace or a class,
4450 enumeration, or dependent type, the program is ill-formed."
4452 We cover classes and dependent types above and namespaces below,
4453 so this code is only looking for enums. */
4454 if (!scope || TREE_CODE (scope) != TYPE_DECL
4455 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
4456 cp_parser_simulate_error (parser);
4458 successful_parse_p = cp_parser_parse_definitely (parser);
4460 /* If that didn't work, try for a namespace-name. */
4461 if (!only_class_p && !successful_parse_p)
4463 /* Restore the saved scope. */
4464 parser->scope = saved_scope;
4465 parser->qualifying_scope = saved_qualifying_scope;
4466 parser->object_scope = saved_object_scope;
4467 /* If we are not looking at an identifier followed by the scope
4468 resolution operator, then this is not part of a
4469 nested-name-specifier. (Note that this function is only used
4470 to parse the components of a nested-name-specifier.) */
4471 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4472 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4473 return error_mark_node;
4474 scope = cp_parser_namespace_name (parser);
4477 return scope;
4480 /* Parse a postfix-expression.
4482 postfix-expression:
4483 primary-expression
4484 postfix-expression [ expression ]
4485 postfix-expression ( expression-list [opt] )
4486 simple-type-specifier ( expression-list [opt] )
4487 typename :: [opt] nested-name-specifier identifier
4488 ( expression-list [opt] )
4489 typename :: [opt] nested-name-specifier template [opt] template-id
4490 ( expression-list [opt] )
4491 postfix-expression . template [opt] id-expression
4492 postfix-expression -> template [opt] id-expression
4493 postfix-expression . pseudo-destructor-name
4494 postfix-expression -> pseudo-destructor-name
4495 postfix-expression ++
4496 postfix-expression --
4497 dynamic_cast < type-id > ( expression )
4498 static_cast < type-id > ( expression )
4499 reinterpret_cast < type-id > ( expression )
4500 const_cast < type-id > ( expression )
4501 typeid ( expression )
4502 typeid ( type-id )
4504 GNU Extension:
4506 postfix-expression:
4507 ( type-id ) { initializer-list , [opt] }
4509 This extension is a GNU version of the C99 compound-literal
4510 construct. (The C99 grammar uses `type-name' instead of `type-id',
4511 but they are essentially the same concept.)
4513 If ADDRESS_P is true, the postfix expression is the operand of the
4514 `&' operator. CAST_P is true if this expression is the target of a
4515 cast.
4517 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4518 class member access expressions [expr.ref].
4520 Returns a representation of the expression. */
4522 static tree
4523 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4524 bool member_access_only_p,
4525 cp_id_kind * pidk_return)
4527 cp_token *token;
4528 enum rid keyword;
4529 cp_id_kind idk = CP_ID_KIND_NONE;
4530 tree postfix_expression = NULL_TREE;
4531 bool is_member_access = false;
4533 /* Peek at the next token. */
4534 token = cp_lexer_peek_token (parser->lexer);
4535 /* Some of the productions are determined by keywords. */
4536 keyword = token->keyword;
4537 switch (keyword)
4539 case RID_DYNCAST:
4540 case RID_STATCAST:
4541 case RID_REINTCAST:
4542 case RID_CONSTCAST:
4544 tree type;
4545 tree expression;
4546 const char *saved_message;
4548 /* All of these can be handled in the same way from the point
4549 of view of parsing. Begin by consuming the token
4550 identifying the cast. */
4551 cp_lexer_consume_token (parser->lexer);
4553 /* New types cannot be defined in the cast. */
4554 saved_message = parser->type_definition_forbidden_message;
4555 parser->type_definition_forbidden_message
4556 = G_("types may not be defined in casts");
4558 /* Look for the opening `<'. */
4559 cp_parser_require (parser, CPP_LESS, "%<<%>");
4560 /* Parse the type to which we are casting. */
4561 type = cp_parser_type_id (parser);
4562 /* Look for the closing `>'. */
4563 cp_parser_require (parser, CPP_GREATER, "%<>%>");
4564 /* Restore the old message. */
4565 parser->type_definition_forbidden_message = saved_message;
4567 /* And the expression which is being cast. */
4568 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4569 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4570 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4572 /* Only type conversions to integral or enumeration types
4573 can be used in constant-expressions. */
4574 if (!cast_valid_in_integral_constant_expression_p (type)
4575 && (cp_parser_non_integral_constant_expression
4576 (parser,
4577 "a cast to a type other than an integral or "
4578 "enumeration type")))
4579 return error_mark_node;
4581 switch (keyword)
4583 case RID_DYNCAST:
4584 postfix_expression
4585 = build_dynamic_cast (type, expression, tf_warning_or_error);
4586 break;
4587 case RID_STATCAST:
4588 postfix_expression
4589 = build_static_cast (type, expression, tf_warning_or_error);
4590 break;
4591 case RID_REINTCAST:
4592 postfix_expression
4593 = build_reinterpret_cast (type, expression,
4594 tf_warning_or_error);
4595 break;
4596 case RID_CONSTCAST:
4597 postfix_expression
4598 = build_const_cast (type, expression, tf_warning_or_error);
4599 break;
4600 default:
4601 gcc_unreachable ();
4604 break;
4606 case RID_TYPEID:
4608 tree type;
4609 const char *saved_message;
4610 bool saved_in_type_id_in_expr_p;
4612 /* Consume the `typeid' token. */
4613 cp_lexer_consume_token (parser->lexer);
4614 /* Look for the `(' token. */
4615 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4616 /* Types cannot be defined in a `typeid' expression. */
4617 saved_message = parser->type_definition_forbidden_message;
4618 parser->type_definition_forbidden_message
4619 = G_("types may not be defined in a %<typeid%> expression");
4620 /* We can't be sure yet whether we're looking at a type-id or an
4621 expression. */
4622 cp_parser_parse_tentatively (parser);
4623 /* Try a type-id first. */
4624 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4625 parser->in_type_id_in_expr_p = true;
4626 type = cp_parser_type_id (parser);
4627 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4628 /* Look for the `)' token. Otherwise, we can't be sure that
4629 we're not looking at an expression: consider `typeid (int
4630 (3))', for example. */
4631 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4632 /* If all went well, simply lookup the type-id. */
4633 if (cp_parser_parse_definitely (parser))
4634 postfix_expression = get_typeid (type);
4635 /* Otherwise, fall back to the expression variant. */
4636 else
4638 tree expression;
4640 /* Look for an expression. */
4641 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4642 /* Compute its typeid. */
4643 postfix_expression = build_typeid (expression);
4644 /* Look for the `)' token. */
4645 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4647 /* Restore the saved message. */
4648 parser->type_definition_forbidden_message = saved_message;
4649 /* `typeid' may not appear in an integral constant expression. */
4650 if (cp_parser_non_integral_constant_expression(parser,
4651 "%<typeid%> operator"))
4652 return error_mark_node;
4654 break;
4656 case RID_TYPENAME:
4658 tree type;
4659 /* The syntax permitted here is the same permitted for an
4660 elaborated-type-specifier. */
4661 type = cp_parser_elaborated_type_specifier (parser,
4662 /*is_friend=*/false,
4663 /*is_declaration=*/false);
4664 postfix_expression = cp_parser_functional_cast (parser, type);
4666 break;
4668 default:
4670 tree type;
4672 /* If the next thing is a simple-type-specifier, we may be
4673 looking at a functional cast. We could also be looking at
4674 an id-expression. So, we try the functional cast, and if
4675 that doesn't work we fall back to the primary-expression. */
4676 cp_parser_parse_tentatively (parser);
4677 /* Look for the simple-type-specifier. */
4678 type = cp_parser_simple_type_specifier (parser,
4679 /*decl_specs=*/NULL,
4680 CP_PARSER_FLAGS_NONE);
4681 /* Parse the cast itself. */
4682 if (!cp_parser_error_occurred (parser))
4683 postfix_expression
4684 = cp_parser_functional_cast (parser, type);
4685 /* If that worked, we're done. */
4686 if (cp_parser_parse_definitely (parser))
4687 break;
4689 /* If the functional-cast didn't work out, try a
4690 compound-literal. */
4691 if (cp_parser_allow_gnu_extensions_p (parser)
4692 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4694 VEC(constructor_elt,gc) *initializer_list = NULL;
4695 bool saved_in_type_id_in_expr_p;
4697 cp_parser_parse_tentatively (parser);
4698 /* Consume the `('. */
4699 cp_lexer_consume_token (parser->lexer);
4700 /* Parse the type. */
4701 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4702 parser->in_type_id_in_expr_p = true;
4703 type = cp_parser_type_id (parser);
4704 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4705 /* Look for the `)'. */
4706 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4707 /* Look for the `{'. */
4708 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4709 /* If things aren't going well, there's no need to
4710 keep going. */
4711 if (!cp_parser_error_occurred (parser))
4713 bool non_constant_p;
4714 /* Parse the initializer-list. */
4715 initializer_list
4716 = cp_parser_initializer_list (parser, &non_constant_p);
4717 /* Allow a trailing `,'. */
4718 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4719 cp_lexer_consume_token (parser->lexer);
4720 /* Look for the final `}'. */
4721 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4723 /* If that worked, we're definitely looking at a
4724 compound-literal expression. */
4725 if (cp_parser_parse_definitely (parser))
4727 /* Warn the user that a compound literal is not
4728 allowed in standard C++. */
4729 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4730 /* For simplicity, we disallow compound literals in
4731 constant-expressions. We could
4732 allow compound literals of integer type, whose
4733 initializer was a constant, in constant
4734 expressions. Permitting that usage, as a further
4735 extension, would not change the meaning of any
4736 currently accepted programs. (Of course, as
4737 compound literals are not part of ISO C++, the
4738 standard has nothing to say.) */
4739 if (cp_parser_non_integral_constant_expression
4740 (parser, "non-constant compound literals"))
4742 postfix_expression = error_mark_node;
4743 break;
4745 /* Form the representation of the compound-literal. */
4746 postfix_expression
4747 = (finish_compound_literal
4748 (type, build_constructor (init_list_type_node,
4749 initializer_list)));
4750 break;
4754 /* It must be a primary-expression. */
4755 postfix_expression
4756 = cp_parser_primary_expression (parser, address_p, cast_p,
4757 /*template_arg_p=*/false,
4758 &idk);
4760 break;
4763 /* Keep looping until the postfix-expression is complete. */
4764 while (true)
4766 if (idk == CP_ID_KIND_UNQUALIFIED
4767 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4768 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4769 /* It is not a Koenig lookup function call. */
4770 postfix_expression
4771 = unqualified_name_lookup_error (postfix_expression);
4773 /* Peek at the next token. */
4774 token = cp_lexer_peek_token (parser->lexer);
4776 switch (token->type)
4778 case CPP_OPEN_SQUARE:
4779 postfix_expression
4780 = cp_parser_postfix_open_square_expression (parser,
4781 postfix_expression,
4782 false);
4783 idk = CP_ID_KIND_NONE;
4784 is_member_access = false;
4785 break;
4787 case CPP_OPEN_PAREN:
4788 /* postfix-expression ( expression-list [opt] ) */
4790 bool koenig_p;
4791 bool is_builtin_constant_p;
4792 bool saved_integral_constant_expression_p = false;
4793 bool saved_non_integral_constant_expression_p = false;
4794 VEC(tree,gc) *args;
4796 is_member_access = false;
4798 is_builtin_constant_p
4799 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4800 if (is_builtin_constant_p)
4802 /* The whole point of __builtin_constant_p is to allow
4803 non-constant expressions to appear as arguments. */
4804 saved_integral_constant_expression_p
4805 = parser->integral_constant_expression_p;
4806 saved_non_integral_constant_expression_p
4807 = parser->non_integral_constant_expression_p;
4808 parser->integral_constant_expression_p = false;
4810 args = (cp_parser_parenthesized_expression_list
4811 (parser, non_attr,
4812 /*cast_p=*/false, /*allow_expansion_p=*/true,
4813 /*non_constant_p=*/NULL));
4814 if (is_builtin_constant_p)
4816 parser->integral_constant_expression_p
4817 = saved_integral_constant_expression_p;
4818 parser->non_integral_constant_expression_p
4819 = saved_non_integral_constant_expression_p;
4822 if (args == NULL)
4824 postfix_expression = error_mark_node;
4825 break;
4828 /* Function calls are not permitted in
4829 constant-expressions. */
4830 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4831 && cp_parser_non_integral_constant_expression (parser,
4832 "a function call"))
4834 postfix_expression = error_mark_node;
4835 release_tree_vector (args);
4836 break;
4839 koenig_p = false;
4840 if (idk == CP_ID_KIND_UNQUALIFIED
4841 || idk == CP_ID_KIND_TEMPLATE_ID)
4843 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4845 if (!VEC_empty (tree, args))
4847 koenig_p = true;
4848 if (!any_type_dependent_arguments_p (args))
4849 postfix_expression
4850 = perform_koenig_lookup (postfix_expression, args);
4852 else
4853 postfix_expression
4854 = unqualified_fn_lookup_error (postfix_expression);
4856 /* We do not perform argument-dependent lookup if
4857 normal lookup finds a non-function, in accordance
4858 with the expected resolution of DR 218. */
4859 else if (!VEC_empty (tree, args)
4860 && is_overloaded_fn (postfix_expression))
4862 tree fn = get_first_fn (postfix_expression);
4863 fn = STRIP_TEMPLATE (fn);
4865 /* Do not do argument dependent lookup if regular
4866 lookup finds a member function or a block-scope
4867 function declaration. [basic.lookup.argdep]/3 */
4868 if (!DECL_FUNCTION_MEMBER_P (fn)
4869 && !DECL_LOCAL_FUNCTION_P (fn))
4871 koenig_p = true;
4872 if (!any_type_dependent_arguments_p (args))
4873 postfix_expression
4874 = perform_koenig_lookup (postfix_expression, args);
4879 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4881 tree instance = TREE_OPERAND (postfix_expression, 0);
4882 tree fn = TREE_OPERAND (postfix_expression, 1);
4884 if (processing_template_decl
4885 && (type_dependent_expression_p (instance)
4886 || (!BASELINK_P (fn)
4887 && TREE_CODE (fn) != FIELD_DECL)
4888 || type_dependent_expression_p (fn)
4889 || any_type_dependent_arguments_p (args)))
4891 postfix_expression
4892 = build_nt_call_vec (postfix_expression, args);
4893 release_tree_vector (args);
4894 break;
4897 if (BASELINK_P (fn))
4899 postfix_expression
4900 = (build_new_method_call
4901 (instance, fn, &args, NULL_TREE,
4902 (idk == CP_ID_KIND_QUALIFIED
4903 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4904 /*fn_p=*/NULL,
4905 tf_warning_or_error));
4907 else
4908 postfix_expression
4909 = finish_call_expr (postfix_expression, &args,
4910 /*disallow_virtual=*/false,
4911 /*koenig_p=*/false,
4912 tf_warning_or_error);
4914 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4915 || TREE_CODE (postfix_expression) == MEMBER_REF
4916 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4917 postfix_expression = (build_offset_ref_call_from_tree
4918 (postfix_expression, &args));
4919 else if (idk == CP_ID_KIND_QUALIFIED)
4920 /* A call to a static class member, or a namespace-scope
4921 function. */
4922 postfix_expression
4923 = finish_call_expr (postfix_expression, &args,
4924 /*disallow_virtual=*/true,
4925 koenig_p,
4926 tf_warning_or_error);
4927 else
4928 /* All other function calls. */
4929 postfix_expression
4930 = finish_call_expr (postfix_expression, &args,
4931 /*disallow_virtual=*/false,
4932 koenig_p,
4933 tf_warning_or_error);
4935 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4936 idk = CP_ID_KIND_NONE;
4938 release_tree_vector (args);
4940 break;
4942 case CPP_DOT:
4943 case CPP_DEREF:
4944 /* postfix-expression . template [opt] id-expression
4945 postfix-expression . pseudo-destructor-name
4946 postfix-expression -> template [opt] id-expression
4947 postfix-expression -> pseudo-destructor-name */
4949 /* Consume the `.' or `->' operator. */
4950 cp_lexer_consume_token (parser->lexer);
4952 postfix_expression
4953 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4954 postfix_expression,
4955 false, &idk,
4956 token->location);
4958 is_member_access = true;
4959 break;
4961 case CPP_PLUS_PLUS:
4962 /* postfix-expression ++ */
4963 /* Consume the `++' token. */
4964 cp_lexer_consume_token (parser->lexer);
4965 /* Generate a representation for the complete expression. */
4966 postfix_expression
4967 = finish_increment_expr (postfix_expression,
4968 POSTINCREMENT_EXPR);
4969 /* Increments may not appear in constant-expressions. */
4970 if (cp_parser_non_integral_constant_expression (parser,
4971 "an increment"))
4972 postfix_expression = error_mark_node;
4973 idk = CP_ID_KIND_NONE;
4974 is_member_access = false;
4975 break;
4977 case CPP_MINUS_MINUS:
4978 /* postfix-expression -- */
4979 /* Consume the `--' token. */
4980 cp_lexer_consume_token (parser->lexer);
4981 /* Generate a representation for the complete expression. */
4982 postfix_expression
4983 = finish_increment_expr (postfix_expression,
4984 POSTDECREMENT_EXPR);
4985 /* Decrements may not appear in constant-expressions. */
4986 if (cp_parser_non_integral_constant_expression (parser,
4987 "a decrement"))
4988 postfix_expression = error_mark_node;
4989 idk = CP_ID_KIND_NONE;
4990 is_member_access = false;
4991 break;
4993 default:
4994 if (pidk_return != NULL)
4995 * pidk_return = idk;
4996 if (member_access_only_p)
4997 return is_member_access? postfix_expression : error_mark_node;
4998 else
4999 return postfix_expression;
5003 /* We should never get here. */
5004 gcc_unreachable ();
5005 return error_mark_node;
5008 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5009 by cp_parser_builtin_offsetof. We're looking for
5011 postfix-expression [ expression ]
5013 FOR_OFFSETOF is set if we're being called in that context, which
5014 changes how we deal with integer constant expressions. */
5016 static tree
5017 cp_parser_postfix_open_square_expression (cp_parser *parser,
5018 tree postfix_expression,
5019 bool for_offsetof)
5021 tree index;
5023 /* Consume the `[' token. */
5024 cp_lexer_consume_token (parser->lexer);
5026 /* Parse the index expression. */
5027 /* ??? For offsetof, there is a question of what to allow here. If
5028 offsetof is not being used in an integral constant expression context,
5029 then we *could* get the right answer by computing the value at runtime.
5030 If we are in an integral constant expression context, then we might
5031 could accept any constant expression; hard to say without analysis.
5032 Rather than open the barn door too wide right away, allow only integer
5033 constant expressions here. */
5034 if (for_offsetof)
5035 index = cp_parser_constant_expression (parser, false, NULL);
5036 else
5037 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5039 /* Look for the closing `]'. */
5040 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5042 /* Build the ARRAY_REF. */
5043 postfix_expression = grok_array_decl (postfix_expression, index);
5045 /* When not doing offsetof, array references are not permitted in
5046 constant-expressions. */
5047 if (!for_offsetof
5048 && (cp_parser_non_integral_constant_expression
5049 (parser, "an array reference")))
5050 postfix_expression = error_mark_node;
5052 return postfix_expression;
5055 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5056 by cp_parser_builtin_offsetof. We're looking for
5058 postfix-expression . template [opt] id-expression
5059 postfix-expression . pseudo-destructor-name
5060 postfix-expression -> template [opt] id-expression
5061 postfix-expression -> pseudo-destructor-name
5063 FOR_OFFSETOF is set if we're being called in that context. That sorta
5064 limits what of the above we'll actually accept, but nevermind.
5065 TOKEN_TYPE is the "." or "->" token, which will already have been
5066 removed from the stream. */
5068 static tree
5069 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5070 enum cpp_ttype token_type,
5071 tree postfix_expression,
5072 bool for_offsetof, cp_id_kind *idk,
5073 location_t location)
5075 tree name;
5076 bool dependent_p;
5077 bool pseudo_destructor_p;
5078 tree scope = NULL_TREE;
5080 /* If this is a `->' operator, dereference the pointer. */
5081 if (token_type == CPP_DEREF)
5082 postfix_expression = build_x_arrow (postfix_expression);
5083 /* Check to see whether or not the expression is type-dependent. */
5084 dependent_p = type_dependent_expression_p (postfix_expression);
5085 /* The identifier following the `->' or `.' is not qualified. */
5086 parser->scope = NULL_TREE;
5087 parser->qualifying_scope = NULL_TREE;
5088 parser->object_scope = NULL_TREE;
5089 *idk = CP_ID_KIND_NONE;
5091 /* Enter the scope corresponding to the type of the object
5092 given by the POSTFIX_EXPRESSION. */
5093 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5095 scope = TREE_TYPE (postfix_expression);
5096 /* According to the standard, no expression should ever have
5097 reference type. Unfortunately, we do not currently match
5098 the standard in this respect in that our internal representation
5099 of an expression may have reference type even when the standard
5100 says it does not. Therefore, we have to manually obtain the
5101 underlying type here. */
5102 scope = non_reference (scope);
5103 /* The type of the POSTFIX_EXPRESSION must be complete. */
5104 if (scope == unknown_type_node)
5106 error_at (location, "%qE does not have class type",
5107 postfix_expression);
5108 scope = NULL_TREE;
5110 else
5111 scope = complete_type_or_else (scope, NULL_TREE);
5112 /* Let the name lookup machinery know that we are processing a
5113 class member access expression. */
5114 parser->context->object_type = scope;
5115 /* If something went wrong, we want to be able to discern that case,
5116 as opposed to the case where there was no SCOPE due to the type
5117 of expression being dependent. */
5118 if (!scope)
5119 scope = error_mark_node;
5120 /* If the SCOPE was erroneous, make the various semantic analysis
5121 functions exit quickly -- and without issuing additional error
5122 messages. */
5123 if (scope == error_mark_node)
5124 postfix_expression = error_mark_node;
5127 /* Assume this expression is not a pseudo-destructor access. */
5128 pseudo_destructor_p = false;
5130 /* If the SCOPE is a scalar type, then, if this is a valid program,
5131 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5132 is type dependent, it can be pseudo-destructor-name or something else.
5133 Try to parse it as pseudo-destructor-name first. */
5134 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5136 tree s;
5137 tree type;
5139 cp_parser_parse_tentatively (parser);
5140 /* Parse the pseudo-destructor-name. */
5141 s = NULL_TREE;
5142 cp_parser_pseudo_destructor_name (parser, &s, &type);
5143 if (dependent_p
5144 && (cp_parser_error_occurred (parser)
5145 || TREE_CODE (type) != TYPE_DECL
5146 || !SCALAR_TYPE_P (TREE_TYPE (type))))
5147 cp_parser_abort_tentative_parse (parser);
5148 else if (cp_parser_parse_definitely (parser))
5150 pseudo_destructor_p = true;
5151 postfix_expression
5152 = finish_pseudo_destructor_expr (postfix_expression,
5153 s, TREE_TYPE (type));
5157 if (!pseudo_destructor_p)
5159 /* If the SCOPE is not a scalar type, we are looking at an
5160 ordinary class member access expression, rather than a
5161 pseudo-destructor-name. */
5162 bool template_p;
5163 cp_token *token = cp_lexer_peek_token (parser->lexer);
5164 /* Parse the id-expression. */
5165 name = (cp_parser_id_expression
5166 (parser,
5167 cp_parser_optional_template_keyword (parser),
5168 /*check_dependency_p=*/true,
5169 &template_p,
5170 /*declarator_p=*/false,
5171 /*optional_p=*/false));
5172 /* In general, build a SCOPE_REF if the member name is qualified.
5173 However, if the name was not dependent and has already been
5174 resolved; there is no need to build the SCOPE_REF. For example;
5176 struct X { void f(); };
5177 template <typename T> void f(T* t) { t->X::f(); }
5179 Even though "t" is dependent, "X::f" is not and has been resolved
5180 to a BASELINK; there is no need to include scope information. */
5182 /* But we do need to remember that there was an explicit scope for
5183 virtual function calls. */
5184 if (parser->scope)
5185 *idk = CP_ID_KIND_QUALIFIED;
5187 /* If the name is a template-id that names a type, we will get a
5188 TYPE_DECL here. That is invalid code. */
5189 if (TREE_CODE (name) == TYPE_DECL)
5191 error_at (token->location, "invalid use of %qD", name);
5192 postfix_expression = error_mark_node;
5194 else
5196 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5198 name = build_qualified_name (/*type=*/NULL_TREE,
5199 parser->scope,
5200 name,
5201 template_p);
5202 parser->scope = NULL_TREE;
5203 parser->qualifying_scope = NULL_TREE;
5204 parser->object_scope = NULL_TREE;
5206 if (scope && name && BASELINK_P (name))
5207 adjust_result_of_qualified_name_lookup
5208 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5209 postfix_expression
5210 = finish_class_member_access_expr (postfix_expression, name,
5211 template_p,
5212 tf_warning_or_error);
5216 /* We no longer need to look up names in the scope of the object on
5217 the left-hand side of the `.' or `->' operator. */
5218 parser->context->object_type = NULL_TREE;
5220 /* Outside of offsetof, these operators may not appear in
5221 constant-expressions. */
5222 if (!for_offsetof
5223 && (cp_parser_non_integral_constant_expression
5224 (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5225 postfix_expression = error_mark_node;
5227 return postfix_expression;
5230 /* Parse a parenthesized expression-list.
5232 expression-list:
5233 assignment-expression
5234 expression-list, assignment-expression
5236 attribute-list:
5237 expression-list
5238 identifier
5239 identifier, expression-list
5241 CAST_P is true if this expression is the target of a cast.
5243 ALLOW_EXPANSION_P is true if this expression allows expansion of an
5244 argument pack.
5246 Returns a vector of trees. Each element is a representation of an
5247 assignment-expression. NULL is returned if the ( and or ) are
5248 missing. An empty, but allocated, vector is returned on no
5249 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
5250 if we are parsing an attribute list for an attribute that wants a
5251 plain identifier argument, normal_attr for an attribute that wants
5252 an expression, or non_attr if we aren't parsing an attribute list. If
5253 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5254 not all of the expressions in the list were constant. */
5256 static VEC(tree,gc) *
5257 cp_parser_parenthesized_expression_list (cp_parser* parser,
5258 int is_attribute_list,
5259 bool cast_p,
5260 bool allow_expansion_p,
5261 bool *non_constant_p)
5263 VEC(tree,gc) *expression_list;
5264 bool fold_expr_p = is_attribute_list != non_attr;
5265 tree identifier = NULL_TREE;
5266 bool saved_greater_than_is_operator_p;
5268 /* Assume all the expressions will be constant. */
5269 if (non_constant_p)
5270 *non_constant_p = false;
5272 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5273 return NULL;
5275 expression_list = make_tree_vector ();
5277 /* Within a parenthesized expression, a `>' token is always
5278 the greater-than operator. */
5279 saved_greater_than_is_operator_p
5280 = parser->greater_than_is_operator_p;
5281 parser->greater_than_is_operator_p = true;
5283 /* Consume expressions until there are no more. */
5284 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5285 while (true)
5287 tree expr;
5289 /* At the beginning of attribute lists, check to see if the
5290 next token is an identifier. */
5291 if (is_attribute_list == id_attr
5292 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5294 cp_token *token;
5296 /* Consume the identifier. */
5297 token = cp_lexer_consume_token (parser->lexer);
5298 /* Save the identifier. */
5299 identifier = token->u.value;
5301 else
5303 bool expr_non_constant_p;
5305 /* Parse the next assignment-expression. */
5306 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5308 /* A braced-init-list. */
5309 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5310 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5311 if (non_constant_p && expr_non_constant_p)
5312 *non_constant_p = true;
5314 else if (non_constant_p)
5316 expr = (cp_parser_constant_expression
5317 (parser, /*allow_non_constant_p=*/true,
5318 &expr_non_constant_p));
5319 if (expr_non_constant_p)
5320 *non_constant_p = true;
5322 else
5323 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5325 if (fold_expr_p)
5326 expr = fold_non_dependent_expr (expr);
5328 /* If we have an ellipsis, then this is an expression
5329 expansion. */
5330 if (allow_expansion_p
5331 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5333 /* Consume the `...'. */
5334 cp_lexer_consume_token (parser->lexer);
5336 /* Build the argument pack. */
5337 expr = make_pack_expansion (expr);
5340 /* Add it to the list. We add error_mark_node
5341 expressions to the list, so that we can still tell if
5342 the correct form for a parenthesized expression-list
5343 is found. That gives better errors. */
5344 VEC_safe_push (tree, gc, expression_list, expr);
5346 if (expr == error_mark_node)
5347 goto skip_comma;
5350 /* After the first item, attribute lists look the same as
5351 expression lists. */
5352 is_attribute_list = non_attr;
5354 get_comma:;
5355 /* If the next token isn't a `,', then we are done. */
5356 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5357 break;
5359 /* Otherwise, consume the `,' and keep going. */
5360 cp_lexer_consume_token (parser->lexer);
5363 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5365 int ending;
5367 skip_comma:;
5368 /* We try and resync to an unnested comma, as that will give the
5369 user better diagnostics. */
5370 ending = cp_parser_skip_to_closing_parenthesis (parser,
5371 /*recovering=*/true,
5372 /*or_comma=*/true,
5373 /*consume_paren=*/true);
5374 if (ending < 0)
5375 goto get_comma;
5376 if (!ending)
5378 parser->greater_than_is_operator_p
5379 = saved_greater_than_is_operator_p;
5380 return NULL;
5384 parser->greater_than_is_operator_p
5385 = saved_greater_than_is_operator_p;
5387 if (identifier)
5388 VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5390 return expression_list;
5393 /* Parse a pseudo-destructor-name.
5395 pseudo-destructor-name:
5396 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5397 :: [opt] nested-name-specifier template template-id :: ~ type-name
5398 :: [opt] nested-name-specifier [opt] ~ type-name
5400 If either of the first two productions is used, sets *SCOPE to the
5401 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5402 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5403 or ERROR_MARK_NODE if the parse fails. */
5405 static void
5406 cp_parser_pseudo_destructor_name (cp_parser* parser,
5407 tree* scope,
5408 tree* type)
5410 bool nested_name_specifier_p;
5412 /* Assume that things will not work out. */
5413 *type = error_mark_node;
5415 /* Look for the optional `::' operator. */
5416 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5417 /* Look for the optional nested-name-specifier. */
5418 nested_name_specifier_p
5419 = (cp_parser_nested_name_specifier_opt (parser,
5420 /*typename_keyword_p=*/false,
5421 /*check_dependency_p=*/true,
5422 /*type_p=*/false,
5423 /*is_declaration=*/false)
5424 != NULL_TREE);
5425 /* Now, if we saw a nested-name-specifier, we might be doing the
5426 second production. */
5427 if (nested_name_specifier_p
5428 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5430 /* Consume the `template' keyword. */
5431 cp_lexer_consume_token (parser->lexer);
5432 /* Parse the template-id. */
5433 cp_parser_template_id (parser,
5434 /*template_keyword_p=*/true,
5435 /*check_dependency_p=*/false,
5436 /*is_declaration=*/true);
5437 /* Look for the `::' token. */
5438 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5440 /* If the next token is not a `~', then there might be some
5441 additional qualification. */
5442 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5444 /* At this point, we're looking for "type-name :: ~". The type-name
5445 must not be a class-name, since this is a pseudo-destructor. So,
5446 it must be either an enum-name, or a typedef-name -- both of which
5447 are just identifiers. So, we peek ahead to check that the "::"
5448 and "~" tokens are present; if they are not, then we can avoid
5449 calling type_name. */
5450 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5451 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5452 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5454 cp_parser_error (parser, "non-scalar type");
5455 return;
5458 /* Look for the type-name. */
5459 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5460 if (*scope == error_mark_node)
5461 return;
5463 /* Look for the `::' token. */
5464 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5466 else
5467 *scope = NULL_TREE;
5469 /* Look for the `~'. */
5470 cp_parser_require (parser, CPP_COMPL, "%<~%>");
5471 /* Look for the type-name again. We are not responsible for
5472 checking that it matches the first type-name. */
5473 *type = cp_parser_nonclass_name (parser);
5476 /* Parse a unary-expression.
5478 unary-expression:
5479 postfix-expression
5480 ++ cast-expression
5481 -- cast-expression
5482 unary-operator cast-expression
5483 sizeof unary-expression
5484 sizeof ( type-id )
5485 new-expression
5486 delete-expression
5488 GNU Extensions:
5490 unary-expression:
5491 __extension__ cast-expression
5492 __alignof__ unary-expression
5493 __alignof__ ( type-id )
5494 __real__ cast-expression
5495 __imag__ cast-expression
5496 && identifier
5498 ADDRESS_P is true iff the unary-expression is appearing as the
5499 operand of the `&' operator. CAST_P is true if this expression is
5500 the target of a cast.
5502 Returns a representation of the expression. */
5504 static tree
5505 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5506 cp_id_kind * pidk)
5508 cp_token *token;
5509 enum tree_code unary_operator;
5511 /* Peek at the next token. */
5512 token = cp_lexer_peek_token (parser->lexer);
5513 /* Some keywords give away the kind of expression. */
5514 if (token->type == CPP_KEYWORD)
5516 enum rid keyword = token->keyword;
5518 switch (keyword)
5520 case RID_ALIGNOF:
5521 case RID_SIZEOF:
5523 tree operand;
5524 enum tree_code op;
5526 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5527 /* Consume the token. */
5528 cp_lexer_consume_token (parser->lexer);
5529 /* Parse the operand. */
5530 operand = cp_parser_sizeof_operand (parser, keyword);
5532 if (TYPE_P (operand))
5533 return cxx_sizeof_or_alignof_type (operand, op, true);
5534 else
5535 return cxx_sizeof_or_alignof_expr (operand, op, true);
5538 case RID_NEW:
5539 return cp_parser_new_expression (parser);
5541 case RID_DELETE:
5542 return cp_parser_delete_expression (parser);
5544 case RID_EXTENSION:
5546 /* The saved value of the PEDANTIC flag. */
5547 int saved_pedantic;
5548 tree expr;
5550 /* Save away the PEDANTIC flag. */
5551 cp_parser_extension_opt (parser, &saved_pedantic);
5552 /* Parse the cast-expression. */
5553 expr = cp_parser_simple_cast_expression (parser);
5554 /* Restore the PEDANTIC flag. */
5555 pedantic = saved_pedantic;
5557 return expr;
5560 case RID_REALPART:
5561 case RID_IMAGPART:
5563 tree expression;
5565 /* Consume the `__real__' or `__imag__' token. */
5566 cp_lexer_consume_token (parser->lexer);
5567 /* Parse the cast-expression. */
5568 expression = cp_parser_simple_cast_expression (parser);
5569 /* Create the complete representation. */
5570 return build_x_unary_op ((keyword == RID_REALPART
5571 ? REALPART_EXPR : IMAGPART_EXPR),
5572 expression,
5573 tf_warning_or_error);
5575 break;
5577 default:
5578 break;
5582 /* Look for the `:: new' and `:: delete', which also signal the
5583 beginning of a new-expression, or delete-expression,
5584 respectively. If the next token is `::', then it might be one of
5585 these. */
5586 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5588 enum rid keyword;
5590 /* See if the token after the `::' is one of the keywords in
5591 which we're interested. */
5592 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5593 /* If it's `new', we have a new-expression. */
5594 if (keyword == RID_NEW)
5595 return cp_parser_new_expression (parser);
5596 /* Similarly, for `delete'. */
5597 else if (keyword == RID_DELETE)
5598 return cp_parser_delete_expression (parser);
5601 /* Look for a unary operator. */
5602 unary_operator = cp_parser_unary_operator (token);
5603 /* The `++' and `--' operators can be handled similarly, even though
5604 they are not technically unary-operators in the grammar. */
5605 if (unary_operator == ERROR_MARK)
5607 if (token->type == CPP_PLUS_PLUS)
5608 unary_operator = PREINCREMENT_EXPR;
5609 else if (token->type == CPP_MINUS_MINUS)
5610 unary_operator = PREDECREMENT_EXPR;
5611 /* Handle the GNU address-of-label extension. */
5612 else if (cp_parser_allow_gnu_extensions_p (parser)
5613 && token->type == CPP_AND_AND)
5615 tree identifier;
5616 tree expression;
5617 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5619 /* Consume the '&&' token. */
5620 cp_lexer_consume_token (parser->lexer);
5621 /* Look for the identifier. */
5622 identifier = cp_parser_identifier (parser);
5623 /* Create an expression representing the address. */
5624 expression = finish_label_address_expr (identifier, loc);
5625 if (cp_parser_non_integral_constant_expression (parser,
5626 "the address of a label"))
5627 expression = error_mark_node;
5628 return expression;
5631 if (unary_operator != ERROR_MARK)
5633 tree cast_expression;
5634 tree expression = error_mark_node;
5635 const char *non_constant_p = NULL;
5637 /* Consume the operator token. */
5638 token = cp_lexer_consume_token (parser->lexer);
5639 /* Parse the cast-expression. */
5640 cast_expression
5641 = cp_parser_cast_expression (parser,
5642 unary_operator == ADDR_EXPR,
5643 /*cast_p=*/false, pidk);
5644 /* Now, build an appropriate representation. */
5645 switch (unary_operator)
5647 case INDIRECT_REF:
5648 non_constant_p = "%<*%>";
5649 expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR,
5650 tf_warning_or_error);
5651 break;
5653 case ADDR_EXPR:
5654 non_constant_p = "%<&%>";
5655 /* Fall through. */
5656 case BIT_NOT_EXPR:
5657 expression = build_x_unary_op (unary_operator, cast_expression,
5658 tf_warning_or_error);
5659 break;
5661 case PREINCREMENT_EXPR:
5662 case PREDECREMENT_EXPR:
5663 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5664 ? "%<++%>" : "%<--%>");
5665 /* Fall through. */
5666 case UNARY_PLUS_EXPR:
5667 case NEGATE_EXPR:
5668 case TRUTH_NOT_EXPR:
5669 expression = finish_unary_op_expr (unary_operator, cast_expression);
5670 break;
5672 default:
5673 gcc_unreachable ();
5676 if (non_constant_p
5677 && cp_parser_non_integral_constant_expression (parser,
5678 non_constant_p))
5679 expression = error_mark_node;
5681 return expression;
5684 return cp_parser_postfix_expression (parser, address_p, cast_p,
5685 /*member_access_only_p=*/false,
5686 pidk);
5689 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5690 unary-operator, the corresponding tree code is returned. */
5692 static enum tree_code
5693 cp_parser_unary_operator (cp_token* token)
5695 switch (token->type)
5697 case CPP_MULT:
5698 return INDIRECT_REF;
5700 case CPP_AND:
5701 return ADDR_EXPR;
5703 case CPP_PLUS:
5704 return UNARY_PLUS_EXPR;
5706 case CPP_MINUS:
5707 return NEGATE_EXPR;
5709 case CPP_NOT:
5710 return TRUTH_NOT_EXPR;
5712 case CPP_COMPL:
5713 return BIT_NOT_EXPR;
5715 default:
5716 return ERROR_MARK;
5720 /* Parse a new-expression.
5722 new-expression:
5723 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5724 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5726 Returns a representation of the expression. */
5728 static tree
5729 cp_parser_new_expression (cp_parser* parser)
5731 bool global_scope_p;
5732 VEC(tree,gc) *placement;
5733 tree type;
5734 VEC(tree,gc) *initializer;
5735 tree nelts;
5736 tree ret;
5738 /* Look for the optional `::' operator. */
5739 global_scope_p
5740 = (cp_parser_global_scope_opt (parser,
5741 /*current_scope_valid_p=*/false)
5742 != NULL_TREE);
5743 /* Look for the `new' operator. */
5744 cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5745 /* There's no easy way to tell a new-placement from the
5746 `( type-id )' construct. */
5747 cp_parser_parse_tentatively (parser);
5748 /* Look for a new-placement. */
5749 placement = cp_parser_new_placement (parser);
5750 /* If that didn't work out, there's no new-placement. */
5751 if (!cp_parser_parse_definitely (parser))
5753 if (placement != NULL)
5754 release_tree_vector (placement);
5755 placement = NULL;
5758 /* If the next token is a `(', then we have a parenthesized
5759 type-id. */
5760 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5762 cp_token *token;
5763 /* Consume the `('. */
5764 cp_lexer_consume_token (parser->lexer);
5765 /* Parse the type-id. */
5766 type = cp_parser_type_id (parser);
5767 /* Look for the closing `)'. */
5768 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5769 token = cp_lexer_peek_token (parser->lexer);
5770 /* There should not be a direct-new-declarator in this production,
5771 but GCC used to allowed this, so we check and emit a sensible error
5772 message for this case. */
5773 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5775 error_at (token->location,
5776 "array bound forbidden after parenthesized type-id");
5777 inform (token->location,
5778 "try removing the parentheses around the type-id");
5779 cp_parser_direct_new_declarator (parser);
5781 nelts = NULL_TREE;
5783 /* Otherwise, there must be a new-type-id. */
5784 else
5785 type = cp_parser_new_type_id (parser, &nelts);
5787 /* If the next token is a `(' or '{', then we have a new-initializer. */
5788 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5789 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5790 initializer = cp_parser_new_initializer (parser);
5791 else
5792 initializer = NULL;
5794 /* A new-expression may not appear in an integral constant
5795 expression. */
5796 if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5797 ret = error_mark_node;
5798 else
5800 /* Create a representation of the new-expression. */
5801 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
5802 tf_warning_or_error);
5805 if (placement != NULL)
5806 release_tree_vector (placement);
5807 if (initializer != NULL)
5808 release_tree_vector (initializer);
5810 return ret;
5813 /* Parse a new-placement.
5815 new-placement:
5816 ( expression-list )
5818 Returns the same representation as for an expression-list. */
5820 static VEC(tree,gc) *
5821 cp_parser_new_placement (cp_parser* parser)
5823 VEC(tree,gc) *expression_list;
5825 /* Parse the expression-list. */
5826 expression_list = (cp_parser_parenthesized_expression_list
5827 (parser, non_attr, /*cast_p=*/false,
5828 /*allow_expansion_p=*/true,
5829 /*non_constant_p=*/NULL));
5831 return expression_list;
5834 /* Parse a new-type-id.
5836 new-type-id:
5837 type-specifier-seq new-declarator [opt]
5839 Returns the TYPE allocated. If the new-type-id indicates an array
5840 type, *NELTS is set to the number of elements in the last array
5841 bound; the TYPE will not include the last array bound. */
5843 static tree
5844 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5846 cp_decl_specifier_seq type_specifier_seq;
5847 cp_declarator *new_declarator;
5848 cp_declarator *declarator;
5849 cp_declarator *outer_declarator;
5850 const char *saved_message;
5851 tree type;
5853 /* The type-specifier sequence must not contain type definitions.
5854 (It cannot contain declarations of new types either, but if they
5855 are not definitions we will catch that because they are not
5856 complete.) */
5857 saved_message = parser->type_definition_forbidden_message;
5858 parser->type_definition_forbidden_message
5859 = G_("types may not be defined in a new-type-id");
5860 /* Parse the type-specifier-seq. */
5861 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
5862 /*is_trailing_return=*/false,
5863 &type_specifier_seq);
5864 /* Restore the old message. */
5865 parser->type_definition_forbidden_message = saved_message;
5866 /* Parse the new-declarator. */
5867 new_declarator = cp_parser_new_declarator_opt (parser);
5869 /* Determine the number of elements in the last array dimension, if
5870 any. */
5871 *nelts = NULL_TREE;
5872 /* Skip down to the last array dimension. */
5873 declarator = new_declarator;
5874 outer_declarator = NULL;
5875 while (declarator && (declarator->kind == cdk_pointer
5876 || declarator->kind == cdk_ptrmem))
5878 outer_declarator = declarator;
5879 declarator = declarator->declarator;
5881 while (declarator
5882 && declarator->kind == cdk_array
5883 && declarator->declarator
5884 && declarator->declarator->kind == cdk_array)
5886 outer_declarator = declarator;
5887 declarator = declarator->declarator;
5890 if (declarator && declarator->kind == cdk_array)
5892 *nelts = declarator->u.array.bounds;
5893 if (*nelts == error_mark_node)
5894 *nelts = integer_one_node;
5896 if (outer_declarator)
5897 outer_declarator->declarator = declarator->declarator;
5898 else
5899 new_declarator = NULL;
5902 type = groktypename (&type_specifier_seq, new_declarator, false);
5903 return type;
5906 /* Parse an (optional) new-declarator.
5908 new-declarator:
5909 ptr-operator new-declarator [opt]
5910 direct-new-declarator
5912 Returns the declarator. */
5914 static cp_declarator *
5915 cp_parser_new_declarator_opt (cp_parser* parser)
5917 enum tree_code code;
5918 tree type;
5919 cp_cv_quals cv_quals;
5921 /* We don't know if there's a ptr-operator next, or not. */
5922 cp_parser_parse_tentatively (parser);
5923 /* Look for a ptr-operator. */
5924 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5925 /* If that worked, look for more new-declarators. */
5926 if (cp_parser_parse_definitely (parser))
5928 cp_declarator *declarator;
5930 /* Parse another optional declarator. */
5931 declarator = cp_parser_new_declarator_opt (parser);
5933 return cp_parser_make_indirect_declarator
5934 (code, type, cv_quals, declarator);
5937 /* If the next token is a `[', there is a direct-new-declarator. */
5938 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5939 return cp_parser_direct_new_declarator (parser);
5941 return NULL;
5944 /* Parse a direct-new-declarator.
5946 direct-new-declarator:
5947 [ expression ]
5948 direct-new-declarator [constant-expression]
5952 static cp_declarator *
5953 cp_parser_direct_new_declarator (cp_parser* parser)
5955 cp_declarator *declarator = NULL;
5957 while (true)
5959 tree expression;
5961 /* Look for the opening `['. */
5962 cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5963 /* The first expression is not required to be constant. */
5964 if (!declarator)
5966 cp_token *token = cp_lexer_peek_token (parser->lexer);
5967 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5968 /* The standard requires that the expression have integral
5969 type. DR 74 adds enumeration types. We believe that the
5970 real intent is that these expressions be handled like the
5971 expression in a `switch' condition, which also allows
5972 classes with a single conversion to integral or
5973 enumeration type. */
5974 if (!processing_template_decl)
5976 expression
5977 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5978 expression,
5979 /*complain=*/true);
5980 if (!expression)
5982 error_at (token->location,
5983 "expression in new-declarator must have integral "
5984 "or enumeration type");
5985 expression = error_mark_node;
5989 /* But all the other expressions must be. */
5990 else
5991 expression
5992 = cp_parser_constant_expression (parser,
5993 /*allow_non_constant=*/false,
5994 NULL);
5995 /* Look for the closing `]'. */
5996 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5998 /* Add this bound to the declarator. */
5999 declarator = make_array_declarator (declarator, expression);
6001 /* If the next token is not a `[', then there are no more
6002 bounds. */
6003 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
6004 break;
6007 return declarator;
6010 /* Parse a new-initializer.
6012 new-initializer:
6013 ( expression-list [opt] )
6014 braced-init-list
6016 Returns a representation of the expression-list. */
6018 static VEC(tree,gc) *
6019 cp_parser_new_initializer (cp_parser* parser)
6021 VEC(tree,gc) *expression_list;
6023 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6025 tree t;
6026 bool expr_non_constant_p;
6027 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6028 t = cp_parser_braced_list (parser, &expr_non_constant_p);
6029 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6030 expression_list = make_tree_vector_single (t);
6032 else
6033 expression_list = (cp_parser_parenthesized_expression_list
6034 (parser, non_attr, /*cast_p=*/false,
6035 /*allow_expansion_p=*/true,
6036 /*non_constant_p=*/NULL));
6038 return expression_list;
6041 /* Parse a delete-expression.
6043 delete-expression:
6044 :: [opt] delete cast-expression
6045 :: [opt] delete [ ] cast-expression
6047 Returns a representation of the expression. */
6049 static tree
6050 cp_parser_delete_expression (cp_parser* parser)
6052 bool global_scope_p;
6053 bool array_p;
6054 tree expression;
6056 /* Look for the optional `::' operator. */
6057 global_scope_p
6058 = (cp_parser_global_scope_opt (parser,
6059 /*current_scope_valid_p=*/false)
6060 != NULL_TREE);
6061 /* Look for the `delete' keyword. */
6062 cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
6063 /* See if the array syntax is in use. */
6064 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6066 /* Consume the `[' token. */
6067 cp_lexer_consume_token (parser->lexer);
6068 /* Look for the `]' token. */
6069 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
6070 /* Remember that this is the `[]' construct. */
6071 array_p = true;
6073 else
6074 array_p = false;
6076 /* Parse the cast-expression. */
6077 expression = cp_parser_simple_cast_expression (parser);
6079 /* A delete-expression may not appear in an integral constant
6080 expression. */
6081 if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
6082 return error_mark_node;
6084 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
6087 /* Returns true if TOKEN may start a cast-expression and false
6088 otherwise. */
6090 static bool
6091 cp_parser_token_starts_cast_expression (cp_token *token)
6093 switch (token->type)
6095 case CPP_COMMA:
6096 case CPP_SEMICOLON:
6097 case CPP_QUERY:
6098 case CPP_COLON:
6099 case CPP_CLOSE_SQUARE:
6100 case CPP_CLOSE_PAREN:
6101 case CPP_CLOSE_BRACE:
6102 case CPP_DOT:
6103 case CPP_DOT_STAR:
6104 case CPP_DEREF:
6105 case CPP_DEREF_STAR:
6106 case CPP_DIV:
6107 case CPP_MOD:
6108 case CPP_LSHIFT:
6109 case CPP_RSHIFT:
6110 case CPP_LESS:
6111 case CPP_GREATER:
6112 case CPP_LESS_EQ:
6113 case CPP_GREATER_EQ:
6114 case CPP_EQ_EQ:
6115 case CPP_NOT_EQ:
6116 case CPP_EQ:
6117 case CPP_MULT_EQ:
6118 case CPP_DIV_EQ:
6119 case CPP_MOD_EQ:
6120 case CPP_PLUS_EQ:
6121 case CPP_MINUS_EQ:
6122 case CPP_RSHIFT_EQ:
6123 case CPP_LSHIFT_EQ:
6124 case CPP_AND_EQ:
6125 case CPP_XOR_EQ:
6126 case CPP_OR_EQ:
6127 case CPP_XOR:
6128 case CPP_OR:
6129 case CPP_OR_OR:
6130 case CPP_EOF:
6131 return false;
6133 /* '[' may start a primary-expression in obj-c++. */
6134 case CPP_OPEN_SQUARE:
6135 return c_dialect_objc ();
6137 default:
6138 return true;
6142 /* Parse a cast-expression.
6144 cast-expression:
6145 unary-expression
6146 ( type-id ) cast-expression
6148 ADDRESS_P is true iff the unary-expression is appearing as the
6149 operand of the `&' operator. CAST_P is true if this expression is
6150 the target of a cast.
6152 Returns a representation of the expression. */
6154 static tree
6155 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6156 cp_id_kind * pidk)
6158 /* If it's a `(', then we might be looking at a cast. */
6159 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6161 tree type = NULL_TREE;
6162 tree expr = NULL_TREE;
6163 bool compound_literal_p;
6164 const char *saved_message;
6166 /* There's no way to know yet whether or not this is a cast.
6167 For example, `(int (3))' is a unary-expression, while `(int)
6168 3' is a cast. So, we resort to parsing tentatively. */
6169 cp_parser_parse_tentatively (parser);
6170 /* Types may not be defined in a cast. */
6171 saved_message = parser->type_definition_forbidden_message;
6172 parser->type_definition_forbidden_message
6173 = G_("types may not be defined in casts");
6174 /* Consume the `('. */
6175 cp_lexer_consume_token (parser->lexer);
6176 /* A very tricky bit is that `(struct S) { 3 }' is a
6177 compound-literal (which we permit in C++ as an extension).
6178 But, that construct is not a cast-expression -- it is a
6179 postfix-expression. (The reason is that `(struct S) { 3 }.i'
6180 is legal; if the compound-literal were a cast-expression,
6181 you'd need an extra set of parentheses.) But, if we parse
6182 the type-id, and it happens to be a class-specifier, then we
6183 will commit to the parse at that point, because we cannot
6184 undo the action that is done when creating a new class. So,
6185 then we cannot back up and do a postfix-expression.
6187 Therefore, we scan ahead to the closing `)', and check to see
6188 if the token after the `)' is a `{'. If so, we are not
6189 looking at a cast-expression.
6191 Save tokens so that we can put them back. */
6192 cp_lexer_save_tokens (parser->lexer);
6193 /* Skip tokens until the next token is a closing parenthesis.
6194 If we find the closing `)', and the next token is a `{', then
6195 we are looking at a compound-literal. */
6196 compound_literal_p
6197 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6198 /*consume_paren=*/true)
6199 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6200 /* Roll back the tokens we skipped. */
6201 cp_lexer_rollback_tokens (parser->lexer);
6202 /* If we were looking at a compound-literal, simulate an error
6203 so that the call to cp_parser_parse_definitely below will
6204 fail. */
6205 if (compound_literal_p)
6206 cp_parser_simulate_error (parser);
6207 else
6209 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6210 parser->in_type_id_in_expr_p = true;
6211 /* Look for the type-id. */
6212 type = cp_parser_type_id (parser);
6213 /* Look for the closing `)'. */
6214 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6215 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6218 /* Restore the saved message. */
6219 parser->type_definition_forbidden_message = saved_message;
6221 /* At this point this can only be either a cast or a
6222 parenthesized ctor such as `(T ())' that looks like a cast to
6223 function returning T. */
6224 if (!cp_parser_error_occurred (parser)
6225 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6226 (parser->lexer)))
6228 cp_parser_parse_definitely (parser);
6229 expr = cp_parser_cast_expression (parser,
6230 /*address_p=*/false,
6231 /*cast_p=*/true, pidk);
6233 /* Warn about old-style casts, if so requested. */
6234 if (warn_old_style_cast
6235 && !in_system_header
6236 && !VOID_TYPE_P (type)
6237 && current_lang_name != lang_name_c)
6238 warning (OPT_Wold_style_cast, "use of old-style cast");
6240 /* Only type conversions to integral or enumeration types
6241 can be used in constant-expressions. */
6242 if (!cast_valid_in_integral_constant_expression_p (type)
6243 && (cp_parser_non_integral_constant_expression
6244 (parser,
6245 "a cast to a type other than an integral or "
6246 "enumeration type")))
6247 return error_mark_node;
6249 /* Perform the cast. */
6250 expr = build_c_cast (input_location, type, expr);
6251 return expr;
6253 else
6254 cp_parser_abort_tentative_parse (parser);
6257 /* If we get here, then it's not a cast, so it must be a
6258 unary-expression. */
6259 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6262 /* Parse a binary expression of the general form:
6264 pm-expression:
6265 cast-expression
6266 pm-expression .* cast-expression
6267 pm-expression ->* cast-expression
6269 multiplicative-expression:
6270 pm-expression
6271 multiplicative-expression * pm-expression
6272 multiplicative-expression / pm-expression
6273 multiplicative-expression % pm-expression
6275 additive-expression:
6276 multiplicative-expression
6277 additive-expression + multiplicative-expression
6278 additive-expression - multiplicative-expression
6280 shift-expression:
6281 additive-expression
6282 shift-expression << additive-expression
6283 shift-expression >> additive-expression
6285 relational-expression:
6286 shift-expression
6287 relational-expression < shift-expression
6288 relational-expression > shift-expression
6289 relational-expression <= shift-expression
6290 relational-expression >= shift-expression
6292 GNU Extension:
6294 relational-expression:
6295 relational-expression <? shift-expression
6296 relational-expression >? shift-expression
6298 equality-expression:
6299 relational-expression
6300 equality-expression == relational-expression
6301 equality-expression != relational-expression
6303 and-expression:
6304 equality-expression
6305 and-expression & equality-expression
6307 exclusive-or-expression:
6308 and-expression
6309 exclusive-or-expression ^ and-expression
6311 inclusive-or-expression:
6312 exclusive-or-expression
6313 inclusive-or-expression | exclusive-or-expression
6315 logical-and-expression:
6316 inclusive-or-expression
6317 logical-and-expression && inclusive-or-expression
6319 logical-or-expression:
6320 logical-and-expression
6321 logical-or-expression || logical-and-expression
6323 All these are implemented with a single function like:
6325 binary-expression:
6326 simple-cast-expression
6327 binary-expression <token> binary-expression
6329 CAST_P is true if this expression is the target of a cast.
6331 The binops_by_token map is used to get the tree codes for each <token> type.
6332 binary-expressions are associated according to a precedence table. */
6334 #define TOKEN_PRECEDENCE(token) \
6335 (((token->type == CPP_GREATER \
6336 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6337 && !parser->greater_than_is_operator_p) \
6338 ? PREC_NOT_OPERATOR \
6339 : binops_by_token[token->type].prec)
6341 static tree
6342 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6343 bool no_toplevel_fold_p,
6344 enum cp_parser_prec prec,
6345 cp_id_kind * pidk)
6347 cp_parser_expression_stack stack;
6348 cp_parser_expression_stack_entry *sp = &stack[0];
6349 tree lhs, rhs;
6350 cp_token *token;
6351 enum tree_code tree_type, lhs_type, rhs_type;
6352 enum cp_parser_prec new_prec, lookahead_prec;
6353 bool overloaded_p;
6355 /* Parse the first expression. */
6356 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6357 lhs_type = ERROR_MARK;
6359 for (;;)
6361 /* Get an operator token. */
6362 token = cp_lexer_peek_token (parser->lexer);
6364 if (warn_cxx0x_compat
6365 && token->type == CPP_RSHIFT
6366 && !parser->greater_than_is_operator_p)
6368 if (warning_at (token->location, OPT_Wc__0x_compat,
6369 "%<>>%> operator will be treated as"
6370 " two right angle brackets in C++0x"))
6371 inform (token->location,
6372 "suggest parentheses around %<>>%> expression");
6375 new_prec = TOKEN_PRECEDENCE (token);
6377 /* Popping an entry off the stack means we completed a subexpression:
6378 - either we found a token which is not an operator (`>' where it is not
6379 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6380 will happen repeatedly;
6381 - or, we found an operator which has lower priority. This is the case
6382 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6383 parsing `3 * 4'. */
6384 if (new_prec <= prec)
6386 if (sp == stack)
6387 break;
6388 else
6389 goto pop;
6392 get_rhs:
6393 tree_type = binops_by_token[token->type].tree_type;
6395 /* We used the operator token. */
6396 cp_lexer_consume_token (parser->lexer);
6398 /* For "false && x" or "true || x", x will never be executed;
6399 disable warnings while evaluating it. */
6400 if (tree_type == TRUTH_ANDIF_EXPR)
6401 c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6402 else if (tree_type == TRUTH_ORIF_EXPR)
6403 c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6405 /* Extract another operand. It may be the RHS of this expression
6406 or the LHS of a new, higher priority expression. */
6407 rhs = cp_parser_simple_cast_expression (parser);
6408 rhs_type = ERROR_MARK;
6410 /* Get another operator token. Look up its precedence to avoid
6411 building a useless (immediately popped) stack entry for common
6412 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6413 token = cp_lexer_peek_token (parser->lexer);
6414 lookahead_prec = TOKEN_PRECEDENCE (token);
6415 if (lookahead_prec > new_prec)
6417 /* ... and prepare to parse the RHS of the new, higher priority
6418 expression. Since precedence levels on the stack are
6419 monotonically increasing, we do not have to care about
6420 stack overflows. */
6421 sp->prec = prec;
6422 sp->tree_type = tree_type;
6423 sp->lhs = lhs;
6424 sp->lhs_type = lhs_type;
6425 sp++;
6426 lhs = rhs;
6427 lhs_type = rhs_type;
6428 prec = new_prec;
6429 new_prec = lookahead_prec;
6430 goto get_rhs;
6432 pop:
6433 lookahead_prec = new_prec;
6434 /* If the stack is not empty, we have parsed into LHS the right side
6435 (`4' in the example above) of an expression we had suspended.
6436 We can use the information on the stack to recover the LHS (`3')
6437 from the stack together with the tree code (`MULT_EXPR'), and
6438 the precedence of the higher level subexpression
6439 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6440 which will be used to actually build the additive expression. */
6441 --sp;
6442 prec = sp->prec;
6443 tree_type = sp->tree_type;
6444 rhs = lhs;
6445 rhs_type = lhs_type;
6446 lhs = sp->lhs;
6447 lhs_type = sp->lhs_type;
6450 /* Undo the disabling of warnings done above. */
6451 if (tree_type == TRUTH_ANDIF_EXPR)
6452 c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6453 else if (tree_type == TRUTH_ORIF_EXPR)
6454 c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6456 overloaded_p = false;
6457 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6458 ERROR_MARK for everything that is not a binary expression.
6459 This makes warn_about_parentheses miss some warnings that
6460 involve unary operators. For unary expressions we should
6461 pass the correct tree_code unless the unary expression was
6462 surrounded by parentheses.
6464 if (no_toplevel_fold_p
6465 && lookahead_prec <= prec
6466 && sp == stack
6467 && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6468 lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6469 else
6470 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6471 &overloaded_p, tf_warning_or_error);
6472 lhs_type = tree_type;
6474 /* If the binary operator required the use of an overloaded operator,
6475 then this expression cannot be an integral constant-expression.
6476 An overloaded operator can be used even if both operands are
6477 otherwise permissible in an integral constant-expression if at
6478 least one of the operands is of enumeration type. */
6480 if (overloaded_p
6481 && (cp_parser_non_integral_constant_expression
6482 (parser, "calls to overloaded operators")))
6483 return error_mark_node;
6486 return lhs;
6490 /* Parse the `? expression : assignment-expression' part of a
6491 conditional-expression. The LOGICAL_OR_EXPR is the
6492 logical-or-expression that started the conditional-expression.
6493 Returns a representation of the entire conditional-expression.
6495 This routine is used by cp_parser_assignment_expression.
6497 ? expression : assignment-expression
6499 GNU Extensions:
6501 ? : assignment-expression */
6503 static tree
6504 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6506 tree expr;
6507 tree assignment_expr;
6509 /* Consume the `?' token. */
6510 cp_lexer_consume_token (parser->lexer);
6511 if (cp_parser_allow_gnu_extensions_p (parser)
6512 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6514 /* Implicit true clause. */
6515 expr = NULL_TREE;
6516 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6518 else
6520 /* Parse the expression. */
6521 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6522 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6523 c_inhibit_evaluation_warnings +=
6524 ((logical_or_expr == truthvalue_true_node)
6525 - (logical_or_expr == truthvalue_false_node));
6528 /* The next token should be a `:'. */
6529 cp_parser_require (parser, CPP_COLON, "%<:%>");
6530 /* Parse the assignment-expression. */
6531 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6532 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6534 /* Build the conditional-expression. */
6535 return build_x_conditional_expr (logical_or_expr,
6536 expr,
6537 assignment_expr,
6538 tf_warning_or_error);
6541 /* Parse an assignment-expression.
6543 assignment-expression:
6544 conditional-expression
6545 logical-or-expression assignment-operator assignment_expression
6546 throw-expression
6548 CAST_P is true if this expression is the target of a cast.
6550 Returns a representation for the expression. */
6552 static tree
6553 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6554 cp_id_kind * pidk)
6556 tree expr;
6558 /* If the next token is the `throw' keyword, then we're looking at
6559 a throw-expression. */
6560 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6561 expr = cp_parser_throw_expression (parser);
6562 /* Otherwise, it must be that we are looking at a
6563 logical-or-expression. */
6564 else
6566 /* Parse the binary expressions (logical-or-expression). */
6567 expr = cp_parser_binary_expression (parser, cast_p, false,
6568 PREC_NOT_OPERATOR, pidk);
6569 /* If the next token is a `?' then we're actually looking at a
6570 conditional-expression. */
6571 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6572 return cp_parser_question_colon_clause (parser, expr);
6573 else
6575 enum tree_code assignment_operator;
6577 /* If it's an assignment-operator, we're using the second
6578 production. */
6579 assignment_operator
6580 = cp_parser_assignment_operator_opt (parser);
6581 if (assignment_operator != ERROR_MARK)
6583 bool non_constant_p;
6585 /* Parse the right-hand side of the assignment. */
6586 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6588 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6589 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6591 /* An assignment may not appear in a
6592 constant-expression. */
6593 if (cp_parser_non_integral_constant_expression (parser,
6594 "an assignment"))
6595 return error_mark_node;
6596 /* Build the assignment expression. */
6597 expr = build_x_modify_expr (expr,
6598 assignment_operator,
6599 rhs,
6600 tf_warning_or_error);
6605 return expr;
6608 /* Parse an (optional) assignment-operator.
6610 assignment-operator: one of
6611 = *= /= %= += -= >>= <<= &= ^= |=
6613 GNU Extension:
6615 assignment-operator: one of
6616 <?= >?=
6618 If the next token is an assignment operator, the corresponding tree
6619 code is returned, and the token is consumed. For example, for
6620 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6621 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6622 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6623 operator, ERROR_MARK is returned. */
6625 static enum tree_code
6626 cp_parser_assignment_operator_opt (cp_parser* parser)
6628 enum tree_code op;
6629 cp_token *token;
6631 /* Peek at the next token. */
6632 token = cp_lexer_peek_token (parser->lexer);
6634 switch (token->type)
6636 case CPP_EQ:
6637 op = NOP_EXPR;
6638 break;
6640 case CPP_MULT_EQ:
6641 op = MULT_EXPR;
6642 break;
6644 case CPP_DIV_EQ:
6645 op = TRUNC_DIV_EXPR;
6646 break;
6648 case CPP_MOD_EQ:
6649 op = TRUNC_MOD_EXPR;
6650 break;
6652 case CPP_PLUS_EQ:
6653 op = PLUS_EXPR;
6654 break;
6656 case CPP_MINUS_EQ:
6657 op = MINUS_EXPR;
6658 break;
6660 case CPP_RSHIFT_EQ:
6661 op = RSHIFT_EXPR;
6662 break;
6664 case CPP_LSHIFT_EQ:
6665 op = LSHIFT_EXPR;
6666 break;
6668 case CPP_AND_EQ:
6669 op = BIT_AND_EXPR;
6670 break;
6672 case CPP_XOR_EQ:
6673 op = BIT_XOR_EXPR;
6674 break;
6676 case CPP_OR_EQ:
6677 op = BIT_IOR_EXPR;
6678 break;
6680 default:
6681 /* Nothing else is an assignment operator. */
6682 op = ERROR_MARK;
6685 /* If it was an assignment operator, consume it. */
6686 if (op != ERROR_MARK)
6687 cp_lexer_consume_token (parser->lexer);
6689 return op;
6692 /* Parse an expression.
6694 expression:
6695 assignment-expression
6696 expression , assignment-expression
6698 CAST_P is true if this expression is the target of a cast.
6700 Returns a representation of the expression. */
6702 static tree
6703 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6705 tree expression = NULL_TREE;
6707 while (true)
6709 tree assignment_expression;
6711 /* Parse the next assignment-expression. */
6712 assignment_expression
6713 = cp_parser_assignment_expression (parser, cast_p, pidk);
6714 /* If this is the first assignment-expression, we can just
6715 save it away. */
6716 if (!expression)
6717 expression = assignment_expression;
6718 else
6719 expression = build_x_compound_expr (expression,
6720 assignment_expression,
6721 tf_warning_or_error);
6722 /* If the next token is not a comma, then we are done with the
6723 expression. */
6724 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6725 break;
6726 /* Consume the `,'. */
6727 cp_lexer_consume_token (parser->lexer);
6728 /* A comma operator cannot appear in a constant-expression. */
6729 if (cp_parser_non_integral_constant_expression (parser,
6730 "a comma operator"))
6731 expression = error_mark_node;
6734 return expression;
6737 /* Parse a constant-expression.
6739 constant-expression:
6740 conditional-expression
6742 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6743 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6744 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6745 is false, NON_CONSTANT_P should be NULL. */
6747 static tree
6748 cp_parser_constant_expression (cp_parser* parser,
6749 bool allow_non_constant_p,
6750 bool *non_constant_p)
6752 bool saved_integral_constant_expression_p;
6753 bool saved_allow_non_integral_constant_expression_p;
6754 bool saved_non_integral_constant_expression_p;
6755 tree expression;
6757 /* It might seem that we could simply parse the
6758 conditional-expression, and then check to see if it were
6759 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6760 one that the compiler can figure out is constant, possibly after
6761 doing some simplifications or optimizations. The standard has a
6762 precise definition of constant-expression, and we must honor
6763 that, even though it is somewhat more restrictive.
6765 For example:
6767 int i[(2, 3)];
6769 is not a legal declaration, because `(2, 3)' is not a
6770 constant-expression. The `,' operator is forbidden in a
6771 constant-expression. However, GCC's constant-folding machinery
6772 will fold this operation to an INTEGER_CST for `3'. */
6774 /* Save the old settings. */
6775 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6776 saved_allow_non_integral_constant_expression_p
6777 = parser->allow_non_integral_constant_expression_p;
6778 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6779 /* We are now parsing a constant-expression. */
6780 parser->integral_constant_expression_p = true;
6781 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6782 parser->non_integral_constant_expression_p = false;
6783 /* Although the grammar says "conditional-expression", we parse an
6784 "assignment-expression", which also permits "throw-expression"
6785 and the use of assignment operators. In the case that
6786 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6787 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6788 actually essential that we look for an assignment-expression.
6789 For example, cp_parser_initializer_clauses uses this function to
6790 determine whether a particular assignment-expression is in fact
6791 constant. */
6792 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6793 /* Restore the old settings. */
6794 parser->integral_constant_expression_p
6795 = saved_integral_constant_expression_p;
6796 parser->allow_non_integral_constant_expression_p
6797 = saved_allow_non_integral_constant_expression_p;
6798 if (allow_non_constant_p)
6799 *non_constant_p = parser->non_integral_constant_expression_p;
6800 else if (parser->non_integral_constant_expression_p)
6801 expression = error_mark_node;
6802 parser->non_integral_constant_expression_p
6803 = saved_non_integral_constant_expression_p;
6805 return expression;
6808 /* Parse __builtin_offsetof.
6810 offsetof-expression:
6811 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6813 offsetof-member-designator:
6814 id-expression
6815 | offsetof-member-designator "." id-expression
6816 | offsetof-member-designator "[" expression "]"
6817 | offsetof-member-designator "->" id-expression */
6819 static tree
6820 cp_parser_builtin_offsetof (cp_parser *parser)
6822 int save_ice_p, save_non_ice_p;
6823 tree type, expr;
6824 cp_id_kind dummy;
6825 cp_token *token;
6827 /* We're about to accept non-integral-constant things, but will
6828 definitely yield an integral constant expression. Save and
6829 restore these values around our local parsing. */
6830 save_ice_p = parser->integral_constant_expression_p;
6831 save_non_ice_p = parser->non_integral_constant_expression_p;
6833 /* Consume the "__builtin_offsetof" token. */
6834 cp_lexer_consume_token (parser->lexer);
6835 /* Consume the opening `('. */
6836 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6837 /* Parse the type-id. */
6838 type = cp_parser_type_id (parser);
6839 /* Look for the `,'. */
6840 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6841 token = cp_lexer_peek_token (parser->lexer);
6843 /* Build the (type *)null that begins the traditional offsetof macro. */
6844 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6845 tf_warning_or_error);
6847 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6848 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6849 true, &dummy, token->location);
6850 while (true)
6852 token = cp_lexer_peek_token (parser->lexer);
6853 switch (token->type)
6855 case CPP_OPEN_SQUARE:
6856 /* offsetof-member-designator "[" expression "]" */
6857 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6858 break;
6860 case CPP_DEREF:
6861 /* offsetof-member-designator "->" identifier */
6862 expr = grok_array_decl (expr, integer_zero_node);
6863 /* FALLTHRU */
6865 case CPP_DOT:
6866 /* offsetof-member-designator "." identifier */
6867 cp_lexer_consume_token (parser->lexer);
6868 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
6869 expr, true, &dummy,
6870 token->location);
6871 break;
6873 case CPP_CLOSE_PAREN:
6874 /* Consume the ")" token. */
6875 cp_lexer_consume_token (parser->lexer);
6876 goto success;
6878 default:
6879 /* Error. We know the following require will fail, but
6880 that gives the proper error message. */
6881 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6882 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6883 expr = error_mark_node;
6884 goto failure;
6888 success:
6889 /* If we're processing a template, we can't finish the semantics yet.
6890 Otherwise we can fold the entire expression now. */
6891 if (processing_template_decl)
6892 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6893 else
6894 expr = finish_offsetof (expr);
6896 failure:
6897 parser->integral_constant_expression_p = save_ice_p;
6898 parser->non_integral_constant_expression_p = save_non_ice_p;
6900 return expr;
6903 /* Parse a trait expression. */
6905 static tree
6906 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6908 cp_trait_kind kind;
6909 tree type1, type2 = NULL_TREE;
6910 bool binary = false;
6911 cp_decl_specifier_seq decl_specs;
6913 switch (keyword)
6915 case RID_HAS_NOTHROW_ASSIGN:
6916 kind = CPTK_HAS_NOTHROW_ASSIGN;
6917 break;
6918 case RID_HAS_NOTHROW_CONSTRUCTOR:
6919 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6920 break;
6921 case RID_HAS_NOTHROW_COPY:
6922 kind = CPTK_HAS_NOTHROW_COPY;
6923 break;
6924 case RID_HAS_TRIVIAL_ASSIGN:
6925 kind = CPTK_HAS_TRIVIAL_ASSIGN;
6926 break;
6927 case RID_HAS_TRIVIAL_CONSTRUCTOR:
6928 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6929 break;
6930 case RID_HAS_TRIVIAL_COPY:
6931 kind = CPTK_HAS_TRIVIAL_COPY;
6932 break;
6933 case RID_HAS_TRIVIAL_DESTRUCTOR:
6934 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6935 break;
6936 case RID_HAS_VIRTUAL_DESTRUCTOR:
6937 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6938 break;
6939 case RID_IS_ABSTRACT:
6940 kind = CPTK_IS_ABSTRACT;
6941 break;
6942 case RID_IS_BASE_OF:
6943 kind = CPTK_IS_BASE_OF;
6944 binary = true;
6945 break;
6946 case RID_IS_CLASS:
6947 kind = CPTK_IS_CLASS;
6948 break;
6949 case RID_IS_CONVERTIBLE_TO:
6950 kind = CPTK_IS_CONVERTIBLE_TO;
6951 binary = true;
6952 break;
6953 case RID_IS_EMPTY:
6954 kind = CPTK_IS_EMPTY;
6955 break;
6956 case RID_IS_ENUM:
6957 kind = CPTK_IS_ENUM;
6958 break;
6959 case RID_IS_POD:
6960 kind = CPTK_IS_POD;
6961 break;
6962 case RID_IS_POLYMORPHIC:
6963 kind = CPTK_IS_POLYMORPHIC;
6964 break;
6965 case RID_IS_STD_LAYOUT:
6966 kind = CPTK_IS_STD_LAYOUT;
6967 break;
6968 case RID_IS_TRIVIAL:
6969 kind = CPTK_IS_TRIVIAL;
6970 break;
6971 case RID_IS_UNION:
6972 kind = CPTK_IS_UNION;
6973 break;
6974 default:
6975 gcc_unreachable ();
6978 /* Consume the token. */
6979 cp_lexer_consume_token (parser->lexer);
6981 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6983 type1 = cp_parser_type_id (parser);
6985 if (type1 == error_mark_node)
6986 return error_mark_node;
6988 /* Build a trivial decl-specifier-seq. */
6989 clear_decl_specs (&decl_specs);
6990 decl_specs.type = type1;
6992 /* Call grokdeclarator to figure out what type this is. */
6993 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6994 /*initialized=*/0, /*attrlist=*/NULL);
6996 if (binary)
6998 cp_parser_require (parser, CPP_COMMA, "%<,%>");
7000 type2 = cp_parser_type_id (parser);
7002 if (type2 == error_mark_node)
7003 return error_mark_node;
7005 /* Build a trivial decl-specifier-seq. */
7006 clear_decl_specs (&decl_specs);
7007 decl_specs.type = type2;
7009 /* Call grokdeclarator to figure out what type this is. */
7010 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7011 /*initialized=*/0, /*attrlist=*/NULL);
7014 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7016 /* Complete the trait expression, which may mean either processing
7017 the trait expr now or saving it for template instantiation. */
7018 return finish_trait_expr (kind, type1, type2);
7021 /* Lambdas that appear in variable initializer or default argument scope
7022 get that in their mangling, so we need to record it. We might as well
7023 use the count for function and namespace scopes as well. */
7024 static GTY(()) tree lambda_scope;
7025 static GTY(()) int lambda_count;
7026 typedef struct GTY(()) tree_int
7028 tree t;
7029 int i;
7030 } tree_int;
7031 DEF_VEC_O(tree_int);
7032 DEF_VEC_ALLOC_O(tree_int,gc);
7033 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7035 static void
7036 start_lambda_scope (tree decl)
7038 tree_int ti;
7039 gcc_assert (decl);
7040 /* Once we're inside a function, we ignore other scopes and just push
7041 the function again so that popping works properly. */
7042 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7043 decl = current_function_decl;
7044 ti.t = lambda_scope;
7045 ti.i = lambda_count;
7046 VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7047 if (lambda_scope != decl)
7049 /* Don't reset the count if we're still in the same function. */
7050 lambda_scope = decl;
7051 lambda_count = 0;
7055 static void
7056 record_lambda_scope (tree lambda)
7058 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7059 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7062 static void
7063 finish_lambda_scope (void)
7065 tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7066 if (lambda_scope != p->t)
7068 lambda_scope = p->t;
7069 lambda_count = p->i;
7071 VEC_pop (tree_int, lambda_scope_stack);
7074 /* Parse a lambda expression.
7076 lambda-expression:
7077 lambda-introducer lambda-declarator [opt] compound-statement
7079 Returns a representation of the expression. */
7081 static tree
7082 cp_parser_lambda_expression (cp_parser* parser)
7084 tree lambda_expr = build_lambda_expr ();
7085 tree type;
7087 LAMBDA_EXPR_LOCATION (lambda_expr)
7088 = cp_lexer_peek_token (parser->lexer)->location;
7090 if (cp_unevaluated_operand)
7091 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
7092 "lambda-expression in unevaluated context");
7094 /* We may be in the middle of deferred access check. Disable
7095 it now. */
7096 push_deferring_access_checks (dk_no_deferred);
7098 cp_parser_lambda_introducer (parser, lambda_expr);
7100 type = begin_lambda_type (lambda_expr);
7102 record_lambda_scope (lambda_expr);
7104 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
7105 determine_visibility (TYPE_NAME (type));
7107 /* Now that we've started the type, add the capture fields for any
7108 explicit captures. */
7109 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
7112 /* Inside the class, surrounding template-parameter-lists do not apply. */
7113 unsigned int saved_num_template_parameter_lists
7114 = parser->num_template_parameter_lists;
7116 parser->num_template_parameter_lists = 0;
7118 /* By virtue of defining a local class, a lambda expression has access to
7119 the private variables of enclosing classes. */
7121 cp_parser_lambda_declarator_opt (parser, lambda_expr);
7123 cp_parser_lambda_body (parser, lambda_expr);
7125 /* The capture list was built up in reverse order; fix that now. */
7127 tree newlist = NULL_TREE;
7128 tree elt, next;
7130 for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7131 elt; elt = next)
7133 tree field = TREE_PURPOSE (elt);
7134 char *buf;
7136 next = TREE_CHAIN (elt);
7137 TREE_CHAIN (elt) = newlist;
7138 newlist = elt;
7140 /* Also add __ to the beginning of the field name so that code
7141 outside the lambda body can't see the captured name. We could
7142 just remove the name entirely, but this is more useful for
7143 debugging. */
7144 if (field == LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
7145 /* The 'this' capture already starts with __. */
7146 continue;
7148 buf = (char *) alloca (IDENTIFIER_LENGTH (DECL_NAME (field)) + 3);
7149 buf[1] = buf[0] = '_';
7150 memcpy (buf + 2, IDENTIFIER_POINTER (DECL_NAME (field)),
7151 IDENTIFIER_LENGTH (DECL_NAME (field)) + 1);
7152 DECL_NAME (field) = get_identifier (buf);
7154 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7157 maybe_add_lambda_conv_op (type);
7159 type = finish_struct (type, /*attributes=*/NULL_TREE);
7161 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7164 pop_deferring_access_checks ();
7166 return build_lambda_object (lambda_expr);
7169 /* Parse the beginning of a lambda expression.
7171 lambda-introducer:
7172 [ lambda-capture [opt] ]
7174 LAMBDA_EXPR is the current representation of the lambda expression. */
7176 static void
7177 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7179 /* Need commas after the first capture. */
7180 bool first = true;
7182 /* Eat the leading `['. */
7183 cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
7185 /* Record default capture mode. "[&" "[=" "[&," "[=," */
7186 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7187 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7188 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7189 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7190 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7192 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7194 cp_lexer_consume_token (parser->lexer);
7195 first = false;
7198 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7200 cp_token* capture_token;
7201 tree capture_id;
7202 tree capture_init_expr;
7203 cp_id_kind idk = CP_ID_KIND_NONE;
7204 bool explicit_init_p = false;
7206 enum capture_kind_type
7208 BY_COPY,
7209 BY_REFERENCE
7211 enum capture_kind_type capture_kind = BY_COPY;
7213 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7215 error ("expected end of capture-list");
7216 return;
7219 if (first)
7220 first = false;
7221 else
7222 cp_parser_require (parser, CPP_COMMA, "%<,%>");
7224 /* Possibly capture `this'. */
7225 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7227 cp_lexer_consume_token (parser->lexer);
7228 add_capture (lambda_expr,
7229 /*id=*/get_identifier ("__this"),
7230 /*initializer=*/finish_this_expr(),
7231 /*by_reference_p=*/false,
7232 explicit_init_p);
7233 continue;
7236 /* Remember whether we want to capture as a reference or not. */
7237 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7239 capture_kind = BY_REFERENCE;
7240 cp_lexer_consume_token (parser->lexer);
7243 /* Get the identifier. */
7244 capture_token = cp_lexer_peek_token (parser->lexer);
7245 capture_id = cp_parser_identifier (parser);
7247 if (capture_id == error_mark_node)
7248 /* Would be nice to have a cp_parser_skip_to_closing_x for general
7249 delimiters, but I modified this to stop on unnested ']' as well. It
7250 was already changed to stop on unnested '}', so the
7251 "closing_parenthesis" name is no more misleading with my change. */
7253 cp_parser_skip_to_closing_parenthesis (parser,
7254 /*recovering=*/true,
7255 /*or_comma=*/true,
7256 /*consume_paren=*/true);
7257 break;
7260 /* Find the initializer for this capture. */
7261 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7263 /* An explicit expression exists. */
7264 cp_lexer_consume_token (parser->lexer);
7265 pedwarn (input_location, OPT_pedantic,
7266 "ISO C++ does not allow initializers "
7267 "in lambda expression capture lists");
7268 capture_init_expr = cp_parser_assignment_expression (parser,
7269 /*cast_p=*/true,
7270 &idk);
7271 explicit_init_p = true;
7273 else
7275 const char* error_msg;
7277 /* Turn the identifier into an id-expression. */
7278 capture_init_expr
7279 = cp_parser_lookup_name
7280 (parser,
7281 capture_id,
7282 none_type,
7283 /*is_template=*/false,
7284 /*is_namespace=*/false,
7285 /*check_dependency=*/true,
7286 /*ambiguous_decls=*/NULL,
7287 capture_token->location);
7289 capture_init_expr
7290 = finish_id_expression
7291 (capture_id,
7292 capture_init_expr,
7293 parser->scope,
7294 &idk,
7295 /*integral_constant_expression_p=*/false,
7296 /*allow_non_integral_constant_expression_p=*/false,
7297 /*non_integral_constant_expression_p=*/NULL,
7298 /*template_p=*/false,
7299 /*done=*/true,
7300 /*address_p=*/false,
7301 /*template_arg_p=*/false,
7302 &error_msg,
7303 capture_token->location);
7306 if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7307 capture_init_expr
7308 = unqualified_name_lookup_error (capture_init_expr);
7310 add_capture (lambda_expr,
7311 capture_id,
7312 capture_init_expr,
7313 /*by_reference_p=*/capture_kind == BY_REFERENCE,
7314 explicit_init_p);
7317 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
7320 /* Parse the (optional) middle of a lambda expression.
7322 lambda-declarator:
7323 ( parameter-declaration-clause [opt] )
7324 attribute-specifier [opt]
7325 mutable [opt]
7326 exception-specification [opt]
7327 lambda-return-type-clause [opt]
7329 LAMBDA_EXPR is the current representation of the lambda expression. */
7331 static void
7332 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7334 /* 5.1.1.4 of the standard says:
7335 If a lambda-expression does not include a lambda-declarator, it is as if
7336 the lambda-declarator were ().
7337 This means an empty parameter list, no attributes, and no exception
7338 specification. */
7339 tree param_list = void_list_node;
7340 tree attributes = NULL_TREE;
7341 tree exception_spec = NULL_TREE;
7342 tree t;
7344 /* The lambda-declarator is optional, but must begin with an opening
7345 parenthesis if present. */
7346 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7348 cp_lexer_consume_token (parser->lexer);
7350 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7352 /* Parse parameters. */
7353 param_list = cp_parser_parameter_declaration_clause (parser);
7355 /* Default arguments shall not be specified in the
7356 parameter-declaration-clause of a lambda-declarator. */
7357 for (t = param_list; t; t = TREE_CHAIN (t))
7358 if (TREE_PURPOSE (t))
7359 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7360 "default argument specified for lambda parameter");
7362 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7364 attributes = cp_parser_attributes_opt (parser);
7366 /* Parse optional `mutable' keyword. */
7367 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7369 cp_lexer_consume_token (parser->lexer);
7370 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7373 /* Parse optional exception specification. */
7374 exception_spec = cp_parser_exception_specification_opt (parser);
7376 /* Parse optional trailing return type. */
7377 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7379 cp_lexer_consume_token (parser->lexer);
7380 LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7383 /* The function parameters must be in scope all the way until after the
7384 trailing-return-type in case of decltype. */
7385 for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
7386 pop_binding (DECL_NAME (t), t);
7388 leave_scope ();
7391 /* Create the function call operator.
7393 Messing with declarators like this is no uglier than building up the
7394 FUNCTION_DECL by hand, and this is less likely to get out of sync with
7395 other code. */
7397 cp_decl_specifier_seq return_type_specs;
7398 cp_declarator* declarator;
7399 tree fco;
7400 int quals;
7401 void *p;
7403 clear_decl_specs (&return_type_specs);
7404 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7405 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7406 else
7407 /* Maybe we will deduce the return type later, but we can use void
7408 as a placeholder return type anyways. */
7409 return_type_specs.type = void_type_node;
7411 p = obstack_alloc (&declarator_obstack, 0);
7413 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7414 sfk_none);
7416 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
7417 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
7418 declarator = make_call_declarator (declarator, param_list, quals,
7419 exception_spec,
7420 /*late_return_type=*/NULL_TREE);
7421 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
7423 fco = grokmethod (&return_type_specs,
7424 declarator,
7425 attributes);
7426 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7427 DECL_ARTIFICIAL (fco) = 1;
7429 finish_member_declaration (fco);
7431 obstack_free (&declarator_obstack, p);
7435 /* Parse the body of a lambda expression, which is simply
7437 compound-statement
7439 but which requires special handling.
7440 LAMBDA_EXPR is the current representation of the lambda expression. */
7442 static void
7443 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7445 bool nested = (current_function_decl != NULL_TREE);
7446 if (nested)
7447 push_function_context ();
7449 /* Finish the function call operator
7450 - class_specifier
7451 + late_parsing_for_member
7452 + function_definition_after_declarator
7453 + ctor_initializer_opt_and_function_body */
7455 tree fco = lambda_function (lambda_expr);
7456 tree body;
7457 bool done = false;
7459 /* Let the front end know that we are going to be defining this
7460 function. */
7461 start_preparsed_function (fco,
7462 NULL_TREE,
7463 SF_PRE_PARSED | SF_INCLASS_INLINE);
7465 start_lambda_scope (fco);
7466 body = begin_function_body ();
7468 /* 5.1.1.4 of the standard says:
7469 If a lambda-expression does not include a trailing-return-type, it
7470 is as if the trailing-return-type denotes the following type:
7471 * if the compound-statement is of the form
7472 { return attribute-specifier [opt] expression ; }
7473 the type of the returned expression after lvalue-to-rvalue
7474 conversion (_conv.lval_ 4.1), array-to-pointer conversion
7475 (_conv.array_ 4.2), and function-to-pointer conversion
7476 (_conv.func_ 4.3);
7477 * otherwise, void. */
7479 /* In a lambda that has neither a lambda-return-type-clause
7480 nor a deducible form, errors should be reported for return statements
7481 in the body. Since we used void as the placeholder return type, parsing
7482 the body as usual will give such desired behavior. */
7483 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7484 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
7485 && cp_lexer_peek_nth_token (parser->lexer, 2)->keyword == RID_RETURN
7486 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_SEMICOLON)
7488 tree compound_stmt;
7489 tree expr = NULL_TREE;
7490 cp_id_kind idk = CP_ID_KIND_NONE;
7492 /* Parse tentatively in case there's more after the initial return
7493 statement. */
7494 cp_parser_parse_tentatively (parser);
7496 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7497 cp_parser_require_keyword (parser, RID_RETURN, "%<return%>");
7499 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7501 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7502 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7504 if (cp_parser_parse_definitely (parser))
7506 apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7508 compound_stmt = begin_compound_stmt (0);
7509 /* Will get error here if type not deduced yet. */
7510 finish_return_stmt (expr);
7511 finish_compound_stmt (compound_stmt);
7513 done = true;
7517 if (!done)
7519 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7520 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
7521 /* TODO: does begin_compound_stmt want BCS_FN_BODY?
7522 cp_parser_compound_stmt does not pass it. */
7523 cp_parser_function_body (parser);
7524 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
7527 finish_function_body (body);
7528 finish_lambda_scope ();
7530 /* Finish the function and generate code for it if necessary. */
7531 expand_or_defer_fn (finish_function (/*inline*/2));
7534 if (nested)
7535 pop_function_context();
7538 /* Statements [gram.stmt.stmt] */
7540 /* Parse a statement.
7542 statement:
7543 labeled-statement
7544 expression-statement
7545 compound-statement
7546 selection-statement
7547 iteration-statement
7548 jump-statement
7549 declaration-statement
7550 try-block
7552 IN_COMPOUND is true when the statement is nested inside a
7553 cp_parser_compound_statement; this matters for certain pragmas.
7555 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7556 is a (possibly labeled) if statement which is not enclosed in braces
7557 and has an else clause. This is used to implement -Wparentheses. */
7559 static void
7560 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
7561 bool in_compound, bool *if_p)
7563 tree statement;
7564 cp_token *token;
7565 location_t statement_location;
7567 restart:
7568 if (if_p != NULL)
7569 *if_p = false;
7570 /* There is no statement yet. */
7571 statement = NULL_TREE;
7572 /* Peek at the next token. */
7573 token = cp_lexer_peek_token (parser->lexer);
7574 /* Remember the location of the first token in the statement. */
7575 statement_location = token->location;
7576 /* If this is a keyword, then that will often determine what kind of
7577 statement we have. */
7578 if (token->type == CPP_KEYWORD)
7580 enum rid keyword = token->keyword;
7582 switch (keyword)
7584 case RID_CASE:
7585 case RID_DEFAULT:
7586 /* Looks like a labeled-statement with a case label.
7587 Parse the label, and then use tail recursion to parse
7588 the statement. */
7589 cp_parser_label_for_labeled_statement (parser);
7590 goto restart;
7592 case RID_IF:
7593 case RID_SWITCH:
7594 statement = cp_parser_selection_statement (parser, if_p);
7595 break;
7597 case RID_WHILE:
7598 case RID_DO:
7599 case RID_FOR:
7600 statement = cp_parser_iteration_statement (parser);
7601 break;
7603 case RID_BREAK:
7604 case RID_CONTINUE:
7605 case RID_RETURN:
7606 case RID_GOTO:
7607 statement = cp_parser_jump_statement (parser);
7608 break;
7610 /* Objective-C++ exception-handling constructs. */
7611 case RID_AT_TRY:
7612 case RID_AT_CATCH:
7613 case RID_AT_FINALLY:
7614 case RID_AT_SYNCHRONIZED:
7615 case RID_AT_THROW:
7616 statement = cp_parser_objc_statement (parser);
7617 break;
7619 case RID_TRY:
7620 statement = cp_parser_try_block (parser);
7621 break;
7623 case RID_NAMESPACE:
7624 /* This must be a namespace alias definition. */
7625 cp_parser_declaration_statement (parser);
7626 return;
7628 default:
7629 /* It might be a keyword like `int' that can start a
7630 declaration-statement. */
7631 break;
7634 else if (token->type == CPP_NAME)
7636 /* If the next token is a `:', then we are looking at a
7637 labeled-statement. */
7638 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7639 if (token->type == CPP_COLON)
7641 /* Looks like a labeled-statement with an ordinary label.
7642 Parse the label, and then use tail recursion to parse
7643 the statement. */
7644 cp_parser_label_for_labeled_statement (parser);
7645 goto restart;
7648 /* Anything that starts with a `{' must be a compound-statement. */
7649 else if (token->type == CPP_OPEN_BRACE)
7650 statement = cp_parser_compound_statement (parser, NULL, false);
7651 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
7652 a statement all its own. */
7653 else if (token->type == CPP_PRAGMA)
7655 /* Only certain OpenMP pragmas are attached to statements, and thus
7656 are considered statements themselves. All others are not. In
7657 the context of a compound, accept the pragma as a "statement" and
7658 return so that we can check for a close brace. Otherwise we
7659 require a real statement and must go back and read one. */
7660 if (in_compound)
7661 cp_parser_pragma (parser, pragma_compound);
7662 else if (!cp_parser_pragma (parser, pragma_stmt))
7663 goto restart;
7664 return;
7666 else if (token->type == CPP_EOF)
7668 cp_parser_error (parser, "expected statement");
7669 return;
7672 /* Everything else must be a declaration-statement or an
7673 expression-statement. Try for the declaration-statement
7674 first, unless we are looking at a `;', in which case we know that
7675 we have an expression-statement. */
7676 if (!statement)
7678 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7680 cp_parser_parse_tentatively (parser);
7681 /* Try to parse the declaration-statement. */
7682 cp_parser_declaration_statement (parser);
7683 /* If that worked, we're done. */
7684 if (cp_parser_parse_definitely (parser))
7685 return;
7687 /* Look for an expression-statement instead. */
7688 statement = cp_parser_expression_statement (parser, in_statement_expr);
7691 /* Set the line number for the statement. */
7692 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7693 SET_EXPR_LOCATION (statement, statement_location);
7696 /* Parse the label for a labeled-statement, i.e.
7698 identifier :
7699 case constant-expression :
7700 default :
7702 GNU Extension:
7703 case constant-expression ... constant-expression : statement
7705 When a label is parsed without errors, the label is added to the
7706 parse tree by the finish_* functions, so this function doesn't
7707 have to return the label. */
7709 static void
7710 cp_parser_label_for_labeled_statement (cp_parser* parser)
7712 cp_token *token;
7713 tree label = NULL_TREE;
7715 /* The next token should be an identifier. */
7716 token = cp_lexer_peek_token (parser->lexer);
7717 if (token->type != CPP_NAME
7718 && token->type != CPP_KEYWORD)
7720 cp_parser_error (parser, "expected labeled-statement");
7721 return;
7724 switch (token->keyword)
7726 case RID_CASE:
7728 tree expr, expr_hi;
7729 cp_token *ellipsis;
7731 /* Consume the `case' token. */
7732 cp_lexer_consume_token (parser->lexer);
7733 /* Parse the constant-expression. */
7734 expr = cp_parser_constant_expression (parser,
7735 /*allow_non_constant_p=*/false,
7736 NULL);
7738 ellipsis = cp_lexer_peek_token (parser->lexer);
7739 if (ellipsis->type == CPP_ELLIPSIS)
7741 /* Consume the `...' token. */
7742 cp_lexer_consume_token (parser->lexer);
7743 expr_hi =
7744 cp_parser_constant_expression (parser,
7745 /*allow_non_constant_p=*/false,
7746 NULL);
7747 /* We don't need to emit warnings here, as the common code
7748 will do this for us. */
7750 else
7751 expr_hi = NULL_TREE;
7753 if (parser->in_switch_statement_p)
7754 finish_case_label (token->location, expr, expr_hi);
7755 else
7756 error_at (token->location,
7757 "case label %qE not within a switch statement",
7758 expr);
7760 break;
7762 case RID_DEFAULT:
7763 /* Consume the `default' token. */
7764 cp_lexer_consume_token (parser->lexer);
7766 if (parser->in_switch_statement_p)
7767 finish_case_label (token->location, NULL_TREE, NULL_TREE);
7768 else
7769 error_at (token->location, "case label not within a switch statement");
7770 break;
7772 default:
7773 /* Anything else must be an ordinary label. */
7774 label = finish_label_stmt (cp_parser_identifier (parser));
7775 break;
7778 /* Require the `:' token. */
7779 cp_parser_require (parser, CPP_COLON, "%<:%>");
7781 /* An ordinary label may optionally be followed by attributes.
7782 However, this is only permitted if the attributes are then
7783 followed by a semicolon. This is because, for backward
7784 compatibility, when parsing
7785 lab: __attribute__ ((unused)) int i;
7786 we want the attribute to attach to "i", not "lab". */
7787 if (label != NULL_TREE
7788 && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
7790 tree attrs;
7792 cp_parser_parse_tentatively (parser);
7793 attrs = cp_parser_attributes_opt (parser);
7794 if (attrs == NULL_TREE
7795 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7796 cp_parser_abort_tentative_parse (parser);
7797 else if (!cp_parser_parse_definitely (parser))
7799 else
7800 cplus_decl_attributes (&label, attrs, 0);
7804 /* Parse an expression-statement.
7806 expression-statement:
7807 expression [opt] ;
7809 Returns the new EXPR_STMT -- or NULL_TREE if the expression
7810 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
7811 indicates whether this expression-statement is part of an
7812 expression statement. */
7814 static tree
7815 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
7817 tree statement = NULL_TREE;
7818 cp_token *token = cp_lexer_peek_token (parser->lexer);
7820 /* If the next token is a ';', then there is no expression
7821 statement. */
7822 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7823 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7825 /* Give a helpful message for "A<T>::type t;" and the like. */
7826 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
7827 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
7829 if (TREE_CODE (statement) == SCOPE_REF)
7830 error_at (token->location, "need %<typename%> before %qE because "
7831 "%qT is a dependent scope",
7832 statement, TREE_OPERAND (statement, 0));
7833 else if (is_overloaded_fn (statement)
7834 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
7836 /* A::A a; */
7837 tree fn = get_first_fn (statement);
7838 error_at (token->location,
7839 "%<%T::%D%> names the constructor, not the type",
7840 DECL_CONTEXT (fn), DECL_NAME (fn));
7844 /* Consume the final `;'. */
7845 cp_parser_consume_semicolon_at_end_of_statement (parser);
7847 if (in_statement_expr
7848 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
7849 /* This is the final expression statement of a statement
7850 expression. */
7851 statement = finish_stmt_expr_expr (statement, in_statement_expr);
7852 else if (statement)
7853 statement = finish_expr_stmt (statement);
7854 else
7855 finish_stmt ();
7857 return statement;
7860 /* Parse a compound-statement.
7862 compound-statement:
7863 { statement-seq [opt] }
7865 GNU extension:
7867 compound-statement:
7868 { label-declaration-seq [opt] statement-seq [opt] }
7870 label-declaration-seq:
7871 label-declaration
7872 label-declaration-seq label-declaration
7874 Returns a tree representing the statement. */
7876 static tree
7877 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7878 bool in_try)
7880 tree compound_stmt;
7882 /* Consume the `{'. */
7883 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7884 return error_mark_node;
7885 /* Begin the compound-statement. */
7886 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7887 /* If the next keyword is `__label__' we have a label declaration. */
7888 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7889 cp_parser_label_declaration (parser);
7890 /* Parse an (optional) statement-seq. */
7891 cp_parser_statement_seq_opt (parser, in_statement_expr);
7892 /* Finish the compound-statement. */
7893 finish_compound_stmt (compound_stmt);
7894 /* Consume the `}'. */
7895 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7897 return compound_stmt;
7900 /* Parse an (optional) statement-seq.
7902 statement-seq:
7903 statement
7904 statement-seq [opt] statement */
7906 static void
7907 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7909 /* Scan statements until there aren't any more. */
7910 while (true)
7912 cp_token *token = cp_lexer_peek_token (parser->lexer);
7914 /* If we're looking at a `}', then we've run out of statements. */
7915 if (token->type == CPP_CLOSE_BRACE
7916 || token->type == CPP_EOF
7917 || token->type == CPP_PRAGMA_EOL)
7918 break;
7920 /* If we are in a compound statement and find 'else' then
7921 something went wrong. */
7922 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7924 if (parser->in_statement & IN_IF_STMT)
7925 break;
7926 else
7928 token = cp_lexer_consume_token (parser->lexer);
7929 error_at (token->location, "%<else%> without a previous %<if%>");
7933 /* Parse the statement. */
7934 cp_parser_statement (parser, in_statement_expr, true, NULL);
7938 /* Parse a selection-statement.
7940 selection-statement:
7941 if ( condition ) statement
7942 if ( condition ) statement else statement
7943 switch ( condition ) statement
7945 Returns the new IF_STMT or SWITCH_STMT.
7947 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7948 is a (possibly labeled) if statement which is not enclosed in
7949 braces and has an else clause. This is used to implement
7950 -Wparentheses. */
7952 static tree
7953 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7955 cp_token *token;
7956 enum rid keyword;
7958 if (if_p != NULL)
7959 *if_p = false;
7961 /* Peek at the next token. */
7962 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7964 /* See what kind of keyword it is. */
7965 keyword = token->keyword;
7966 switch (keyword)
7968 case RID_IF:
7969 case RID_SWITCH:
7971 tree statement;
7972 tree condition;
7974 /* Look for the `('. */
7975 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7977 cp_parser_skip_to_end_of_statement (parser);
7978 return error_mark_node;
7981 /* Begin the selection-statement. */
7982 if (keyword == RID_IF)
7983 statement = begin_if_stmt ();
7984 else
7985 statement = begin_switch_stmt ();
7987 /* Parse the condition. */
7988 condition = cp_parser_condition (parser);
7989 /* Look for the `)'. */
7990 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7991 cp_parser_skip_to_closing_parenthesis (parser, true, false,
7992 /*consume_paren=*/true);
7994 if (keyword == RID_IF)
7996 bool nested_if;
7997 unsigned char in_statement;
7999 /* Add the condition. */
8000 finish_if_stmt_cond (condition, statement);
8002 /* Parse the then-clause. */
8003 in_statement = parser->in_statement;
8004 parser->in_statement |= IN_IF_STMT;
8005 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8007 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8008 add_stmt (build_empty_stmt (loc));
8009 cp_lexer_consume_token (parser->lexer);
8010 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
8011 warning_at (loc, OPT_Wempty_body, "suggest braces around "
8012 "empty body in an %<if%> statement");
8013 nested_if = false;
8015 else
8016 cp_parser_implicitly_scoped_statement (parser, &nested_if);
8017 parser->in_statement = in_statement;
8019 finish_then_clause (statement);
8021 /* If the next token is `else', parse the else-clause. */
8022 if (cp_lexer_next_token_is_keyword (parser->lexer,
8023 RID_ELSE))
8025 /* Consume the `else' keyword. */
8026 cp_lexer_consume_token (parser->lexer);
8027 begin_else_clause (statement);
8028 /* Parse the else-clause. */
8029 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8031 location_t loc;
8032 loc = cp_lexer_peek_token (parser->lexer)->location;
8033 warning_at (loc,
8034 OPT_Wempty_body, "suggest braces around "
8035 "empty body in an %<else%> statement");
8036 add_stmt (build_empty_stmt (loc));
8037 cp_lexer_consume_token (parser->lexer);
8039 else
8040 cp_parser_implicitly_scoped_statement (parser, NULL);
8042 finish_else_clause (statement);
8044 /* If we are currently parsing a then-clause, then
8045 IF_P will not be NULL. We set it to true to
8046 indicate that this if statement has an else clause.
8047 This may trigger the Wparentheses warning below
8048 when we get back up to the parent if statement. */
8049 if (if_p != NULL)
8050 *if_p = true;
8052 else
8054 /* This if statement does not have an else clause. If
8055 NESTED_IF is true, then the then-clause is an if
8056 statement which does have an else clause. We warn
8057 about the potential ambiguity. */
8058 if (nested_if)
8059 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
8060 "suggest explicit braces to avoid ambiguous"
8061 " %<else%>");
8064 /* Now we're all done with the if-statement. */
8065 finish_if_stmt (statement);
8067 else
8069 bool in_switch_statement_p;
8070 unsigned char in_statement;
8072 /* Add the condition. */
8073 finish_switch_cond (condition, statement);
8075 /* Parse the body of the switch-statement. */
8076 in_switch_statement_p = parser->in_switch_statement_p;
8077 in_statement = parser->in_statement;
8078 parser->in_switch_statement_p = true;
8079 parser->in_statement |= IN_SWITCH_STMT;
8080 cp_parser_implicitly_scoped_statement (parser, NULL);
8081 parser->in_switch_statement_p = in_switch_statement_p;
8082 parser->in_statement = in_statement;
8084 /* Now we're all done with the switch-statement. */
8085 finish_switch_stmt (statement);
8088 return statement;
8090 break;
8092 default:
8093 cp_parser_error (parser, "expected selection-statement");
8094 return error_mark_node;
8098 /* Parse a condition.
8100 condition:
8101 expression
8102 type-specifier-seq declarator = initializer-clause
8103 type-specifier-seq declarator braced-init-list
8105 GNU Extension:
8107 condition:
8108 type-specifier-seq declarator asm-specification [opt]
8109 attributes [opt] = assignment-expression
8111 Returns the expression that should be tested. */
8113 static tree
8114 cp_parser_condition (cp_parser* parser)
8116 cp_decl_specifier_seq type_specifiers;
8117 const char *saved_message;
8119 /* Try the declaration first. */
8120 cp_parser_parse_tentatively (parser);
8121 /* New types are not allowed in the type-specifier-seq for a
8122 condition. */
8123 saved_message = parser->type_definition_forbidden_message;
8124 parser->type_definition_forbidden_message
8125 = G_("types may not be defined in conditions");
8126 /* Parse the type-specifier-seq. */
8127 cp_parser_type_specifier_seq (parser, /*is_declaration==*/true,
8128 /*is_trailing_return=*/false,
8129 &type_specifiers);
8130 /* Restore the saved message. */
8131 parser->type_definition_forbidden_message = saved_message;
8132 /* If all is well, we might be looking at a declaration. */
8133 if (!cp_parser_error_occurred (parser))
8135 tree decl;
8136 tree asm_specification;
8137 tree attributes;
8138 cp_declarator *declarator;
8139 tree initializer = NULL_TREE;
8141 /* Parse the declarator. */
8142 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8143 /*ctor_dtor_or_conv_p=*/NULL,
8144 /*parenthesized_p=*/NULL,
8145 /*member_p=*/false);
8146 /* Parse the attributes. */
8147 attributes = cp_parser_attributes_opt (parser);
8148 /* Parse the asm-specification. */
8149 asm_specification = cp_parser_asm_specification_opt (parser);
8150 /* If the next token is not an `=' or '{', then we might still be
8151 looking at an expression. For example:
8153 if (A(a).x)
8155 looks like a decl-specifier-seq and a declarator -- but then
8156 there is no `=', so this is an expression. */
8157 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8158 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8159 cp_parser_simulate_error (parser);
8161 /* If we did see an `=' or '{', then we are looking at a declaration
8162 for sure. */
8163 if (cp_parser_parse_definitely (parser))
8165 tree pushed_scope;
8166 bool non_constant_p;
8167 bool flags = LOOKUP_ONLYCONVERTING;
8169 /* Create the declaration. */
8170 decl = start_decl (declarator, &type_specifiers,
8171 /*initialized_p=*/true,
8172 attributes, /*prefix_attributes=*/NULL_TREE,
8173 &pushed_scope);
8175 /* Parse the initializer. */
8176 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8178 initializer = cp_parser_braced_list (parser, &non_constant_p);
8179 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8180 flags = 0;
8182 else
8184 /* Consume the `='. */
8185 cp_parser_require (parser, CPP_EQ, "%<=%>");
8186 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8188 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
8189 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8191 if (!non_constant_p)
8192 initializer = fold_non_dependent_expr (initializer);
8194 /* Process the initializer. */
8195 cp_finish_decl (decl,
8196 initializer, !non_constant_p,
8197 asm_specification,
8198 flags);
8200 if (pushed_scope)
8201 pop_scope (pushed_scope);
8203 return convert_from_reference (decl);
8206 /* If we didn't even get past the declarator successfully, we are
8207 definitely not looking at a declaration. */
8208 else
8209 cp_parser_abort_tentative_parse (parser);
8211 /* Otherwise, we are looking at an expression. */
8212 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
8215 /* Parse an iteration-statement.
8217 iteration-statement:
8218 while ( condition ) statement
8219 do statement while ( expression ) ;
8220 for ( for-init-statement condition [opt] ; expression [opt] )
8221 statement
8223 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
8225 static tree
8226 cp_parser_iteration_statement (cp_parser* parser)
8228 cp_token *token;
8229 enum rid keyword;
8230 tree statement;
8231 unsigned char in_statement;
8233 /* Peek at the next token. */
8234 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
8235 if (!token)
8236 return error_mark_node;
8238 /* Remember whether or not we are already within an iteration
8239 statement. */
8240 in_statement = parser->in_statement;
8242 /* See what kind of keyword it is. */
8243 keyword = token->keyword;
8244 switch (keyword)
8246 case RID_WHILE:
8248 tree condition;
8250 /* Begin the while-statement. */
8251 statement = begin_while_stmt ();
8252 /* Look for the `('. */
8253 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8254 /* Parse the condition. */
8255 condition = cp_parser_condition (parser);
8256 finish_while_stmt_cond (condition, statement);
8257 /* Look for the `)'. */
8258 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8259 /* Parse the dependent statement. */
8260 parser->in_statement = IN_ITERATION_STMT;
8261 cp_parser_already_scoped_statement (parser);
8262 parser->in_statement = in_statement;
8263 /* We're done with the while-statement. */
8264 finish_while_stmt (statement);
8266 break;
8268 case RID_DO:
8270 tree expression;
8272 /* Begin the do-statement. */
8273 statement = begin_do_stmt ();
8274 /* Parse the body of the do-statement. */
8275 parser->in_statement = IN_ITERATION_STMT;
8276 cp_parser_implicitly_scoped_statement (parser, NULL);
8277 parser->in_statement = in_statement;
8278 finish_do_body (statement);
8279 /* Look for the `while' keyword. */
8280 cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
8281 /* Look for the `('. */
8282 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8283 /* Parse the expression. */
8284 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8285 /* We're done with the do-statement. */
8286 finish_do_stmt (expression, statement);
8287 /* Look for the `)'. */
8288 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8289 /* Look for the `;'. */
8290 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8292 break;
8294 case RID_FOR:
8296 tree condition = NULL_TREE;
8297 tree expression = NULL_TREE;
8299 /* Begin the for-statement. */
8300 statement = begin_for_stmt ();
8301 /* Look for the `('. */
8302 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8303 /* Parse the initialization. */
8304 cp_parser_for_init_statement (parser);
8305 finish_for_init_stmt (statement);
8307 /* If there's a condition, process it. */
8308 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8309 condition = cp_parser_condition (parser);
8310 finish_for_cond (condition, statement);
8311 /* Look for the `;'. */
8312 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8314 /* If there's an expression, process it. */
8315 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8316 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8317 finish_for_expr (expression, statement);
8318 /* Look for the `)'. */
8319 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8321 /* Parse the body of the for-statement. */
8322 parser->in_statement = IN_ITERATION_STMT;
8323 cp_parser_already_scoped_statement (parser);
8324 parser->in_statement = in_statement;
8326 /* We're done with the for-statement. */
8327 finish_for_stmt (statement);
8329 break;
8331 default:
8332 cp_parser_error (parser, "expected iteration-statement");
8333 statement = error_mark_node;
8334 break;
8337 return statement;
8340 /* Parse a for-init-statement.
8342 for-init-statement:
8343 expression-statement
8344 simple-declaration */
8346 static void
8347 cp_parser_for_init_statement (cp_parser* parser)
8349 /* If the next token is a `;', then we have an empty
8350 expression-statement. Grammatically, this is also a
8351 simple-declaration, but an invalid one, because it does not
8352 declare anything. Therefore, if we did not handle this case
8353 specially, we would issue an error message about an invalid
8354 declaration. */
8355 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8357 /* We're going to speculatively look for a declaration, falling back
8358 to an expression, if necessary. */
8359 cp_parser_parse_tentatively (parser);
8360 /* Parse the declaration. */
8361 cp_parser_simple_declaration (parser,
8362 /*function_definition_allowed_p=*/false);
8363 /* If the tentative parse failed, then we shall need to look for an
8364 expression-statement. */
8365 if (cp_parser_parse_definitely (parser))
8366 return;
8369 cp_parser_expression_statement (parser, NULL_TREE);
8372 /* Parse a jump-statement.
8374 jump-statement:
8375 break ;
8376 continue ;
8377 return expression [opt] ;
8378 return braced-init-list ;
8379 goto identifier ;
8381 GNU extension:
8383 jump-statement:
8384 goto * expression ;
8386 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
8388 static tree
8389 cp_parser_jump_statement (cp_parser* parser)
8391 tree statement = error_mark_node;
8392 cp_token *token;
8393 enum rid keyword;
8394 unsigned char in_statement;
8396 /* Peek at the next token. */
8397 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
8398 if (!token)
8399 return error_mark_node;
8401 /* See what kind of keyword it is. */
8402 keyword = token->keyword;
8403 switch (keyword)
8405 case RID_BREAK:
8406 in_statement = parser->in_statement & ~IN_IF_STMT;
8407 switch (in_statement)
8409 case 0:
8410 error_at (token->location, "break statement not within loop or switch");
8411 break;
8412 default:
8413 gcc_assert ((in_statement & IN_SWITCH_STMT)
8414 || in_statement == IN_ITERATION_STMT);
8415 statement = finish_break_stmt ();
8416 break;
8417 case IN_OMP_BLOCK:
8418 error_at (token->location, "invalid exit from OpenMP structured block");
8419 break;
8420 case IN_OMP_FOR:
8421 error_at (token->location, "break statement used with OpenMP for loop");
8422 break;
8424 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8425 break;
8427 case RID_CONTINUE:
8428 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
8430 case 0:
8431 error_at (token->location, "continue statement not within a loop");
8432 break;
8433 case IN_ITERATION_STMT:
8434 case IN_OMP_FOR:
8435 statement = finish_continue_stmt ();
8436 break;
8437 case IN_OMP_BLOCK:
8438 error_at (token->location, "invalid exit from OpenMP structured block");
8439 break;
8440 default:
8441 gcc_unreachable ();
8443 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8444 break;
8446 case RID_RETURN:
8448 tree expr;
8449 bool expr_non_constant_p;
8451 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8453 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8454 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8456 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8457 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8458 else
8459 /* If the next token is a `;', then there is no
8460 expression. */
8461 expr = NULL_TREE;
8462 /* Build the return-statement. */
8463 statement = finish_return_stmt (expr);
8464 /* Look for the final `;'. */
8465 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8467 break;
8469 case RID_GOTO:
8470 /* Create the goto-statement. */
8471 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
8473 /* Issue a warning about this use of a GNU extension. */
8474 pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
8475 /* Consume the '*' token. */
8476 cp_lexer_consume_token (parser->lexer);
8477 /* Parse the dependent expression. */
8478 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
8480 else
8481 finish_goto_stmt (cp_parser_identifier (parser));
8482 /* Look for the final `;'. */
8483 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8484 break;
8486 default:
8487 cp_parser_error (parser, "expected jump-statement");
8488 break;
8491 return statement;
8494 /* Parse a declaration-statement.
8496 declaration-statement:
8497 block-declaration */
8499 static void
8500 cp_parser_declaration_statement (cp_parser* parser)
8502 void *p;
8504 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
8505 p = obstack_alloc (&declarator_obstack, 0);
8507 /* Parse the block-declaration. */
8508 cp_parser_block_declaration (parser, /*statement_p=*/true);
8510 /* Free any declarators allocated. */
8511 obstack_free (&declarator_obstack, p);
8513 /* Finish off the statement. */
8514 finish_stmt ();
8517 /* Some dependent statements (like `if (cond) statement'), are
8518 implicitly in their own scope. In other words, if the statement is
8519 a single statement (as opposed to a compound-statement), it is
8520 none-the-less treated as if it were enclosed in braces. Any
8521 declarations appearing in the dependent statement are out of scope
8522 after control passes that point. This function parses a statement,
8523 but ensures that is in its own scope, even if it is not a
8524 compound-statement.
8526 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8527 is a (possibly labeled) if statement which is not enclosed in
8528 braces and has an else clause. This is used to implement
8529 -Wparentheses.
8531 Returns the new statement. */
8533 static tree
8534 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
8536 tree statement;
8538 if (if_p != NULL)
8539 *if_p = false;
8541 /* Mark if () ; with a special NOP_EXPR. */
8542 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8544 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8545 cp_lexer_consume_token (parser->lexer);
8546 statement = add_stmt (build_empty_stmt (loc));
8548 /* if a compound is opened, we simply parse the statement directly. */
8549 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8550 statement = cp_parser_compound_statement (parser, NULL, false);
8551 /* If the token is not a `{', then we must take special action. */
8552 else
8554 /* Create a compound-statement. */
8555 statement = begin_compound_stmt (0);
8556 /* Parse the dependent-statement. */
8557 cp_parser_statement (parser, NULL_TREE, false, if_p);
8558 /* Finish the dummy compound-statement. */
8559 finish_compound_stmt (statement);
8562 /* Return the statement. */
8563 return statement;
8566 /* For some dependent statements (like `while (cond) statement'), we
8567 have already created a scope. Therefore, even if the dependent
8568 statement is a compound-statement, we do not want to create another
8569 scope. */
8571 static void
8572 cp_parser_already_scoped_statement (cp_parser* parser)
8574 /* If the token is a `{', then we must take special action. */
8575 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8576 cp_parser_statement (parser, NULL_TREE, false, NULL);
8577 else
8579 /* Avoid calling cp_parser_compound_statement, so that we
8580 don't create a new scope. Do everything else by hand. */
8581 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
8582 /* If the next keyword is `__label__' we have a label declaration. */
8583 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8584 cp_parser_label_declaration (parser);
8585 /* Parse an (optional) statement-seq. */
8586 cp_parser_statement_seq_opt (parser, NULL_TREE);
8587 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8591 /* Declarations [gram.dcl.dcl] */
8593 /* Parse an optional declaration-sequence.
8595 declaration-seq:
8596 declaration
8597 declaration-seq declaration */
8599 static void
8600 cp_parser_declaration_seq_opt (cp_parser* parser)
8602 while (true)
8604 cp_token *token;
8606 token = cp_lexer_peek_token (parser->lexer);
8608 if (token->type == CPP_CLOSE_BRACE
8609 || token->type == CPP_EOF
8610 || token->type == CPP_PRAGMA_EOL)
8611 break;
8613 if (token->type == CPP_SEMICOLON)
8615 /* A declaration consisting of a single semicolon is
8616 invalid. Allow it unless we're being pedantic. */
8617 cp_lexer_consume_token (parser->lexer);
8618 if (!in_system_header)
8619 pedwarn (input_location, OPT_pedantic, "extra %<;%>");
8620 continue;
8623 /* If we're entering or exiting a region that's implicitly
8624 extern "C", modify the lang context appropriately. */
8625 if (!parser->implicit_extern_c && token->implicit_extern_c)
8627 push_lang_context (lang_name_c);
8628 parser->implicit_extern_c = true;
8630 else if (parser->implicit_extern_c && !token->implicit_extern_c)
8632 pop_lang_context ();
8633 parser->implicit_extern_c = false;
8636 if (token->type == CPP_PRAGMA)
8638 /* A top-level declaration can consist solely of a #pragma.
8639 A nested declaration cannot, so this is done here and not
8640 in cp_parser_declaration. (A #pragma at block scope is
8641 handled in cp_parser_statement.) */
8642 cp_parser_pragma (parser, pragma_external);
8643 continue;
8646 /* Parse the declaration itself. */
8647 cp_parser_declaration (parser);
8651 /* Parse a declaration.
8653 declaration:
8654 block-declaration
8655 function-definition
8656 template-declaration
8657 explicit-instantiation
8658 explicit-specialization
8659 linkage-specification
8660 namespace-definition
8662 GNU extension:
8664 declaration:
8665 __extension__ declaration */
8667 static void
8668 cp_parser_declaration (cp_parser* parser)
8670 cp_token token1;
8671 cp_token token2;
8672 int saved_pedantic;
8673 void *p;
8675 /* Check for the `__extension__' keyword. */
8676 if (cp_parser_extension_opt (parser, &saved_pedantic))
8678 /* Parse the qualified declaration. */
8679 cp_parser_declaration (parser);
8680 /* Restore the PEDANTIC flag. */
8681 pedantic = saved_pedantic;
8683 return;
8686 /* Try to figure out what kind of declaration is present. */
8687 token1 = *cp_lexer_peek_token (parser->lexer);
8689 if (token1.type != CPP_EOF)
8690 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
8691 else
8693 token2.type = CPP_EOF;
8694 token2.keyword = RID_MAX;
8697 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
8698 p = obstack_alloc (&declarator_obstack, 0);
8700 /* If the next token is `extern' and the following token is a string
8701 literal, then we have a linkage specification. */
8702 if (token1.keyword == RID_EXTERN
8703 && cp_parser_is_string_literal (&token2))
8704 cp_parser_linkage_specification (parser);
8705 /* If the next token is `template', then we have either a template
8706 declaration, an explicit instantiation, or an explicit
8707 specialization. */
8708 else if (token1.keyword == RID_TEMPLATE)
8710 /* `template <>' indicates a template specialization. */
8711 if (token2.type == CPP_LESS
8712 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
8713 cp_parser_explicit_specialization (parser);
8714 /* `template <' indicates a template declaration. */
8715 else if (token2.type == CPP_LESS)
8716 cp_parser_template_declaration (parser, /*member_p=*/false);
8717 /* Anything else must be an explicit instantiation. */
8718 else
8719 cp_parser_explicit_instantiation (parser);
8721 /* If the next token is `export', then we have a template
8722 declaration. */
8723 else if (token1.keyword == RID_EXPORT)
8724 cp_parser_template_declaration (parser, /*member_p=*/false);
8725 /* If the next token is `extern', 'static' or 'inline' and the one
8726 after that is `template', we have a GNU extended explicit
8727 instantiation directive. */
8728 else if (cp_parser_allow_gnu_extensions_p (parser)
8729 && (token1.keyword == RID_EXTERN
8730 || token1.keyword == RID_STATIC
8731 || token1.keyword == RID_INLINE)
8732 && token2.keyword == RID_TEMPLATE)
8733 cp_parser_explicit_instantiation (parser);
8734 /* If the next token is `namespace', check for a named or unnamed
8735 namespace definition. */
8736 else if (token1.keyword == RID_NAMESPACE
8737 && (/* A named namespace definition. */
8738 (token2.type == CPP_NAME
8739 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
8740 != CPP_EQ))
8741 /* An unnamed namespace definition. */
8742 || token2.type == CPP_OPEN_BRACE
8743 || token2.keyword == RID_ATTRIBUTE))
8744 cp_parser_namespace_definition (parser);
8745 /* An inline (associated) namespace definition. */
8746 else if (token1.keyword == RID_INLINE
8747 && token2.keyword == RID_NAMESPACE)
8748 cp_parser_namespace_definition (parser);
8749 /* Objective-C++ declaration/definition. */
8750 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
8751 cp_parser_objc_declaration (parser);
8752 /* We must have either a block declaration or a function
8753 definition. */
8754 else
8755 /* Try to parse a block-declaration, or a function-definition. */
8756 cp_parser_block_declaration (parser, /*statement_p=*/false);
8758 /* Free any declarators allocated. */
8759 obstack_free (&declarator_obstack, p);
8762 /* Parse a block-declaration.
8764 block-declaration:
8765 simple-declaration
8766 asm-definition
8767 namespace-alias-definition
8768 using-declaration
8769 using-directive
8771 GNU Extension:
8773 block-declaration:
8774 __extension__ block-declaration
8776 C++0x Extension:
8778 block-declaration:
8779 static_assert-declaration
8781 If STATEMENT_P is TRUE, then this block-declaration is occurring as
8782 part of a declaration-statement. */
8784 static void
8785 cp_parser_block_declaration (cp_parser *parser,
8786 bool statement_p)
8788 cp_token *token1;
8789 int saved_pedantic;
8791 /* Check for the `__extension__' keyword. */
8792 if (cp_parser_extension_opt (parser, &saved_pedantic))
8794 /* Parse the qualified declaration. */
8795 cp_parser_block_declaration (parser, statement_p);
8796 /* Restore the PEDANTIC flag. */
8797 pedantic = saved_pedantic;
8799 return;
8802 /* Peek at the next token to figure out which kind of declaration is
8803 present. */
8804 token1 = cp_lexer_peek_token (parser->lexer);
8806 /* If the next keyword is `asm', we have an asm-definition. */
8807 if (token1->keyword == RID_ASM)
8809 if (statement_p)
8810 cp_parser_commit_to_tentative_parse (parser);
8811 cp_parser_asm_definition (parser);
8813 /* If the next keyword is `namespace', we have a
8814 namespace-alias-definition. */
8815 else if (token1->keyword == RID_NAMESPACE)
8816 cp_parser_namespace_alias_definition (parser);
8817 /* If the next keyword is `using', we have either a
8818 using-declaration or a using-directive. */
8819 else if (token1->keyword == RID_USING)
8821 cp_token *token2;
8823 if (statement_p)
8824 cp_parser_commit_to_tentative_parse (parser);
8825 /* If the token after `using' is `namespace', then we have a
8826 using-directive. */
8827 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8828 if (token2->keyword == RID_NAMESPACE)
8829 cp_parser_using_directive (parser);
8830 /* Otherwise, it's a using-declaration. */
8831 else
8832 cp_parser_using_declaration (parser,
8833 /*access_declaration_p=*/false);
8835 /* If the next keyword is `__label__' we have a misplaced label
8836 declaration. */
8837 else if (token1->keyword == RID_LABEL)
8839 cp_lexer_consume_token (parser->lexer);
8840 error_at (token1->location, "%<__label__%> not at the beginning of a block");
8841 cp_parser_skip_to_end_of_statement (parser);
8842 /* If the next token is now a `;', consume it. */
8843 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8844 cp_lexer_consume_token (parser->lexer);
8846 /* If the next token is `static_assert' we have a static assertion. */
8847 else if (token1->keyword == RID_STATIC_ASSERT)
8848 cp_parser_static_assert (parser, /*member_p=*/false);
8849 /* Anything else must be a simple-declaration. */
8850 else
8851 cp_parser_simple_declaration (parser, !statement_p);
8854 /* Parse a simple-declaration.
8856 simple-declaration:
8857 decl-specifier-seq [opt] init-declarator-list [opt] ;
8859 init-declarator-list:
8860 init-declarator
8861 init-declarator-list , init-declarator
8863 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8864 function-definition as a simple-declaration. */
8866 static void
8867 cp_parser_simple_declaration (cp_parser* parser,
8868 bool function_definition_allowed_p)
8870 cp_decl_specifier_seq decl_specifiers;
8871 int declares_class_or_enum;
8872 bool saw_declarator;
8874 /* Defer access checks until we know what is being declared; the
8875 checks for names appearing in the decl-specifier-seq should be
8876 done as if we were in the scope of the thing being declared. */
8877 push_deferring_access_checks (dk_deferred);
8879 /* Parse the decl-specifier-seq. We have to keep track of whether
8880 or not the decl-specifier-seq declares a named class or
8881 enumeration type, since that is the only case in which the
8882 init-declarator-list is allowed to be empty.
8884 [dcl.dcl]
8886 In a simple-declaration, the optional init-declarator-list can be
8887 omitted only when declaring a class or enumeration, that is when
8888 the decl-specifier-seq contains either a class-specifier, an
8889 elaborated-type-specifier, or an enum-specifier. */
8890 cp_parser_decl_specifier_seq (parser,
8891 CP_PARSER_FLAGS_OPTIONAL,
8892 &decl_specifiers,
8893 &declares_class_or_enum);
8894 /* We no longer need to defer access checks. */
8895 stop_deferring_access_checks ();
8897 /* In a block scope, a valid declaration must always have a
8898 decl-specifier-seq. By not trying to parse declarators, we can
8899 resolve the declaration/expression ambiguity more quickly. */
8900 if (!function_definition_allowed_p
8901 && !decl_specifiers.any_specifiers_p)
8903 cp_parser_error (parser, "expected declaration");
8904 goto done;
8907 /* If the next two tokens are both identifiers, the code is
8908 erroneous. The usual cause of this situation is code like:
8910 T t;
8912 where "T" should name a type -- but does not. */
8913 if (!decl_specifiers.any_type_specifiers_p
8914 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8916 /* If parsing tentatively, we should commit; we really are
8917 looking at a declaration. */
8918 cp_parser_commit_to_tentative_parse (parser);
8919 /* Give up. */
8920 goto done;
8923 /* If we have seen at least one decl-specifier, and the next token
8924 is not a parenthesis, then we must be looking at a declaration.
8925 (After "int (" we might be looking at a functional cast.) */
8926 if (decl_specifiers.any_specifiers_p
8927 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8928 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
8929 && !cp_parser_error_occurred (parser))
8930 cp_parser_commit_to_tentative_parse (parser);
8932 /* Keep going until we hit the `;' at the end of the simple
8933 declaration. */
8934 saw_declarator = false;
8935 while (cp_lexer_next_token_is_not (parser->lexer,
8936 CPP_SEMICOLON))
8938 cp_token *token;
8939 bool function_definition_p;
8940 tree decl;
8942 if (saw_declarator)
8944 /* If we are processing next declarator, coma is expected */
8945 token = cp_lexer_peek_token (parser->lexer);
8946 gcc_assert (token->type == CPP_COMMA);
8947 cp_lexer_consume_token (parser->lexer);
8949 else
8950 saw_declarator = true;
8952 /* Parse the init-declarator. */
8953 decl = cp_parser_init_declarator (parser, &decl_specifiers,
8954 /*checks=*/NULL,
8955 function_definition_allowed_p,
8956 /*member_p=*/false,
8957 declares_class_or_enum,
8958 &function_definition_p);
8959 /* If an error occurred while parsing tentatively, exit quickly.
8960 (That usually happens when in the body of a function; each
8961 statement is treated as a declaration-statement until proven
8962 otherwise.) */
8963 if (cp_parser_error_occurred (parser))
8964 goto done;
8965 /* Handle function definitions specially. */
8966 if (function_definition_p)
8968 /* If the next token is a `,', then we are probably
8969 processing something like:
8971 void f() {}, *p;
8973 which is erroneous. */
8974 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8976 cp_token *token = cp_lexer_peek_token (parser->lexer);
8977 error_at (token->location,
8978 "mixing"
8979 " declarations and function-definitions is forbidden");
8981 /* Otherwise, we're done with the list of declarators. */
8982 else
8984 pop_deferring_access_checks ();
8985 return;
8988 /* The next token should be either a `,' or a `;'. */
8989 token = cp_lexer_peek_token (parser->lexer);
8990 /* If it's a `,', there are more declarators to come. */
8991 if (token->type == CPP_COMMA)
8992 /* will be consumed next time around */;
8993 /* If it's a `;', we are done. */
8994 else if (token->type == CPP_SEMICOLON)
8995 break;
8996 /* Anything else is an error. */
8997 else
8999 /* If we have already issued an error message we don't need
9000 to issue another one. */
9001 if (decl != error_mark_node
9002 || cp_parser_uncommitted_to_tentative_parse_p (parser))
9003 cp_parser_error (parser, "expected %<,%> or %<;%>");
9004 /* Skip tokens until we reach the end of the statement. */
9005 cp_parser_skip_to_end_of_statement (parser);
9006 /* If the next token is now a `;', consume it. */
9007 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9008 cp_lexer_consume_token (parser->lexer);
9009 goto done;
9011 /* After the first time around, a function-definition is not
9012 allowed -- even if it was OK at first. For example:
9014 int i, f() {}
9016 is not valid. */
9017 function_definition_allowed_p = false;
9020 /* Issue an error message if no declarators are present, and the
9021 decl-specifier-seq does not itself declare a class or
9022 enumeration. */
9023 if (!saw_declarator)
9025 if (cp_parser_declares_only_class_p (parser))
9026 shadow_tag (&decl_specifiers);
9027 /* Perform any deferred access checks. */
9028 perform_deferred_access_checks ();
9031 /* Consume the `;'. */
9032 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
9034 done:
9035 pop_deferring_access_checks ();
9038 /* Parse a decl-specifier-seq.
9040 decl-specifier-seq:
9041 decl-specifier-seq [opt] decl-specifier
9043 decl-specifier:
9044 storage-class-specifier
9045 type-specifier
9046 function-specifier
9047 friend
9048 typedef
9050 GNU Extension:
9052 decl-specifier:
9053 attributes
9055 Set *DECL_SPECS to a representation of the decl-specifier-seq.
9057 The parser flags FLAGS is used to control type-specifier parsing.
9059 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
9060 flags:
9062 1: one of the decl-specifiers is an elaborated-type-specifier
9063 (i.e., a type declaration)
9064 2: one of the decl-specifiers is an enum-specifier or a
9065 class-specifier (i.e., a type definition)
9069 static void
9070 cp_parser_decl_specifier_seq (cp_parser* parser,
9071 cp_parser_flags flags,
9072 cp_decl_specifier_seq *decl_specs,
9073 int* declares_class_or_enum)
9075 bool constructor_possible_p = !parser->in_declarator_p;
9076 cp_token *start_token = NULL;
9078 /* Clear DECL_SPECS. */
9079 clear_decl_specs (decl_specs);
9081 /* Assume no class or enumeration type is declared. */
9082 *declares_class_or_enum = 0;
9084 /* Keep reading specifiers until there are no more to read. */
9085 while (true)
9087 bool constructor_p;
9088 bool found_decl_spec;
9089 cp_token *token;
9091 /* Peek at the next token. */
9092 token = cp_lexer_peek_token (parser->lexer);
9094 /* Save the first token of the decl spec list for error
9095 reporting. */
9096 if (!start_token)
9097 start_token = token;
9098 /* Handle attributes. */
9099 if (token->keyword == RID_ATTRIBUTE)
9101 /* Parse the attributes. */
9102 decl_specs->attributes
9103 = chainon (decl_specs->attributes,
9104 cp_parser_attributes_opt (parser));
9105 continue;
9107 /* Assume we will find a decl-specifier keyword. */
9108 found_decl_spec = true;
9109 /* If the next token is an appropriate keyword, we can simply
9110 add it to the list. */
9111 switch (token->keyword)
9113 /* decl-specifier:
9114 friend
9115 constexpr */
9116 case RID_FRIEND:
9117 if (!at_class_scope_p ())
9119 error_at (token->location, "%<friend%> used outside of class");
9120 cp_lexer_purge_token (parser->lexer);
9122 else
9124 ++decl_specs->specs[(int) ds_friend];
9125 /* Consume the token. */
9126 cp_lexer_consume_token (parser->lexer);
9128 break;
9130 case RID_CONSTEXPR:
9131 ++decl_specs->specs[(int) ds_constexpr];
9132 cp_lexer_consume_token (parser->lexer);
9133 break;
9135 /* function-specifier:
9136 inline
9137 virtual
9138 explicit */
9139 case RID_INLINE:
9140 case RID_VIRTUAL:
9141 case RID_EXPLICIT:
9142 cp_parser_function_specifier_opt (parser, decl_specs);
9143 break;
9145 /* decl-specifier:
9146 typedef */
9147 case RID_TYPEDEF:
9148 ++decl_specs->specs[(int) ds_typedef];
9149 /* Consume the token. */
9150 cp_lexer_consume_token (parser->lexer);
9151 /* A constructor declarator cannot appear in a typedef. */
9152 constructor_possible_p = false;
9153 /* The "typedef" keyword can only occur in a declaration; we
9154 may as well commit at this point. */
9155 cp_parser_commit_to_tentative_parse (parser);
9157 if (decl_specs->storage_class != sc_none)
9158 decl_specs->conflicting_specifiers_p = true;
9159 break;
9161 /* storage-class-specifier:
9162 auto
9163 register
9164 static
9165 extern
9166 mutable
9168 GNU Extension:
9169 thread */
9170 case RID_AUTO:
9171 if (cxx_dialect == cxx98)
9173 /* Consume the token. */
9174 cp_lexer_consume_token (parser->lexer);
9176 /* Complain about `auto' as a storage specifier, if
9177 we're complaining about C++0x compatibility. */
9178 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9179 " will change meaning in C++0x; please remove it");
9181 /* Set the storage class anyway. */
9182 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9183 token->location);
9185 else
9186 /* C++0x auto type-specifier. */
9187 found_decl_spec = false;
9188 break;
9190 case RID_REGISTER:
9191 case RID_STATIC:
9192 case RID_EXTERN:
9193 case RID_MUTABLE:
9194 /* Consume the token. */
9195 cp_lexer_consume_token (parser->lexer);
9196 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9197 token->location);
9198 break;
9199 case RID_THREAD:
9200 /* Consume the token. */
9201 cp_lexer_consume_token (parser->lexer);
9202 ++decl_specs->specs[(int) ds_thread];
9203 break;
9205 default:
9206 /* We did not yet find a decl-specifier yet. */
9207 found_decl_spec = false;
9208 break;
9211 /* Constructors are a special case. The `S' in `S()' is not a
9212 decl-specifier; it is the beginning of the declarator. */
9213 constructor_p
9214 = (!found_decl_spec
9215 && constructor_possible_p
9216 && (cp_parser_constructor_declarator_p
9217 (parser, decl_specs->specs[(int) ds_friend] != 0)));
9219 /* If we don't have a DECL_SPEC yet, then we must be looking at
9220 a type-specifier. */
9221 if (!found_decl_spec && !constructor_p)
9223 int decl_spec_declares_class_or_enum;
9224 bool is_cv_qualifier;
9225 tree type_spec;
9227 type_spec
9228 = cp_parser_type_specifier (parser, flags,
9229 decl_specs,
9230 /*is_declaration=*/true,
9231 &decl_spec_declares_class_or_enum,
9232 &is_cv_qualifier);
9233 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
9235 /* If this type-specifier referenced a user-defined type
9236 (a typedef, class-name, etc.), then we can't allow any
9237 more such type-specifiers henceforth.
9239 [dcl.spec]
9241 The longest sequence of decl-specifiers that could
9242 possibly be a type name is taken as the
9243 decl-specifier-seq of a declaration. The sequence shall
9244 be self-consistent as described below.
9246 [dcl.type]
9248 As a general rule, at most one type-specifier is allowed
9249 in the complete decl-specifier-seq of a declaration. The
9250 only exceptions are the following:
9252 -- const or volatile can be combined with any other
9253 type-specifier.
9255 -- signed or unsigned can be combined with char, long,
9256 short, or int.
9258 -- ..
9260 Example:
9262 typedef char* Pc;
9263 void g (const int Pc);
9265 Here, Pc is *not* part of the decl-specifier seq; it's
9266 the declarator. Therefore, once we see a type-specifier
9267 (other than a cv-qualifier), we forbid any additional
9268 user-defined types. We *do* still allow things like `int
9269 int' to be considered a decl-specifier-seq, and issue the
9270 error message later. */
9271 if (type_spec && !is_cv_qualifier)
9272 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
9273 /* A constructor declarator cannot follow a type-specifier. */
9274 if (type_spec)
9276 constructor_possible_p = false;
9277 found_decl_spec = true;
9278 if (!is_cv_qualifier)
9279 decl_specs->any_type_specifiers_p = true;
9283 /* If we still do not have a DECL_SPEC, then there are no more
9284 decl-specifiers. */
9285 if (!found_decl_spec)
9286 break;
9288 decl_specs->any_specifiers_p = true;
9289 /* After we see one decl-specifier, further decl-specifiers are
9290 always optional. */
9291 flags |= CP_PARSER_FLAGS_OPTIONAL;
9294 cp_parser_check_decl_spec (decl_specs, start_token->location);
9296 /* Don't allow a friend specifier with a class definition. */
9297 if (decl_specs->specs[(int) ds_friend] != 0
9298 && (*declares_class_or_enum & 2))
9299 error_at (start_token->location,
9300 "class definition may not be declared a friend");
9303 /* Parse an (optional) storage-class-specifier.
9305 storage-class-specifier:
9306 auto
9307 register
9308 static
9309 extern
9310 mutable
9312 GNU Extension:
9314 storage-class-specifier:
9315 thread
9317 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
9319 static tree
9320 cp_parser_storage_class_specifier_opt (cp_parser* parser)
9322 switch (cp_lexer_peek_token (parser->lexer)->keyword)
9324 case RID_AUTO:
9325 if (cxx_dialect != cxx98)
9326 return NULL_TREE;
9327 /* Fall through for C++98. */
9329 case RID_REGISTER:
9330 case RID_STATIC:
9331 case RID_EXTERN:
9332 case RID_MUTABLE:
9333 case RID_THREAD:
9334 /* Consume the token. */
9335 return cp_lexer_consume_token (parser->lexer)->u.value;
9337 default:
9338 return NULL_TREE;
9342 /* Parse an (optional) function-specifier.
9344 function-specifier:
9345 inline
9346 virtual
9347 explicit
9349 Returns an IDENTIFIER_NODE corresponding to the keyword used.
9350 Updates DECL_SPECS, if it is non-NULL. */
9352 static tree
9353 cp_parser_function_specifier_opt (cp_parser* parser,
9354 cp_decl_specifier_seq *decl_specs)
9356 cp_token *token = cp_lexer_peek_token (parser->lexer);
9357 switch (token->keyword)
9359 case RID_INLINE:
9360 if (decl_specs)
9361 ++decl_specs->specs[(int) ds_inline];
9362 break;
9364 case RID_VIRTUAL:
9365 /* 14.5.2.3 [temp.mem]
9367 A member function template shall not be virtual. */
9368 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
9369 error_at (token->location, "templates may not be %<virtual%>");
9370 else if (decl_specs)
9371 ++decl_specs->specs[(int) ds_virtual];
9372 break;
9374 case RID_EXPLICIT:
9375 if (decl_specs)
9376 ++decl_specs->specs[(int) ds_explicit];
9377 break;
9379 default:
9380 return NULL_TREE;
9383 /* Consume the token. */
9384 return cp_lexer_consume_token (parser->lexer)->u.value;
9387 /* Parse a linkage-specification.
9389 linkage-specification:
9390 extern string-literal { declaration-seq [opt] }
9391 extern string-literal declaration */
9393 static void
9394 cp_parser_linkage_specification (cp_parser* parser)
9396 tree linkage;
9398 /* Look for the `extern' keyword. */
9399 cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
9401 /* Look for the string-literal. */
9402 linkage = cp_parser_string_literal (parser, false, false);
9404 /* Transform the literal into an identifier. If the literal is a
9405 wide-character string, or contains embedded NULs, then we can't
9406 handle it as the user wants. */
9407 if (strlen (TREE_STRING_POINTER (linkage))
9408 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
9410 cp_parser_error (parser, "invalid linkage-specification");
9411 /* Assume C++ linkage. */
9412 linkage = lang_name_cplusplus;
9414 else
9415 linkage = get_identifier (TREE_STRING_POINTER (linkage));
9417 /* We're now using the new linkage. */
9418 push_lang_context (linkage);
9420 /* If the next token is a `{', then we're using the first
9421 production. */
9422 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9424 /* Consume the `{' token. */
9425 cp_lexer_consume_token (parser->lexer);
9426 /* Parse the declarations. */
9427 cp_parser_declaration_seq_opt (parser);
9428 /* Look for the closing `}'. */
9429 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
9431 /* Otherwise, there's just one declaration. */
9432 else
9434 bool saved_in_unbraced_linkage_specification_p;
9436 saved_in_unbraced_linkage_specification_p
9437 = parser->in_unbraced_linkage_specification_p;
9438 parser->in_unbraced_linkage_specification_p = true;
9439 cp_parser_declaration (parser);
9440 parser->in_unbraced_linkage_specification_p
9441 = saved_in_unbraced_linkage_specification_p;
9444 /* We're done with the linkage-specification. */
9445 pop_lang_context ();
9448 /* Parse a static_assert-declaration.
9450 static_assert-declaration:
9451 static_assert ( constant-expression , string-literal ) ;
9453 If MEMBER_P, this static_assert is a class member. */
9455 static void
9456 cp_parser_static_assert(cp_parser *parser, bool member_p)
9458 tree condition;
9459 tree message;
9460 cp_token *token;
9461 location_t saved_loc;
9463 /* Peek at the `static_assert' token so we can keep track of exactly
9464 where the static assertion started. */
9465 token = cp_lexer_peek_token (parser->lexer);
9466 saved_loc = token->location;
9468 /* Look for the `static_assert' keyword. */
9469 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
9470 "%<static_assert%>"))
9471 return;
9473 /* We know we are in a static assertion; commit to any tentative
9474 parse. */
9475 if (cp_parser_parsing_tentatively (parser))
9476 cp_parser_commit_to_tentative_parse (parser);
9478 /* Parse the `(' starting the static assertion condition. */
9479 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
9481 /* Parse the constant-expression. */
9482 condition =
9483 cp_parser_constant_expression (parser,
9484 /*allow_non_constant_p=*/false,
9485 /*non_constant_p=*/NULL);
9487 /* Parse the separating `,'. */
9488 cp_parser_require (parser, CPP_COMMA, "%<,%>");
9490 /* Parse the string-literal message. */
9491 message = cp_parser_string_literal (parser,
9492 /*translate=*/false,
9493 /*wide_ok=*/true);
9495 /* A `)' completes the static assertion. */
9496 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
9497 cp_parser_skip_to_closing_parenthesis (parser,
9498 /*recovering=*/true,
9499 /*or_comma=*/false,
9500 /*consume_paren=*/true);
9502 /* A semicolon terminates the declaration. */
9503 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
9505 /* Complete the static assertion, which may mean either processing
9506 the static assert now or saving it for template instantiation. */
9507 finish_static_assert (condition, message, saved_loc, member_p);
9510 /* Parse a `decltype' type. Returns the type.
9512 simple-type-specifier:
9513 decltype ( expression ) */
9515 static tree
9516 cp_parser_decltype (cp_parser *parser)
9518 tree expr;
9519 bool id_expression_or_member_access_p = false;
9520 const char *saved_message;
9521 bool saved_integral_constant_expression_p;
9522 bool saved_non_integral_constant_expression_p;
9523 cp_token *id_expr_start_token;
9525 /* Look for the `decltype' token. */
9526 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
9527 return error_mark_node;
9529 /* Types cannot be defined in a `decltype' expression. Save away the
9530 old message. */
9531 saved_message = parser->type_definition_forbidden_message;
9533 /* And create the new one. */
9534 parser->type_definition_forbidden_message
9535 = G_("types may not be defined in %<decltype%> expressions");
9537 /* The restrictions on constant-expressions do not apply inside
9538 decltype expressions. */
9539 saved_integral_constant_expression_p
9540 = parser->integral_constant_expression_p;
9541 saved_non_integral_constant_expression_p
9542 = parser->non_integral_constant_expression_p;
9543 parser->integral_constant_expression_p = false;
9545 /* Do not actually evaluate the expression. */
9546 ++cp_unevaluated_operand;
9548 /* Do not warn about problems with the expression. */
9549 ++c_inhibit_evaluation_warnings;
9551 /* Parse the opening `('. */
9552 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
9553 return error_mark_node;
9555 /* First, try parsing an id-expression. */
9556 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
9557 cp_parser_parse_tentatively (parser);
9558 expr = cp_parser_id_expression (parser,
9559 /*template_keyword_p=*/false,
9560 /*check_dependency_p=*/true,
9561 /*template_p=*/NULL,
9562 /*declarator_p=*/false,
9563 /*optional_p=*/false);
9565 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
9567 bool non_integral_constant_expression_p = false;
9568 tree id_expression = expr;
9569 cp_id_kind idk;
9570 const char *error_msg;
9572 if (TREE_CODE (expr) == IDENTIFIER_NODE)
9573 /* Lookup the name we got back from the id-expression. */
9574 expr = cp_parser_lookup_name (parser, expr,
9575 none_type,
9576 /*is_template=*/false,
9577 /*is_namespace=*/false,
9578 /*check_dependency=*/true,
9579 /*ambiguous_decls=*/NULL,
9580 id_expr_start_token->location);
9582 if (expr
9583 && expr != error_mark_node
9584 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
9585 && TREE_CODE (expr) != TYPE_DECL
9586 && (TREE_CODE (expr) != BIT_NOT_EXPR
9587 || !TYPE_P (TREE_OPERAND (expr, 0)))
9588 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9590 /* Complete lookup of the id-expression. */
9591 expr = (finish_id_expression
9592 (id_expression, expr, parser->scope, &idk,
9593 /*integral_constant_expression_p=*/false,
9594 /*allow_non_integral_constant_expression_p=*/true,
9595 &non_integral_constant_expression_p,
9596 /*template_p=*/false,
9597 /*done=*/true,
9598 /*address_p=*/false,
9599 /*template_arg_p=*/false,
9600 &error_msg,
9601 id_expr_start_token->location));
9603 if (expr == error_mark_node)
9604 /* We found an id-expression, but it was something that we
9605 should not have found. This is an error, not something
9606 we can recover from, so note that we found an
9607 id-expression and we'll recover as gracefully as
9608 possible. */
9609 id_expression_or_member_access_p = true;
9612 if (expr
9613 && expr != error_mark_node
9614 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9615 /* We have an id-expression. */
9616 id_expression_or_member_access_p = true;
9619 if (!id_expression_or_member_access_p)
9621 /* Abort the id-expression parse. */
9622 cp_parser_abort_tentative_parse (parser);
9624 /* Parsing tentatively, again. */
9625 cp_parser_parse_tentatively (parser);
9627 /* Parse a class member access. */
9628 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
9629 /*cast_p=*/false,
9630 /*member_access_only_p=*/true, NULL);
9632 if (expr
9633 && expr != error_mark_node
9634 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9635 /* We have an id-expression. */
9636 id_expression_or_member_access_p = true;
9639 if (id_expression_or_member_access_p)
9640 /* We have parsed the complete id-expression or member access. */
9641 cp_parser_parse_definitely (parser);
9642 else
9644 bool saved_greater_than_is_operator_p;
9646 /* Abort our attempt to parse an id-expression or member access
9647 expression. */
9648 cp_parser_abort_tentative_parse (parser);
9650 /* Within a parenthesized expression, a `>' token is always
9651 the greater-than operator. */
9652 saved_greater_than_is_operator_p
9653 = parser->greater_than_is_operator_p;
9654 parser->greater_than_is_operator_p = true;
9656 /* Parse a full expression. */
9657 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9659 /* The `>' token might be the end of a template-id or
9660 template-parameter-list now. */
9661 parser->greater_than_is_operator_p
9662 = saved_greater_than_is_operator_p;
9665 /* Go back to evaluating expressions. */
9666 --cp_unevaluated_operand;
9667 --c_inhibit_evaluation_warnings;
9669 /* Restore the old message and the integral constant expression
9670 flags. */
9671 parser->type_definition_forbidden_message = saved_message;
9672 parser->integral_constant_expression_p
9673 = saved_integral_constant_expression_p;
9674 parser->non_integral_constant_expression_p
9675 = saved_non_integral_constant_expression_p;
9677 if (expr == error_mark_node)
9679 /* Skip everything up to the closing `)'. */
9680 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9681 /*consume_paren=*/true);
9682 return error_mark_node;
9685 /* Parse to the closing `)'. */
9686 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
9688 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9689 /*consume_paren=*/true);
9690 return error_mark_node;
9693 return finish_decltype_type (expr, id_expression_or_member_access_p);
9696 /* Special member functions [gram.special] */
9698 /* Parse a conversion-function-id.
9700 conversion-function-id:
9701 operator conversion-type-id
9703 Returns an IDENTIFIER_NODE representing the operator. */
9705 static tree
9706 cp_parser_conversion_function_id (cp_parser* parser)
9708 tree type;
9709 tree saved_scope;
9710 tree saved_qualifying_scope;
9711 tree saved_object_scope;
9712 tree pushed_scope = NULL_TREE;
9714 /* Look for the `operator' token. */
9715 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9716 return error_mark_node;
9717 /* When we parse the conversion-type-id, the current scope will be
9718 reset. However, we need that information in able to look up the
9719 conversion function later, so we save it here. */
9720 saved_scope = parser->scope;
9721 saved_qualifying_scope = parser->qualifying_scope;
9722 saved_object_scope = parser->object_scope;
9723 /* We must enter the scope of the class so that the names of
9724 entities declared within the class are available in the
9725 conversion-type-id. For example, consider:
9727 struct S {
9728 typedef int I;
9729 operator I();
9732 S::operator I() { ... }
9734 In order to see that `I' is a type-name in the definition, we
9735 must be in the scope of `S'. */
9736 if (saved_scope)
9737 pushed_scope = push_scope (saved_scope);
9738 /* Parse the conversion-type-id. */
9739 type = cp_parser_conversion_type_id (parser);
9740 /* Leave the scope of the class, if any. */
9741 if (pushed_scope)
9742 pop_scope (pushed_scope);
9743 /* Restore the saved scope. */
9744 parser->scope = saved_scope;
9745 parser->qualifying_scope = saved_qualifying_scope;
9746 parser->object_scope = saved_object_scope;
9747 /* If the TYPE is invalid, indicate failure. */
9748 if (type == error_mark_node)
9749 return error_mark_node;
9750 return mangle_conv_op_name_for_type (type);
9753 /* Parse a conversion-type-id:
9755 conversion-type-id:
9756 type-specifier-seq conversion-declarator [opt]
9758 Returns the TYPE specified. */
9760 static tree
9761 cp_parser_conversion_type_id (cp_parser* parser)
9763 tree attributes;
9764 cp_decl_specifier_seq type_specifiers;
9765 cp_declarator *declarator;
9766 tree type_specified;
9768 /* Parse the attributes. */
9769 attributes = cp_parser_attributes_opt (parser);
9770 /* Parse the type-specifiers. */
9771 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
9772 /*is_trailing_return=*/false,
9773 &type_specifiers);
9774 /* If that didn't work, stop. */
9775 if (type_specifiers.type == error_mark_node)
9776 return error_mark_node;
9777 /* Parse the conversion-declarator. */
9778 declarator = cp_parser_conversion_declarator_opt (parser);
9780 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
9781 /*initialized=*/0, &attributes);
9782 if (attributes)
9783 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
9785 /* Don't give this error when parsing tentatively. This happens to
9786 work because we always parse this definitively once. */
9787 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
9788 && type_uses_auto (type_specified))
9790 error ("invalid use of %<auto%> in conversion operator");
9791 return error_mark_node;
9794 return type_specified;
9797 /* Parse an (optional) conversion-declarator.
9799 conversion-declarator:
9800 ptr-operator conversion-declarator [opt]
9804 static cp_declarator *
9805 cp_parser_conversion_declarator_opt (cp_parser* parser)
9807 enum tree_code code;
9808 tree class_type;
9809 cp_cv_quals cv_quals;
9811 /* We don't know if there's a ptr-operator next, or not. */
9812 cp_parser_parse_tentatively (parser);
9813 /* Try the ptr-operator. */
9814 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
9815 /* If it worked, look for more conversion-declarators. */
9816 if (cp_parser_parse_definitely (parser))
9818 cp_declarator *declarator;
9820 /* Parse another optional declarator. */
9821 declarator = cp_parser_conversion_declarator_opt (parser);
9823 return cp_parser_make_indirect_declarator
9824 (code, class_type, cv_quals, declarator);
9827 return NULL;
9830 /* Parse an (optional) ctor-initializer.
9832 ctor-initializer:
9833 : mem-initializer-list
9835 Returns TRUE iff the ctor-initializer was actually present. */
9837 static bool
9838 cp_parser_ctor_initializer_opt (cp_parser* parser)
9840 /* If the next token is not a `:', then there is no
9841 ctor-initializer. */
9842 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
9844 /* Do default initialization of any bases and members. */
9845 if (DECL_CONSTRUCTOR_P (current_function_decl))
9846 finish_mem_initializers (NULL_TREE);
9848 return false;
9851 /* Consume the `:' token. */
9852 cp_lexer_consume_token (parser->lexer);
9853 /* And the mem-initializer-list. */
9854 cp_parser_mem_initializer_list (parser);
9856 return true;
9859 /* Parse a mem-initializer-list.
9861 mem-initializer-list:
9862 mem-initializer ... [opt]
9863 mem-initializer ... [opt] , mem-initializer-list */
9865 static void
9866 cp_parser_mem_initializer_list (cp_parser* parser)
9868 tree mem_initializer_list = NULL_TREE;
9869 cp_token *token = cp_lexer_peek_token (parser->lexer);
9871 /* Let the semantic analysis code know that we are starting the
9872 mem-initializer-list. */
9873 if (!DECL_CONSTRUCTOR_P (current_function_decl))
9874 error_at (token->location,
9875 "only constructors take base initializers");
9877 /* Loop through the list. */
9878 while (true)
9880 tree mem_initializer;
9882 token = cp_lexer_peek_token (parser->lexer);
9883 /* Parse the mem-initializer. */
9884 mem_initializer = cp_parser_mem_initializer (parser);
9885 /* If the next token is a `...', we're expanding member initializers. */
9886 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9888 /* Consume the `...'. */
9889 cp_lexer_consume_token (parser->lexer);
9891 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9892 can be expanded but members cannot. */
9893 if (mem_initializer != error_mark_node
9894 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9896 error_at (token->location,
9897 "cannot expand initializer for member %<%D%>",
9898 TREE_PURPOSE (mem_initializer));
9899 mem_initializer = error_mark_node;
9902 /* Construct the pack expansion type. */
9903 if (mem_initializer != error_mark_node)
9904 mem_initializer = make_pack_expansion (mem_initializer);
9906 /* Add it to the list, unless it was erroneous. */
9907 if (mem_initializer != error_mark_node)
9909 TREE_CHAIN (mem_initializer) = mem_initializer_list;
9910 mem_initializer_list = mem_initializer;
9912 /* If the next token is not a `,', we're done. */
9913 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9914 break;
9915 /* Consume the `,' token. */
9916 cp_lexer_consume_token (parser->lexer);
9919 /* Perform semantic analysis. */
9920 if (DECL_CONSTRUCTOR_P (current_function_decl))
9921 finish_mem_initializers (mem_initializer_list);
9924 /* Parse a mem-initializer.
9926 mem-initializer:
9927 mem-initializer-id ( expression-list [opt] )
9928 mem-initializer-id braced-init-list
9930 GNU extension:
9932 mem-initializer:
9933 ( expression-list [opt] )
9935 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
9936 class) or FIELD_DECL (for a non-static data member) to initialize;
9937 the TREE_VALUE is the expression-list. An empty initialization
9938 list is represented by void_list_node. */
9940 static tree
9941 cp_parser_mem_initializer (cp_parser* parser)
9943 tree mem_initializer_id;
9944 tree expression_list;
9945 tree member;
9946 cp_token *token = cp_lexer_peek_token (parser->lexer);
9948 /* Find out what is being initialized. */
9949 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9951 permerror (token->location,
9952 "anachronistic old-style base class initializer");
9953 mem_initializer_id = NULL_TREE;
9955 else
9957 mem_initializer_id = cp_parser_mem_initializer_id (parser);
9958 if (mem_initializer_id == error_mark_node)
9959 return mem_initializer_id;
9961 member = expand_member_init (mem_initializer_id);
9962 if (member && !DECL_P (member))
9963 in_base_initializer = 1;
9965 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9967 bool expr_non_constant_p;
9968 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9969 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9970 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9971 expression_list = build_tree_list (NULL_TREE, expression_list);
9973 else
9975 VEC(tree,gc)* vec;
9976 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
9977 /*cast_p=*/false,
9978 /*allow_expansion_p=*/true,
9979 /*non_constant_p=*/NULL);
9980 if (vec == NULL)
9981 return error_mark_node;
9982 expression_list = build_tree_list_vec (vec);
9983 release_tree_vector (vec);
9986 if (expression_list == error_mark_node)
9987 return error_mark_node;
9988 if (!expression_list)
9989 expression_list = void_type_node;
9991 in_base_initializer = 0;
9993 return member ? build_tree_list (member, expression_list) : error_mark_node;
9996 /* Parse a mem-initializer-id.
9998 mem-initializer-id:
9999 :: [opt] nested-name-specifier [opt] class-name
10000 identifier
10002 Returns a TYPE indicating the class to be initializer for the first
10003 production. Returns an IDENTIFIER_NODE indicating the data member
10004 to be initialized for the second production. */
10006 static tree
10007 cp_parser_mem_initializer_id (cp_parser* parser)
10009 bool global_scope_p;
10010 bool nested_name_specifier_p;
10011 bool template_p = false;
10012 tree id;
10014 cp_token *token = cp_lexer_peek_token (parser->lexer);
10016 /* `typename' is not allowed in this context ([temp.res]). */
10017 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
10019 error_at (token->location,
10020 "keyword %<typename%> not allowed in this context (a qualified "
10021 "member initializer is implicitly a type)");
10022 cp_lexer_consume_token (parser->lexer);
10024 /* Look for the optional `::' operator. */
10025 global_scope_p
10026 = (cp_parser_global_scope_opt (parser,
10027 /*current_scope_valid_p=*/false)
10028 != NULL_TREE);
10029 /* Look for the optional nested-name-specifier. The simplest way to
10030 implement:
10032 [temp.res]
10034 The keyword `typename' is not permitted in a base-specifier or
10035 mem-initializer; in these contexts a qualified name that
10036 depends on a template-parameter is implicitly assumed to be a
10037 type name.
10039 is to assume that we have seen the `typename' keyword at this
10040 point. */
10041 nested_name_specifier_p
10042 = (cp_parser_nested_name_specifier_opt (parser,
10043 /*typename_keyword_p=*/true,
10044 /*check_dependency_p=*/true,
10045 /*type_p=*/true,
10046 /*is_declaration=*/true)
10047 != NULL_TREE);
10048 if (nested_name_specifier_p)
10049 template_p = cp_parser_optional_template_keyword (parser);
10050 /* If there is a `::' operator or a nested-name-specifier, then we
10051 are definitely looking for a class-name. */
10052 if (global_scope_p || nested_name_specifier_p)
10053 return cp_parser_class_name (parser,
10054 /*typename_keyword_p=*/true,
10055 /*template_keyword_p=*/template_p,
10056 typename_type,
10057 /*check_dependency_p=*/true,
10058 /*class_head_p=*/false,
10059 /*is_declaration=*/true);
10060 /* Otherwise, we could also be looking for an ordinary identifier. */
10061 cp_parser_parse_tentatively (parser);
10062 /* Try a class-name. */
10063 id = cp_parser_class_name (parser,
10064 /*typename_keyword_p=*/true,
10065 /*template_keyword_p=*/false,
10066 none_type,
10067 /*check_dependency_p=*/true,
10068 /*class_head_p=*/false,
10069 /*is_declaration=*/true);
10070 /* If we found one, we're done. */
10071 if (cp_parser_parse_definitely (parser))
10072 return id;
10073 /* Otherwise, look for an ordinary identifier. */
10074 return cp_parser_identifier (parser);
10077 /* Overloading [gram.over] */
10079 /* Parse an operator-function-id.
10081 operator-function-id:
10082 operator operator
10084 Returns an IDENTIFIER_NODE for the operator which is a
10085 human-readable spelling of the identifier, e.g., `operator +'. */
10087 static tree
10088 cp_parser_operator_function_id (cp_parser* parser)
10090 /* Look for the `operator' keyword. */
10091 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
10092 return error_mark_node;
10093 /* And then the name of the operator itself. */
10094 return cp_parser_operator (parser);
10097 /* Parse an operator.
10099 operator:
10100 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10101 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10102 || ++ -- , ->* -> () []
10104 GNU Extensions:
10106 operator:
10107 <? >? <?= >?=
10109 Returns an IDENTIFIER_NODE for the operator which is a
10110 human-readable spelling of the identifier, e.g., `operator +'. */
10112 static tree
10113 cp_parser_operator (cp_parser* parser)
10115 tree id = NULL_TREE;
10116 cp_token *token;
10118 /* Peek at the next token. */
10119 token = cp_lexer_peek_token (parser->lexer);
10120 /* Figure out which operator we have. */
10121 switch (token->type)
10123 case CPP_KEYWORD:
10125 enum tree_code op;
10127 /* The keyword should be either `new' or `delete'. */
10128 if (token->keyword == RID_NEW)
10129 op = NEW_EXPR;
10130 else if (token->keyword == RID_DELETE)
10131 op = DELETE_EXPR;
10132 else
10133 break;
10135 /* Consume the `new' or `delete' token. */
10136 cp_lexer_consume_token (parser->lexer);
10138 /* Peek at the next token. */
10139 token = cp_lexer_peek_token (parser->lexer);
10140 /* If it's a `[' token then this is the array variant of the
10141 operator. */
10142 if (token->type == CPP_OPEN_SQUARE)
10144 /* Consume the `[' token. */
10145 cp_lexer_consume_token (parser->lexer);
10146 /* Look for the `]' token. */
10147 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
10148 id = ansi_opname (op == NEW_EXPR
10149 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10151 /* Otherwise, we have the non-array variant. */
10152 else
10153 id = ansi_opname (op);
10155 return id;
10158 case CPP_PLUS:
10159 id = ansi_opname (PLUS_EXPR);
10160 break;
10162 case CPP_MINUS:
10163 id = ansi_opname (MINUS_EXPR);
10164 break;
10166 case CPP_MULT:
10167 id = ansi_opname (MULT_EXPR);
10168 break;
10170 case CPP_DIV:
10171 id = ansi_opname (TRUNC_DIV_EXPR);
10172 break;
10174 case CPP_MOD:
10175 id = ansi_opname (TRUNC_MOD_EXPR);
10176 break;
10178 case CPP_XOR:
10179 id = ansi_opname (BIT_XOR_EXPR);
10180 break;
10182 case CPP_AND:
10183 id = ansi_opname (BIT_AND_EXPR);
10184 break;
10186 case CPP_OR:
10187 id = ansi_opname (BIT_IOR_EXPR);
10188 break;
10190 case CPP_COMPL:
10191 id = ansi_opname (BIT_NOT_EXPR);
10192 break;
10194 case CPP_NOT:
10195 id = ansi_opname (TRUTH_NOT_EXPR);
10196 break;
10198 case CPP_EQ:
10199 id = ansi_assopname (NOP_EXPR);
10200 break;
10202 case CPP_LESS:
10203 id = ansi_opname (LT_EXPR);
10204 break;
10206 case CPP_GREATER:
10207 id = ansi_opname (GT_EXPR);
10208 break;
10210 case CPP_PLUS_EQ:
10211 id = ansi_assopname (PLUS_EXPR);
10212 break;
10214 case CPP_MINUS_EQ:
10215 id = ansi_assopname (MINUS_EXPR);
10216 break;
10218 case CPP_MULT_EQ:
10219 id = ansi_assopname (MULT_EXPR);
10220 break;
10222 case CPP_DIV_EQ:
10223 id = ansi_assopname (TRUNC_DIV_EXPR);
10224 break;
10226 case CPP_MOD_EQ:
10227 id = ansi_assopname (TRUNC_MOD_EXPR);
10228 break;
10230 case CPP_XOR_EQ:
10231 id = ansi_assopname (BIT_XOR_EXPR);
10232 break;
10234 case CPP_AND_EQ:
10235 id = ansi_assopname (BIT_AND_EXPR);
10236 break;
10238 case CPP_OR_EQ:
10239 id = ansi_assopname (BIT_IOR_EXPR);
10240 break;
10242 case CPP_LSHIFT:
10243 id = ansi_opname (LSHIFT_EXPR);
10244 break;
10246 case CPP_RSHIFT:
10247 id = ansi_opname (RSHIFT_EXPR);
10248 break;
10250 case CPP_LSHIFT_EQ:
10251 id = ansi_assopname (LSHIFT_EXPR);
10252 break;
10254 case CPP_RSHIFT_EQ:
10255 id = ansi_assopname (RSHIFT_EXPR);
10256 break;
10258 case CPP_EQ_EQ:
10259 id = ansi_opname (EQ_EXPR);
10260 break;
10262 case CPP_NOT_EQ:
10263 id = ansi_opname (NE_EXPR);
10264 break;
10266 case CPP_LESS_EQ:
10267 id = ansi_opname (LE_EXPR);
10268 break;
10270 case CPP_GREATER_EQ:
10271 id = ansi_opname (GE_EXPR);
10272 break;
10274 case CPP_AND_AND:
10275 id = ansi_opname (TRUTH_ANDIF_EXPR);
10276 break;
10278 case CPP_OR_OR:
10279 id = ansi_opname (TRUTH_ORIF_EXPR);
10280 break;
10282 case CPP_PLUS_PLUS:
10283 id = ansi_opname (POSTINCREMENT_EXPR);
10284 break;
10286 case CPP_MINUS_MINUS:
10287 id = ansi_opname (PREDECREMENT_EXPR);
10288 break;
10290 case CPP_COMMA:
10291 id = ansi_opname (COMPOUND_EXPR);
10292 break;
10294 case CPP_DEREF_STAR:
10295 id = ansi_opname (MEMBER_REF);
10296 break;
10298 case CPP_DEREF:
10299 id = ansi_opname (COMPONENT_REF);
10300 break;
10302 case CPP_OPEN_PAREN:
10303 /* Consume the `('. */
10304 cp_lexer_consume_token (parser->lexer);
10305 /* Look for the matching `)'. */
10306 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
10307 return ansi_opname (CALL_EXPR);
10309 case CPP_OPEN_SQUARE:
10310 /* Consume the `['. */
10311 cp_lexer_consume_token (parser->lexer);
10312 /* Look for the matching `]'. */
10313 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
10314 return ansi_opname (ARRAY_REF);
10316 default:
10317 /* Anything else is an error. */
10318 break;
10321 /* If we have selected an identifier, we need to consume the
10322 operator token. */
10323 if (id)
10324 cp_lexer_consume_token (parser->lexer);
10325 /* Otherwise, no valid operator name was present. */
10326 else
10328 cp_parser_error (parser, "expected operator");
10329 id = error_mark_node;
10332 return id;
10335 /* Parse a template-declaration.
10337 template-declaration:
10338 export [opt] template < template-parameter-list > declaration
10340 If MEMBER_P is TRUE, this template-declaration occurs within a
10341 class-specifier.
10343 The grammar rule given by the standard isn't correct. What
10344 is really meant is:
10346 template-declaration:
10347 export [opt] template-parameter-list-seq
10348 decl-specifier-seq [opt] init-declarator [opt] ;
10349 export [opt] template-parameter-list-seq
10350 function-definition
10352 template-parameter-list-seq:
10353 template-parameter-list-seq [opt]
10354 template < template-parameter-list > */
10356 static void
10357 cp_parser_template_declaration (cp_parser* parser, bool member_p)
10359 /* Check for `export'. */
10360 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
10362 /* Consume the `export' token. */
10363 cp_lexer_consume_token (parser->lexer);
10364 /* Warn that we do not support `export'. */
10365 warning (0, "keyword %<export%> not implemented, and will be ignored");
10368 cp_parser_template_declaration_after_export (parser, member_p);
10371 /* Parse a template-parameter-list.
10373 template-parameter-list:
10374 template-parameter
10375 template-parameter-list , template-parameter
10377 Returns a TREE_LIST. Each node represents a template parameter.
10378 The nodes are connected via their TREE_CHAINs. */
10380 static tree
10381 cp_parser_template_parameter_list (cp_parser* parser)
10383 tree parameter_list = NULL_TREE;
10385 begin_template_parm_list ();
10386 while (true)
10388 tree parameter;
10389 bool is_non_type;
10390 bool is_parameter_pack;
10391 location_t parm_loc;
10393 /* Parse the template-parameter. */
10394 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
10395 parameter = cp_parser_template_parameter (parser,
10396 &is_non_type,
10397 &is_parameter_pack);
10398 /* Add it to the list. */
10399 if (parameter != error_mark_node)
10400 parameter_list = process_template_parm (parameter_list,
10401 parm_loc,
10402 parameter,
10403 is_non_type,
10404 is_parameter_pack);
10405 else
10407 tree err_parm = build_tree_list (parameter, parameter);
10408 TREE_VALUE (err_parm) = error_mark_node;
10409 parameter_list = chainon (parameter_list, err_parm);
10412 /* If the next token is not a `,', we're done. */
10413 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10414 break;
10415 /* Otherwise, consume the `,' token. */
10416 cp_lexer_consume_token (parser->lexer);
10419 return end_template_parm_list (parameter_list);
10422 /* Parse a template-parameter.
10424 template-parameter:
10425 type-parameter
10426 parameter-declaration
10428 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
10429 the parameter. The TREE_PURPOSE is the default value, if any.
10430 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
10431 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
10432 set to true iff this parameter is a parameter pack. */
10434 static tree
10435 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
10436 bool *is_parameter_pack)
10438 cp_token *token;
10439 cp_parameter_declarator *parameter_declarator;
10440 cp_declarator *id_declarator;
10441 tree parm;
10443 /* Assume it is a type parameter or a template parameter. */
10444 *is_non_type = false;
10445 /* Assume it not a parameter pack. */
10446 *is_parameter_pack = false;
10447 /* Peek at the next token. */
10448 token = cp_lexer_peek_token (parser->lexer);
10449 /* If it is `class' or `template', we have a type-parameter. */
10450 if (token->keyword == RID_TEMPLATE)
10451 return cp_parser_type_parameter (parser, is_parameter_pack);
10452 /* If it is `class' or `typename' we do not know yet whether it is a
10453 type parameter or a non-type parameter. Consider:
10455 template <typename T, typename T::X X> ...
10459 template <class C, class D*> ...
10461 Here, the first parameter is a type parameter, and the second is
10462 a non-type parameter. We can tell by looking at the token after
10463 the identifier -- if it is a `,', `=', or `>' then we have a type
10464 parameter. */
10465 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
10467 /* Peek at the token after `class' or `typename'. */
10468 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10469 /* If it's an ellipsis, we have a template type parameter
10470 pack. */
10471 if (token->type == CPP_ELLIPSIS)
10472 return cp_parser_type_parameter (parser, is_parameter_pack);
10473 /* If it's an identifier, skip it. */
10474 if (token->type == CPP_NAME)
10475 token = cp_lexer_peek_nth_token (parser->lexer, 3);
10476 /* Now, see if the token looks like the end of a template
10477 parameter. */
10478 if (token->type == CPP_COMMA
10479 || token->type == CPP_EQ
10480 || token->type == CPP_GREATER)
10481 return cp_parser_type_parameter (parser, is_parameter_pack);
10484 /* Otherwise, it is a non-type parameter.
10486 [temp.param]
10488 When parsing a default template-argument for a non-type
10489 template-parameter, the first non-nested `>' is taken as the end
10490 of the template parameter-list rather than a greater-than
10491 operator. */
10492 *is_non_type = true;
10493 parameter_declarator
10494 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
10495 /*parenthesized_p=*/NULL);
10497 /* If the parameter declaration is marked as a parameter pack, set
10498 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
10499 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
10500 grokdeclarator. */
10501 if (parameter_declarator
10502 && parameter_declarator->declarator
10503 && parameter_declarator->declarator->parameter_pack_p)
10505 *is_parameter_pack = true;
10506 parameter_declarator->declarator->parameter_pack_p = false;
10509 /* If the next token is an ellipsis, and we don't already have it
10510 marked as a parameter pack, then we have a parameter pack (that
10511 has no declarator). */
10512 if (!*is_parameter_pack
10513 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
10514 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
10516 /* Consume the `...'. */
10517 cp_lexer_consume_token (parser->lexer);
10518 maybe_warn_variadic_templates ();
10520 *is_parameter_pack = true;
10522 /* We might end up with a pack expansion as the type of the non-type
10523 template parameter, in which case this is a non-type template
10524 parameter pack. */
10525 else if (parameter_declarator
10526 && parameter_declarator->decl_specifiers.type
10527 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
10529 *is_parameter_pack = true;
10530 parameter_declarator->decl_specifiers.type =
10531 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
10534 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10536 /* Parameter packs cannot have default arguments. However, a
10537 user may try to do so, so we'll parse them and give an
10538 appropriate diagnostic here. */
10540 /* Consume the `='. */
10541 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
10542 cp_lexer_consume_token (parser->lexer);
10544 /* Find the name of the parameter pack. */
10545 id_declarator = parameter_declarator->declarator;
10546 while (id_declarator && id_declarator->kind != cdk_id)
10547 id_declarator = id_declarator->declarator;
10549 if (id_declarator && id_declarator->kind == cdk_id)
10550 error_at (start_token->location,
10551 "template parameter pack %qD cannot have a default argument",
10552 id_declarator->u.id.unqualified_name);
10553 else
10554 error_at (start_token->location,
10555 "template parameter pack cannot have a default argument");
10557 /* Parse the default argument, but throw away the result. */
10558 cp_parser_default_argument (parser, /*template_parm_p=*/true);
10561 parm = grokdeclarator (parameter_declarator->declarator,
10562 &parameter_declarator->decl_specifiers,
10563 TPARM, /*initialized=*/0,
10564 /*attrlist=*/NULL);
10565 if (parm == error_mark_node)
10566 return error_mark_node;
10568 return build_tree_list (parameter_declarator->default_argument, parm);
10571 /* Parse a type-parameter.
10573 type-parameter:
10574 class identifier [opt]
10575 class identifier [opt] = type-id
10576 typename identifier [opt]
10577 typename identifier [opt] = type-id
10578 template < template-parameter-list > class identifier [opt]
10579 template < template-parameter-list > class identifier [opt]
10580 = id-expression
10582 GNU Extension (variadic templates):
10584 type-parameter:
10585 class ... identifier [opt]
10586 typename ... identifier [opt]
10588 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
10589 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
10590 the declaration of the parameter.
10592 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
10594 static tree
10595 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
10597 cp_token *token;
10598 tree parameter;
10600 /* Look for a keyword to tell us what kind of parameter this is. */
10601 token = cp_parser_require (parser, CPP_KEYWORD,
10602 "%<class%>, %<typename%>, or %<template%>");
10603 if (!token)
10604 return error_mark_node;
10606 switch (token->keyword)
10608 case RID_CLASS:
10609 case RID_TYPENAME:
10611 tree identifier;
10612 tree default_argument;
10614 /* If the next token is an ellipsis, we have a template
10615 argument pack. */
10616 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10618 /* Consume the `...' token. */
10619 cp_lexer_consume_token (parser->lexer);
10620 maybe_warn_variadic_templates ();
10622 *is_parameter_pack = true;
10625 /* If the next token is an identifier, then it names the
10626 parameter. */
10627 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10628 identifier = cp_parser_identifier (parser);
10629 else
10630 identifier = NULL_TREE;
10632 /* Create the parameter. */
10633 parameter = finish_template_type_parm (class_type_node, identifier);
10635 /* If the next token is an `=', we have a default argument. */
10636 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10638 /* Consume the `=' token. */
10639 cp_lexer_consume_token (parser->lexer);
10640 /* Parse the default-argument. */
10641 push_deferring_access_checks (dk_no_deferred);
10642 default_argument = cp_parser_type_id (parser);
10644 /* Template parameter packs cannot have default
10645 arguments. */
10646 if (*is_parameter_pack)
10648 if (identifier)
10649 error_at (token->location,
10650 "template parameter pack %qD cannot have a "
10651 "default argument", identifier);
10652 else
10653 error_at (token->location,
10654 "template parameter packs cannot have "
10655 "default arguments");
10656 default_argument = NULL_TREE;
10658 pop_deferring_access_checks ();
10660 else
10661 default_argument = NULL_TREE;
10663 /* Create the combined representation of the parameter and the
10664 default argument. */
10665 parameter = build_tree_list (default_argument, parameter);
10667 break;
10669 case RID_TEMPLATE:
10671 tree identifier;
10672 tree default_argument;
10674 /* Look for the `<'. */
10675 cp_parser_require (parser, CPP_LESS, "%<<%>");
10676 /* Parse the template-parameter-list. */
10677 cp_parser_template_parameter_list (parser);
10678 /* Look for the `>'. */
10679 cp_parser_require (parser, CPP_GREATER, "%<>%>");
10680 /* Look for the `class' keyword. */
10681 cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
10682 /* If the next token is an ellipsis, we have a template
10683 argument pack. */
10684 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10686 /* Consume the `...' token. */
10687 cp_lexer_consume_token (parser->lexer);
10688 maybe_warn_variadic_templates ();
10690 *is_parameter_pack = true;
10692 /* If the next token is an `=', then there is a
10693 default-argument. If the next token is a `>', we are at
10694 the end of the parameter-list. If the next token is a `,',
10695 then we are at the end of this parameter. */
10696 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10697 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
10698 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10700 identifier = cp_parser_identifier (parser);
10701 /* Treat invalid names as if the parameter were nameless. */
10702 if (identifier == error_mark_node)
10703 identifier = NULL_TREE;
10705 else
10706 identifier = NULL_TREE;
10708 /* Create the template parameter. */
10709 parameter = finish_template_template_parm (class_type_node,
10710 identifier);
10712 /* If the next token is an `=', then there is a
10713 default-argument. */
10714 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10716 bool is_template;
10718 /* Consume the `='. */
10719 cp_lexer_consume_token (parser->lexer);
10720 /* Parse the id-expression. */
10721 push_deferring_access_checks (dk_no_deferred);
10722 /* save token before parsing the id-expression, for error
10723 reporting */
10724 token = cp_lexer_peek_token (parser->lexer);
10725 default_argument
10726 = cp_parser_id_expression (parser,
10727 /*template_keyword_p=*/false,
10728 /*check_dependency_p=*/true,
10729 /*template_p=*/&is_template,
10730 /*declarator_p=*/false,
10731 /*optional_p=*/false);
10732 if (TREE_CODE (default_argument) == TYPE_DECL)
10733 /* If the id-expression was a template-id that refers to
10734 a template-class, we already have the declaration here,
10735 so no further lookup is needed. */
10737 else
10738 /* Look up the name. */
10739 default_argument
10740 = cp_parser_lookup_name (parser, default_argument,
10741 none_type,
10742 /*is_template=*/is_template,
10743 /*is_namespace=*/false,
10744 /*check_dependency=*/true,
10745 /*ambiguous_decls=*/NULL,
10746 token->location);
10747 /* See if the default argument is valid. */
10748 default_argument
10749 = check_template_template_default_arg (default_argument);
10751 /* Template parameter packs cannot have default
10752 arguments. */
10753 if (*is_parameter_pack)
10755 if (identifier)
10756 error_at (token->location,
10757 "template parameter pack %qD cannot "
10758 "have a default argument",
10759 identifier);
10760 else
10761 error_at (token->location, "template parameter packs cannot "
10762 "have default arguments");
10763 default_argument = NULL_TREE;
10765 pop_deferring_access_checks ();
10767 else
10768 default_argument = NULL_TREE;
10770 /* Create the combined representation of the parameter and the
10771 default argument. */
10772 parameter = build_tree_list (default_argument, parameter);
10774 break;
10776 default:
10777 gcc_unreachable ();
10778 break;
10781 return parameter;
10784 /* Parse a template-id.
10786 template-id:
10787 template-name < template-argument-list [opt] >
10789 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
10790 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
10791 returned. Otherwise, if the template-name names a function, or set
10792 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
10793 names a class, returns a TYPE_DECL for the specialization.
10795 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
10796 uninstantiated templates. */
10798 static tree
10799 cp_parser_template_id (cp_parser *parser,
10800 bool template_keyword_p,
10801 bool check_dependency_p,
10802 bool is_declaration)
10804 int i;
10805 tree templ;
10806 tree arguments;
10807 tree template_id;
10808 cp_token_position start_of_id = 0;
10809 deferred_access_check *chk;
10810 VEC (deferred_access_check,gc) *access_check;
10811 cp_token *next_token = NULL, *next_token_2 = NULL;
10812 bool is_identifier;
10814 /* If the next token corresponds to a template-id, there is no need
10815 to reparse it. */
10816 next_token = cp_lexer_peek_token (parser->lexer);
10817 if (next_token->type == CPP_TEMPLATE_ID)
10819 struct tree_check *check_value;
10821 /* Get the stored value. */
10822 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
10823 /* Perform any access checks that were deferred. */
10824 access_check = check_value->checks;
10825 if (access_check)
10827 for (i = 0 ;
10828 VEC_iterate (deferred_access_check, access_check, i, chk) ;
10829 ++i)
10831 perform_or_defer_access_check (chk->binfo,
10832 chk->decl,
10833 chk->diag_decl);
10836 /* Return the stored value. */
10837 return check_value->value;
10840 /* Avoid performing name lookup if there is no possibility of
10841 finding a template-id. */
10842 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
10843 || (next_token->type == CPP_NAME
10844 && !cp_parser_nth_token_starts_template_argument_list_p
10845 (parser, 2)))
10847 cp_parser_error (parser, "expected template-id");
10848 return error_mark_node;
10851 /* Remember where the template-id starts. */
10852 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
10853 start_of_id = cp_lexer_token_position (parser->lexer, false);
10855 push_deferring_access_checks (dk_deferred);
10857 /* Parse the template-name. */
10858 is_identifier = false;
10859 templ = cp_parser_template_name (parser, template_keyword_p,
10860 check_dependency_p,
10861 is_declaration,
10862 &is_identifier);
10863 if (templ == error_mark_node || is_identifier)
10865 pop_deferring_access_checks ();
10866 return templ;
10869 /* If we find the sequence `[:' after a template-name, it's probably
10870 a digraph-typo for `< ::'. Substitute the tokens and check if we can
10871 parse correctly the argument list. */
10872 next_token = cp_lexer_peek_token (parser->lexer);
10873 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10874 if (next_token->type == CPP_OPEN_SQUARE
10875 && next_token->flags & DIGRAPH
10876 && next_token_2->type == CPP_COLON
10877 && !(next_token_2->flags & PREV_WHITE))
10879 cp_parser_parse_tentatively (parser);
10880 /* Change `:' into `::'. */
10881 next_token_2->type = CPP_SCOPE;
10882 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
10883 CPP_LESS. */
10884 cp_lexer_consume_token (parser->lexer);
10886 /* Parse the arguments. */
10887 arguments = cp_parser_enclosed_template_argument_list (parser);
10888 if (!cp_parser_parse_definitely (parser))
10890 /* If we couldn't parse an argument list, then we revert our changes
10891 and return simply an error. Maybe this is not a template-id
10892 after all. */
10893 next_token_2->type = CPP_COLON;
10894 cp_parser_error (parser, "expected %<<%>");
10895 pop_deferring_access_checks ();
10896 return error_mark_node;
10898 /* Otherwise, emit an error about the invalid digraph, but continue
10899 parsing because we got our argument list. */
10900 if (permerror (next_token->location,
10901 "%<<::%> cannot begin a template-argument list"))
10903 static bool hint = false;
10904 inform (next_token->location,
10905 "%<<:%> is an alternate spelling for %<[%>."
10906 " Insert whitespace between %<<%> and %<::%>");
10907 if (!hint && !flag_permissive)
10909 inform (next_token->location, "(if you use %<-fpermissive%>"
10910 " G++ will accept your code)");
10911 hint = true;
10915 else
10917 /* Look for the `<' that starts the template-argument-list. */
10918 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10920 pop_deferring_access_checks ();
10921 return error_mark_node;
10923 /* Parse the arguments. */
10924 arguments = cp_parser_enclosed_template_argument_list (parser);
10927 /* Build a representation of the specialization. */
10928 if (TREE_CODE (templ) == IDENTIFIER_NODE)
10929 template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10930 else if (DECL_CLASS_TEMPLATE_P (templ)
10931 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10933 bool entering_scope;
10934 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10935 template (rather than some instantiation thereof) only if
10936 is not nested within some other construct. For example, in
10937 "template <typename T> void f(T) { A<T>::", A<T> is just an
10938 instantiation of A. */
10939 entering_scope = (template_parm_scope_p ()
10940 && cp_lexer_next_token_is (parser->lexer,
10941 CPP_SCOPE));
10942 template_id
10943 = finish_template_type (templ, arguments, entering_scope);
10945 else
10947 /* If it's not a class-template or a template-template, it should be
10948 a function-template. */
10949 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10950 || TREE_CODE (templ) == OVERLOAD
10951 || BASELINK_P (templ)));
10953 template_id = lookup_template_function (templ, arguments);
10956 /* If parsing tentatively, replace the sequence of tokens that makes
10957 up the template-id with a CPP_TEMPLATE_ID token. That way,
10958 should we re-parse the token stream, we will not have to repeat
10959 the effort required to do the parse, nor will we issue duplicate
10960 error messages about problems during instantiation of the
10961 template. */
10962 if (start_of_id)
10964 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10966 /* Reset the contents of the START_OF_ID token. */
10967 token->type = CPP_TEMPLATE_ID;
10968 /* Retrieve any deferred checks. Do not pop this access checks yet
10969 so the memory will not be reclaimed during token replacing below. */
10970 token->u.tree_check_value = GGC_CNEW (struct tree_check);
10971 token->u.tree_check_value->value = template_id;
10972 token->u.tree_check_value->checks = get_deferred_access_checks ();
10973 token->keyword = RID_MAX;
10975 /* Purge all subsequent tokens. */
10976 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10978 /* ??? Can we actually assume that, if template_id ==
10979 error_mark_node, we will have issued a diagnostic to the
10980 user, as opposed to simply marking the tentative parse as
10981 failed? */
10982 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10983 error_at (token->location, "parse error in template argument list");
10986 pop_deferring_access_checks ();
10987 return template_id;
10990 /* Parse a template-name.
10992 template-name:
10993 identifier
10995 The standard should actually say:
10997 template-name:
10998 identifier
10999 operator-function-id
11001 A defect report has been filed about this issue.
11003 A conversion-function-id cannot be a template name because they cannot
11004 be part of a template-id. In fact, looking at this code:
11006 a.operator K<int>()
11008 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
11009 It is impossible to call a templated conversion-function-id with an
11010 explicit argument list, since the only allowed template parameter is
11011 the type to which it is converting.
11013 If TEMPLATE_KEYWORD_P is true, then we have just seen the
11014 `template' keyword, in a construction like:
11016 T::template f<3>()
11018 In that case `f' is taken to be a template-name, even though there
11019 is no way of knowing for sure.
11021 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
11022 name refers to a set of overloaded functions, at least one of which
11023 is a template, or an IDENTIFIER_NODE with the name of the template,
11024 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
11025 names are looked up inside uninstantiated templates. */
11027 static tree
11028 cp_parser_template_name (cp_parser* parser,
11029 bool template_keyword_p,
11030 bool check_dependency_p,
11031 bool is_declaration,
11032 bool *is_identifier)
11034 tree identifier;
11035 tree decl;
11036 tree fns;
11037 cp_token *token = cp_lexer_peek_token (parser->lexer);
11039 /* If the next token is `operator', then we have either an
11040 operator-function-id or a conversion-function-id. */
11041 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
11043 /* We don't know whether we're looking at an
11044 operator-function-id or a conversion-function-id. */
11045 cp_parser_parse_tentatively (parser);
11046 /* Try an operator-function-id. */
11047 identifier = cp_parser_operator_function_id (parser);
11048 /* If that didn't work, try a conversion-function-id. */
11049 if (!cp_parser_parse_definitely (parser))
11051 cp_parser_error (parser, "expected template-name");
11052 return error_mark_node;
11055 /* Look for the identifier. */
11056 else
11057 identifier = cp_parser_identifier (parser);
11059 /* If we didn't find an identifier, we don't have a template-id. */
11060 if (identifier == error_mark_node)
11061 return error_mark_node;
11063 /* If the name immediately followed the `template' keyword, then it
11064 is a template-name. However, if the next token is not `<', then
11065 we do not treat it as a template-name, since it is not being used
11066 as part of a template-id. This enables us to handle constructs
11067 like:
11069 template <typename T> struct S { S(); };
11070 template <typename T> S<T>::S();
11072 correctly. We would treat `S' as a template -- if it were `S<T>'
11073 -- but we do not if there is no `<'. */
11075 if (processing_template_decl
11076 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
11078 /* In a declaration, in a dependent context, we pretend that the
11079 "template" keyword was present in order to improve error
11080 recovery. For example, given:
11082 template <typename T> void f(T::X<int>);
11084 we want to treat "X<int>" as a template-id. */
11085 if (is_declaration
11086 && !template_keyword_p
11087 && parser->scope && TYPE_P (parser->scope)
11088 && check_dependency_p
11089 && dependent_scope_p (parser->scope)
11090 /* Do not do this for dtors (or ctors), since they never
11091 need the template keyword before their name. */
11092 && !constructor_name_p (identifier, parser->scope))
11094 cp_token_position start = 0;
11096 /* Explain what went wrong. */
11097 error_at (token->location, "non-template %qD used as template",
11098 identifier);
11099 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
11100 parser->scope, identifier);
11101 /* If parsing tentatively, find the location of the "<" token. */
11102 if (cp_parser_simulate_error (parser))
11103 start = cp_lexer_token_position (parser->lexer, true);
11104 /* Parse the template arguments so that we can issue error
11105 messages about them. */
11106 cp_lexer_consume_token (parser->lexer);
11107 cp_parser_enclosed_template_argument_list (parser);
11108 /* Skip tokens until we find a good place from which to
11109 continue parsing. */
11110 cp_parser_skip_to_closing_parenthesis (parser,
11111 /*recovering=*/true,
11112 /*or_comma=*/true,
11113 /*consume_paren=*/false);
11114 /* If parsing tentatively, permanently remove the
11115 template argument list. That will prevent duplicate
11116 error messages from being issued about the missing
11117 "template" keyword. */
11118 if (start)
11119 cp_lexer_purge_tokens_after (parser->lexer, start);
11120 if (is_identifier)
11121 *is_identifier = true;
11122 return identifier;
11125 /* If the "template" keyword is present, then there is generally
11126 no point in doing name-lookup, so we just return IDENTIFIER.
11127 But, if the qualifying scope is non-dependent then we can
11128 (and must) do name-lookup normally. */
11129 if (template_keyword_p
11130 && (!parser->scope
11131 || (TYPE_P (parser->scope)
11132 && dependent_type_p (parser->scope))))
11133 return identifier;
11136 /* Look up the name. */
11137 decl = cp_parser_lookup_name (parser, identifier,
11138 none_type,
11139 /*is_template=*/true,
11140 /*is_namespace=*/false,
11141 check_dependency_p,
11142 /*ambiguous_decls=*/NULL,
11143 token->location);
11145 /* If DECL is a template, then the name was a template-name. */
11146 if (TREE_CODE (decl) == TEMPLATE_DECL)
11148 else
11150 tree fn = NULL_TREE;
11152 /* The standard does not explicitly indicate whether a name that
11153 names a set of overloaded declarations, some of which are
11154 templates, is a template-name. However, such a name should
11155 be a template-name; otherwise, there is no way to form a
11156 template-id for the overloaded templates. */
11157 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11158 if (TREE_CODE (fns) == OVERLOAD)
11159 for (fn = fns; fn; fn = OVL_NEXT (fn))
11160 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11161 break;
11163 if (!fn)
11165 /* The name does not name a template. */
11166 cp_parser_error (parser, "expected template-name");
11167 return error_mark_node;
11171 /* If DECL is dependent, and refers to a function, then just return
11172 its name; we will look it up again during template instantiation. */
11173 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11175 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
11176 if (TYPE_P (scope) && dependent_type_p (scope))
11177 return identifier;
11180 return decl;
11183 /* Parse a template-argument-list.
11185 template-argument-list:
11186 template-argument ... [opt]
11187 template-argument-list , template-argument ... [opt]
11189 Returns a TREE_VEC containing the arguments. */
11191 static tree
11192 cp_parser_template_argument_list (cp_parser* parser)
11194 tree fixed_args[10];
11195 unsigned n_args = 0;
11196 unsigned alloced = 10;
11197 tree *arg_ary = fixed_args;
11198 tree vec;
11199 bool saved_in_template_argument_list_p;
11200 bool saved_ice_p;
11201 bool saved_non_ice_p;
11203 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
11204 parser->in_template_argument_list_p = true;
11205 /* Even if the template-id appears in an integral
11206 constant-expression, the contents of the argument list do
11207 not. */
11208 saved_ice_p = parser->integral_constant_expression_p;
11209 parser->integral_constant_expression_p = false;
11210 saved_non_ice_p = parser->non_integral_constant_expression_p;
11211 parser->non_integral_constant_expression_p = false;
11212 /* Parse the arguments. */
11215 tree argument;
11217 if (n_args)
11218 /* Consume the comma. */
11219 cp_lexer_consume_token (parser->lexer);
11221 /* Parse the template-argument. */
11222 argument = cp_parser_template_argument (parser);
11224 /* If the next token is an ellipsis, we're expanding a template
11225 argument pack. */
11226 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11228 if (argument == error_mark_node)
11230 cp_token *token = cp_lexer_peek_token (parser->lexer);
11231 error_at (token->location,
11232 "expected parameter pack before %<...%>");
11234 /* Consume the `...' token. */
11235 cp_lexer_consume_token (parser->lexer);
11237 /* Make the argument into a TYPE_PACK_EXPANSION or
11238 EXPR_PACK_EXPANSION. */
11239 argument = make_pack_expansion (argument);
11242 if (n_args == alloced)
11244 alloced *= 2;
11246 if (arg_ary == fixed_args)
11248 arg_ary = XNEWVEC (tree, alloced);
11249 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
11251 else
11252 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
11254 arg_ary[n_args++] = argument;
11256 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
11258 vec = make_tree_vec (n_args);
11260 while (n_args--)
11261 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
11263 if (arg_ary != fixed_args)
11264 free (arg_ary);
11265 parser->non_integral_constant_expression_p = saved_non_ice_p;
11266 parser->integral_constant_expression_p = saved_ice_p;
11267 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
11268 #ifdef ENABLE_CHECKING
11269 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
11270 #endif
11271 return vec;
11274 /* Parse a template-argument.
11276 template-argument:
11277 assignment-expression
11278 type-id
11279 id-expression
11281 The representation is that of an assignment-expression, type-id, or
11282 id-expression -- except that the qualified id-expression is
11283 evaluated, so that the value returned is either a DECL or an
11284 OVERLOAD.
11286 Although the standard says "assignment-expression", it forbids
11287 throw-expressions or assignments in the template argument.
11288 Therefore, we use "conditional-expression" instead. */
11290 static tree
11291 cp_parser_template_argument (cp_parser* parser)
11293 tree argument;
11294 bool template_p;
11295 bool address_p;
11296 bool maybe_type_id = false;
11297 cp_token *token = NULL, *argument_start_token = NULL;
11298 cp_id_kind idk;
11300 /* There's really no way to know what we're looking at, so we just
11301 try each alternative in order.
11303 [temp.arg]
11305 In a template-argument, an ambiguity between a type-id and an
11306 expression is resolved to a type-id, regardless of the form of
11307 the corresponding template-parameter.
11309 Therefore, we try a type-id first. */
11310 cp_parser_parse_tentatively (parser);
11311 argument = cp_parser_template_type_arg (parser);
11312 /* If there was no error parsing the type-id but the next token is a
11313 '>>', our behavior depends on which dialect of C++ we're
11314 parsing. In C++98, we probably found a typo for '> >'. But there
11315 are type-id which are also valid expressions. For instance:
11317 struct X { int operator >> (int); };
11318 template <int V> struct Foo {};
11319 Foo<X () >> 5> r;
11321 Here 'X()' is a valid type-id of a function type, but the user just
11322 wanted to write the expression "X() >> 5". Thus, we remember that we
11323 found a valid type-id, but we still try to parse the argument as an
11324 expression to see what happens.
11326 In C++0x, the '>>' will be considered two separate '>'
11327 tokens. */
11328 if (!cp_parser_error_occurred (parser)
11329 && cxx_dialect == cxx98
11330 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
11332 maybe_type_id = true;
11333 cp_parser_abort_tentative_parse (parser);
11335 else
11337 /* If the next token isn't a `,' or a `>', then this argument wasn't
11338 really finished. This means that the argument is not a valid
11339 type-id. */
11340 if (!cp_parser_next_token_ends_template_argument_p (parser))
11341 cp_parser_error (parser, "expected template-argument");
11342 /* If that worked, we're done. */
11343 if (cp_parser_parse_definitely (parser))
11344 return argument;
11346 /* We're still not sure what the argument will be. */
11347 cp_parser_parse_tentatively (parser);
11348 /* Try a template. */
11349 argument_start_token = cp_lexer_peek_token (parser->lexer);
11350 argument = cp_parser_id_expression (parser,
11351 /*template_keyword_p=*/false,
11352 /*check_dependency_p=*/true,
11353 &template_p,
11354 /*declarator_p=*/false,
11355 /*optional_p=*/false);
11356 /* If the next token isn't a `,' or a `>', then this argument wasn't
11357 really finished. */
11358 if (!cp_parser_next_token_ends_template_argument_p (parser))
11359 cp_parser_error (parser, "expected template-argument");
11360 if (!cp_parser_error_occurred (parser))
11362 /* Figure out what is being referred to. If the id-expression
11363 was for a class template specialization, then we will have a
11364 TYPE_DECL at this point. There is no need to do name lookup
11365 at this point in that case. */
11366 if (TREE_CODE (argument) != TYPE_DECL)
11367 argument = cp_parser_lookup_name (parser, argument,
11368 none_type,
11369 /*is_template=*/template_p,
11370 /*is_namespace=*/false,
11371 /*check_dependency=*/true,
11372 /*ambiguous_decls=*/NULL,
11373 argument_start_token->location);
11374 if (TREE_CODE (argument) != TEMPLATE_DECL
11375 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
11376 cp_parser_error (parser, "expected template-name");
11378 if (cp_parser_parse_definitely (parser))
11379 return argument;
11380 /* It must be a non-type argument. There permitted cases are given
11381 in [temp.arg.nontype]:
11383 -- an integral constant-expression of integral or enumeration
11384 type; or
11386 -- the name of a non-type template-parameter; or
11388 -- the name of an object or function with external linkage...
11390 -- the address of an object or function with external linkage...
11392 -- a pointer to member... */
11393 /* Look for a non-type template parameter. */
11394 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11396 cp_parser_parse_tentatively (parser);
11397 argument = cp_parser_primary_expression (parser,
11398 /*address_p=*/false,
11399 /*cast_p=*/false,
11400 /*template_arg_p=*/true,
11401 &idk);
11402 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
11403 || !cp_parser_next_token_ends_template_argument_p (parser))
11404 cp_parser_simulate_error (parser);
11405 if (cp_parser_parse_definitely (parser))
11406 return argument;
11409 /* If the next token is "&", the argument must be the address of an
11410 object or function with external linkage. */
11411 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
11412 if (address_p)
11413 cp_lexer_consume_token (parser->lexer);
11414 /* See if we might have an id-expression. */
11415 token = cp_lexer_peek_token (parser->lexer);
11416 if (token->type == CPP_NAME
11417 || token->keyword == RID_OPERATOR
11418 || token->type == CPP_SCOPE
11419 || token->type == CPP_TEMPLATE_ID
11420 || token->type == CPP_NESTED_NAME_SPECIFIER)
11422 cp_parser_parse_tentatively (parser);
11423 argument = cp_parser_primary_expression (parser,
11424 address_p,
11425 /*cast_p=*/false,
11426 /*template_arg_p=*/true,
11427 &idk);
11428 if (cp_parser_error_occurred (parser)
11429 || !cp_parser_next_token_ends_template_argument_p (parser))
11430 cp_parser_abort_tentative_parse (parser);
11431 else
11433 tree probe;
11435 if (TREE_CODE (argument) == INDIRECT_REF)
11437 gcc_assert (REFERENCE_REF_P (argument));
11438 argument = TREE_OPERAND (argument, 0);
11441 /* If we're in a template, we represent a qualified-id referring
11442 to a static data member as a SCOPE_REF even if the scope isn't
11443 dependent so that we can check access control later. */
11444 probe = argument;
11445 if (TREE_CODE (probe) == SCOPE_REF)
11446 probe = TREE_OPERAND (probe, 1);
11447 if (TREE_CODE (probe) == VAR_DECL)
11449 /* A variable without external linkage might still be a
11450 valid constant-expression, so no error is issued here
11451 if the external-linkage check fails. */
11452 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
11453 cp_parser_simulate_error (parser);
11455 else if (is_overloaded_fn (argument))
11456 /* All overloaded functions are allowed; if the external
11457 linkage test does not pass, an error will be issued
11458 later. */
11460 else if (address_p
11461 && (TREE_CODE (argument) == OFFSET_REF
11462 || TREE_CODE (argument) == SCOPE_REF))
11463 /* A pointer-to-member. */
11465 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
11467 else
11468 cp_parser_simulate_error (parser);
11470 if (cp_parser_parse_definitely (parser))
11472 if (address_p)
11473 argument = build_x_unary_op (ADDR_EXPR, argument,
11474 tf_warning_or_error);
11475 return argument;
11479 /* If the argument started with "&", there are no other valid
11480 alternatives at this point. */
11481 if (address_p)
11483 cp_parser_error (parser, "invalid non-type template argument");
11484 return error_mark_node;
11487 /* If the argument wasn't successfully parsed as a type-id followed
11488 by '>>', the argument can only be a constant expression now.
11489 Otherwise, we try parsing the constant-expression tentatively,
11490 because the argument could really be a type-id. */
11491 if (maybe_type_id)
11492 cp_parser_parse_tentatively (parser);
11493 argument = cp_parser_constant_expression (parser,
11494 /*allow_non_constant_p=*/false,
11495 /*non_constant_p=*/NULL);
11496 argument = fold_non_dependent_expr (argument);
11497 if (!maybe_type_id)
11498 return argument;
11499 if (!cp_parser_next_token_ends_template_argument_p (parser))
11500 cp_parser_error (parser, "expected template-argument");
11501 if (cp_parser_parse_definitely (parser))
11502 return argument;
11503 /* We did our best to parse the argument as a non type-id, but that
11504 was the only alternative that matched (albeit with a '>' after
11505 it). We can assume it's just a typo from the user, and a
11506 diagnostic will then be issued. */
11507 return cp_parser_template_type_arg (parser);
11510 /* Parse an explicit-instantiation.
11512 explicit-instantiation:
11513 template declaration
11515 Although the standard says `declaration', what it really means is:
11517 explicit-instantiation:
11518 template decl-specifier-seq [opt] declarator [opt] ;
11520 Things like `template int S<int>::i = 5, int S<double>::j;' are not
11521 supposed to be allowed. A defect report has been filed about this
11522 issue.
11524 GNU Extension:
11526 explicit-instantiation:
11527 storage-class-specifier template
11528 decl-specifier-seq [opt] declarator [opt] ;
11529 function-specifier template
11530 decl-specifier-seq [opt] declarator [opt] ; */
11532 static void
11533 cp_parser_explicit_instantiation (cp_parser* parser)
11535 int declares_class_or_enum;
11536 cp_decl_specifier_seq decl_specifiers;
11537 tree extension_specifier = NULL_TREE;
11539 /* Look for an (optional) storage-class-specifier or
11540 function-specifier. */
11541 if (cp_parser_allow_gnu_extensions_p (parser))
11543 extension_specifier
11544 = cp_parser_storage_class_specifier_opt (parser);
11545 if (!extension_specifier)
11546 extension_specifier
11547 = cp_parser_function_specifier_opt (parser,
11548 /*decl_specs=*/NULL);
11551 /* Look for the `template' keyword. */
11552 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
11553 /* Let the front end know that we are processing an explicit
11554 instantiation. */
11555 begin_explicit_instantiation ();
11556 /* [temp.explicit] says that we are supposed to ignore access
11557 control while processing explicit instantiation directives. */
11558 push_deferring_access_checks (dk_no_check);
11559 /* Parse a decl-specifier-seq. */
11560 cp_parser_decl_specifier_seq (parser,
11561 CP_PARSER_FLAGS_OPTIONAL,
11562 &decl_specifiers,
11563 &declares_class_or_enum);
11564 /* If there was exactly one decl-specifier, and it declared a class,
11565 and there's no declarator, then we have an explicit type
11566 instantiation. */
11567 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
11569 tree type;
11571 type = check_tag_decl (&decl_specifiers);
11572 /* Turn access control back on for names used during
11573 template instantiation. */
11574 pop_deferring_access_checks ();
11575 if (type)
11576 do_type_instantiation (type, extension_specifier,
11577 /*complain=*/tf_error);
11579 else
11581 cp_declarator *declarator;
11582 tree decl;
11584 /* Parse the declarator. */
11585 declarator
11586 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11587 /*ctor_dtor_or_conv_p=*/NULL,
11588 /*parenthesized_p=*/NULL,
11589 /*member_p=*/false);
11590 if (declares_class_or_enum & 2)
11591 cp_parser_check_for_definition_in_return_type (declarator,
11592 decl_specifiers.type,
11593 decl_specifiers.type_location);
11594 if (declarator != cp_error_declarator)
11596 decl = grokdeclarator (declarator, &decl_specifiers,
11597 NORMAL, 0, &decl_specifiers.attributes);
11598 /* Turn access control back on for names used during
11599 template instantiation. */
11600 pop_deferring_access_checks ();
11601 /* Do the explicit instantiation. */
11602 do_decl_instantiation (decl, extension_specifier);
11604 else
11606 pop_deferring_access_checks ();
11607 /* Skip the body of the explicit instantiation. */
11608 cp_parser_skip_to_end_of_statement (parser);
11611 /* We're done with the instantiation. */
11612 end_explicit_instantiation ();
11614 cp_parser_consume_semicolon_at_end_of_statement (parser);
11617 /* Parse an explicit-specialization.
11619 explicit-specialization:
11620 template < > declaration
11622 Although the standard says `declaration', what it really means is:
11624 explicit-specialization:
11625 template <> decl-specifier [opt] init-declarator [opt] ;
11626 template <> function-definition
11627 template <> explicit-specialization
11628 template <> template-declaration */
11630 static void
11631 cp_parser_explicit_specialization (cp_parser* parser)
11633 bool need_lang_pop;
11634 cp_token *token = cp_lexer_peek_token (parser->lexer);
11636 /* Look for the `template' keyword. */
11637 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
11638 /* Look for the `<'. */
11639 cp_parser_require (parser, CPP_LESS, "%<<%>");
11640 /* Look for the `>'. */
11641 cp_parser_require (parser, CPP_GREATER, "%<>%>");
11642 /* We have processed another parameter list. */
11643 ++parser->num_template_parameter_lists;
11644 /* [temp]
11646 A template ... explicit specialization ... shall not have C
11647 linkage. */
11648 if (current_lang_name == lang_name_c)
11650 error_at (token->location, "template specialization with C linkage");
11651 /* Give it C++ linkage to avoid confusing other parts of the
11652 front end. */
11653 push_lang_context (lang_name_cplusplus);
11654 need_lang_pop = true;
11656 else
11657 need_lang_pop = false;
11658 /* Let the front end know that we are beginning a specialization. */
11659 if (!begin_specialization ())
11661 end_specialization ();
11662 return;
11665 /* If the next keyword is `template', we need to figure out whether
11666 or not we're looking a template-declaration. */
11667 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
11669 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
11670 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
11671 cp_parser_template_declaration_after_export (parser,
11672 /*member_p=*/false);
11673 else
11674 cp_parser_explicit_specialization (parser);
11676 else
11677 /* Parse the dependent declaration. */
11678 cp_parser_single_declaration (parser,
11679 /*checks=*/NULL,
11680 /*member_p=*/false,
11681 /*explicit_specialization_p=*/true,
11682 /*friend_p=*/NULL);
11683 /* We're done with the specialization. */
11684 end_specialization ();
11685 /* For the erroneous case of a template with C linkage, we pushed an
11686 implicit C++ linkage scope; exit that scope now. */
11687 if (need_lang_pop)
11688 pop_lang_context ();
11689 /* We're done with this parameter list. */
11690 --parser->num_template_parameter_lists;
11693 /* Parse a type-specifier.
11695 type-specifier:
11696 simple-type-specifier
11697 class-specifier
11698 enum-specifier
11699 elaborated-type-specifier
11700 cv-qualifier
11702 GNU Extension:
11704 type-specifier:
11705 __complex__
11707 Returns a representation of the type-specifier. For a
11708 class-specifier, enum-specifier, or elaborated-type-specifier, a
11709 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
11711 The parser flags FLAGS is used to control type-specifier parsing.
11713 If IS_DECLARATION is TRUE, then this type-specifier is appearing
11714 in a decl-specifier-seq.
11716 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
11717 class-specifier, enum-specifier, or elaborated-type-specifier, then
11718 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
11719 if a type is declared; 2 if it is defined. Otherwise, it is set to
11720 zero.
11722 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
11723 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
11724 is set to FALSE. */
11726 static tree
11727 cp_parser_type_specifier (cp_parser* parser,
11728 cp_parser_flags flags,
11729 cp_decl_specifier_seq *decl_specs,
11730 bool is_declaration,
11731 int* declares_class_or_enum,
11732 bool* is_cv_qualifier)
11734 tree type_spec = NULL_TREE;
11735 cp_token *token;
11736 enum rid keyword;
11737 cp_decl_spec ds = ds_last;
11739 /* Assume this type-specifier does not declare a new type. */
11740 if (declares_class_or_enum)
11741 *declares_class_or_enum = 0;
11742 /* And that it does not specify a cv-qualifier. */
11743 if (is_cv_qualifier)
11744 *is_cv_qualifier = false;
11745 /* Peek at the next token. */
11746 token = cp_lexer_peek_token (parser->lexer);
11748 /* If we're looking at a keyword, we can use that to guide the
11749 production we choose. */
11750 keyword = token->keyword;
11751 switch (keyword)
11753 case RID_ENUM:
11754 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
11755 goto elaborated_type_specifier;
11757 /* Look for the enum-specifier. */
11758 type_spec = cp_parser_enum_specifier (parser);
11759 /* If that worked, we're done. */
11760 if (type_spec)
11762 if (declares_class_or_enum)
11763 *declares_class_or_enum = 2;
11764 if (decl_specs)
11765 cp_parser_set_decl_spec_type (decl_specs,
11766 type_spec,
11767 token->location,
11768 /*user_defined_p=*/true);
11769 return type_spec;
11771 else
11772 goto elaborated_type_specifier;
11774 /* Any of these indicate either a class-specifier, or an
11775 elaborated-type-specifier. */
11776 case RID_CLASS:
11777 case RID_STRUCT:
11778 case RID_UNION:
11779 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
11780 goto elaborated_type_specifier;
11782 /* Parse tentatively so that we can back up if we don't find a
11783 class-specifier. */
11784 cp_parser_parse_tentatively (parser);
11785 /* Look for the class-specifier. */
11786 type_spec = cp_parser_class_specifier (parser);
11787 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
11788 /* If that worked, we're done. */
11789 if (cp_parser_parse_definitely (parser))
11791 if (declares_class_or_enum)
11792 *declares_class_or_enum = 2;
11793 if (decl_specs)
11794 cp_parser_set_decl_spec_type (decl_specs,
11795 type_spec,
11796 token->location,
11797 /*user_defined_p=*/true);
11798 return type_spec;
11801 /* Fall through. */
11802 elaborated_type_specifier:
11803 /* We're declaring (not defining) a class or enum. */
11804 if (declares_class_or_enum)
11805 *declares_class_or_enum = 1;
11807 /* Fall through. */
11808 case RID_TYPENAME:
11809 /* Look for an elaborated-type-specifier. */
11810 type_spec
11811 = (cp_parser_elaborated_type_specifier
11812 (parser,
11813 decl_specs && decl_specs->specs[(int) ds_friend],
11814 is_declaration));
11815 if (decl_specs)
11816 cp_parser_set_decl_spec_type (decl_specs,
11817 type_spec,
11818 token->location,
11819 /*user_defined_p=*/true);
11820 return type_spec;
11822 case RID_CONST:
11823 ds = ds_const;
11824 if (is_cv_qualifier)
11825 *is_cv_qualifier = true;
11826 break;
11828 case RID_VOLATILE:
11829 ds = ds_volatile;
11830 if (is_cv_qualifier)
11831 *is_cv_qualifier = true;
11832 break;
11834 case RID_RESTRICT:
11835 ds = ds_restrict;
11836 if (is_cv_qualifier)
11837 *is_cv_qualifier = true;
11838 break;
11840 case RID_COMPLEX:
11841 /* The `__complex__' keyword is a GNU extension. */
11842 ds = ds_complex;
11843 break;
11845 default:
11846 break;
11849 /* Handle simple keywords. */
11850 if (ds != ds_last)
11852 if (decl_specs)
11854 ++decl_specs->specs[(int)ds];
11855 decl_specs->any_specifiers_p = true;
11857 return cp_lexer_consume_token (parser->lexer)->u.value;
11860 /* If we do not already have a type-specifier, assume we are looking
11861 at a simple-type-specifier. */
11862 type_spec = cp_parser_simple_type_specifier (parser,
11863 decl_specs,
11864 flags);
11866 /* If we didn't find a type-specifier, and a type-specifier was not
11867 optional in this context, issue an error message. */
11868 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11870 cp_parser_error (parser, "expected type specifier");
11871 return error_mark_node;
11874 return type_spec;
11877 /* Parse a simple-type-specifier.
11879 simple-type-specifier:
11880 :: [opt] nested-name-specifier [opt] type-name
11881 :: [opt] nested-name-specifier template template-id
11882 char
11883 wchar_t
11884 bool
11885 short
11887 long
11888 signed
11889 unsigned
11890 float
11891 double
11892 void
11894 C++0x Extension:
11896 simple-type-specifier:
11897 auto
11898 decltype ( expression )
11899 char16_t
11900 char32_t
11902 GNU Extension:
11904 simple-type-specifier:
11905 __typeof__ unary-expression
11906 __typeof__ ( type-id )
11908 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
11909 appropriately updated. */
11911 static tree
11912 cp_parser_simple_type_specifier (cp_parser* parser,
11913 cp_decl_specifier_seq *decl_specs,
11914 cp_parser_flags flags)
11916 tree type = NULL_TREE;
11917 cp_token *token;
11919 /* Peek at the next token. */
11920 token = cp_lexer_peek_token (parser->lexer);
11922 /* If we're looking at a keyword, things are easy. */
11923 switch (token->keyword)
11925 case RID_CHAR:
11926 if (decl_specs)
11927 decl_specs->explicit_char_p = true;
11928 type = char_type_node;
11929 break;
11930 case RID_CHAR16:
11931 type = char16_type_node;
11932 break;
11933 case RID_CHAR32:
11934 type = char32_type_node;
11935 break;
11936 case RID_WCHAR:
11937 type = wchar_type_node;
11938 break;
11939 case RID_BOOL:
11940 type = boolean_type_node;
11941 break;
11942 case RID_SHORT:
11943 if (decl_specs)
11944 ++decl_specs->specs[(int) ds_short];
11945 type = short_integer_type_node;
11946 break;
11947 case RID_INT:
11948 if (decl_specs)
11949 decl_specs->explicit_int_p = true;
11950 type = integer_type_node;
11951 break;
11952 case RID_LONG:
11953 if (decl_specs)
11954 ++decl_specs->specs[(int) ds_long];
11955 type = long_integer_type_node;
11956 break;
11957 case RID_SIGNED:
11958 if (decl_specs)
11959 ++decl_specs->specs[(int) ds_signed];
11960 type = integer_type_node;
11961 break;
11962 case RID_UNSIGNED:
11963 if (decl_specs)
11964 ++decl_specs->specs[(int) ds_unsigned];
11965 type = unsigned_type_node;
11966 break;
11967 case RID_FLOAT:
11968 type = float_type_node;
11969 break;
11970 case RID_DOUBLE:
11971 type = double_type_node;
11972 break;
11973 case RID_VOID:
11974 type = void_type_node;
11975 break;
11977 case RID_AUTO:
11978 maybe_warn_cpp0x (CPP0X_AUTO);
11979 type = make_auto ();
11980 break;
11982 case RID_DECLTYPE:
11983 /* Parse the `decltype' type. */
11984 type = cp_parser_decltype (parser);
11986 if (decl_specs)
11987 cp_parser_set_decl_spec_type (decl_specs, type,
11988 token->location,
11989 /*user_defined_p=*/true);
11991 return type;
11993 case RID_TYPEOF:
11994 /* Consume the `typeof' token. */
11995 cp_lexer_consume_token (parser->lexer);
11996 /* Parse the operand to `typeof'. */
11997 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11998 /* If it is not already a TYPE, take its type. */
11999 if (!TYPE_P (type))
12000 type = finish_typeof (type);
12002 if (decl_specs)
12003 cp_parser_set_decl_spec_type (decl_specs, type,
12004 token->location,
12005 /*user_defined_p=*/true);
12007 return type;
12009 default:
12010 break;
12013 /* If the type-specifier was for a built-in type, we're done. */
12014 if (type)
12016 /* Record the type. */
12017 if (decl_specs
12018 && (token->keyword != RID_SIGNED
12019 && token->keyword != RID_UNSIGNED
12020 && token->keyword != RID_SHORT
12021 && token->keyword != RID_LONG))
12022 cp_parser_set_decl_spec_type (decl_specs,
12023 type,
12024 token->location,
12025 /*user_defined=*/false);
12026 if (decl_specs)
12027 decl_specs->any_specifiers_p = true;
12029 /* Consume the token. */
12030 cp_lexer_consume_token (parser->lexer);
12032 /* There is no valid C++ program where a non-template type is
12033 followed by a "<". That usually indicates that the user thought
12034 that the type was a template. */
12035 cp_parser_check_for_invalid_template_id (parser, type, token->location);
12037 return TYPE_NAME (type);
12040 /* The type-specifier must be a user-defined type. */
12041 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
12043 bool qualified_p;
12044 bool global_p;
12046 /* Don't gobble tokens or issue error messages if this is an
12047 optional type-specifier. */
12048 if (flags & CP_PARSER_FLAGS_OPTIONAL)
12049 cp_parser_parse_tentatively (parser);
12051 /* Look for the optional `::' operator. */
12052 global_p
12053 = (cp_parser_global_scope_opt (parser,
12054 /*current_scope_valid_p=*/false)
12055 != NULL_TREE);
12056 /* Look for the nested-name specifier. */
12057 qualified_p
12058 = (cp_parser_nested_name_specifier_opt (parser,
12059 /*typename_keyword_p=*/false,
12060 /*check_dependency_p=*/true,
12061 /*type_p=*/false,
12062 /*is_declaration=*/false)
12063 != NULL_TREE);
12064 token = cp_lexer_peek_token (parser->lexer);
12065 /* If we have seen a nested-name-specifier, and the next token
12066 is `template', then we are using the template-id production. */
12067 if (parser->scope
12068 && cp_parser_optional_template_keyword (parser))
12070 /* Look for the template-id. */
12071 type = cp_parser_template_id (parser,
12072 /*template_keyword_p=*/true,
12073 /*check_dependency_p=*/true,
12074 /*is_declaration=*/false);
12075 /* If the template-id did not name a type, we are out of
12076 luck. */
12077 if (TREE_CODE (type) != TYPE_DECL)
12079 cp_parser_error (parser, "expected template-id for type");
12080 type = NULL_TREE;
12083 /* Otherwise, look for a type-name. */
12084 else
12085 type = cp_parser_type_name (parser);
12086 /* Keep track of all name-lookups performed in class scopes. */
12087 if (type
12088 && !global_p
12089 && !qualified_p
12090 && TREE_CODE (type) == TYPE_DECL
12091 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
12092 maybe_note_name_used_in_class (DECL_NAME (type), type);
12093 /* If it didn't work out, we don't have a TYPE. */
12094 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
12095 && !cp_parser_parse_definitely (parser))
12096 type = NULL_TREE;
12097 if (type && decl_specs)
12098 cp_parser_set_decl_spec_type (decl_specs, type,
12099 token->location,
12100 /*user_defined=*/true);
12103 /* If we didn't get a type-name, issue an error message. */
12104 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12106 cp_parser_error (parser, "expected type-name");
12107 return error_mark_node;
12110 /* There is no valid C++ program where a non-template type is
12111 followed by a "<". That usually indicates that the user thought
12112 that the type was a template. */
12113 if (type && type != error_mark_node)
12115 /* As a last-ditch effort, see if TYPE is an Objective-C type.
12116 If it is, then the '<'...'>' enclose protocol names rather than
12117 template arguments, and so everything is fine. */
12118 if (c_dialect_objc ()
12119 && (objc_is_id (type) || objc_is_class_name (type)))
12121 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12122 tree qual_type = objc_get_protocol_qualified_type (type, protos);
12124 /* Clobber the "unqualified" type previously entered into
12125 DECL_SPECS with the new, improved protocol-qualified version. */
12126 if (decl_specs)
12127 decl_specs->type = qual_type;
12129 return qual_type;
12132 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12133 token->location);
12136 return type;
12139 /* Parse a type-name.
12141 type-name:
12142 class-name
12143 enum-name
12144 typedef-name
12146 enum-name:
12147 identifier
12149 typedef-name:
12150 identifier
12152 Returns a TYPE_DECL for the type. */
12154 static tree
12155 cp_parser_type_name (cp_parser* parser)
12157 tree type_decl;
12159 /* We can't know yet whether it is a class-name or not. */
12160 cp_parser_parse_tentatively (parser);
12161 /* Try a class-name. */
12162 type_decl = cp_parser_class_name (parser,
12163 /*typename_keyword_p=*/false,
12164 /*template_keyword_p=*/false,
12165 none_type,
12166 /*check_dependency_p=*/true,
12167 /*class_head_p=*/false,
12168 /*is_declaration=*/false);
12169 /* If it's not a class-name, keep looking. */
12170 if (!cp_parser_parse_definitely (parser))
12172 /* It must be a typedef-name or an enum-name. */
12173 return cp_parser_nonclass_name (parser);
12176 return type_decl;
12179 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12181 enum-name:
12182 identifier
12184 typedef-name:
12185 identifier
12187 Returns a TYPE_DECL for the type. */
12189 static tree
12190 cp_parser_nonclass_name (cp_parser* parser)
12192 tree type_decl;
12193 tree identifier;
12195 cp_token *token = cp_lexer_peek_token (parser->lexer);
12196 identifier = cp_parser_identifier (parser);
12197 if (identifier == error_mark_node)
12198 return error_mark_node;
12200 /* Look up the type-name. */
12201 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
12203 if (TREE_CODE (type_decl) != TYPE_DECL
12204 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
12206 /* See if this is an Objective-C type. */
12207 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12208 tree type = objc_get_protocol_qualified_type (identifier, protos);
12209 if (type)
12210 type_decl = TYPE_NAME (type);
12213 /* Issue an error if we did not find a type-name. */
12214 if (TREE_CODE (type_decl) != TYPE_DECL)
12216 if (!cp_parser_simulate_error (parser))
12217 cp_parser_name_lookup_error (parser, identifier, type_decl,
12218 "is not a type", token->location);
12219 return error_mark_node;
12221 /* Remember that the name was used in the definition of the
12222 current class so that we can check later to see if the
12223 meaning would have been different after the class was
12224 entirely defined. */
12225 else if (type_decl != error_mark_node
12226 && !parser->scope)
12227 maybe_note_name_used_in_class (identifier, type_decl);
12229 return type_decl;
12232 /* Parse an elaborated-type-specifier. Note that the grammar given
12233 here incorporates the resolution to DR68.
12235 elaborated-type-specifier:
12236 class-key :: [opt] nested-name-specifier [opt] identifier
12237 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
12238 enum-key :: [opt] nested-name-specifier [opt] identifier
12239 typename :: [opt] nested-name-specifier identifier
12240 typename :: [opt] nested-name-specifier template [opt]
12241 template-id
12243 GNU extension:
12245 elaborated-type-specifier:
12246 class-key attributes :: [opt] nested-name-specifier [opt] identifier
12247 class-key attributes :: [opt] nested-name-specifier [opt]
12248 template [opt] template-id
12249 enum attributes :: [opt] nested-name-specifier [opt] identifier
12251 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
12252 declared `friend'. If IS_DECLARATION is TRUE, then this
12253 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
12254 something is being declared.
12256 Returns the TYPE specified. */
12258 static tree
12259 cp_parser_elaborated_type_specifier (cp_parser* parser,
12260 bool is_friend,
12261 bool is_declaration)
12263 enum tag_types tag_type;
12264 tree identifier;
12265 tree type = NULL_TREE;
12266 tree attributes = NULL_TREE;
12267 tree globalscope;
12268 cp_token *token = NULL;
12270 /* See if we're looking at the `enum' keyword. */
12271 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
12273 /* Consume the `enum' token. */
12274 cp_lexer_consume_token (parser->lexer);
12275 /* Remember that it's an enumeration type. */
12276 tag_type = enum_type;
12277 /* Parse the optional `struct' or `class' key (for C++0x scoped
12278 enums). */
12279 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12280 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12282 if (cxx_dialect == cxx98)
12283 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
12285 /* Consume the `struct' or `class'. */
12286 cp_lexer_consume_token (parser->lexer);
12288 /* Parse the attributes. */
12289 attributes = cp_parser_attributes_opt (parser);
12291 /* Or, it might be `typename'. */
12292 else if (cp_lexer_next_token_is_keyword (parser->lexer,
12293 RID_TYPENAME))
12295 /* Consume the `typename' token. */
12296 cp_lexer_consume_token (parser->lexer);
12297 /* Remember that it's a `typename' type. */
12298 tag_type = typename_type;
12300 /* Otherwise it must be a class-key. */
12301 else
12303 tag_type = cp_parser_class_key (parser);
12304 if (tag_type == none_type)
12305 return error_mark_node;
12306 /* Parse the attributes. */
12307 attributes = cp_parser_attributes_opt (parser);
12310 /* Look for the `::' operator. */
12311 globalscope = cp_parser_global_scope_opt (parser,
12312 /*current_scope_valid_p=*/false);
12313 /* Look for the nested-name-specifier. */
12314 if (tag_type == typename_type && !globalscope)
12316 if (!cp_parser_nested_name_specifier (parser,
12317 /*typename_keyword_p=*/true,
12318 /*check_dependency_p=*/true,
12319 /*type_p=*/true,
12320 is_declaration))
12321 return error_mark_node;
12323 else
12324 /* Even though `typename' is not present, the proposed resolution
12325 to Core Issue 180 says that in `class A<T>::B', `B' should be
12326 considered a type-name, even if `A<T>' is dependent. */
12327 cp_parser_nested_name_specifier_opt (parser,
12328 /*typename_keyword_p=*/true,
12329 /*check_dependency_p=*/true,
12330 /*type_p=*/true,
12331 is_declaration);
12332 /* For everything but enumeration types, consider a template-id.
12333 For an enumeration type, consider only a plain identifier. */
12334 if (tag_type != enum_type)
12336 bool template_p = false;
12337 tree decl;
12339 /* Allow the `template' keyword. */
12340 template_p = cp_parser_optional_template_keyword (parser);
12341 /* If we didn't see `template', we don't know if there's a
12342 template-id or not. */
12343 if (!template_p)
12344 cp_parser_parse_tentatively (parser);
12345 /* Parse the template-id. */
12346 token = cp_lexer_peek_token (parser->lexer);
12347 decl = cp_parser_template_id (parser, template_p,
12348 /*check_dependency_p=*/true,
12349 is_declaration);
12350 /* If we didn't find a template-id, look for an ordinary
12351 identifier. */
12352 if (!template_p && !cp_parser_parse_definitely (parser))
12354 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
12355 in effect, then we must assume that, upon instantiation, the
12356 template will correspond to a class. */
12357 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12358 && tag_type == typename_type)
12359 type = make_typename_type (parser->scope, decl,
12360 typename_type,
12361 /*complain=*/tf_error);
12362 /* If the `typename' keyword is in effect and DECL is not a type
12363 decl. Then type is non existant. */
12364 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
12365 type = NULL_TREE;
12366 else
12367 type = TREE_TYPE (decl);
12370 if (!type)
12372 token = cp_lexer_peek_token (parser->lexer);
12373 identifier = cp_parser_identifier (parser);
12375 if (identifier == error_mark_node)
12377 parser->scope = NULL_TREE;
12378 return error_mark_node;
12381 /* For a `typename', we needn't call xref_tag. */
12382 if (tag_type == typename_type
12383 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
12384 return cp_parser_make_typename_type (parser, parser->scope,
12385 identifier,
12386 token->location);
12387 /* Look up a qualified name in the usual way. */
12388 if (parser->scope)
12390 tree decl;
12391 tree ambiguous_decls;
12393 decl = cp_parser_lookup_name (parser, identifier,
12394 tag_type,
12395 /*is_template=*/false,
12396 /*is_namespace=*/false,
12397 /*check_dependency=*/true,
12398 &ambiguous_decls,
12399 token->location);
12401 /* If the lookup was ambiguous, an error will already have been
12402 issued. */
12403 if (ambiguous_decls)
12404 return error_mark_node;
12406 /* If we are parsing friend declaration, DECL may be a
12407 TEMPLATE_DECL tree node here. However, we need to check
12408 whether this TEMPLATE_DECL results in valid code. Consider
12409 the following example:
12411 namespace N {
12412 template <class T> class C {};
12414 class X {
12415 template <class T> friend class N::C; // #1, valid code
12417 template <class T> class Y {
12418 friend class N::C; // #2, invalid code
12421 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
12422 name lookup of `N::C'. We see that friend declaration must
12423 be template for the code to be valid. Note that
12424 processing_template_decl does not work here since it is
12425 always 1 for the above two cases. */
12427 decl = (cp_parser_maybe_treat_template_as_class
12428 (decl, /*tag_name_p=*/is_friend
12429 && parser->num_template_parameter_lists));
12431 if (TREE_CODE (decl) != TYPE_DECL)
12433 cp_parser_diagnose_invalid_type_name (parser,
12434 parser->scope,
12435 identifier,
12436 token->location);
12437 return error_mark_node;
12440 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
12442 bool allow_template = (parser->num_template_parameter_lists
12443 || DECL_SELF_REFERENCE_P (decl));
12444 type = check_elaborated_type_specifier (tag_type, decl,
12445 allow_template);
12447 if (type == error_mark_node)
12448 return error_mark_node;
12451 /* Forward declarations of nested types, such as
12453 class C1::C2;
12454 class C1::C2::C3;
12456 are invalid unless all components preceding the final '::'
12457 are complete. If all enclosing types are complete, these
12458 declarations become merely pointless.
12460 Invalid forward declarations of nested types are errors
12461 caught elsewhere in parsing. Those that are pointless arrive
12462 here. */
12464 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12465 && !is_friend && !processing_explicit_instantiation)
12466 warning (0, "declaration %qD does not declare anything", decl);
12468 type = TREE_TYPE (decl);
12470 else
12472 /* An elaborated-type-specifier sometimes introduces a new type and
12473 sometimes names an existing type. Normally, the rule is that it
12474 introduces a new type only if there is not an existing type of
12475 the same name already in scope. For example, given:
12477 struct S {};
12478 void f() { struct S s; }
12480 the `struct S' in the body of `f' is the same `struct S' as in
12481 the global scope; the existing definition is used. However, if
12482 there were no global declaration, this would introduce a new
12483 local class named `S'.
12485 An exception to this rule applies to the following code:
12487 namespace N { struct S; }
12489 Here, the elaborated-type-specifier names a new type
12490 unconditionally; even if there is already an `S' in the
12491 containing scope this declaration names a new type.
12492 This exception only applies if the elaborated-type-specifier
12493 forms the complete declaration:
12495 [class.name]
12497 A declaration consisting solely of `class-key identifier ;' is
12498 either a redeclaration of the name in the current scope or a
12499 forward declaration of the identifier as a class name. It
12500 introduces the name into the current scope.
12502 We are in this situation precisely when the next token is a `;'.
12504 An exception to the exception is that a `friend' declaration does
12505 *not* name a new type; i.e., given:
12507 struct S { friend struct T; };
12509 `T' is not a new type in the scope of `S'.
12511 Also, `new struct S' or `sizeof (struct S)' never results in the
12512 definition of a new type; a new type can only be declared in a
12513 declaration context. */
12515 tag_scope ts;
12516 bool template_p;
12518 if (is_friend)
12519 /* Friends have special name lookup rules. */
12520 ts = ts_within_enclosing_non_class;
12521 else if (is_declaration
12522 && cp_lexer_next_token_is (parser->lexer,
12523 CPP_SEMICOLON))
12524 /* This is a `class-key identifier ;' */
12525 ts = ts_current;
12526 else
12527 ts = ts_global;
12529 template_p =
12530 (parser->num_template_parameter_lists
12531 && (cp_parser_next_token_starts_class_definition_p (parser)
12532 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
12533 /* An unqualified name was used to reference this type, so
12534 there were no qualifying templates. */
12535 if (!cp_parser_check_template_parameters (parser,
12536 /*num_templates=*/0,
12537 token->location,
12538 /*declarator=*/NULL))
12539 return error_mark_node;
12540 type = xref_tag (tag_type, identifier, ts, template_p);
12544 if (type == error_mark_node)
12545 return error_mark_node;
12547 /* Allow attributes on forward declarations of classes. */
12548 if (attributes)
12550 if (TREE_CODE (type) == TYPENAME_TYPE)
12551 warning (OPT_Wattributes,
12552 "attributes ignored on uninstantiated type");
12553 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
12554 && ! processing_explicit_instantiation)
12555 warning (OPT_Wattributes,
12556 "attributes ignored on template instantiation");
12557 else if (is_declaration && cp_parser_declares_only_class_p (parser))
12558 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
12559 else
12560 warning (OPT_Wattributes,
12561 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
12564 if (tag_type != enum_type)
12565 cp_parser_check_class_key (tag_type, type);
12567 /* A "<" cannot follow an elaborated type specifier. If that
12568 happens, the user was probably trying to form a template-id. */
12569 cp_parser_check_for_invalid_template_id (parser, type, token->location);
12571 return type;
12574 /* Parse an enum-specifier.
12576 enum-specifier:
12577 enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
12579 enum-key:
12580 enum
12581 enum class [C++0x]
12582 enum struct [C++0x]
12584 enum-base: [C++0x]
12585 : type-specifier-seq
12587 GNU Extensions:
12588 enum-key attributes[opt] identifier [opt] enum-base [opt]
12589 { enumerator-list [opt] }attributes[opt]
12591 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
12592 if the token stream isn't an enum-specifier after all. */
12594 static tree
12595 cp_parser_enum_specifier (cp_parser* parser)
12597 tree identifier;
12598 tree type;
12599 tree attributes;
12600 bool scoped_enum_p = false;
12601 bool has_underlying_type = false;
12602 tree underlying_type = NULL_TREE;
12604 /* Parse tentatively so that we can back up if we don't find a
12605 enum-specifier. */
12606 cp_parser_parse_tentatively (parser);
12608 /* Caller guarantees that the current token is 'enum', an identifier
12609 possibly follows, and the token after that is an opening brace.
12610 If we don't have an identifier, fabricate an anonymous name for
12611 the enumeration being defined. */
12612 cp_lexer_consume_token (parser->lexer);
12614 /* Parse the "class" or "struct", which indicates a scoped
12615 enumeration type in C++0x. */
12616 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12617 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12619 if (cxx_dialect == cxx98)
12620 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
12622 /* Consume the `struct' or `class' token. */
12623 cp_lexer_consume_token (parser->lexer);
12625 scoped_enum_p = true;
12628 attributes = cp_parser_attributes_opt (parser);
12630 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12631 identifier = cp_parser_identifier (parser);
12632 else
12633 identifier = make_anon_name ();
12635 /* Check for the `:' that denotes a specified underlying type in C++0x.
12636 Note that a ':' could also indicate a bitfield width, however. */
12637 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12639 cp_decl_specifier_seq type_specifiers;
12641 /* Consume the `:'. */
12642 cp_lexer_consume_token (parser->lexer);
12644 /* Parse the type-specifier-seq. */
12645 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12646 /*is_trailing_return=*/false,
12647 &type_specifiers);
12649 /* At this point this is surely not elaborated type specifier. */
12650 if (!cp_parser_parse_definitely (parser))
12651 return NULL_TREE;
12653 if (cxx_dialect == cxx98)
12654 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
12656 has_underlying_type = true;
12658 /* If that didn't work, stop. */
12659 if (type_specifiers.type != error_mark_node)
12661 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
12662 /*initialized=*/0, NULL);
12663 if (underlying_type == error_mark_node)
12664 underlying_type = NULL_TREE;
12668 /* Look for the `{' but don't consume it yet. */
12669 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12671 cp_parser_error (parser, "expected %<{%>");
12672 if (has_underlying_type)
12673 return NULL_TREE;
12676 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
12677 return NULL_TREE;
12679 /* Issue an error message if type-definitions are forbidden here. */
12680 if (!cp_parser_check_type_definition (parser))
12681 type = error_mark_node;
12682 else
12683 /* Create the new type. We do this before consuming the opening
12684 brace so the enum will be recorded as being on the line of its
12685 tag (or the 'enum' keyword, if there is no tag). */
12686 type = start_enum (identifier, underlying_type, scoped_enum_p);
12688 /* Consume the opening brace. */
12689 cp_lexer_consume_token (parser->lexer);
12691 if (type == error_mark_node)
12693 cp_parser_skip_to_end_of_block_or_statement (parser);
12694 return error_mark_node;
12697 /* If the next token is not '}', then there are some enumerators. */
12698 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12699 cp_parser_enumerator_list (parser, type);
12701 /* Consume the final '}'. */
12702 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12704 /* Look for trailing attributes to apply to this enumeration, and
12705 apply them if appropriate. */
12706 if (cp_parser_allow_gnu_extensions_p (parser))
12708 tree trailing_attr = cp_parser_attributes_opt (parser);
12709 trailing_attr = chainon (trailing_attr, attributes);
12710 cplus_decl_attributes (&type,
12711 trailing_attr,
12712 (int) ATTR_FLAG_TYPE_IN_PLACE);
12715 /* Finish up the enumeration. */
12716 finish_enum (type);
12718 return type;
12721 /* Parse an enumerator-list. The enumerators all have the indicated
12722 TYPE.
12724 enumerator-list:
12725 enumerator-definition
12726 enumerator-list , enumerator-definition */
12728 static void
12729 cp_parser_enumerator_list (cp_parser* parser, tree type)
12731 while (true)
12733 /* Parse an enumerator-definition. */
12734 cp_parser_enumerator_definition (parser, type);
12736 /* If the next token is not a ',', we've reached the end of
12737 the list. */
12738 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12739 break;
12740 /* Otherwise, consume the `,' and keep going. */
12741 cp_lexer_consume_token (parser->lexer);
12742 /* If the next token is a `}', there is a trailing comma. */
12743 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
12745 if (!in_system_header)
12746 pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
12747 break;
12752 /* Parse an enumerator-definition. The enumerator has the indicated
12753 TYPE.
12755 enumerator-definition:
12756 enumerator
12757 enumerator = constant-expression
12759 enumerator:
12760 identifier */
12762 static void
12763 cp_parser_enumerator_definition (cp_parser* parser, tree type)
12765 tree identifier;
12766 tree value;
12768 /* Look for the identifier. */
12769 identifier = cp_parser_identifier (parser);
12770 if (identifier == error_mark_node)
12771 return;
12773 /* If the next token is an '=', then there is an explicit value. */
12774 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12776 /* Consume the `=' token. */
12777 cp_lexer_consume_token (parser->lexer);
12778 /* Parse the value. */
12779 value = cp_parser_constant_expression (parser,
12780 /*allow_non_constant_p=*/false,
12781 NULL);
12783 else
12784 value = NULL_TREE;
12786 /* If we are processing a template, make sure the initializer of the
12787 enumerator doesn't contain any bare template parameter pack. */
12788 if (check_for_bare_parameter_packs (value))
12789 value = error_mark_node;
12791 /* Create the enumerator. */
12792 build_enumerator (identifier, value, type);
12795 /* Parse a namespace-name.
12797 namespace-name:
12798 original-namespace-name
12799 namespace-alias
12801 Returns the NAMESPACE_DECL for the namespace. */
12803 static tree
12804 cp_parser_namespace_name (cp_parser* parser)
12806 tree identifier;
12807 tree namespace_decl;
12809 cp_token *token = cp_lexer_peek_token (parser->lexer);
12811 /* Get the name of the namespace. */
12812 identifier = cp_parser_identifier (parser);
12813 if (identifier == error_mark_node)
12814 return error_mark_node;
12816 /* Look up the identifier in the currently active scope. Look only
12817 for namespaces, due to:
12819 [basic.lookup.udir]
12821 When looking up a namespace-name in a using-directive or alias
12822 definition, only namespace names are considered.
12824 And:
12826 [basic.lookup.qual]
12828 During the lookup of a name preceding the :: scope resolution
12829 operator, object, function, and enumerator names are ignored.
12831 (Note that cp_parser_qualifying_entity only calls this
12832 function if the token after the name is the scope resolution
12833 operator.) */
12834 namespace_decl = cp_parser_lookup_name (parser, identifier,
12835 none_type,
12836 /*is_template=*/false,
12837 /*is_namespace=*/true,
12838 /*check_dependency=*/true,
12839 /*ambiguous_decls=*/NULL,
12840 token->location);
12841 /* If it's not a namespace, issue an error. */
12842 if (namespace_decl == error_mark_node
12843 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
12845 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12846 error_at (token->location, "%qD is not a namespace-name", identifier);
12847 cp_parser_error (parser, "expected namespace-name");
12848 namespace_decl = error_mark_node;
12851 return namespace_decl;
12854 /* Parse a namespace-definition.
12856 namespace-definition:
12857 named-namespace-definition
12858 unnamed-namespace-definition
12860 named-namespace-definition:
12861 original-namespace-definition
12862 extension-namespace-definition
12864 original-namespace-definition:
12865 namespace identifier { namespace-body }
12867 extension-namespace-definition:
12868 namespace original-namespace-name { namespace-body }
12870 unnamed-namespace-definition:
12871 namespace { namespace-body } */
12873 static void
12874 cp_parser_namespace_definition (cp_parser* parser)
12876 tree identifier, attribs;
12877 bool has_visibility;
12878 bool is_inline;
12880 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
12882 is_inline = true;
12883 cp_lexer_consume_token (parser->lexer);
12885 else
12886 is_inline = false;
12888 /* Look for the `namespace' keyword. */
12889 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12891 /* Get the name of the namespace. We do not attempt to distinguish
12892 between an original-namespace-definition and an
12893 extension-namespace-definition at this point. The semantic
12894 analysis routines are responsible for that. */
12895 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12896 identifier = cp_parser_identifier (parser);
12897 else
12898 identifier = NULL_TREE;
12900 /* Parse any specified attributes. */
12901 attribs = cp_parser_attributes_opt (parser);
12903 /* Look for the `{' to start the namespace. */
12904 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
12905 /* Start the namespace. */
12906 push_namespace (identifier);
12908 /* "inline namespace" is equivalent to a stub namespace definition
12909 followed by a strong using directive. */
12910 if (is_inline)
12912 tree name_space = current_namespace;
12913 /* Set up namespace association. */
12914 DECL_NAMESPACE_ASSOCIATIONS (name_space)
12915 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
12916 DECL_NAMESPACE_ASSOCIATIONS (name_space));
12917 /* Import the contents of the inline namespace. */
12918 pop_namespace ();
12919 do_using_directive (name_space);
12920 push_namespace (identifier);
12923 has_visibility = handle_namespace_attrs (current_namespace, attribs);
12925 /* Parse the body of the namespace. */
12926 cp_parser_namespace_body (parser);
12928 #ifdef HANDLE_PRAGMA_VISIBILITY
12929 if (has_visibility)
12930 pop_visibility (1);
12931 #endif
12933 /* Finish the namespace. */
12934 pop_namespace ();
12935 /* Look for the final `}'. */
12936 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12939 /* Parse a namespace-body.
12941 namespace-body:
12942 declaration-seq [opt] */
12944 static void
12945 cp_parser_namespace_body (cp_parser* parser)
12947 cp_parser_declaration_seq_opt (parser);
12950 /* Parse a namespace-alias-definition.
12952 namespace-alias-definition:
12953 namespace identifier = qualified-namespace-specifier ; */
12955 static void
12956 cp_parser_namespace_alias_definition (cp_parser* parser)
12958 tree identifier;
12959 tree namespace_specifier;
12961 cp_token *token = cp_lexer_peek_token (parser->lexer);
12963 /* Look for the `namespace' keyword. */
12964 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12965 /* Look for the identifier. */
12966 identifier = cp_parser_identifier (parser);
12967 if (identifier == error_mark_node)
12968 return;
12969 /* Look for the `=' token. */
12970 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12971 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12973 error_at (token->location, "%<namespace%> definition is not allowed here");
12974 /* Skip the definition. */
12975 cp_lexer_consume_token (parser->lexer);
12976 if (cp_parser_skip_to_closing_brace (parser))
12977 cp_lexer_consume_token (parser->lexer);
12978 return;
12980 cp_parser_require (parser, CPP_EQ, "%<=%>");
12981 /* Look for the qualified-namespace-specifier. */
12982 namespace_specifier
12983 = cp_parser_qualified_namespace_specifier (parser);
12984 /* Look for the `;' token. */
12985 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12987 /* Register the alias in the symbol table. */
12988 do_namespace_alias (identifier, namespace_specifier);
12991 /* Parse a qualified-namespace-specifier.
12993 qualified-namespace-specifier:
12994 :: [opt] nested-name-specifier [opt] namespace-name
12996 Returns a NAMESPACE_DECL corresponding to the specified
12997 namespace. */
12999 static tree
13000 cp_parser_qualified_namespace_specifier (cp_parser* parser)
13002 /* Look for the optional `::'. */
13003 cp_parser_global_scope_opt (parser,
13004 /*current_scope_valid_p=*/false);
13006 /* Look for the optional nested-name-specifier. */
13007 cp_parser_nested_name_specifier_opt (parser,
13008 /*typename_keyword_p=*/false,
13009 /*check_dependency_p=*/true,
13010 /*type_p=*/false,
13011 /*is_declaration=*/true);
13013 return cp_parser_namespace_name (parser);
13016 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
13017 access declaration.
13019 using-declaration:
13020 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
13021 using :: unqualified-id ;
13023 access-declaration:
13024 qualified-id ;
13028 static bool
13029 cp_parser_using_declaration (cp_parser* parser,
13030 bool access_declaration_p)
13032 cp_token *token;
13033 bool typename_p = false;
13034 bool global_scope_p;
13035 tree decl;
13036 tree identifier;
13037 tree qscope;
13039 if (access_declaration_p)
13040 cp_parser_parse_tentatively (parser);
13041 else
13043 /* Look for the `using' keyword. */
13044 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
13046 /* Peek at the next token. */
13047 token = cp_lexer_peek_token (parser->lexer);
13048 /* See if it's `typename'. */
13049 if (token->keyword == RID_TYPENAME)
13051 /* Remember that we've seen it. */
13052 typename_p = true;
13053 /* Consume the `typename' token. */
13054 cp_lexer_consume_token (parser->lexer);
13058 /* Look for the optional global scope qualification. */
13059 global_scope_p
13060 = (cp_parser_global_scope_opt (parser,
13061 /*current_scope_valid_p=*/false)
13062 != NULL_TREE);
13064 /* If we saw `typename', or didn't see `::', then there must be a
13065 nested-name-specifier present. */
13066 if (typename_p || !global_scope_p)
13067 qscope = cp_parser_nested_name_specifier (parser, typename_p,
13068 /*check_dependency_p=*/true,
13069 /*type_p=*/false,
13070 /*is_declaration=*/true);
13071 /* Otherwise, we could be in either of the two productions. In that
13072 case, treat the nested-name-specifier as optional. */
13073 else
13074 qscope = cp_parser_nested_name_specifier_opt (parser,
13075 /*typename_keyword_p=*/false,
13076 /*check_dependency_p=*/true,
13077 /*type_p=*/false,
13078 /*is_declaration=*/true);
13079 if (!qscope)
13080 qscope = global_namespace;
13082 if (access_declaration_p && cp_parser_error_occurred (parser))
13083 /* Something has already gone wrong; there's no need to parse
13084 further. Since an error has occurred, the return value of
13085 cp_parser_parse_definitely will be false, as required. */
13086 return cp_parser_parse_definitely (parser);
13088 token = cp_lexer_peek_token (parser->lexer);
13089 /* Parse the unqualified-id. */
13090 identifier = cp_parser_unqualified_id (parser,
13091 /*template_keyword_p=*/false,
13092 /*check_dependency_p=*/true,
13093 /*declarator_p=*/true,
13094 /*optional_p=*/false);
13096 if (access_declaration_p)
13098 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13099 cp_parser_simulate_error (parser);
13100 if (!cp_parser_parse_definitely (parser))
13101 return false;
13104 /* The function we call to handle a using-declaration is different
13105 depending on what scope we are in. */
13106 if (qscope == error_mark_node || identifier == error_mark_node)
13108 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
13109 && TREE_CODE (identifier) != BIT_NOT_EXPR)
13110 /* [namespace.udecl]
13112 A using declaration shall not name a template-id. */
13113 error_at (token->location,
13114 "a template-id may not appear in a using-declaration");
13115 else
13117 if (at_class_scope_p ())
13119 /* Create the USING_DECL. */
13120 decl = do_class_using_decl (parser->scope, identifier);
13122 if (check_for_bare_parameter_packs (decl))
13123 return false;
13124 else
13125 /* Add it to the list of members in this class. */
13126 finish_member_declaration (decl);
13128 else
13130 decl = cp_parser_lookup_name_simple (parser,
13131 identifier,
13132 token->location);
13133 if (decl == error_mark_node)
13134 cp_parser_name_lookup_error (parser, identifier,
13135 decl, NULL,
13136 token->location);
13137 else if (check_for_bare_parameter_packs (decl))
13138 return false;
13139 else if (!at_namespace_scope_p ())
13140 do_local_using_decl (decl, qscope, identifier);
13141 else
13142 do_toplevel_using_decl (decl, qscope, identifier);
13146 /* Look for the final `;'. */
13147 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13149 return true;
13152 /* Parse a using-directive.
13154 using-directive:
13155 using namespace :: [opt] nested-name-specifier [opt]
13156 namespace-name ; */
13158 static void
13159 cp_parser_using_directive (cp_parser* parser)
13161 tree namespace_decl;
13162 tree attribs;
13164 /* Look for the `using' keyword. */
13165 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
13166 /* And the `namespace' keyword. */
13167 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
13168 /* Look for the optional `::' operator. */
13169 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13170 /* And the optional nested-name-specifier. */
13171 cp_parser_nested_name_specifier_opt (parser,
13172 /*typename_keyword_p=*/false,
13173 /*check_dependency_p=*/true,
13174 /*type_p=*/false,
13175 /*is_declaration=*/true);
13176 /* Get the namespace being used. */
13177 namespace_decl = cp_parser_namespace_name (parser);
13178 /* And any specified attributes. */
13179 attribs = cp_parser_attributes_opt (parser);
13180 /* Update the symbol table. */
13181 parse_using_directive (namespace_decl, attribs);
13182 /* Look for the final `;'. */
13183 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13186 /* Parse an asm-definition.
13188 asm-definition:
13189 asm ( string-literal ) ;
13191 GNU Extension:
13193 asm-definition:
13194 asm volatile [opt] ( string-literal ) ;
13195 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
13196 asm volatile [opt] ( string-literal : asm-operand-list [opt]
13197 : asm-operand-list [opt] ) ;
13198 asm volatile [opt] ( string-literal : asm-operand-list [opt]
13199 : asm-operand-list [opt]
13200 : asm-clobber-list [opt] ) ;
13201 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
13202 : asm-clobber-list [opt]
13203 : asm-goto-list ) ; */
13205 static void
13206 cp_parser_asm_definition (cp_parser* parser)
13208 tree string;
13209 tree outputs = NULL_TREE;
13210 tree inputs = NULL_TREE;
13211 tree clobbers = NULL_TREE;
13212 tree labels = NULL_TREE;
13213 tree asm_stmt;
13214 bool volatile_p = false;
13215 bool extended_p = false;
13216 bool invalid_inputs_p = false;
13217 bool invalid_outputs_p = false;
13218 bool goto_p = false;
13219 const char *missing = NULL;
13221 /* Look for the `asm' keyword. */
13222 cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
13223 /* See if the next token is `volatile'. */
13224 if (cp_parser_allow_gnu_extensions_p (parser)
13225 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
13227 /* Remember that we saw the `volatile' keyword. */
13228 volatile_p = true;
13229 /* Consume the token. */
13230 cp_lexer_consume_token (parser->lexer);
13232 if (cp_parser_allow_gnu_extensions_p (parser)
13233 && parser->in_function_body
13234 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
13236 /* Remember that we saw the `goto' keyword. */
13237 goto_p = true;
13238 /* Consume the token. */
13239 cp_lexer_consume_token (parser->lexer);
13241 /* Look for the opening `('. */
13242 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
13243 return;
13244 /* Look for the string. */
13245 string = cp_parser_string_literal (parser, false, false);
13246 if (string == error_mark_node)
13248 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13249 /*consume_paren=*/true);
13250 return;
13253 /* If we're allowing GNU extensions, check for the extended assembly
13254 syntax. Unfortunately, the `:' tokens need not be separated by
13255 a space in C, and so, for compatibility, we tolerate that here
13256 too. Doing that means that we have to treat the `::' operator as
13257 two `:' tokens. */
13258 if (cp_parser_allow_gnu_extensions_p (parser)
13259 && parser->in_function_body
13260 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
13261 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
13263 bool inputs_p = false;
13264 bool clobbers_p = false;
13265 bool labels_p = false;
13267 /* The extended syntax was used. */
13268 extended_p = true;
13270 /* Look for outputs. */
13271 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13273 /* Consume the `:'. */
13274 cp_lexer_consume_token (parser->lexer);
13275 /* Parse the output-operands. */
13276 if (cp_lexer_next_token_is_not (parser->lexer,
13277 CPP_COLON)
13278 && cp_lexer_next_token_is_not (parser->lexer,
13279 CPP_SCOPE)
13280 && cp_lexer_next_token_is_not (parser->lexer,
13281 CPP_CLOSE_PAREN)
13282 && !goto_p)
13283 outputs = cp_parser_asm_operand_list (parser);
13285 if (outputs == error_mark_node)
13286 invalid_outputs_p = true;
13288 /* If the next token is `::', there are no outputs, and the
13289 next token is the beginning of the inputs. */
13290 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13291 /* The inputs are coming next. */
13292 inputs_p = true;
13294 /* Look for inputs. */
13295 if (inputs_p
13296 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13298 /* Consume the `:' or `::'. */
13299 cp_lexer_consume_token (parser->lexer);
13300 /* Parse the output-operands. */
13301 if (cp_lexer_next_token_is_not (parser->lexer,
13302 CPP_COLON)
13303 && cp_lexer_next_token_is_not (parser->lexer,
13304 CPP_SCOPE)
13305 && cp_lexer_next_token_is_not (parser->lexer,
13306 CPP_CLOSE_PAREN))
13307 inputs = cp_parser_asm_operand_list (parser);
13309 if (inputs == error_mark_node)
13310 invalid_inputs_p = true;
13312 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13313 /* The clobbers are coming next. */
13314 clobbers_p = true;
13316 /* Look for clobbers. */
13317 if (clobbers_p
13318 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13320 clobbers_p = true;
13321 /* Consume the `:' or `::'. */
13322 cp_lexer_consume_token (parser->lexer);
13323 /* Parse the clobbers. */
13324 if (cp_lexer_next_token_is_not (parser->lexer,
13325 CPP_COLON)
13326 && cp_lexer_next_token_is_not (parser->lexer,
13327 CPP_CLOSE_PAREN))
13328 clobbers = cp_parser_asm_clobber_list (parser);
13330 else if (goto_p
13331 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13332 /* The labels are coming next. */
13333 labels_p = true;
13335 /* Look for labels. */
13336 if (labels_p
13337 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
13339 labels_p = true;
13340 /* Consume the `:' or `::'. */
13341 cp_lexer_consume_token (parser->lexer);
13342 /* Parse the labels. */
13343 labels = cp_parser_asm_label_list (parser);
13346 if (goto_p && !labels_p)
13347 missing = clobbers_p ? "%<:%>" : "%<:%> or %<::%>";
13349 else if (goto_p)
13350 missing = "%<:%> or %<::%>";
13352 /* Look for the closing `)'. */
13353 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
13354 missing ? missing : "%<)%>"))
13355 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13356 /*consume_paren=*/true);
13357 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13359 if (!invalid_inputs_p && !invalid_outputs_p)
13361 /* Create the ASM_EXPR. */
13362 if (parser->in_function_body)
13364 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
13365 inputs, clobbers, labels);
13366 /* If the extended syntax was not used, mark the ASM_EXPR. */
13367 if (!extended_p)
13369 tree temp = asm_stmt;
13370 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
13371 temp = TREE_OPERAND (temp, 0);
13373 ASM_INPUT_P (temp) = 1;
13376 else
13377 cgraph_add_asm_node (string);
13381 /* Declarators [gram.dcl.decl] */
13383 /* Parse an init-declarator.
13385 init-declarator:
13386 declarator initializer [opt]
13388 GNU Extension:
13390 init-declarator:
13391 declarator asm-specification [opt] attributes [opt] initializer [opt]
13393 function-definition:
13394 decl-specifier-seq [opt] declarator ctor-initializer [opt]
13395 function-body
13396 decl-specifier-seq [opt] declarator function-try-block
13398 GNU Extension:
13400 function-definition:
13401 __extension__ function-definition
13403 The DECL_SPECIFIERS apply to this declarator. Returns a
13404 representation of the entity declared. If MEMBER_P is TRUE, then
13405 this declarator appears in a class scope. The new DECL created by
13406 this declarator is returned.
13408 The CHECKS are access checks that should be performed once we know
13409 what entity is being declared (and, therefore, what classes have
13410 befriended it).
13412 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
13413 for a function-definition here as well. If the declarator is a
13414 declarator for a function-definition, *FUNCTION_DEFINITION_P will
13415 be TRUE upon return. By that point, the function-definition will
13416 have been completely parsed.
13418 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
13419 is FALSE. */
13421 static tree
13422 cp_parser_init_declarator (cp_parser* parser,
13423 cp_decl_specifier_seq *decl_specifiers,
13424 VEC (deferred_access_check,gc)* checks,
13425 bool function_definition_allowed_p,
13426 bool member_p,
13427 int declares_class_or_enum,
13428 bool* function_definition_p)
13430 cp_token *token = NULL, *asm_spec_start_token = NULL,
13431 *attributes_start_token = NULL;
13432 cp_declarator *declarator;
13433 tree prefix_attributes;
13434 tree attributes;
13435 tree asm_specification;
13436 tree initializer;
13437 tree decl = NULL_TREE;
13438 tree scope;
13439 int is_initialized;
13440 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
13441 initialized with "= ..", CPP_OPEN_PAREN if initialized with
13442 "(...)". */
13443 enum cpp_ttype initialization_kind;
13444 bool is_direct_init = false;
13445 bool is_non_constant_init;
13446 int ctor_dtor_or_conv_p;
13447 bool friend_p;
13448 tree pushed_scope = NULL;
13450 /* Gather the attributes that were provided with the
13451 decl-specifiers. */
13452 prefix_attributes = decl_specifiers->attributes;
13454 /* Assume that this is not the declarator for a function
13455 definition. */
13456 if (function_definition_p)
13457 *function_definition_p = false;
13459 /* Defer access checks while parsing the declarator; we cannot know
13460 what names are accessible until we know what is being
13461 declared. */
13462 resume_deferring_access_checks ();
13464 /* Parse the declarator. */
13465 token = cp_lexer_peek_token (parser->lexer);
13466 declarator
13467 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13468 &ctor_dtor_or_conv_p,
13469 /*parenthesized_p=*/NULL,
13470 /*member_p=*/false);
13471 /* Gather up the deferred checks. */
13472 stop_deferring_access_checks ();
13474 /* If the DECLARATOR was erroneous, there's no need to go
13475 further. */
13476 if (declarator == cp_error_declarator)
13477 return error_mark_node;
13479 /* Check that the number of template-parameter-lists is OK. */
13480 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
13481 token->location))
13482 return error_mark_node;
13484 if (declares_class_or_enum & 2)
13485 cp_parser_check_for_definition_in_return_type (declarator,
13486 decl_specifiers->type,
13487 decl_specifiers->type_location);
13489 /* Figure out what scope the entity declared by the DECLARATOR is
13490 located in. `grokdeclarator' sometimes changes the scope, so
13491 we compute it now. */
13492 scope = get_scope_of_declarator (declarator);
13494 /* Perform any lookups in the declared type which were thought to be
13495 dependent, but are not in the scope of the declarator. */
13496 decl_specifiers->type
13497 = maybe_update_decl_type (decl_specifiers->type, scope);
13499 /* If we're allowing GNU extensions, look for an asm-specification
13500 and attributes. */
13501 if (cp_parser_allow_gnu_extensions_p (parser))
13503 /* Look for an asm-specification. */
13504 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
13505 asm_specification = cp_parser_asm_specification_opt (parser);
13506 /* And attributes. */
13507 attributes_start_token = cp_lexer_peek_token (parser->lexer);
13508 attributes = cp_parser_attributes_opt (parser);
13510 else
13512 asm_specification = NULL_TREE;
13513 attributes = NULL_TREE;
13516 /* Peek at the next token. */
13517 token = cp_lexer_peek_token (parser->lexer);
13518 /* Check to see if the token indicates the start of a
13519 function-definition. */
13520 if (function_declarator_p (declarator)
13521 && cp_parser_token_starts_function_definition_p (token))
13523 if (!function_definition_allowed_p)
13525 /* If a function-definition should not appear here, issue an
13526 error message. */
13527 cp_parser_error (parser,
13528 "a function-definition is not allowed here");
13529 return error_mark_node;
13531 else
13533 location_t func_brace_location
13534 = cp_lexer_peek_token (parser->lexer)->location;
13536 /* Neither attributes nor an asm-specification are allowed
13537 on a function-definition. */
13538 if (asm_specification)
13539 error_at (asm_spec_start_token->location,
13540 "an asm-specification is not allowed "
13541 "on a function-definition");
13542 if (attributes)
13543 error_at (attributes_start_token->location,
13544 "attributes are not allowed on a function-definition");
13545 /* This is a function-definition. */
13546 *function_definition_p = true;
13548 /* Parse the function definition. */
13549 if (member_p)
13550 decl = cp_parser_save_member_function_body (parser,
13551 decl_specifiers,
13552 declarator,
13553 prefix_attributes);
13554 else
13555 decl
13556 = (cp_parser_function_definition_from_specifiers_and_declarator
13557 (parser, decl_specifiers, prefix_attributes, declarator));
13559 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
13561 /* This is where the prologue starts... */
13562 DECL_STRUCT_FUNCTION (decl)->function_start_locus
13563 = func_brace_location;
13566 return decl;
13570 /* [dcl.dcl]
13572 Only in function declarations for constructors, destructors, and
13573 type conversions can the decl-specifier-seq be omitted.
13575 We explicitly postpone this check past the point where we handle
13576 function-definitions because we tolerate function-definitions
13577 that are missing their return types in some modes. */
13578 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
13580 cp_parser_error (parser,
13581 "expected constructor, destructor, or type conversion");
13582 return error_mark_node;
13585 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
13586 if (token->type == CPP_EQ
13587 || token->type == CPP_OPEN_PAREN
13588 || token->type == CPP_OPEN_BRACE)
13590 is_initialized = SD_INITIALIZED;
13591 initialization_kind = token->type;
13593 if (token->type == CPP_EQ
13594 && function_declarator_p (declarator))
13596 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13597 if (t2->keyword == RID_DEFAULT)
13598 is_initialized = SD_DEFAULTED;
13599 else if (t2->keyword == RID_DELETE)
13600 is_initialized = SD_DELETED;
13603 else
13605 /* If the init-declarator isn't initialized and isn't followed by a
13606 `,' or `;', it's not a valid init-declarator. */
13607 if (token->type != CPP_COMMA
13608 && token->type != CPP_SEMICOLON)
13610 cp_parser_error (parser, "expected initializer");
13611 return error_mark_node;
13613 is_initialized = SD_UNINITIALIZED;
13614 initialization_kind = CPP_EOF;
13617 /* Because start_decl has side-effects, we should only call it if we
13618 know we're going ahead. By this point, we know that we cannot
13619 possibly be looking at any other construct. */
13620 cp_parser_commit_to_tentative_parse (parser);
13622 /* If the decl specifiers were bad, issue an error now that we're
13623 sure this was intended to be a declarator. Then continue
13624 declaring the variable(s), as int, to try to cut down on further
13625 errors. */
13626 if (decl_specifiers->any_specifiers_p
13627 && decl_specifiers->type == error_mark_node)
13629 cp_parser_error (parser, "invalid type in declaration");
13630 decl_specifiers->type = integer_type_node;
13633 /* Check to see whether or not this declaration is a friend. */
13634 friend_p = cp_parser_friend_p (decl_specifiers);
13636 /* Enter the newly declared entry in the symbol table. If we're
13637 processing a declaration in a class-specifier, we wait until
13638 after processing the initializer. */
13639 if (!member_p)
13641 if (parser->in_unbraced_linkage_specification_p)
13642 decl_specifiers->storage_class = sc_extern;
13643 decl = start_decl (declarator, decl_specifiers,
13644 is_initialized, attributes, prefix_attributes,
13645 &pushed_scope);
13647 else if (scope)
13648 /* Enter the SCOPE. That way unqualified names appearing in the
13649 initializer will be looked up in SCOPE. */
13650 pushed_scope = push_scope (scope);
13652 /* Perform deferred access control checks, now that we know in which
13653 SCOPE the declared entity resides. */
13654 if (!member_p && decl)
13656 tree saved_current_function_decl = NULL_TREE;
13658 /* If the entity being declared is a function, pretend that we
13659 are in its scope. If it is a `friend', it may have access to
13660 things that would not otherwise be accessible. */
13661 if (TREE_CODE (decl) == FUNCTION_DECL)
13663 saved_current_function_decl = current_function_decl;
13664 current_function_decl = decl;
13667 /* Perform access checks for template parameters. */
13668 cp_parser_perform_template_parameter_access_checks (checks);
13670 /* Perform the access control checks for the declarator and the
13671 decl-specifiers. */
13672 perform_deferred_access_checks ();
13674 /* Restore the saved value. */
13675 if (TREE_CODE (decl) == FUNCTION_DECL)
13676 current_function_decl = saved_current_function_decl;
13679 /* Parse the initializer. */
13680 initializer = NULL_TREE;
13681 is_direct_init = false;
13682 is_non_constant_init = true;
13683 if (is_initialized)
13685 if (function_declarator_p (declarator))
13687 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
13688 if (initialization_kind == CPP_EQ)
13689 initializer = cp_parser_pure_specifier (parser);
13690 else
13692 /* If the declaration was erroneous, we don't really
13693 know what the user intended, so just silently
13694 consume the initializer. */
13695 if (decl != error_mark_node)
13696 error_at (initializer_start_token->location,
13697 "initializer provided for function");
13698 cp_parser_skip_to_closing_parenthesis (parser,
13699 /*recovering=*/true,
13700 /*or_comma=*/false,
13701 /*consume_paren=*/true);
13704 else
13706 /* We want to record the extra mangling scope for in-class
13707 initializers of class members and initializers of static data
13708 member templates. The former is a C++0x feature which isn't
13709 implemented yet, and I expect it will involve deferring
13710 parsing of the initializer until end of class as with default
13711 arguments. So right here we only handle the latter. */
13712 if (!member_p && processing_template_decl)
13713 start_lambda_scope (decl);
13714 initializer = cp_parser_initializer (parser,
13715 &is_direct_init,
13716 &is_non_constant_init);
13717 if (!member_p && processing_template_decl)
13718 finish_lambda_scope ();
13722 /* The old parser allows attributes to appear after a parenthesized
13723 initializer. Mark Mitchell proposed removing this functionality
13724 on the GCC mailing lists on 2002-08-13. This parser accepts the
13725 attributes -- but ignores them. */
13726 if (cp_parser_allow_gnu_extensions_p (parser)
13727 && initialization_kind == CPP_OPEN_PAREN)
13728 if (cp_parser_attributes_opt (parser))
13729 warning (OPT_Wattributes,
13730 "attributes after parenthesized initializer ignored");
13732 /* For an in-class declaration, use `grokfield' to create the
13733 declaration. */
13734 if (member_p)
13736 if (pushed_scope)
13738 pop_scope (pushed_scope);
13739 pushed_scope = false;
13741 decl = grokfield (declarator, decl_specifiers,
13742 initializer, !is_non_constant_init,
13743 /*asmspec=*/NULL_TREE,
13744 prefix_attributes);
13745 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
13746 cp_parser_save_default_args (parser, decl);
13749 /* Finish processing the declaration. But, skip friend
13750 declarations. */
13751 if (!friend_p && decl && decl != error_mark_node)
13753 cp_finish_decl (decl,
13754 initializer, !is_non_constant_init,
13755 asm_specification,
13756 /* If the initializer is in parentheses, then this is
13757 a direct-initialization, which means that an
13758 `explicit' constructor is OK. Otherwise, an
13759 `explicit' constructor cannot be used. */
13760 ((is_direct_init || !is_initialized)
13761 ? 0 : LOOKUP_ONLYCONVERTING));
13763 else if ((cxx_dialect != cxx98) && friend_p
13764 && decl && TREE_CODE (decl) == FUNCTION_DECL)
13765 /* Core issue #226 (C++0x only): A default template-argument
13766 shall not be specified in a friend class template
13767 declaration. */
13768 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
13769 /*is_partial=*/0, /*is_friend_decl=*/1);
13771 if (!friend_p && pushed_scope)
13772 pop_scope (pushed_scope);
13774 return decl;
13777 /* Parse a declarator.
13779 declarator:
13780 direct-declarator
13781 ptr-operator declarator
13783 abstract-declarator:
13784 ptr-operator abstract-declarator [opt]
13785 direct-abstract-declarator
13787 GNU Extensions:
13789 declarator:
13790 attributes [opt] direct-declarator
13791 attributes [opt] ptr-operator declarator
13793 abstract-declarator:
13794 attributes [opt] ptr-operator abstract-declarator [opt]
13795 attributes [opt] direct-abstract-declarator
13797 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
13798 detect constructor, destructor or conversion operators. It is set
13799 to -1 if the declarator is a name, and +1 if it is a
13800 function. Otherwise it is set to zero. Usually you just want to
13801 test for >0, but internally the negative value is used.
13803 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
13804 a decl-specifier-seq unless it declares a constructor, destructor,
13805 or conversion. It might seem that we could check this condition in
13806 semantic analysis, rather than parsing, but that makes it difficult
13807 to handle something like `f()'. We want to notice that there are
13808 no decl-specifiers, and therefore realize that this is an
13809 expression, not a declaration.)
13811 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
13812 the declarator is a direct-declarator of the form "(...)".
13814 MEMBER_P is true iff this declarator is a member-declarator. */
13816 static cp_declarator *
13817 cp_parser_declarator (cp_parser* parser,
13818 cp_parser_declarator_kind dcl_kind,
13819 int* ctor_dtor_or_conv_p,
13820 bool* parenthesized_p,
13821 bool member_p)
13823 cp_declarator *declarator;
13824 enum tree_code code;
13825 cp_cv_quals cv_quals;
13826 tree class_type;
13827 tree attributes = NULL_TREE;
13829 /* Assume this is not a constructor, destructor, or type-conversion
13830 operator. */
13831 if (ctor_dtor_or_conv_p)
13832 *ctor_dtor_or_conv_p = 0;
13834 if (cp_parser_allow_gnu_extensions_p (parser))
13835 attributes = cp_parser_attributes_opt (parser);
13837 /* Check for the ptr-operator production. */
13838 cp_parser_parse_tentatively (parser);
13839 /* Parse the ptr-operator. */
13840 code = cp_parser_ptr_operator (parser,
13841 &class_type,
13842 &cv_quals);
13843 /* If that worked, then we have a ptr-operator. */
13844 if (cp_parser_parse_definitely (parser))
13846 /* If a ptr-operator was found, then this declarator was not
13847 parenthesized. */
13848 if (parenthesized_p)
13849 *parenthesized_p = true;
13850 /* The dependent declarator is optional if we are parsing an
13851 abstract-declarator. */
13852 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13853 cp_parser_parse_tentatively (parser);
13855 /* Parse the dependent declarator. */
13856 declarator = cp_parser_declarator (parser, dcl_kind,
13857 /*ctor_dtor_or_conv_p=*/NULL,
13858 /*parenthesized_p=*/NULL,
13859 /*member_p=*/false);
13861 /* If we are parsing an abstract-declarator, we must handle the
13862 case where the dependent declarator is absent. */
13863 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
13864 && !cp_parser_parse_definitely (parser))
13865 declarator = NULL;
13867 declarator = cp_parser_make_indirect_declarator
13868 (code, class_type, cv_quals, declarator);
13870 /* Everything else is a direct-declarator. */
13871 else
13873 if (parenthesized_p)
13874 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
13875 CPP_OPEN_PAREN);
13876 declarator = cp_parser_direct_declarator (parser, dcl_kind,
13877 ctor_dtor_or_conv_p,
13878 member_p);
13881 if (attributes && declarator && declarator != cp_error_declarator)
13882 declarator->attributes = attributes;
13884 return declarator;
13887 /* Parse a direct-declarator or direct-abstract-declarator.
13889 direct-declarator:
13890 declarator-id
13891 direct-declarator ( parameter-declaration-clause )
13892 cv-qualifier-seq [opt]
13893 exception-specification [opt]
13894 direct-declarator [ constant-expression [opt] ]
13895 ( declarator )
13897 direct-abstract-declarator:
13898 direct-abstract-declarator [opt]
13899 ( parameter-declaration-clause )
13900 cv-qualifier-seq [opt]
13901 exception-specification [opt]
13902 direct-abstract-declarator [opt] [ constant-expression [opt] ]
13903 ( abstract-declarator )
13905 Returns a representation of the declarator. DCL_KIND is
13906 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
13907 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
13908 we are parsing a direct-declarator. It is
13909 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
13910 of ambiguity we prefer an abstract declarator, as per
13911 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
13912 cp_parser_declarator. */
13914 static cp_declarator *
13915 cp_parser_direct_declarator (cp_parser* parser,
13916 cp_parser_declarator_kind dcl_kind,
13917 int* ctor_dtor_or_conv_p,
13918 bool member_p)
13920 cp_token *token;
13921 cp_declarator *declarator = NULL;
13922 tree scope = NULL_TREE;
13923 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13924 bool saved_in_declarator_p = parser->in_declarator_p;
13925 bool first = true;
13926 tree pushed_scope = NULL_TREE;
13928 while (true)
13930 /* Peek at the next token. */
13931 token = cp_lexer_peek_token (parser->lexer);
13932 if (token->type == CPP_OPEN_PAREN)
13934 /* This is either a parameter-declaration-clause, or a
13935 parenthesized declarator. When we know we are parsing a
13936 named declarator, it must be a parenthesized declarator
13937 if FIRST is true. For instance, `(int)' is a
13938 parameter-declaration-clause, with an omitted
13939 direct-abstract-declarator. But `((*))', is a
13940 parenthesized abstract declarator. Finally, when T is a
13941 template parameter `(T)' is a
13942 parameter-declaration-clause, and not a parenthesized
13943 named declarator.
13945 We first try and parse a parameter-declaration-clause,
13946 and then try a nested declarator (if FIRST is true).
13948 It is not an error for it not to be a
13949 parameter-declaration-clause, even when FIRST is
13950 false. Consider,
13952 int i (int);
13953 int i (3);
13955 The first is the declaration of a function while the
13956 second is the definition of a variable, including its
13957 initializer.
13959 Having seen only the parenthesis, we cannot know which of
13960 these two alternatives should be selected. Even more
13961 complex are examples like:
13963 int i (int (a));
13964 int i (int (3));
13966 The former is a function-declaration; the latter is a
13967 variable initialization.
13969 Thus again, we try a parameter-declaration-clause, and if
13970 that fails, we back out and return. */
13972 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13974 tree params;
13975 unsigned saved_num_template_parameter_lists;
13976 bool is_declarator = false;
13977 tree t;
13979 /* In a member-declarator, the only valid interpretation
13980 of a parenthesis is the start of a
13981 parameter-declaration-clause. (It is invalid to
13982 initialize a static data member with a parenthesized
13983 initializer; only the "=" form of initialization is
13984 permitted.) */
13985 if (!member_p)
13986 cp_parser_parse_tentatively (parser);
13988 /* Consume the `('. */
13989 cp_lexer_consume_token (parser->lexer);
13990 if (first)
13992 /* If this is going to be an abstract declarator, we're
13993 in a declarator and we can't have default args. */
13994 parser->default_arg_ok_p = false;
13995 parser->in_declarator_p = true;
13998 /* Inside the function parameter list, surrounding
13999 template-parameter-lists do not apply. */
14000 saved_num_template_parameter_lists
14001 = parser->num_template_parameter_lists;
14002 parser->num_template_parameter_lists = 0;
14004 begin_scope (sk_function_parms, NULL_TREE);
14006 /* Parse the parameter-declaration-clause. */
14007 params = cp_parser_parameter_declaration_clause (parser);
14009 parser->num_template_parameter_lists
14010 = saved_num_template_parameter_lists;
14012 /* If all went well, parse the cv-qualifier-seq and the
14013 exception-specification. */
14014 if (member_p || cp_parser_parse_definitely (parser))
14016 cp_cv_quals cv_quals;
14017 tree exception_specification;
14018 tree late_return;
14020 is_declarator = true;
14022 if (ctor_dtor_or_conv_p)
14023 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
14024 first = false;
14025 /* Consume the `)'. */
14026 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
14028 /* Parse the cv-qualifier-seq. */
14029 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14030 /* And the exception-specification. */
14031 exception_specification
14032 = cp_parser_exception_specification_opt (parser);
14034 late_return
14035 = cp_parser_late_return_type_opt (parser);
14037 /* Create the function-declarator. */
14038 declarator = make_call_declarator (declarator,
14039 params,
14040 cv_quals,
14041 exception_specification,
14042 late_return);
14043 /* Any subsequent parameter lists are to do with
14044 return type, so are not those of the declared
14045 function. */
14046 parser->default_arg_ok_p = false;
14049 /* Remove the function parms from scope. */
14050 for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
14051 pop_binding (DECL_NAME (t), t);
14052 leave_scope();
14054 if (is_declarator)
14055 /* Repeat the main loop. */
14056 continue;
14059 /* If this is the first, we can try a parenthesized
14060 declarator. */
14061 if (first)
14063 bool saved_in_type_id_in_expr_p;
14065 parser->default_arg_ok_p = saved_default_arg_ok_p;
14066 parser->in_declarator_p = saved_in_declarator_p;
14068 /* Consume the `('. */
14069 cp_lexer_consume_token (parser->lexer);
14070 /* Parse the nested declarator. */
14071 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14072 parser->in_type_id_in_expr_p = true;
14073 declarator
14074 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
14075 /*parenthesized_p=*/NULL,
14076 member_p);
14077 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14078 first = false;
14079 /* Expect a `)'. */
14080 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
14081 declarator = cp_error_declarator;
14082 if (declarator == cp_error_declarator)
14083 break;
14085 goto handle_declarator;
14087 /* Otherwise, we must be done. */
14088 else
14089 break;
14091 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14092 && token->type == CPP_OPEN_SQUARE)
14094 /* Parse an array-declarator. */
14095 tree bounds;
14097 if (ctor_dtor_or_conv_p)
14098 *ctor_dtor_or_conv_p = 0;
14100 first = false;
14101 parser->default_arg_ok_p = false;
14102 parser->in_declarator_p = true;
14103 /* Consume the `['. */
14104 cp_lexer_consume_token (parser->lexer);
14105 /* Peek at the next token. */
14106 token = cp_lexer_peek_token (parser->lexer);
14107 /* If the next token is `]', then there is no
14108 constant-expression. */
14109 if (token->type != CPP_CLOSE_SQUARE)
14111 bool non_constant_p;
14113 bounds
14114 = cp_parser_constant_expression (parser,
14115 /*allow_non_constant=*/true,
14116 &non_constant_p);
14117 if (!non_constant_p)
14118 bounds = fold_non_dependent_expr (bounds);
14119 /* Normally, the array bound must be an integral constant
14120 expression. However, as an extension, we allow VLAs
14121 in function scopes as long as they aren't part of a
14122 parameter declaration. */
14123 else if (!parser->in_function_body
14124 || current_binding_level->kind == sk_function_parms)
14126 cp_parser_error (parser,
14127 "array bound is not an integer constant");
14128 bounds = error_mark_node;
14130 else if (processing_template_decl && !error_operand_p (bounds))
14132 /* Remember this wasn't a constant-expression. */
14133 bounds = build_nop (TREE_TYPE (bounds), bounds);
14134 TREE_SIDE_EFFECTS (bounds) = 1;
14137 else
14138 bounds = NULL_TREE;
14139 /* Look for the closing `]'. */
14140 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
14142 declarator = cp_error_declarator;
14143 break;
14146 declarator = make_array_declarator (declarator, bounds);
14148 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
14151 tree qualifying_scope;
14152 tree unqualified_name;
14153 special_function_kind sfk;
14154 bool abstract_ok;
14155 bool pack_expansion_p = false;
14156 cp_token *declarator_id_start_token;
14158 /* Parse a declarator-id */
14159 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
14160 if (abstract_ok)
14162 cp_parser_parse_tentatively (parser);
14164 /* If we see an ellipsis, we should be looking at a
14165 parameter pack. */
14166 if (token->type == CPP_ELLIPSIS)
14168 /* Consume the `...' */
14169 cp_lexer_consume_token (parser->lexer);
14171 pack_expansion_p = true;
14175 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
14176 unqualified_name
14177 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
14178 qualifying_scope = parser->scope;
14179 if (abstract_ok)
14181 bool okay = false;
14183 if (!unqualified_name && pack_expansion_p)
14185 /* Check whether an error occurred. */
14186 okay = !cp_parser_error_occurred (parser);
14188 /* We already consumed the ellipsis to mark a
14189 parameter pack, but we have no way to report it,
14190 so abort the tentative parse. We will be exiting
14191 immediately anyway. */
14192 cp_parser_abort_tentative_parse (parser);
14194 else
14195 okay = cp_parser_parse_definitely (parser);
14197 if (!okay)
14198 unqualified_name = error_mark_node;
14199 else if (unqualified_name
14200 && (qualifying_scope
14201 || (TREE_CODE (unqualified_name)
14202 != IDENTIFIER_NODE)))
14204 cp_parser_error (parser, "expected unqualified-id");
14205 unqualified_name = error_mark_node;
14209 if (!unqualified_name)
14210 return NULL;
14211 if (unqualified_name == error_mark_node)
14213 declarator = cp_error_declarator;
14214 pack_expansion_p = false;
14215 declarator->parameter_pack_p = false;
14216 break;
14219 if (qualifying_scope && at_namespace_scope_p ()
14220 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
14222 /* In the declaration of a member of a template class
14223 outside of the class itself, the SCOPE will sometimes
14224 be a TYPENAME_TYPE. For example, given:
14226 template <typename T>
14227 int S<T>::R::i = 3;
14229 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
14230 this context, we must resolve S<T>::R to an ordinary
14231 type, rather than a typename type.
14233 The reason we normally avoid resolving TYPENAME_TYPEs
14234 is that a specialization of `S' might render
14235 `S<T>::R' not a type. However, if `S' is
14236 specialized, then this `i' will not be used, so there
14237 is no harm in resolving the types here. */
14238 tree type;
14240 /* Resolve the TYPENAME_TYPE. */
14241 type = resolve_typename_type (qualifying_scope,
14242 /*only_current_p=*/false);
14243 /* If that failed, the declarator is invalid. */
14244 if (TREE_CODE (type) == TYPENAME_TYPE)
14246 if (typedef_variant_p (type))
14247 error_at (declarator_id_start_token->location,
14248 "cannot define member of dependent typedef "
14249 "%qT", type);
14250 else
14251 error_at (declarator_id_start_token->location,
14252 "%<%T::%E%> is not a type",
14253 TYPE_CONTEXT (qualifying_scope),
14254 TYPE_IDENTIFIER (qualifying_scope));
14256 qualifying_scope = type;
14259 sfk = sfk_none;
14261 if (unqualified_name)
14263 tree class_type;
14265 if (qualifying_scope
14266 && CLASS_TYPE_P (qualifying_scope))
14267 class_type = qualifying_scope;
14268 else
14269 class_type = current_class_type;
14271 if (TREE_CODE (unqualified_name) == TYPE_DECL)
14273 tree name_type = TREE_TYPE (unqualified_name);
14274 if (class_type && same_type_p (name_type, class_type))
14276 if (qualifying_scope
14277 && CLASSTYPE_USE_TEMPLATE (name_type))
14279 error_at (declarator_id_start_token->location,
14280 "invalid use of constructor as a template");
14281 inform (declarator_id_start_token->location,
14282 "use %<%T::%D%> instead of %<%T::%D%> to "
14283 "name the constructor in a qualified name",
14284 class_type,
14285 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
14286 class_type, name_type);
14287 declarator = cp_error_declarator;
14288 break;
14290 else
14291 unqualified_name = constructor_name (class_type);
14293 else
14295 /* We do not attempt to print the declarator
14296 here because we do not have enough
14297 information about its original syntactic
14298 form. */
14299 cp_parser_error (parser, "invalid declarator");
14300 declarator = cp_error_declarator;
14301 break;
14305 if (class_type)
14307 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
14308 sfk = sfk_destructor;
14309 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
14310 sfk = sfk_conversion;
14311 else if (/* There's no way to declare a constructor
14312 for an anonymous type, even if the type
14313 got a name for linkage purposes. */
14314 !TYPE_WAS_ANONYMOUS (class_type)
14315 && constructor_name_p (unqualified_name,
14316 class_type))
14318 unqualified_name = constructor_name (class_type);
14319 sfk = sfk_constructor;
14321 else if (is_overloaded_fn (unqualified_name)
14322 && DECL_CONSTRUCTOR_P (get_first_fn
14323 (unqualified_name)))
14324 sfk = sfk_constructor;
14326 if (ctor_dtor_or_conv_p && sfk != sfk_none)
14327 *ctor_dtor_or_conv_p = -1;
14330 declarator = make_id_declarator (qualifying_scope,
14331 unqualified_name,
14332 sfk);
14333 declarator->id_loc = token->location;
14334 declarator->parameter_pack_p = pack_expansion_p;
14336 if (pack_expansion_p)
14337 maybe_warn_variadic_templates ();
14340 handle_declarator:;
14341 scope = get_scope_of_declarator (declarator);
14342 if (scope)
14343 /* Any names that appear after the declarator-id for a
14344 member are looked up in the containing scope. */
14345 pushed_scope = push_scope (scope);
14346 parser->in_declarator_p = true;
14347 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
14348 || (declarator && declarator->kind == cdk_id))
14349 /* Default args are only allowed on function
14350 declarations. */
14351 parser->default_arg_ok_p = saved_default_arg_ok_p;
14352 else
14353 parser->default_arg_ok_p = false;
14355 first = false;
14357 /* We're done. */
14358 else
14359 break;
14362 /* For an abstract declarator, we might wind up with nothing at this
14363 point. That's an error; the declarator is not optional. */
14364 if (!declarator)
14365 cp_parser_error (parser, "expected declarator");
14367 /* If we entered a scope, we must exit it now. */
14368 if (pushed_scope)
14369 pop_scope (pushed_scope);
14371 parser->default_arg_ok_p = saved_default_arg_ok_p;
14372 parser->in_declarator_p = saved_in_declarator_p;
14374 return declarator;
14377 /* Parse a ptr-operator.
14379 ptr-operator:
14380 * cv-qualifier-seq [opt]
14382 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
14384 GNU Extension:
14386 ptr-operator:
14387 & cv-qualifier-seq [opt]
14389 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
14390 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
14391 an rvalue reference. In the case of a pointer-to-member, *TYPE is
14392 filled in with the TYPE containing the member. *CV_QUALS is
14393 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
14394 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
14395 Note that the tree codes returned by this function have nothing
14396 to do with the types of trees that will be eventually be created
14397 to represent the pointer or reference type being parsed. They are
14398 just constants with suggestive names. */
14399 static enum tree_code
14400 cp_parser_ptr_operator (cp_parser* parser,
14401 tree* type,
14402 cp_cv_quals *cv_quals)
14404 enum tree_code code = ERROR_MARK;
14405 cp_token *token;
14407 /* Assume that it's not a pointer-to-member. */
14408 *type = NULL_TREE;
14409 /* And that there are no cv-qualifiers. */
14410 *cv_quals = TYPE_UNQUALIFIED;
14412 /* Peek at the next token. */
14413 token = cp_lexer_peek_token (parser->lexer);
14415 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
14416 if (token->type == CPP_MULT)
14417 code = INDIRECT_REF;
14418 else if (token->type == CPP_AND)
14419 code = ADDR_EXPR;
14420 else if ((cxx_dialect != cxx98) &&
14421 token->type == CPP_AND_AND) /* C++0x only */
14422 code = NON_LVALUE_EXPR;
14424 if (code != ERROR_MARK)
14426 /* Consume the `*', `&' or `&&'. */
14427 cp_lexer_consume_token (parser->lexer);
14429 /* A `*' can be followed by a cv-qualifier-seq, and so can a
14430 `&', if we are allowing GNU extensions. (The only qualifier
14431 that can legally appear after `&' is `restrict', but that is
14432 enforced during semantic analysis. */
14433 if (code == INDIRECT_REF
14434 || cp_parser_allow_gnu_extensions_p (parser))
14435 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14437 else
14439 /* Try the pointer-to-member case. */
14440 cp_parser_parse_tentatively (parser);
14441 /* Look for the optional `::' operator. */
14442 cp_parser_global_scope_opt (parser,
14443 /*current_scope_valid_p=*/false);
14444 /* Look for the nested-name specifier. */
14445 token = cp_lexer_peek_token (parser->lexer);
14446 cp_parser_nested_name_specifier (parser,
14447 /*typename_keyword_p=*/false,
14448 /*check_dependency_p=*/true,
14449 /*type_p=*/false,
14450 /*is_declaration=*/false);
14451 /* If we found it, and the next token is a `*', then we are
14452 indeed looking at a pointer-to-member operator. */
14453 if (!cp_parser_error_occurred (parser)
14454 && cp_parser_require (parser, CPP_MULT, "%<*%>"))
14456 /* Indicate that the `*' operator was used. */
14457 code = INDIRECT_REF;
14459 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
14460 error_at (token->location, "%qD is a namespace", parser->scope);
14461 else
14463 /* The type of which the member is a member is given by the
14464 current SCOPE. */
14465 *type = parser->scope;
14466 /* The next name will not be qualified. */
14467 parser->scope = NULL_TREE;
14468 parser->qualifying_scope = NULL_TREE;
14469 parser->object_scope = NULL_TREE;
14470 /* Look for the optional cv-qualifier-seq. */
14471 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14474 /* If that didn't work we don't have a ptr-operator. */
14475 if (!cp_parser_parse_definitely (parser))
14476 cp_parser_error (parser, "expected ptr-operator");
14479 return code;
14482 /* Parse an (optional) cv-qualifier-seq.
14484 cv-qualifier-seq:
14485 cv-qualifier cv-qualifier-seq [opt]
14487 cv-qualifier:
14488 const
14489 volatile
14491 GNU Extension:
14493 cv-qualifier:
14494 __restrict__
14496 Returns a bitmask representing the cv-qualifiers. */
14498 static cp_cv_quals
14499 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
14501 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
14503 while (true)
14505 cp_token *token;
14506 cp_cv_quals cv_qualifier;
14508 /* Peek at the next token. */
14509 token = cp_lexer_peek_token (parser->lexer);
14510 /* See if it's a cv-qualifier. */
14511 switch (token->keyword)
14513 case RID_CONST:
14514 cv_qualifier = TYPE_QUAL_CONST;
14515 break;
14517 case RID_VOLATILE:
14518 cv_qualifier = TYPE_QUAL_VOLATILE;
14519 break;
14521 case RID_RESTRICT:
14522 cv_qualifier = TYPE_QUAL_RESTRICT;
14523 break;
14525 default:
14526 cv_qualifier = TYPE_UNQUALIFIED;
14527 break;
14530 if (!cv_qualifier)
14531 break;
14533 if (cv_quals & cv_qualifier)
14535 error_at (token->location, "duplicate cv-qualifier");
14536 cp_lexer_purge_token (parser->lexer);
14538 else
14540 cp_lexer_consume_token (parser->lexer);
14541 cv_quals |= cv_qualifier;
14545 return cv_quals;
14548 /* Parse a late-specified return type, if any. This is not a separate
14549 non-terminal, but part of a function declarator, which looks like
14551 -> trailing-type-specifier-seq abstract-declarator(opt)
14553 Returns the type indicated by the type-id. */
14555 static tree
14556 cp_parser_late_return_type_opt (cp_parser* parser)
14558 cp_token *token;
14560 /* Peek at the next token. */
14561 token = cp_lexer_peek_token (parser->lexer);
14562 /* A late-specified return type is indicated by an initial '->'. */
14563 if (token->type != CPP_DEREF)
14564 return NULL_TREE;
14566 /* Consume the ->. */
14567 cp_lexer_consume_token (parser->lexer);
14569 return cp_parser_trailing_type_id (parser);
14572 /* Parse a declarator-id.
14574 declarator-id:
14575 id-expression
14576 :: [opt] nested-name-specifier [opt] type-name
14578 In the `id-expression' case, the value returned is as for
14579 cp_parser_id_expression if the id-expression was an unqualified-id.
14580 If the id-expression was a qualified-id, then a SCOPE_REF is
14581 returned. The first operand is the scope (either a NAMESPACE_DECL
14582 or TREE_TYPE), but the second is still just a representation of an
14583 unqualified-id. */
14585 static tree
14586 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
14588 tree id;
14589 /* The expression must be an id-expression. Assume that qualified
14590 names are the names of types so that:
14592 template <class T>
14593 int S<T>::R::i = 3;
14595 will work; we must treat `S<T>::R' as the name of a type.
14596 Similarly, assume that qualified names are templates, where
14597 required, so that:
14599 template <class T>
14600 int S<T>::R<T>::i = 3;
14602 will work, too. */
14603 id = cp_parser_id_expression (parser,
14604 /*template_keyword_p=*/false,
14605 /*check_dependency_p=*/false,
14606 /*template_p=*/NULL,
14607 /*declarator_p=*/true,
14608 optional_p);
14609 if (id && BASELINK_P (id))
14610 id = BASELINK_FUNCTIONS (id);
14611 return id;
14614 /* Parse a type-id.
14616 type-id:
14617 type-specifier-seq abstract-declarator [opt]
14619 Returns the TYPE specified. */
14621 static tree
14622 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
14623 bool is_trailing_return)
14625 cp_decl_specifier_seq type_specifier_seq;
14626 cp_declarator *abstract_declarator;
14628 /* Parse the type-specifier-seq. */
14629 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14630 is_trailing_return,
14631 &type_specifier_seq);
14632 if (type_specifier_seq.type == error_mark_node)
14633 return error_mark_node;
14635 /* There might or might not be an abstract declarator. */
14636 cp_parser_parse_tentatively (parser);
14637 /* Look for the declarator. */
14638 abstract_declarator
14639 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
14640 /*parenthesized_p=*/NULL,
14641 /*member_p=*/false);
14642 /* Check to see if there really was a declarator. */
14643 if (!cp_parser_parse_definitely (parser))
14644 abstract_declarator = NULL;
14646 if (type_specifier_seq.type
14647 && type_uses_auto (type_specifier_seq.type))
14649 /* A type-id with type 'auto' is only ok if the abstract declarator
14650 is a function declarator with a late-specified return type. */
14651 if (abstract_declarator
14652 && abstract_declarator->kind == cdk_function
14653 && abstract_declarator->u.function.late_return_type)
14654 /* OK */;
14655 else
14657 error ("invalid use of %<auto%>");
14658 return error_mark_node;
14662 return groktypename (&type_specifier_seq, abstract_declarator,
14663 is_template_arg);
14666 static tree cp_parser_type_id (cp_parser *parser)
14668 return cp_parser_type_id_1 (parser, false, false);
14671 static tree cp_parser_template_type_arg (cp_parser *parser)
14673 return cp_parser_type_id_1 (parser, true, false);
14676 static tree cp_parser_trailing_type_id (cp_parser *parser)
14678 return cp_parser_type_id_1 (parser, false, true);
14681 /* Parse a type-specifier-seq.
14683 type-specifier-seq:
14684 type-specifier type-specifier-seq [opt]
14686 GNU extension:
14688 type-specifier-seq:
14689 attributes type-specifier-seq [opt]
14691 If IS_DECLARATION is true, we are at the start of a "condition" or
14692 exception-declaration, so we might be followed by a declarator-id.
14694 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
14695 i.e. we've just seen "->".
14697 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
14699 static void
14700 cp_parser_type_specifier_seq (cp_parser* parser,
14701 bool is_declaration,
14702 bool is_trailing_return,
14703 cp_decl_specifier_seq *type_specifier_seq)
14705 bool seen_type_specifier = false;
14706 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
14707 cp_token *start_token = NULL;
14709 /* Clear the TYPE_SPECIFIER_SEQ. */
14710 clear_decl_specs (type_specifier_seq);
14712 /* In the context of a trailing return type, enum E { } is an
14713 elaborated-type-specifier followed by a function-body, not an
14714 enum-specifier. */
14715 if (is_trailing_return)
14716 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
14718 /* Parse the type-specifiers and attributes. */
14719 while (true)
14721 tree type_specifier;
14722 bool is_cv_qualifier;
14724 /* Check for attributes first. */
14725 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
14727 type_specifier_seq->attributes =
14728 chainon (type_specifier_seq->attributes,
14729 cp_parser_attributes_opt (parser));
14730 continue;
14733 /* record the token of the beginning of the type specifier seq,
14734 for error reporting purposes*/
14735 if (!start_token)
14736 start_token = cp_lexer_peek_token (parser->lexer);
14738 /* Look for the type-specifier. */
14739 type_specifier = cp_parser_type_specifier (parser,
14740 flags,
14741 type_specifier_seq,
14742 /*is_declaration=*/false,
14743 NULL,
14744 &is_cv_qualifier);
14745 if (!type_specifier)
14747 /* If the first type-specifier could not be found, this is not a
14748 type-specifier-seq at all. */
14749 if (!seen_type_specifier)
14751 cp_parser_error (parser, "expected type-specifier");
14752 type_specifier_seq->type = error_mark_node;
14753 return;
14755 /* If subsequent type-specifiers could not be found, the
14756 type-specifier-seq is complete. */
14757 break;
14760 seen_type_specifier = true;
14761 /* The standard says that a condition can be:
14763 type-specifier-seq declarator = assignment-expression
14765 However, given:
14767 struct S {};
14768 if (int S = ...)
14770 we should treat the "S" as a declarator, not as a
14771 type-specifier. The standard doesn't say that explicitly for
14772 type-specifier-seq, but it does say that for
14773 decl-specifier-seq in an ordinary declaration. Perhaps it
14774 would be clearer just to allow a decl-specifier-seq here, and
14775 then add a semantic restriction that if any decl-specifiers
14776 that are not type-specifiers appear, the program is invalid. */
14777 if (is_declaration && !is_cv_qualifier)
14778 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
14781 cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
14784 /* Parse a parameter-declaration-clause.
14786 parameter-declaration-clause:
14787 parameter-declaration-list [opt] ... [opt]
14788 parameter-declaration-list , ...
14790 Returns a representation for the parameter declarations. A return
14791 value of NULL indicates a parameter-declaration-clause consisting
14792 only of an ellipsis. */
14794 static tree
14795 cp_parser_parameter_declaration_clause (cp_parser* parser)
14797 tree parameters;
14798 cp_token *token;
14799 bool ellipsis_p;
14800 bool is_error;
14802 /* Peek at the next token. */
14803 token = cp_lexer_peek_token (parser->lexer);
14804 /* Check for trivial parameter-declaration-clauses. */
14805 if (token->type == CPP_ELLIPSIS)
14807 /* Consume the `...' token. */
14808 cp_lexer_consume_token (parser->lexer);
14809 return NULL_TREE;
14811 else if (token->type == CPP_CLOSE_PAREN)
14812 /* There are no parameters. */
14814 #ifndef NO_IMPLICIT_EXTERN_C
14815 if (in_system_header && current_class_type == NULL
14816 && current_lang_name == lang_name_c)
14817 return NULL_TREE;
14818 else
14819 #endif
14820 return void_list_node;
14822 /* Check for `(void)', too, which is a special case. */
14823 else if (token->keyword == RID_VOID
14824 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
14825 == CPP_CLOSE_PAREN))
14827 /* Consume the `void' token. */
14828 cp_lexer_consume_token (parser->lexer);
14829 /* There are no parameters. */
14830 return void_list_node;
14833 /* Parse the parameter-declaration-list. */
14834 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
14835 /* If a parse error occurred while parsing the
14836 parameter-declaration-list, then the entire
14837 parameter-declaration-clause is erroneous. */
14838 if (is_error)
14839 return NULL;
14841 /* Peek at the next token. */
14842 token = cp_lexer_peek_token (parser->lexer);
14843 /* If it's a `,', the clause should terminate with an ellipsis. */
14844 if (token->type == CPP_COMMA)
14846 /* Consume the `,'. */
14847 cp_lexer_consume_token (parser->lexer);
14848 /* Expect an ellipsis. */
14849 ellipsis_p
14850 = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
14852 /* It might also be `...' if the optional trailing `,' was
14853 omitted. */
14854 else if (token->type == CPP_ELLIPSIS)
14856 /* Consume the `...' token. */
14857 cp_lexer_consume_token (parser->lexer);
14858 /* And remember that we saw it. */
14859 ellipsis_p = true;
14861 else
14862 ellipsis_p = false;
14864 /* Finish the parameter list. */
14865 if (!ellipsis_p)
14866 parameters = chainon (parameters, void_list_node);
14868 return parameters;
14871 /* Parse a parameter-declaration-list.
14873 parameter-declaration-list:
14874 parameter-declaration
14875 parameter-declaration-list , parameter-declaration
14877 Returns a representation of the parameter-declaration-list, as for
14878 cp_parser_parameter_declaration_clause. However, the
14879 `void_list_node' is never appended to the list. Upon return,
14880 *IS_ERROR will be true iff an error occurred. */
14882 static tree
14883 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
14885 tree parameters = NULL_TREE;
14886 tree *tail = &parameters;
14887 bool saved_in_unbraced_linkage_specification_p;
14888 int index = 0;
14890 /* Assume all will go well. */
14891 *is_error = false;
14892 /* The special considerations that apply to a function within an
14893 unbraced linkage specifications do not apply to the parameters
14894 to the function. */
14895 saved_in_unbraced_linkage_specification_p
14896 = parser->in_unbraced_linkage_specification_p;
14897 parser->in_unbraced_linkage_specification_p = false;
14899 /* Look for more parameters. */
14900 while (true)
14902 cp_parameter_declarator *parameter;
14903 tree decl = error_mark_node;
14904 bool parenthesized_p;
14905 /* Parse the parameter. */
14906 parameter
14907 = cp_parser_parameter_declaration (parser,
14908 /*template_parm_p=*/false,
14909 &parenthesized_p);
14911 /* We don't know yet if the enclosing context is deprecated, so wait
14912 and warn in grokparms if appropriate. */
14913 deprecated_state = DEPRECATED_SUPPRESS;
14915 if (parameter)
14916 decl = grokdeclarator (parameter->declarator,
14917 &parameter->decl_specifiers,
14918 PARM,
14919 parameter->default_argument != NULL_TREE,
14920 &parameter->decl_specifiers.attributes);
14922 deprecated_state = DEPRECATED_NORMAL;
14924 /* If a parse error occurred parsing the parameter declaration,
14925 then the entire parameter-declaration-list is erroneous. */
14926 if (decl == error_mark_node)
14928 *is_error = true;
14929 parameters = error_mark_node;
14930 break;
14933 if (parameter->decl_specifiers.attributes)
14934 cplus_decl_attributes (&decl,
14935 parameter->decl_specifiers.attributes,
14937 if (DECL_NAME (decl))
14938 decl = pushdecl (decl);
14940 if (decl != error_mark_node)
14942 retrofit_lang_decl (decl);
14943 DECL_PARM_INDEX (decl) = ++index;
14946 /* Add the new parameter to the list. */
14947 *tail = build_tree_list (parameter->default_argument, decl);
14948 tail = &TREE_CHAIN (*tail);
14950 /* Peek at the next token. */
14951 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
14952 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
14953 /* These are for Objective-C++ */
14954 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14955 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14956 /* The parameter-declaration-list is complete. */
14957 break;
14958 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14960 cp_token *token;
14962 /* Peek at the next token. */
14963 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14964 /* If it's an ellipsis, then the list is complete. */
14965 if (token->type == CPP_ELLIPSIS)
14966 break;
14967 /* Otherwise, there must be more parameters. Consume the
14968 `,'. */
14969 cp_lexer_consume_token (parser->lexer);
14970 /* When parsing something like:
14972 int i(float f, double d)
14974 we can tell after seeing the declaration for "f" that we
14975 are not looking at an initialization of a variable "i",
14976 but rather at the declaration of a function "i".
14978 Due to the fact that the parsing of template arguments
14979 (as specified to a template-id) requires backtracking we
14980 cannot use this technique when inside a template argument
14981 list. */
14982 if (!parser->in_template_argument_list_p
14983 && !parser->in_type_id_in_expr_p
14984 && cp_parser_uncommitted_to_tentative_parse_p (parser)
14985 /* However, a parameter-declaration of the form
14986 "foat(f)" (which is a valid declaration of a
14987 parameter "f") can also be interpreted as an
14988 expression (the conversion of "f" to "float"). */
14989 && !parenthesized_p)
14990 cp_parser_commit_to_tentative_parse (parser);
14992 else
14994 cp_parser_error (parser, "expected %<,%> or %<...%>");
14995 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14996 cp_parser_skip_to_closing_parenthesis (parser,
14997 /*recovering=*/true,
14998 /*or_comma=*/false,
14999 /*consume_paren=*/false);
15000 break;
15004 parser->in_unbraced_linkage_specification_p
15005 = saved_in_unbraced_linkage_specification_p;
15007 return parameters;
15010 /* Parse a parameter declaration.
15012 parameter-declaration:
15013 decl-specifier-seq ... [opt] declarator
15014 decl-specifier-seq declarator = assignment-expression
15015 decl-specifier-seq ... [opt] abstract-declarator [opt]
15016 decl-specifier-seq abstract-declarator [opt] = assignment-expression
15018 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
15019 declares a template parameter. (In that case, a non-nested `>'
15020 token encountered during the parsing of the assignment-expression
15021 is not interpreted as a greater-than operator.)
15023 Returns a representation of the parameter, or NULL if an error
15024 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
15025 true iff the declarator is of the form "(p)". */
15027 static cp_parameter_declarator *
15028 cp_parser_parameter_declaration (cp_parser *parser,
15029 bool template_parm_p,
15030 bool *parenthesized_p)
15032 int declares_class_or_enum;
15033 cp_decl_specifier_seq decl_specifiers;
15034 cp_declarator *declarator;
15035 tree default_argument;
15036 cp_token *token = NULL, *declarator_token_start = NULL;
15037 const char *saved_message;
15039 /* In a template parameter, `>' is not an operator.
15041 [temp.param]
15043 When parsing a default template-argument for a non-type
15044 template-parameter, the first non-nested `>' is taken as the end
15045 of the template parameter-list rather than a greater-than
15046 operator. */
15048 /* Type definitions may not appear in parameter types. */
15049 saved_message = parser->type_definition_forbidden_message;
15050 parser->type_definition_forbidden_message
15051 = G_("types may not be defined in parameter types");
15053 /* Parse the declaration-specifiers. */
15054 cp_parser_decl_specifier_seq (parser,
15055 CP_PARSER_FLAGS_NONE,
15056 &decl_specifiers,
15057 &declares_class_or_enum);
15059 /* Complain about missing 'typename' or other invalid type names. */
15060 if (!decl_specifiers.any_type_specifiers_p)
15061 cp_parser_parse_and_diagnose_invalid_type_name (parser);
15063 /* If an error occurred, there's no reason to attempt to parse the
15064 rest of the declaration. */
15065 if (cp_parser_error_occurred (parser))
15067 parser->type_definition_forbidden_message = saved_message;
15068 return NULL;
15071 /* Peek at the next token. */
15072 token = cp_lexer_peek_token (parser->lexer);
15074 /* If the next token is a `)', `,', `=', `>', or `...', then there
15075 is no declarator. However, when variadic templates are enabled,
15076 there may be a declarator following `...'. */
15077 if (token->type == CPP_CLOSE_PAREN
15078 || token->type == CPP_COMMA
15079 || token->type == CPP_EQ
15080 || token->type == CPP_GREATER)
15082 declarator = NULL;
15083 if (parenthesized_p)
15084 *parenthesized_p = false;
15086 /* Otherwise, there should be a declarator. */
15087 else
15089 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
15090 parser->default_arg_ok_p = false;
15092 /* After seeing a decl-specifier-seq, if the next token is not a
15093 "(", there is no possibility that the code is a valid
15094 expression. Therefore, if parsing tentatively, we commit at
15095 this point. */
15096 if (!parser->in_template_argument_list_p
15097 /* In an expression context, having seen:
15099 (int((char ...
15101 we cannot be sure whether we are looking at a
15102 function-type (taking a "char" as a parameter) or a cast
15103 of some object of type "char" to "int". */
15104 && !parser->in_type_id_in_expr_p
15105 && cp_parser_uncommitted_to_tentative_parse_p (parser)
15106 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
15107 cp_parser_commit_to_tentative_parse (parser);
15108 /* Parse the declarator. */
15109 declarator_token_start = token;
15110 declarator = cp_parser_declarator (parser,
15111 CP_PARSER_DECLARATOR_EITHER,
15112 /*ctor_dtor_or_conv_p=*/NULL,
15113 parenthesized_p,
15114 /*member_p=*/false);
15115 parser->default_arg_ok_p = saved_default_arg_ok_p;
15116 /* After the declarator, allow more attributes. */
15117 decl_specifiers.attributes
15118 = chainon (decl_specifiers.attributes,
15119 cp_parser_attributes_opt (parser));
15122 /* If the next token is an ellipsis, and we have not seen a
15123 declarator name, and the type of the declarator contains parameter
15124 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
15125 a parameter pack expansion expression. Otherwise, leave the
15126 ellipsis for a C-style variadic function. */
15127 token = cp_lexer_peek_token (parser->lexer);
15128 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15130 tree type = decl_specifiers.type;
15132 if (type && DECL_P (type))
15133 type = TREE_TYPE (type);
15135 if (type
15136 && TREE_CODE (type) != TYPE_PACK_EXPANSION
15137 && declarator_can_be_parameter_pack (declarator)
15138 && (!declarator || !declarator->parameter_pack_p)
15139 && uses_parameter_packs (type))
15141 /* Consume the `...'. */
15142 cp_lexer_consume_token (parser->lexer);
15143 maybe_warn_variadic_templates ();
15145 /* Build a pack expansion type */
15146 if (declarator)
15147 declarator->parameter_pack_p = true;
15148 else
15149 decl_specifiers.type = make_pack_expansion (type);
15153 /* The restriction on defining new types applies only to the type
15154 of the parameter, not to the default argument. */
15155 parser->type_definition_forbidden_message = saved_message;
15157 /* If the next token is `=', then process a default argument. */
15158 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15160 /* Consume the `='. */
15161 cp_lexer_consume_token (parser->lexer);
15163 /* If we are defining a class, then the tokens that make up the
15164 default argument must be saved and processed later. */
15165 if (!template_parm_p && at_class_scope_p ()
15166 && TYPE_BEING_DEFINED (current_class_type)
15167 && !LAMBDA_TYPE_P (current_class_type))
15169 unsigned depth = 0;
15170 int maybe_template_id = 0;
15171 cp_token *first_token;
15172 cp_token *token;
15174 /* Add tokens until we have processed the entire default
15175 argument. We add the range [first_token, token). */
15176 first_token = cp_lexer_peek_token (parser->lexer);
15177 while (true)
15179 bool done = false;
15181 /* Peek at the next token. */
15182 token = cp_lexer_peek_token (parser->lexer);
15183 /* What we do depends on what token we have. */
15184 switch (token->type)
15186 /* In valid code, a default argument must be
15187 immediately followed by a `,' `)', or `...'. */
15188 case CPP_COMMA:
15189 if (depth == 0 && maybe_template_id)
15191 /* If we've seen a '<', we might be in a
15192 template-argument-list. Until Core issue 325 is
15193 resolved, we don't know how this situation ought
15194 to be handled, so try to DTRT. We check whether
15195 what comes after the comma is a valid parameter
15196 declaration list. If it is, then the comma ends
15197 the default argument; otherwise the default
15198 argument continues. */
15199 bool error = false;
15201 /* Set ITALP so cp_parser_parameter_declaration_list
15202 doesn't decide to commit to this parse. */
15203 bool saved_italp = parser->in_template_argument_list_p;
15204 parser->in_template_argument_list_p = true;
15206 cp_parser_parse_tentatively (parser);
15207 cp_lexer_consume_token (parser->lexer);
15208 cp_parser_parameter_declaration_list (parser, &error);
15209 if (!cp_parser_error_occurred (parser) && !error)
15210 done = true;
15211 cp_parser_abort_tentative_parse (parser);
15213 parser->in_template_argument_list_p = saved_italp;
15214 break;
15216 case CPP_CLOSE_PAREN:
15217 case CPP_ELLIPSIS:
15218 /* If we run into a non-nested `;', `}', or `]',
15219 then the code is invalid -- but the default
15220 argument is certainly over. */
15221 case CPP_SEMICOLON:
15222 case CPP_CLOSE_BRACE:
15223 case CPP_CLOSE_SQUARE:
15224 if (depth == 0)
15225 done = true;
15226 /* Update DEPTH, if necessary. */
15227 else if (token->type == CPP_CLOSE_PAREN
15228 || token->type == CPP_CLOSE_BRACE
15229 || token->type == CPP_CLOSE_SQUARE)
15230 --depth;
15231 break;
15233 case CPP_OPEN_PAREN:
15234 case CPP_OPEN_SQUARE:
15235 case CPP_OPEN_BRACE:
15236 ++depth;
15237 break;
15239 case CPP_LESS:
15240 if (depth == 0)
15241 /* This might be the comparison operator, or it might
15242 start a template argument list. */
15243 ++maybe_template_id;
15244 break;
15246 case CPP_RSHIFT:
15247 if (cxx_dialect == cxx98)
15248 break;
15249 /* Fall through for C++0x, which treats the `>>'
15250 operator like two `>' tokens in certain
15251 cases. */
15253 case CPP_GREATER:
15254 if (depth == 0)
15256 /* This might be an operator, or it might close a
15257 template argument list. But if a previous '<'
15258 started a template argument list, this will have
15259 closed it, so we can't be in one anymore. */
15260 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
15261 if (maybe_template_id < 0)
15262 maybe_template_id = 0;
15264 break;
15266 /* If we run out of tokens, issue an error message. */
15267 case CPP_EOF:
15268 case CPP_PRAGMA_EOL:
15269 error_at (token->location, "file ends in default argument");
15270 done = true;
15271 break;
15273 case CPP_NAME:
15274 case CPP_SCOPE:
15275 /* In these cases, we should look for template-ids.
15276 For example, if the default argument is
15277 `X<int, double>()', we need to do name lookup to
15278 figure out whether or not `X' is a template; if
15279 so, the `,' does not end the default argument.
15281 That is not yet done. */
15282 break;
15284 default:
15285 break;
15288 /* If we've reached the end, stop. */
15289 if (done)
15290 break;
15292 /* Add the token to the token block. */
15293 token = cp_lexer_consume_token (parser->lexer);
15296 /* Create a DEFAULT_ARG to represent the unparsed default
15297 argument. */
15298 default_argument = make_node (DEFAULT_ARG);
15299 DEFARG_TOKENS (default_argument)
15300 = cp_token_cache_new (first_token, token);
15301 DEFARG_INSTANTIATIONS (default_argument) = NULL;
15303 /* Outside of a class definition, we can just parse the
15304 assignment-expression. */
15305 else
15307 token = cp_lexer_peek_token (parser->lexer);
15308 default_argument
15309 = cp_parser_default_argument (parser, template_parm_p);
15312 if (!parser->default_arg_ok_p)
15314 if (flag_permissive)
15315 warning (0, "deprecated use of default argument for parameter of non-function");
15316 else
15318 error_at (token->location,
15319 "default arguments are only "
15320 "permitted for function parameters");
15321 default_argument = NULL_TREE;
15324 else if ((declarator && declarator->parameter_pack_p)
15325 || (decl_specifiers.type
15326 && PACK_EXPANSION_P (decl_specifiers.type)))
15328 /* Find the name of the parameter pack. */
15329 cp_declarator *id_declarator = declarator;
15330 while (id_declarator && id_declarator->kind != cdk_id)
15331 id_declarator = id_declarator->declarator;
15333 if (id_declarator && id_declarator->kind == cdk_id)
15334 error_at (declarator_token_start->location,
15335 template_parm_p
15336 ? "template parameter pack %qD"
15337 " cannot have a default argument"
15338 : "parameter pack %qD cannot have a default argument",
15339 id_declarator->u.id.unqualified_name);
15340 else
15341 error_at (declarator_token_start->location,
15342 template_parm_p
15343 ? "template parameter pack cannot have a default argument"
15344 : "parameter pack cannot have a default argument");
15346 default_argument = NULL_TREE;
15349 else
15350 default_argument = NULL_TREE;
15352 return make_parameter_declarator (&decl_specifiers,
15353 declarator,
15354 default_argument);
15357 /* Parse a default argument and return it.
15359 TEMPLATE_PARM_P is true if this is a default argument for a
15360 non-type template parameter. */
15361 static tree
15362 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
15364 tree default_argument = NULL_TREE;
15365 bool saved_greater_than_is_operator_p;
15366 bool saved_local_variables_forbidden_p;
15368 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
15369 set correctly. */
15370 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
15371 parser->greater_than_is_operator_p = !template_parm_p;
15372 /* Local variable names (and the `this' keyword) may not
15373 appear in a default argument. */
15374 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15375 parser->local_variables_forbidden_p = true;
15376 /* Parse the assignment-expression. */
15377 if (template_parm_p)
15378 push_deferring_access_checks (dk_no_deferred);
15379 default_argument
15380 = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
15381 if (template_parm_p)
15382 pop_deferring_access_checks ();
15383 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
15384 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15386 return default_argument;
15389 /* Parse a function-body.
15391 function-body:
15392 compound_statement */
15394 static void
15395 cp_parser_function_body (cp_parser *parser)
15397 cp_parser_compound_statement (parser, NULL, false);
15400 /* Parse a ctor-initializer-opt followed by a function-body. Return
15401 true if a ctor-initializer was present. */
15403 static bool
15404 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
15406 tree body;
15407 bool ctor_initializer_p;
15409 /* Begin the function body. */
15410 body = begin_function_body ();
15411 /* Parse the optional ctor-initializer. */
15412 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
15413 /* Parse the function-body. */
15414 cp_parser_function_body (parser);
15415 /* Finish the function body. */
15416 finish_function_body (body);
15418 return ctor_initializer_p;
15421 /* Parse an initializer.
15423 initializer:
15424 = initializer-clause
15425 ( expression-list )
15427 Returns an expression representing the initializer. If no
15428 initializer is present, NULL_TREE is returned.
15430 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
15431 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
15432 set to TRUE if there is no initializer present. If there is an
15433 initializer, and it is not a constant-expression, *NON_CONSTANT_P
15434 is set to true; otherwise it is set to false. */
15436 static tree
15437 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
15438 bool* non_constant_p)
15440 cp_token *token;
15441 tree init;
15443 /* Peek at the next token. */
15444 token = cp_lexer_peek_token (parser->lexer);
15446 /* Let our caller know whether or not this initializer was
15447 parenthesized. */
15448 *is_direct_init = (token->type != CPP_EQ);
15449 /* Assume that the initializer is constant. */
15450 *non_constant_p = false;
15452 if (token->type == CPP_EQ)
15454 /* Consume the `='. */
15455 cp_lexer_consume_token (parser->lexer);
15456 /* Parse the initializer-clause. */
15457 init = cp_parser_initializer_clause (parser, non_constant_p);
15459 else if (token->type == CPP_OPEN_PAREN)
15461 VEC(tree,gc) *vec;
15462 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
15463 /*cast_p=*/false,
15464 /*allow_expansion_p=*/true,
15465 non_constant_p);
15466 if (vec == NULL)
15467 return error_mark_node;
15468 init = build_tree_list_vec (vec);
15469 release_tree_vector (vec);
15471 else if (token->type == CPP_OPEN_BRACE)
15473 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
15474 init = cp_parser_braced_list (parser, non_constant_p);
15475 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
15477 else
15479 /* Anything else is an error. */
15480 cp_parser_error (parser, "expected initializer");
15481 init = error_mark_node;
15484 return init;
15487 /* Parse an initializer-clause.
15489 initializer-clause:
15490 assignment-expression
15491 braced-init-list
15493 Returns an expression representing the initializer.
15495 If the `assignment-expression' production is used the value
15496 returned is simply a representation for the expression.
15498 Otherwise, calls cp_parser_braced_list. */
15500 static tree
15501 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
15503 tree initializer;
15505 /* Assume the expression is constant. */
15506 *non_constant_p = false;
15508 /* If it is not a `{', then we are looking at an
15509 assignment-expression. */
15510 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
15512 initializer
15513 = cp_parser_constant_expression (parser,
15514 /*allow_non_constant_p=*/true,
15515 non_constant_p);
15516 if (!*non_constant_p)
15517 initializer = fold_non_dependent_expr (initializer);
15519 else
15520 initializer = cp_parser_braced_list (parser, non_constant_p);
15522 return initializer;
15525 /* Parse a brace-enclosed initializer list.
15527 braced-init-list:
15528 { initializer-list , [opt] }
15531 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
15532 the elements of the initializer-list (or NULL, if the last
15533 production is used). The TREE_TYPE for the CONSTRUCTOR will be
15534 NULL_TREE. There is no way to detect whether or not the optional
15535 trailing `,' was provided. NON_CONSTANT_P is as for
15536 cp_parser_initializer. */
15538 static tree
15539 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
15541 tree initializer;
15543 /* Consume the `{' token. */
15544 cp_lexer_consume_token (parser->lexer);
15545 /* Create a CONSTRUCTOR to represent the braced-initializer. */
15546 initializer = make_node (CONSTRUCTOR);
15547 /* If it's not a `}', then there is a non-trivial initializer. */
15548 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
15550 /* Parse the initializer list. */
15551 CONSTRUCTOR_ELTS (initializer)
15552 = cp_parser_initializer_list (parser, non_constant_p);
15553 /* A trailing `,' token is allowed. */
15554 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15555 cp_lexer_consume_token (parser->lexer);
15557 /* Now, there should be a trailing `}'. */
15558 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15559 TREE_TYPE (initializer) = init_list_type_node;
15560 return initializer;
15563 /* Parse an initializer-list.
15565 initializer-list:
15566 initializer-clause ... [opt]
15567 initializer-list , initializer-clause ... [opt]
15569 GNU Extension:
15571 initializer-list:
15572 identifier : initializer-clause
15573 initializer-list, identifier : initializer-clause
15575 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
15576 for the initializer. If the INDEX of the elt is non-NULL, it is the
15577 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
15578 as for cp_parser_initializer. */
15580 static VEC(constructor_elt,gc) *
15581 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
15583 VEC(constructor_elt,gc) *v = NULL;
15585 /* Assume all of the expressions are constant. */
15586 *non_constant_p = false;
15588 /* Parse the rest of the list. */
15589 while (true)
15591 cp_token *token;
15592 tree identifier;
15593 tree initializer;
15594 bool clause_non_constant_p;
15596 /* If the next token is an identifier and the following one is a
15597 colon, we are looking at the GNU designated-initializer
15598 syntax. */
15599 if (cp_parser_allow_gnu_extensions_p (parser)
15600 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
15601 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
15603 /* Warn the user that they are using an extension. */
15604 pedwarn (input_location, OPT_pedantic,
15605 "ISO C++ does not allow designated initializers");
15606 /* Consume the identifier. */
15607 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
15608 /* Consume the `:'. */
15609 cp_lexer_consume_token (parser->lexer);
15611 else
15612 identifier = NULL_TREE;
15614 /* Parse the initializer. */
15615 initializer = cp_parser_initializer_clause (parser,
15616 &clause_non_constant_p);
15617 /* If any clause is non-constant, so is the entire initializer. */
15618 if (clause_non_constant_p)
15619 *non_constant_p = true;
15621 /* If we have an ellipsis, this is an initializer pack
15622 expansion. */
15623 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15625 /* Consume the `...'. */
15626 cp_lexer_consume_token (parser->lexer);
15628 /* Turn the initializer into an initializer expansion. */
15629 initializer = make_pack_expansion (initializer);
15632 /* Add it to the vector. */
15633 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
15635 /* If the next token is not a comma, we have reached the end of
15636 the list. */
15637 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15638 break;
15640 /* Peek at the next token. */
15641 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15642 /* If the next token is a `}', then we're still done. An
15643 initializer-clause can have a trailing `,' after the
15644 initializer-list and before the closing `}'. */
15645 if (token->type == CPP_CLOSE_BRACE)
15646 break;
15648 /* Consume the `,' token. */
15649 cp_lexer_consume_token (parser->lexer);
15652 return v;
15655 /* Classes [gram.class] */
15657 /* Parse a class-name.
15659 class-name:
15660 identifier
15661 template-id
15663 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
15664 to indicate that names looked up in dependent types should be
15665 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
15666 keyword has been used to indicate that the name that appears next
15667 is a template. TAG_TYPE indicates the explicit tag given before
15668 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
15669 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
15670 is the class being defined in a class-head.
15672 Returns the TYPE_DECL representing the class. */
15674 static tree
15675 cp_parser_class_name (cp_parser *parser,
15676 bool typename_keyword_p,
15677 bool template_keyword_p,
15678 enum tag_types tag_type,
15679 bool check_dependency_p,
15680 bool class_head_p,
15681 bool is_declaration)
15683 tree decl;
15684 tree scope;
15685 bool typename_p;
15686 cp_token *token;
15687 tree identifier = NULL_TREE;
15689 /* All class-names start with an identifier. */
15690 token = cp_lexer_peek_token (parser->lexer);
15691 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
15693 cp_parser_error (parser, "expected class-name");
15694 return error_mark_node;
15697 /* PARSER->SCOPE can be cleared when parsing the template-arguments
15698 to a template-id, so we save it here. */
15699 scope = parser->scope;
15700 if (scope == error_mark_node)
15701 return error_mark_node;
15703 /* Any name names a type if we're following the `typename' keyword
15704 in a qualified name where the enclosing scope is type-dependent. */
15705 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
15706 && dependent_type_p (scope));
15707 /* Handle the common case (an identifier, but not a template-id)
15708 efficiently. */
15709 if (token->type == CPP_NAME
15710 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
15712 cp_token *identifier_token;
15713 bool ambiguous_p;
15715 /* Look for the identifier. */
15716 identifier_token = cp_lexer_peek_token (parser->lexer);
15717 ambiguous_p = identifier_token->ambiguous_p;
15718 identifier = cp_parser_identifier (parser);
15719 /* If the next token isn't an identifier, we are certainly not
15720 looking at a class-name. */
15721 if (identifier == error_mark_node)
15722 decl = error_mark_node;
15723 /* If we know this is a type-name, there's no need to look it
15724 up. */
15725 else if (typename_p)
15726 decl = identifier;
15727 else
15729 tree ambiguous_decls;
15730 /* If we already know that this lookup is ambiguous, then
15731 we've already issued an error message; there's no reason
15732 to check again. */
15733 if (ambiguous_p)
15735 cp_parser_simulate_error (parser);
15736 return error_mark_node;
15738 /* If the next token is a `::', then the name must be a type
15739 name.
15741 [basic.lookup.qual]
15743 During the lookup for a name preceding the :: scope
15744 resolution operator, object, function, and enumerator
15745 names are ignored. */
15746 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15747 tag_type = typename_type;
15748 /* Look up the name. */
15749 decl = cp_parser_lookup_name (parser, identifier,
15750 tag_type,
15751 /*is_template=*/false,
15752 /*is_namespace=*/false,
15753 check_dependency_p,
15754 &ambiguous_decls,
15755 identifier_token->location);
15756 if (ambiguous_decls)
15758 if (cp_parser_parsing_tentatively (parser))
15759 cp_parser_simulate_error (parser);
15760 return error_mark_node;
15764 else
15766 /* Try a template-id. */
15767 decl = cp_parser_template_id (parser, template_keyword_p,
15768 check_dependency_p,
15769 is_declaration);
15770 if (decl == error_mark_node)
15771 return error_mark_node;
15774 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
15776 /* If this is a typename, create a TYPENAME_TYPE. */
15777 if (typename_p && decl != error_mark_node)
15779 decl = make_typename_type (scope, decl, typename_type,
15780 /*complain=*/tf_error);
15781 if (decl != error_mark_node)
15782 decl = TYPE_NAME (decl);
15785 /* Check to see that it is really the name of a class. */
15786 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15787 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
15788 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15789 /* Situations like this:
15791 template <typename T> struct A {
15792 typename T::template X<int>::I i;
15795 are problematic. Is `T::template X<int>' a class-name? The
15796 standard does not seem to be definitive, but there is no other
15797 valid interpretation of the following `::'. Therefore, those
15798 names are considered class-names. */
15800 decl = make_typename_type (scope, decl, tag_type, tf_error);
15801 if (decl != error_mark_node)
15802 decl = TYPE_NAME (decl);
15804 else if (TREE_CODE (decl) != TYPE_DECL
15805 || TREE_TYPE (decl) == error_mark_node
15806 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
15807 decl = error_mark_node;
15809 if (decl == error_mark_node)
15810 cp_parser_error (parser, "expected class-name");
15811 else if (identifier && !parser->scope)
15812 maybe_note_name_used_in_class (identifier, decl);
15814 return decl;
15817 /* Parse a class-specifier.
15819 class-specifier:
15820 class-head { member-specification [opt] }
15822 Returns the TREE_TYPE representing the class. */
15824 static tree
15825 cp_parser_class_specifier (cp_parser* parser)
15827 tree type;
15828 tree attributes = NULL_TREE;
15829 bool nested_name_specifier_p;
15830 unsigned saved_num_template_parameter_lists;
15831 bool saved_in_function_body;
15832 bool saved_in_unbraced_linkage_specification_p;
15833 tree old_scope = NULL_TREE;
15834 tree scope = NULL_TREE;
15835 tree bases;
15837 push_deferring_access_checks (dk_no_deferred);
15839 /* Parse the class-head. */
15840 type = cp_parser_class_head (parser,
15841 &nested_name_specifier_p,
15842 &attributes,
15843 &bases);
15844 /* If the class-head was a semantic disaster, skip the entire body
15845 of the class. */
15846 if (!type)
15848 cp_parser_skip_to_end_of_block_or_statement (parser);
15849 pop_deferring_access_checks ();
15850 return error_mark_node;
15853 /* Look for the `{'. */
15854 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
15856 pop_deferring_access_checks ();
15857 return error_mark_node;
15860 /* Process the base classes. If they're invalid, skip the
15861 entire class body. */
15862 if (!xref_basetypes (type, bases))
15864 /* Consuming the closing brace yields better error messages
15865 later on. */
15866 if (cp_parser_skip_to_closing_brace (parser))
15867 cp_lexer_consume_token (parser->lexer);
15868 pop_deferring_access_checks ();
15869 return error_mark_node;
15872 /* Issue an error message if type-definitions are forbidden here. */
15873 cp_parser_check_type_definition (parser);
15874 /* Remember that we are defining one more class. */
15875 ++parser->num_classes_being_defined;
15876 /* Inside the class, surrounding template-parameter-lists do not
15877 apply. */
15878 saved_num_template_parameter_lists
15879 = parser->num_template_parameter_lists;
15880 parser->num_template_parameter_lists = 0;
15881 /* We are not in a function body. */
15882 saved_in_function_body = parser->in_function_body;
15883 parser->in_function_body = false;
15884 /* We are not immediately inside an extern "lang" block. */
15885 saved_in_unbraced_linkage_specification_p
15886 = parser->in_unbraced_linkage_specification_p;
15887 parser->in_unbraced_linkage_specification_p = false;
15889 /* Start the class. */
15890 if (nested_name_specifier_p)
15892 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
15893 old_scope = push_inner_scope (scope);
15895 type = begin_class_definition (type, attributes);
15897 if (type == error_mark_node)
15898 /* If the type is erroneous, skip the entire body of the class. */
15899 cp_parser_skip_to_closing_brace (parser);
15900 else
15901 /* Parse the member-specification. */
15902 cp_parser_member_specification_opt (parser);
15904 /* Look for the trailing `}'. */
15905 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15906 /* Look for trailing attributes to apply to this class. */
15907 if (cp_parser_allow_gnu_extensions_p (parser))
15908 attributes = cp_parser_attributes_opt (parser);
15909 if (type != error_mark_node)
15910 type = finish_struct (type, attributes);
15911 if (nested_name_specifier_p)
15912 pop_inner_scope (old_scope, scope);
15913 /* If this class is not itself within the scope of another class,
15914 then we need to parse the bodies of all of the queued function
15915 definitions. Note that the queued functions defined in a class
15916 are not always processed immediately following the
15917 class-specifier for that class. Consider:
15919 struct A {
15920 struct B { void f() { sizeof (A); } };
15923 If `f' were processed before the processing of `A' were
15924 completed, there would be no way to compute the size of `A'.
15925 Note that the nesting we are interested in here is lexical --
15926 not the semantic nesting given by TYPE_CONTEXT. In particular,
15927 for:
15929 struct A { struct B; };
15930 struct A::B { void f() { } };
15932 there is no need to delay the parsing of `A::B::f'. */
15933 if (--parser->num_classes_being_defined == 0)
15935 tree queue_entry;
15936 tree fn;
15937 tree class_type = NULL_TREE;
15938 tree pushed_scope = NULL_TREE;
15940 /* In a first pass, parse default arguments to the functions.
15941 Then, in a second pass, parse the bodies of the functions.
15942 This two-phased approach handles cases like:
15944 struct S {
15945 void f() { g(); }
15946 void g(int i = 3);
15950 for (TREE_PURPOSE (parser->unparsed_functions_queues)
15951 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
15952 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
15953 TREE_PURPOSE (parser->unparsed_functions_queues)
15954 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
15956 fn = TREE_VALUE (queue_entry);
15957 /* If there are default arguments that have not yet been processed,
15958 take care of them now. */
15959 if (class_type != TREE_PURPOSE (queue_entry))
15961 if (pushed_scope)
15962 pop_scope (pushed_scope);
15963 class_type = TREE_PURPOSE (queue_entry);
15964 pushed_scope = push_scope (class_type);
15966 /* Make sure that any template parameters are in scope. */
15967 maybe_begin_member_template_processing (fn);
15968 /* Parse the default argument expressions. */
15969 cp_parser_late_parsing_default_args (parser, fn);
15970 /* Remove any template parameters from the symbol table. */
15971 maybe_end_member_template_processing ();
15973 if (pushed_scope)
15974 pop_scope (pushed_scope);
15975 /* Now parse the body of the functions. */
15976 for (TREE_VALUE (parser->unparsed_functions_queues)
15977 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
15978 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
15979 TREE_VALUE (parser->unparsed_functions_queues)
15980 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
15982 /* Figure out which function we need to process. */
15983 fn = TREE_VALUE (queue_entry);
15984 /* Parse the function. */
15985 cp_parser_late_parsing_for_member (parser, fn);
15989 /* Put back any saved access checks. */
15990 pop_deferring_access_checks ();
15992 /* Restore saved state. */
15993 parser->in_function_body = saved_in_function_body;
15994 parser->num_template_parameter_lists
15995 = saved_num_template_parameter_lists;
15996 parser->in_unbraced_linkage_specification_p
15997 = saved_in_unbraced_linkage_specification_p;
15999 return type;
16002 /* Parse a class-head.
16004 class-head:
16005 class-key identifier [opt] base-clause [opt]
16006 class-key nested-name-specifier identifier base-clause [opt]
16007 class-key nested-name-specifier [opt] template-id
16008 base-clause [opt]
16010 GNU Extensions:
16011 class-key attributes identifier [opt] base-clause [opt]
16012 class-key attributes nested-name-specifier identifier base-clause [opt]
16013 class-key attributes nested-name-specifier [opt] template-id
16014 base-clause [opt]
16016 Upon return BASES is initialized to the list of base classes (or
16017 NULL, if there are none) in the same form returned by
16018 cp_parser_base_clause.
16020 Returns the TYPE of the indicated class. Sets
16021 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
16022 involving a nested-name-specifier was used, and FALSE otherwise.
16024 Returns error_mark_node if this is not a class-head.
16026 Returns NULL_TREE if the class-head is syntactically valid, but
16027 semantically invalid in a way that means we should skip the entire
16028 body of the class. */
16030 static tree
16031 cp_parser_class_head (cp_parser* parser,
16032 bool* nested_name_specifier_p,
16033 tree *attributes_p,
16034 tree *bases)
16036 tree nested_name_specifier;
16037 enum tag_types class_key;
16038 tree id = NULL_TREE;
16039 tree type = NULL_TREE;
16040 tree attributes;
16041 bool template_id_p = false;
16042 bool qualified_p = false;
16043 bool invalid_nested_name_p = false;
16044 bool invalid_explicit_specialization_p = false;
16045 tree pushed_scope = NULL_TREE;
16046 unsigned num_templates;
16047 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
16048 /* Assume no nested-name-specifier will be present. */
16049 *nested_name_specifier_p = false;
16050 /* Assume no template parameter lists will be used in defining the
16051 type. */
16052 num_templates = 0;
16054 *bases = NULL_TREE;
16056 /* Look for the class-key. */
16057 class_key = cp_parser_class_key (parser);
16058 if (class_key == none_type)
16059 return error_mark_node;
16061 /* Parse the attributes. */
16062 attributes = cp_parser_attributes_opt (parser);
16064 /* If the next token is `::', that is invalid -- but sometimes
16065 people do try to write:
16067 struct ::S {};
16069 Handle this gracefully by accepting the extra qualifier, and then
16070 issuing an error about it later if this really is a
16071 class-head. If it turns out just to be an elaborated type
16072 specifier, remain silent. */
16073 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
16074 qualified_p = true;
16076 push_deferring_access_checks (dk_no_check);
16078 /* Determine the name of the class. Begin by looking for an
16079 optional nested-name-specifier. */
16080 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
16081 nested_name_specifier
16082 = cp_parser_nested_name_specifier_opt (parser,
16083 /*typename_keyword_p=*/false,
16084 /*check_dependency_p=*/false,
16085 /*type_p=*/false,
16086 /*is_declaration=*/false);
16087 /* If there was a nested-name-specifier, then there *must* be an
16088 identifier. */
16089 if (nested_name_specifier)
16091 type_start_token = cp_lexer_peek_token (parser->lexer);
16092 /* Although the grammar says `identifier', it really means
16093 `class-name' or `template-name'. You are only allowed to
16094 define a class that has already been declared with this
16095 syntax.
16097 The proposed resolution for Core Issue 180 says that wherever
16098 you see `class T::X' you should treat `X' as a type-name.
16100 It is OK to define an inaccessible class; for example:
16102 class A { class B; };
16103 class A::B {};
16105 We do not know if we will see a class-name, or a
16106 template-name. We look for a class-name first, in case the
16107 class-name is a template-id; if we looked for the
16108 template-name first we would stop after the template-name. */
16109 cp_parser_parse_tentatively (parser);
16110 type = cp_parser_class_name (parser,
16111 /*typename_keyword_p=*/false,
16112 /*template_keyword_p=*/false,
16113 class_type,
16114 /*check_dependency_p=*/false,
16115 /*class_head_p=*/true,
16116 /*is_declaration=*/false);
16117 /* If that didn't work, ignore the nested-name-specifier. */
16118 if (!cp_parser_parse_definitely (parser))
16120 invalid_nested_name_p = true;
16121 type_start_token = cp_lexer_peek_token (parser->lexer);
16122 id = cp_parser_identifier (parser);
16123 if (id == error_mark_node)
16124 id = NULL_TREE;
16126 /* If we could not find a corresponding TYPE, treat this
16127 declaration like an unqualified declaration. */
16128 if (type == error_mark_node)
16129 nested_name_specifier = NULL_TREE;
16130 /* Otherwise, count the number of templates used in TYPE and its
16131 containing scopes. */
16132 else
16134 tree scope;
16136 for (scope = TREE_TYPE (type);
16137 scope && TREE_CODE (scope) != NAMESPACE_DECL;
16138 scope = (TYPE_P (scope)
16139 ? TYPE_CONTEXT (scope)
16140 : DECL_CONTEXT (scope)))
16141 if (TYPE_P (scope)
16142 && CLASS_TYPE_P (scope)
16143 && CLASSTYPE_TEMPLATE_INFO (scope)
16144 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
16145 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
16146 ++num_templates;
16149 /* Otherwise, the identifier is optional. */
16150 else
16152 /* We don't know whether what comes next is a template-id,
16153 an identifier, or nothing at all. */
16154 cp_parser_parse_tentatively (parser);
16155 /* Check for a template-id. */
16156 type_start_token = cp_lexer_peek_token (parser->lexer);
16157 id = cp_parser_template_id (parser,
16158 /*template_keyword_p=*/false,
16159 /*check_dependency_p=*/true,
16160 /*is_declaration=*/true);
16161 /* If that didn't work, it could still be an identifier. */
16162 if (!cp_parser_parse_definitely (parser))
16164 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16166 type_start_token = cp_lexer_peek_token (parser->lexer);
16167 id = cp_parser_identifier (parser);
16169 else
16170 id = NULL_TREE;
16172 else
16174 template_id_p = true;
16175 ++num_templates;
16179 pop_deferring_access_checks ();
16181 if (id)
16182 cp_parser_check_for_invalid_template_id (parser, id,
16183 type_start_token->location);
16185 /* If it's not a `:' or a `{' then we can't really be looking at a
16186 class-head, since a class-head only appears as part of a
16187 class-specifier. We have to detect this situation before calling
16188 xref_tag, since that has irreversible side-effects. */
16189 if (!cp_parser_next_token_starts_class_definition_p (parser))
16191 cp_parser_error (parser, "expected %<{%> or %<:%>");
16192 return error_mark_node;
16195 /* At this point, we're going ahead with the class-specifier, even
16196 if some other problem occurs. */
16197 cp_parser_commit_to_tentative_parse (parser);
16198 /* Issue the error about the overly-qualified name now. */
16199 if (qualified_p)
16201 cp_parser_error (parser,
16202 "global qualification of class name is invalid");
16203 return error_mark_node;
16205 else if (invalid_nested_name_p)
16207 cp_parser_error (parser,
16208 "qualified name does not name a class");
16209 return error_mark_node;
16211 else if (nested_name_specifier)
16213 tree scope;
16215 /* Reject typedef-names in class heads. */
16216 if (!DECL_IMPLICIT_TYPEDEF_P (type))
16218 error_at (type_start_token->location,
16219 "invalid class name in declaration of %qD",
16220 type);
16221 type = NULL_TREE;
16222 goto done;
16225 /* Figure out in what scope the declaration is being placed. */
16226 scope = current_scope ();
16227 /* If that scope does not contain the scope in which the
16228 class was originally declared, the program is invalid. */
16229 if (scope && !is_ancestor (scope, nested_name_specifier))
16231 if (at_namespace_scope_p ())
16232 error_at (type_start_token->location,
16233 "declaration of %qD in namespace %qD which does not "
16234 "enclose %qD",
16235 type, scope, nested_name_specifier);
16236 else
16237 error_at (type_start_token->location,
16238 "declaration of %qD in %qD which does not enclose %qD",
16239 type, scope, nested_name_specifier);
16240 type = NULL_TREE;
16241 goto done;
16243 /* [dcl.meaning]
16245 A declarator-id shall not be qualified except for the
16246 definition of a ... nested class outside of its class
16247 ... [or] the definition or explicit instantiation of a
16248 class member of a namespace outside of its namespace. */
16249 if (scope == nested_name_specifier)
16251 permerror (nested_name_specifier_token_start->location,
16252 "extra qualification not allowed");
16253 nested_name_specifier = NULL_TREE;
16254 num_templates = 0;
16257 /* An explicit-specialization must be preceded by "template <>". If
16258 it is not, try to recover gracefully. */
16259 if (at_namespace_scope_p ()
16260 && parser->num_template_parameter_lists == 0
16261 && template_id_p)
16263 error_at (type_start_token->location,
16264 "an explicit specialization must be preceded by %<template <>%>");
16265 invalid_explicit_specialization_p = true;
16266 /* Take the same action that would have been taken by
16267 cp_parser_explicit_specialization. */
16268 ++parser->num_template_parameter_lists;
16269 begin_specialization ();
16271 /* There must be no "return" statements between this point and the
16272 end of this function; set "type "to the correct return value and
16273 use "goto done;" to return. */
16274 /* Make sure that the right number of template parameters were
16275 present. */
16276 if (!cp_parser_check_template_parameters (parser, num_templates,
16277 type_start_token->location,
16278 /*declarator=*/NULL))
16280 /* If something went wrong, there is no point in even trying to
16281 process the class-definition. */
16282 type = NULL_TREE;
16283 goto done;
16286 /* Look up the type. */
16287 if (template_id_p)
16289 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
16290 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
16291 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
16293 error_at (type_start_token->location,
16294 "function template %qD redeclared as a class template", id);
16295 type = error_mark_node;
16297 else
16299 type = TREE_TYPE (id);
16300 type = maybe_process_partial_specialization (type);
16302 if (nested_name_specifier)
16303 pushed_scope = push_scope (nested_name_specifier);
16305 else if (nested_name_specifier)
16307 tree class_type;
16309 /* Given:
16311 template <typename T> struct S { struct T };
16312 template <typename T> struct S<T>::T { };
16314 we will get a TYPENAME_TYPE when processing the definition of
16315 `S::T'. We need to resolve it to the actual type before we
16316 try to define it. */
16317 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
16319 class_type = resolve_typename_type (TREE_TYPE (type),
16320 /*only_current_p=*/false);
16321 if (TREE_CODE (class_type) != TYPENAME_TYPE)
16322 type = TYPE_NAME (class_type);
16323 else
16325 cp_parser_error (parser, "could not resolve typename type");
16326 type = error_mark_node;
16330 if (maybe_process_partial_specialization (TREE_TYPE (type))
16331 == error_mark_node)
16333 type = NULL_TREE;
16334 goto done;
16337 class_type = current_class_type;
16338 /* Enter the scope indicated by the nested-name-specifier. */
16339 pushed_scope = push_scope (nested_name_specifier);
16340 /* Get the canonical version of this type. */
16341 type = TYPE_MAIN_DECL (TREE_TYPE (type));
16342 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
16343 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
16345 type = push_template_decl (type);
16346 if (type == error_mark_node)
16348 type = NULL_TREE;
16349 goto done;
16353 type = TREE_TYPE (type);
16354 *nested_name_specifier_p = true;
16356 else /* The name is not a nested name. */
16358 /* If the class was unnamed, create a dummy name. */
16359 if (!id)
16360 id = make_anon_name ();
16361 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
16362 parser->num_template_parameter_lists);
16365 /* Indicate whether this class was declared as a `class' or as a
16366 `struct'. */
16367 if (TREE_CODE (type) == RECORD_TYPE)
16368 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
16369 cp_parser_check_class_key (class_key, type);
16371 /* If this type was already complete, and we see another definition,
16372 that's an error. */
16373 if (type != error_mark_node && COMPLETE_TYPE_P (type))
16375 error_at (type_start_token->location, "redefinition of %q#T",
16376 type);
16377 error_at (type_start_token->location, "previous definition of %q+#T",
16378 type);
16379 type = NULL_TREE;
16380 goto done;
16382 else if (type == error_mark_node)
16383 type = NULL_TREE;
16385 /* We will have entered the scope containing the class; the names of
16386 base classes should be looked up in that context. For example:
16388 struct A { struct B {}; struct C; };
16389 struct A::C : B {};
16391 is valid. */
16393 /* Get the list of base-classes, if there is one. */
16394 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16395 *bases = cp_parser_base_clause (parser);
16397 done:
16398 /* Leave the scope given by the nested-name-specifier. We will
16399 enter the class scope itself while processing the members. */
16400 if (pushed_scope)
16401 pop_scope (pushed_scope);
16403 if (invalid_explicit_specialization_p)
16405 end_specialization ();
16406 --parser->num_template_parameter_lists;
16409 if (type)
16410 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
16411 *attributes_p = attributes;
16412 return type;
16415 /* Parse a class-key.
16417 class-key:
16418 class
16419 struct
16420 union
16422 Returns the kind of class-key specified, or none_type to indicate
16423 error. */
16425 static enum tag_types
16426 cp_parser_class_key (cp_parser* parser)
16428 cp_token *token;
16429 enum tag_types tag_type;
16431 /* Look for the class-key. */
16432 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
16433 if (!token)
16434 return none_type;
16436 /* Check to see if the TOKEN is a class-key. */
16437 tag_type = cp_parser_token_is_class_key (token);
16438 if (!tag_type)
16439 cp_parser_error (parser, "expected class-key");
16440 return tag_type;
16443 /* Parse an (optional) member-specification.
16445 member-specification:
16446 member-declaration member-specification [opt]
16447 access-specifier : member-specification [opt] */
16449 static void
16450 cp_parser_member_specification_opt (cp_parser* parser)
16452 while (true)
16454 cp_token *token;
16455 enum rid keyword;
16457 /* Peek at the next token. */
16458 token = cp_lexer_peek_token (parser->lexer);
16459 /* If it's a `}', or EOF then we've seen all the members. */
16460 if (token->type == CPP_CLOSE_BRACE
16461 || token->type == CPP_EOF
16462 || token->type == CPP_PRAGMA_EOL)
16463 break;
16465 /* See if this token is a keyword. */
16466 keyword = token->keyword;
16467 switch (keyword)
16469 case RID_PUBLIC:
16470 case RID_PROTECTED:
16471 case RID_PRIVATE:
16472 /* Consume the access-specifier. */
16473 cp_lexer_consume_token (parser->lexer);
16474 /* Remember which access-specifier is active. */
16475 current_access_specifier = token->u.value;
16476 /* Look for the `:'. */
16477 cp_parser_require (parser, CPP_COLON, "%<:%>");
16478 break;
16480 default:
16481 /* Accept #pragmas at class scope. */
16482 if (token->type == CPP_PRAGMA)
16484 cp_parser_pragma (parser, pragma_external);
16485 break;
16488 /* Otherwise, the next construction must be a
16489 member-declaration. */
16490 cp_parser_member_declaration (parser);
16495 /* Parse a member-declaration.
16497 member-declaration:
16498 decl-specifier-seq [opt] member-declarator-list [opt] ;
16499 function-definition ; [opt]
16500 :: [opt] nested-name-specifier template [opt] unqualified-id ;
16501 using-declaration
16502 template-declaration
16504 member-declarator-list:
16505 member-declarator
16506 member-declarator-list , member-declarator
16508 member-declarator:
16509 declarator pure-specifier [opt]
16510 declarator constant-initializer [opt]
16511 identifier [opt] : constant-expression
16513 GNU Extensions:
16515 member-declaration:
16516 __extension__ member-declaration
16518 member-declarator:
16519 declarator attributes [opt] pure-specifier [opt]
16520 declarator attributes [opt] constant-initializer [opt]
16521 identifier [opt] attributes [opt] : constant-expression
16523 C++0x Extensions:
16525 member-declaration:
16526 static_assert-declaration */
16528 static void
16529 cp_parser_member_declaration (cp_parser* parser)
16531 cp_decl_specifier_seq decl_specifiers;
16532 tree prefix_attributes;
16533 tree decl;
16534 int declares_class_or_enum;
16535 bool friend_p;
16536 cp_token *token = NULL;
16537 cp_token *decl_spec_token_start = NULL;
16538 cp_token *initializer_token_start = NULL;
16539 int saved_pedantic;
16541 /* Check for the `__extension__' keyword. */
16542 if (cp_parser_extension_opt (parser, &saved_pedantic))
16544 /* Recurse. */
16545 cp_parser_member_declaration (parser);
16546 /* Restore the old value of the PEDANTIC flag. */
16547 pedantic = saved_pedantic;
16549 return;
16552 /* Check for a template-declaration. */
16553 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16555 /* An explicit specialization here is an error condition, and we
16556 expect the specialization handler to detect and report this. */
16557 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16558 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
16559 cp_parser_explicit_specialization (parser);
16560 else
16561 cp_parser_template_declaration (parser, /*member_p=*/true);
16563 return;
16566 /* Check for a using-declaration. */
16567 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
16569 /* Parse the using-declaration. */
16570 cp_parser_using_declaration (parser,
16571 /*access_declaration_p=*/false);
16572 return;
16575 /* Check for @defs. */
16576 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
16578 tree ivar, member;
16579 tree ivar_chains = cp_parser_objc_defs_expression (parser);
16580 ivar = ivar_chains;
16581 while (ivar)
16583 member = ivar;
16584 ivar = TREE_CHAIN (member);
16585 TREE_CHAIN (member) = NULL_TREE;
16586 finish_member_declaration (member);
16588 return;
16591 /* If the next token is `static_assert' we have a static assertion. */
16592 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
16594 cp_parser_static_assert (parser, /*member_p=*/true);
16595 return;
16598 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
16599 return;
16601 /* Parse the decl-specifier-seq. */
16602 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
16603 cp_parser_decl_specifier_seq (parser,
16604 CP_PARSER_FLAGS_OPTIONAL,
16605 &decl_specifiers,
16606 &declares_class_or_enum);
16607 prefix_attributes = decl_specifiers.attributes;
16608 decl_specifiers.attributes = NULL_TREE;
16609 /* Check for an invalid type-name. */
16610 if (!decl_specifiers.any_type_specifiers_p
16611 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
16612 return;
16613 /* If there is no declarator, then the decl-specifier-seq should
16614 specify a type. */
16615 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16617 /* If there was no decl-specifier-seq, and the next token is a
16618 `;', then we have something like:
16620 struct S { ; };
16622 [class.mem]
16624 Each member-declaration shall declare at least one member
16625 name of the class. */
16626 if (!decl_specifiers.any_specifiers_p)
16628 cp_token *token = cp_lexer_peek_token (parser->lexer);
16629 if (!in_system_header_at (token->location))
16630 pedwarn (token->location, OPT_pedantic, "extra %<;%>");
16632 else
16634 tree type;
16636 /* See if this declaration is a friend. */
16637 friend_p = cp_parser_friend_p (&decl_specifiers);
16638 /* If there were decl-specifiers, check to see if there was
16639 a class-declaration. */
16640 type = check_tag_decl (&decl_specifiers);
16641 /* Nested classes have already been added to the class, but
16642 a `friend' needs to be explicitly registered. */
16643 if (friend_p)
16645 /* If the `friend' keyword was present, the friend must
16646 be introduced with a class-key. */
16647 if (!declares_class_or_enum)
16648 error_at (decl_spec_token_start->location,
16649 "a class-key must be used when declaring a friend");
16650 /* In this case:
16652 template <typename T> struct A {
16653 friend struct A<T>::B;
16656 A<T>::B will be represented by a TYPENAME_TYPE, and
16657 therefore not recognized by check_tag_decl. */
16658 if (!type
16659 && decl_specifiers.type
16660 && TYPE_P (decl_specifiers.type))
16661 type = decl_specifiers.type;
16662 if (!type || !TYPE_P (type))
16663 error_at (decl_spec_token_start->location,
16664 "friend declaration does not name a class or "
16665 "function");
16666 else
16667 make_friend_class (current_class_type, type,
16668 /*complain=*/true);
16670 /* If there is no TYPE, an error message will already have
16671 been issued. */
16672 else if (!type || type == error_mark_node)
16674 /* An anonymous aggregate has to be handled specially; such
16675 a declaration really declares a data member (with a
16676 particular type), as opposed to a nested class. */
16677 else if (ANON_AGGR_TYPE_P (type))
16679 /* Remove constructors and such from TYPE, now that we
16680 know it is an anonymous aggregate. */
16681 fixup_anonymous_aggr (type);
16682 /* And make the corresponding data member. */
16683 decl = build_decl (decl_spec_token_start->location,
16684 FIELD_DECL, NULL_TREE, type);
16685 /* Add it to the class. */
16686 finish_member_declaration (decl);
16688 else
16689 cp_parser_check_access_in_redeclaration
16690 (TYPE_NAME (type),
16691 decl_spec_token_start->location);
16694 else
16696 /* See if these declarations will be friends. */
16697 friend_p = cp_parser_friend_p (&decl_specifiers);
16699 /* Keep going until we hit the `;' at the end of the
16700 declaration. */
16701 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16703 tree attributes = NULL_TREE;
16704 tree first_attribute;
16706 /* Peek at the next token. */
16707 token = cp_lexer_peek_token (parser->lexer);
16709 /* Check for a bitfield declaration. */
16710 if (token->type == CPP_COLON
16711 || (token->type == CPP_NAME
16712 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
16713 == CPP_COLON))
16715 tree identifier;
16716 tree width;
16718 /* Get the name of the bitfield. Note that we cannot just
16719 check TOKEN here because it may have been invalidated by
16720 the call to cp_lexer_peek_nth_token above. */
16721 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
16722 identifier = cp_parser_identifier (parser);
16723 else
16724 identifier = NULL_TREE;
16726 /* Consume the `:' token. */
16727 cp_lexer_consume_token (parser->lexer);
16728 /* Get the width of the bitfield. */
16729 width
16730 = cp_parser_constant_expression (parser,
16731 /*allow_non_constant=*/false,
16732 NULL);
16734 /* Look for attributes that apply to the bitfield. */
16735 attributes = cp_parser_attributes_opt (parser);
16736 /* Remember which attributes are prefix attributes and
16737 which are not. */
16738 first_attribute = attributes;
16739 /* Combine the attributes. */
16740 attributes = chainon (prefix_attributes, attributes);
16742 /* Create the bitfield declaration. */
16743 decl = grokbitfield (identifier
16744 ? make_id_declarator (NULL_TREE,
16745 identifier,
16746 sfk_none)
16747 : NULL,
16748 &decl_specifiers,
16749 width,
16750 attributes);
16752 else
16754 cp_declarator *declarator;
16755 tree initializer;
16756 tree asm_specification;
16757 int ctor_dtor_or_conv_p;
16759 /* Parse the declarator. */
16760 declarator
16761 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16762 &ctor_dtor_or_conv_p,
16763 /*parenthesized_p=*/NULL,
16764 /*member_p=*/true);
16766 /* If something went wrong parsing the declarator, make sure
16767 that we at least consume some tokens. */
16768 if (declarator == cp_error_declarator)
16770 /* Skip to the end of the statement. */
16771 cp_parser_skip_to_end_of_statement (parser);
16772 /* If the next token is not a semicolon, that is
16773 probably because we just skipped over the body of
16774 a function. So, we consume a semicolon if
16775 present, but do not issue an error message if it
16776 is not present. */
16777 if (cp_lexer_next_token_is (parser->lexer,
16778 CPP_SEMICOLON))
16779 cp_lexer_consume_token (parser->lexer);
16780 return;
16783 if (declares_class_or_enum & 2)
16784 cp_parser_check_for_definition_in_return_type
16785 (declarator, decl_specifiers.type,
16786 decl_specifiers.type_location);
16788 /* Look for an asm-specification. */
16789 asm_specification = cp_parser_asm_specification_opt (parser);
16790 /* Look for attributes that apply to the declaration. */
16791 attributes = cp_parser_attributes_opt (parser);
16792 /* Remember which attributes are prefix attributes and
16793 which are not. */
16794 first_attribute = attributes;
16795 /* Combine the attributes. */
16796 attributes = chainon (prefix_attributes, attributes);
16798 /* If it's an `=', then we have a constant-initializer or a
16799 pure-specifier. It is not correct to parse the
16800 initializer before registering the member declaration
16801 since the member declaration should be in scope while
16802 its initializer is processed. However, the rest of the
16803 front end does not yet provide an interface that allows
16804 us to handle this correctly. */
16805 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16807 /* In [class.mem]:
16809 A pure-specifier shall be used only in the declaration of
16810 a virtual function.
16812 A member-declarator can contain a constant-initializer
16813 only if it declares a static member of integral or
16814 enumeration type.
16816 Therefore, if the DECLARATOR is for a function, we look
16817 for a pure-specifier; otherwise, we look for a
16818 constant-initializer. When we call `grokfield', it will
16819 perform more stringent semantics checks. */
16820 initializer_token_start = cp_lexer_peek_token (parser->lexer);
16821 if (function_declarator_p (declarator))
16822 initializer = cp_parser_pure_specifier (parser);
16823 else
16824 /* Parse the initializer. */
16825 initializer = cp_parser_constant_initializer (parser);
16827 /* Otherwise, there is no initializer. */
16828 else
16829 initializer = NULL_TREE;
16831 /* See if we are probably looking at a function
16832 definition. We are certainly not looking at a
16833 member-declarator. Calling `grokfield' has
16834 side-effects, so we must not do it unless we are sure
16835 that we are looking at a member-declarator. */
16836 if (cp_parser_token_starts_function_definition_p
16837 (cp_lexer_peek_token (parser->lexer)))
16839 /* The grammar does not allow a pure-specifier to be
16840 used when a member function is defined. (It is
16841 possible that this fact is an oversight in the
16842 standard, since a pure function may be defined
16843 outside of the class-specifier. */
16844 if (initializer)
16845 error_at (initializer_token_start->location,
16846 "pure-specifier on function-definition");
16847 decl = cp_parser_save_member_function_body (parser,
16848 &decl_specifiers,
16849 declarator,
16850 attributes);
16851 /* If the member was not a friend, declare it here. */
16852 if (!friend_p)
16853 finish_member_declaration (decl);
16854 /* Peek at the next token. */
16855 token = cp_lexer_peek_token (parser->lexer);
16856 /* If the next token is a semicolon, consume it. */
16857 if (token->type == CPP_SEMICOLON)
16858 cp_lexer_consume_token (parser->lexer);
16859 return;
16861 else
16862 if (declarator->kind == cdk_function)
16863 declarator->id_loc = token->location;
16864 /* Create the declaration. */
16865 decl = grokfield (declarator, &decl_specifiers,
16866 initializer, /*init_const_expr_p=*/true,
16867 asm_specification,
16868 attributes);
16871 /* Reset PREFIX_ATTRIBUTES. */
16872 while (attributes && TREE_CHAIN (attributes) != first_attribute)
16873 attributes = TREE_CHAIN (attributes);
16874 if (attributes)
16875 TREE_CHAIN (attributes) = NULL_TREE;
16877 /* If there is any qualification still in effect, clear it
16878 now; we will be starting fresh with the next declarator. */
16879 parser->scope = NULL_TREE;
16880 parser->qualifying_scope = NULL_TREE;
16881 parser->object_scope = NULL_TREE;
16882 /* If it's a `,', then there are more declarators. */
16883 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16884 cp_lexer_consume_token (parser->lexer);
16885 /* If the next token isn't a `;', then we have a parse error. */
16886 else if (cp_lexer_next_token_is_not (parser->lexer,
16887 CPP_SEMICOLON))
16889 cp_parser_error (parser, "expected %<;%>");
16890 /* Skip tokens until we find a `;'. */
16891 cp_parser_skip_to_end_of_statement (parser);
16893 break;
16896 if (decl)
16898 /* Add DECL to the list of members. */
16899 if (!friend_p)
16900 finish_member_declaration (decl);
16902 if (TREE_CODE (decl) == FUNCTION_DECL)
16903 cp_parser_save_default_args (parser, decl);
16908 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16911 /* Parse a pure-specifier.
16913 pure-specifier:
16916 Returns INTEGER_ZERO_NODE if a pure specifier is found.
16917 Otherwise, ERROR_MARK_NODE is returned. */
16919 static tree
16920 cp_parser_pure_specifier (cp_parser* parser)
16922 cp_token *token;
16924 /* Look for the `=' token. */
16925 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16926 return error_mark_node;
16927 /* Look for the `0' token. */
16928 token = cp_lexer_peek_token (parser->lexer);
16930 if (token->type == CPP_EOF
16931 || token->type == CPP_PRAGMA_EOL)
16932 return error_mark_node;
16934 cp_lexer_consume_token (parser->lexer);
16936 /* Accept = default or = delete in c++0x mode. */
16937 if (token->keyword == RID_DEFAULT
16938 || token->keyword == RID_DELETE)
16940 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
16941 return token->u.value;
16944 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
16945 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
16947 cp_parser_error (parser,
16948 "invalid pure specifier (only %<= 0%> is allowed)");
16949 cp_parser_skip_to_end_of_statement (parser);
16950 return error_mark_node;
16952 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
16954 error_at (token->location, "templates may not be %<virtual%>");
16955 return error_mark_node;
16958 return integer_zero_node;
16961 /* Parse a constant-initializer.
16963 constant-initializer:
16964 = constant-expression
16966 Returns a representation of the constant-expression. */
16968 static tree
16969 cp_parser_constant_initializer (cp_parser* parser)
16971 /* Look for the `=' token. */
16972 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16973 return error_mark_node;
16975 /* It is invalid to write:
16977 struct S { static const int i = { 7 }; };
16980 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16982 cp_parser_error (parser,
16983 "a brace-enclosed initializer is not allowed here");
16984 /* Consume the opening brace. */
16985 cp_lexer_consume_token (parser->lexer);
16986 /* Skip the initializer. */
16987 cp_parser_skip_to_closing_brace (parser);
16988 /* Look for the trailing `}'. */
16989 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
16991 return error_mark_node;
16994 return cp_parser_constant_expression (parser,
16995 /*allow_non_constant=*/false,
16996 NULL);
16999 /* Derived classes [gram.class.derived] */
17001 /* Parse a base-clause.
17003 base-clause:
17004 : base-specifier-list
17006 base-specifier-list:
17007 base-specifier ... [opt]
17008 base-specifier-list , base-specifier ... [opt]
17010 Returns a TREE_LIST representing the base-classes, in the order in
17011 which they were declared. The representation of each node is as
17012 described by cp_parser_base_specifier.
17014 In the case that no bases are specified, this function will return
17015 NULL_TREE, not ERROR_MARK_NODE. */
17017 static tree
17018 cp_parser_base_clause (cp_parser* parser)
17020 tree bases = NULL_TREE;
17022 /* Look for the `:' that begins the list. */
17023 cp_parser_require (parser, CPP_COLON, "%<:%>");
17025 /* Scan the base-specifier-list. */
17026 while (true)
17028 cp_token *token;
17029 tree base;
17030 bool pack_expansion_p = false;
17032 /* Look for the base-specifier. */
17033 base = cp_parser_base_specifier (parser);
17034 /* Look for the (optional) ellipsis. */
17035 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17037 /* Consume the `...'. */
17038 cp_lexer_consume_token (parser->lexer);
17040 pack_expansion_p = true;
17043 /* Add BASE to the front of the list. */
17044 if (base != error_mark_node)
17046 if (pack_expansion_p)
17047 /* Make this a pack expansion type. */
17048 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
17051 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
17053 TREE_CHAIN (base) = bases;
17054 bases = base;
17057 /* Peek at the next token. */
17058 token = cp_lexer_peek_token (parser->lexer);
17059 /* If it's not a comma, then the list is complete. */
17060 if (token->type != CPP_COMMA)
17061 break;
17062 /* Consume the `,'. */
17063 cp_lexer_consume_token (parser->lexer);
17066 /* PARSER->SCOPE may still be non-NULL at this point, if the last
17067 base class had a qualified name. However, the next name that
17068 appears is certainly not qualified. */
17069 parser->scope = NULL_TREE;
17070 parser->qualifying_scope = NULL_TREE;
17071 parser->object_scope = NULL_TREE;
17073 return nreverse (bases);
17076 /* Parse a base-specifier.
17078 base-specifier:
17079 :: [opt] nested-name-specifier [opt] class-name
17080 virtual access-specifier [opt] :: [opt] nested-name-specifier
17081 [opt] class-name
17082 access-specifier virtual [opt] :: [opt] nested-name-specifier
17083 [opt] class-name
17085 Returns a TREE_LIST. The TREE_PURPOSE will be one of
17086 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
17087 indicate the specifiers provided. The TREE_VALUE will be a TYPE
17088 (or the ERROR_MARK_NODE) indicating the type that was specified. */
17090 static tree
17091 cp_parser_base_specifier (cp_parser* parser)
17093 cp_token *token;
17094 bool done = false;
17095 bool virtual_p = false;
17096 bool duplicate_virtual_error_issued_p = false;
17097 bool duplicate_access_error_issued_p = false;
17098 bool class_scope_p, template_p;
17099 tree access = access_default_node;
17100 tree type;
17102 /* Process the optional `virtual' and `access-specifier'. */
17103 while (!done)
17105 /* Peek at the next token. */
17106 token = cp_lexer_peek_token (parser->lexer);
17107 /* Process `virtual'. */
17108 switch (token->keyword)
17110 case RID_VIRTUAL:
17111 /* If `virtual' appears more than once, issue an error. */
17112 if (virtual_p && !duplicate_virtual_error_issued_p)
17114 cp_parser_error (parser,
17115 "%<virtual%> specified more than once in base-specified");
17116 duplicate_virtual_error_issued_p = true;
17119 virtual_p = true;
17121 /* Consume the `virtual' token. */
17122 cp_lexer_consume_token (parser->lexer);
17124 break;
17126 case RID_PUBLIC:
17127 case RID_PROTECTED:
17128 case RID_PRIVATE:
17129 /* If more than one access specifier appears, issue an
17130 error. */
17131 if (access != access_default_node
17132 && !duplicate_access_error_issued_p)
17134 cp_parser_error (parser,
17135 "more than one access specifier in base-specified");
17136 duplicate_access_error_issued_p = true;
17139 access = ridpointers[(int) token->keyword];
17141 /* Consume the access-specifier. */
17142 cp_lexer_consume_token (parser->lexer);
17144 break;
17146 default:
17147 done = true;
17148 break;
17151 /* It is not uncommon to see programs mechanically, erroneously, use
17152 the 'typename' keyword to denote (dependent) qualified types
17153 as base classes. */
17154 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
17156 token = cp_lexer_peek_token (parser->lexer);
17157 if (!processing_template_decl)
17158 error_at (token->location,
17159 "keyword %<typename%> not allowed outside of templates");
17160 else
17161 error_at (token->location,
17162 "keyword %<typename%> not allowed in this context "
17163 "(the base class is implicitly a type)");
17164 cp_lexer_consume_token (parser->lexer);
17167 /* Look for the optional `::' operator. */
17168 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
17169 /* Look for the nested-name-specifier. The simplest way to
17170 implement:
17172 [temp.res]
17174 The keyword `typename' is not permitted in a base-specifier or
17175 mem-initializer; in these contexts a qualified name that
17176 depends on a template-parameter is implicitly assumed to be a
17177 type name.
17179 is to pretend that we have seen the `typename' keyword at this
17180 point. */
17181 cp_parser_nested_name_specifier_opt (parser,
17182 /*typename_keyword_p=*/true,
17183 /*check_dependency_p=*/true,
17184 typename_type,
17185 /*is_declaration=*/true);
17186 /* If the base class is given by a qualified name, assume that names
17187 we see are type names or templates, as appropriate. */
17188 class_scope_p = (parser->scope && TYPE_P (parser->scope));
17189 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
17191 /* Finally, look for the class-name. */
17192 type = cp_parser_class_name (parser,
17193 class_scope_p,
17194 template_p,
17195 typename_type,
17196 /*check_dependency_p=*/true,
17197 /*class_head_p=*/false,
17198 /*is_declaration=*/true);
17200 if (type == error_mark_node)
17201 return error_mark_node;
17203 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
17206 /* Exception handling [gram.exception] */
17208 /* Parse an (optional) exception-specification.
17210 exception-specification:
17211 throw ( type-id-list [opt] )
17213 Returns a TREE_LIST representing the exception-specification. The
17214 TREE_VALUE of each node is a type. */
17216 static tree
17217 cp_parser_exception_specification_opt (cp_parser* parser)
17219 cp_token *token;
17220 tree type_id_list;
17222 /* Peek at the next token. */
17223 token = cp_lexer_peek_token (parser->lexer);
17224 /* If it's not `throw', then there's no exception-specification. */
17225 if (!cp_parser_is_keyword (token, RID_THROW))
17226 return NULL_TREE;
17228 /* Consume the `throw'. */
17229 cp_lexer_consume_token (parser->lexer);
17231 /* Look for the `('. */
17232 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17234 /* Peek at the next token. */
17235 token = cp_lexer_peek_token (parser->lexer);
17236 /* If it's not a `)', then there is a type-id-list. */
17237 if (token->type != CPP_CLOSE_PAREN)
17239 const char *saved_message;
17241 /* Types may not be defined in an exception-specification. */
17242 saved_message = parser->type_definition_forbidden_message;
17243 parser->type_definition_forbidden_message
17244 = G_("types may not be defined in an exception-specification");
17245 /* Parse the type-id-list. */
17246 type_id_list = cp_parser_type_id_list (parser);
17247 /* Restore the saved message. */
17248 parser->type_definition_forbidden_message = saved_message;
17250 else
17251 type_id_list = empty_except_spec;
17253 /* Look for the `)'. */
17254 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17256 return type_id_list;
17259 /* Parse an (optional) type-id-list.
17261 type-id-list:
17262 type-id ... [opt]
17263 type-id-list , type-id ... [opt]
17265 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
17266 in the order that the types were presented. */
17268 static tree
17269 cp_parser_type_id_list (cp_parser* parser)
17271 tree types = NULL_TREE;
17273 while (true)
17275 cp_token *token;
17276 tree type;
17278 /* Get the next type-id. */
17279 type = cp_parser_type_id (parser);
17280 /* Parse the optional ellipsis. */
17281 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17283 /* Consume the `...'. */
17284 cp_lexer_consume_token (parser->lexer);
17286 /* Turn the type into a pack expansion expression. */
17287 type = make_pack_expansion (type);
17289 /* Add it to the list. */
17290 types = add_exception_specifier (types, type, /*complain=*/1);
17291 /* Peek at the next token. */
17292 token = cp_lexer_peek_token (parser->lexer);
17293 /* If it is not a `,', we are done. */
17294 if (token->type != CPP_COMMA)
17295 break;
17296 /* Consume the `,'. */
17297 cp_lexer_consume_token (parser->lexer);
17300 return nreverse (types);
17303 /* Parse a try-block.
17305 try-block:
17306 try compound-statement handler-seq */
17308 static tree
17309 cp_parser_try_block (cp_parser* parser)
17311 tree try_block;
17313 cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
17314 try_block = begin_try_block ();
17315 cp_parser_compound_statement (parser, NULL, true);
17316 finish_try_block (try_block);
17317 cp_parser_handler_seq (parser);
17318 finish_handler_sequence (try_block);
17320 return try_block;
17323 /* Parse a function-try-block.
17325 function-try-block:
17326 try ctor-initializer [opt] function-body handler-seq */
17328 static bool
17329 cp_parser_function_try_block (cp_parser* parser)
17331 tree compound_stmt;
17332 tree try_block;
17333 bool ctor_initializer_p;
17335 /* Look for the `try' keyword. */
17336 if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
17337 return false;
17338 /* Let the rest of the front end know where we are. */
17339 try_block = begin_function_try_block (&compound_stmt);
17340 /* Parse the function-body. */
17341 ctor_initializer_p
17342 = cp_parser_ctor_initializer_opt_and_function_body (parser);
17343 /* We're done with the `try' part. */
17344 finish_function_try_block (try_block);
17345 /* Parse the handlers. */
17346 cp_parser_handler_seq (parser);
17347 /* We're done with the handlers. */
17348 finish_function_handler_sequence (try_block, compound_stmt);
17350 return ctor_initializer_p;
17353 /* Parse a handler-seq.
17355 handler-seq:
17356 handler handler-seq [opt] */
17358 static void
17359 cp_parser_handler_seq (cp_parser* parser)
17361 while (true)
17363 cp_token *token;
17365 /* Parse the handler. */
17366 cp_parser_handler (parser);
17367 /* Peek at the next token. */
17368 token = cp_lexer_peek_token (parser->lexer);
17369 /* If it's not `catch' then there are no more handlers. */
17370 if (!cp_parser_is_keyword (token, RID_CATCH))
17371 break;
17375 /* Parse a handler.
17377 handler:
17378 catch ( exception-declaration ) compound-statement */
17380 static void
17381 cp_parser_handler (cp_parser* parser)
17383 tree handler;
17384 tree declaration;
17386 cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
17387 handler = begin_handler ();
17388 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17389 declaration = cp_parser_exception_declaration (parser);
17390 finish_handler_parms (declaration, handler);
17391 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17392 cp_parser_compound_statement (parser, NULL, false);
17393 finish_handler (handler);
17396 /* Parse an exception-declaration.
17398 exception-declaration:
17399 type-specifier-seq declarator
17400 type-specifier-seq abstract-declarator
17401 type-specifier-seq
17404 Returns a VAR_DECL for the declaration, or NULL_TREE if the
17405 ellipsis variant is used. */
17407 static tree
17408 cp_parser_exception_declaration (cp_parser* parser)
17410 cp_decl_specifier_seq type_specifiers;
17411 cp_declarator *declarator;
17412 const char *saved_message;
17414 /* If it's an ellipsis, it's easy to handle. */
17415 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17417 /* Consume the `...' token. */
17418 cp_lexer_consume_token (parser->lexer);
17419 return NULL_TREE;
17422 /* Types may not be defined in exception-declarations. */
17423 saved_message = parser->type_definition_forbidden_message;
17424 parser->type_definition_forbidden_message
17425 = G_("types may not be defined in exception-declarations");
17427 /* Parse the type-specifier-seq. */
17428 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
17429 /*is_trailing_return=*/false,
17430 &type_specifiers);
17431 /* If it's a `)', then there is no declarator. */
17432 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
17433 declarator = NULL;
17434 else
17435 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
17436 /*ctor_dtor_or_conv_p=*/NULL,
17437 /*parenthesized_p=*/NULL,
17438 /*member_p=*/false);
17440 /* Restore the saved message. */
17441 parser->type_definition_forbidden_message = saved_message;
17443 if (!type_specifiers.any_specifiers_p)
17444 return error_mark_node;
17446 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
17449 /* Parse a throw-expression.
17451 throw-expression:
17452 throw assignment-expression [opt]
17454 Returns a THROW_EXPR representing the throw-expression. */
17456 static tree
17457 cp_parser_throw_expression (cp_parser* parser)
17459 tree expression;
17460 cp_token* token;
17462 cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
17463 token = cp_lexer_peek_token (parser->lexer);
17464 /* Figure out whether or not there is an assignment-expression
17465 following the "throw" keyword. */
17466 if (token->type == CPP_COMMA
17467 || token->type == CPP_SEMICOLON
17468 || token->type == CPP_CLOSE_PAREN
17469 || token->type == CPP_CLOSE_SQUARE
17470 || token->type == CPP_CLOSE_BRACE
17471 || token->type == CPP_COLON)
17472 expression = NULL_TREE;
17473 else
17474 expression = cp_parser_assignment_expression (parser,
17475 /*cast_p=*/false, NULL);
17477 return build_throw (expression);
17480 /* GNU Extensions */
17482 /* Parse an (optional) asm-specification.
17484 asm-specification:
17485 asm ( string-literal )
17487 If the asm-specification is present, returns a STRING_CST
17488 corresponding to the string-literal. Otherwise, returns
17489 NULL_TREE. */
17491 static tree
17492 cp_parser_asm_specification_opt (cp_parser* parser)
17494 cp_token *token;
17495 tree asm_specification;
17497 /* Peek at the next token. */
17498 token = cp_lexer_peek_token (parser->lexer);
17499 /* If the next token isn't the `asm' keyword, then there's no
17500 asm-specification. */
17501 if (!cp_parser_is_keyword (token, RID_ASM))
17502 return NULL_TREE;
17504 /* Consume the `asm' token. */
17505 cp_lexer_consume_token (parser->lexer);
17506 /* Look for the `('. */
17507 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17509 /* Look for the string-literal. */
17510 asm_specification = cp_parser_string_literal (parser, false, false);
17512 /* Look for the `)'. */
17513 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17515 return asm_specification;
17518 /* Parse an asm-operand-list.
17520 asm-operand-list:
17521 asm-operand
17522 asm-operand-list , asm-operand
17524 asm-operand:
17525 string-literal ( expression )
17526 [ string-literal ] string-literal ( expression )
17528 Returns a TREE_LIST representing the operands. The TREE_VALUE of
17529 each node is the expression. The TREE_PURPOSE is itself a
17530 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
17531 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
17532 is a STRING_CST for the string literal before the parenthesis. Returns
17533 ERROR_MARK_NODE if any of the operands are invalid. */
17535 static tree
17536 cp_parser_asm_operand_list (cp_parser* parser)
17538 tree asm_operands = NULL_TREE;
17539 bool invalid_operands = false;
17541 while (true)
17543 tree string_literal;
17544 tree expression;
17545 tree name;
17547 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
17549 /* Consume the `[' token. */
17550 cp_lexer_consume_token (parser->lexer);
17551 /* Read the operand name. */
17552 name = cp_parser_identifier (parser);
17553 if (name != error_mark_node)
17554 name = build_string (IDENTIFIER_LENGTH (name),
17555 IDENTIFIER_POINTER (name));
17556 /* Look for the closing `]'. */
17557 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
17559 else
17560 name = NULL_TREE;
17561 /* Look for the string-literal. */
17562 string_literal = cp_parser_string_literal (parser, false, false);
17564 /* Look for the `('. */
17565 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17566 /* Parse the expression. */
17567 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
17568 /* Look for the `)'. */
17569 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17571 if (name == error_mark_node
17572 || string_literal == error_mark_node
17573 || expression == error_mark_node)
17574 invalid_operands = true;
17576 /* Add this operand to the list. */
17577 asm_operands = tree_cons (build_tree_list (name, string_literal),
17578 expression,
17579 asm_operands);
17580 /* If the next token is not a `,', there are no more
17581 operands. */
17582 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17583 break;
17584 /* Consume the `,'. */
17585 cp_lexer_consume_token (parser->lexer);
17588 return invalid_operands ? error_mark_node : nreverse (asm_operands);
17591 /* Parse an asm-clobber-list.
17593 asm-clobber-list:
17594 string-literal
17595 asm-clobber-list , string-literal
17597 Returns a TREE_LIST, indicating the clobbers in the order that they
17598 appeared. The TREE_VALUE of each node is a STRING_CST. */
17600 static tree
17601 cp_parser_asm_clobber_list (cp_parser* parser)
17603 tree clobbers = NULL_TREE;
17605 while (true)
17607 tree string_literal;
17609 /* Look for the string literal. */
17610 string_literal = cp_parser_string_literal (parser, false, false);
17611 /* Add it to the list. */
17612 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
17613 /* If the next token is not a `,', then the list is
17614 complete. */
17615 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17616 break;
17617 /* Consume the `,' token. */
17618 cp_lexer_consume_token (parser->lexer);
17621 return clobbers;
17624 /* Parse an asm-label-list.
17626 asm-label-list:
17627 identifier
17628 asm-label-list , identifier
17630 Returns a TREE_LIST, indicating the labels in the order that they
17631 appeared. The TREE_VALUE of each node is a label. */
17633 static tree
17634 cp_parser_asm_label_list (cp_parser* parser)
17636 tree labels = NULL_TREE;
17638 while (true)
17640 tree identifier, label, name;
17642 /* Look for the identifier. */
17643 identifier = cp_parser_identifier (parser);
17644 if (!error_operand_p (identifier))
17646 label = lookup_label (identifier);
17647 if (TREE_CODE (label) == LABEL_DECL)
17649 TREE_USED (label) = 1;
17650 check_goto (label);
17651 name = build_string (IDENTIFIER_LENGTH (identifier),
17652 IDENTIFIER_POINTER (identifier));
17653 labels = tree_cons (name, label, labels);
17656 /* If the next token is not a `,', then the list is
17657 complete. */
17658 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17659 break;
17660 /* Consume the `,' token. */
17661 cp_lexer_consume_token (parser->lexer);
17664 return nreverse (labels);
17667 /* Parse an (optional) series of attributes.
17669 attributes:
17670 attributes attribute
17672 attribute:
17673 __attribute__ (( attribute-list [opt] ))
17675 The return value is as for cp_parser_attribute_list. */
17677 static tree
17678 cp_parser_attributes_opt (cp_parser* parser)
17680 tree attributes = NULL_TREE;
17682 while (true)
17684 cp_token *token;
17685 tree attribute_list;
17687 /* Peek at the next token. */
17688 token = cp_lexer_peek_token (parser->lexer);
17689 /* If it's not `__attribute__', then we're done. */
17690 if (token->keyword != RID_ATTRIBUTE)
17691 break;
17693 /* Consume the `__attribute__' keyword. */
17694 cp_lexer_consume_token (parser->lexer);
17695 /* Look for the two `(' tokens. */
17696 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17697 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17699 /* Peek at the next token. */
17700 token = cp_lexer_peek_token (parser->lexer);
17701 if (token->type != CPP_CLOSE_PAREN)
17702 /* Parse the attribute-list. */
17703 attribute_list = cp_parser_attribute_list (parser);
17704 else
17705 /* If the next token is a `)', then there is no attribute
17706 list. */
17707 attribute_list = NULL;
17709 /* Look for the two `)' tokens. */
17710 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17711 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17713 /* Add these new attributes to the list. */
17714 attributes = chainon (attributes, attribute_list);
17717 return attributes;
17720 /* Parse an attribute-list.
17722 attribute-list:
17723 attribute
17724 attribute-list , attribute
17726 attribute:
17727 identifier
17728 identifier ( identifier )
17729 identifier ( identifier , expression-list )
17730 identifier ( expression-list )
17732 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
17733 to an attribute. The TREE_PURPOSE of each node is the identifier
17734 indicating which attribute is in use. The TREE_VALUE represents
17735 the arguments, if any. */
17737 static tree
17738 cp_parser_attribute_list (cp_parser* parser)
17740 tree attribute_list = NULL_TREE;
17741 bool save_translate_strings_p = parser->translate_strings_p;
17743 parser->translate_strings_p = false;
17744 while (true)
17746 cp_token *token;
17747 tree identifier;
17748 tree attribute;
17750 /* Look for the identifier. We also allow keywords here; for
17751 example `__attribute__ ((const))' is legal. */
17752 token = cp_lexer_peek_token (parser->lexer);
17753 if (token->type == CPP_NAME
17754 || token->type == CPP_KEYWORD)
17756 tree arguments = NULL_TREE;
17758 /* Consume the token. */
17759 token = cp_lexer_consume_token (parser->lexer);
17761 /* Save away the identifier that indicates which attribute
17762 this is. */
17763 identifier = (token->type == CPP_KEYWORD)
17764 /* For keywords, use the canonical spelling, not the
17765 parsed identifier. */
17766 ? ridpointers[(int) token->keyword]
17767 : token->u.value;
17769 attribute = build_tree_list (identifier, NULL_TREE);
17771 /* Peek at the next token. */
17772 token = cp_lexer_peek_token (parser->lexer);
17773 /* If it's an `(', then parse the attribute arguments. */
17774 if (token->type == CPP_OPEN_PAREN)
17776 VEC(tree,gc) *vec;
17777 int attr_flag = (attribute_takes_identifier_p (identifier)
17778 ? id_attr : normal_attr);
17779 vec = cp_parser_parenthesized_expression_list
17780 (parser, attr_flag, /*cast_p=*/false,
17781 /*allow_expansion_p=*/false,
17782 /*non_constant_p=*/NULL);
17783 if (vec == NULL)
17784 arguments = error_mark_node;
17785 else
17787 arguments = build_tree_list_vec (vec);
17788 release_tree_vector (vec);
17790 /* Save the arguments away. */
17791 TREE_VALUE (attribute) = arguments;
17794 if (arguments != error_mark_node)
17796 /* Add this attribute to the list. */
17797 TREE_CHAIN (attribute) = attribute_list;
17798 attribute_list = attribute;
17801 token = cp_lexer_peek_token (parser->lexer);
17803 /* Now, look for more attributes. If the next token isn't a
17804 `,', we're done. */
17805 if (token->type != CPP_COMMA)
17806 break;
17808 /* Consume the comma and keep going. */
17809 cp_lexer_consume_token (parser->lexer);
17811 parser->translate_strings_p = save_translate_strings_p;
17813 /* We built up the list in reverse order. */
17814 return nreverse (attribute_list);
17817 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
17818 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
17819 current value of the PEDANTIC flag, regardless of whether or not
17820 the `__extension__' keyword is present. The caller is responsible
17821 for restoring the value of the PEDANTIC flag. */
17823 static bool
17824 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
17826 /* Save the old value of the PEDANTIC flag. */
17827 *saved_pedantic = pedantic;
17829 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
17831 /* Consume the `__extension__' token. */
17832 cp_lexer_consume_token (parser->lexer);
17833 /* We're not being pedantic while the `__extension__' keyword is
17834 in effect. */
17835 pedantic = 0;
17837 return true;
17840 return false;
17843 /* Parse a label declaration.
17845 label-declaration:
17846 __label__ label-declarator-seq ;
17848 label-declarator-seq:
17849 identifier , label-declarator-seq
17850 identifier */
17852 static void
17853 cp_parser_label_declaration (cp_parser* parser)
17855 /* Look for the `__label__' keyword. */
17856 cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
17858 while (true)
17860 tree identifier;
17862 /* Look for an identifier. */
17863 identifier = cp_parser_identifier (parser);
17864 /* If we failed, stop. */
17865 if (identifier == error_mark_node)
17866 break;
17867 /* Declare it as a label. */
17868 finish_label_decl (identifier);
17869 /* If the next token is a `;', stop. */
17870 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17871 break;
17872 /* Look for the `,' separating the label declarations. */
17873 cp_parser_require (parser, CPP_COMMA, "%<,%>");
17876 /* Look for the final `;'. */
17877 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
17880 /* Support Functions */
17882 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
17883 NAME should have one of the representations used for an
17884 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
17885 is returned. If PARSER->SCOPE is a dependent type, then a
17886 SCOPE_REF is returned.
17888 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
17889 returned; the name was already resolved when the TEMPLATE_ID_EXPR
17890 was formed. Abstractly, such entities should not be passed to this
17891 function, because they do not need to be looked up, but it is
17892 simpler to check for this special case here, rather than at the
17893 call-sites.
17895 In cases not explicitly covered above, this function returns a
17896 DECL, OVERLOAD, or baselink representing the result of the lookup.
17897 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
17898 is returned.
17900 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
17901 (e.g., "struct") that was used. In that case bindings that do not
17902 refer to types are ignored.
17904 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
17905 ignored.
17907 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
17908 are ignored.
17910 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
17911 types.
17913 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
17914 TREE_LIST of candidates if name-lookup results in an ambiguity, and
17915 NULL_TREE otherwise. */
17917 static tree
17918 cp_parser_lookup_name (cp_parser *parser, tree name,
17919 enum tag_types tag_type,
17920 bool is_template,
17921 bool is_namespace,
17922 bool check_dependency,
17923 tree *ambiguous_decls,
17924 location_t name_location)
17926 int flags = 0;
17927 tree decl;
17928 tree object_type = parser->context->object_type;
17930 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17931 flags |= LOOKUP_COMPLAIN;
17933 /* Assume that the lookup will be unambiguous. */
17934 if (ambiguous_decls)
17935 *ambiguous_decls = NULL_TREE;
17937 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
17938 no longer valid. Note that if we are parsing tentatively, and
17939 the parse fails, OBJECT_TYPE will be automatically restored. */
17940 parser->context->object_type = NULL_TREE;
17942 if (name == error_mark_node)
17943 return error_mark_node;
17945 /* A template-id has already been resolved; there is no lookup to
17946 do. */
17947 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
17948 return name;
17949 if (BASELINK_P (name))
17951 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
17952 == TEMPLATE_ID_EXPR);
17953 return name;
17956 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
17957 it should already have been checked to make sure that the name
17958 used matches the type being destroyed. */
17959 if (TREE_CODE (name) == BIT_NOT_EXPR)
17961 tree type;
17963 /* Figure out to which type this destructor applies. */
17964 if (parser->scope)
17965 type = parser->scope;
17966 else if (object_type)
17967 type = object_type;
17968 else
17969 type = current_class_type;
17970 /* If that's not a class type, there is no destructor. */
17971 if (!type || !CLASS_TYPE_P (type))
17972 return error_mark_node;
17973 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
17974 lazily_declare_fn (sfk_destructor, type);
17975 if (!CLASSTYPE_DESTRUCTORS (type))
17976 return error_mark_node;
17977 /* If it was a class type, return the destructor. */
17978 return CLASSTYPE_DESTRUCTORS (type);
17981 /* By this point, the NAME should be an ordinary identifier. If
17982 the id-expression was a qualified name, the qualifying scope is
17983 stored in PARSER->SCOPE at this point. */
17984 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
17986 /* Perform the lookup. */
17987 if (parser->scope)
17989 bool dependent_p;
17991 if (parser->scope == error_mark_node)
17992 return error_mark_node;
17994 /* If the SCOPE is dependent, the lookup must be deferred until
17995 the template is instantiated -- unless we are explicitly
17996 looking up names in uninstantiated templates. Even then, we
17997 cannot look up the name if the scope is not a class type; it
17998 might, for example, be a template type parameter. */
17999 dependent_p = (TYPE_P (parser->scope)
18000 && dependent_scope_p (parser->scope));
18001 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
18002 && dependent_p)
18003 /* Defer lookup. */
18004 decl = error_mark_node;
18005 else
18007 tree pushed_scope = NULL_TREE;
18009 /* If PARSER->SCOPE is a dependent type, then it must be a
18010 class type, and we must not be checking dependencies;
18011 otherwise, we would have processed this lookup above. So
18012 that PARSER->SCOPE is not considered a dependent base by
18013 lookup_member, we must enter the scope here. */
18014 if (dependent_p)
18015 pushed_scope = push_scope (parser->scope);
18017 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
18018 lookup result and the nested-name-specifier nominates a class C:
18019 * if the name specified after the nested-name-specifier, when
18020 looked up in C, is the injected-class-name of C (Clause 9), or
18021 * if the name specified after the nested-name-specifier is the
18022 same as the identifier or the simple-template-id's template-
18023 name in the last component of the nested-name-specifier,
18024 the name is instead considered to name the constructor of
18025 class C. [ Note: for example, the constructor is not an
18026 acceptable lookup result in an elaborated-type-specifier so
18027 the constructor would not be used in place of the
18028 injected-class-name. --end note ] Such a constructor name
18029 shall be used only in the declarator-id of a declaration that
18030 names a constructor or in a using-declaration. */
18031 if (tag_type == none_type
18032 && CLASS_TYPE_P (parser->scope)
18033 && constructor_name_p (name, parser->scope))
18034 name = ctor_identifier;
18036 /* If the PARSER->SCOPE is a template specialization, it
18037 may be instantiated during name lookup. In that case,
18038 errors may be issued. Even if we rollback the current
18039 tentative parse, those errors are valid. */
18040 decl = lookup_qualified_name (parser->scope, name,
18041 tag_type != none_type,
18042 /*complain=*/true);
18044 /* If we have a single function from a using decl, pull it out. */
18045 if (TREE_CODE (decl) == OVERLOAD
18046 && !really_overloaded_fn (decl))
18047 decl = OVL_FUNCTION (decl);
18049 if (pushed_scope)
18050 pop_scope (pushed_scope);
18053 /* If the scope is a dependent type and either we deferred lookup or
18054 we did lookup but didn't find the name, rememeber the name. */
18055 if (decl == error_mark_node && TYPE_P (parser->scope)
18056 && dependent_type_p (parser->scope))
18058 if (tag_type)
18060 tree type;
18062 /* The resolution to Core Issue 180 says that `struct
18063 A::B' should be considered a type-name, even if `A'
18064 is dependent. */
18065 type = make_typename_type (parser->scope, name, tag_type,
18066 /*complain=*/tf_error);
18067 decl = TYPE_NAME (type);
18069 else if (is_template
18070 && (cp_parser_next_token_ends_template_argument_p (parser)
18071 || cp_lexer_next_token_is (parser->lexer,
18072 CPP_CLOSE_PAREN)))
18073 decl = make_unbound_class_template (parser->scope,
18074 name, NULL_TREE,
18075 /*complain=*/tf_error);
18076 else
18077 decl = build_qualified_name (/*type=*/NULL_TREE,
18078 parser->scope, name,
18079 is_template);
18081 parser->qualifying_scope = parser->scope;
18082 parser->object_scope = NULL_TREE;
18084 else if (object_type)
18086 tree object_decl = NULL_TREE;
18087 /* Look up the name in the scope of the OBJECT_TYPE, unless the
18088 OBJECT_TYPE is not a class. */
18089 if (CLASS_TYPE_P (object_type))
18090 /* If the OBJECT_TYPE is a template specialization, it may
18091 be instantiated during name lookup. In that case, errors
18092 may be issued. Even if we rollback the current tentative
18093 parse, those errors are valid. */
18094 object_decl = lookup_member (object_type,
18095 name,
18096 /*protect=*/0,
18097 tag_type != none_type);
18098 /* Look it up in the enclosing context, too. */
18099 decl = lookup_name_real (name, tag_type != none_type,
18100 /*nonclass=*/0,
18101 /*block_p=*/true, is_namespace, flags);
18102 parser->object_scope = object_type;
18103 parser->qualifying_scope = NULL_TREE;
18104 if (object_decl)
18105 decl = object_decl;
18107 else
18109 decl = lookup_name_real (name, tag_type != none_type,
18110 /*nonclass=*/0,
18111 /*block_p=*/true, is_namespace, flags);
18112 parser->qualifying_scope = NULL_TREE;
18113 parser->object_scope = NULL_TREE;
18116 /* If the lookup failed, let our caller know. */
18117 if (!decl || decl == error_mark_node)
18118 return error_mark_node;
18120 /* Pull out the template from an injected-class-name (or multiple). */
18121 if (is_template)
18122 decl = maybe_get_template_decl_from_type_decl (decl);
18124 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
18125 if (TREE_CODE (decl) == TREE_LIST)
18127 if (ambiguous_decls)
18128 *ambiguous_decls = decl;
18129 /* The error message we have to print is too complicated for
18130 cp_parser_error, so we incorporate its actions directly. */
18131 if (!cp_parser_simulate_error (parser))
18133 error_at (name_location, "reference to %qD is ambiguous",
18134 name);
18135 print_candidates (decl);
18137 return error_mark_node;
18140 gcc_assert (DECL_P (decl)
18141 || TREE_CODE (decl) == OVERLOAD
18142 || TREE_CODE (decl) == SCOPE_REF
18143 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
18144 || BASELINK_P (decl));
18146 /* If we have resolved the name of a member declaration, check to
18147 see if the declaration is accessible. When the name resolves to
18148 set of overloaded functions, accessibility is checked when
18149 overload resolution is done.
18151 During an explicit instantiation, access is not checked at all,
18152 as per [temp.explicit]. */
18153 if (DECL_P (decl))
18154 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
18156 return decl;
18159 /* Like cp_parser_lookup_name, but for use in the typical case where
18160 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
18161 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
18163 static tree
18164 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
18166 return cp_parser_lookup_name (parser, name,
18167 none_type,
18168 /*is_template=*/false,
18169 /*is_namespace=*/false,
18170 /*check_dependency=*/true,
18171 /*ambiguous_decls=*/NULL,
18172 location);
18175 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
18176 the current context, return the TYPE_DECL. If TAG_NAME_P is
18177 true, the DECL indicates the class being defined in a class-head,
18178 or declared in an elaborated-type-specifier.
18180 Otherwise, return DECL. */
18182 static tree
18183 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
18185 /* If the TEMPLATE_DECL is being declared as part of a class-head,
18186 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
18188 struct A {
18189 template <typename T> struct B;
18192 template <typename T> struct A::B {};
18194 Similarly, in an elaborated-type-specifier:
18196 namespace N { struct X{}; }
18198 struct A {
18199 template <typename T> friend struct N::X;
18202 However, if the DECL refers to a class type, and we are in
18203 the scope of the class, then the name lookup automatically
18204 finds the TYPE_DECL created by build_self_reference rather
18205 than a TEMPLATE_DECL. For example, in:
18207 template <class T> struct S {
18208 S s;
18211 there is no need to handle such case. */
18213 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
18214 return DECL_TEMPLATE_RESULT (decl);
18216 return decl;
18219 /* If too many, or too few, template-parameter lists apply to the
18220 declarator, issue an error message. Returns TRUE if all went well,
18221 and FALSE otherwise. */
18223 static bool
18224 cp_parser_check_declarator_template_parameters (cp_parser* parser,
18225 cp_declarator *declarator,
18226 location_t declarator_location)
18228 unsigned num_templates;
18230 /* We haven't seen any classes that involve template parameters yet. */
18231 num_templates = 0;
18233 switch (declarator->kind)
18235 case cdk_id:
18236 if (declarator->u.id.qualifying_scope)
18238 tree scope;
18240 scope = declarator->u.id.qualifying_scope;
18242 while (scope && CLASS_TYPE_P (scope))
18244 /* You're supposed to have one `template <...>'
18245 for every template class, but you don't need one
18246 for a full specialization. For example:
18248 template <class T> struct S{};
18249 template <> struct S<int> { void f(); };
18250 void S<int>::f () {}
18252 is correct; there shouldn't be a `template <>' for
18253 the definition of `S<int>::f'. */
18254 if (!CLASSTYPE_TEMPLATE_INFO (scope))
18255 /* If SCOPE does not have template information of any
18256 kind, then it is not a template, nor is it nested
18257 within a template. */
18258 break;
18259 if (explicit_class_specialization_p (scope))
18260 break;
18261 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
18262 ++num_templates;
18264 scope = TYPE_CONTEXT (scope);
18267 else if (TREE_CODE (declarator->u.id.unqualified_name)
18268 == TEMPLATE_ID_EXPR)
18269 /* If the DECLARATOR has the form `X<y>' then it uses one
18270 additional level of template parameters. */
18271 ++num_templates;
18273 return cp_parser_check_template_parameters
18274 (parser, num_templates, declarator_location, declarator);
18277 case cdk_function:
18278 case cdk_array:
18279 case cdk_pointer:
18280 case cdk_reference:
18281 case cdk_ptrmem:
18282 return (cp_parser_check_declarator_template_parameters
18283 (parser, declarator->declarator, declarator_location));
18285 case cdk_error:
18286 return true;
18288 default:
18289 gcc_unreachable ();
18291 return false;
18294 /* NUM_TEMPLATES were used in the current declaration. If that is
18295 invalid, return FALSE and issue an error messages. Otherwise,
18296 return TRUE. If DECLARATOR is non-NULL, then we are checking a
18297 declarator and we can print more accurate diagnostics. */
18299 static bool
18300 cp_parser_check_template_parameters (cp_parser* parser,
18301 unsigned num_templates,
18302 location_t location,
18303 cp_declarator *declarator)
18305 /* If there are the same number of template classes and parameter
18306 lists, that's OK. */
18307 if (parser->num_template_parameter_lists == num_templates)
18308 return true;
18309 /* If there are more, but only one more, then we are referring to a
18310 member template. That's OK too. */
18311 if (parser->num_template_parameter_lists == num_templates + 1)
18312 return true;
18313 /* If there are more template classes than parameter lists, we have
18314 something like:
18316 template <class T> void S<T>::R<T>::f (); */
18317 if (parser->num_template_parameter_lists < num_templates)
18319 if (declarator && !current_function_decl)
18320 error_at (location, "specializing member %<%T::%E%> "
18321 "requires %<template<>%> syntax",
18322 declarator->u.id.qualifying_scope,
18323 declarator->u.id.unqualified_name);
18324 else if (declarator)
18325 error_at (location, "invalid declaration of %<%T::%E%>",
18326 declarator->u.id.qualifying_scope,
18327 declarator->u.id.unqualified_name);
18328 else
18329 error_at (location, "too few template-parameter-lists");
18330 return false;
18332 /* Otherwise, there are too many template parameter lists. We have
18333 something like:
18335 template <class T> template <class U> void S::f(); */
18336 error_at (location, "too many template-parameter-lists");
18337 return false;
18340 /* Parse an optional `::' token indicating that the following name is
18341 from the global namespace. If so, PARSER->SCOPE is set to the
18342 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
18343 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
18344 Returns the new value of PARSER->SCOPE, if the `::' token is
18345 present, and NULL_TREE otherwise. */
18347 static tree
18348 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
18350 cp_token *token;
18352 /* Peek at the next token. */
18353 token = cp_lexer_peek_token (parser->lexer);
18354 /* If we're looking at a `::' token then we're starting from the
18355 global namespace, not our current location. */
18356 if (token->type == CPP_SCOPE)
18358 /* Consume the `::' token. */
18359 cp_lexer_consume_token (parser->lexer);
18360 /* Set the SCOPE so that we know where to start the lookup. */
18361 parser->scope = global_namespace;
18362 parser->qualifying_scope = global_namespace;
18363 parser->object_scope = NULL_TREE;
18365 return parser->scope;
18367 else if (!current_scope_valid_p)
18369 parser->scope = NULL_TREE;
18370 parser->qualifying_scope = NULL_TREE;
18371 parser->object_scope = NULL_TREE;
18374 return NULL_TREE;
18377 /* Returns TRUE if the upcoming token sequence is the start of a
18378 constructor declarator. If FRIEND_P is true, the declarator is
18379 preceded by the `friend' specifier. */
18381 static bool
18382 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
18384 bool constructor_p;
18385 tree nested_name_specifier;
18386 cp_token *next_token;
18388 /* The common case is that this is not a constructor declarator, so
18389 try to avoid doing lots of work if at all possible. It's not
18390 valid declare a constructor at function scope. */
18391 if (parser->in_function_body)
18392 return false;
18393 /* And only certain tokens can begin a constructor declarator. */
18394 next_token = cp_lexer_peek_token (parser->lexer);
18395 if (next_token->type != CPP_NAME
18396 && next_token->type != CPP_SCOPE
18397 && next_token->type != CPP_NESTED_NAME_SPECIFIER
18398 && next_token->type != CPP_TEMPLATE_ID)
18399 return false;
18401 /* Parse tentatively; we are going to roll back all of the tokens
18402 consumed here. */
18403 cp_parser_parse_tentatively (parser);
18404 /* Assume that we are looking at a constructor declarator. */
18405 constructor_p = true;
18407 /* Look for the optional `::' operator. */
18408 cp_parser_global_scope_opt (parser,
18409 /*current_scope_valid_p=*/false);
18410 /* Look for the nested-name-specifier. */
18411 nested_name_specifier
18412 = (cp_parser_nested_name_specifier_opt (parser,
18413 /*typename_keyword_p=*/false,
18414 /*check_dependency_p=*/false,
18415 /*type_p=*/false,
18416 /*is_declaration=*/false));
18417 /* Outside of a class-specifier, there must be a
18418 nested-name-specifier. */
18419 if (!nested_name_specifier &&
18420 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
18421 || friend_p))
18422 constructor_p = false;
18423 else if (nested_name_specifier == error_mark_node)
18424 constructor_p = false;
18426 /* If we have a class scope, this is easy; DR 147 says that S::S always
18427 names the constructor, and no other qualified name could. */
18428 if (constructor_p && nested_name_specifier
18429 && TYPE_P (nested_name_specifier))
18431 tree id = cp_parser_unqualified_id (parser,
18432 /*template_keyword_p=*/false,
18433 /*check_dependency_p=*/false,
18434 /*declarator_p=*/true,
18435 /*optional_p=*/false);
18436 if (is_overloaded_fn (id))
18437 id = DECL_NAME (get_first_fn (id));
18438 if (!constructor_name_p (id, nested_name_specifier))
18439 constructor_p = false;
18441 /* If we still think that this might be a constructor-declarator,
18442 look for a class-name. */
18443 else if (constructor_p)
18445 /* If we have:
18447 template <typename T> struct S {
18448 S();
18451 we must recognize that the nested `S' names a class. */
18452 tree type_decl;
18453 type_decl = cp_parser_class_name (parser,
18454 /*typename_keyword_p=*/false,
18455 /*template_keyword_p=*/false,
18456 none_type,
18457 /*check_dependency_p=*/false,
18458 /*class_head_p=*/false,
18459 /*is_declaration=*/false);
18460 /* If there was no class-name, then this is not a constructor. */
18461 constructor_p = !cp_parser_error_occurred (parser);
18463 /* If we're still considering a constructor, we have to see a `(',
18464 to begin the parameter-declaration-clause, followed by either a
18465 `)', an `...', or a decl-specifier. We need to check for a
18466 type-specifier to avoid being fooled into thinking that:
18468 S (f) (int);
18470 is a constructor. (It is actually a function named `f' that
18471 takes one parameter (of type `int') and returns a value of type
18472 `S'. */
18473 if (constructor_p
18474 && !cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
18475 constructor_p = false;
18477 if (constructor_p
18478 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
18479 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
18480 /* A parameter declaration begins with a decl-specifier,
18481 which is either the "attribute" keyword, a storage class
18482 specifier, or (usually) a type-specifier. */
18483 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
18485 tree type;
18486 tree pushed_scope = NULL_TREE;
18487 unsigned saved_num_template_parameter_lists;
18489 /* Names appearing in the type-specifier should be looked up
18490 in the scope of the class. */
18491 if (current_class_type)
18492 type = NULL_TREE;
18493 else
18495 type = TREE_TYPE (type_decl);
18496 if (TREE_CODE (type) == TYPENAME_TYPE)
18498 type = resolve_typename_type (type,
18499 /*only_current_p=*/false);
18500 if (TREE_CODE (type) == TYPENAME_TYPE)
18502 cp_parser_abort_tentative_parse (parser);
18503 return false;
18506 pushed_scope = push_scope (type);
18509 /* Inside the constructor parameter list, surrounding
18510 template-parameter-lists do not apply. */
18511 saved_num_template_parameter_lists
18512 = parser->num_template_parameter_lists;
18513 parser->num_template_parameter_lists = 0;
18515 /* Look for the type-specifier. */
18516 cp_parser_type_specifier (parser,
18517 CP_PARSER_FLAGS_NONE,
18518 /*decl_specs=*/NULL,
18519 /*is_declarator=*/true,
18520 /*declares_class_or_enum=*/NULL,
18521 /*is_cv_qualifier=*/NULL);
18523 parser->num_template_parameter_lists
18524 = saved_num_template_parameter_lists;
18526 /* Leave the scope of the class. */
18527 if (pushed_scope)
18528 pop_scope (pushed_scope);
18530 constructor_p = !cp_parser_error_occurred (parser);
18534 /* We did not really want to consume any tokens. */
18535 cp_parser_abort_tentative_parse (parser);
18537 return constructor_p;
18540 /* Parse the definition of the function given by the DECL_SPECIFIERS,
18541 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
18542 they must be performed once we are in the scope of the function.
18544 Returns the function defined. */
18546 static tree
18547 cp_parser_function_definition_from_specifiers_and_declarator
18548 (cp_parser* parser,
18549 cp_decl_specifier_seq *decl_specifiers,
18550 tree attributes,
18551 const cp_declarator *declarator)
18553 tree fn;
18554 bool success_p;
18556 /* Begin the function-definition. */
18557 success_p = start_function (decl_specifiers, declarator, attributes);
18559 /* The things we're about to see are not directly qualified by any
18560 template headers we've seen thus far. */
18561 reset_specialization ();
18563 /* If there were names looked up in the decl-specifier-seq that we
18564 did not check, check them now. We must wait until we are in the
18565 scope of the function to perform the checks, since the function
18566 might be a friend. */
18567 perform_deferred_access_checks ();
18569 if (!success_p)
18571 /* Skip the entire function. */
18572 cp_parser_skip_to_end_of_block_or_statement (parser);
18573 fn = error_mark_node;
18575 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
18577 /* Seen already, skip it. An error message has already been output. */
18578 cp_parser_skip_to_end_of_block_or_statement (parser);
18579 fn = current_function_decl;
18580 current_function_decl = NULL_TREE;
18581 /* If this is a function from a class, pop the nested class. */
18582 if (current_class_name)
18583 pop_nested_class ();
18585 else
18586 fn = cp_parser_function_definition_after_declarator (parser,
18587 /*inline_p=*/false);
18589 return fn;
18592 /* Parse the part of a function-definition that follows the
18593 declarator. INLINE_P is TRUE iff this function is an inline
18594 function defined within a class-specifier.
18596 Returns the function defined. */
18598 static tree
18599 cp_parser_function_definition_after_declarator (cp_parser* parser,
18600 bool inline_p)
18602 tree fn;
18603 bool ctor_initializer_p = false;
18604 bool saved_in_unbraced_linkage_specification_p;
18605 bool saved_in_function_body;
18606 unsigned saved_num_template_parameter_lists;
18607 cp_token *token;
18609 saved_in_function_body = parser->in_function_body;
18610 parser->in_function_body = true;
18611 /* If the next token is `return', then the code may be trying to
18612 make use of the "named return value" extension that G++ used to
18613 support. */
18614 token = cp_lexer_peek_token (parser->lexer);
18615 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
18617 /* Consume the `return' keyword. */
18618 cp_lexer_consume_token (parser->lexer);
18619 /* Look for the identifier that indicates what value is to be
18620 returned. */
18621 cp_parser_identifier (parser);
18622 /* Issue an error message. */
18623 error_at (token->location,
18624 "named return values are no longer supported");
18625 /* Skip tokens until we reach the start of the function body. */
18626 while (true)
18628 cp_token *token = cp_lexer_peek_token (parser->lexer);
18629 if (token->type == CPP_OPEN_BRACE
18630 || token->type == CPP_EOF
18631 || token->type == CPP_PRAGMA_EOL)
18632 break;
18633 cp_lexer_consume_token (parser->lexer);
18636 /* The `extern' in `extern "C" void f () { ... }' does not apply to
18637 anything declared inside `f'. */
18638 saved_in_unbraced_linkage_specification_p
18639 = parser->in_unbraced_linkage_specification_p;
18640 parser->in_unbraced_linkage_specification_p = false;
18641 /* Inside the function, surrounding template-parameter-lists do not
18642 apply. */
18643 saved_num_template_parameter_lists
18644 = parser->num_template_parameter_lists;
18645 parser->num_template_parameter_lists = 0;
18647 start_lambda_scope (current_function_decl);
18649 /* If the next token is `try', then we are looking at a
18650 function-try-block. */
18651 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
18652 ctor_initializer_p = cp_parser_function_try_block (parser);
18653 /* A function-try-block includes the function-body, so we only do
18654 this next part if we're not processing a function-try-block. */
18655 else
18656 ctor_initializer_p
18657 = cp_parser_ctor_initializer_opt_and_function_body (parser);
18659 finish_lambda_scope ();
18661 /* Finish the function. */
18662 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
18663 (inline_p ? 2 : 0));
18664 /* Generate code for it, if necessary. */
18665 expand_or_defer_fn (fn);
18666 /* Restore the saved values. */
18667 parser->in_unbraced_linkage_specification_p
18668 = saved_in_unbraced_linkage_specification_p;
18669 parser->num_template_parameter_lists
18670 = saved_num_template_parameter_lists;
18671 parser->in_function_body = saved_in_function_body;
18673 return fn;
18676 /* Parse a template-declaration, assuming that the `export' (and
18677 `extern') keywords, if present, has already been scanned. MEMBER_P
18678 is as for cp_parser_template_declaration. */
18680 static void
18681 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
18683 tree decl = NULL_TREE;
18684 VEC (deferred_access_check,gc) *checks;
18685 tree parameter_list;
18686 bool friend_p = false;
18687 bool need_lang_pop;
18688 cp_token *token;
18690 /* Look for the `template' keyword. */
18691 token = cp_lexer_peek_token (parser->lexer);
18692 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
18693 return;
18695 /* And the `<'. */
18696 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
18697 return;
18698 if (at_class_scope_p () && current_function_decl)
18700 /* 14.5.2.2 [temp.mem]
18702 A local class shall not have member templates. */
18703 error_at (token->location,
18704 "invalid declaration of member template in local class");
18705 cp_parser_skip_to_end_of_block_or_statement (parser);
18706 return;
18708 /* [temp]
18710 A template ... shall not have C linkage. */
18711 if (current_lang_name == lang_name_c)
18713 error_at (token->location, "template with C linkage");
18714 /* Give it C++ linkage to avoid confusing other parts of the
18715 front end. */
18716 push_lang_context (lang_name_cplusplus);
18717 need_lang_pop = true;
18719 else
18720 need_lang_pop = false;
18722 /* We cannot perform access checks on the template parameter
18723 declarations until we know what is being declared, just as we
18724 cannot check the decl-specifier list. */
18725 push_deferring_access_checks (dk_deferred);
18727 /* If the next token is `>', then we have an invalid
18728 specialization. Rather than complain about an invalid template
18729 parameter, issue an error message here. */
18730 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
18732 cp_parser_error (parser, "invalid explicit specialization");
18733 begin_specialization ();
18734 parameter_list = NULL_TREE;
18736 else
18737 /* Parse the template parameters. */
18738 parameter_list = cp_parser_template_parameter_list (parser);
18740 /* Get the deferred access checks from the parameter list. These
18741 will be checked once we know what is being declared, as for a
18742 member template the checks must be performed in the scope of the
18743 class containing the member. */
18744 checks = get_deferred_access_checks ();
18746 /* Look for the `>'. */
18747 cp_parser_skip_to_end_of_template_parameter_list (parser);
18748 /* We just processed one more parameter list. */
18749 ++parser->num_template_parameter_lists;
18750 /* If the next token is `template', there are more template
18751 parameters. */
18752 if (cp_lexer_next_token_is_keyword (parser->lexer,
18753 RID_TEMPLATE))
18754 cp_parser_template_declaration_after_export (parser, member_p);
18755 else
18757 /* There are no access checks when parsing a template, as we do not
18758 know if a specialization will be a friend. */
18759 push_deferring_access_checks (dk_no_check);
18760 token = cp_lexer_peek_token (parser->lexer);
18761 decl = cp_parser_single_declaration (parser,
18762 checks,
18763 member_p,
18764 /*explicit_specialization_p=*/false,
18765 &friend_p);
18766 pop_deferring_access_checks ();
18768 /* If this is a member template declaration, let the front
18769 end know. */
18770 if (member_p && !friend_p && decl)
18772 if (TREE_CODE (decl) == TYPE_DECL)
18773 cp_parser_check_access_in_redeclaration (decl, token->location);
18775 decl = finish_member_template_decl (decl);
18777 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
18778 make_friend_class (current_class_type, TREE_TYPE (decl),
18779 /*complain=*/true);
18781 /* We are done with the current parameter list. */
18782 --parser->num_template_parameter_lists;
18784 pop_deferring_access_checks ();
18786 /* Finish up. */
18787 finish_template_decl (parameter_list);
18789 /* Register member declarations. */
18790 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
18791 finish_member_declaration (decl);
18792 /* For the erroneous case of a template with C linkage, we pushed an
18793 implicit C++ linkage scope; exit that scope now. */
18794 if (need_lang_pop)
18795 pop_lang_context ();
18796 /* If DECL is a function template, we must return to parse it later.
18797 (Even though there is no definition, there might be default
18798 arguments that need handling.) */
18799 if (member_p && decl
18800 && (TREE_CODE (decl) == FUNCTION_DECL
18801 || DECL_FUNCTION_TEMPLATE_P (decl)))
18802 TREE_VALUE (parser->unparsed_functions_queues)
18803 = tree_cons (NULL_TREE, decl,
18804 TREE_VALUE (parser->unparsed_functions_queues));
18807 /* Perform the deferred access checks from a template-parameter-list.
18808 CHECKS is a TREE_LIST of access checks, as returned by
18809 get_deferred_access_checks. */
18811 static void
18812 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
18814 ++processing_template_parmlist;
18815 perform_access_checks (checks);
18816 --processing_template_parmlist;
18819 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
18820 `function-definition' sequence. MEMBER_P is true, this declaration
18821 appears in a class scope.
18823 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
18824 *FRIEND_P is set to TRUE iff the declaration is a friend. */
18826 static tree
18827 cp_parser_single_declaration (cp_parser* parser,
18828 VEC (deferred_access_check,gc)* checks,
18829 bool member_p,
18830 bool explicit_specialization_p,
18831 bool* friend_p)
18833 int declares_class_or_enum;
18834 tree decl = NULL_TREE;
18835 cp_decl_specifier_seq decl_specifiers;
18836 bool function_definition_p = false;
18837 cp_token *decl_spec_token_start;
18839 /* This function is only used when processing a template
18840 declaration. */
18841 gcc_assert (innermost_scope_kind () == sk_template_parms
18842 || innermost_scope_kind () == sk_template_spec);
18844 /* Defer access checks until we know what is being declared. */
18845 push_deferring_access_checks (dk_deferred);
18847 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
18848 alternative. */
18849 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
18850 cp_parser_decl_specifier_seq (parser,
18851 CP_PARSER_FLAGS_OPTIONAL,
18852 &decl_specifiers,
18853 &declares_class_or_enum);
18854 if (friend_p)
18855 *friend_p = cp_parser_friend_p (&decl_specifiers);
18857 /* There are no template typedefs. */
18858 if (decl_specifiers.specs[(int) ds_typedef])
18860 error_at (decl_spec_token_start->location,
18861 "template declaration of %<typedef%>");
18862 decl = error_mark_node;
18865 /* Gather up the access checks that occurred the
18866 decl-specifier-seq. */
18867 stop_deferring_access_checks ();
18869 /* Check for the declaration of a template class. */
18870 if (declares_class_or_enum)
18872 if (cp_parser_declares_only_class_p (parser))
18874 decl = shadow_tag (&decl_specifiers);
18876 /* In this case:
18878 struct C {
18879 friend template <typename T> struct A<T>::B;
18882 A<T>::B will be represented by a TYPENAME_TYPE, and
18883 therefore not recognized by shadow_tag. */
18884 if (friend_p && *friend_p
18885 && !decl
18886 && decl_specifiers.type
18887 && TYPE_P (decl_specifiers.type))
18888 decl = decl_specifiers.type;
18890 if (decl && decl != error_mark_node)
18891 decl = TYPE_NAME (decl);
18892 else
18893 decl = error_mark_node;
18895 /* Perform access checks for template parameters. */
18896 cp_parser_perform_template_parameter_access_checks (checks);
18900 /* Complain about missing 'typename' or other invalid type names. */
18901 if (!decl_specifiers.any_type_specifiers_p)
18902 cp_parser_parse_and_diagnose_invalid_type_name (parser);
18904 /* If it's not a template class, try for a template function. If
18905 the next token is a `;', then this declaration does not declare
18906 anything. But, if there were errors in the decl-specifiers, then
18907 the error might well have come from an attempted class-specifier.
18908 In that case, there's no need to warn about a missing declarator. */
18909 if (!decl
18910 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
18911 || decl_specifiers.type != error_mark_node))
18913 decl = cp_parser_init_declarator (parser,
18914 &decl_specifiers,
18915 checks,
18916 /*function_definition_allowed_p=*/true,
18917 member_p,
18918 declares_class_or_enum,
18919 &function_definition_p);
18921 /* 7.1.1-1 [dcl.stc]
18923 A storage-class-specifier shall not be specified in an explicit
18924 specialization... */
18925 if (decl
18926 && explicit_specialization_p
18927 && decl_specifiers.storage_class != sc_none)
18929 error_at (decl_spec_token_start->location,
18930 "explicit template specialization cannot have a storage class");
18931 decl = error_mark_node;
18935 pop_deferring_access_checks ();
18937 /* Clear any current qualification; whatever comes next is the start
18938 of something new. */
18939 parser->scope = NULL_TREE;
18940 parser->qualifying_scope = NULL_TREE;
18941 parser->object_scope = NULL_TREE;
18942 /* Look for a trailing `;' after the declaration. */
18943 if (!function_definition_p
18944 && (decl == error_mark_node
18945 || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
18946 cp_parser_skip_to_end_of_block_or_statement (parser);
18948 return decl;
18951 /* Parse a cast-expression that is not the operand of a unary "&". */
18953 static tree
18954 cp_parser_simple_cast_expression (cp_parser *parser)
18956 return cp_parser_cast_expression (parser, /*address_p=*/false,
18957 /*cast_p=*/false, NULL);
18960 /* Parse a functional cast to TYPE. Returns an expression
18961 representing the cast. */
18963 static tree
18964 cp_parser_functional_cast (cp_parser* parser, tree type)
18966 VEC(tree,gc) *vec;
18967 tree expression_list;
18968 tree cast;
18969 bool nonconst_p;
18971 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18973 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
18974 expression_list = cp_parser_braced_list (parser, &nonconst_p);
18975 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
18976 if (TREE_CODE (type) == TYPE_DECL)
18977 type = TREE_TYPE (type);
18978 return finish_compound_literal (type, expression_list);
18982 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
18983 /*cast_p=*/true,
18984 /*allow_expansion_p=*/true,
18985 /*non_constant_p=*/NULL);
18986 if (vec == NULL)
18987 expression_list = error_mark_node;
18988 else
18990 expression_list = build_tree_list_vec (vec);
18991 release_tree_vector (vec);
18994 cast = build_functional_cast (type, expression_list,
18995 tf_warning_or_error);
18996 /* [expr.const]/1: In an integral constant expression "only type
18997 conversions to integral or enumeration type can be used". */
18998 if (TREE_CODE (type) == TYPE_DECL)
18999 type = TREE_TYPE (type);
19000 if (cast != error_mark_node
19001 && !cast_valid_in_integral_constant_expression_p (type)
19002 && (cp_parser_non_integral_constant_expression
19003 (parser, "a call to a constructor")))
19004 return error_mark_node;
19005 return cast;
19008 /* Save the tokens that make up the body of a member function defined
19009 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
19010 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
19011 specifiers applied to the declaration. Returns the FUNCTION_DECL
19012 for the member function. */
19014 static tree
19015 cp_parser_save_member_function_body (cp_parser* parser,
19016 cp_decl_specifier_seq *decl_specifiers,
19017 cp_declarator *declarator,
19018 tree attributes)
19020 cp_token *first;
19021 cp_token *last;
19022 tree fn;
19024 /* Create the FUNCTION_DECL. */
19025 fn = grokmethod (decl_specifiers, declarator, attributes);
19026 /* If something went badly wrong, bail out now. */
19027 if (fn == error_mark_node)
19029 /* If there's a function-body, skip it. */
19030 if (cp_parser_token_starts_function_definition_p
19031 (cp_lexer_peek_token (parser->lexer)))
19032 cp_parser_skip_to_end_of_block_or_statement (parser);
19033 return error_mark_node;
19036 /* Remember it, if there default args to post process. */
19037 cp_parser_save_default_args (parser, fn);
19039 /* Save away the tokens that make up the body of the
19040 function. */
19041 first = parser->lexer->next_token;
19042 /* We can have braced-init-list mem-initializers before the fn body. */
19043 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19045 cp_lexer_consume_token (parser->lexer);
19046 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
19047 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
19049 /* cache_group will stop after an un-nested { } pair, too. */
19050 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
19051 break;
19053 /* variadic mem-inits have ... after the ')'. */
19054 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19055 cp_lexer_consume_token (parser->lexer);
19058 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
19059 /* Handle function try blocks. */
19060 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
19061 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
19062 last = parser->lexer->next_token;
19064 /* Save away the inline definition; we will process it when the
19065 class is complete. */
19066 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
19067 DECL_PENDING_INLINE_P (fn) = 1;
19069 /* We need to know that this was defined in the class, so that
19070 friend templates are handled correctly. */
19071 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
19073 /* Add FN to the queue of functions to be parsed later. */
19074 TREE_VALUE (parser->unparsed_functions_queues)
19075 = tree_cons (NULL_TREE, fn,
19076 TREE_VALUE (parser->unparsed_functions_queues));
19078 return fn;
19081 /* Parse a template-argument-list, as well as the trailing ">" (but
19082 not the opening ">"). See cp_parser_template_argument_list for the
19083 return value. */
19085 static tree
19086 cp_parser_enclosed_template_argument_list (cp_parser* parser)
19088 tree arguments;
19089 tree saved_scope;
19090 tree saved_qualifying_scope;
19091 tree saved_object_scope;
19092 bool saved_greater_than_is_operator_p;
19093 int saved_unevaluated_operand;
19094 int saved_inhibit_evaluation_warnings;
19096 /* [temp.names]
19098 When parsing a template-id, the first non-nested `>' is taken as
19099 the end of the template-argument-list rather than a greater-than
19100 operator. */
19101 saved_greater_than_is_operator_p
19102 = parser->greater_than_is_operator_p;
19103 parser->greater_than_is_operator_p = false;
19104 /* Parsing the argument list may modify SCOPE, so we save it
19105 here. */
19106 saved_scope = parser->scope;
19107 saved_qualifying_scope = parser->qualifying_scope;
19108 saved_object_scope = parser->object_scope;
19109 /* We need to evaluate the template arguments, even though this
19110 template-id may be nested within a "sizeof". */
19111 saved_unevaluated_operand = cp_unevaluated_operand;
19112 cp_unevaluated_operand = 0;
19113 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
19114 c_inhibit_evaluation_warnings = 0;
19115 /* Parse the template-argument-list itself. */
19116 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
19117 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
19118 arguments = NULL_TREE;
19119 else
19120 arguments = cp_parser_template_argument_list (parser);
19121 /* Look for the `>' that ends the template-argument-list. If we find
19122 a '>>' instead, it's probably just a typo. */
19123 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
19125 if (cxx_dialect != cxx98)
19127 /* In C++0x, a `>>' in a template argument list or cast
19128 expression is considered to be two separate `>'
19129 tokens. So, change the current token to a `>', but don't
19130 consume it: it will be consumed later when the outer
19131 template argument list (or cast expression) is parsed.
19132 Note that this replacement of `>' for `>>' is necessary
19133 even if we are parsing tentatively: in the tentative
19134 case, after calling
19135 cp_parser_enclosed_template_argument_list we will always
19136 throw away all of the template arguments and the first
19137 closing `>', either because the template argument list
19138 was erroneous or because we are replacing those tokens
19139 with a CPP_TEMPLATE_ID token. The second `>' (which will
19140 not have been thrown away) is needed either to close an
19141 outer template argument list or to complete a new-style
19142 cast. */
19143 cp_token *token = cp_lexer_peek_token (parser->lexer);
19144 token->type = CPP_GREATER;
19146 else if (!saved_greater_than_is_operator_p)
19148 /* If we're in a nested template argument list, the '>>' has
19149 to be a typo for '> >'. We emit the error message, but we
19150 continue parsing and we push a '>' as next token, so that
19151 the argument list will be parsed correctly. Note that the
19152 global source location is still on the token before the
19153 '>>', so we need to say explicitly where we want it. */
19154 cp_token *token = cp_lexer_peek_token (parser->lexer);
19155 error_at (token->location, "%<>>%> should be %<> >%> "
19156 "within a nested template argument list");
19158 token->type = CPP_GREATER;
19160 else
19162 /* If this is not a nested template argument list, the '>>'
19163 is a typo for '>'. Emit an error message and continue.
19164 Same deal about the token location, but here we can get it
19165 right by consuming the '>>' before issuing the diagnostic. */
19166 cp_token *token = cp_lexer_consume_token (parser->lexer);
19167 error_at (token->location,
19168 "spurious %<>>%>, use %<>%> to terminate "
19169 "a template argument list");
19172 else
19173 cp_parser_skip_to_end_of_template_parameter_list (parser);
19174 /* The `>' token might be a greater-than operator again now. */
19175 parser->greater_than_is_operator_p
19176 = saved_greater_than_is_operator_p;
19177 /* Restore the SAVED_SCOPE. */
19178 parser->scope = saved_scope;
19179 parser->qualifying_scope = saved_qualifying_scope;
19180 parser->object_scope = saved_object_scope;
19181 cp_unevaluated_operand = saved_unevaluated_operand;
19182 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
19184 return arguments;
19187 /* MEMBER_FUNCTION is a member function, or a friend. If default
19188 arguments, or the body of the function have not yet been parsed,
19189 parse them now. */
19191 static void
19192 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
19194 /* If this member is a template, get the underlying
19195 FUNCTION_DECL. */
19196 if (DECL_FUNCTION_TEMPLATE_P (member_function))
19197 member_function = DECL_TEMPLATE_RESULT (member_function);
19199 /* There should not be any class definitions in progress at this
19200 point; the bodies of members are only parsed outside of all class
19201 definitions. */
19202 gcc_assert (parser->num_classes_being_defined == 0);
19203 /* While we're parsing the member functions we might encounter more
19204 classes. We want to handle them right away, but we don't want
19205 them getting mixed up with functions that are currently in the
19206 queue. */
19207 parser->unparsed_functions_queues
19208 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
19210 /* Make sure that any template parameters are in scope. */
19211 maybe_begin_member_template_processing (member_function);
19213 /* If the body of the function has not yet been parsed, parse it
19214 now. */
19215 if (DECL_PENDING_INLINE_P (member_function))
19217 tree function_scope;
19218 cp_token_cache *tokens;
19220 /* The function is no longer pending; we are processing it. */
19221 tokens = DECL_PENDING_INLINE_INFO (member_function);
19222 DECL_PENDING_INLINE_INFO (member_function) = NULL;
19223 DECL_PENDING_INLINE_P (member_function) = 0;
19225 /* If this is a local class, enter the scope of the containing
19226 function. */
19227 function_scope = current_function_decl;
19228 if (function_scope)
19229 push_function_context ();
19231 /* Push the body of the function onto the lexer stack. */
19232 cp_parser_push_lexer_for_tokens (parser, tokens);
19234 /* Let the front end know that we going to be defining this
19235 function. */
19236 start_preparsed_function (member_function, NULL_TREE,
19237 SF_PRE_PARSED | SF_INCLASS_INLINE);
19239 /* Don't do access checking if it is a templated function. */
19240 if (processing_template_decl)
19241 push_deferring_access_checks (dk_no_check);
19243 /* Now, parse the body of the function. */
19244 cp_parser_function_definition_after_declarator (parser,
19245 /*inline_p=*/true);
19247 if (processing_template_decl)
19248 pop_deferring_access_checks ();
19250 /* Leave the scope of the containing function. */
19251 if (function_scope)
19252 pop_function_context ();
19253 cp_parser_pop_lexer (parser);
19256 /* Remove any template parameters from the symbol table. */
19257 maybe_end_member_template_processing ();
19259 /* Restore the queue. */
19260 parser->unparsed_functions_queues
19261 = TREE_CHAIN (parser->unparsed_functions_queues);
19264 /* If DECL contains any default args, remember it on the unparsed
19265 functions queue. */
19267 static void
19268 cp_parser_save_default_args (cp_parser* parser, tree decl)
19270 tree probe;
19272 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
19273 probe;
19274 probe = TREE_CHAIN (probe))
19275 if (TREE_PURPOSE (probe))
19277 TREE_PURPOSE (parser->unparsed_functions_queues)
19278 = tree_cons (current_class_type, decl,
19279 TREE_PURPOSE (parser->unparsed_functions_queues));
19280 break;
19284 /* FN is a FUNCTION_DECL which may contains a parameter with an
19285 unparsed DEFAULT_ARG. Parse the default args now. This function
19286 assumes that the current scope is the scope in which the default
19287 argument should be processed. */
19289 static void
19290 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
19292 bool saved_local_variables_forbidden_p;
19293 tree parm, parmdecl;
19295 /* While we're parsing the default args, we might (due to the
19296 statement expression extension) encounter more classes. We want
19297 to handle them right away, but we don't want them getting mixed
19298 up with default args that are currently in the queue. */
19299 parser->unparsed_functions_queues
19300 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
19302 /* Local variable names (and the `this' keyword) may not appear
19303 in a default argument. */
19304 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
19305 parser->local_variables_forbidden_p = true;
19307 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
19308 parmdecl = DECL_ARGUMENTS (fn);
19309 parm && parm != void_list_node;
19310 parm = TREE_CHAIN (parm),
19311 parmdecl = TREE_CHAIN (parmdecl))
19313 cp_token_cache *tokens;
19314 tree default_arg = TREE_PURPOSE (parm);
19315 tree parsed_arg;
19316 VEC(tree,gc) *insts;
19317 tree copy;
19318 unsigned ix;
19320 if (!default_arg)
19321 continue;
19323 if (TREE_CODE (default_arg) != DEFAULT_ARG)
19324 /* This can happen for a friend declaration for a function
19325 already declared with default arguments. */
19326 continue;
19328 /* Push the saved tokens for the default argument onto the parser's
19329 lexer stack. */
19330 tokens = DEFARG_TOKENS (default_arg);
19331 cp_parser_push_lexer_for_tokens (parser, tokens);
19333 start_lambda_scope (parmdecl);
19335 /* Parse the assignment-expression. */
19336 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
19337 if (parsed_arg == error_mark_node)
19339 cp_parser_pop_lexer (parser);
19340 continue;
19343 if (!processing_template_decl)
19344 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
19346 TREE_PURPOSE (parm) = parsed_arg;
19348 /* Update any instantiations we've already created. */
19349 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
19350 VEC_iterate (tree, insts, ix, copy); ix++)
19351 TREE_PURPOSE (copy) = parsed_arg;
19353 finish_lambda_scope ();
19355 /* If the token stream has not been completely used up, then
19356 there was extra junk after the end of the default
19357 argument. */
19358 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
19359 cp_parser_error (parser, "expected %<,%>");
19361 /* Revert to the main lexer. */
19362 cp_parser_pop_lexer (parser);
19365 /* Make sure no default arg is missing. */
19366 check_default_args (fn);
19368 /* Restore the state of local_variables_forbidden_p. */
19369 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
19371 /* Restore the queue. */
19372 parser->unparsed_functions_queues
19373 = TREE_CHAIN (parser->unparsed_functions_queues);
19376 /* Parse the operand of `sizeof' (or a similar operator). Returns
19377 either a TYPE or an expression, depending on the form of the
19378 input. The KEYWORD indicates which kind of expression we have
19379 encountered. */
19381 static tree
19382 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
19384 tree expr = NULL_TREE;
19385 const char *saved_message;
19386 char *tmp;
19387 bool saved_integral_constant_expression_p;
19388 bool saved_non_integral_constant_expression_p;
19389 bool pack_expansion_p = false;
19391 /* Types cannot be defined in a `sizeof' expression. Save away the
19392 old message. */
19393 saved_message = parser->type_definition_forbidden_message;
19394 /* And create the new one. */
19395 tmp = concat ("types may not be defined in %<",
19396 IDENTIFIER_POINTER (ridpointers[keyword]),
19397 "%> expressions", NULL);
19398 parser->type_definition_forbidden_message = tmp;
19400 /* The restrictions on constant-expressions do not apply inside
19401 sizeof expressions. */
19402 saved_integral_constant_expression_p
19403 = parser->integral_constant_expression_p;
19404 saved_non_integral_constant_expression_p
19405 = parser->non_integral_constant_expression_p;
19406 parser->integral_constant_expression_p = false;
19408 /* If it's a `...', then we are computing the length of a parameter
19409 pack. */
19410 if (keyword == RID_SIZEOF
19411 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19413 /* Consume the `...'. */
19414 cp_lexer_consume_token (parser->lexer);
19415 maybe_warn_variadic_templates ();
19417 /* Note that this is an expansion. */
19418 pack_expansion_p = true;
19421 /* Do not actually evaluate the expression. */
19422 ++cp_unevaluated_operand;
19423 ++c_inhibit_evaluation_warnings;
19424 /* If it's a `(', then we might be looking at the type-id
19425 construction. */
19426 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19428 tree type;
19429 bool saved_in_type_id_in_expr_p;
19431 /* We can't be sure yet whether we're looking at a type-id or an
19432 expression. */
19433 cp_parser_parse_tentatively (parser);
19434 /* Consume the `('. */
19435 cp_lexer_consume_token (parser->lexer);
19436 /* Parse the type-id. */
19437 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
19438 parser->in_type_id_in_expr_p = true;
19439 type = cp_parser_type_id (parser);
19440 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
19441 /* Now, look for the trailing `)'. */
19442 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19443 /* If all went well, then we're done. */
19444 if (cp_parser_parse_definitely (parser))
19446 cp_decl_specifier_seq decl_specs;
19448 /* Build a trivial decl-specifier-seq. */
19449 clear_decl_specs (&decl_specs);
19450 decl_specs.type = type;
19452 /* Call grokdeclarator to figure out what type this is. */
19453 expr = grokdeclarator (NULL,
19454 &decl_specs,
19455 TYPENAME,
19456 /*initialized=*/0,
19457 /*attrlist=*/NULL);
19461 /* If the type-id production did not work out, then we must be
19462 looking at the unary-expression production. */
19463 if (!expr)
19464 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
19465 /*cast_p=*/false, NULL);
19467 if (pack_expansion_p)
19468 /* Build a pack expansion. */
19469 expr = make_pack_expansion (expr);
19471 /* Go back to evaluating expressions. */
19472 --cp_unevaluated_operand;
19473 --c_inhibit_evaluation_warnings;
19475 /* Free the message we created. */
19476 free (tmp);
19477 /* And restore the old one. */
19478 parser->type_definition_forbidden_message = saved_message;
19479 parser->integral_constant_expression_p
19480 = saved_integral_constant_expression_p;
19481 parser->non_integral_constant_expression_p
19482 = saved_non_integral_constant_expression_p;
19484 return expr;
19487 /* If the current declaration has no declarator, return true. */
19489 static bool
19490 cp_parser_declares_only_class_p (cp_parser *parser)
19492 /* If the next token is a `;' or a `,' then there is no
19493 declarator. */
19494 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
19495 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
19498 /* Update the DECL_SPECS to reflect the storage class indicated by
19499 KEYWORD. */
19501 static void
19502 cp_parser_set_storage_class (cp_parser *parser,
19503 cp_decl_specifier_seq *decl_specs,
19504 enum rid keyword,
19505 location_t location)
19507 cp_storage_class storage_class;
19509 if (parser->in_unbraced_linkage_specification_p)
19511 error_at (location, "invalid use of %qD in linkage specification",
19512 ridpointers[keyword]);
19513 return;
19515 else if (decl_specs->storage_class != sc_none)
19517 decl_specs->conflicting_specifiers_p = true;
19518 return;
19521 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
19522 && decl_specs->specs[(int) ds_thread])
19524 error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
19525 decl_specs->specs[(int) ds_thread] = 0;
19528 switch (keyword)
19530 case RID_AUTO:
19531 storage_class = sc_auto;
19532 break;
19533 case RID_REGISTER:
19534 storage_class = sc_register;
19535 break;
19536 case RID_STATIC:
19537 storage_class = sc_static;
19538 break;
19539 case RID_EXTERN:
19540 storage_class = sc_extern;
19541 break;
19542 case RID_MUTABLE:
19543 storage_class = sc_mutable;
19544 break;
19545 default:
19546 gcc_unreachable ();
19548 decl_specs->storage_class = storage_class;
19550 /* A storage class specifier cannot be applied alongside a typedef
19551 specifier. If there is a typedef specifier present then set
19552 conflicting_specifiers_p which will trigger an error later
19553 on in grokdeclarator. */
19554 if (decl_specs->specs[(int)ds_typedef])
19555 decl_specs->conflicting_specifiers_p = true;
19558 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
19559 is true, the type is a user-defined type; otherwise it is a
19560 built-in type specified by a keyword. */
19562 static void
19563 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
19564 tree type_spec,
19565 location_t location,
19566 bool user_defined_p)
19568 decl_specs->any_specifiers_p = true;
19570 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
19571 (with, for example, in "typedef int wchar_t;") we remember that
19572 this is what happened. In system headers, we ignore these
19573 declarations so that G++ can work with system headers that are not
19574 C++-safe. */
19575 if (decl_specs->specs[(int) ds_typedef]
19576 && !user_defined_p
19577 && (type_spec == boolean_type_node
19578 || type_spec == char16_type_node
19579 || type_spec == char32_type_node
19580 || type_spec == wchar_type_node)
19581 && (decl_specs->type
19582 || decl_specs->specs[(int) ds_long]
19583 || decl_specs->specs[(int) ds_short]
19584 || decl_specs->specs[(int) ds_unsigned]
19585 || decl_specs->specs[(int) ds_signed]))
19587 decl_specs->redefined_builtin_type = type_spec;
19588 if (!decl_specs->type)
19590 decl_specs->type = type_spec;
19591 decl_specs->user_defined_type_p = false;
19592 decl_specs->type_location = location;
19595 else if (decl_specs->type)
19596 decl_specs->multiple_types_p = true;
19597 else
19599 decl_specs->type = type_spec;
19600 decl_specs->user_defined_type_p = user_defined_p;
19601 decl_specs->redefined_builtin_type = NULL_TREE;
19602 decl_specs->type_location = location;
19606 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
19607 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
19609 static bool
19610 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
19612 return decl_specifiers->specs[(int) ds_friend] != 0;
19615 /* If the next token is of the indicated TYPE, consume it. Otherwise,
19616 issue an error message indicating that TOKEN_DESC was expected.
19618 Returns the token consumed, if the token had the appropriate type.
19619 Otherwise, returns NULL. */
19621 static cp_token *
19622 cp_parser_require (cp_parser* parser,
19623 enum cpp_ttype type,
19624 const char* token_desc)
19626 if (cp_lexer_next_token_is (parser->lexer, type))
19627 return cp_lexer_consume_token (parser->lexer);
19628 else
19630 /* Output the MESSAGE -- unless we're parsing tentatively. */
19631 if (!cp_parser_simulate_error (parser))
19633 char *message = concat ("expected ", token_desc, NULL);
19634 cp_parser_error (parser, message);
19635 free (message);
19637 return NULL;
19641 /* An error message is produced if the next token is not '>'.
19642 All further tokens are skipped until the desired token is
19643 found or '{', '}', ';' or an unbalanced ')' or ']'. */
19645 static void
19646 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
19648 /* Current level of '< ... >'. */
19649 unsigned level = 0;
19650 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
19651 unsigned nesting_depth = 0;
19653 /* Are we ready, yet? If not, issue error message. */
19654 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
19655 return;
19657 /* Skip tokens until the desired token is found. */
19658 while (true)
19660 /* Peek at the next token. */
19661 switch (cp_lexer_peek_token (parser->lexer)->type)
19663 case CPP_LESS:
19664 if (!nesting_depth)
19665 ++level;
19666 break;
19668 case CPP_RSHIFT:
19669 if (cxx_dialect == cxx98)
19670 /* C++0x views the `>>' operator as two `>' tokens, but
19671 C++98 does not. */
19672 break;
19673 else if (!nesting_depth && level-- == 0)
19675 /* We've hit a `>>' where the first `>' closes the
19676 template argument list, and the second `>' is
19677 spurious. Just consume the `>>' and stop; we've
19678 already produced at least one error. */
19679 cp_lexer_consume_token (parser->lexer);
19680 return;
19682 /* Fall through for C++0x, so we handle the second `>' in
19683 the `>>'. */
19685 case CPP_GREATER:
19686 if (!nesting_depth && level-- == 0)
19688 /* We've reached the token we want, consume it and stop. */
19689 cp_lexer_consume_token (parser->lexer);
19690 return;
19692 break;
19694 case CPP_OPEN_PAREN:
19695 case CPP_OPEN_SQUARE:
19696 ++nesting_depth;
19697 break;
19699 case CPP_CLOSE_PAREN:
19700 case CPP_CLOSE_SQUARE:
19701 if (nesting_depth-- == 0)
19702 return;
19703 break;
19705 case CPP_EOF:
19706 case CPP_PRAGMA_EOL:
19707 case CPP_SEMICOLON:
19708 case CPP_OPEN_BRACE:
19709 case CPP_CLOSE_BRACE:
19710 /* The '>' was probably forgotten, don't look further. */
19711 return;
19713 default:
19714 break;
19717 /* Consume this token. */
19718 cp_lexer_consume_token (parser->lexer);
19722 /* If the next token is the indicated keyword, consume it. Otherwise,
19723 issue an error message indicating that TOKEN_DESC was expected.
19725 Returns the token consumed, if the token had the appropriate type.
19726 Otherwise, returns NULL. */
19728 static cp_token *
19729 cp_parser_require_keyword (cp_parser* parser,
19730 enum rid keyword,
19731 const char* token_desc)
19733 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
19735 if (token && token->keyword != keyword)
19737 dyn_string_t error_msg;
19739 /* Format the error message. */
19740 error_msg = dyn_string_new (0);
19741 dyn_string_append_cstr (error_msg, "expected ");
19742 dyn_string_append_cstr (error_msg, token_desc);
19743 cp_parser_error (parser, error_msg->s);
19744 dyn_string_delete (error_msg);
19745 return NULL;
19748 return token;
19751 /* Returns TRUE iff TOKEN is a token that can begin the body of a
19752 function-definition. */
19754 static bool
19755 cp_parser_token_starts_function_definition_p (cp_token* token)
19757 return (/* An ordinary function-body begins with an `{'. */
19758 token->type == CPP_OPEN_BRACE
19759 /* A ctor-initializer begins with a `:'. */
19760 || token->type == CPP_COLON
19761 /* A function-try-block begins with `try'. */
19762 || token->keyword == RID_TRY
19763 /* The named return value extension begins with `return'. */
19764 || token->keyword == RID_RETURN);
19767 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
19768 definition. */
19770 static bool
19771 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
19773 cp_token *token;
19775 token = cp_lexer_peek_token (parser->lexer);
19776 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
19779 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
19780 C++0x) ending a template-argument. */
19782 static bool
19783 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
19785 cp_token *token;
19787 token = cp_lexer_peek_token (parser->lexer);
19788 return (token->type == CPP_COMMA
19789 || token->type == CPP_GREATER
19790 || token->type == CPP_ELLIPSIS
19791 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
19794 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
19795 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
19797 static bool
19798 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
19799 size_t n)
19801 cp_token *token;
19803 token = cp_lexer_peek_nth_token (parser->lexer, n);
19804 if (token->type == CPP_LESS)
19805 return true;
19806 /* Check for the sequence `<::' in the original code. It would be lexed as
19807 `[:', where `[' is a digraph, and there is no whitespace before
19808 `:'. */
19809 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
19811 cp_token *token2;
19812 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
19813 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
19814 return true;
19816 return false;
19819 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
19820 or none_type otherwise. */
19822 static enum tag_types
19823 cp_parser_token_is_class_key (cp_token* token)
19825 switch (token->keyword)
19827 case RID_CLASS:
19828 return class_type;
19829 case RID_STRUCT:
19830 return record_type;
19831 case RID_UNION:
19832 return union_type;
19834 default:
19835 return none_type;
19839 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
19841 static void
19842 cp_parser_check_class_key (enum tag_types class_key, tree type)
19844 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
19845 permerror (input_location, "%qs tag used in naming %q#T",
19846 class_key == union_type ? "union"
19847 : class_key == record_type ? "struct" : "class",
19848 type);
19851 /* Issue an error message if DECL is redeclared with different
19852 access than its original declaration [class.access.spec/3].
19853 This applies to nested classes and nested class templates.
19854 [class.mem/1]. */
19856 static void
19857 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
19859 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
19860 return;
19862 if ((TREE_PRIVATE (decl)
19863 != (current_access_specifier == access_private_node))
19864 || (TREE_PROTECTED (decl)
19865 != (current_access_specifier == access_protected_node)))
19866 error_at (location, "%qD redeclared with different access", decl);
19869 /* Look for the `template' keyword, as a syntactic disambiguator.
19870 Return TRUE iff it is present, in which case it will be
19871 consumed. */
19873 static bool
19874 cp_parser_optional_template_keyword (cp_parser *parser)
19876 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
19878 /* The `template' keyword can only be used within templates;
19879 outside templates the parser can always figure out what is a
19880 template and what is not. */
19881 if (!processing_template_decl)
19883 cp_token *token = cp_lexer_peek_token (parser->lexer);
19884 error_at (token->location,
19885 "%<template%> (as a disambiguator) is only allowed "
19886 "within templates");
19887 /* If this part of the token stream is rescanned, the same
19888 error message would be generated. So, we purge the token
19889 from the stream. */
19890 cp_lexer_purge_token (parser->lexer);
19891 return false;
19893 else
19895 /* Consume the `template' keyword. */
19896 cp_lexer_consume_token (parser->lexer);
19897 return true;
19901 return false;
19904 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
19905 set PARSER->SCOPE, and perform other related actions. */
19907 static void
19908 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
19910 int i;
19911 struct tree_check *check_value;
19912 deferred_access_check *chk;
19913 VEC (deferred_access_check,gc) *checks;
19915 /* Get the stored value. */
19916 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
19917 /* Perform any access checks that were deferred. */
19918 checks = check_value->checks;
19919 if (checks)
19921 for (i = 0 ;
19922 VEC_iterate (deferred_access_check, checks, i, chk) ;
19923 ++i)
19925 perform_or_defer_access_check (chk->binfo,
19926 chk->decl,
19927 chk->diag_decl);
19930 /* Set the scope from the stored value. */
19931 parser->scope = check_value->value;
19932 parser->qualifying_scope = check_value->qualifying_scope;
19933 parser->object_scope = NULL_TREE;
19936 /* Consume tokens up through a non-nested END token. Returns TRUE if we
19937 encounter the end of a block before what we were looking for. */
19939 static bool
19940 cp_parser_cache_group (cp_parser *parser,
19941 enum cpp_ttype end,
19942 unsigned depth)
19944 while (true)
19946 cp_token *token = cp_lexer_peek_token (parser->lexer);
19948 /* Abort a parenthesized expression if we encounter a semicolon. */
19949 if ((end == CPP_CLOSE_PAREN || depth == 0)
19950 && token->type == CPP_SEMICOLON)
19951 return true;
19952 /* If we've reached the end of the file, stop. */
19953 if (token->type == CPP_EOF
19954 || (end != CPP_PRAGMA_EOL
19955 && token->type == CPP_PRAGMA_EOL))
19956 return true;
19957 if (token->type == CPP_CLOSE_BRACE && depth == 0)
19958 /* We've hit the end of an enclosing block, so there's been some
19959 kind of syntax error. */
19960 return true;
19962 /* Consume the token. */
19963 cp_lexer_consume_token (parser->lexer);
19964 /* See if it starts a new group. */
19965 if (token->type == CPP_OPEN_BRACE)
19967 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
19968 /* In theory this should probably check end == '}', but
19969 cp_parser_save_member_function_body needs it to exit
19970 after either '}' or ')' when called with ')'. */
19971 if (depth == 0)
19972 return false;
19974 else if (token->type == CPP_OPEN_PAREN)
19976 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
19977 if (depth == 0 && end == CPP_CLOSE_PAREN)
19978 return false;
19980 else if (token->type == CPP_PRAGMA)
19981 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
19982 else if (token->type == end)
19983 return false;
19987 /* Begin parsing tentatively. We always save tokens while parsing
19988 tentatively so that if the tentative parsing fails we can restore the
19989 tokens. */
19991 static void
19992 cp_parser_parse_tentatively (cp_parser* parser)
19994 /* Enter a new parsing context. */
19995 parser->context = cp_parser_context_new (parser->context);
19996 /* Begin saving tokens. */
19997 cp_lexer_save_tokens (parser->lexer);
19998 /* In order to avoid repetitive access control error messages,
19999 access checks are queued up until we are no longer parsing
20000 tentatively. */
20001 push_deferring_access_checks (dk_deferred);
20004 /* Commit to the currently active tentative parse. */
20006 static void
20007 cp_parser_commit_to_tentative_parse (cp_parser* parser)
20009 cp_parser_context *context;
20010 cp_lexer *lexer;
20012 /* Mark all of the levels as committed. */
20013 lexer = parser->lexer;
20014 for (context = parser->context; context->next; context = context->next)
20016 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
20017 break;
20018 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
20019 while (!cp_lexer_saving_tokens (lexer))
20020 lexer = lexer->next;
20021 cp_lexer_commit_tokens (lexer);
20025 /* Abort the currently active tentative parse. All consumed tokens
20026 will be rolled back, and no diagnostics will be issued. */
20028 static void
20029 cp_parser_abort_tentative_parse (cp_parser* parser)
20031 cp_parser_simulate_error (parser);
20032 /* Now, pretend that we want to see if the construct was
20033 successfully parsed. */
20034 cp_parser_parse_definitely (parser);
20037 /* Stop parsing tentatively. If a parse error has occurred, restore the
20038 token stream. Otherwise, commit to the tokens we have consumed.
20039 Returns true if no error occurred; false otherwise. */
20041 static bool
20042 cp_parser_parse_definitely (cp_parser* parser)
20044 bool error_occurred;
20045 cp_parser_context *context;
20047 /* Remember whether or not an error occurred, since we are about to
20048 destroy that information. */
20049 error_occurred = cp_parser_error_occurred (parser);
20050 /* Remove the topmost context from the stack. */
20051 context = parser->context;
20052 parser->context = context->next;
20053 /* If no parse errors occurred, commit to the tentative parse. */
20054 if (!error_occurred)
20056 /* Commit to the tokens read tentatively, unless that was
20057 already done. */
20058 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
20059 cp_lexer_commit_tokens (parser->lexer);
20061 pop_to_parent_deferring_access_checks ();
20063 /* Otherwise, if errors occurred, roll back our state so that things
20064 are just as they were before we began the tentative parse. */
20065 else
20067 cp_lexer_rollback_tokens (parser->lexer);
20068 pop_deferring_access_checks ();
20070 /* Add the context to the front of the free list. */
20071 context->next = cp_parser_context_free_list;
20072 cp_parser_context_free_list = context;
20074 return !error_occurred;
20077 /* Returns true if we are parsing tentatively and are not committed to
20078 this tentative parse. */
20080 static bool
20081 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
20083 return (cp_parser_parsing_tentatively (parser)
20084 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
20087 /* Returns nonzero iff an error has occurred during the most recent
20088 tentative parse. */
20090 static bool
20091 cp_parser_error_occurred (cp_parser* parser)
20093 return (cp_parser_parsing_tentatively (parser)
20094 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
20097 /* Returns nonzero if GNU extensions are allowed. */
20099 static bool
20100 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
20102 return parser->allow_gnu_extensions_p;
20105 /* Objective-C++ Productions */
20108 /* Parse an Objective-C expression, which feeds into a primary-expression
20109 above.
20111 objc-expression:
20112 objc-message-expression
20113 objc-string-literal
20114 objc-encode-expression
20115 objc-protocol-expression
20116 objc-selector-expression
20118 Returns a tree representation of the expression. */
20120 static tree
20121 cp_parser_objc_expression (cp_parser* parser)
20123 /* Try to figure out what kind of declaration is present. */
20124 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20126 switch (kwd->type)
20128 case CPP_OPEN_SQUARE:
20129 return cp_parser_objc_message_expression (parser);
20131 case CPP_OBJC_STRING:
20132 kwd = cp_lexer_consume_token (parser->lexer);
20133 return objc_build_string_object (kwd->u.value);
20135 case CPP_KEYWORD:
20136 switch (kwd->keyword)
20138 case RID_AT_ENCODE:
20139 return cp_parser_objc_encode_expression (parser);
20141 case RID_AT_PROTOCOL:
20142 return cp_parser_objc_protocol_expression (parser);
20144 case RID_AT_SELECTOR:
20145 return cp_parser_objc_selector_expression (parser);
20147 default:
20148 break;
20150 default:
20151 error_at (kwd->location,
20152 "misplaced %<@%D%> Objective-C++ construct",
20153 kwd->u.value);
20154 cp_parser_skip_to_end_of_block_or_statement (parser);
20157 return error_mark_node;
20160 /* Parse an Objective-C message expression.
20162 objc-message-expression:
20163 [ objc-message-receiver objc-message-args ]
20165 Returns a representation of an Objective-C message. */
20167 static tree
20168 cp_parser_objc_message_expression (cp_parser* parser)
20170 tree receiver, messageargs;
20172 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
20173 receiver = cp_parser_objc_message_receiver (parser);
20174 messageargs = cp_parser_objc_message_args (parser);
20175 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
20177 return objc_build_message_expr (build_tree_list (receiver, messageargs));
20180 /* Parse an objc-message-receiver.
20182 objc-message-receiver:
20183 expression
20184 simple-type-specifier
20186 Returns a representation of the type or expression. */
20188 static tree
20189 cp_parser_objc_message_receiver (cp_parser* parser)
20191 tree rcv;
20193 /* An Objective-C message receiver may be either (1) a type
20194 or (2) an expression. */
20195 cp_parser_parse_tentatively (parser);
20196 rcv = cp_parser_expression (parser, false, NULL);
20198 if (cp_parser_parse_definitely (parser))
20199 return rcv;
20201 rcv = cp_parser_simple_type_specifier (parser,
20202 /*decl_specs=*/NULL,
20203 CP_PARSER_FLAGS_NONE);
20205 return objc_get_class_reference (rcv);
20208 /* Parse the arguments and selectors comprising an Objective-C message.
20210 objc-message-args:
20211 objc-selector
20212 objc-selector-args
20213 objc-selector-args , objc-comma-args
20215 objc-selector-args:
20216 objc-selector [opt] : assignment-expression
20217 objc-selector-args objc-selector [opt] : assignment-expression
20219 objc-comma-args:
20220 assignment-expression
20221 objc-comma-args , assignment-expression
20223 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
20224 selector arguments and TREE_VALUE containing a list of comma
20225 arguments. */
20227 static tree
20228 cp_parser_objc_message_args (cp_parser* parser)
20230 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
20231 bool maybe_unary_selector_p = true;
20232 cp_token *token = cp_lexer_peek_token (parser->lexer);
20234 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
20236 tree selector = NULL_TREE, arg;
20238 if (token->type != CPP_COLON)
20239 selector = cp_parser_objc_selector (parser);
20241 /* Detect if we have a unary selector. */
20242 if (maybe_unary_selector_p
20243 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
20244 return build_tree_list (selector, NULL_TREE);
20246 maybe_unary_selector_p = false;
20247 cp_parser_require (parser, CPP_COLON, "%<:%>");
20248 arg = cp_parser_assignment_expression (parser, false, NULL);
20250 sel_args
20251 = chainon (sel_args,
20252 build_tree_list (selector, arg));
20254 token = cp_lexer_peek_token (parser->lexer);
20257 /* Handle non-selector arguments, if any. */
20258 while (token->type == CPP_COMMA)
20260 tree arg;
20262 cp_lexer_consume_token (parser->lexer);
20263 arg = cp_parser_assignment_expression (parser, false, NULL);
20265 addl_args
20266 = chainon (addl_args,
20267 build_tree_list (NULL_TREE, arg));
20269 token = cp_lexer_peek_token (parser->lexer);
20272 return build_tree_list (sel_args, addl_args);
20275 /* Parse an Objective-C encode expression.
20277 objc-encode-expression:
20278 @encode objc-typename
20280 Returns an encoded representation of the type argument. */
20282 static tree
20283 cp_parser_objc_encode_expression (cp_parser* parser)
20285 tree type;
20286 cp_token *token;
20288 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
20289 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20290 token = cp_lexer_peek_token (parser->lexer);
20291 type = complete_type (cp_parser_type_id (parser));
20292 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20294 if (!type)
20296 error_at (token->location,
20297 "%<@encode%> must specify a type as an argument");
20298 return error_mark_node;
20301 return objc_build_encode_expr (type);
20304 /* Parse an Objective-C @defs expression. */
20306 static tree
20307 cp_parser_objc_defs_expression (cp_parser *parser)
20309 tree name;
20311 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
20312 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20313 name = cp_parser_identifier (parser);
20314 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20316 return objc_get_class_ivars (name);
20319 /* Parse an Objective-C protocol expression.
20321 objc-protocol-expression:
20322 @protocol ( identifier )
20324 Returns a representation of the protocol expression. */
20326 static tree
20327 cp_parser_objc_protocol_expression (cp_parser* parser)
20329 tree proto;
20331 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
20332 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20333 proto = cp_parser_identifier (parser);
20334 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20336 return objc_build_protocol_expr (proto);
20339 /* Parse an Objective-C selector expression.
20341 objc-selector-expression:
20342 @selector ( objc-method-signature )
20344 objc-method-signature:
20345 objc-selector
20346 objc-selector-seq
20348 objc-selector-seq:
20349 objc-selector :
20350 objc-selector-seq objc-selector :
20352 Returns a representation of the method selector. */
20354 static tree
20355 cp_parser_objc_selector_expression (cp_parser* parser)
20357 tree sel_seq = NULL_TREE;
20358 bool maybe_unary_selector_p = true;
20359 cp_token *token;
20360 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
20362 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
20363 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20364 token = cp_lexer_peek_token (parser->lexer);
20366 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
20367 || token->type == CPP_SCOPE)
20369 tree selector = NULL_TREE;
20371 if (token->type != CPP_COLON
20372 || token->type == CPP_SCOPE)
20373 selector = cp_parser_objc_selector (parser);
20375 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
20376 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
20378 /* Detect if we have a unary selector. */
20379 if (maybe_unary_selector_p)
20381 sel_seq = selector;
20382 goto finish_selector;
20384 else
20386 cp_parser_error (parser, "expected %<:%>");
20389 maybe_unary_selector_p = false;
20390 token = cp_lexer_consume_token (parser->lexer);
20392 if (token->type == CPP_SCOPE)
20394 sel_seq
20395 = chainon (sel_seq,
20396 build_tree_list (selector, NULL_TREE));
20397 sel_seq
20398 = chainon (sel_seq,
20399 build_tree_list (NULL_TREE, NULL_TREE));
20401 else
20402 sel_seq
20403 = chainon (sel_seq,
20404 build_tree_list (selector, NULL_TREE));
20406 token = cp_lexer_peek_token (parser->lexer);
20409 finish_selector:
20410 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20412 return objc_build_selector_expr (loc, sel_seq);
20415 /* Parse a list of identifiers.
20417 objc-identifier-list:
20418 identifier
20419 objc-identifier-list , identifier
20421 Returns a TREE_LIST of identifier nodes. */
20423 static tree
20424 cp_parser_objc_identifier_list (cp_parser* parser)
20426 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
20427 cp_token *sep = cp_lexer_peek_token (parser->lexer);
20429 while (sep->type == CPP_COMMA)
20431 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
20432 list = chainon (list,
20433 build_tree_list (NULL_TREE,
20434 cp_parser_identifier (parser)));
20435 sep = cp_lexer_peek_token (parser->lexer);
20438 return list;
20441 /* Parse an Objective-C alias declaration.
20443 objc-alias-declaration:
20444 @compatibility_alias identifier identifier ;
20446 This function registers the alias mapping with the Objective-C front end.
20447 It returns nothing. */
20449 static void
20450 cp_parser_objc_alias_declaration (cp_parser* parser)
20452 tree alias, orig;
20454 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
20455 alias = cp_parser_identifier (parser);
20456 orig = cp_parser_identifier (parser);
20457 objc_declare_alias (alias, orig);
20458 cp_parser_consume_semicolon_at_end_of_statement (parser);
20461 /* Parse an Objective-C class forward-declaration.
20463 objc-class-declaration:
20464 @class objc-identifier-list ;
20466 The function registers the forward declarations with the Objective-C
20467 front end. It returns nothing. */
20469 static void
20470 cp_parser_objc_class_declaration (cp_parser* parser)
20472 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
20473 objc_declare_class (cp_parser_objc_identifier_list (parser));
20474 cp_parser_consume_semicolon_at_end_of_statement (parser);
20477 /* Parse a list of Objective-C protocol references.
20479 objc-protocol-refs-opt:
20480 objc-protocol-refs [opt]
20482 objc-protocol-refs:
20483 < objc-identifier-list >
20485 Returns a TREE_LIST of identifiers, if any. */
20487 static tree
20488 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
20490 tree protorefs = NULL_TREE;
20492 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
20494 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
20495 protorefs = cp_parser_objc_identifier_list (parser);
20496 cp_parser_require (parser, CPP_GREATER, "%<>%>");
20499 return protorefs;
20502 /* Parse a Objective-C visibility specification. */
20504 static void
20505 cp_parser_objc_visibility_spec (cp_parser* parser)
20507 cp_token *vis = cp_lexer_peek_token (parser->lexer);
20509 switch (vis->keyword)
20511 case RID_AT_PRIVATE:
20512 objc_set_visibility (2);
20513 break;
20514 case RID_AT_PROTECTED:
20515 objc_set_visibility (0);
20516 break;
20517 case RID_AT_PUBLIC:
20518 objc_set_visibility (1);
20519 break;
20520 default:
20521 return;
20524 /* Eat '@private'/'@protected'/'@public'. */
20525 cp_lexer_consume_token (parser->lexer);
20528 /* Parse an Objective-C method type. */
20530 static void
20531 cp_parser_objc_method_type (cp_parser* parser)
20533 objc_set_method_type
20534 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
20535 ? PLUS_EXPR
20536 : MINUS_EXPR);
20539 /* Parse an Objective-C protocol qualifier. */
20541 static tree
20542 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
20544 tree quals = NULL_TREE, node;
20545 cp_token *token = cp_lexer_peek_token (parser->lexer);
20547 node = token->u.value;
20549 while (node && TREE_CODE (node) == IDENTIFIER_NODE
20550 && (node == ridpointers [(int) RID_IN]
20551 || node == ridpointers [(int) RID_OUT]
20552 || node == ridpointers [(int) RID_INOUT]
20553 || node == ridpointers [(int) RID_BYCOPY]
20554 || node == ridpointers [(int) RID_BYREF]
20555 || node == ridpointers [(int) RID_ONEWAY]))
20557 quals = tree_cons (NULL_TREE, node, quals);
20558 cp_lexer_consume_token (parser->lexer);
20559 token = cp_lexer_peek_token (parser->lexer);
20560 node = token->u.value;
20563 return quals;
20566 /* Parse an Objective-C typename. */
20568 static tree
20569 cp_parser_objc_typename (cp_parser* parser)
20571 tree type_name = NULL_TREE;
20573 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20575 tree proto_quals, cp_type = NULL_TREE;
20577 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
20578 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
20580 /* An ObjC type name may consist of just protocol qualifiers, in which
20581 case the type shall default to 'id'. */
20582 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
20583 cp_type = cp_parser_type_id (parser);
20585 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20586 type_name = build_tree_list (proto_quals, cp_type);
20589 return type_name;
20592 /* Check to see if TYPE refers to an Objective-C selector name. */
20594 static bool
20595 cp_parser_objc_selector_p (enum cpp_ttype type)
20597 return (type == CPP_NAME || type == CPP_KEYWORD
20598 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
20599 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
20600 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
20601 || type == CPP_XOR || type == CPP_XOR_EQ);
20604 /* Parse an Objective-C selector. */
20606 static tree
20607 cp_parser_objc_selector (cp_parser* parser)
20609 cp_token *token = cp_lexer_consume_token (parser->lexer);
20611 if (!cp_parser_objc_selector_p (token->type))
20613 error_at (token->location, "invalid Objective-C++ selector name");
20614 return error_mark_node;
20617 /* C++ operator names are allowed to appear in ObjC selectors. */
20618 switch (token->type)
20620 case CPP_AND_AND: return get_identifier ("and");
20621 case CPP_AND_EQ: return get_identifier ("and_eq");
20622 case CPP_AND: return get_identifier ("bitand");
20623 case CPP_OR: return get_identifier ("bitor");
20624 case CPP_COMPL: return get_identifier ("compl");
20625 case CPP_NOT: return get_identifier ("not");
20626 case CPP_NOT_EQ: return get_identifier ("not_eq");
20627 case CPP_OR_OR: return get_identifier ("or");
20628 case CPP_OR_EQ: return get_identifier ("or_eq");
20629 case CPP_XOR: return get_identifier ("xor");
20630 case CPP_XOR_EQ: return get_identifier ("xor_eq");
20631 default: return token->u.value;
20635 /* Parse an Objective-C params list. */
20637 static tree
20638 cp_parser_objc_method_keyword_params (cp_parser* parser)
20640 tree params = NULL_TREE;
20641 bool maybe_unary_selector_p = true;
20642 cp_token *token = cp_lexer_peek_token (parser->lexer);
20644 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
20646 tree selector = NULL_TREE, type_name, identifier;
20648 if (token->type != CPP_COLON)
20649 selector = cp_parser_objc_selector (parser);
20651 /* Detect if we have a unary selector. */
20652 if (maybe_unary_selector_p
20653 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
20654 return selector;
20656 maybe_unary_selector_p = false;
20657 cp_parser_require (parser, CPP_COLON, "%<:%>");
20658 type_name = cp_parser_objc_typename (parser);
20659 identifier = cp_parser_identifier (parser);
20661 params
20662 = chainon (params,
20663 objc_build_keyword_decl (selector,
20664 type_name,
20665 identifier));
20667 token = cp_lexer_peek_token (parser->lexer);
20670 return params;
20673 /* Parse the non-keyword Objective-C params. */
20675 static tree
20676 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
20678 tree params = make_node (TREE_LIST);
20679 cp_token *token = cp_lexer_peek_token (parser->lexer);
20680 *ellipsisp = false; /* Initially, assume no ellipsis. */
20682 while (token->type == CPP_COMMA)
20684 cp_parameter_declarator *parmdecl;
20685 tree parm;
20687 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
20688 token = cp_lexer_peek_token (parser->lexer);
20690 if (token->type == CPP_ELLIPSIS)
20692 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
20693 *ellipsisp = true;
20694 break;
20697 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20698 parm = grokdeclarator (parmdecl->declarator,
20699 &parmdecl->decl_specifiers,
20700 PARM, /*initialized=*/0,
20701 /*attrlist=*/NULL);
20703 chainon (params, build_tree_list (NULL_TREE, parm));
20704 token = cp_lexer_peek_token (parser->lexer);
20707 return params;
20710 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
20712 static void
20713 cp_parser_objc_interstitial_code (cp_parser* parser)
20715 cp_token *token = cp_lexer_peek_token (parser->lexer);
20717 /* If the next token is `extern' and the following token is a string
20718 literal, then we have a linkage specification. */
20719 if (token->keyword == RID_EXTERN
20720 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
20721 cp_parser_linkage_specification (parser);
20722 /* Handle #pragma, if any. */
20723 else if (token->type == CPP_PRAGMA)
20724 cp_parser_pragma (parser, pragma_external);
20725 /* Allow stray semicolons. */
20726 else if (token->type == CPP_SEMICOLON)
20727 cp_lexer_consume_token (parser->lexer);
20728 /* Finally, try to parse a block-declaration, or a function-definition. */
20729 else
20730 cp_parser_block_declaration (parser, /*statement_p=*/false);
20733 /* Parse a method signature. */
20735 static tree
20736 cp_parser_objc_method_signature (cp_parser* parser)
20738 tree rettype, kwdparms, optparms;
20739 bool ellipsis = false;
20741 cp_parser_objc_method_type (parser);
20742 rettype = cp_parser_objc_typename (parser);
20743 kwdparms = cp_parser_objc_method_keyword_params (parser);
20744 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
20746 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
20749 /* Pars an Objective-C method prototype list. */
20751 static void
20752 cp_parser_objc_method_prototype_list (cp_parser* parser)
20754 cp_token *token = cp_lexer_peek_token (parser->lexer);
20756 while (token->keyword != RID_AT_END)
20758 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
20760 objc_add_method_declaration
20761 (cp_parser_objc_method_signature (parser));
20762 cp_parser_consume_semicolon_at_end_of_statement (parser);
20764 else
20765 /* Allow for interspersed non-ObjC++ code. */
20766 cp_parser_objc_interstitial_code (parser);
20768 token = cp_lexer_peek_token (parser->lexer);
20771 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
20772 objc_finish_interface ();
20775 /* Parse an Objective-C method definition list. */
20777 static void
20778 cp_parser_objc_method_definition_list (cp_parser* parser)
20780 cp_token *token = cp_lexer_peek_token (parser->lexer);
20782 while (token->keyword != RID_AT_END)
20784 tree meth;
20786 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
20788 push_deferring_access_checks (dk_deferred);
20789 objc_start_method_definition
20790 (cp_parser_objc_method_signature (parser));
20792 /* For historical reasons, we accept an optional semicolon. */
20793 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20794 cp_lexer_consume_token (parser->lexer);
20796 perform_deferred_access_checks ();
20797 stop_deferring_access_checks ();
20798 meth = cp_parser_function_definition_after_declarator (parser,
20799 false);
20800 pop_deferring_access_checks ();
20801 objc_finish_method_definition (meth);
20803 else
20804 /* Allow for interspersed non-ObjC++ code. */
20805 cp_parser_objc_interstitial_code (parser);
20807 token = cp_lexer_peek_token (parser->lexer);
20810 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
20811 objc_finish_implementation ();
20814 /* Parse Objective-C ivars. */
20816 static void
20817 cp_parser_objc_class_ivars (cp_parser* parser)
20819 cp_token *token = cp_lexer_peek_token (parser->lexer);
20821 if (token->type != CPP_OPEN_BRACE)
20822 return; /* No ivars specified. */
20824 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
20825 token = cp_lexer_peek_token (parser->lexer);
20827 while (token->type != CPP_CLOSE_BRACE)
20829 cp_decl_specifier_seq declspecs;
20830 int decl_class_or_enum_p;
20831 tree prefix_attributes;
20833 cp_parser_objc_visibility_spec (parser);
20835 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
20836 break;
20838 cp_parser_decl_specifier_seq (parser,
20839 CP_PARSER_FLAGS_OPTIONAL,
20840 &declspecs,
20841 &decl_class_or_enum_p);
20842 prefix_attributes = declspecs.attributes;
20843 declspecs.attributes = NULL_TREE;
20845 /* Keep going until we hit the `;' at the end of the
20846 declaration. */
20847 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20849 tree width = NULL_TREE, attributes, first_attribute, decl;
20850 cp_declarator *declarator = NULL;
20851 int ctor_dtor_or_conv_p;
20853 /* Check for a (possibly unnamed) bitfield declaration. */
20854 token = cp_lexer_peek_token (parser->lexer);
20855 if (token->type == CPP_COLON)
20856 goto eat_colon;
20858 if (token->type == CPP_NAME
20859 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
20860 == CPP_COLON))
20862 /* Get the name of the bitfield. */
20863 declarator = make_id_declarator (NULL_TREE,
20864 cp_parser_identifier (parser),
20865 sfk_none);
20867 eat_colon:
20868 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
20869 /* Get the width of the bitfield. */
20870 width
20871 = cp_parser_constant_expression (parser,
20872 /*allow_non_constant=*/false,
20873 NULL);
20875 else
20877 /* Parse the declarator. */
20878 declarator
20879 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20880 &ctor_dtor_or_conv_p,
20881 /*parenthesized_p=*/NULL,
20882 /*member_p=*/false);
20885 /* Look for attributes that apply to the ivar. */
20886 attributes = cp_parser_attributes_opt (parser);
20887 /* Remember which attributes are prefix attributes and
20888 which are not. */
20889 first_attribute = attributes;
20890 /* Combine the attributes. */
20891 attributes = chainon (prefix_attributes, attributes);
20893 if (width)
20894 /* Create the bitfield declaration. */
20895 decl = grokbitfield (declarator, &declspecs,
20896 width,
20897 attributes);
20898 else
20899 decl = grokfield (declarator, &declspecs,
20900 NULL_TREE, /*init_const_expr_p=*/false,
20901 NULL_TREE, attributes);
20903 /* Add the instance variable. */
20904 objc_add_instance_variable (decl);
20906 /* Reset PREFIX_ATTRIBUTES. */
20907 while (attributes && TREE_CHAIN (attributes) != first_attribute)
20908 attributes = TREE_CHAIN (attributes);
20909 if (attributes)
20910 TREE_CHAIN (attributes) = NULL_TREE;
20912 token = cp_lexer_peek_token (parser->lexer);
20914 if (token->type == CPP_COMMA)
20916 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
20917 continue;
20919 break;
20922 cp_parser_consume_semicolon_at_end_of_statement (parser);
20923 token = cp_lexer_peek_token (parser->lexer);
20926 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
20927 /* For historical reasons, we accept an optional semicolon. */
20928 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20929 cp_lexer_consume_token (parser->lexer);
20932 /* Parse an Objective-C protocol declaration. */
20934 static void
20935 cp_parser_objc_protocol_declaration (cp_parser* parser)
20937 tree proto, protorefs;
20938 cp_token *tok;
20940 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
20941 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
20943 tok = cp_lexer_peek_token (parser->lexer);
20944 error_at (tok->location, "identifier expected after %<@protocol%>");
20945 goto finish;
20948 /* See if we have a forward declaration or a definition. */
20949 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
20951 /* Try a forward declaration first. */
20952 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
20954 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
20955 finish:
20956 cp_parser_consume_semicolon_at_end_of_statement (parser);
20959 /* Ok, we got a full-fledged definition (or at least should). */
20960 else
20962 proto = cp_parser_identifier (parser);
20963 protorefs = cp_parser_objc_protocol_refs_opt (parser);
20964 objc_start_protocol (proto, protorefs);
20965 cp_parser_objc_method_prototype_list (parser);
20969 /* Parse an Objective-C superclass or category. */
20971 static void
20972 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
20973 tree *categ)
20975 cp_token *next = cp_lexer_peek_token (parser->lexer);
20977 *super = *categ = NULL_TREE;
20978 if (next->type == CPP_COLON)
20980 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
20981 *super = cp_parser_identifier (parser);
20983 else if (next->type == CPP_OPEN_PAREN)
20985 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
20986 *categ = cp_parser_identifier (parser);
20987 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20991 /* Parse an Objective-C class interface. */
20993 static void
20994 cp_parser_objc_class_interface (cp_parser* parser)
20996 tree name, super, categ, protos;
20998 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
20999 name = cp_parser_identifier (parser);
21000 cp_parser_objc_superclass_or_category (parser, &super, &categ);
21001 protos = cp_parser_objc_protocol_refs_opt (parser);
21003 /* We have either a class or a category on our hands. */
21004 if (categ)
21005 objc_start_category_interface (name, categ, protos);
21006 else
21008 objc_start_class_interface (name, super, protos);
21009 /* Handle instance variable declarations, if any. */
21010 cp_parser_objc_class_ivars (parser);
21011 objc_continue_interface ();
21014 cp_parser_objc_method_prototype_list (parser);
21017 /* Parse an Objective-C class implementation. */
21019 static void
21020 cp_parser_objc_class_implementation (cp_parser* parser)
21022 tree name, super, categ;
21024 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
21025 name = cp_parser_identifier (parser);
21026 cp_parser_objc_superclass_or_category (parser, &super, &categ);
21028 /* We have either a class or a category on our hands. */
21029 if (categ)
21030 objc_start_category_implementation (name, categ);
21031 else
21033 objc_start_class_implementation (name, super);
21034 /* Handle instance variable declarations, if any. */
21035 cp_parser_objc_class_ivars (parser);
21036 objc_continue_implementation ();
21039 cp_parser_objc_method_definition_list (parser);
21042 /* Consume the @end token and finish off the implementation. */
21044 static void
21045 cp_parser_objc_end_implementation (cp_parser* parser)
21047 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
21048 objc_finish_implementation ();
21051 /* Parse an Objective-C declaration. */
21053 static void
21054 cp_parser_objc_declaration (cp_parser* parser)
21056 /* Try to figure out what kind of declaration is present. */
21057 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21059 switch (kwd->keyword)
21061 case RID_AT_ALIAS:
21062 cp_parser_objc_alias_declaration (parser);
21063 break;
21064 case RID_AT_CLASS:
21065 cp_parser_objc_class_declaration (parser);
21066 break;
21067 case RID_AT_PROTOCOL:
21068 cp_parser_objc_protocol_declaration (parser);
21069 break;
21070 case RID_AT_INTERFACE:
21071 cp_parser_objc_class_interface (parser);
21072 break;
21073 case RID_AT_IMPLEMENTATION:
21074 cp_parser_objc_class_implementation (parser);
21075 break;
21076 case RID_AT_END:
21077 cp_parser_objc_end_implementation (parser);
21078 break;
21079 default:
21080 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
21081 kwd->u.value);
21082 cp_parser_skip_to_end_of_block_or_statement (parser);
21086 /* Parse an Objective-C try-catch-finally statement.
21088 objc-try-catch-finally-stmt:
21089 @try compound-statement objc-catch-clause-seq [opt]
21090 objc-finally-clause [opt]
21092 objc-catch-clause-seq:
21093 objc-catch-clause objc-catch-clause-seq [opt]
21095 objc-catch-clause:
21096 @catch ( exception-declaration ) compound-statement
21098 objc-finally-clause
21099 @finally compound-statement
21101 Returns NULL_TREE. */
21103 static tree
21104 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
21105 location_t location;
21106 tree stmt;
21108 cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
21109 location = cp_lexer_peek_token (parser->lexer)->location;
21110 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
21111 node, lest it get absorbed into the surrounding block. */
21112 stmt = push_stmt_list ();
21113 cp_parser_compound_statement (parser, NULL, false);
21114 objc_begin_try_stmt (location, pop_stmt_list (stmt));
21116 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
21118 cp_parameter_declarator *parmdecl;
21119 tree parm;
21121 cp_lexer_consume_token (parser->lexer);
21122 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
21123 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
21124 parm = grokdeclarator (parmdecl->declarator,
21125 &parmdecl->decl_specifiers,
21126 PARM, /*initialized=*/0,
21127 /*attrlist=*/NULL);
21128 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
21129 objc_begin_catch_clause (parm);
21130 cp_parser_compound_statement (parser, NULL, false);
21131 objc_finish_catch_clause ();
21134 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
21136 cp_lexer_consume_token (parser->lexer);
21137 location = cp_lexer_peek_token (parser->lexer)->location;
21138 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
21139 node, lest it get absorbed into the surrounding block. */
21140 stmt = push_stmt_list ();
21141 cp_parser_compound_statement (parser, NULL, false);
21142 objc_build_finally_clause (location, pop_stmt_list (stmt));
21145 return objc_finish_try_stmt ();
21148 /* Parse an Objective-C synchronized statement.
21150 objc-synchronized-stmt:
21151 @synchronized ( expression ) compound-statement
21153 Returns NULL_TREE. */
21155 static tree
21156 cp_parser_objc_synchronized_statement (cp_parser *parser) {
21157 location_t location;
21158 tree lock, stmt;
21160 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
21162 location = cp_lexer_peek_token (parser->lexer)->location;
21163 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
21164 lock = cp_parser_expression (parser, false, NULL);
21165 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
21167 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
21168 node, lest it get absorbed into the surrounding block. */
21169 stmt = push_stmt_list ();
21170 cp_parser_compound_statement (parser, NULL, false);
21172 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
21175 /* Parse an Objective-C throw statement.
21177 objc-throw-stmt:
21178 @throw assignment-expression [opt] ;
21180 Returns a constructed '@throw' statement. */
21182 static tree
21183 cp_parser_objc_throw_statement (cp_parser *parser) {
21184 tree expr = NULL_TREE;
21185 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21187 cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
21189 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21190 expr = cp_parser_assignment_expression (parser, false, NULL);
21192 cp_parser_consume_semicolon_at_end_of_statement (parser);
21194 return objc_build_throw_stmt (loc, expr);
21197 /* Parse an Objective-C statement. */
21199 static tree
21200 cp_parser_objc_statement (cp_parser * parser) {
21201 /* Try to figure out what kind of declaration is present. */
21202 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21204 switch (kwd->keyword)
21206 case RID_AT_TRY:
21207 return cp_parser_objc_try_catch_finally_statement (parser);
21208 case RID_AT_SYNCHRONIZED:
21209 return cp_parser_objc_synchronized_statement (parser);
21210 case RID_AT_THROW:
21211 return cp_parser_objc_throw_statement (parser);
21212 default:
21213 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
21214 kwd->u.value);
21215 cp_parser_skip_to_end_of_block_or_statement (parser);
21218 return error_mark_node;
21221 /* OpenMP 2.5 parsing routines. */
21223 /* Returns name of the next clause.
21224 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
21225 the token is not consumed. Otherwise appropriate pragma_omp_clause is
21226 returned and the token is consumed. */
21228 static pragma_omp_clause
21229 cp_parser_omp_clause_name (cp_parser *parser)
21231 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
21233 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
21234 result = PRAGMA_OMP_CLAUSE_IF;
21235 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
21236 result = PRAGMA_OMP_CLAUSE_DEFAULT;
21237 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
21238 result = PRAGMA_OMP_CLAUSE_PRIVATE;
21239 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21241 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21242 const char *p = IDENTIFIER_POINTER (id);
21244 switch (p[0])
21246 case 'c':
21247 if (!strcmp ("collapse", p))
21248 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
21249 else if (!strcmp ("copyin", p))
21250 result = PRAGMA_OMP_CLAUSE_COPYIN;
21251 else if (!strcmp ("copyprivate", p))
21252 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
21253 break;
21254 case 'f':
21255 if (!strcmp ("firstprivate", p))
21256 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
21257 break;
21258 case 'l':
21259 if (!strcmp ("lastprivate", p))
21260 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
21261 break;
21262 case 'n':
21263 if (!strcmp ("nowait", p))
21264 result = PRAGMA_OMP_CLAUSE_NOWAIT;
21265 else if (!strcmp ("num_threads", p))
21266 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
21267 break;
21268 case 'o':
21269 if (!strcmp ("ordered", p))
21270 result = PRAGMA_OMP_CLAUSE_ORDERED;
21271 break;
21272 case 'r':
21273 if (!strcmp ("reduction", p))
21274 result = PRAGMA_OMP_CLAUSE_REDUCTION;
21275 break;
21276 case 's':
21277 if (!strcmp ("schedule", p))
21278 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
21279 else if (!strcmp ("shared", p))
21280 result = PRAGMA_OMP_CLAUSE_SHARED;
21281 break;
21282 case 'u':
21283 if (!strcmp ("untied", p))
21284 result = PRAGMA_OMP_CLAUSE_UNTIED;
21285 break;
21289 if (result != PRAGMA_OMP_CLAUSE_NONE)
21290 cp_lexer_consume_token (parser->lexer);
21292 return result;
21295 /* Validate that a clause of the given type does not already exist. */
21297 static void
21298 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
21299 const char *name, location_t location)
21301 tree c;
21303 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21304 if (OMP_CLAUSE_CODE (c) == code)
21306 error_at (location, "too many %qs clauses", name);
21307 break;
21311 /* OpenMP 2.5:
21312 variable-list:
21313 identifier
21314 variable-list , identifier
21316 In addition, we match a closing parenthesis. An opening parenthesis
21317 will have been consumed by the caller.
21319 If KIND is nonzero, create the appropriate node and install the decl
21320 in OMP_CLAUSE_DECL and add the node to the head of the list.
21322 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
21323 return the list created. */
21325 static tree
21326 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
21327 tree list)
21329 cp_token *token;
21330 while (1)
21332 tree name, decl;
21334 token = cp_lexer_peek_token (parser->lexer);
21335 name = cp_parser_id_expression (parser, /*template_p=*/false,
21336 /*check_dependency_p=*/true,
21337 /*template_p=*/NULL,
21338 /*declarator_p=*/false,
21339 /*optional_p=*/false);
21340 if (name == error_mark_node)
21341 goto skip_comma;
21343 decl = cp_parser_lookup_name_simple (parser, name, token->location);
21344 if (decl == error_mark_node)
21345 cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
21346 else if (kind != 0)
21348 tree u = build_omp_clause (token->location, kind);
21349 OMP_CLAUSE_DECL (u) = decl;
21350 OMP_CLAUSE_CHAIN (u) = list;
21351 list = u;
21353 else
21354 list = tree_cons (decl, NULL_TREE, list);
21356 get_comma:
21357 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21358 break;
21359 cp_lexer_consume_token (parser->lexer);
21362 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21364 int ending;
21366 /* Try to resync to an unnested comma. Copied from
21367 cp_parser_parenthesized_expression_list. */
21368 skip_comma:
21369 ending = cp_parser_skip_to_closing_parenthesis (parser,
21370 /*recovering=*/true,
21371 /*or_comma=*/true,
21372 /*consume_paren=*/true);
21373 if (ending < 0)
21374 goto get_comma;
21377 return list;
21380 /* Similarly, but expect leading and trailing parenthesis. This is a very
21381 common case for omp clauses. */
21383 static tree
21384 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
21386 if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21387 return cp_parser_omp_var_list_no_open (parser, kind, list);
21388 return list;
21391 /* OpenMP 3.0:
21392 collapse ( constant-expression ) */
21394 static tree
21395 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
21397 tree c, num;
21398 location_t loc;
21399 HOST_WIDE_INT n;
21401 loc = cp_lexer_peek_token (parser->lexer)->location;
21402 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21403 return list;
21405 num = cp_parser_constant_expression (parser, false, NULL);
21407 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21408 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21409 /*or_comma=*/false,
21410 /*consume_paren=*/true);
21412 if (num == error_mark_node)
21413 return list;
21414 num = fold_non_dependent_expr (num);
21415 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
21416 || !host_integerp (num, 0)
21417 || (n = tree_low_cst (num, 0)) <= 0
21418 || (int) n != n)
21420 error_at (loc, "collapse argument needs positive constant integer expression");
21421 return list;
21424 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
21425 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
21426 OMP_CLAUSE_CHAIN (c) = list;
21427 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
21429 return c;
21432 /* OpenMP 2.5:
21433 default ( shared | none ) */
21435 static tree
21436 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
21438 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
21439 tree c;
21441 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21442 return list;
21443 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21445 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21446 const char *p = IDENTIFIER_POINTER (id);
21448 switch (p[0])
21450 case 'n':
21451 if (strcmp ("none", p) != 0)
21452 goto invalid_kind;
21453 kind = OMP_CLAUSE_DEFAULT_NONE;
21454 break;
21456 case 's':
21457 if (strcmp ("shared", p) != 0)
21458 goto invalid_kind;
21459 kind = OMP_CLAUSE_DEFAULT_SHARED;
21460 break;
21462 default:
21463 goto invalid_kind;
21466 cp_lexer_consume_token (parser->lexer);
21468 else
21470 invalid_kind:
21471 cp_parser_error (parser, "expected %<none%> or %<shared%>");
21474 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21475 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21476 /*or_comma=*/false,
21477 /*consume_paren=*/true);
21479 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
21480 return list;
21482 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
21483 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
21484 OMP_CLAUSE_CHAIN (c) = list;
21485 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
21487 return c;
21490 /* OpenMP 2.5:
21491 if ( expression ) */
21493 static tree
21494 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
21496 tree t, c;
21498 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21499 return list;
21501 t = cp_parser_condition (parser);
21503 if (t == error_mark_node
21504 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21505 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21506 /*or_comma=*/false,
21507 /*consume_paren=*/true);
21509 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
21511 c = build_omp_clause (location, OMP_CLAUSE_IF);
21512 OMP_CLAUSE_IF_EXPR (c) = t;
21513 OMP_CLAUSE_CHAIN (c) = list;
21515 return c;
21518 /* OpenMP 2.5:
21519 nowait */
21521 static tree
21522 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
21523 tree list, location_t location)
21525 tree c;
21527 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
21529 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
21530 OMP_CLAUSE_CHAIN (c) = list;
21531 return c;
21534 /* OpenMP 2.5:
21535 num_threads ( expression ) */
21537 static tree
21538 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
21539 location_t location)
21541 tree t, c;
21543 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21544 return list;
21546 t = cp_parser_expression (parser, false, NULL);
21548 if (t == error_mark_node
21549 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21550 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21551 /*or_comma=*/false,
21552 /*consume_paren=*/true);
21554 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
21555 "num_threads", location);
21557 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
21558 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
21559 OMP_CLAUSE_CHAIN (c) = list;
21561 return c;
21564 /* OpenMP 2.5:
21565 ordered */
21567 static tree
21568 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
21569 tree list, location_t location)
21571 tree c;
21573 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
21574 "ordered", location);
21576 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
21577 OMP_CLAUSE_CHAIN (c) = list;
21578 return c;
21581 /* OpenMP 2.5:
21582 reduction ( reduction-operator : variable-list )
21584 reduction-operator:
21585 One of: + * - & ^ | && || */
21587 static tree
21588 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
21590 enum tree_code code;
21591 tree nlist, c;
21593 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21594 return list;
21596 switch (cp_lexer_peek_token (parser->lexer)->type)
21598 case CPP_PLUS:
21599 code = PLUS_EXPR;
21600 break;
21601 case CPP_MULT:
21602 code = MULT_EXPR;
21603 break;
21604 case CPP_MINUS:
21605 code = MINUS_EXPR;
21606 break;
21607 case CPP_AND:
21608 code = BIT_AND_EXPR;
21609 break;
21610 case CPP_XOR:
21611 code = BIT_XOR_EXPR;
21612 break;
21613 case CPP_OR:
21614 code = BIT_IOR_EXPR;
21615 break;
21616 case CPP_AND_AND:
21617 code = TRUTH_ANDIF_EXPR;
21618 break;
21619 case CPP_OR_OR:
21620 code = TRUTH_ORIF_EXPR;
21621 break;
21622 default:
21623 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
21624 "%<|%>, %<&&%>, or %<||%>");
21625 resync_fail:
21626 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21627 /*or_comma=*/false,
21628 /*consume_paren=*/true);
21629 return list;
21631 cp_lexer_consume_token (parser->lexer);
21633 if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
21634 goto resync_fail;
21636 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
21637 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
21638 OMP_CLAUSE_REDUCTION_CODE (c) = code;
21640 return nlist;
21643 /* OpenMP 2.5:
21644 schedule ( schedule-kind )
21645 schedule ( schedule-kind , expression )
21647 schedule-kind:
21648 static | dynamic | guided | runtime | auto */
21650 static tree
21651 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
21653 tree c, t;
21655 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21656 return list;
21658 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
21660 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21662 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21663 const char *p = IDENTIFIER_POINTER (id);
21665 switch (p[0])
21667 case 'd':
21668 if (strcmp ("dynamic", p) != 0)
21669 goto invalid_kind;
21670 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
21671 break;
21673 case 'g':
21674 if (strcmp ("guided", p) != 0)
21675 goto invalid_kind;
21676 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
21677 break;
21679 case 'r':
21680 if (strcmp ("runtime", p) != 0)
21681 goto invalid_kind;
21682 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
21683 break;
21685 default:
21686 goto invalid_kind;
21689 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
21690 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
21691 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
21692 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
21693 else
21694 goto invalid_kind;
21695 cp_lexer_consume_token (parser->lexer);
21697 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21699 cp_token *token;
21700 cp_lexer_consume_token (parser->lexer);
21702 token = cp_lexer_peek_token (parser->lexer);
21703 t = cp_parser_assignment_expression (parser, false, NULL);
21705 if (t == error_mark_node)
21706 goto resync_fail;
21707 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
21708 error_at (token->location, "schedule %<runtime%> does not take "
21709 "a %<chunk_size%> parameter");
21710 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
21711 error_at (token->location, "schedule %<auto%> does not take "
21712 "a %<chunk_size%> parameter");
21713 else
21714 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
21716 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21717 goto resync_fail;
21719 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
21720 goto resync_fail;
21722 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
21723 OMP_CLAUSE_CHAIN (c) = list;
21724 return c;
21726 invalid_kind:
21727 cp_parser_error (parser, "invalid schedule kind");
21728 resync_fail:
21729 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21730 /*or_comma=*/false,
21731 /*consume_paren=*/true);
21732 return list;
21735 /* OpenMP 3.0:
21736 untied */
21738 static tree
21739 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
21740 tree list, location_t location)
21742 tree c;
21744 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
21746 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
21747 OMP_CLAUSE_CHAIN (c) = list;
21748 return c;
21751 /* Parse all OpenMP clauses. The set clauses allowed by the directive
21752 is a bitmask in MASK. Return the list of clauses found; the result
21753 of clause default goes in *pdefault. */
21755 static tree
21756 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
21757 const char *where, cp_token *pragma_tok)
21759 tree clauses = NULL;
21760 bool first = true;
21761 cp_token *token = NULL;
21763 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
21765 pragma_omp_clause c_kind;
21766 const char *c_name;
21767 tree prev = clauses;
21769 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21770 cp_lexer_consume_token (parser->lexer);
21772 token = cp_lexer_peek_token (parser->lexer);
21773 c_kind = cp_parser_omp_clause_name (parser);
21774 first = false;
21776 switch (c_kind)
21778 case PRAGMA_OMP_CLAUSE_COLLAPSE:
21779 clauses = cp_parser_omp_clause_collapse (parser, clauses,
21780 token->location);
21781 c_name = "collapse";
21782 break;
21783 case PRAGMA_OMP_CLAUSE_COPYIN:
21784 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
21785 c_name = "copyin";
21786 break;
21787 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
21788 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
21789 clauses);
21790 c_name = "copyprivate";
21791 break;
21792 case PRAGMA_OMP_CLAUSE_DEFAULT:
21793 clauses = cp_parser_omp_clause_default (parser, clauses,
21794 token->location);
21795 c_name = "default";
21796 break;
21797 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
21798 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
21799 clauses);
21800 c_name = "firstprivate";
21801 break;
21802 case PRAGMA_OMP_CLAUSE_IF:
21803 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
21804 c_name = "if";
21805 break;
21806 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
21807 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
21808 clauses);
21809 c_name = "lastprivate";
21810 break;
21811 case PRAGMA_OMP_CLAUSE_NOWAIT:
21812 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
21813 c_name = "nowait";
21814 break;
21815 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
21816 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
21817 token->location);
21818 c_name = "num_threads";
21819 break;
21820 case PRAGMA_OMP_CLAUSE_ORDERED:
21821 clauses = cp_parser_omp_clause_ordered (parser, clauses,
21822 token->location);
21823 c_name = "ordered";
21824 break;
21825 case PRAGMA_OMP_CLAUSE_PRIVATE:
21826 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
21827 clauses);
21828 c_name = "private";
21829 break;
21830 case PRAGMA_OMP_CLAUSE_REDUCTION:
21831 clauses = cp_parser_omp_clause_reduction (parser, clauses);
21832 c_name = "reduction";
21833 break;
21834 case PRAGMA_OMP_CLAUSE_SCHEDULE:
21835 clauses = cp_parser_omp_clause_schedule (parser, clauses,
21836 token->location);
21837 c_name = "schedule";
21838 break;
21839 case PRAGMA_OMP_CLAUSE_SHARED:
21840 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
21841 clauses);
21842 c_name = "shared";
21843 break;
21844 case PRAGMA_OMP_CLAUSE_UNTIED:
21845 clauses = cp_parser_omp_clause_untied (parser, clauses,
21846 token->location);
21847 c_name = "nowait";
21848 break;
21849 default:
21850 cp_parser_error (parser, "expected %<#pragma omp%> clause");
21851 goto saw_error;
21854 if (((mask >> c_kind) & 1) == 0)
21856 /* Remove the invalid clause(s) from the list to avoid
21857 confusing the rest of the compiler. */
21858 clauses = prev;
21859 error_at (token->location, "%qs is not valid for %qs", c_name, where);
21862 saw_error:
21863 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
21864 return finish_omp_clauses (clauses);
21867 /* OpenMP 2.5:
21868 structured-block:
21869 statement
21871 In practice, we're also interested in adding the statement to an
21872 outer node. So it is convenient if we work around the fact that
21873 cp_parser_statement calls add_stmt. */
21875 static unsigned
21876 cp_parser_begin_omp_structured_block (cp_parser *parser)
21878 unsigned save = parser->in_statement;
21880 /* Only move the values to IN_OMP_BLOCK if they weren't false.
21881 This preserves the "not within loop or switch" style error messages
21882 for nonsense cases like
21883 void foo() {
21884 #pragma omp single
21885 break;
21888 if (parser->in_statement)
21889 parser->in_statement = IN_OMP_BLOCK;
21891 return save;
21894 static void
21895 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
21897 parser->in_statement = save;
21900 static tree
21901 cp_parser_omp_structured_block (cp_parser *parser)
21903 tree stmt = begin_omp_structured_block ();
21904 unsigned int save = cp_parser_begin_omp_structured_block (parser);
21906 cp_parser_statement (parser, NULL_TREE, false, NULL);
21908 cp_parser_end_omp_structured_block (parser, save);
21909 return finish_omp_structured_block (stmt);
21912 /* OpenMP 2.5:
21913 # pragma omp atomic new-line
21914 expression-stmt
21916 expression-stmt:
21917 x binop= expr | x++ | ++x | x-- | --x
21918 binop:
21919 +, *, -, /, &, ^, |, <<, >>
21921 where x is an lvalue expression with scalar type. */
21923 static void
21924 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
21926 tree lhs, rhs;
21927 enum tree_code code;
21929 cp_parser_require_pragma_eol (parser, pragma_tok);
21931 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
21932 /*cast_p=*/false, NULL);
21933 switch (TREE_CODE (lhs))
21935 case ERROR_MARK:
21936 goto saw_error;
21938 case PREINCREMENT_EXPR:
21939 case POSTINCREMENT_EXPR:
21940 lhs = TREE_OPERAND (lhs, 0);
21941 code = PLUS_EXPR;
21942 rhs = integer_one_node;
21943 break;
21945 case PREDECREMENT_EXPR:
21946 case POSTDECREMENT_EXPR:
21947 lhs = TREE_OPERAND (lhs, 0);
21948 code = MINUS_EXPR;
21949 rhs = integer_one_node;
21950 break;
21952 default:
21953 switch (cp_lexer_peek_token (parser->lexer)->type)
21955 case CPP_MULT_EQ:
21956 code = MULT_EXPR;
21957 break;
21958 case CPP_DIV_EQ:
21959 code = TRUNC_DIV_EXPR;
21960 break;
21961 case CPP_PLUS_EQ:
21962 code = PLUS_EXPR;
21963 break;
21964 case CPP_MINUS_EQ:
21965 code = MINUS_EXPR;
21966 break;
21967 case CPP_LSHIFT_EQ:
21968 code = LSHIFT_EXPR;
21969 break;
21970 case CPP_RSHIFT_EQ:
21971 code = RSHIFT_EXPR;
21972 break;
21973 case CPP_AND_EQ:
21974 code = BIT_AND_EXPR;
21975 break;
21976 case CPP_OR_EQ:
21977 code = BIT_IOR_EXPR;
21978 break;
21979 case CPP_XOR_EQ:
21980 code = BIT_XOR_EXPR;
21981 break;
21982 default:
21983 cp_parser_error (parser,
21984 "invalid operator for %<#pragma omp atomic%>");
21985 goto saw_error;
21987 cp_lexer_consume_token (parser->lexer);
21989 rhs = cp_parser_expression (parser, false, NULL);
21990 if (rhs == error_mark_node)
21991 goto saw_error;
21992 break;
21994 finish_omp_atomic (code, lhs, rhs);
21995 cp_parser_consume_semicolon_at_end_of_statement (parser);
21996 return;
21998 saw_error:
21999 cp_parser_skip_to_end_of_block_or_statement (parser);
22003 /* OpenMP 2.5:
22004 # pragma omp barrier new-line */
22006 static void
22007 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
22009 cp_parser_require_pragma_eol (parser, pragma_tok);
22010 finish_omp_barrier ();
22013 /* OpenMP 2.5:
22014 # pragma omp critical [(name)] new-line
22015 structured-block */
22017 static tree
22018 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
22020 tree stmt, name = NULL;
22022 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22024 cp_lexer_consume_token (parser->lexer);
22026 name = cp_parser_identifier (parser);
22028 if (name == error_mark_node
22029 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
22030 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22031 /*or_comma=*/false,
22032 /*consume_paren=*/true);
22033 if (name == error_mark_node)
22034 name = NULL;
22036 cp_parser_require_pragma_eol (parser, pragma_tok);
22038 stmt = cp_parser_omp_structured_block (parser);
22039 return c_finish_omp_critical (input_location, stmt, name);
22042 /* OpenMP 2.5:
22043 # pragma omp flush flush-vars[opt] new-line
22045 flush-vars:
22046 ( variable-list ) */
22048 static void
22049 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
22051 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22052 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
22053 cp_parser_require_pragma_eol (parser, pragma_tok);
22055 finish_omp_flush ();
22058 /* Helper function, to parse omp for increment expression. */
22060 static tree
22061 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
22063 tree cond = cp_parser_binary_expression (parser, false, true,
22064 PREC_NOT_OPERATOR, NULL);
22065 bool overloaded_p;
22067 if (cond == error_mark_node
22068 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22070 cp_parser_skip_to_end_of_statement (parser);
22071 return error_mark_node;
22074 switch (TREE_CODE (cond))
22076 case GT_EXPR:
22077 case GE_EXPR:
22078 case LT_EXPR:
22079 case LE_EXPR:
22080 break;
22081 default:
22082 return error_mark_node;
22085 /* If decl is an iterator, preserve LHS and RHS of the relational
22086 expr until finish_omp_for. */
22087 if (decl
22088 && (type_dependent_expression_p (decl)
22089 || CLASS_TYPE_P (TREE_TYPE (decl))))
22090 return cond;
22092 return build_x_binary_op (TREE_CODE (cond),
22093 TREE_OPERAND (cond, 0), ERROR_MARK,
22094 TREE_OPERAND (cond, 1), ERROR_MARK,
22095 &overloaded_p, tf_warning_or_error);
22098 /* Helper function, to parse omp for increment expression. */
22100 static tree
22101 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
22103 cp_token *token = cp_lexer_peek_token (parser->lexer);
22104 enum tree_code op;
22105 tree lhs, rhs;
22106 cp_id_kind idk;
22107 bool decl_first;
22109 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
22111 op = (token->type == CPP_PLUS_PLUS
22112 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
22113 cp_lexer_consume_token (parser->lexer);
22114 lhs = cp_parser_cast_expression (parser, false, false, NULL);
22115 if (lhs != decl)
22116 return error_mark_node;
22117 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
22120 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
22121 if (lhs != decl)
22122 return error_mark_node;
22124 token = cp_lexer_peek_token (parser->lexer);
22125 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
22127 op = (token->type == CPP_PLUS_PLUS
22128 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
22129 cp_lexer_consume_token (parser->lexer);
22130 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
22133 op = cp_parser_assignment_operator_opt (parser);
22134 if (op == ERROR_MARK)
22135 return error_mark_node;
22137 if (op != NOP_EXPR)
22139 rhs = cp_parser_assignment_expression (parser, false, NULL);
22140 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
22141 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
22144 lhs = cp_parser_binary_expression (parser, false, false,
22145 PREC_ADDITIVE_EXPRESSION, NULL);
22146 token = cp_lexer_peek_token (parser->lexer);
22147 decl_first = lhs == decl;
22148 if (decl_first)
22149 lhs = NULL_TREE;
22150 if (token->type != CPP_PLUS
22151 && token->type != CPP_MINUS)
22152 return error_mark_node;
22156 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
22157 cp_lexer_consume_token (parser->lexer);
22158 rhs = cp_parser_binary_expression (parser, false, false,
22159 PREC_ADDITIVE_EXPRESSION, NULL);
22160 token = cp_lexer_peek_token (parser->lexer);
22161 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
22163 if (lhs == NULL_TREE)
22165 if (op == PLUS_EXPR)
22166 lhs = rhs;
22167 else
22168 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
22170 else
22171 lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
22172 NULL, tf_warning_or_error);
22175 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
22177 if (!decl_first)
22179 if (rhs != decl || op == MINUS_EXPR)
22180 return error_mark_node;
22181 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
22183 else
22184 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
22186 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
22189 /* Parse the restricted form of the for statement allowed by OpenMP. */
22191 static tree
22192 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
22194 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
22195 tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
22196 tree this_pre_body, cl;
22197 location_t loc_first;
22198 bool collapse_err = false;
22199 int i, collapse = 1, nbraces = 0;
22201 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
22202 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
22203 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
22205 gcc_assert (collapse >= 1);
22207 declv = make_tree_vec (collapse);
22208 initv = make_tree_vec (collapse);
22209 condv = make_tree_vec (collapse);
22210 incrv = make_tree_vec (collapse);
22212 loc_first = cp_lexer_peek_token (parser->lexer)->location;
22214 for (i = 0; i < collapse; i++)
22216 int bracecount = 0;
22217 bool add_private_clause = false;
22218 location_t loc;
22220 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22222 cp_parser_error (parser, "for statement expected");
22223 return NULL;
22225 loc = cp_lexer_consume_token (parser->lexer)->location;
22227 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
22228 return NULL;
22230 init = decl = real_decl = NULL;
22231 this_pre_body = push_stmt_list ();
22232 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22234 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
22236 init-expr:
22237 var = lb
22238 integer-type var = lb
22239 random-access-iterator-type var = lb
22240 pointer-type var = lb
22242 cp_decl_specifier_seq type_specifiers;
22244 /* First, try to parse as an initialized declaration. See
22245 cp_parser_condition, from whence the bulk of this is copied. */
22247 cp_parser_parse_tentatively (parser);
22248 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
22249 /*is_trailing_return=*/false,
22250 &type_specifiers);
22251 if (cp_parser_parse_definitely (parser))
22253 /* If parsing a type specifier seq succeeded, then this
22254 MUST be a initialized declaration. */
22255 tree asm_specification, attributes;
22256 cp_declarator *declarator;
22258 declarator = cp_parser_declarator (parser,
22259 CP_PARSER_DECLARATOR_NAMED,
22260 /*ctor_dtor_or_conv_p=*/NULL,
22261 /*parenthesized_p=*/NULL,
22262 /*member_p=*/false);
22263 attributes = cp_parser_attributes_opt (parser);
22264 asm_specification = cp_parser_asm_specification_opt (parser);
22266 if (declarator == cp_error_declarator)
22267 cp_parser_skip_to_end_of_statement (parser);
22269 else
22271 tree pushed_scope, auto_node;
22273 decl = start_decl (declarator, &type_specifiers,
22274 SD_INITIALIZED, attributes,
22275 /*prefix_attributes=*/NULL_TREE,
22276 &pushed_scope);
22278 auto_node = type_uses_auto (TREE_TYPE (decl));
22279 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
22281 if (cp_lexer_next_token_is (parser->lexer,
22282 CPP_OPEN_PAREN))
22283 error ("parenthesized initialization is not allowed in "
22284 "OpenMP %<for%> loop");
22285 else
22286 /* Trigger an error. */
22287 cp_parser_require (parser, CPP_EQ, "%<=%>");
22289 init = error_mark_node;
22290 cp_parser_skip_to_end_of_statement (parser);
22292 else if (CLASS_TYPE_P (TREE_TYPE (decl))
22293 || type_dependent_expression_p (decl)
22294 || auto_node)
22296 bool is_direct_init, is_non_constant_init;
22298 init = cp_parser_initializer (parser,
22299 &is_direct_init,
22300 &is_non_constant_init);
22302 if (auto_node && describable_type (init))
22304 TREE_TYPE (decl)
22305 = do_auto_deduction (TREE_TYPE (decl), init,
22306 auto_node);
22308 if (!CLASS_TYPE_P (TREE_TYPE (decl))
22309 && !type_dependent_expression_p (decl))
22310 goto non_class;
22313 cp_finish_decl (decl, init, !is_non_constant_init,
22314 asm_specification,
22315 LOOKUP_ONLYCONVERTING);
22316 if (CLASS_TYPE_P (TREE_TYPE (decl)))
22318 for_block
22319 = tree_cons (NULL, this_pre_body, for_block);
22320 init = NULL_TREE;
22322 else
22323 init = pop_stmt_list (this_pre_body);
22324 this_pre_body = NULL_TREE;
22326 else
22328 /* Consume '='. */
22329 cp_lexer_consume_token (parser->lexer);
22330 init = cp_parser_assignment_expression (parser, false, NULL);
22332 non_class:
22333 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
22334 init = error_mark_node;
22335 else
22336 cp_finish_decl (decl, NULL_TREE,
22337 /*init_const_expr_p=*/false,
22338 asm_specification,
22339 LOOKUP_ONLYCONVERTING);
22342 if (pushed_scope)
22343 pop_scope (pushed_scope);
22346 else
22348 cp_id_kind idk;
22349 /* If parsing a type specifier sequence failed, then
22350 this MUST be a simple expression. */
22351 cp_parser_parse_tentatively (parser);
22352 decl = cp_parser_primary_expression (parser, false, false,
22353 false, &idk);
22354 if (!cp_parser_error_occurred (parser)
22355 && decl
22356 && DECL_P (decl)
22357 && CLASS_TYPE_P (TREE_TYPE (decl)))
22359 tree rhs;
22361 cp_parser_parse_definitely (parser);
22362 cp_parser_require (parser, CPP_EQ, "%<=%>");
22363 rhs = cp_parser_assignment_expression (parser, false, NULL);
22364 finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
22365 rhs,
22366 tf_warning_or_error));
22367 add_private_clause = true;
22369 else
22371 decl = NULL;
22372 cp_parser_abort_tentative_parse (parser);
22373 init = cp_parser_expression (parser, false, NULL);
22374 if (init)
22376 if (TREE_CODE (init) == MODIFY_EXPR
22377 || TREE_CODE (init) == MODOP_EXPR)
22378 real_decl = TREE_OPERAND (init, 0);
22383 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
22384 if (this_pre_body)
22386 this_pre_body = pop_stmt_list (this_pre_body);
22387 if (pre_body)
22389 tree t = pre_body;
22390 pre_body = push_stmt_list ();
22391 add_stmt (t);
22392 add_stmt (this_pre_body);
22393 pre_body = pop_stmt_list (pre_body);
22395 else
22396 pre_body = this_pre_body;
22399 if (decl)
22400 real_decl = decl;
22401 if (par_clauses != NULL && real_decl != NULL_TREE)
22403 tree *c;
22404 for (c = par_clauses; *c ; )
22405 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
22406 && OMP_CLAUSE_DECL (*c) == real_decl)
22408 error_at (loc, "iteration variable %qD"
22409 " should not be firstprivate", real_decl);
22410 *c = OMP_CLAUSE_CHAIN (*c);
22412 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
22413 && OMP_CLAUSE_DECL (*c) == real_decl)
22415 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
22416 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
22417 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
22418 OMP_CLAUSE_DECL (l) = real_decl;
22419 OMP_CLAUSE_CHAIN (l) = clauses;
22420 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
22421 clauses = l;
22422 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
22423 CP_OMP_CLAUSE_INFO (*c) = NULL;
22424 add_private_clause = false;
22426 else
22428 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
22429 && OMP_CLAUSE_DECL (*c) == real_decl)
22430 add_private_clause = false;
22431 c = &OMP_CLAUSE_CHAIN (*c);
22435 if (add_private_clause)
22437 tree c;
22438 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
22440 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
22441 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
22442 && OMP_CLAUSE_DECL (c) == decl)
22443 break;
22444 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
22445 && OMP_CLAUSE_DECL (c) == decl)
22446 error_at (loc, "iteration variable %qD "
22447 "should not be firstprivate",
22448 decl);
22449 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
22450 && OMP_CLAUSE_DECL (c) == decl)
22451 error_at (loc, "iteration variable %qD should not be reduction",
22452 decl);
22454 if (c == NULL)
22456 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
22457 OMP_CLAUSE_DECL (c) = decl;
22458 c = finish_omp_clauses (c);
22459 if (c)
22461 OMP_CLAUSE_CHAIN (c) = clauses;
22462 clauses = c;
22467 cond = NULL;
22468 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22469 cond = cp_parser_omp_for_cond (parser, decl);
22470 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
22472 incr = NULL;
22473 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
22475 /* If decl is an iterator, preserve the operator on decl
22476 until finish_omp_for. */
22477 if (decl
22478 && (type_dependent_expression_p (decl)
22479 || CLASS_TYPE_P (TREE_TYPE (decl))))
22480 incr = cp_parser_omp_for_incr (parser, decl);
22481 else
22482 incr = cp_parser_expression (parser, false, NULL);
22485 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
22486 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22487 /*or_comma=*/false,
22488 /*consume_paren=*/true);
22490 TREE_VEC_ELT (declv, i) = decl;
22491 TREE_VEC_ELT (initv, i) = init;
22492 TREE_VEC_ELT (condv, i) = cond;
22493 TREE_VEC_ELT (incrv, i) = incr;
22495 if (i == collapse - 1)
22496 break;
22498 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
22499 in between the collapsed for loops to be still considered perfectly
22500 nested. Hopefully the final version clarifies this.
22501 For now handle (multiple) {'s and empty statements. */
22502 cp_parser_parse_tentatively (parser);
22505 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22506 break;
22507 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22509 cp_lexer_consume_token (parser->lexer);
22510 bracecount++;
22512 else if (bracecount
22513 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22514 cp_lexer_consume_token (parser->lexer);
22515 else
22517 loc = cp_lexer_peek_token (parser->lexer)->location;
22518 error_at (loc, "not enough collapsed for loops");
22519 collapse_err = true;
22520 cp_parser_abort_tentative_parse (parser);
22521 declv = NULL_TREE;
22522 break;
22525 while (1);
22527 if (declv)
22529 cp_parser_parse_definitely (parser);
22530 nbraces += bracecount;
22534 /* Note that we saved the original contents of this flag when we entered
22535 the structured block, and so we don't need to re-save it here. */
22536 parser->in_statement = IN_OMP_FOR;
22538 /* Note that the grammar doesn't call for a structured block here,
22539 though the loop as a whole is a structured block. */
22540 body = push_stmt_list ();
22541 cp_parser_statement (parser, NULL_TREE, false, NULL);
22542 body = pop_stmt_list (body);
22544 if (declv == NULL_TREE)
22545 ret = NULL_TREE;
22546 else
22547 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
22548 pre_body, clauses);
22550 while (nbraces)
22552 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
22554 cp_lexer_consume_token (parser->lexer);
22555 nbraces--;
22557 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22558 cp_lexer_consume_token (parser->lexer);
22559 else
22561 if (!collapse_err)
22563 error_at (cp_lexer_peek_token (parser->lexer)->location,
22564 "collapsed loops not perfectly nested");
22566 collapse_err = true;
22567 cp_parser_statement_seq_opt (parser, NULL);
22568 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
22569 break;
22573 while (for_block)
22575 add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
22576 for_block = TREE_CHAIN (for_block);
22579 return ret;
22582 /* OpenMP 2.5:
22583 #pragma omp for for-clause[optseq] new-line
22584 for-loop */
22586 #define OMP_FOR_CLAUSE_MASK \
22587 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22588 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22589 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
22590 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
22591 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
22592 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
22593 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
22594 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
22596 static tree
22597 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
22599 tree clauses, sb, ret;
22600 unsigned int save;
22602 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
22603 "#pragma omp for", pragma_tok);
22605 sb = begin_omp_structured_block ();
22606 save = cp_parser_begin_omp_structured_block (parser);
22608 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
22610 cp_parser_end_omp_structured_block (parser, save);
22611 add_stmt (finish_omp_structured_block (sb));
22613 return ret;
22616 /* OpenMP 2.5:
22617 # pragma omp master new-line
22618 structured-block */
22620 static tree
22621 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
22623 cp_parser_require_pragma_eol (parser, pragma_tok);
22624 return c_finish_omp_master (input_location,
22625 cp_parser_omp_structured_block (parser));
22628 /* OpenMP 2.5:
22629 # pragma omp ordered new-line
22630 structured-block */
22632 static tree
22633 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
22635 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22636 cp_parser_require_pragma_eol (parser, pragma_tok);
22637 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
22640 /* OpenMP 2.5:
22642 section-scope:
22643 { section-sequence }
22645 section-sequence:
22646 section-directive[opt] structured-block
22647 section-sequence section-directive structured-block */
22649 static tree
22650 cp_parser_omp_sections_scope (cp_parser *parser)
22652 tree stmt, substmt;
22653 bool error_suppress = false;
22654 cp_token *tok;
22656 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
22657 return NULL_TREE;
22659 stmt = push_stmt_list ();
22661 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
22663 unsigned save;
22665 substmt = begin_omp_structured_block ();
22666 save = cp_parser_begin_omp_structured_block (parser);
22668 while (1)
22670 cp_parser_statement (parser, NULL_TREE, false, NULL);
22672 tok = cp_lexer_peek_token (parser->lexer);
22673 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
22674 break;
22675 if (tok->type == CPP_CLOSE_BRACE)
22676 break;
22677 if (tok->type == CPP_EOF)
22678 break;
22681 cp_parser_end_omp_structured_block (parser, save);
22682 substmt = finish_omp_structured_block (substmt);
22683 substmt = build1 (OMP_SECTION, void_type_node, substmt);
22684 add_stmt (substmt);
22687 while (1)
22689 tok = cp_lexer_peek_token (parser->lexer);
22690 if (tok->type == CPP_CLOSE_BRACE)
22691 break;
22692 if (tok->type == CPP_EOF)
22693 break;
22695 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
22697 cp_lexer_consume_token (parser->lexer);
22698 cp_parser_require_pragma_eol (parser, tok);
22699 error_suppress = false;
22701 else if (!error_suppress)
22703 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
22704 error_suppress = true;
22707 substmt = cp_parser_omp_structured_block (parser);
22708 substmt = build1 (OMP_SECTION, void_type_node, substmt);
22709 add_stmt (substmt);
22711 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
22713 substmt = pop_stmt_list (stmt);
22715 stmt = make_node (OMP_SECTIONS);
22716 TREE_TYPE (stmt) = void_type_node;
22717 OMP_SECTIONS_BODY (stmt) = substmt;
22719 add_stmt (stmt);
22720 return stmt;
22723 /* OpenMP 2.5:
22724 # pragma omp sections sections-clause[optseq] newline
22725 sections-scope */
22727 #define OMP_SECTIONS_CLAUSE_MASK \
22728 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22729 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22730 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
22731 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
22732 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
22734 static tree
22735 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
22737 tree clauses, ret;
22739 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
22740 "#pragma omp sections", pragma_tok);
22742 ret = cp_parser_omp_sections_scope (parser);
22743 if (ret)
22744 OMP_SECTIONS_CLAUSES (ret) = clauses;
22746 return ret;
22749 /* OpenMP 2.5:
22750 # pragma parallel parallel-clause new-line
22751 # pragma parallel for parallel-for-clause new-line
22752 # pragma parallel sections parallel-sections-clause new-line */
22754 #define OMP_PARALLEL_CLAUSE_MASK \
22755 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
22756 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22757 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22758 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
22759 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
22760 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
22761 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
22762 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
22764 static tree
22765 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
22767 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
22768 const char *p_name = "#pragma omp parallel";
22769 tree stmt, clauses, par_clause, ws_clause, block;
22770 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
22771 unsigned int save;
22772 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22774 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22776 cp_lexer_consume_token (parser->lexer);
22777 p_kind = PRAGMA_OMP_PARALLEL_FOR;
22778 p_name = "#pragma omp parallel for";
22779 mask |= OMP_FOR_CLAUSE_MASK;
22780 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
22782 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22784 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
22785 const char *p = IDENTIFIER_POINTER (id);
22786 if (strcmp (p, "sections") == 0)
22788 cp_lexer_consume_token (parser->lexer);
22789 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
22790 p_name = "#pragma omp parallel sections";
22791 mask |= OMP_SECTIONS_CLAUSE_MASK;
22792 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
22796 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
22797 block = begin_omp_parallel ();
22798 save = cp_parser_begin_omp_structured_block (parser);
22800 switch (p_kind)
22802 case PRAGMA_OMP_PARALLEL:
22803 cp_parser_statement (parser, NULL_TREE, false, NULL);
22804 par_clause = clauses;
22805 break;
22807 case PRAGMA_OMP_PARALLEL_FOR:
22808 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
22809 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
22810 break;
22812 case PRAGMA_OMP_PARALLEL_SECTIONS:
22813 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
22814 stmt = cp_parser_omp_sections_scope (parser);
22815 if (stmt)
22816 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
22817 break;
22819 default:
22820 gcc_unreachable ();
22823 cp_parser_end_omp_structured_block (parser, save);
22824 stmt = finish_omp_parallel (par_clause, block);
22825 if (p_kind != PRAGMA_OMP_PARALLEL)
22826 OMP_PARALLEL_COMBINED (stmt) = 1;
22827 return stmt;
22830 /* OpenMP 2.5:
22831 # pragma omp single single-clause[optseq] new-line
22832 structured-block */
22834 #define OMP_SINGLE_CLAUSE_MASK \
22835 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22836 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22837 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
22838 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
22840 static tree
22841 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
22843 tree stmt = make_node (OMP_SINGLE);
22844 TREE_TYPE (stmt) = void_type_node;
22846 OMP_SINGLE_CLAUSES (stmt)
22847 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
22848 "#pragma omp single", pragma_tok);
22849 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
22851 return add_stmt (stmt);
22854 /* OpenMP 3.0:
22855 # pragma omp task task-clause[optseq] new-line
22856 structured-block */
22858 #define OMP_TASK_CLAUSE_MASK \
22859 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
22860 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
22861 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
22862 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22863 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22864 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
22866 static tree
22867 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
22869 tree clauses, block;
22870 unsigned int save;
22872 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
22873 "#pragma omp task", pragma_tok);
22874 block = begin_omp_task ();
22875 save = cp_parser_begin_omp_structured_block (parser);
22876 cp_parser_statement (parser, NULL_TREE, false, NULL);
22877 cp_parser_end_omp_structured_block (parser, save);
22878 return finish_omp_task (clauses, block);
22881 /* OpenMP 3.0:
22882 # pragma omp taskwait new-line */
22884 static void
22885 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
22887 cp_parser_require_pragma_eol (parser, pragma_tok);
22888 finish_omp_taskwait ();
22891 /* OpenMP 2.5:
22892 # pragma omp threadprivate (variable-list) */
22894 static void
22895 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
22897 tree vars;
22899 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
22900 cp_parser_require_pragma_eol (parser, pragma_tok);
22902 finish_omp_threadprivate (vars);
22905 /* Main entry point to OpenMP statement pragmas. */
22907 static void
22908 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
22910 tree stmt;
22912 switch (pragma_tok->pragma_kind)
22914 case PRAGMA_OMP_ATOMIC:
22915 cp_parser_omp_atomic (parser, pragma_tok);
22916 return;
22917 case PRAGMA_OMP_CRITICAL:
22918 stmt = cp_parser_omp_critical (parser, pragma_tok);
22919 break;
22920 case PRAGMA_OMP_FOR:
22921 stmt = cp_parser_omp_for (parser, pragma_tok);
22922 break;
22923 case PRAGMA_OMP_MASTER:
22924 stmt = cp_parser_omp_master (parser, pragma_tok);
22925 break;
22926 case PRAGMA_OMP_ORDERED:
22927 stmt = cp_parser_omp_ordered (parser, pragma_tok);
22928 break;
22929 case PRAGMA_OMP_PARALLEL:
22930 stmt = cp_parser_omp_parallel (parser, pragma_tok);
22931 break;
22932 case PRAGMA_OMP_SECTIONS:
22933 stmt = cp_parser_omp_sections (parser, pragma_tok);
22934 break;
22935 case PRAGMA_OMP_SINGLE:
22936 stmt = cp_parser_omp_single (parser, pragma_tok);
22937 break;
22938 case PRAGMA_OMP_TASK:
22939 stmt = cp_parser_omp_task (parser, pragma_tok);
22940 break;
22941 default:
22942 gcc_unreachable ();
22945 if (stmt)
22946 SET_EXPR_LOCATION (stmt, pragma_tok->location);
22949 /* The parser. */
22951 static GTY (()) cp_parser *the_parser;
22954 /* Special handling for the first token or line in the file. The first
22955 thing in the file might be #pragma GCC pch_preprocess, which loads a
22956 PCH file, which is a GC collection point. So we need to handle this
22957 first pragma without benefit of an existing lexer structure.
22959 Always returns one token to the caller in *FIRST_TOKEN. This is
22960 either the true first token of the file, or the first token after
22961 the initial pragma. */
22963 static void
22964 cp_parser_initial_pragma (cp_token *first_token)
22966 tree name = NULL;
22968 cp_lexer_get_preprocessor_token (NULL, first_token);
22969 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
22970 return;
22972 cp_lexer_get_preprocessor_token (NULL, first_token);
22973 if (first_token->type == CPP_STRING)
22975 name = first_token->u.value;
22977 cp_lexer_get_preprocessor_token (NULL, first_token);
22978 if (first_token->type != CPP_PRAGMA_EOL)
22979 error_at (first_token->location,
22980 "junk at end of %<#pragma GCC pch_preprocess%>");
22982 else
22983 error_at (first_token->location, "expected string literal");
22985 /* Skip to the end of the pragma. */
22986 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
22987 cp_lexer_get_preprocessor_token (NULL, first_token);
22989 /* Now actually load the PCH file. */
22990 if (name)
22991 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
22993 /* Read one more token to return to our caller. We have to do this
22994 after reading the PCH file in, since its pointers have to be
22995 live. */
22996 cp_lexer_get_preprocessor_token (NULL, first_token);
22999 /* Normal parsing of a pragma token. Here we can (and must) use the
23000 regular lexer. */
23002 static bool
23003 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
23005 cp_token *pragma_tok;
23006 unsigned int id;
23008 pragma_tok = cp_lexer_consume_token (parser->lexer);
23009 gcc_assert (pragma_tok->type == CPP_PRAGMA);
23010 parser->lexer->in_pragma = true;
23012 id = pragma_tok->pragma_kind;
23013 switch (id)
23015 case PRAGMA_GCC_PCH_PREPROCESS:
23016 error_at (pragma_tok->location,
23017 "%<#pragma GCC pch_preprocess%> must be first");
23018 break;
23020 case PRAGMA_OMP_BARRIER:
23021 switch (context)
23023 case pragma_compound:
23024 cp_parser_omp_barrier (parser, pragma_tok);
23025 return false;
23026 case pragma_stmt:
23027 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
23028 "used in compound statements");
23029 break;
23030 default:
23031 goto bad_stmt;
23033 break;
23035 case PRAGMA_OMP_FLUSH:
23036 switch (context)
23038 case pragma_compound:
23039 cp_parser_omp_flush (parser, pragma_tok);
23040 return false;
23041 case pragma_stmt:
23042 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
23043 "used in compound statements");
23044 break;
23045 default:
23046 goto bad_stmt;
23048 break;
23050 case PRAGMA_OMP_TASKWAIT:
23051 switch (context)
23053 case pragma_compound:
23054 cp_parser_omp_taskwait (parser, pragma_tok);
23055 return false;
23056 case pragma_stmt:
23057 error_at (pragma_tok->location,
23058 "%<#pragma omp taskwait%> may only be "
23059 "used in compound statements");
23060 break;
23061 default:
23062 goto bad_stmt;
23064 break;
23066 case PRAGMA_OMP_THREADPRIVATE:
23067 cp_parser_omp_threadprivate (parser, pragma_tok);
23068 return false;
23070 case PRAGMA_OMP_ATOMIC:
23071 case PRAGMA_OMP_CRITICAL:
23072 case PRAGMA_OMP_FOR:
23073 case PRAGMA_OMP_MASTER:
23074 case PRAGMA_OMP_ORDERED:
23075 case PRAGMA_OMP_PARALLEL:
23076 case PRAGMA_OMP_SECTIONS:
23077 case PRAGMA_OMP_SINGLE:
23078 case PRAGMA_OMP_TASK:
23079 if (context == pragma_external)
23080 goto bad_stmt;
23081 cp_parser_omp_construct (parser, pragma_tok);
23082 return true;
23084 case PRAGMA_OMP_SECTION:
23085 error_at (pragma_tok->location,
23086 "%<#pragma omp section%> may only be used in "
23087 "%<#pragma omp sections%> construct");
23088 break;
23090 default:
23091 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
23092 c_invoke_pragma_handler (id);
23093 break;
23095 bad_stmt:
23096 cp_parser_error (parser, "expected declaration specifiers");
23097 break;
23100 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
23101 return false;
23104 /* The interface the pragma parsers have to the lexer. */
23106 enum cpp_ttype
23107 pragma_lex (tree *value)
23109 cp_token *tok;
23110 enum cpp_ttype ret;
23112 tok = cp_lexer_peek_token (the_parser->lexer);
23114 ret = tok->type;
23115 *value = tok->u.value;
23117 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
23118 ret = CPP_EOF;
23119 else if (ret == CPP_STRING)
23120 *value = cp_parser_string_literal (the_parser, false, false);
23121 else
23123 cp_lexer_consume_token (the_parser->lexer);
23124 if (ret == CPP_KEYWORD)
23125 ret = CPP_NAME;
23128 return ret;
23132 /* External interface. */
23134 /* Parse one entire translation unit. */
23136 void
23137 c_parse_file (void)
23139 static bool already_called = false;
23141 if (already_called)
23143 sorry ("inter-module optimizations not implemented for C++");
23144 return;
23146 already_called = true;
23148 the_parser = cp_parser_new ();
23149 push_deferring_access_checks (flag_access_control
23150 ? dk_no_deferred : dk_no_check);
23151 cp_parser_translation_unit (the_parser);
23152 the_parser = NULL;
23155 #include "gt-cp-parser.h"